PageRenderTime 32ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llmessage/lltransfersourcefile.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 174 lines | 113 code | 27 blank | 34 comment | 16 complexity | 5b053788eaa45903f54cce005b17c7a6 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file lltransfersourcefile.cpp
  3. * @brief Transfer system for sending a file.
  4. *
  5. * $LicenseInfo:firstyear=2006&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #include "linden_common.h"
  27. #include "lltransfersourcefile.h"
  28. #include "llerror.h"
  29. #include "message.h"
  30. #include "lldatapacker.h"
  31. #include "lldir.h"
  32. LLTransferSourceFile::LLTransferSourceFile(const LLUUID &request_id, const F32 priority) :
  33. LLTransferSource(LLTST_FILE, request_id, priority),
  34. mFP(NULL)
  35. {
  36. }
  37. LLTransferSourceFile::~LLTransferSourceFile()
  38. {
  39. if (mFP)
  40. {
  41. llerrs << "Destructor called without the completion callback being called!" << llendl;
  42. }
  43. }
  44. void LLTransferSourceFile::initTransfer()
  45. {
  46. std::string filename = mParams.getFilename();
  47. std::string delimiter = gDirUtilp->getDirDelimiter();
  48. if((filename == ".")
  49. || (filename == "..")
  50. || (filename.find(delimiter[0]) != std::string::npos))
  51. {
  52. llwarns << "Attempting to transfer file " << filename << " with path delimiter, aborting!" << llendl;
  53. sendTransferStatus(LLTS_ERROR);
  54. return;
  55. }
  56. // Look for the file.
  57. mFP = LLFile::fopen(mParams.getFilename(), "rb"); /* Flawfinder: ignore */
  58. if (!mFP)
  59. {
  60. sendTransferStatus(LLTS_ERROR);
  61. return;
  62. }
  63. // Get the size of the file using the hack from
  64. fseek(mFP,0,SEEK_END);
  65. mSize = ftell(mFP);
  66. fseek(mFP,0,SEEK_SET);
  67. sendTransferStatus(LLTS_OK);
  68. }
  69. F32 LLTransferSourceFile::updatePriority()
  70. {
  71. return 0.f;
  72. }
  73. LLTSCode LLTransferSourceFile::dataCallback(const S32 packet_id,
  74. const S32 max_bytes,
  75. U8 **data_handle,
  76. S32 &returned_bytes,
  77. BOOL &delete_returned)
  78. {
  79. //llinfos << "LLTransferSourceFile::dataCallback" << llendl;
  80. if (!mFP)
  81. {
  82. llerrs << "Data callback without file set!" << llendl;
  83. return LLTS_ERROR;
  84. }
  85. if (packet_id != mLastPacketID + 1)
  86. {
  87. llerrs << "Can't handle out of order file transfer yet!" << llendl;
  88. }
  89. // Grab up until the max number of bytes from the file.
  90. delete_returned = TRUE;
  91. U8 *tmpp = new U8[max_bytes];
  92. *data_handle = tmpp;
  93. returned_bytes = (S32)fread(tmpp, 1, max_bytes, mFP);
  94. if (!returned_bytes)
  95. {
  96. delete[] tmpp;
  97. *data_handle = NULL;
  98. returned_bytes = 0;
  99. delete_returned = FALSE;
  100. return LLTS_DONE;
  101. }
  102. return LLTS_OK;
  103. }
  104. void LLTransferSourceFile::completionCallback(const LLTSCode status)
  105. {
  106. // No matter what happens, all we want to do is close the file pointer if
  107. // we've got it open.
  108. if (mFP)
  109. {
  110. fclose(mFP);
  111. mFP = NULL;
  112. }
  113. // Delete the file iff the filename begins with "TEMP"
  114. if (mParams.getDeleteOnCompletion() && mParams.getFilename().substr(0, 4) == "TEMP")
  115. {
  116. LLFile::remove(mParams.getFilename());
  117. }
  118. }
  119. void LLTransferSourceFile::packParams(LLDataPacker& dp) const
  120. {
  121. //llinfos << "LLTransferSourceFile::packParams" << llendl;
  122. mParams.packParams(dp);
  123. }
  124. BOOL LLTransferSourceFile::unpackParams(LLDataPacker &dp)
  125. {
  126. //llinfos << "LLTransferSourceFile::unpackParams" << llendl;
  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. }