lltransfertargetvfile.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /**
  2. * @file lltransfertargetvfile.cpp
  3. * @brief Transfer system for receiving a vfile.
  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 "lltransfertargetvfile.h"
  34. #include "lldatapacker.h"
  35. #include "llfilesystem.h"
  36. //static
  37. void LLTransferTargetVFile::updateQueue(bool shutdown)
  38. {
  39. }
  40. LLTransferTargetParamsVFile::LLTransferTargetParamsVFile()
  41. : LLTransferTargetParams(LLTTT_VFILE),
  42. mAssetType(LLAssetType::AT_NONE),
  43. mCompleteCallback(NULL),
  44. mRequestDatap(NULL),
  45. mErrCode(0)
  46. {
  47. }
  48. void LLTransferTargetParamsVFile::setAsset(const LLUUID& asset_id,
  49. LLAssetType::EType asset_type)
  50. {
  51. mAssetID = asset_id;
  52. mAssetType = asset_type;
  53. }
  54. void LLTransferTargetParamsVFile::setCallback(LLTTVFCompleteCallback cb,
  55. LLBaseDownloadRequest& request)
  56. {
  57. mCompleteCallback = cb;
  58. if (mRequestDatap)
  59. {
  60. delete mRequestDatap;
  61. }
  62. mRequestDatap = request.getCopy();
  63. }
  64. bool LLTransferTargetParamsVFile::unpackParams(LLDataPacker& dp)
  65. {
  66. // if the source provided a new key, assign that to the asset id.
  67. if (dp.hasNext())
  68. {
  69. LLUUID dummy_id;
  70. dp.unpackUUID(dummy_id, "AgentID");
  71. dp.unpackUUID(dummy_id, "SessionID");
  72. dp.unpackUUID(dummy_id, "OwnerID");
  73. dp.unpackUUID(dummy_id, "TaskID");
  74. dp.unpackUUID(dummy_id, "ItemID");
  75. dp.unpackUUID(mAssetID, "AssetID");
  76. S32 dummy_type;
  77. dp.unpackS32(dummy_type, "AssetType");
  78. }
  79. // if we never got an asset id, this will always fail.
  80. if (mAssetID.isNull())
  81. {
  82. return false;
  83. }
  84. return true;
  85. }
  86. LLTransferTargetVFile::LLTransferTargetVFile(const LLUUID& uuid,
  87. LLTransferSourceType src_type)
  88. : LLTransferTarget(LLTTT_VFILE, uuid, src_type),
  89. mNeedsCreate(true)
  90. {
  91. mTempID.generate();
  92. }
  93. LLTransferTargetVFile::~LLTransferTargetVFile()
  94. {
  95. if (mParams.mRequestDatap)
  96. {
  97. // *TODO: consider doing it in LLTransferTargetParamsVFile's destructor
  98. delete mParams.mRequestDatap;
  99. mParams.mRequestDatap = NULL;
  100. }
  101. }
  102. // virtual
  103. bool LLTransferTargetVFile::unpackParams(LLDataPacker& dp)
  104. {
  105. if (LLTST_SIM_INV_ITEM == mSourceType)
  106. {
  107. return mParams.unpackParams(dp);
  108. }
  109. return true;
  110. }
  111. void LLTransferTargetVFile::applyParams(const LLTransferTargetParams& params)
  112. {
  113. if (params.getType() != mType)
  114. {
  115. llwarns << "Target parameter type doesn't match!" << llendl;
  116. return;
  117. }
  118. mParams = (LLTransferTargetParamsVFile&)params;
  119. }
  120. LLTSCode LLTransferTargetVFile::dataCallback(S32 packet_id, U8* in_datap,
  121. S32 in_size)
  122. {
  123. if (!gAssetStoragep)
  124. {
  125. llwarns << "Aborting vfile transfer after asset storage shut down !"
  126. << llendl;
  127. return LLTS_ERROR;
  128. }
  129. LLFileSystem vf(mTempID, LLFileSystem::APPEND);
  130. if (mNeedsCreate)
  131. {
  132. mNeedsCreate = false;
  133. }
  134. if (!in_size)
  135. {
  136. return LLTS_OK;
  137. }
  138. if (!vf.write(in_datap, in_size))
  139. {
  140. llwarns << "Failure in data callback !" << llendl;
  141. return LLTS_ERROR;
  142. }
  143. return LLTS_OK;
  144. }
  145. void LLTransferTargetVFile::completionCallback(LLTSCode status)
  146. {
  147. //llinfos << "LLTransferTargetVFile::completionCallback" << llendl;
  148. if (!gAssetStoragep)
  149. {
  150. llwarns << "Aborting vfile transfer after asset storage shut down !"
  151. << llendl;
  152. return;
  153. }
  154. // Still need to gracefully handle error conditions.
  155. S32 err_code = 0;
  156. switch (status)
  157. {
  158. case LLTS_DONE:
  159. {
  160. if (!mNeedsCreate)
  161. {
  162. if (!LLFileSystem::renameFile(mTempID, mParams.getAssetID()))
  163. {
  164. llwarns << "Rename of cache file from temp asset Id "
  165. << mTempID << " to asset Id "
  166. << mParams.getAssetID() << " failed." << llendl;
  167. }
  168. }
  169. err_code = LL_ERR_NOERR;
  170. LL_DEBUGS("FileTransfer") << "Callback completed for "
  171. << mParams.getAssetID() << ","
  172. << LLAssetType::lookup(mParams.getAssetType())
  173. << " with temp id " << mTempID
  174. << LL_ENDL;
  175. break;
  176. }
  177. case LLTS_ERROR:
  178. case LLTS_ABORT:
  179. case LLTS_UNKNOWN_SOURCE:
  180. default:
  181. {
  182. // We are aborting this transfer, we do not want to keep this file.
  183. llwarns << "Aborting vfile transfer for " << mParams.getAssetID()
  184. << llendl;
  185. LLFileSystem vf(mTempID, LLFileSystem::APPEND);
  186. vf.remove();
  187. }
  188. }
  189. switch (status)
  190. {
  191. case LLTS_DONE:
  192. err_code = LL_ERR_NOERR;
  193. break;
  194. case LLTS_UNKNOWN_SOURCE:
  195. err_code = LL_ERR_ASSET_REQUEST_NOT_IN_DATABASE;
  196. break;
  197. case LLTS_INSUFFICIENT_PERMISSIONS:
  198. err_code = LL_ERR_INSUFFICIENT_PERMISSIONS;
  199. break;
  200. case LLTS_ERROR:
  201. case LLTS_ABORT:
  202. default:
  203. err_code = LL_ERR_ASSET_REQUEST_FAILED;
  204. }
  205. if (mParams.mRequestDatap)
  206. {
  207. if (mParams.mCompleteCallback)
  208. {
  209. mParams.mCompleteCallback(err_code, mParams.getAssetID(),
  210. mParams.getAssetType(),
  211. mParams.mRequestDatap, LLExtStat::NONE);
  212. }
  213. delete mParams.mRequestDatap;
  214. mParams.mRequestDatap = NULL;
  215. }
  216. }