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