2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
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.
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.
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.
23 int always_checksum = 0;
26 int block_size=BLOCK_SIZE;
28 char *backup_suffix = BACKUP_SUFFIX;
31 static char *rsync_path = RSYNC_NAME;
36 int preserve_links = 0;
37 int preserve_hard_links = 0;
38 int preserve_perms = 0;
39 int preserve_devices = 0;
42 int preserve_times = 0;
49 int one_file_system=0;
60 static int port = RSYNC_PORT;
62 static char *shell_cmd;
64 extern int csum_length;
71 static void usage(int fd);
73 static void report(int f)
76 time_t t = time(NULL);
80 if (am_server && am_sender) {
81 write_longint(f,read_total());
82 write_longint(f,write_total());
83 write_longint(f,total_size);
94 out = read_longint(f);
95 tsize = read_longint(f);
98 printf("wrote %.0f bytes read %.0f bytes %.2f bytes/sec\n",
99 (double)out,(double)in,(in+out)/(0.5 + (t-starttime)));
100 printf("total size is %.0f speedup is %.2f\n",
101 (double)tsize,(1.0*tsize)/(in+out));
105 static void server_options(char **args,int *argc)
108 static char argstr[50];
109 static char bsize[30];
110 static char iotime[30];
113 args[ac++] = "--server";
116 args[ac++] = "--sender";
120 for (i=0;i<verbose;i++)
134 if (preserve_hard_links)
140 if (preserve_devices)
164 if (x != 1) args[ac++] = argstr;
166 if (block_size != BLOCK_SIZE) {
167 sprintf(bsize,"-B%d",block_size);
172 sprintf(iotime,"--timeout=%d",io_timeout);
176 if (strcmp(backup_suffix, BACKUP_SUFFIX)) {
177 args[ac++] = "--suffix";
178 args[ac++] = backup_suffix;
182 args[ac++] = "--delete";
185 args[ac++] = "--force";
188 args[ac++] = "--numeric-ids";
191 args[ac++] = "--temp-dir";
200 static int do_cmd(char *cmd,char *machine,char *user,char *path,int *f_in,int *f_out)
208 cmd = getenv(RSYNC_RSH_ENV);
215 for (tok=strtok(cmd," ");tok;tok=strtok(NULL," ")) {
220 /* remsh (on HPUX) takes the arguments the other way around */
221 args[argc++] = machine;
231 args[argc++] = machine;
234 args[argc++] = rsync_path;
236 server_options(args,&argc);
247 rprintf(FINFO,"cmd=");
249 rprintf(FINFO,"%s ",args[i]);
254 ret = local_child(argc, args, f_in, f_out);
256 ret = piped_child(args,f_in,f_out);
264 out_of_memory("do_cmd");
265 return 0; /* not reached */
271 static char *get_local_name(struct file_list *flist,char *name)
275 if (do_stat(name,&st) == 0) {
276 if (S_ISDIR(st.st_mode)) {
277 if (chdir(name) != 0) {
278 rprintf(FERROR,"chdir %s : %s (1)\n",name,strerror(errno));
283 if (flist->count > 1) {
284 rprintf(FERROR,"ERROR: destination must be a directory when copying more than 1 file\n");
290 if (flist->count == 1)
296 if (do_mkdir(name,0777 & ~orig_umask) != 0) {
297 rprintf(FERROR,"mkdir %s : %s (1)\n",name,strerror(errno));
300 rprintf(FINFO,"created directory %s\n",name);
303 if (chdir(name) != 0) {
304 rprintf(FERROR,"chdir %s : %s (2)\n",name,strerror(errno));
314 static void do_server_sender(int f_in, int f_out, int argc,char *argv[])
317 struct file_list *flist;
321 rprintf(FINFO,"server_sender starting pid=%d\n",(int)getpid());
323 if (!relative_paths && chdir(dir) != 0) {
324 rprintf(FERROR,"chdir %s: %s (3)\n",dir,strerror(errno));
330 if (strcmp(dir,".")) {
332 if (strcmp(dir,"/") == 0)
338 if (argc == 0 && recurse) {
345 flist = send_file_list(f_out,argc,argv);
346 send_files(flist,f_out,f_in);
352 static int do_recv(int f_in,int f_out,struct file_list *flist,char *local_name)
358 if (preserve_hard_links)
359 init_hard_links(flist);
361 if (pipe(recv_pipe) < 0) {
362 rprintf(FERROR,"pipe failed in do_recv\n");
367 if ((pid=do_fork()) == 0) {
368 recv_files(f_in,flist,local_name,recv_pipe[1]);
370 rprintf(FINFO,"receiver read %ld\n",(long)read_total());
374 generate_files(f_out,flist,local_name,recv_pipe[0]);
376 waitpid(pid, &status, 0);
382 static void do_server_recv(int f_in, int f_out, int argc,char *argv[])
385 struct file_list *flist;
386 char *local_name=NULL;
390 rprintf(FINFO,"server_recv(%d) starting pid=%d\n",argc,(int)getpid());
396 if (chdir(dir) != 0) {
397 rprintf(FERROR,"chdir %s : %s (4)\n",
398 dir,strerror(errno));
404 recv_exclude_list(f_in);
406 flist = recv_file_list(f_in);
407 if (!flist || flist->count == 0) {
408 rprintf(FERROR,"nothing to do\n");
413 if (strcmp(dir,".")) {
414 argv[0] += strlen(dir);
415 if (argv[0][0] == '/') argv[0]++;
417 local_name = get_local_name(flist,argv[0]);
420 status = do_recv(f_in,f_out,flist,local_name);
421 exit_cleanup(status);
425 void start_server(int f_in, int f_out, int argc, char *argv[])
427 setup_protocol(f_out, f_in);
430 recv_exclude_list(f_in);
433 do_server_sender(f_in, f_out, argc, argv);
435 do_server_recv(f_in, f_out, argc, argv);
440 static int client_run(int f_in, int f_out, int pid, int argc, char *argv[])
442 struct file_list *flist;
443 int status = 0, status2 = 0;
444 char *local_name = NULL;
446 setup_protocol(f_out,f_in);
452 send_exclude_list(f_out);
453 flist = send_file_list(f_out,argc,argv);
455 rprintf(FINFO,"file list sent\n");
456 send_files(flist,f_out,f_in);
459 rprintf(FINFO,"waiting on %d\n",pid);
460 waitpid(pid, &status, 0);
463 exit_cleanup(status);
466 send_exclude_list(f_out);
468 flist = recv_file_list(f_in);
469 if (!flist || flist->count == 0) {
470 rprintf(FINFO,"nothing to do\n");
474 local_name = get_local_name(flist,argv[0]);
476 status2 = do_recv(f_in,f_out,flist,local_name);
481 waitpid(pid, &status, 0);
484 return status | status2;
488 int start_socket_client(char *host, char *path, int argc, char *argv[])
494 fd = open_socket_out(host, port);
496 rprintf(FERROR,"failed to connect to %s - %s\n", host, strerror(errno));
500 server_options(sargs,&sargc);
502 sargs[sargc++] = ".";
505 sargs[sargc++] = path;
509 return client_run(fd, fd, -1, argc, argv);
512 int start_client(int argc, char *argv[])
515 char *shell_machine = NULL;
516 char *shell_path = NULL;
517 char *shell_user = NULL;
521 p = strchr(argv[0],':');
526 return start_socket_client(argv[0], p+2, argc-1, argv+1);
530 shell_machine = argv[0];
537 p = strchr(argv[argc-1],':');
540 } else if (p[1] == ':') {
542 return start_socket_client(argv[argc-1], p+2, argc-1, argv);
546 shell_machine = NULL;
547 shell_path = argv[argc-1];
550 shell_machine = argv[argc-1];
557 p = strchr(shell_machine,'@');
560 shell_user = shell_machine;
566 rprintf(FINFO,"cmd=%s machine=%s user=%s path=%s\n",
567 shell_cmd?shell_cmd:"",
568 shell_machine?shell_machine:"",
569 shell_user?shell_user:"",
570 shell_path?shell_path:"");
573 if (!am_sender && argc != 1) {
578 pid = do_cmd(shell_cmd,shell_machine,shell_user,shell_path,&f_in,&f_out);
585 return client_run(f_in, f_out, pid, argc, argv);
589 static void usage(int F)
591 rprintf(F,"rsync version %s Copyright Andrew Tridgell and Paul Mackerras\n\n",
593 rprintf(F,"Usage:\t%s [options] src user@host:dest\nOR",RSYNC_NAME);
594 rprintf(F,"\t%s [options] user@host:src dest\n\n",RSYNC_NAME);
595 rprintf(F,"Options:\n");
596 rprintf(F,"-v, --verbose increase verbosity\n");
597 rprintf(F,"-c, --checksum always checksum\n");
598 rprintf(F,"-a, --archive archive mode (same as -rlptDog)\n");
599 rprintf(F,"-r, --recursive recurse into directories\n");
600 rprintf(F,"-R, --relative use relative path names\n");
601 rprintf(F,"-b, --backup make backups (default ~ extension)\n");
602 rprintf(F,"-u, --update update only (don't overwrite newer files)\n");
603 rprintf(F,"-l, --links preserve soft links\n");
604 rprintf(F,"-L, --copy-links treat soft links like regular files\n");
605 rprintf(F,"-H, --hard-links preserve hard links\n");
606 rprintf(F,"-p, --perms preserve permissions\n");
607 rprintf(F,"-o, --owner preserve owner (root only)\n");
608 rprintf(F,"-g, --group preserve group\n");
609 rprintf(F,"-D, --devices preserve devices (root only)\n");
610 rprintf(F,"-t, --times preserve times\n");
611 rprintf(F,"-S, --sparse handle sparse files efficiently\n");
612 rprintf(F,"-n, --dry-run show what would have been transferred\n");
613 rprintf(F,"-W, --whole-file copy whole files, no incremental checks\n");
614 rprintf(F,"-x, --one-file-system don't cross filesystem boundaries\n");
615 rprintf(F,"-B, --block-size SIZE checksum blocking size\n");
616 rprintf(F,"-e, --rsh COMMAND specify rsh replacement\n");
617 rprintf(F," --rsync-path PATH specify path to rsync on the remote machine\n");
618 rprintf(F,"-C, --cvs-exclude auto ignore files in the same way CVS does\n");
619 rprintf(F," --delete delete files that don't exist on the sending side\n");
620 rprintf(F," --force force deletion of directories even if not empty\n");
621 rprintf(F," --numeric-ids don't map uid/gid values by user/group name\n");
622 rprintf(F," --timeout TIME set IO timeout in seconds\n");
623 rprintf(F,"-I, --ignore-times don't exclude files that match length and time\n");
624 rprintf(F,"-T --temp-dir DIR create temporary files in directory DIR\n");
625 rprintf(F,"-z, --compress compress file data\n");
626 rprintf(F," --exclude FILE exclude file FILE\n");
627 rprintf(F," --exclude-from FILE exclude files listed in FILE\n");
628 rprintf(F," --suffix SUFFIX override backup suffix\n");
629 rprintf(F," --version print version number\n");
632 rprintf(F,"the backup suffix defaults to %s\n",BACKUP_SUFFIX);
633 rprintf(F,"the block size defaults to %d\n",BLOCK_SIZE);
636 enum {OPT_VERSION,OPT_SUFFIX,OPT_SENDER,OPT_SERVER,OPT_EXCLUDE,
637 OPT_EXCLUDE_FROM,OPT_DELETE,OPT_NUMERIC_IDS,OPT_RSYNC_PATH,
638 OPT_FORCE,OPT_TIMEOUT,OPT_DAEMON};
640 static char *short_options = "oblLWHpguDCtcahvrRIxnSe:B:T:z";
642 static struct option long_options[] = {
643 {"version", 0, 0, OPT_VERSION},
644 {"server", 0, 0, OPT_SERVER},
645 {"sender", 0, 0, OPT_SENDER},
646 {"delete", 0, 0, OPT_DELETE},
647 {"force", 0, 0, OPT_FORCE},
648 {"numeric-ids", 0, 0, OPT_NUMERIC_IDS},
649 {"exclude", 1, 0, OPT_EXCLUDE},
650 {"exclude-from",1, 0, OPT_EXCLUDE_FROM},
651 {"rsync-path", 1, 0, OPT_RSYNC_PATH},
652 {"one-file-system",0, 0, 'x'},
653 {"ignore-times",0, 0, 'I'},
655 {"dry-run", 0, 0, 'n'},
656 {"sparse", 0, 0, 'S'},
657 {"cvs-exclude", 0, 0, 'C'},
658 {"archive", 0, 0, 'a'},
659 {"checksum", 0, 0, 'c'},
660 {"backup", 0, 0, 'b'},
661 {"update", 0, 0, 'u'},
662 {"verbose", 0, 0, 'v'},
663 {"recursive", 0, 0, 'r'},
664 {"relative", 0, 0, 'R'},
665 {"devices", 0, 0, 'D'},
666 {"perms", 0, 0, 'p'},
667 {"links", 0, 0, 'l'},
668 {"copy-links", 0, 0, 'L'},
669 {"whole-file", 0, 0, 'W'},
670 {"hard-links", 0, 0, 'H'},
671 {"owner", 0, 0, 'o'},
672 {"group", 0, 0, 'g'},
673 {"times", 0, 0, 't'},
675 {"suffix", 1, 0, OPT_SUFFIX},
676 {"block-size", 1, 0, 'B'},
677 {"timeout", 1, 0, OPT_TIMEOUT},
678 {"temp-dir", 1, 0, 'T'},
679 {"compress", 0, 0, 'z'},
680 {"daemon", 0, 0, OPT_DAEMON},
683 RETSIGTYPE sigusr1_handler(int val) {
688 static void parse_arguments(int argc, char *argv[])
693 while ((opt = getopt_long(argc, argv,
694 short_options, long_options, &option_index))
699 printf("rsync version %s protocol version %d\n",
700 VERSION,PROTOCOL_VERSION);
704 backup_suffix = optarg;
727 case OPT_NUMERIC_IDS:
735 case OPT_EXCLUDE_FROM:
736 add_exclude_file(optarg,1);
776 #if SUPPORT_HARD_LINKS
777 preserve_hard_links=1;
779 rprintf(FERROR,"ERROR: hard links not supported on this platform\n");
851 block_size = atoi(optarg);
855 io_timeout = atoi(optarg);
871 /* rprintf(FERROR,"bad option -%c\n",opt); */
877 int main(int argc,char *argv[])
880 signal(SIGUSR1, sigusr1_handler);
882 starttime = time(NULL);
883 am_root = (getuid() == 0);
885 /* we set a 0 umask so that correct file permissions can be
887 orig_umask = (int)umask(0);
889 parse_arguments(argc, argv);
897 signal(SIGCHLD,SIG_IGN);
898 signal(SIGINT,SIGNAL_CAST sig_int);
899 signal(SIGPIPE,SIGNAL_CAST sig_int);
900 signal(SIGHUP,SIGNAL_CAST sig_int);
903 verbose = MAX(verbose,1);
905 #ifndef SUPPORT_LINKS
906 if (!am_server && preserve_links) {
907 rprintf(FERROR,"ERROR: symbolic links not supported\n");
913 start_server(STDIN_FILENO, STDOUT_FILENO, argc, argv);
921 return start_client(argc, argv);