forked from kolosy/rsync.net
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Receiver.cs
490 lines (438 loc) · 15.6 KB
/
Receiver.cs
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/**
* Copyright (C) 2006 Alex Pedenko
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
using System;
using System.Collections.Generic;
using System.IO;
namespace NetSync
{
public class Receiver
{
private Options options;
private CheckSum checkSum;
public Receiver(Options opt)
{
options = opt;
checkSum = new CheckSum(options);
}
private string LocalizePath(ClientInfo cInfo, string path)
{
string normalized = cInfo.Options.dir.Replace('\\', '/').Replace(":", String.Empty).ToLower();
string ret = String.Empty;
if (path.ToLower().IndexOf(normalized) != -1)
{
ret = path.Substring(path.ToLower().IndexOf(normalized) + normalized.Length).Replace('/', Path.DirectorySeparatorChar);
}
if (ret == String.Empty)
{
return path.TrimEnd('\\');
}
if (ret[0] == Path.DirectorySeparatorChar)
{
ret = ret.Substring(1);
}
return ret;
}
public int ReceiveFiles(ClientInfo clientInfo, List<FileStruct> fileList, string localName)
{
FStat st = new FStat();
FileStruct file;
IOStream ioStream = clientInfo.IoStream;
string fileName;
string fNameCmp = String.Empty, fNameTmp = String.Empty;
bool saveMakeBackups = options.makeBackups;
int i, phase = 0;
bool recv_ok;
if (options.verbose > 2)
{
Log.WriteLine("ReceiveFiles(" + fileList.Count + ") starting");
}
while (true)
{
i = ioStream.readInt();
if (i == -1)
{
if (phase != 0)
{
break;
}
phase = 1;
checkSum.length = CheckSum.SUM_LENGTH;
if (options.verbose > 2)
{
Log.WriteLine("ReceiveFiles phase=" + phase);
}
ioStream.writeInt(0); //send_msg DONE
if (options.keepPartial)
{
options.makeBackups = false;
}
continue;
}
if (i < 0 || i >= fileList.Count)
{
MainClass.Exit("Invalid file index " + i + " in receiveFiles (count=" + fileList.Count + ")", clientInfo);
}
file = (fileList[i]);
Options.stats.currentFileIndex = i;
Options.stats.numTransferredFiles++;
Options.stats.totalTransferredSize += file.length;
if (localName != null && localName.CompareTo(String.Empty) != 0)
{
fileName = localName;
}
else
{
fileName = Path.Combine(options.dir, LocalizePath(clientInfo, file.GetFullName().Replace(":", String.Empty)).Replace("\\", "/"));
//fileName = Path.Combine(options.dir, file.FNameTo().Replace(":",String.Empty)).Replace("\\", "/");
// TODO: path length
Directory.CreateDirectory(Path.Combine(options.dir, LocalizePath(clientInfo, file.dirName.Replace(":", String.Empty))).Replace("\\", "/"));
Log.WriteLine(Path.Combine(options.dir, file.dirName));
//FileSystem.Directory.CreateDirectory(Path.Combine(options.dir,file.dirName.Replace(":",String.Empty)).Replace("\\", "/"));
}
if (options.dryRun)
{
if (!options.amServer && options.verbose > 0)
{
Log.WriteLine(fileName);
}
continue;
}
if (options.verbose > 2)
{
Log.WriteLine("receiveFiles(" + fileName + ")");
}
if (options.partialDir != null && options.partialDir.CompareTo(String.Empty) != 0)
{
}
else
{
fNameCmp = fileName;
}
FileStream fd1 = null;
try
{
fd1 = new FileStream(fNameCmp, FileMode.Open, FileAccess.Read);
}
catch (FileNotFoundException)
{
fNameCmp = fileName;
try
{
fd1 = new FileStream(fNameCmp, FileMode.Open, FileAccess.Read);
}
catch (FileNotFoundException)
{
}
}
catch (Exception e)
{
Log.Write(e.Message);
}
try
{
FileInfo fi = new FileInfo(fNameCmp);
// TODO: path length
st.size = fi.Length;
}
catch { }
String tempFileName = getTmpName(fileName);
FileStream fd2 = null;
fd2 = new FileStream(tempFileName, FileMode.OpenOrCreate, FileAccess.Write);
if (!options.amServer && options.verbose > 0)
{
Log.WriteLine(fileName);
}
/* recv file data */
recv_ok = ReceiveData(clientInfo, fNameCmp, fd1, st.size,
fileName, fd2, file.length);
if (fd1 != null)
{
fd1.Close();
}
if (fd2 != null)
{
fd2.Close();
}
// TODO: path length
File.Copy(tempFileName, fileName, true);
// TODO: path length
File.Delete(tempFileName);
if (recv_ok || options.inplace)
{
FinishTransfer(fileName, fNameTmp, file, recv_ok);
}
}
options.makeBackups = saveMakeBackups;
if (options.deleteAfter && options.recurse && localName == null && fileList.Count > 0)
{
DeleteFiles(fileList);
}
if (options.verbose > 2)
{
Log.WriteLine("ReceiveFiles finished");
}
return 0;
}
public bool ReceiveData(ClientInfo clientInfo, string fileNameR, Stream fdR, long sizeR, string fileName, Stream fd, int totalSize)
{
IOStream f = clientInfo.IoStream;
byte[] fileSum1 = new byte[CheckSum.MD4_SUM_LENGTH];
byte[] fileSum2 = new byte[CheckSum.MD4_SUM_LENGTH];
byte[] data = new byte[Match.CHUNK_SIZE];
SumStruct sumStruct = new SumStruct();
MapFile mapBuf = null;
Sender sender = new Sender(options);
sender.ReadSumHead(clientInfo, ref sumStruct);
int offset = 0;
UInt32 len;
if (fdR != null && sizeR > 0)
{
int mapSize = (int)Math.Max(sumStruct.bLength * 2, 16 * 1024);
mapBuf = new MapFile(fdR, (int)sizeR, mapSize, (int)sumStruct.bLength);
if (options.verbose > 2)
{
Log.WriteLine("recv mapped " + fileNameR + " of size " + sizeR);
}
}
Sum sum = new Sum(options);
sum.Init(options.checksumSeed);
int i;
Token token = new Token(options);
while ((i = token.ReceiveToken(f, ref data, 0)) != 0)
{
if (options.doProgress)
{
Progress.ShowProgress(offset, totalSize);
}
if (i > 0)
{
if (options.verbose > 3)
{
Log.WriteLine("data recv " + i + " at " + offset);
}
Options.stats.literalData += i;
sum.Update(data, 0, i);
if (fd != null && FileIO.WriteFile(fd, data, 0, i) != i)
{
goto report_write_error;
}
offset += i;
continue;
}
i = -(i + 1);
int offset2 = (int)(i * sumStruct.bLength);
len = sumStruct.bLength;
if (i == sumStruct.count - 1 && sumStruct.remainder != 0)
{
len = sumStruct.remainder;
}
Options.stats.matchedData += len;
if (options.verbose > 3)
{
Log.WriteLine("chunk[" + i + "] of size " + len + " at " + offset2 + " offset=" + offset);
}
byte[] map = null;
int off = 0;
if (mapBuf != null)
{
off = mapBuf.MapPtr(offset2, (int)len);
map = mapBuf.p;
token.SeeToken(map, offset, (int)len);
sum.Update(map, off, (int)len);
}
if (options.inplace)
{
if (offset == offset2 && fd != null)
{
offset += (int)len;
if (fd.Seek(len, SeekOrigin.Current) != offset)
{
MainClass.Exit("seek failed on " + Util.fullFileName(fileName), clientInfo);
}
continue;
}
}
if (fd != null && FileIO.WriteFile(fd, map, off, (int)len) != (int)len)
{
goto report_write_error;
}
offset += (int)len;
}
if (options.doProgress)
{
Progress.EndProgress(totalSize);
}
if (fd != null && offset > 0 && FileIO.SparseEnd(fd) != 0)
{
MainClass.Exit("write failed on " + Util.fullFileName(fileName), clientInfo);
}
fileSum1 = sum.End();
if (mapBuf != null)
{
mapBuf = null;
}
fileSum2 = f.ReadBuffer(CheckSum.MD4_SUM_LENGTH);
if (options.verbose > 2)
{
Log.WriteLine("got fileSum");
}
if (fd != null && Util.MemoryCompare(fileSum1, 0, fileSum2, 0, CheckSum.MD4_SUM_LENGTH) != 0)
{
return false;
}
return true;
report_write_error:
{
MainClass.Exit("write failed on " + Util.fullFileName(fileName), clientInfo);
}
return true;
}
public static void FinishTransfer(string fileName, string fileNameTmp, FileStruct file, bool okToSetTime)
{
}
public void deleteOne(string fileName, bool isDir)
{
SysCall sc = new SysCall(options);
if (!isDir)
{
if (!sc.robustUnlink(fileName))
{
Log.WriteLine("Can't delete '" + fileName + "' file");
}
else
{
if (options.verbose > 0)
{
Log.WriteLine("deleting file " + fileName);
}
}
}
else
{
if (!sc.doRmDir(fileName))
{
Log.WriteLine("Can't delete '" + fileName + "' dir");
}
else
{
if (options.verbose > 0)
{
Log.WriteLine("deleting directory " + fileName);
}
}
}
}
public bool isBackupFile(string fileName)
{
return fileName.EndsWith(options.backupSuffix);
}
public string getTmpName(string fileName)
{
return Path.GetTempFileName();
}
public void DeleteFiles(List<FileStruct> fileList)
{
string[] argv = new string[1];
List<FileStruct> localFileList = null;
if (options.cvsExclude)
{
Exclude.AddCvsExcludes();
}
for (int j = 0; j < fileList.Count; j++)
{
if ((fileList[j].mode & Options.FLAG_TOP_DIR) == 0 || !Util.S_ISDIR(fileList[j].mode))
{
continue;
}
argv[0] = options.dir + fileList[j].GetFullName();
FileList fList = new FileList(options);
if ((localFileList = fList.sendFileList(null, argv)) == null)
{
continue;
}
for (int i = localFileList.Count - 1; i >= 0; i--)
{
if (localFileList[i].baseName == null)
{
continue;
}
localFileList[i].dirName = localFileList[i].dirName.Substring(options.dir.Length);
if (FileList.fileListFind(fileList, (localFileList[i])) < 0)
{
localFileList[i].dirName = options.dir + localFileList[i].dirName;
deleteOne(localFileList[i].GetFullName(), Util.S_ISDIR(localFileList[i].mode));
}
}
}
}
}
public class SysCall
{
private Options options;
public SysCall(Options opt)
{
options = opt;
}
public bool doRmDir(string pathName)
{
if (options.dryRun)
{
return true;
}
if (options.readOnly || options.listOnly)
{
return false;
}
try
{
// TODO: path length
Directory.Delete(pathName);
return true;
}
catch
{
return false;
}
}
public bool robustUnlink(string fileName)
{
return doUnlink(fileName);
}
public bool doUnlink(string fileName)
{
if (options.dryRun)
{
return true;
}
if (options.readOnly || options.listOnly)
{
return false;
}
try
{
// TODO: path length
File.Delete(fileName);
return true;
}
catch
{
return false;
}
}
}
}