summaryrefslogtreecommitdiff
path: root/indra/llmessage/llxfer_vfile.cpp
blob: 5030556eb034a5994c0e1995558a5007b0c18ca2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/** 
 * @file llxfer_vfile.cpp
 * @brief implementation of LLXfer_VFile class for a single xfer (vfile).
 *
 * Copyright (c) 2002-$CurrentYear$, Linden Research, Inc.
 * $License$
 */

#include "linden_common.h"

#include "llxfer_vfile.h"
#include "lluuid.h"
#include "llerror.h"
#include "llmath.h"
#include "llvfile.h"
#include "llvfs.h"
#include "lldir.h"

// size of chunks read from/written to disk
const U32 LL_MAX_XFER_FILE_BUFFER = 65536;

///////////////////////////////////////////////////////////

LLXfer_VFile::LLXfer_VFile ()
: LLXfer(-1)
{
	init(NULL, LLUUID::null, LLAssetType::AT_NONE);
}

LLXfer_VFile::LLXfer_VFile (LLVFS *vfs, const LLUUID &local_id, LLAssetType::EType type)
: LLXfer(-1)
{
	init(vfs, local_id, type);
}

///////////////////////////////////////////////////////////

LLXfer_VFile::~LLXfer_VFile ()
{
	free();
}

///////////////////////////////////////////////////////////

void LLXfer_VFile::init (LLVFS *vfs, const LLUUID &local_id, LLAssetType::EType type)
{

	mVFS = vfs;
	mLocalID = local_id;
	mType = type;

	mVFile = NULL;

	char id_string[UUID_STR_LENGTH];			/* Flawfinder : ignore */
	mLocalID.toString(id_string);

	snprintf(mName, sizeof(mName), "VFile %s:%s", id_string, LLAssetType::lookup(mType));		/* Flawfinder : ignore */
}
	
///////////////////////////////////////////////////////////

void LLXfer_VFile::free ()
{
	LLVFile file(mVFS, mTempID, mType, LLVFile::WRITE);
	file.remove();

	delete mVFile;
	mVFile = NULL;

	LLXfer::free();
}

///////////////////////////////////////////////////////////

S32 LLXfer_VFile::initializeRequest(U64 xfer_id,
									LLVFS* vfs,
									const LLUUID& local_id,
									const LLUUID& remote_id,
									LLAssetType::EType type,
									const LLHost& remote_host,
									void (*callback)(void**,S32),
									void** user_data)
{
 	S32 retval = 0;  // presume success
	
	mRemoteHost = remote_host;

	mVFS = vfs;
	mLocalID = local_id;
	mRemoteID = remote_id;
	mType = type;

	mID = xfer_id;
	mCallback = callback;
	mCallbackDataHandle = user_data;
	mCallbackResult = LL_ERR_NOERR;

	char id_string[UUID_STR_LENGTH];		/* Flawfinder : ignore */
	mLocalID.toString(id_string);

	snprintf(mName, sizeof(mName), "VFile %s:%s", id_string, LLAssetType::lookup(mType));		/* Flawfinder : ignore */

	llinfos << "Requesting " << mName << llendl;

	if (mBuffer)
	{
		delete[] mBuffer;
		mBuffer = NULL;
	}

	mBuffer = new char[LL_MAX_XFER_FILE_BUFFER];

	mBufferLength = 0;
	mPacketNum = 0;
	mTempID.generate();
 	mStatus = e_LL_XFER_PENDING;
	return retval;
}

//////////////////////////////////////////////////////////

S32 LLXfer_VFile::startDownload()
{
 	S32 retval = 0;  // presume success
	LLVFile file(mVFS, mTempID, mType, LLVFile::APPEND);

	gMessageSystem->newMessageFast(_PREHASH_RequestXfer);
	gMessageSystem->nextBlockFast(_PREHASH_XferID);
	gMessageSystem->addU64Fast(_PREHASH_ID, mID);
	gMessageSystem->addStringFast(_PREHASH_Filename, "");
	gMessageSystem->addU8("FilePath", (U8) LL_PATH_NONE);
	gMessageSystem->addBOOL("DeleteOnCompletion", FALSE);
	gMessageSystem->addBOOL("UseBigPackets", BOOL(mChunkSize == LL_XFER_LARGE_PAYLOAD));
	gMessageSystem->addUUIDFast(_PREHASH_VFileID, mRemoteID);
	gMessageSystem->addS16Fast(_PREHASH_VFileType, (S16)mType);

	gMessageSystem->sendReliable(mRemoteHost);		
	mStatus = e_LL_XFER_IN_PROGRESS;

	return (retval);
}

