00001
00032 #include "linden_common.h"
00033
00034 #include "lltransfersourcefile.h"
00035
00036 #include "llerror.h"
00037 #include "message.h"
00038 #include "lldatapacker.h"
00039 #include "lldir.h"
00040
00041 LLTransferSourceFile::LLTransferSourceFile(const LLUUID &request_id, const F32 priority) :
00042 LLTransferSource(LLTST_FILE, request_id, priority),
00043 mFP(NULL)
00044 {
00045 }
00046
00047 LLTransferSourceFile::~LLTransferSourceFile()
00048 {
00049 if (mFP)
00050 {
00051 llerrs << "Destructor called without the completion callback being called!" << llendl;
00052 }
00053 }
00054
00055 void LLTransferSourceFile::initTransfer()
00056 {
00057 std::string filename = mParams.getFilename();
00058 std::string delimiter = gDirUtilp->getDirDelimiter();
00059
00060 if((filename == ".")
00061 || (filename == "..")
00062 || (filename.find(delimiter[0]) != std::string::npos))
00063 {
00064 llwarns << "Attempting to transfer file " << filename << " with path delimiter, aborting!" << llendl;
00065
00066 sendTransferStatus(LLTS_ERROR);
00067 return;
00068 }
00069
00070 mFP = LLFile::fopen(mParams.getFilename().c_str(), "rb");
00071 if (!mFP)
00072 {
00073 sendTransferStatus(LLTS_ERROR);
00074 return;
00075 }
00076
00077
00078 fseek(mFP,0,SEEK_END);
00079 mSize = ftell(mFP);
00080 fseek(mFP,0,SEEK_SET);
00081
00082 sendTransferStatus(LLTS_OK);
00083 }
00084
00085 F32 LLTransferSourceFile::updatePriority()
00086 {
00087 return 0.f;
00088 }
00089
00090 LLTSCode LLTransferSourceFile::dataCallback(const S32 packet_id,
00091 const S32 max_bytes,
00092 U8 **data_handle,
00093 S32 &returned_bytes,
00094 BOOL &delete_returned)
00095 {
00096
00097
00098 if (!mFP)
00099 {
00100 llerrs << "Data callback without file set!" << llendl;
00101 return LLTS_ERROR;
00102 }
00103
00104 if (packet_id != mLastPacketID + 1)
00105 {
00106 llerrs << "Can't handle out of order file transfer yet!" << llendl;
00107 }
00108
00109
00110 delete_returned = TRUE;
00111 U8 *tmpp = new U8[max_bytes];
00112 *data_handle = tmpp;
00113 returned_bytes = (S32)fread(tmpp, 1, max_bytes, mFP);
00114 if (!returned_bytes)
00115 {
00116 delete[] tmpp;
00117 *data_handle = NULL;
00118 returned_bytes = 0;
00119 delete_returned = FALSE;
00120 return LLTS_DONE;
00121 }
00122
00123 return LLTS_OK;
00124 }
00125
00126 void LLTransferSourceFile::completionCallback(const LLTSCode status)
00127 {
00128
00129
00130 if (mFP)
00131 {
00132 fclose(mFP);
00133 mFP = NULL;
00134
00135 }
00136
00137 if (mParams.getDeleteOnCompletion() && memcmp(mParams.getFilename().c_str(), "TEMP", 4) == 0)
00138 {
00139 LLFile::remove(mParams.getFilename().c_str());
00140 }
00141 }
00142
00143 void LLTransferSourceFile::packParams(LLDataPacker& dp) const
00144 {
00145
00146 mParams.packParams(dp);
00147 }
00148
00149 BOOL LLTransferSourceFile::unpackParams(LLDataPacker &dp)
00150 {
00151
00152 return mParams.unpackParams(dp);
00153 }
00154
00155
00156 LLTransferSourceParamsFile::LLTransferSourceParamsFile() : LLTransferSourceParams(LLTST_FILE)
00157 {
00158 }
00159
00160
00161 void LLTransferSourceParamsFile::packParams(LLDataPacker &dp) const
00162 {
00163 dp.packString(mFilename.c_str(), "Filename");
00164 dp.packU8((U8)mDeleteOnCompletion, "Delete");
00165 }
00166
00167
00168 BOOL LLTransferSourceParamsFile::unpackParams(LLDataPacker &dp)
00169 {
00170 dp.unpackString(mFilename, "Filename");
00171 U8 delete_flag;
00172 dp.unpackU8(delete_flag, "Delete");
00173 mDeleteOnCompletion = delete_flag;
00174
00175 llinfos << "Unpacked filename: " << mFilename << llendl;
00176 return TRUE;
00177 }