lltransfersourcefile.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**
  2. * @file lltransfersourcefile.cpp
  3. * @brief Transfer system for sending a file.
  4. *
  5. * $LicenseInfo:firstyear=2006&license=viewergpl$
  6. *
  7. * Copyright (c) 2006-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "linden_common.h"
  33. #include "lltransfersourcefile.h"
  34. #include "lldatapacker.h"
  35. #include "lldir.h"
  36. #include "llmessage.h"
  37. LLTransferSourceFile::LLTransferSourceFile(const LLUUID& request_id, F32 prio)
  38. : LLTransferSource(LLTST_FILE, request_id, prio),
  39. mFP(NULL)
  40. {
  41. }
  42. LLTransferSourceFile::~LLTransferSourceFile()
  43. {
  44. if (mFP)
  45. {
  46. llerrs << "Destructor called without the completion callback being called !"
  47. << llendl;
  48. }
  49. }
  50. void LLTransferSourceFile::initTransfer()
  51. {
  52. std::string filename = mParams.getFilename();
  53. std::string delimiter = LL_DIR_DELIM_STR;
  54. if (filename == "." || filename == ".." ||
  55. filename.find(delimiter[0]) != std::string::npos)
  56. {
  57. llwarns << "Attempting to transfer file " << filename
  58. << " with path delimiter, aborting!" << llendl;
  59. sendTransferStatus(LLTS_ERROR);
  60. return;
  61. }
  62. // Look for the file.
  63. mFP = LLFile::open(mParams.getFilename(), "rb");
  64. if (!mFP)
  65. {
  66. sendTransferStatus(LLTS_ERROR);
  67. return;
  68. }
  69. // Get the size of the file using the hack from
  70. fseek(mFP,0,SEEK_END);
  71. mSize = ftell(mFP);
  72. fseek(mFP,0,SEEK_SET);
  73. sendTransferStatus(LLTS_OK);
  74. }
  75. LLTSCode LLTransferSourceFile::dataCallback(S32 packet_id, S32 max_bytes,
  76. U8** data_handle,
  77. S32& returned_bytes,
  78. bool& delete_returned)
  79. {
  80. //llinfos << "LLTransferSourceFile::dataCallback" << llendl;
  81. if (!mFP)
  82. {
  83. llerrs << "Data callback without file set !" << llendl;
  84. return LLTS_ERROR;
  85. }
  86. if (packet_id != mLastPacketID + 1)
  87. {
  88. llerrs << "Cannot handle out of order file transfer yet !" << llendl;
  89. }
  90. // Grab up until the max number of bytes from the file.
  91. delete_returned = true;
  92. U8* tmpp = new U8[max_bytes];
  93. *data_handle = tmpp;
  94. returned_bytes = (S32)fread(tmpp, 1, max_bytes, mFP);
  95. if (!returned_bytes)
  96. {
  97. delete[] tmpp;
  98. *data_handle = NULL;
  99. returned_bytes = 0;
  100. delete_returned = false;
  101. return LLTS_DONE;
  102. }
  103. return LLTS_OK;
  104. }
  105. void LLTransferSourceFile::completionCallback(LLTSCode status)
  106. {
  107. // No matter what happens, all we want to do is close the file pointer if
  108. // we have got it open.
  109. if (mFP)
  110. {
  111. LLFile::close(mFP);
  112. mFP = NULL;
  113. }
  114. // Delete the file iff the filename begins with "TEMP"
  115. if (mParams.getDeleteOnCompletion() &&
  116. mParams.getFilename().substr(0, 4) == "TEMP")
  117. {
  118. LLFile::remove(mParams.getFilename());
  119. }
  120. }
  121. void LLTransferSourceFile::packParams(LLDataPacker& dp) const
  122. {
  123. mParams.packParams(dp);
  124. }
  125. bool LLTransferSourceFile::unpackParams(LLDataPacker& dp)
  126. {
  127. return mParams.unpackParams(dp);
  128. }
  129. LLTransferSourceParamsFile::LLTransferSourceParamsFile()
  130. : LLTransferSourceParams(LLTST_FILE),
  131. mDeleteOnCompletion(false)
  132. {
  133. }
  134. void LLTransferSourceParamsFile::packParams(LLDataPacker& dp) const
  135. {
  136. dp.packString(mFilename, "Filename");
  137. dp.packU8((U8)mDeleteOnCompletion, "Delete");
  138. }
  139. bool LLTransferSourceParamsFile::unpackParams(LLDataPacker& dp)
  140. {
  141. dp.unpackString(mFilename, "Filename");
  142. U8 delete_flag;
  143. dp.unpackU8(delete_flag, "Delete");
  144. mDeleteOnCompletion = delete_flag;
  145. llinfos << "Unpacked filename: " << mFilename << llendl;
  146. return true;
  147. }