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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
|
/**
* @file llxfer_file.cpp
* @brief implementation of LLXfer_File class for a single xfer (file)
*
* Copyright (c) 2001-$CurrentYear$, Linden Research, Inc.
* $License$
*/
#include "linden_common.h"
#if !LL_WINDOWS
#include <errno.h>
#include <unistd.h>
#endif
#include "llxfer_file.h"
#include "lluuid.h"
#include "llerror.h"
#include "llmath.h"
#include "llstring.h"
#include "lldir.h"
// size of chunks read from/written to disk
const U32 LL_MAX_XFER_FILE_BUFFER = 65536;
// local function to copy a file
S32 copy_file(const char* from, const char* to);
///////////////////////////////////////////////////////////
LLXfer_File::LLXfer_File (S32 chunk_size)
: LLXfer(chunk_size)
{
init(LLString::null, FALSE, chunk_size);
}
LLXfer_File::LLXfer_File (const LLString& local_filename, BOOL delete_local_on_completion, S32 chunk_size)
: LLXfer(chunk_size)
{
init(local_filename, delete_local_on_completion, chunk_size);
}
///////////////////////////////////////////////////////////
LLXfer_File::~LLXfer_File ()
{
free();
}
///////////////////////////////////////////////////////////
void LLXfer_File::init (const LLString& local_filename, BOOL delete_local_on_completion, S32 chunk_size)
{
mFp = NULL;
mLocalFilename[0] = 0;
mRemoteFilename[0] = 0;
mRemotePath = LL_PATH_NONE;
mTempFilename[0] = 0;
mDeleteLocalOnCompletion = FALSE;
mDeleteRemoteOnCompletion = FALSE;
if (!local_filename.empty())
{
strncpy(mLocalFilename, local_filename.c_str(), LL_MAX_PATH-1);
mLocalFilename[LL_MAX_PATH-1] = '\0'; // stupid strncpy.
// You can only automatically delete .tmp file as a safeguard against nasty messages.
mDeleteLocalOnCompletion = (delete_local_on_completion && (strstr(mLocalFilename,".tmp") == &mLocalFilename[strlen(mLocalFilename)-4])); /* Flawfinder : ignore */
}
}
///////////////////////////////////////////////////////////
void LLXfer_File::free ()
{
if (mFp)
{
fclose(mFp);
mFp = NULL;
}
LLFile::remove(mTempFilename);
if (mDeleteLocalOnCompletion)
{
lldebugs << "Removing file: " << mLocalFilename << llendl;
LLFile::remove(mLocalFilename);
}
else
{
lldebugs << "Keeping local file: " << mLocalFilename << llendl;
}
LLXfer::free();
}
///////////////////////////////////////////////////////////
S32 LLXfer_File::initializeRequest(U64 xfer_id,
const LLString& local_filename,
const LLString& remote_filename,
ELLPath remote_path,
const LLHost& remote_host,
BOOL delete_remote_on_completion,
void (*callback)(void**,S32,LLExtStat),
void** user_data)
{
S32 retval = 0; // presume success
mID = xfer_id;
strncpy(mLocalFilename, local_filename.c_str(), LL_MAX_PATH-1);
mLocalFilename[LL_MAX_PATH-1] = '\0'; // stupid strncpy.
strncpy(mRemoteFilename,remote_filename.c_str(), LL_MAX_PATH-1);
mRemoteFilename[LL_MAX_PATH-1] = '\0'; // stupid strncpy.
mRemotePath = remote_path;
mRemoteHost = remote_host;
mDeleteRemoteOnCompletion = delete_remote_on_completion;
snprintf(mTempFilename, sizeof(mTempFilename), "%s",gDirUtilp->getTempFilename().c_str()); /* Flawfinder: ignore */
mCallback = callback;
mCallbackDataHandle = user_data;
mCallbackResult = LL_ERR_NOERR;
llinfos << "Requesting xfer from " << remote_host << " for file: " << mLocalFilename << llendl;
if (mBuffer)
{
delete(mBuffer);
mBuffer = NULL;
}
mBuffer = new char[LL_MAX_XFER_FILE_BUFFER];
mBufferLength = 0;
mPacketNum = 0;
mStatus = e_LL_XFER_PENDING;
return retval;
}
///////////////////////////////////////////////////////////
S32 LLXfer_File::startDownload()
{
S32 retval = 0; // presume success
mFp = LLFile::fopen(mTempFilename,"w+b"); /* Flawfinder : ignore */
if (mFp)
{
fclose(mFp);
mFp = NULL;
gMessageSystem->newMessageFast(_PREHASH_RequestXfer);
gMessageSystem->nextBlockFast(_PREHASH_XferID);
gMessageSystem->addU64Fast(_PREHASH_ID, mID);
gMessageSystem->addStringFast(_PREHASH_Filename, mRemoteFilename);
gMessageSystem->addU8("FilePath", (U8) mRemotePath);
gMessageSystem->addBOOL("DeleteOnCompletion", mDeleteRemoteOnCompletion);
gMessageSystem->addBOOL("UseBigPackets", BOOL(mChunkSize == LL_XFER_LARGE_PAYLOAD));
gMessageSystem->addUUIDFast(_PREHASH_VFileID, LLUUID::null);
gMessageSystem->addS16Fast(_PREHASH_VFileType, -1);
gMessageSystem->sendReliable(mRemoteHost);
mStatus = e_LL_XFER_IN_PROGRESS;
}
else
{
llwarns << "Couldn't create file to be received!" << llendl;
retval = -1;
}
return (retval);
}
///////////////////////////////////////////////////////////
S32 LLXfer_File::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;
mFp = LLFile::fopen(mLocalFilename,"rb"); /* Flawfinder : ignore */
if (mFp)
{
fseek(mFp,0,SEEK_END);
S32 file_size = ftell(mFp);
if (file_size <= 0)
{
return LL_ERR_FILE_EMPTY;
}
setXferSize(file_size);
fseek(mFp,0,SEEK_SET);
}
else
{
llinfos << "Warning: " << mLocalFilename << " not found." << llendl;
return (LL_ERR_FILE_NOT_FOUND);
}
mStatus = e_LL_XFER_PENDING;
return (retval);
}
///////////////////////////////////////////////////////////
S32 LLXfer_File::getMaxBufferSize ()
{
return(LL_MAX_XFER_FILE_BUFFER);
}
///////////////////////////////////////////////////////////
S32 LLXfer_File::suck(S32 start_position)
{
S32 retval = 0;
if (mFp)
{
// grab a buffer from the right place in the file
fseek (mFp,start_position,SEEK_SET);
mBufferLength = (U32)fread(mBuffer,1,LL_MAX_XFER_FILE_BUFFER,mFp);
mBufferStartOffset = start_position;
if (feof(mFp))
{
mBufferContainsEOF = TRUE;
}
else
{
mBufferContainsEOF = FALSE;
}
}
else
{
retval = -1;
}
return (retval);
}
///////////////////////////////////////////////////////////
S32 LLXfer_File::flush()
{
S32 retval = 0;
if (mBufferLength)
{
if (mFp)
{
llerrs << "Overwriting open file pointer!" << llendl;
}
mFp = LLFile::fopen(mTempFilename,"a+b"); /* Flawfinder : ignore */
if (mFp)
{
if (fwrite(mBuffer,1,mBufferLength,mFp) != mBufferLength)
{
llwarns << "Short write" << llendl;
}
// llinfos << "******* wrote " << mBufferLength << " bytes of file xfer" << llendl;
fclose(mFp);
mFp = NULL;
mBufferLength = 0;
}
else
{
llwarns << "LLXfer_File::flush() unable to open " << mTempFilename << " for writing!" << llendl;
retval = LL_ERR_CANNOT_OPEN_FILE;
}
}
return (retval);
}
///////////////////////////////////////////////////////////
S32 LLXfer_File::processEOF()
{
S32 retval = 0;
mStatus = e_LL_XFER_COMPLETE;
S32 flushval = flush();
// If we have no other errors, our error becomes the error generated by
// flush.
if (!mCallbackResult)
{
mCallbackResult = flushval;
}
LLFile::remove(mLocalFilename);
if (!mCallbackResult)
{
if (LLFile::rename(mTempFilename,mLocalFilename))
{
#if !LL_WINDOWS
S32 error_number = errno;
llinfos << "Rename failure (" << error_number << ") - "
<< mTempFilename << " to " << mLocalFilename << llendl;
if(EXDEV == error_number)
{
if(copy_file(mTempFilename, mLocalFilename) == 0)
{
llinfos << "Rename across mounts; copying+unlinking the file instead." << llendl;
unlink(mTempFilename);
}
else
{
llwarns << "Copy failure - " << mTempFilename << " to "
<< mLocalFilename << llendl;
}
}
else
{
//FILE* fp = LLFile::fopen(mTempFilename, "r");
//llwarns << "File " << mTempFilename << " does "
// << (!fp ? "not" : "" ) << " exit." << llendl;
//if(fp) fclose(fp);
//fp = LLFile::fopen(mLocalFilename, "r");
//llwarns << "File " << mLocalFilename << " does "
// << (!fp ? "not" : "" ) << " exit." << llendl;
//if(fp) fclose(fp);
llwarns << "Rename fatally failed, can only handle EXDEV ("
<< EXDEV << ")" << llendl;
}
#else
llwarns << "Rename failure - " << mTempFilename << " to "
<< mLocalFilename << llendl;
#endif
}
}
if (mFp)
{
fclose(mFp);
mFp = NULL;
}
retval = LLXfer::processEOF();
return(retval);
}
///////////////////////////////////////////////////////////
BOOL LLXfer_File::matchesLocalFilename(const LLString& filename)
{
return (filename == mLocalFilename);
}
///////////////////////////////////////////////////////////
BOOL LLXfer_File::matchesRemoteFilename(const LLString& filename, ELLPath remote_path)
{
return ((filename == mRemoteFilename) && (remote_path == mRemotePath));
}
///////////////////////////////////////////////////////////
const char * LLXfer_File::getName()
{
return (mLocalFilename);
}
///////////////////////////////////////////////////////////
// hacky - doesn't matter what this is
// as long as it's different from the other classes
U32 LLXfer_File::getXferTypeTag()
{
return LLXfer::XFER_FILE;
}
///////////////////////////////////////////////////////////
#if !LL_WINDOWS
// This is really close to, but not quite a general purpose copy
// function. It does not really spam enough information, but is useful
// for this cpp file, because this should never be called in a
// production environment.
S32 copy_file(const char* from, const char* to)
{
S32 rv = 0;
FILE* in = LLFile::fopen(from, "rb"); /*Flawfinder: ignore*/
FILE* out = LLFile::fopen(to, "wb"); /*Flawfinder: ignore*/
if(in && out)
{
S32 read = 0;
const S32 COPY_BUFFER_SIZE = 16384;
U8 buffer[COPY_BUFFER_SIZE];
while(((read = fread(buffer, 1, sizeof(buffer), in)) > 0)
&& (fwrite(buffer, 1, read, out) == (U32)read)); /* Flawfinder : ignore */
if(ferror(in) || ferror(out)) rv = -2;
}
else
{
rv = -1;
}
if(in) fclose(in);
if(out) fclose(out);
return rv;
}
#endif
|