-
Notifications
You must be signed in to change notification settings - Fork 32
/
MainClass.cs
565 lines (517 loc) · 19.5 KB
/
MainClass.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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/**
* 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.IO;
using System.Net.Sockets;
using System.Text;
namespace NetSync
{
public class WinRsync
{
[STAThread]
static void Main(string[] args)
{
MainClass mc = new MainClass();
mc.Run(args);
}
}
public class MainClass
{
const string BACKUP_SUFFIX = "~";
const string RSYNC_NAME = "rsync";
const string RSYNC_VERSION = "1.0";
public static Options opt;
public void Run(string[] args)
{
opt = new Options();
opt.Init();
if(args.Length == 0)
{
Usage();
MainClass.Exit("", null);
}
int argsNotUsed = CommandLineParser.ParseArguments(args, opt);
if (argsNotUsed == -1)
{
MainClass.Exit("Error parsing options", null);
}
string[] args2 = new string[argsNotUsed];
for(int i = 0; i < argsNotUsed; i++)
args2[i] = args[args.Length - argsNotUsed + i];
if(opt.amDaemon && !opt.amSender)
{
Daemon.DaemonMain(opt);
return;
}
ClientInfo cInfo = new ClientInfo();
cInfo.Options = opt;
StartClient(args2, cInfo);
opt.doStats = true;
cInfo.IoStream = null;
Report(cInfo);
Console.Write("Press 'Enter' to exit.");
Console.Read();
}
public static void Report(ClientInfo cInfo)
{
IOStream f = cInfo.IoStream;
Options options = cInfo.Options;
Int64 totalWritten = Options.stats.totalWritten;
Int64 totalRead = Options.stats.totalRead;
if (options.amServer && f != null)
{
if (options.amSender)
{
f.WriteLongInt(totalRead);
f.WriteLongInt(totalWritten);
f.WriteLongInt(Options.stats.totalSize);
}
return;
}
if (!options.amSender && f != null)
{
/* Read the first two in opposite order because the meaning of
* read/write swaps when switching from sender to receiver. */
totalWritten = f.ReadLongInt();
totalRead = f.ReadLongInt();
Options.stats.totalSize = f.ReadLongInt();
}
if (options.doStats)
{
Log.WriteLine("Number of files: " + Options.stats.numFiles);
Log.WriteLine("Number of files transferred: " + Options.stats.numTransferredFiles);
Log.WriteLine("Total file size: " + Options.stats.totalSize);
Log.WriteLine("Total transferred file size: " + Options.stats.totalTransferredSize);
Log.WriteLine("Literal data: " + Options.stats.literalData);
Log.WriteLine("Matched data: " + Options.stats.matchedData);
Log.WriteLine("File list size: " + Options.stats.flistSize);
Log.WriteLine("Total bytes written: " + totalWritten);
Log.WriteLine("Total bytes received: " + totalRead);
}
}
public static int StartClient(string[] args, ClientInfo cInfo)
{
Options options = cInfo.Options;
if (args[0].StartsWith(Options.URL_PREFIX) && !options.readBatch) //source is remote
{
string path, user = "";
string host = args[0].Substring(Options.URL_PREFIX.Length,args[0].Length - Options.URL_PREFIX.Length);
if(host.LastIndexOf('@') != -1)
{
user = host.Substring(0, host.LastIndexOf('@'));
host = host.Substring(host.LastIndexOf('@') + 1);
}
else
{
MainClass.Exit("Unknown host", null);
}
if(host.IndexOf("/") != -1)
{
path = host.Substring(host.IndexOf("/")+1);
host = host.Substring(0, host.IndexOf("/"));
}
else
path ="";
if(host[0] == '[' && host.IndexOf(']') != -1)
{
host = host.Remove(host.IndexOf(']'),1);
host = host.Remove(host.IndexOf('['),1);
}
if(host.IndexOf(':') != -1)
{
options.rsyncPort = Convert.ToInt32(host.Substring(host.IndexOf(':')));
host = host.Substring(0, host.IndexOf(':'));
}
string[] newArgs = (string[])Util.ArrayDeleteFirstElement(args);
return StartSocketClient(host, path, user, newArgs, cInfo);
}
//source is local
if(!options.readBatch)
{
int p = Util.FindColon(args[0]);
string user = "";
options.amSender = true;
if (args[args.Length - 1].StartsWith(Options.URL_PREFIX) && !options.readBatch)
{
string path;
string host = args[args.Length - 1].Substring(Options.URL_PREFIX.Length);
if(host.LastIndexOf('@') != -1)
{
user = host.Substring(0, host.LastIndexOf('@'));
host = host.Substring(host.LastIndexOf('@') + 1);
}
else
{
MainClass.Exit("Unknown host", null);
}
if(host.IndexOf("/") != -1)
{
path = host.Substring(host.IndexOf("/")+1);
host = host.Substring(0, host.IndexOf("/"));
}
else
path ="";
if(host[0] == '[' && host.IndexOf(']') != -1)
{
host = host.Remove(host.IndexOf(']'),1);
host = host.Remove(host.IndexOf('['),1);
}
if(host.IndexOf(':') != -1)
{
options.rsyncPort = Convert.ToInt32(host.Substring(host.IndexOf(':')));
host = host.Substring(0, host.IndexOf(':'));
}
string[] newArgs = (string[])Util.ArrayDeleteLastElement(args);
return StartSocketClient(host, path, user, newArgs, cInfo);
}
p = Util.FindColon(args[args.Length - 1]);
if(p == -1) //src & dest are local
{
/* no realized*/
}
else
if (args[args.Length - 1][p + 1] == ':')
{
if(options.shellCmd == null)
return StartSocketClient(args[args.Length - 1].Substring(0, p), args[args.Length - 1].Substring(p + 2), user, args, cInfo);
}
}
return 0;
}
public static int StartSocketClient(string host, string path, string user, string[] args, ClientInfo cInfo)
{
Options options = cInfo.Options;
if(path.CompareTo("") != 0 && path[0] == '/')
{
Log.WriteLine("ERROR: The remote path must start with a module name not a /");
return -1;
}
cInfo.IoStream = OpenSocketOutWrapped(host, options.rsyncPort, options.bindAddress);
if(cInfo.IoStream != null)
StartInbandExchange(user,path, cInfo, args.Length);
Client client = new Client();
return client.ClientRun(cInfo, -1, args);
}
public static int StartInbandExchange(string user, string path, ClientInfo cInfo, int argc)
{
Options options = cInfo.Options;
IOStream f = cInfo.IoStream;
string[] sargs = new string[Options.MAX_ARGS];
int sargc = options.ServerOptions(sargs);
sargs[sargc++] = ".";
//if(path != null && path.Length>0)
//sargs[sargc++] = path;
if(argc == 0 && !options.amSender)
options.listOnly = true;
if(path[0] == '/')
{
Log.WriteLine("ERROR: The remote path must start with a module name");
return -1;
}
f.IOPrintf("@RSYNCD: " + options.protocolVersion + "\n");
string line = f.ReadLine();
try
{
options.remoteProtocol = Int32.Parse(line.Substring(9,2));
}
catch
{
options.remoteProtocol = 0;
}
bool isValidstring = line.StartsWith("@RSYNCD: ") && line.EndsWith("\n") && options.remoteProtocol > 0;
if(!isValidstring)
{
f.IOPrintf("@ERROR: protocol startup error\n");
return -1;
}
if(options.protocolVersion > options.remoteProtocol)
options.protocolVersion = options.remoteProtocol;
f.IOPrintf(path + "\n");
while(true)
{
line = f.ReadLine();
if (line.CompareTo("@RSYNCD: OK\n") == 0)
break;
if (line.Length > 18 && line.Substring(0, 18).CompareTo("@RSYNCD: AUTHREQD ") == 0)
{
string pass = "";
if(user.IndexOf(':') != -1)
{
pass = user.Substring(user.IndexOf(':')+1);
user = user.Substring(0, user.IndexOf(':'));
}
f.IOPrintf(user + " " + Authentication.auth_client(user, pass, line.Substring(18).Replace("\n",""), options) + "\n");
continue;
}
if (line.CompareTo("@RSYNCD: EXIT\n") == 0)
MainClass.Exit("@RSYNCD: EXIT", null);
if(line.StartsWith("@ERROR: "))
{
MainClass.Exit("Server: " + line.Replace("\n",""), null);
}
}
for(int i=0; i< sargc; i++)
f.IOPrintf(sargs[i] + "\n");
f.IOPrintf("\n");
return 0;
}
public static IOStream OpenSocketOutWrapped(string host, int port, string bindAddress)
{
return OpenSocketOut(host,port,bindAddress);
}
public static IOStream OpenSocketOut(string host, int port, string bindAddress)
{
TcpClient client = null;
try
{
client = new TcpClient(host, port);
}
catch(Exception)
{
MainClass.Exit("Can't connect to server", null);
}
IOStream stream = new IOStream(client.GetStream());
return stream;
}
public static void SetupProtocol(ClientInfo cInfo)
{
IOStream f = cInfo.IoStream;
Options options = cInfo.Options;
if(options.remoteProtocol == 0)
{
if(!options.readBatch)
f.writeInt(options.protocolVersion);
options.remoteProtocol = f.readInt();
if(options.protocolVersion > options.remoteProtocol)
options.protocolVersion = options.remoteProtocol;
}
if(options.readBatch && options.remoteProtocol > options.protocolVersion)
{
MainClass.Exit("The protocol version in the batch file is too new", null);
}
if(options.verbose > 3)
{
Log.WriteLine("(" + (options.amServer ? "Server" : "Client") + ") Protocol versions: remote="+options.remoteProtocol+", negotiated=" + options.protocolVersion);
}
if(options.remoteProtocol < Options.MIN_PROTOCOL_VERSION || options.remoteProtocol > Options.MAX_PROTOCOL_VERSION)
{
MainClass.Exit("Protocol version mistmatch", null);
}
if (options.amServer)
{
if (options.checksumSeed == 0)
options.checksumSeed = (int)System.DateTime.Now.Ticks;
f.writeInt(options.checksumSeed);
}
else
options.checksumSeed = f.readInt();
}
public static void PrintRsyncVersion()
{
Log.WriteLine(RSYNC_NAME+" version "+RSYNC_VERSION);
Log.WriteLine(@"
This port is Copyright (C) 2006 Alex Pedenko, Michael Feingold and Ivan Semenov
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");
}
public static void Usage()
{
PrintRsyncVersion();
Log.Write("\nrsync is a file transfer program capable of efficient remote update\nvia a fast differencing algorithm.\n\n");
Log.Write("Usage: rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST\n");
Log.Write(" or rsync [OPTION]... [USER@]HOST:SRC DEST\n");
Log.Write(" or rsync [OPTION]... SRC [SRC]... DEST\n");
Log.Write(" or rsync [OPTION]... [USER@]HOST::SRC [DEST]\n");
Log.Write(" or rsync [OPTION]... SRC [SRC]... [USER@]HOST::DEST\n");
Log.Write(" or rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]\n");
Log.Write(" or rsync [OPTION]... SRC [SRC]... rsync://[USER@]HOST[:PORT]/DEST\n");
Log.Write("SRC on single-colon remote HOST will be expanded by remote shell\n");
Log.Write("SRC on server remote HOST may contain shell wildcards or multiple\n");
Log.Write(" sources separated by space as long as they have same top-level\n");
Log.Write("\nOptions\n");
Log.Write(" -v, --verbose increase verbosity\n");
Log.Write(" -q, --quiet decrease verbosity\n");
Log.Write(" -c, --checksum always checksum\n");
Log.Write(" -a, --archive archive mode, equivalent to -rlptgoD (no -H)\n");
Log.Write(" -r, --recursive recurse into directories\n");
Log.Write(" -R, --relative use relative path names\n");
Log.Write(" --no-relative turn off --relative\n");
Log.Write(" --no-implied-dirs don't send implied dirs with -R\n");
Log.Write(" -b, --backup make backups (see --suffix & --backup-dir)\n");
Log.Write(" --backup-dir make backups into this directory\n");
Log.Write(" --suffix=SUFFIX backup suffix (default "+BACKUP_SUFFIX+" w/o --backup-dir)\n");
Log.Write(" -u, --update update only (don't overwrite newer files)\n");
Log.Write(" --inplace update destination files inplace (SEE MAN PAGE)\n");
Log.Write(" -K, --keep-dirlinks treat symlinked dir on receiver as dir\n");
Log.Write(" -l, --links copy symlinks as symlinks\n");
Log.Write(" -L, --copy-links copy the referent of all symlinks\n");
Log.Write(" --copy-unsafe-links copy the referent of \"unsafe\" symlinks\n");
Log.Write(" --safe-links ignore \"unsafe\" symlinks\n");
Log.Write(" -H, --hard-links preserve hard links\n");
Log.Write(" -p, --perms preserve permissions\n");
Log.Write(" -o, --owner preserve owner (root only)\n");
Log.Write(" -g, --group preserve group\n");
Log.Write(" -D, --devices preserve devices (root only)\n");
Log.Write(" -t, --times preserve times\n");
Log.Write(" -S, --sparse handle sparse files efficiently\n");
Log.Write(" -n, --dry-run show what would have been transferred\n");
Log.Write(" -W, --whole-file copy whole files, no incremental checks\n");
Log.Write(" --no-whole-file turn off --whole-file\n");
Log.Write(" -x, --one-file-system don't cross filesystem boundaries\n");
Log.Write(" -B, --block-size=SIZE force a fixed checksum block-size\n");
Log.Write(" -e, --rsh=COMMAND specify the remote shell\n");
Log.Write(" --rsync-path=PATH specify path to rsync on the remote machine\n");
Log.Write(" --existing only update files that already exist\n");
Log.Write(" --ignore-existing ignore files that already exist on receiving side\n");
Log.Write(" --delete delete files that don't exist on the sending side\n");
Log.Write(" --delete-excluded also delete excluded files on the receiving side\n");
Log.Write(" --delete-after receiver deletes after transferring, not before\n");
Log.Write(" --ignore-errors delete even if there are I/O errors\n");
Log.Write(" --max-delete=NUM don't delete more than NUM files\n");
Log.Write(" --partial keep partially transferred files\n");
Log.Write(" --partial-dir=DIR put a partially transferred file into DIR\n");
Log.Write(" --force force deletion of directories even if not empty\n");
Log.Write(" --numeric-ids don't map uid/gid values by user/group name\n");
Log.Write(" --timeout=TIME set I/O timeout in seconds\n");
Log.Write(" -I, --ignore-times turn off mod time & file size quick check\n");
Log.Write(" --size-only ignore mod time for quick check (use size)\n");
Log.Write(" --modify-window=NUM compare mod times with reduced accuracy\n");
Log.Write(" -T, --temp-dir=DIR create temporary files in directory DIR\n");
Log.Write(" --compare-dest=DIR also compare destination files relative to DIR\n");
Log.Write(" --link-dest=DIR create hardlinks to DIR for unchanged files\n");
Log.Write(" -P equivalent to --partial --progress\n");
Log.Write(" -z, --compress compress file data\n");
Log.Write(" -C, --cvs-exclude auto ignore files in the same way CVS does\n");
Log.Write(" --exclude=PATTERN exclude files matching PATTERN\n");
Log.Write(" --exclude-from=FILE exclude patterns listed in FILE\n");
Log.Write(" --include=PATTERN don't exclude files matching PATTERN\n");
Log.Write(" --include-from=FILE don't exclude patterns listed in FILE\n");
Log.Write(" --files-from=FILE read FILE for list of source-file names\n");
Log.Write(" -0, --from0 all *-from file lists are delimited by nulls\n");
Log.Write(" --version print version number\n");
Log.Write(" --blocking-io use blocking I/O for the remote shell\n");
Log.Write(" --no-blocking-io turn off --blocking-io\n");
Log.Write(" --stats give some file transfer stats\n");
Log.Write(" --progress show progress during transfer\n");
Log.Write(" --log-format=FORMAT log file transfers using specified format\n");
Log.Write(" --password-file=FILE get password from FILE\n");
Log.Write(" --bwlimit=KBPS limit I/O bandwidth, KBytes per second\n");
Log.Write(" --write-batch=FILE write a batch to FILE\n");
Log.Write(" --read-batch=FILE read a batch from FILE\n");
Log.Write(" -h, --help show this help screen\n");
}
public static void Exit(string message, ClientInfo cInfo)
{
Log.Write(message);
if(!opt.amDaemon)
{
Console.Read();
System.Environment.Exit(0);
} else
{
if(cInfo != null && cInfo.IoStream !=null && cInfo.IoStream.ClientThread != null)
{
cInfo.IoStream.ClientThread.Abort();
}
}
}
}
class Log
{
public static void WriteLine(string str)
{
LogWrite(str);
}
public static void Write(string str)
{
LogWrite(str);
}
public static void LogSend(FileStruct file, Stats initialStats)
{
}
private static void LogWrite(string str)
{
if(Daemon.ServerOptions != null)
{
if(Daemon.ServerOptions.logFile == null)
{
try
{
Daemon.ServerOptions.logFile = new FileStream(Path.Combine(Environment.SystemDirectory,"rsyncd.log"), FileMode.OpenOrCreate | FileMode.Append, FileAccess.Write);
}
catch(Exception e)
{
return;
}
}
str = "[ " + DateTime.Now + " ] " + str + "\r\n";
Daemon.ServerOptions.logFile.Write(Encoding.ASCII.GetBytes(str),0,str.Length);
Daemon.ServerOptions.logFile.Flush();
}
else
{
if(!MainClass.opt.amDaemon )
Console.WriteLine(str);
}
}
}
public class SumBuf
{
public int offset;
public UInt32 len;
public UInt32 sum1;
public byte flags;
public byte[] sum2 = new byte[CheckSum.SUM_LENGTH];
}
public class SumStruct
{
public int fLength;
public int count;
public UInt32 bLength;
public UInt32 remainder;
public int s2Length;
public SumBuf[] sums;
}
public class FStat
{
public long size;
public System.DateTime mTime;
public int mode;
public int uid;
public int gid;
public int rdev;
}
public class Progress
{
public static void ShowProgress(long offset, long size)
{
}
public static void EndProgress(long size)
{
}
}
}