new exit/cleanup code
[rsync/rsync.git] / main.c
CommitLineData
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
22int verbose = 0;
23int always_checksum = 0;
24time_t starttime;
25off_t total_size = 0;
26int block_size=BLOCK_SIZE;
27
28char *backup_suffix = BACKUP_SUFFIX;
29
30static char *rsync_path = RSYNC_NAME;
31
32int make_backups = 0;
33int preserve_links = 0;
34int preserve_perms = 0;
35int preserve_devices = 0;
36int preserve_uid = 0;
37int preserve_gid = 0;
38int preserve_times = 0;
39int update_only = 0;
40int cvs_exclude = 0;
41int dry_run=0;
42int local_server=0;
43int ignore_times=0;
44int delete_mode=0;
45int one_file_system=0;
4fe159a8 46int remote_version=0;
34ccb63e 47extern int csum_length;
c627d613
AT
48
49int am_server = 0;
50static int sender = 0;
51int recurse = 0;
52
53static void usage(FILE *f);
54
55static void report(int f)
56{
57 int in,out,tsize;
58 time_t t = time(NULL);
59
60 if (!verbose) return;
61
62 if (am_server && sender) {
63 write_int(f,read_total());
64 write_int(f,write_total());
65 write_int(f,total_size);
66 write_flush(f);
67 return;
68 }
69
70 if (sender) {
71 in = read_total();
72 out = write_total();
73 tsize = (int)total_size;
74 } else {
75 in = read_int(f);
76 out = read_int(f);
77 tsize = read_int(f);
78 }
79
80 printf("wrote %d bytes read %d bytes %g bytes/sec\n",
81 out,in,(in+out)/(0.5 + (t-starttime)));
82 printf("total size is %d speedup is %g\n",
83 tsize,(1.0*tsize)/(in+out));
84}
85
86
87static void server_options(char **args,int *argc)
88{
89 int ac = *argc;
90 static char argstr[50];
91 static char bsize[30];
43a481dc 92 static char slength[30];
c627d613
AT
93 int i, x;
94
95 args[ac++] = "--server";
96
97 if (!sender)
98 args[ac++] = "--sender";
99
100 x = 1;
101 argstr[0] = '-';
102 for (i=0;i<verbose;i++)
103 argstr[x++] = 'v';
104 if (make_backups)
105 argstr[x++] = 'b';
106 if (update_only)
107 argstr[x++] = 'u';
108 if (dry_run)
109 argstr[x++] = 'n';
110 if (preserve_links)
111 argstr[x++] = 'l';
112 if (preserve_uid)
113 argstr[x++] = 'o';
114 if (preserve_gid)
115 argstr[x++] = 'g';
116 if (preserve_devices)
117 argstr[x++] = 'D';
118 if (preserve_times)
119 argstr[x++] = 't';
120 if (preserve_perms)
121 argstr[x++] = 'p';
122 if (recurse)
123 argstr[x++] = 'r';
124 if (always_checksum)
125 argstr[x++] = 'c';
126 if (cvs_exclude)
127 argstr[x++] = 'C';
128 if (ignore_times)
129 argstr[x++] = 'I';
130 if (one_file_system)
131 argstr[x++] = 'x';
132 argstr[x] = 0;
133
134 if (x != 1) args[ac++] = argstr;
135
136 if (block_size != BLOCK_SIZE) {
137 sprintf(bsize,"-B%d",block_size);
138 args[ac++] = bsize;
139 }
43a481dc
AT
140
141 if (csum_length != SUM_LENGTH) {
142 sprintf(slength,"--csum-length=%d",csum_length);
143 args[ac++] = slength;
144 }
c627d613
AT
145
146 if (delete_mode)
147 args[ac++] = "--delete";
148
149 *argc = ac;
150}
151
152
153
154int do_cmd(char *cmd,char *machine,char *user,char *path,int *f_in,int *f_out)
155{
156 char *args[100];
157 int i,argc=0;
158 char *tok,*p;
159
160 if (!local_server) {
161 if (!cmd)
162 cmd = getenv(RSYNC_RSH_ENV);
163 if (!cmd)
164 cmd = RSYNC_RSH;
165 cmd = strdup(cmd);
166 if (!cmd)
167 goto oom;
168
169 for (tok=strtok(cmd," ");tok;tok=strtok(NULL," ")) {
170 args[argc++] = tok;
171 }
172
173 if (user) {
174 args[argc++] = "-l";
175 args[argc++] = user;
176 }
177 args[argc++] = machine;
178 }
179
180 args[argc++] = rsync_path;
181
182 server_options(args,&argc);
183
184 if (path && *path) {
185 char *dir = strdup(path);
186 p = strrchr(dir,'/');
187 if (p) {
188 *p = 0;
189 args[argc++] = dir;
190 p++;
191 } else {
192 args[argc++] = ".";
193 p = dir;
194 }
195 if (p[0])
196 args[argc++] = path;
197 }
198
199 args[argc] = NULL;
200
201 if (verbose > 3) {
202 fprintf(stderr,"cmd=");
203 for (i=0;i<argc;i++)
204 fprintf(stderr,"%s ",args[i]);
205 fprintf(stderr,"\n");
206 }
207
208 return piped_child(args,f_in,f_out);
209
210oom:
211 out_of_memory("do_cmd");
212 return 0; /* not reached */
213}
214
215
216
217
218static char *get_local_name(struct file_list *flist,char *name)
219{
220 struct stat st;
221
222 if (stat(name,&st) == 0) {
223 if (S_ISDIR(st.st_mode)) {
224 if (chdir(name) != 0) {
225 fprintf(stderr,"chdir %s : %s\n",name,strerror(errno));
34ccb63e 226 exit_cleanup(1);
c627d613
AT
227 }
228 return NULL;
229 }
230 if (flist->count > 1) {
231 fprintf(stderr,"ERROR: destination must be a directory when copying more than 1 file\n");
34ccb63e 232 exit_cleanup(1);
c627d613
AT
233 }
234 return name;
235 }
236
237 if (flist->count == 1)
238 return name;
239
240 if (!name)
241 return NULL;
242
243 if (mkdir(name,0777) != 0) {
244 fprintf(stderr,"mkdir %s : %s\n",name,strerror(errno));
34ccb63e 245 exit_cleanup(1);
94481d91
AT
246 } else {
247 fprintf(am_server?stderr:stdout,"created directory %s\n",name);
c627d613
AT
248 }
249
250 if (chdir(name) != 0) {
251 fprintf(stderr,"chdir %s : %s\n",name,strerror(errno));
34ccb63e 252 exit_cleanup(1);
c627d613
AT
253 }
254
255 return NULL;
256}
257
258
259
260
261void do_server_sender(int argc,char *argv[])
262{
263 int i;
264 char *dir = argv[0];
265 struct file_list *flist;
266
267 if (verbose > 2)
268 fprintf(stderr,"server_sender starting pid=%d\n",(int)getpid());
269
270 if (chdir(dir) != 0) {
271 fprintf(stderr,"chdir %s: %s\n",dir,strerror(errno));
34ccb63e 272 exit_cleanup(1);
c627d613
AT
273 }
274 argc--;
275 argv++;
276
277 if (strcmp(dir,".")) {
278 int l = strlen(dir);
279 for (i=0;i<argc;i++)
280 argv[i] += l+1;
281 }
282
283 if (argc == 0 && recurse) {
284 argc=1;
285 argv--;
286 argv[0] = ".";
287 }
288
289
290 flist = send_file_list(STDOUT_FILENO,recurse,argc,argv);
291 send_files(flist,STDOUT_FILENO,STDIN_FILENO);
292 report(STDOUT_FILENO);
34ccb63e 293 exit_cleanup(0);
c627d613
AT
294}
295
296
297
298void do_server_recv(int argc,char *argv[])
299{
300 int pid,status;
301 char *dir = NULL;
302 struct file_list *flist;
303 char *local_name=NULL;
304
305 if (verbose > 2)
306 fprintf(stderr,"server_recv(%d) starting pid=%d\n",argc,(int)getpid());
307
308 if (argc > 0) {
309 dir = argv[0];
310 argc--;
311 argv++;
312 if (chdir(dir) != 0) {
313 fprintf(stderr,"chdir %s : %s\n",dir,strerror(errno));
34ccb63e 314 exit_cleanup(1);
c627d613
AT
315 }
316 }
317
318 if (delete_mode)
319 recv_exclude_list(STDIN_FILENO);
320
321 flist = recv_file_list(STDIN_FILENO);
322 if (!flist || flist->count == 0) {
323 fprintf(stderr,"nothing to do\n");
34ccb63e 324 exit_cleanup(1);
c627d613
AT
325 }
326
327 if (argc > 0) {
328 if (strcmp(dir,".")) {
329 argv[0] += strlen(dir);
330 if (argv[0][0] == '/') argv[0]++;
331 }
332 local_name = get_local_name(flist,argv[0]);
333 }
334
335 if ((pid=fork()) == 0) {
336 recv_files(STDIN_FILENO,flist,local_name);
337 if (verbose > 2)
338 fprintf(stderr,"receiver read %d\n",read_total());
34ccb63e 339 exit_cleanup(0);
c627d613
AT
340 }
341
342 generate_files(STDOUT_FILENO,flist,local_name);
343
344 waitpid(pid, &status, 0);
34ccb63e 345 exit_cleanup(status);
c627d613
AT
346}
347
348
349static void usage(FILE *f)
350{
351 fprintf(f,"rsync version %s Copyright Andrew Tridgell and Paul Mackerras\n\n",
352 VERSION);
353 fprintf(f,"Usage:\t%s [options] src user@host:dest\nOR",RSYNC_NAME);
354 fprintf(f,"\t%s [options] user@host:src dest\n\n",RSYNC_NAME);
355 fprintf(f,"Options:\n");
356 fprintf(f,"-v, --verbose increase verbosity\n");
357 fprintf(f,"-c, --checksum always checksum\n");
358 fprintf(f,"-a, --archive archive mode (same as -rlptDog)\n");
359 fprintf(f,"-r, --recursive recurse into directories\n");
360 fprintf(f,"-b, --backup make backups (default ~ extension)\n");
361 fprintf(f,"-u, --update update only (don't overwrite newer files)\n");
362 fprintf(f,"-l, --links preserve soft links\n");
363 fprintf(f,"-p, --perms preserve permissions\n");
364 fprintf(f,"-o, --owner preserve owner (root only)\n");
365 fprintf(f,"-g, --group preserve group\n");
366 fprintf(f,"-D, --devices preserve devices (root only)\n");
367 fprintf(f,"-t, --times preserve times\n");
368 fprintf(f,"-n, --dry-run show what would have been transferred\n");
369 fprintf(f,"-x, --one-file-system don't cross filesystem boundaries\n");
370 fprintf(f,"-B, --block-size SIZE checksum blocking size\n");
371 fprintf(f,"-e, --rsh COMMAND specify rsh replacement\n");
372 fprintf(f," --rsync-path PATH specify path to rsync on the remote machine\n");
373 fprintf(f,"-C, --cvs-exclude auto ignore files in the same way CVS does\n");
374 fprintf(f," --delete delete files that don't exist on the sending side\n");
375 fprintf(f,"-I, --ignore-times don't exclude files that match length and time\n");
376 fprintf(f," --exclude FILE exclude file FILE\n");
377 fprintf(f," --exclude-from FILE exclude files listed in FILE\n");
378 fprintf(f," --suffix SUFFIX override backup suffix\n");
43a481dc 379 fprintf(f," --csum-length LENGTH set the checksum length\n");
c627d613
AT
380 fprintf(f," --version print version number\n");
381
382 fprintf(f,"\n");
383 fprintf(f,"the backup suffix defaults to %s\n",BACKUP_SUFFIX);
384 fprintf(f,"the block size defaults to %d\n",BLOCK_SIZE);
385}
386
387enum {OPT_VERSION,OPT_SUFFIX,OPT_SENDER,OPT_SERVER,OPT_EXCLUDE,
43a481dc 388 OPT_EXCLUDE_FROM,OPT_DELETE,OPT_RSYNC_PATH,OPT_CSUM_LENGTH};
c627d613
AT
389
390static char *short_options = "oblpguDCtcahvrIxne:B:";
391
392static struct option long_options[] = {
393 {"version", 0, 0, OPT_VERSION},
394 {"server", 0, 0, OPT_SERVER},
395 {"sender", 0, 0, OPT_SENDER},
396 {"delete", 0, 0, OPT_DELETE},
397 {"exclude", 1, 0, OPT_EXCLUDE},
398 {"exclude-from",1, 0, OPT_EXCLUDE_FROM},
399 {"rsync-path", 1, 0, OPT_RSYNC_PATH},
43a481dc 400 {"csum-length", 1, 0, OPT_CSUM_LENGTH},
c627d613
AT
401 {"one-file-system",0, 0, 'x'},
402 {"ignore-times",0, 0, 'I'},
403 {"help", 0, 0, 'h'},
404 {"dry-run", 0, 0, 'n'},
405 {"cvs-exclude", 0, 0, 'C'},
406 {"archive", 0, 0, 'a'},
407 {"checksum", 0, 0, 'c'},
408 {"backup", 0, 0, 'b'},
409 {"update", 0, 0, 'u'},
410 {"verbose", 0, 0, 'v'},
411 {"recursive", 0, 0, 'r'},
412 {"devices", 0, 0, 'D'},
413 {"perms", 0, 0, 'p'},
414 {"links", 0, 0, 'l'},
415 {"owner", 0, 0, 'o'},
416 {"group", 0, 0, 'g'},
417 {"times", 0, 0, 't'},
418 {"rsh", 1, 0, 'e'},
419 {"suffix", 1, 0, OPT_SUFFIX},
420 {"block-size", 1, 0, 'B'},
421 {0,0,0,0}};
422
423int main(int argc,char *argv[])
424{
425 int pid, status, pid2, status2;
426 int opt;
427 int option_index;
428 char *shell_cmd = NULL;
429 char *shell_machine = NULL;
430 char *shell_path = NULL;
431 char *shell_user = NULL;
432 char *p;
433 int f_in,f_out;
434 struct file_list *flist;
435 char *local_name = NULL;
436
437 starttime = time(NULL);
438
34ccb63e
AT
439 checksum_init();
440
c627d613
AT
441 while ((opt = getopt_long(argc, argv,
442 short_options, long_options, &option_index))
443 != -1) {
444 switch (opt)
445 {
446 case OPT_VERSION:
447 printf("rsync version %s protocol version %d\n",
448 VERSION,PROTOCOL_VERSION);
34ccb63e 449 exit_cleanup(0);
c627d613
AT
450
451 case OPT_SUFFIX:
452 backup_suffix = optarg;
453 break;
454
455 case OPT_RSYNC_PATH:
456 rsync_path = optarg;
457 break;
458
43a481dc
AT
459 case OPT_CSUM_LENGTH:
460 csum_length = atoi(optarg);
461 csum_length = MIN(csum_length,SUM_LENGTH);
462 break;
463
c627d613
AT
464 case 'I':
465 ignore_times = 1;
466 break;
467
468 case 'x':
469 one_file_system=1;
470 break;
471
472 case OPT_DELETE:
473 delete_mode = 1;
474 break;
475
476 case OPT_EXCLUDE:
477 add_exclude(optarg);
478 break;
479
480 case OPT_EXCLUDE_FROM:
481 add_exclude_file(optarg,1);
482 break;
483
484 case 'h':
485 usage(stdout);
34ccb63e 486 exit_cleanup(0);
c627d613
AT
487
488 case 'b':
489 make_backups=1;
490 break;
491
492 case 'n':
493 dry_run=1;
494 break;
495
496 case 'C':
497 cvs_exclude=1;
498 break;
499
500 case 'u':
501 update_only=1;
502 break;
503
504#if SUPPORT_LINKS
505 case 'l':
506 preserve_links=1;
507 break;
508#endif
509
510 case 'p':
511 preserve_perms=1;
512 break;
513
514 case 'o':
515 if (getuid() == 0) {
516 preserve_uid=1;
517 } else {
518 fprintf(stderr,"-o only allowed for root\n");
34ccb63e 519 exit_cleanup(1);
c627d613
AT
520 }
521 break;
522
523 case 'g':
524 preserve_gid=1;
525 break;
526
527 case 'D':
528 if (getuid() == 0) {
529 preserve_devices=1;
530 } else {
531 fprintf(stderr,"-D only allowed for root\n");
34ccb63e 532 exit_cleanup(1);
c627d613
AT
533 }
534 break;
535
536 case 't':
537 preserve_times=1;
538 break;
539
540 case 'c':
541 always_checksum=1;
542 break;
543
544 case 'v':
545 verbose++;
546 break;
547
548 case 'a':
549 recurse=1;
550#if SUPPORT_LINKS
551 preserve_links=1;
552#endif
553 preserve_perms=1;
554 preserve_times=1;
555 preserve_gid=1;
556 if (getuid() == 0) {
557 preserve_devices=1;
558 preserve_uid=1;
559 }
560 break;
561
562 case OPT_SERVER:
563 am_server = 1;
564 break;
565
566 case OPT_SENDER:
567 if (!am_server) {
568 usage(stderr);
34ccb63e 569 exit_cleanup(1);
c627d613
AT
570 }
571 sender = 1;
572 break;
573
574 case 'r':
575 recurse = 1;
576 break;
577
578 case 'e':
579 shell_cmd = optarg;
580 break;
581
582 case 'B':
583 block_size = atoi(optarg);
584 break;
585
586 default:
587 fprintf(stderr,"bad option -%c\n",opt);
34ccb63e 588 exit_cleanup(1);
c627d613
AT
589 }
590 }
591
592 while (optind--) {
593 argc--;
594 argv++;
595 }
596
597 if (dry_run)
598 verbose = MAX(verbose,1);
599
600 if (am_server) {
4fe159a8 601 remote_version = read_int(STDIN_FILENO);
7bec6a5c
AT
602 if (remote_version < MIN_PROTOCOL_VERSION ||
603 remote_version > MAX_PROTOCOL_VERSION) {
604 fprintf(stderr,"protocol version mismatch - is your shell clean?\n");
34ccb63e 605 exit_cleanup(1);
c627d613
AT
606 }
607 write_int(STDOUT_FILENO,PROTOCOL_VERSION);
608 write_flush(STDOUT_FILENO);
4fe159a8
AT
609
610 setup_protocol();
c627d613
AT
611
612 if (sender) {
613 recv_exclude_list(STDIN_FILENO);
614 if (cvs_exclude)
615 add_cvs_excludes();
616 do_server_sender(argc,argv);
617 } else {
618 do_server_recv(argc,argv);
619 }
34ccb63e 620 exit_cleanup(0);
c627d613
AT
621 }
622
623 if (argc < 2) {
624 usage(stderr);
34ccb63e 625 exit_cleanup(1);
c627d613
AT
626 }
627
628 p = strchr(argv[0],':');
629
630 if (p) {
631 sender = 0;
632 *p = 0;
633 shell_machine = argv[0];
634 shell_path = p+1;
635 argc--;
636 argv++;
637 } else {
638 sender = 1;
639
640 p = strchr(argv[argc-1],':');
641 if (!p) {
642 local_server = 1;
643 }
644
645 if (local_server) {
646 shell_machine = NULL;
647 shell_path = argv[argc-1];
648 } else {
649 *p = 0;
650 shell_machine = argv[argc-1];
651 shell_path = p+1;
652 }
653 argc--;
654 }
655
656 if (shell_machine) {
657 p = strchr(shell_machine,'@');
658 if (p) {
659 *p = 0;
660 shell_user = shell_machine;
661 shell_machine = p+1;
662 }
663 }
664
665 if (verbose > 3) {
666 fprintf(stderr,"cmd=%s machine=%s user=%s path=%s\n",
667 shell_cmd?shell_cmd:"",
668 shell_machine?shell_machine:"",
669 shell_user?shell_user:"",
670 shell_path?shell_path:"");
671 }
672
673 signal(SIGCHLD,SIG_IGN);
ac1eb754 674 signal(SIGINT,SIGNAL_CAST sig_int);
58d433ab 675 signal(SIGPIPE,SIGNAL_CAST sig_int);
c627d613
AT
676
677 if (!sender && argc != 1) {
678 usage(stderr);
34ccb63e 679 exit_cleanup(1);
c627d613
AT
680 }
681
682 pid = do_cmd(shell_cmd,shell_machine,shell_user,shell_path,&f_in,&f_out);
683
684 write_int(f_out,PROTOCOL_VERSION);
685 write_flush(f_out);
686 {
4fe159a8 687 remote_version = read_int(f_in);
7bec6a5c
AT
688 if (remote_version < MIN_PROTOCOL_VERSION ||
689 remote_version > MAX_PROTOCOL_VERSION) {
690 fprintf(stderr,"protocol version mismatch - is your shell clean?\n");
34ccb63e 691 exit_cleanup(1);
c627d613
AT
692 }
693 }
694
4fe159a8
AT
695 setup_protocol();
696
c627d613
AT
697 if (verbose > 3)
698 fprintf(stderr,"parent=%d child=%d sender=%d recurse=%d\n",
699 (int)getpid(),pid,sender,recurse);
700
701 if (sender) {
702 if (cvs_exclude)
703 add_cvs_excludes();
704 if (delete_mode)
705 send_exclude_list(f_out);
706 flist = send_file_list(f_out,recurse,argc,argv);
707 if (verbose > 3)
708 fprintf(stderr,"file list sent\n");
709 send_files(flist,f_out,f_in);
710 if (verbose > 3)
711 fprintf(stderr,"waiting on %d\n",pid);
712 waitpid(pid, &status, 0);
713 report(-1);
34ccb63e 714 exit_cleanup(status);
c627d613
AT
715 }
716
717 send_exclude_list(f_out);
718
719 flist = recv_file_list(f_in);
720 if (!flist || flist->count == 0) {
721 fprintf(stderr,"nothing to do\n");
34ccb63e 722 exit_cleanup(0);
c627d613
AT
723 }
724
725 local_name = get_local_name(flist,argv[0]);
726
727 if ((pid2=fork()) == 0) {
728 recv_files(f_in,flist,local_name);
729 if (verbose > 1)
730 fprintf(stderr,"receiver read %d\n",read_total());
34ccb63e 731 exit_cleanup(0);
c627d613
AT
732 }
733
734 generate_files(f_out,flist,local_name);
735
736 waitpid(pid2, &status2, 0);
737
738 report(f_in);
739
740 waitpid(pid, &status, 0);
741
742 return status | status2;
743}