///////////////////////////////////////////////////////////

S32 LLXfer_VFile::startSend (U64 xfer_id, const LLHost &remote_host)
{
	S32 retval = LL_ERR_NOERR;  // presume success
	
    mRemoteHost = remote_host;
	mID = xfer_id;
   	mPacketNum = -1;

//	cout << "Sending file: " << mLocalFilename << endl;

	delete [] mBuffer;
	mBuffer = new char[LL_MAX_XFER_FILE_BUFFER];

	mBufferLength = 0;
	mBufferStartOffset = 0;	
	
	delete mVFile;
	mVFile = NULL;
	if(mVFS->getExists(mLocalID, mType))
	{
		mVFile = new LLVFile(mVFS, mLocalID, mType, LLVFile::READ);

		if (mVFile->getSize() <= 0)
		{
			delete mVFile;
			mVFile = NULL;

			return LL_ERR_FILE_EMPTY;
		}
	}

	if(mVFile)
	{
		setXferSize(mVFile->getSize());
		mStatus = e_LL_XFER_PENDING;
	}
	else
	{
		retval = LL_ERR_FILE_NOT_FOUND;
	}

	return (retval);
}

///////////////////////////////////////////////////////////
void LLXfer_VFile::setXferSize (S32 xfer_size)
{	
	LLXfer::setXferSize(xfer_size);

	// Don't do this on the server side, where we have a persistent mVFile
	// It would be nice if LLXFers could tell which end of the pipe they were
	if (! mVFile)
	{
		LLVFile file(mVFS, mTempID, mType, LLVFile::APPEND);
		file.setMaxSize(xfer_size);
	}
}

///////////////////////////////////////////////////////////

S32 LLXfer_VFile::getMaxBufferSize ()
{
	return(LL_MAX_XFER_FILE_BUFFER);
}

///////////////////////////////////////////////////////////

S32 LLXfer_VFile::suck(S32 start_position)
{
	S32 retval = 0;

	if (mVFile)
	{
		// grab a buffer from the right place in the file
		if (! mVFile->seek(start_position, 0))
		{
			llwarns << "VFile Xfer Can't seek to position " << start_position << ", file length " << mVFile->getSize() << llendl;
			llwarns << "While sending file " << mLocalID << llendl;
			return -1;
		}
		
		if (mVFile->read((U8*)mBuffer, LL_MAX_XFER_FILE_BUFFER))		/* Flawfinder : ignore */
		{
			mBufferLength = mVFile->getLastBytesRead();
			mBufferStartOffset = start_position;

			mBufferContainsEOF = mVFile->eof();
		}
		else
		{
			retval = -1;
		}
	}
	else
	{
		retval = -1;
	}

	return (retval);
}

///////////////////////////////////////////////////////////

S32 LLXfer_VFile::flush()
{
	S32 retval = 0;
	if (mBufferLength)
	{
		LLVFile file(mVFS, mTempID, mType, LLVFile::APPEND);

		file.write((U8*)mBuffer, mBufferLength);
			
		mBufferLength = 0;
	}
	return (retval);
}

///////////////////////////////////////////////////////////

S32 LLXfer_VFile::processEOF()
{
	S32 retval = 0;
	mStatus = e_LL_XFER_COMPLETE;

	flush();

	if (!mCallbackResult)
	{
		LLVFile file(mVFS, mTempID, mType, LLVFile::WRITE);
		if (! file.rename(mLocalID, mType))
		{
			llinfos << "copy from temp file failed: unable to rename to " << mLocalID << llendl;
		}

	}

	if (mVFile)
	{
		delete mVFile;
		mVFile = NULL;
	}

	retval = LLXfer::processEOF();

	return(retval);
}

////////////////////////////////////////////////////////////

BOOL LLXfer_VFile::matchesLocalFile(const LLUUID &id, LLAssetType::EType type)
{
	return (id == mLocalID && type == mType);
}

//////////////////////////////////////////////////////////

BOOL LLXfer_VFile::matchesRemoteFile(const LLUUID &id, LLAssetType::EType type)
{
	return (id == mRemoteID && type == mType);
}

//////////////////////////////////////////////////////////

const char * LLXfer_VFile::getName() 
{
	return mName;
}

//////////////////////////////////////////////////////////

// hacky - doesn't matter what this is
// as long as it's different from the other classes
U32 LLXfer_VFile::getXferTypeTag()
{
	return LLXfer::XFER_VFILE;
}