Commit | Line | Data |
---|---|---|
c627d613 AT |
1 | /* |
2 | Copyright (C) Andrew Tridgell 1996 | |
3 | Copyright (C) Paul Mackerras 1996 | |
4 | ||
5 | This program is free software; you can redistribute it and/or modify | |
6 | it under the terms of the GNU General Public License as published by | |
7 | the Free Software Foundation; either version 2 of the License, or | |
8 | (at your option) any later version. | |
9 | ||
10 | This program is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU General Public License | |
16 | along with this program; if not, write to the Free Software | |
17 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
18 | */ | |
19 | ||
20 | #include "rsync.h" | |
21 | ||
22 | int verbose = 0; | |
23 | int always_checksum = 0; | |
24 | time_t starttime; | |
71c46176 | 25 | int64 total_size = 0; |
c627d613 AT |
26 | int block_size=BLOCK_SIZE; |
27 | ||
28 | char *backup_suffix = BACKUP_SUFFIX; | |
950ab32d | 29 | char *tmpdir = NULL; |
c627d613 AT |
30 | |
31 | static char *rsync_path = RSYNC_NAME; | |
32 | ||
33 | int make_backups = 0; | |
82306bf6 AT |
34 | int whole_file = 0; |
35 | int copy_links = 0; | |
c627d613 | 36 | int preserve_links = 0; |
dc5ddbcc | 37 | int preserve_hard_links = 0; |
c627d613 AT |
38 | int preserve_perms = 0; |
39 | int preserve_devices = 0; | |
40 | int preserve_uid = 0; | |
41 | int preserve_gid = 0; | |
42 | int preserve_times = 0; | |
43 | int update_only = 0; | |
44 | int cvs_exclude = 0; | |
45 | int dry_run=0; | |
46 | int local_server=0; | |
47 | int ignore_times=0; | |
48 | int delete_mode=0; | |
49 | int one_file_system=0; | |
4fe159a8 | 50 | int remote_version=0; |
dc5ddbcc | 51 | int sparse_files=0; |
70d794dc | 52 | int do_compression=0; |
7b8356d0 AT |
53 | int am_root=0; |
54 | int orig_umask=0; | |
6574b4f7 | 55 | int relative_paths=0; |
f6c34742 | 56 | int numeric_ids = 0; |
3cb6f5d6 | 57 | int force_delete = 0; |
6ba9279f AT |
58 | int io_timeout = 0; |
59 | int io_error = 0; | |
70d794dc | 60 | |
5d6bcd44 AT |
61 | static char *shell_cmd; |
62 | ||
34ccb63e | 63 | extern int csum_length; |
c627d613 AT |
64 | |
65 | int am_server = 0; | |
366345fe | 66 | int am_sender; |
c627d613 AT |
67 | int recurse = 0; |
68 | ||
69 | static void usage(FILE *f); | |
70 | ||
71 | static void report(int f) | |
72 | { | |
71c46176 | 73 | int64 in,out,tsize; |
c627d613 AT |
74 | time_t t = time(NULL); |
75 | ||
76 | if (!verbose) return; | |
77 | ||
366345fe | 78 | if (am_server && am_sender) { |
3a6a366f AT |
79 | write_longint(f,read_total()); |
80 | write_longint(f,write_total()); | |
81 | write_longint(f,total_size); | |
c627d613 AT |
82 | write_flush(f); |
83 | return; | |
84 | } | |
85 | ||
366345fe | 86 | if (am_sender) { |
c627d613 AT |
87 | in = read_total(); |
88 | out = write_total(); | |
3a6a366f | 89 | tsize = total_size; |
c627d613 | 90 | } else { |
3a6a366f AT |
91 | in = read_longint(f); |
92 | out = read_longint(f); | |
93 | tsize = read_longint(f); | |
c627d613 AT |
94 | } |
95 | ||
efb2f6bf AT |
96 | printf("wrote %.0f bytes read %.0f bytes %.2f bytes/sec\n", |
97 | (double)out,(double)in,(in+out)/(0.5 + (t-starttime))); | |
98 | printf("total size is %.0f speedup is %.2f\n", | |
99 | (double)tsize,(1.0*tsize)/(in+out)); | |
c627d613 AT |
100 | } |
101 | ||
102 | ||
103 | static void server_options(char **args,int *argc) | |
104 | { | |
105 | int ac = *argc; | |
106 | static char argstr[50]; | |
107 | static char bsize[30]; | |
6ba9279f | 108 | static char iotime[30]; |
c627d613 AT |
109 | int i, x; |
110 | ||
111 | args[ac++] = "--server"; | |
112 | ||
366345fe | 113 | if (!am_sender) |
c627d613 AT |
114 | args[ac++] = "--sender"; |
115 | ||
116 | x = 1; | |
117 | argstr[0] = '-'; | |
118 | for (i=0;i<verbose;i++) | |
119 | argstr[x++] = 'v'; | |
120 | if (make_backups) | |
121 | argstr[x++] = 'b'; | |
122 | if (update_only) | |
123 | argstr[x++] = 'u'; | |
124 | if (dry_run) | |
125 | argstr[x++] = 'n'; | |
126 | if (preserve_links) | |
127 | argstr[x++] = 'l'; | |
82306bf6 AT |
128 | if (copy_links) |
129 | argstr[x++] = 'L'; | |
130 | if (whole_file) | |
131 | argstr[x++] = 'W'; | |
dc5ddbcc AT |
132 | if (preserve_hard_links) |
133 | argstr[x++] = 'H'; | |
c627d613 AT |
134 | if (preserve_uid) |
135 | argstr[x++] = 'o'; | |
136 | if (preserve_gid) | |
137 | argstr[x++] = 'g'; | |
138 | if (preserve_devices) | |
139 | argstr[x++] = 'D'; | |
140 | if (preserve_times) | |
141 | argstr[x++] = 't'; | |
142 | if (preserve_perms) | |
143 | argstr[x++] = 'p'; | |
144 | if (recurse) | |
145 | argstr[x++] = 'r'; | |
146 | if (always_checksum) | |
147 | argstr[x++] = 'c'; | |
148 | if (cvs_exclude) | |
149 | argstr[x++] = 'C'; | |
150 | if (ignore_times) | |
151 | argstr[x++] = 'I'; | |
6574b4f7 AT |
152 | if (relative_paths) |
153 | argstr[x++] = 'R'; | |
c627d613 AT |
154 | if (one_file_system) |
155 | argstr[x++] = 'x'; | |
dc5ddbcc AT |
156 | if (sparse_files) |
157 | argstr[x++] = 'S'; | |
861c20b4 PM |
158 | if (do_compression) |
159 | argstr[x++] = 'z'; | |
c627d613 AT |
160 | argstr[x] = 0; |
161 | ||
162 | if (x != 1) args[ac++] = argstr; | |
163 | ||
164 | if (block_size != BLOCK_SIZE) { | |
165 | sprintf(bsize,"-B%d",block_size); | |
166 | args[ac++] = bsize; | |
167 | } | |
43a481dc | 168 | |
6ba9279f AT |
169 | if (io_timeout) { |
170 | sprintf(iotime,"--timeout=%d",io_timeout); | |
171 | args[ac++] = iotime; | |
172 | } | |
173 | ||
dc7a9478 AT |
174 | if (strcmp(backup_suffix, BACKUP_SUFFIX)) { |
175 | args[ac++] = "--suffix"; | |
176 | args[ac++] = backup_suffix; | |
177 | } | |
178 | ||
c627d613 AT |
179 | if (delete_mode) |
180 | args[ac++] = "--delete"; | |
181 | ||
3cb6f5d6 AT |
182 | if (force_delete) |
183 | args[ac++] = "--force"; | |
184 | ||
f6c34742 AT |
185 | if (numeric_ids) |
186 | args[ac++] = "--numeric-ids"; | |
187 | ||
950ab32d AT |
188 | if (tmpdir) { |
189 | args[ac++] = "--temp-dir"; | |
190 | args[ac++] = tmpdir; | |
191 | } | |
192 | ||
c627d613 AT |
193 | *argc = ac; |
194 | } | |
195 | ||
196 | ||
197 | ||
e3cd198f | 198 | static int do_cmd(char *cmd,char *machine,char *user,char *path,int *f_in,int *f_out) |
c627d613 | 199 | { |
366345fe AT |
200 | char *args[100]; |
201 | int i,argc=0, ret; | |
202 | char *tok,*dir=NULL; | |
203 | ||
204 | if (!local_server) { | |
205 | if (!cmd) | |
206 | cmd = getenv(RSYNC_RSH_ENV); | |
207 | if (!cmd) | |
208 | cmd = RSYNC_RSH; | |
209 | cmd = strdup(cmd); | |
210 | if (!cmd) | |
211 | goto oom; | |
212 | ||
213 | for (tok=strtok(cmd," ");tok;tok=strtok(NULL," ")) { | |
214 | args[argc++] = tok; | |
215 | } | |
c627d613 | 216 | |
7b8356d0 | 217 | #if HAVE_REMSH |
366345fe AT |
218 | /* remsh (on HPUX) takes the arguments the other way around */ |
219 | args[argc++] = machine; | |
220 | if (user) { | |
221 | args[argc++] = "-l"; | |
222 | args[argc++] = user; | |
223 | } | |
7b8356d0 | 224 | #else |
366345fe AT |
225 | if (user) { |
226 | args[argc++] = "-l"; | |
227 | args[argc++] = user; | |
228 | } | |
229 | args[argc++] = machine; | |
7b8356d0 | 230 | #endif |
c627d613 | 231 | |
366345fe | 232 | args[argc++] = rsync_path; |
c627d613 | 233 | |
366345fe AT |
234 | server_options(args,&argc); |
235 | } | |
c627d613 | 236 | |
366345fe | 237 | args[argc++] = "."; |
76076c4b | 238 | |
366345fe AT |
239 | if (path && *path) |
240 | args[argc++] = path; | |
c627d613 | 241 | |
366345fe | 242 | args[argc] = NULL; |
c627d613 | 243 | |
366345fe AT |
244 | if (verbose > 3) { |
245 | fprintf(FINFO,"cmd="); | |
246 | for (i=0;i<argc;i++) | |
247 | fprintf(FINFO,"%s ",args[i]); | |
248 | fprintf(FINFO,"\n"); | |
249 | } | |
250 | ||
251 | if (local_server) { | |
252 | ret = local_child(argc, args, f_in, f_out); | |
253 | } else { | |
254 | ret = piped_child(args,f_in,f_out); | |
255 | } | |
c627d613 | 256 | |
366345fe | 257 | if (dir) free(dir); |
82306bf6 | 258 | |
366345fe | 259 | return ret; |
c627d613 AT |
260 | |
261 | oom: | |
366345fe AT |
262 | out_of_memory("do_cmd"); |
263 | return 0; /* not reached */ | |
c627d613 AT |
264 | } |
265 | ||
266 | ||
267 | ||
268 | ||
269 | static char *get_local_name(struct file_list *flist,char *name) | |
270 | { | |
bcacc18b | 271 | STRUCT_STAT st; |
c627d613 | 272 | |
bcacc18b | 273 | if (do_stat(name,&st) == 0) { |
c627d613 AT |
274 | if (S_ISDIR(st.st_mode)) { |
275 | if (chdir(name) != 0) { | |
6574b4f7 | 276 | fprintf(FERROR,"chdir %s : %s (1)\n",name,strerror(errno)); |
34ccb63e | 277 | exit_cleanup(1); |
c627d613 AT |
278 | } |
279 | return NULL; | |
280 | } | |
281 | if (flist->count > 1) { | |
dc5ddbcc | 282 | fprintf(FERROR,"ERROR: destination must be a directory when copying more than 1 file\n"); |
34ccb63e | 283 | exit_cleanup(1); |
c627d613 AT |
284 | } |
285 | return name; | |
286 | } | |
287 | ||
288 | if (flist->count == 1) | |
289 | return name; | |
290 | ||
291 | if (!name) | |
292 | return NULL; | |
293 | ||
1b2d733a | 294 | if (do_mkdir(name,0777 & ~orig_umask) != 0) { |
6574b4f7 | 295 | fprintf(FERROR,"mkdir %s : %s (1)\n",name,strerror(errno)); |
34ccb63e | 296 | exit_cleanup(1); |
94481d91 | 297 | } else { |
dc5ddbcc | 298 | fprintf(FINFO,"created directory %s\n",name); |
c627d613 AT |
299 | } |
300 | ||
301 | if (chdir(name) != 0) { | |
6574b4f7 | 302 | fprintf(FERROR,"chdir %s : %s (2)\n",name,strerror(errno)); |
34ccb63e | 303 | exit_cleanup(1); |
c627d613 AT |
304 | } |
305 | ||
306 | return NULL; | |
307 | } | |
308 | ||
309 | ||
310 | ||
311 | ||
312 | void do_server_sender(int argc,char *argv[]) | |
313 | { | |
314 | int i; | |
c627d613 | 315 | struct file_list *flist; |
76076c4b | 316 | char *dir = argv[0]; |
c627d613 AT |
317 | |
318 | if (verbose > 2) | |
6ba9279f | 319 | fprintf(FINFO,"server_sender starting pid=%d\n",(int)getpid()); |
c627d613 | 320 | |
6574b4f7 | 321 | if (!relative_paths && chdir(dir) != 0) { |
76076c4b AT |
322 | fprintf(FERROR,"chdir %s: %s (3)\n",dir,strerror(errno)); |
323 | exit_cleanup(1); | |
c627d613 AT |
324 | } |
325 | argc--; | |
326 | argv++; | |
327 | ||
328 | if (strcmp(dir,".")) { | |
76076c4b AT |
329 | int l = strlen(dir); |
330 | if (strcmp(dir,"/") == 0) | |
331 | l = 0; | |
332 | for (i=0;i<argc;i++) | |
333 | argv[i] += l+1; | |
c627d613 AT |
334 | } |
335 | ||
336 | if (argc == 0 && recurse) { | |
76076c4b AT |
337 | argc=1; |
338 | argv--; | |
339 | argv[0] = "."; | |
c627d613 AT |
340 | } |
341 | ||
342 | ||
a06d19e3 | 343 | flist = send_file_list(STDOUT_FILENO,argc,argv); |
c627d613 AT |
344 | send_files(flist,STDOUT_FILENO,STDIN_FILENO); |
345 | report(STDOUT_FILENO); | |
34ccb63e | 346 | exit_cleanup(0); |
c627d613 AT |
347 | } |
348 | ||
349 | ||
dc5ddbcc AT |
350 | static int do_recv(int f_in,int f_out,struct file_list *flist,char *local_name) |
351 | { | |
352 | int pid; | |
353 | int status=0; | |
c6e7fcb4 | 354 | int recv_pipe[2]; |
dc5ddbcc AT |
355 | |
356 | if (preserve_hard_links) | |
357 | init_hard_links(flist); | |
358 | ||
c6e7fcb4 AT |
359 | if (pipe(recv_pipe) < 0) { |
360 | fprintf(FERROR,"pipe failed in do_recv\n"); | |
361 | exit(1); | |
362 | } | |
363 | ||
364 | ||
3ba62a83 | 365 | if ((pid=do_fork()) == 0) { |
c6e7fcb4 | 366 | recv_files(f_in,flist,local_name,recv_pipe[1]); |
dc5ddbcc | 367 | if (verbose > 2) |
6ba9279f | 368 | fprintf(FINFO,"receiver read %ld\n",(long)read_total()); |
dc5ddbcc AT |
369 | exit_cleanup(0); |
370 | } | |
371 | ||
c6e7fcb4 | 372 | generate_files(f_out,flist,local_name,recv_pipe[0]); |
dc5ddbcc AT |
373 | |
374 | waitpid(pid, &status, 0); | |
375 | ||
376 | return status; | |
377 | } | |
378 | ||
c627d613 AT |
379 | |
380 | void do_server_recv(int argc,char *argv[]) | |
381 | { | |
dc5ddbcc | 382 | int status; |
c627d613 AT |
383 | struct file_list *flist; |
384 | char *local_name=NULL; | |
76076c4b | 385 | char *dir = NULL; |
c627d613 AT |
386 | |
387 | if (verbose > 2) | |
6ba9279f | 388 | fprintf(FINFO,"server_recv(%d) starting pid=%d\n",argc,(int)getpid()); |
c627d613 AT |
389 | |
390 | if (argc > 0) { | |
76076c4b AT |
391 | dir = argv[0]; |
392 | argc--; | |
393 | argv++; | |
394 | if (chdir(dir) != 0) { | |
395 | fprintf(FERROR,"chdir %s : %s (4)\n", | |
396 | dir,strerror(errno)); | |
397 | exit_cleanup(1); | |
398 | } | |
c627d613 AT |
399 | } |
400 | ||
401 | if (delete_mode) | |
402 | recv_exclude_list(STDIN_FILENO); | |
403 | ||
404 | flist = recv_file_list(STDIN_FILENO); | |
405 | if (!flist || flist->count == 0) { | |
dc5ddbcc | 406 | fprintf(FERROR,"nothing to do\n"); |
34ccb63e | 407 | exit_cleanup(1); |
c627d613 AT |
408 | } |
409 | ||
410 | if (argc > 0) { | |
76076c4b AT |
411 | if (strcmp(dir,".")) { |
412 | argv[0] += strlen(dir); | |
413 | if (argv[0][0] == '/') argv[0]++; | |
414 | } | |
415 | local_name = get_local_name(flist,argv[0]); | |
c627d613 AT |
416 | } |
417 | ||
dc5ddbcc | 418 | status = do_recv(STDIN_FILENO,STDOUT_FILENO,flist,local_name); |
34ccb63e | 419 | exit_cleanup(status); |
c627d613 AT |
420 | } |
421 | ||
422 | ||
366345fe AT |
423 | void start_server(int argc, char *argv[]) |
424 | { | |
425 | setup_protocol(STDOUT_FILENO,STDIN_FILENO); | |
426 | ||
427 | if (am_sender) { | |
428 | recv_exclude_list(STDIN_FILENO); | |
429 | if (cvs_exclude) | |
430 | add_cvs_excludes(); | |
431 | do_server_sender(argc,argv); | |
432 | } else { | |
433 | do_server_recv(argc,argv); | |
434 | } | |
435 | exit_cleanup(0); | |
436 | } | |
437 | ||
5d6bcd44 AT |
438 | int start_client(int argc, char *argv[]) |
439 | { | |
440 | char *p; | |
441 | char *shell_machine = NULL; | |
442 | char *shell_path = NULL; | |
443 | char *shell_user = NULL; | |
444 | int pid, status = 0, status2 = 0; | |
445 | int f_in,f_out; | |
446 | struct file_list *flist; | |
447 | char *local_name = NULL; | |
448 | ||
449 | p = strchr(argv[0],':'); | |
450 | ||
451 | if (p) { | |
452 | am_sender = 0; | |
453 | *p = 0; | |
454 | shell_machine = argv[0]; | |
455 | shell_path = p+1; | |
456 | argc--; | |
457 | argv++; | |
458 | } else { | |
459 | am_sender = 1; | |
460 | ||
461 | p = strchr(argv[argc-1],':'); | |
462 | if (!p) { | |
463 | local_server = 1; | |
464 | } | |
465 | ||
466 | if (local_server) { | |
467 | shell_machine = NULL; | |
468 | shell_path = argv[argc-1]; | |
469 | } else { | |
470 | *p = 0; | |
471 | shell_machine = argv[argc-1]; | |
472 | shell_path = p+1; | |
473 | } | |
474 | argc--; | |
475 | } | |
476 | ||
477 | if (shell_machine) { | |
478 | p = strchr(shell_machine,'@'); | |
479 | if (p) { | |
480 | *p = 0; | |
481 | shell_user = shell_machine; | |
482 | shell_machine = p+1; | |
483 | } | |
484 | } | |
485 | ||
486 | if (verbose > 3) { | |
487 | fprintf(FINFO,"cmd=%s machine=%s user=%s path=%s\n", | |
488 | shell_cmd?shell_cmd:"", | |
489 | shell_machine?shell_machine:"", | |
490 | shell_user?shell_user:"", | |
491 | shell_path?shell_path:""); | |
492 | } | |
493 | ||
494 | if (!am_sender && argc != 1) { | |
495 | usage(FERROR); | |
496 | exit_cleanup(1); | |
497 | } | |
498 | ||
499 | pid = do_cmd(shell_cmd,shell_machine,shell_user,shell_path,&f_in,&f_out); | |
500 | ||
501 | setup_protocol(f_out,f_in); | |
502 | ||
503 | #if HAVE_SETLINEBUF | |
504 | setlinebuf(FINFO); | |
505 | setlinebuf(FERROR); | |
506 | #endif | |
507 | ||
508 | if (verbose > 3) | |
509 | fprintf(FINFO,"parent=%d child=%d sender=%d recurse=%d\n", | |
510 | (int)getpid(),pid,am_sender,recurse); | |
511 | ||
512 | if (am_sender) { | |
513 | if (cvs_exclude) | |
514 | add_cvs_excludes(); | |
515 | if (delete_mode) | |
516 | send_exclude_list(f_out); | |
517 | flist = send_file_list(f_out,argc,argv); | |
518 | if (verbose > 3) | |
519 | fprintf(FINFO,"file list sent\n"); | |
520 | send_files(flist,f_out,f_in); | |
521 | if (verbose > 3) | |
522 | fprintf(FINFO,"waiting on %d\n",pid); | |
523 | waitpid(pid, &status, 0); | |
524 | report(-1); | |
525 | exit_cleanup(status); | |
526 | } | |
527 | ||
528 | send_exclude_list(f_out); | |
529 | ||
530 | flist = recv_file_list(f_in); | |
531 | if (!flist || flist->count == 0) { | |
532 | fprintf(FINFO,"nothing to do\n"); | |
533 | exit_cleanup(0); | |
534 | } | |
535 | ||
536 | local_name = get_local_name(flist,argv[0]); | |
537 | ||
538 | status2 = do_recv(f_in,f_out,flist,local_name); | |
539 | ||
540 | report(f_in); | |
541 | ||
542 | waitpid(pid, &status, 0); | |
543 | ||
544 | return status | status2; | |
545 | } | |
546 | ||
366345fe | 547 | |
c627d613 AT |
548 | static void usage(FILE *f) |
549 | { | |
550 | fprintf(f,"rsync version %s Copyright Andrew Tridgell and Paul Mackerras\n\n", | |
551 | VERSION); | |
552 | fprintf(f,"Usage:\t%s [options] src user@host:dest\nOR",RSYNC_NAME); | |
553 | fprintf(f,"\t%s [options] user@host:src dest\n\n",RSYNC_NAME); | |
554 | fprintf(f,"Options:\n"); | |
555 | fprintf(f,"-v, --verbose increase verbosity\n"); | |
556 | fprintf(f,"-c, --checksum always checksum\n"); | |
557 | fprintf(f,"-a, --archive archive mode (same as -rlptDog)\n"); | |
558 | fprintf(f,"-r, --recursive recurse into directories\n"); | |
6574b4f7 | 559 | fprintf(f,"-R, --relative use relative path names\n"); |
c627d613 AT |
560 | fprintf(f,"-b, --backup make backups (default ~ extension)\n"); |
561 | fprintf(f,"-u, --update update only (don't overwrite newer files)\n"); | |
562 | fprintf(f,"-l, --links preserve soft links\n"); | |
82306bf6 | 563 | fprintf(f,"-L, --copy-links treat soft links like regular files\n"); |
dc5ddbcc | 564 | fprintf(f,"-H, --hard-links preserve hard links\n"); |
c627d613 AT |
565 | fprintf(f,"-p, --perms preserve permissions\n"); |
566 | fprintf(f,"-o, --owner preserve owner (root only)\n"); | |
567 | fprintf(f,"-g, --group preserve group\n"); | |
568 | fprintf(f,"-D, --devices preserve devices (root only)\n"); | |
569 | fprintf(f,"-t, --times preserve times\n"); | |
dc5ddbcc | 570 | fprintf(f,"-S, --sparse handle sparse files efficiently\n"); |
c627d613 | 571 | fprintf(f,"-n, --dry-run show what would have been transferred\n"); |
82306bf6 | 572 | fprintf(f,"-W, --whole-file copy whole files, no incremental checks\n"); |
c627d613 AT |
573 | fprintf(f,"-x, --one-file-system don't cross filesystem boundaries\n"); |
574 | fprintf(f,"-B, --block-size SIZE checksum blocking size\n"); | |
575 | fprintf(f,"-e, --rsh COMMAND specify rsh replacement\n"); | |
576 | fprintf(f," --rsync-path PATH specify path to rsync on the remote machine\n"); | |
577 | fprintf(f,"-C, --cvs-exclude auto ignore files in the same way CVS does\n"); | |
578 | fprintf(f," --delete delete files that don't exist on the sending side\n"); | |
3cb6f5d6 | 579 | fprintf(f," --force force deletion of directories even if not empty\n"); |
f6c34742 | 580 | fprintf(f," --numeric-ids don't map uid/gid values by user/group name\n"); |
6ba9279f | 581 | fprintf(f," --timeout TIME set IO timeout in seconds\n"); |
c627d613 | 582 | fprintf(f,"-I, --ignore-times don't exclude files that match length and time\n"); |
950ab32d | 583 | fprintf(f,"-T --temp-dir DIR create temporary files in directory DIR\n"); |
861c20b4 | 584 | fprintf(f,"-z, --compress compress file data\n"); |
c627d613 AT |
585 | fprintf(f," --exclude FILE exclude file FILE\n"); |
586 | fprintf(f," --exclude-from FILE exclude files listed in FILE\n"); | |
587 | fprintf(f," --suffix SUFFIX override backup suffix\n"); | |
588 | fprintf(f," --version print version number\n"); | |
589 | ||
590 | fprintf(f,"\n"); | |
591 | fprintf(f,"the backup suffix defaults to %s\n",BACKUP_SUFFIX); | |
592 | fprintf(f,"the block size defaults to %d\n",BLOCK_SIZE); | |
593 | } | |
594 | ||
595 | enum {OPT_VERSION,OPT_SUFFIX,OPT_SENDER,OPT_SERVER,OPT_EXCLUDE, | |
6ba9279f AT |
596 | OPT_EXCLUDE_FROM,OPT_DELETE,OPT_NUMERIC_IDS,OPT_RSYNC_PATH, |
597 | OPT_FORCE,OPT_TIMEOUT}; | |
c627d613 | 598 | |
950ab32d | 599 | static char *short_options = "oblLWHpguDCtcahvrRIxnSe:B:T:z"; |
c627d613 AT |
600 | |
601 | static struct option long_options[] = { | |
602 | {"version", 0, 0, OPT_VERSION}, | |
603 | {"server", 0, 0, OPT_SERVER}, | |
604 | {"sender", 0, 0, OPT_SENDER}, | |
605 | {"delete", 0, 0, OPT_DELETE}, | |
3cb6f5d6 | 606 | {"force", 0, 0, OPT_FORCE}, |
f6c34742 | 607 | {"numeric-ids", 0, 0, OPT_NUMERIC_IDS}, |
c627d613 AT |
608 | {"exclude", 1, 0, OPT_EXCLUDE}, |
609 | {"exclude-from",1, 0, OPT_EXCLUDE_FROM}, | |
610 | {"rsync-path", 1, 0, OPT_RSYNC_PATH}, | |
611 | {"one-file-system",0, 0, 'x'}, | |
612 | {"ignore-times",0, 0, 'I'}, | |
613 | {"help", 0, 0, 'h'}, | |
614 | {"dry-run", 0, 0, 'n'}, | |
dc5ddbcc | 615 | {"sparse", 0, 0, 'S'}, |
c627d613 AT |
616 | {"cvs-exclude", 0, 0, 'C'}, |
617 | {"archive", 0, 0, 'a'}, | |
618 | {"checksum", 0, 0, 'c'}, | |
619 | {"backup", 0, 0, 'b'}, | |
620 | {"update", 0, 0, 'u'}, | |
621 | {"verbose", 0, 0, 'v'}, | |
622 | {"recursive", 0, 0, 'r'}, | |
6574b4f7 | 623 | {"relative", 0, 0, 'R'}, |
c627d613 AT |
624 | {"devices", 0, 0, 'D'}, |
625 | {"perms", 0, 0, 'p'}, | |
626 | {"links", 0, 0, 'l'}, | |
82306bf6 AT |
627 | {"copy-links", 0, 0, 'L'}, |
628 | {"whole-file", 0, 0, 'W'}, | |
dc5ddbcc | 629 | {"hard-links", 0, 0, 'H'}, |
c627d613 AT |
630 | {"owner", 0, 0, 'o'}, |
631 | {"group", 0, 0, 'g'}, | |
632 | {"times", 0, 0, 't'}, | |
633 | {"rsh", 1, 0, 'e'}, | |
634 | {"suffix", 1, 0, OPT_SUFFIX}, | |
635 | {"block-size", 1, 0, 'B'}, | |
6ba9279f | 636 | {"timeout", 1, 0, OPT_TIMEOUT}, |
950ab32d | 637 | {"temp-dir", 1, 0, 'T'}, |
861c20b4 | 638 | {"compress", 0, 0, 'z'}, |
c627d613 AT |
639 | {0,0,0,0}}; |
640 | ||
82306bf6 AT |
641 | RETSIGTYPE sigusr1_handler(int val) { |
642 | exit_cleanup(1); | |
643 | } | |
644 | ||
5d6bcd44 AT |
645 | |
646 | static void parse_arguments(int argc, char *argv[]) | |
c627d613 | 647 | { |
c627d613 AT |
648 | int opt; |
649 | int option_index; | |
c627d613 AT |
650 | |
651 | while ((opt = getopt_long(argc, argv, | |
652 | short_options, long_options, &option_index)) | |
653 | != -1) { | |
654 | switch (opt) | |
655 | { | |
656 | case OPT_VERSION: | |
657 | printf("rsync version %s protocol version %d\n", | |
658 | VERSION,PROTOCOL_VERSION); | |
34ccb63e | 659 | exit_cleanup(0); |
c627d613 AT |
660 | |
661 | case OPT_SUFFIX: | |
662 | backup_suffix = optarg; | |
663 | break; | |
664 | ||
665 | case OPT_RSYNC_PATH: | |
666 | rsync_path = optarg; | |
667 | break; | |
668 | ||
669 | case 'I': | |
670 | ignore_times = 1; | |
671 | break; | |
672 | ||
673 | case 'x': | |
674 | one_file_system=1; | |
675 | break; | |
676 | ||
677 | case OPT_DELETE: | |
678 | delete_mode = 1; | |
679 | break; | |
680 | ||
3cb6f5d6 AT |
681 | case OPT_FORCE: |
682 | force_delete = 1; | |
683 | break; | |
684 | ||
f6c34742 AT |
685 | case OPT_NUMERIC_IDS: |
686 | numeric_ids = 1; | |
687 | break; | |
688 | ||
c627d613 AT |
689 | case OPT_EXCLUDE: |
690 | add_exclude(optarg); | |
691 | break; | |
692 | ||
693 | case OPT_EXCLUDE_FROM: | |
694 | add_exclude_file(optarg,1); | |
695 | break; | |
696 | ||
697 | case 'h': | |
dc5ddbcc | 698 | usage(FINFO); |
34ccb63e | 699 | exit_cleanup(0); |
c627d613 AT |
700 | |
701 | case 'b': | |
702 | make_backups=1; | |
703 | break; | |
704 | ||
705 | case 'n': | |
706 | dry_run=1; | |
707 | break; | |
708 | ||
dc5ddbcc AT |
709 | case 'S': |
710 | sparse_files=1; | |
711 | break; | |
712 | ||
c627d613 AT |
713 | case 'C': |
714 | cvs_exclude=1; | |
715 | break; | |
716 | ||
717 | case 'u': | |
718 | update_only=1; | |
719 | break; | |
720 | ||
c627d613 AT |
721 | case 'l': |
722 | preserve_links=1; | |
723 | break; | |
dc5ddbcc | 724 | |
82306bf6 AT |
725 | case 'L': |
726 | copy_links=1; | |
727 | break; | |
728 | ||
729 | case 'W': | |
730 | whole_file=1; | |
731 | break; | |
732 | ||
dc5ddbcc AT |
733 | case 'H': |
734 | #if SUPPORT_HARD_LINKS | |
735 | preserve_hard_links=1; | |
cbbe4892 AT |
736 | #else |
737 | fprintf(FERROR,"ERROR: hard links not supported on this platform\n"); | |
738 | exit_cleanup(1); | |
c627d613 | 739 | #endif |
dc5ddbcc | 740 | break; |
c627d613 AT |
741 | |
742 | case 'p': | |
743 | preserve_perms=1; | |
744 | break; | |
745 | ||
746 | case 'o': | |
7b8356d0 | 747 | preserve_uid=1; |
c627d613 AT |
748 | break; |
749 | ||
750 | case 'g': | |
751 | preserve_gid=1; | |
752 | break; | |
753 | ||
754 | case 'D': | |
7b8356d0 | 755 | preserve_devices=1; |
c627d613 AT |
756 | break; |
757 | ||
758 | case 't': | |
759 | preserve_times=1; | |
760 | break; | |
761 | ||
762 | case 'c': | |
763 | always_checksum=1; | |
764 | break; | |
765 | ||
766 | case 'v': | |
767 | verbose++; | |
768 | break; | |
769 | ||
770 | case 'a': | |
771 | recurse=1; | |
772 | #if SUPPORT_LINKS | |
773 | preserve_links=1; | |
774 | #endif | |
775 | preserve_perms=1; | |
776 | preserve_times=1; | |
777 | preserve_gid=1; | |
7b8356d0 | 778 | if (am_root) { |
c627d613 AT |
779 | preserve_devices=1; |
780 | preserve_uid=1; | |
7b8356d0 | 781 | } |
c627d613 AT |
782 | break; |
783 | ||
784 | case OPT_SERVER: | |
785 | am_server = 1; | |
786 | break; | |
787 | ||
788 | case OPT_SENDER: | |
789 | if (!am_server) { | |
dc5ddbcc | 790 | usage(FERROR); |
34ccb63e | 791 | exit_cleanup(1); |
c627d613 | 792 | } |
366345fe | 793 | am_sender = 1; |
c627d613 AT |
794 | break; |
795 | ||
796 | case 'r': | |
797 | recurse = 1; | |
798 | break; | |
799 | ||
6574b4f7 AT |
800 | case 'R': |
801 | relative_paths = 1; | |
802 | break; | |
803 | ||
c627d613 AT |
804 | case 'e': |
805 | shell_cmd = optarg; | |
806 | break; | |
807 | ||
808 | case 'B': | |
809 | block_size = atoi(optarg); | |
810 | break; | |
811 | ||
6ba9279f AT |
812 | case OPT_TIMEOUT: |
813 | io_timeout = atoi(optarg); | |
814 | break; | |
815 | ||
950ab32d AT |
816 | case 'T': |
817 | tmpdir = optarg; | |
818 | break; | |
819 | ||
861c20b4 PM |
820 | case 'z': |
821 | do_compression = 1; | |
822 | break; | |
823 | ||
c627d613 | 824 | default: |
861c20b4 | 825 | /* fprintf(FERROR,"bad option -%c\n",opt); */ |
34ccb63e | 826 | exit_cleanup(1); |
c627d613 AT |
827 | } |
828 | } | |
5d6bcd44 AT |
829 | } |
830 | ||
831 | int main(int argc,char *argv[]) | |
832 | { | |
833 | ||
834 | signal(SIGUSR1, sigusr1_handler); | |
835 | ||
836 | starttime = time(NULL); | |
837 | am_root = (getuid() == 0); | |
c627d613 | 838 | |
5d6bcd44 AT |
839 | /* we set a 0 umask so that correct file permissions can be |
840 | carried across */ | |
841 | orig_umask = (int)umask(0); | |
842 | ||
843 | parse_arguments(argc, argv); | |
844 | ||
845 | while (optind) { | |
c627d613 AT |
846 | argc--; |
847 | argv++; | |
5d6bcd44 | 848 | optind--; |
c627d613 AT |
849 | } |
850 | ||
6b83141d AT |
851 | signal(SIGCHLD,SIG_IGN); |
852 | signal(SIGINT,SIGNAL_CAST sig_int); | |
853 | signal(SIGPIPE,SIGNAL_CAST sig_int); | |
854 | signal(SIGHUP,SIGNAL_CAST sig_int); | |
855 | ||
c627d613 AT |
856 | if (dry_run) |
857 | verbose = MAX(verbose,1); | |
858 | ||
cbbe4892 AT |
859 | #ifndef SUPPORT_LINKS |
860 | if (!am_server && preserve_links) { | |
861 | fprintf(FERROR,"ERROR: symbolic links not supported\n"); | |
862 | exit_cleanup(1); | |
863 | } | |
864 | #endif | |
865 | ||
c627d613 | 866 | if (am_server) { |
366345fe | 867 | start_server(argc, argv); |
c627d613 AT |
868 | } |
869 | ||
870 | if (argc < 2) { | |
dc5ddbcc | 871 | usage(FERROR); |
34ccb63e | 872 | exit_cleanup(1); |
c627d613 AT |
873 | } |
874 | ||
5d6bcd44 | 875 | return start_client(argc, argv); |
c627d613 | 876 | } |
82306bf6 | 877 |