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