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;
61 static char *shell_cmd;
63 extern int csum_length;
69 static void usage(FILE *f);
71 static void report(int f)
74 time_t t = time(NULL);
78 if (am_server && am_sender) {
79 write_longint(f,read_total());
80 write_longint(f,write_total());
81 write_longint(f,total_size);
92 out = read_longint(f);
93 tsize = read_longint(f);
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));
103 static void server_options(char **args,int *argc)
106 static char argstr[50];
107 static char bsize[30];
108 static char iotime[30];
111 args[ac++] = "--server";
114 args[ac++] = "--sender";
118 for (i=0;i<verbose;i++)
132 if (preserve_hard_links)
138 if (preserve_devices)
162 if (x != 1) args[ac++] = argstr;
164 if (block_size != BLOCK_SIZE) {
165 sprintf(bsize,"-B%d",block_size);
170 sprintf(iotime,"--timeout=%d",io_timeout);
174 if (strcmp(backup_suffix, BACKUP_SUFFIX)) {
175 args[ac++] = "--suffix";
176 args[ac++] = backup_suffix;
180 args[ac++] = "--delete";
183 args[ac++] = "--force";
186 args[ac++] = "--numeric-ids";
189 args[ac++] = "--temp-dir";
198 static int do_cmd(char *cmd,char *machine,char *user,char *path,int *f_in,int *f_out)
206 cmd = getenv(RSYNC_RSH_ENV);
213 for (tok=strtok(cmd," ");tok;tok=strtok(NULL," ")) {
218 /* remsh (on HPUX) takes the arguments the other way around */
219 args[argc++] = machine;
229 args[argc++] = machine;
232 args[argc++] = rsync_path;
234 server_options(args,&argc);
245 fprintf(FINFO,"cmd=");
247 fprintf(FINFO,"%s ",args[i]);
252 ret = local_child(argc, args, f_in, f_out);
254 ret = piped_child(args,f_in,f_out);
262 out_of_memory("do_cmd");
263 return 0; /* not reached */
269 static char *get_local_name(struct file_list *flist,char *name)
273 if (do_stat(name,&st) == 0) {
274 if (S_ISDIR(st.st_mode)) {
275 if (chdir(name) != 0) {
276 fprintf(FERROR,"chdir %s : %s (1)\n",name,strerror(errno));
281 if (flist->count > 1) {
282 fprintf(FERROR,"ERROR: destination must be a directory when copying more than 1 file\n");
288 if (flist->count == 1)
294 if (do_mkdir(name,0777 & ~orig_umask) != 0) {
295 fprintf(FERROR,"mkdir %s : %s (1)\n",name,strerror(errno));
298 fprintf(FINFO,"created directory %s\n",name);
301 if (chdir(name) != 0) {
302 fprintf(FERROR,"chdir %s : %s (2)\n",name,strerror(errno));
312 void do_server_sender(int argc,char *argv[])
315 struct file_list *flist;
319 fprintf(FINFO,"server_sender starting pid=%d\n",(int)getpid());
321 if (!relative_paths && chdir(dir) != 0) {
322 fprintf(FERROR,"chdir %s: %s (3)\n",dir,strerror(errno));
328 if (strcmp(dir,".")) {
330 if (strcmp(dir,"/") == 0)
336 if (argc == 0 && recurse) {
343 flist = send_file_list(STDOUT_FILENO,argc,argv);
344 send_files(flist,STDOUT_FILENO,STDIN_FILENO);
345 report(STDOUT_FILENO);
350 static int do_recv(int f_in,int f_out,struct file_list *flist,char *local_name)
356 if (preserve_hard_links)
357 init_hard_links(flist);
359 if (pipe(recv_pipe) < 0) {
360 fprintf(FERROR,"pipe failed in do_recv\n");
365 if ((pid=do_fork()) == 0) {
366 recv_files(f_in,flist,local_name,recv_pipe[1]);
368 fprintf(FINFO,"receiver read %ld\n",(long)read_total());
372 generate_files(f_out,flist,local_name,recv_pipe[0]);
374 waitpid(pid, &status, 0);
380 void do_server_recv(int argc,char *argv[])
383 struct file_list *flist;
384 char *local_name=NULL;
388 fprintf(FINFO,"server_recv(%d) starting pid=%d\n",argc,(int)getpid());
394 if (chdir(dir) != 0) {
395 fprintf(FERROR,"chdir %s : %s (4)\n",
396 dir,strerror(errno));
402 recv_exclude_list(STDIN_FILENO);
404 flist = recv_file_list(STDIN_FILENO);
405 if (!flist || flist->count == 0) {
406 fprintf(FERROR,"nothing to do\n");
411 if (strcmp(dir,".")) {
412 argv[0] += strlen(dir);
413 if (argv[0][0] == '/') argv[0]++;
415 local_name = get_local_name(flist,argv[0]);
418 status = do_recv(STDIN_FILENO,STDOUT_FILENO,flist,local_name);
419 exit_cleanup(status);
423 void start_server(int argc, char *argv[])
425 setup_protocol(STDOUT_FILENO,STDIN_FILENO);
428 recv_exclude_list(STDIN_FILENO);
431 do_server_sender(argc,argv);
433 do_server_recv(argc,argv);
438 int start_client(int argc, char *argv[])
441 char *shell_machine = NULL;
442 char *shell_path = NULL;
443 char *shell_user = NULL;
444 int pid, status = 0, status2 = 0;
446 struct file_list *flist;
447 char *local_name = NULL;
449 p = strchr(argv[0],':');
454 shell_machine = argv[0];
461 p = strchr(argv[argc-1],':');
467 shell_machine = NULL;
468 shell_path = argv[argc-1];
471 shell_machine = argv[argc-1];
478 p = strchr(shell_machine,'@');
481 shell_user = shell_machine;
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:"");
494 if (!am_sender && argc != 1) {
499 pid = do_cmd(shell_cmd,shell_machine,shell_user,shell_path,&f_in,&f_out);
501 setup_protocol(f_out,f_in);
509 fprintf(FINFO,"parent=%d child=%d sender=%d recurse=%d\n",
510 (int)getpid(),pid,am_sender,recurse);
516 send_exclude_list(f_out);
517 flist = send_file_list(f_out,argc,argv);
519 fprintf(FINFO,"file list sent\n");
520 send_files(flist,f_out,f_in);
522 fprintf(FINFO,"waiting on %d\n",pid);
523 waitpid(pid, &status, 0);
525 exit_cleanup(status);
528 send_exclude_list(f_out);
530 flist = recv_file_list(f_in);
531 if (!flist || flist->count == 0) {
532 fprintf(FINFO,"nothing to do\n");
536 local_name = get_local_name(flist,argv[0]);
538 status2 = do_recv(f_in,f_out,flist,local_name);
542 waitpid(pid, &status, 0);
544 return status | status2;
548 static void usage(FILE *f)
550 fprintf(f,"rsync version %s Copyright Andrew Tridgell and Paul Mackerras\n\n",
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");
559 fprintf(f,"-R, --relative use relative path names\n");
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");
563 fprintf(f,"-L, --copy-links treat soft links like regular files\n");
564 fprintf(f,"-H, --hard-links preserve hard links\n");
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");
570 fprintf(f,"-S, --sparse handle sparse files efficiently\n");
571 fprintf(f,"-n, --dry-run show what would have been transferred\n");
572 fprintf(f,"-W, --whole-file copy whole files, no incremental checks\n");
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");
579 fprintf(f," --force force deletion of directories even if not empty\n");
580 fprintf(f," --numeric-ids don't map uid/gid values by user/group name\n");
581 fprintf(f," --timeout TIME set IO timeout in seconds\n");
582 fprintf(f,"-I, --ignore-times don't exclude files that match length and time\n");
583 fprintf(f,"-T --temp-dir DIR create temporary files in directory DIR\n");
584 fprintf(f,"-z, --compress compress file data\n");
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");
591 fprintf(f,"the backup suffix defaults to %s\n",BACKUP_SUFFIX);
592 fprintf(f,"the block size defaults to %d\n",BLOCK_SIZE);
595 enum {OPT_VERSION,OPT_SUFFIX,OPT_SENDER,OPT_SERVER,OPT_EXCLUDE,
596 OPT_EXCLUDE_FROM,OPT_DELETE,OPT_NUMERIC_IDS,OPT_RSYNC_PATH,
597 OPT_FORCE,OPT_TIMEOUT};
599 static char *short_options = "oblLWHpguDCtcahvrRIxnSe:B:T:z";
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},
606 {"force", 0, 0, OPT_FORCE},
607 {"numeric-ids", 0, 0, OPT_NUMERIC_IDS},
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'},
614 {"dry-run", 0, 0, 'n'},
615 {"sparse", 0, 0, 'S'},
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'},
623 {"relative", 0, 0, 'R'},
624 {"devices", 0, 0, 'D'},
625 {"perms", 0, 0, 'p'},
626 {"links", 0, 0, 'l'},
627 {"copy-links", 0, 0, 'L'},
628 {"whole-file", 0, 0, 'W'},
629 {"hard-links", 0, 0, 'H'},
630 {"owner", 0, 0, 'o'},
631 {"group", 0, 0, 'g'},
632 {"times", 0, 0, 't'},
634 {"suffix", 1, 0, OPT_SUFFIX},
635 {"block-size", 1, 0, 'B'},
636 {"timeout", 1, 0, OPT_TIMEOUT},
637 {"temp-dir", 1, 0, 'T'},
638 {"compress", 0, 0, 'z'},
641 RETSIGTYPE sigusr1_handler(int val) {
646 static void parse_arguments(int argc, char *argv[])
651 while ((opt = getopt_long(argc, argv,
652 short_options, long_options, &option_index))
657 printf("rsync version %s protocol version %d\n",
658 VERSION,PROTOCOL_VERSION);
662 backup_suffix = optarg;
685 case OPT_NUMERIC_IDS:
693 case OPT_EXCLUDE_FROM:
694 add_exclude_file(optarg,1);
734 #if SUPPORT_HARD_LINKS
735 preserve_hard_links=1;
737 fprintf(FERROR,"ERROR: hard links not supported on this platform\n");
809 block_size = atoi(optarg);
813 io_timeout = atoi(optarg);
825 /* fprintf(FERROR,"bad option -%c\n",opt); */
831 int main(int argc,char *argv[])
834 signal(SIGUSR1, sigusr1_handler);
836 starttime = time(NULL);
837 am_root = (getuid() == 0);
839 /* we set a 0 umask so that correct file permissions can be
841 orig_umask = (int)umask(0);
843 parse_arguments(argc, argv);
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);
857 verbose = MAX(verbose,1);
859 #ifndef SUPPORT_LINKS
860 if (!am_server && preserve_links) {
861 fprintf(FERROR,"ERROR: symbolic links not supported\n");
867 start_server(argc, argv);
875 return start_client(argc, argv);