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 extern int csum_length;
67 static void usage(FILE *f);
69 static void report(int f)
72 time_t t = time(NULL);
76 if (am_server && am_sender) {
77 write_longint(f,read_total());
78 write_longint(f,write_total());
79 write_longint(f,total_size);
90 out = read_longint(f);
91 tsize = read_longint(f);
94 printf("wrote %.0f bytes read %.0f bytes %.2f bytes/sec\n",
95 (double)out,(double)in,(in+out)/(0.5 + (t-starttime)));
96 printf("total size is %.0f speedup is %.2f\n",
97 (double)tsize,(1.0*tsize)/(in+out));
101 static void server_options(char **args,int *argc)
104 static char argstr[50];
105 static char bsize[30];
106 static char iotime[30];
109 args[ac++] = "--server";
112 args[ac++] = "--sender";
116 for (i=0;i<verbose;i++)
130 if (preserve_hard_links)
136 if (preserve_devices)
160 if (x != 1) args[ac++] = argstr;
162 if (block_size != BLOCK_SIZE) {
163 sprintf(bsize,"-B%d",block_size);
168 sprintf(iotime,"--timeout=%d",io_timeout);
172 if (strcmp(backup_suffix, BACKUP_SUFFIX)) {
173 args[ac++] = "--suffix";
174 args[ac++] = backup_suffix;
178 args[ac++] = "--delete";
181 args[ac++] = "--force";
184 args[ac++] = "--numeric-ids";
187 args[ac++] = "--temp-dir";
196 static int do_cmd(char *cmd,char *machine,char *user,char *path,int *f_in,int *f_out)
204 cmd = getenv(RSYNC_RSH_ENV);
211 for (tok=strtok(cmd," ");tok;tok=strtok(NULL," ")) {
216 /* remsh (on HPUX) takes the arguments the other way around */
217 args[argc++] = machine;
227 args[argc++] = machine;
230 args[argc++] = rsync_path;
232 server_options(args,&argc);
243 fprintf(FINFO,"cmd=");
245 fprintf(FINFO,"%s ",args[i]);
250 ret = local_child(argc, args, f_in, f_out);
252 ret = piped_child(args,f_in,f_out);
260 out_of_memory("do_cmd");
261 return 0; /* not reached */
267 static char *get_local_name(struct file_list *flist,char *name)
271 if (do_stat(name,&st) == 0) {
272 if (S_ISDIR(st.st_mode)) {
273 if (chdir(name) != 0) {
274 fprintf(FERROR,"chdir %s : %s (1)\n",name,strerror(errno));
279 if (flist->count > 1) {
280 fprintf(FERROR,"ERROR: destination must be a directory when copying more than 1 file\n");
286 if (flist->count == 1)
292 if (do_mkdir(name,0777 & ~orig_umask) != 0) {
293 fprintf(FERROR,"mkdir %s : %s (1)\n",name,strerror(errno));
296 fprintf(FINFO,"created directory %s\n",name);
299 if (chdir(name) != 0) {
300 fprintf(FERROR,"chdir %s : %s (2)\n",name,strerror(errno));
310 void do_server_sender(int argc,char *argv[])
313 struct file_list *flist;
317 fprintf(FINFO,"server_sender starting pid=%d\n",(int)getpid());
319 if (!relative_paths && chdir(dir) != 0) {
320 fprintf(FERROR,"chdir %s: %s (3)\n",dir,strerror(errno));
326 if (strcmp(dir,".")) {
328 if (strcmp(dir,"/") == 0)
334 if (argc == 0 && recurse) {
341 flist = send_file_list(STDOUT_FILENO,argc,argv);
342 send_files(flist,STDOUT_FILENO,STDIN_FILENO);
343 report(STDOUT_FILENO);
348 static int do_recv(int f_in,int f_out,struct file_list *flist,char *local_name)
354 if (preserve_hard_links)
355 init_hard_links(flist);
357 if (pipe(recv_pipe) < 0) {
358 fprintf(FERROR,"pipe failed in do_recv\n");
363 if ((pid=do_fork()) == 0) {
364 recv_files(f_in,flist,local_name,recv_pipe[1]);
366 fprintf(FINFO,"receiver read %ld\n",(long)read_total());
370 generate_files(f_out,flist,local_name,recv_pipe[0]);
372 waitpid(pid, &status, 0);
378 void do_server_recv(int argc,char *argv[])
381 struct file_list *flist;
382 char *local_name=NULL;
386 fprintf(FINFO,"server_recv(%d) starting pid=%d\n",argc,(int)getpid());
392 if (chdir(dir) != 0) {
393 fprintf(FERROR,"chdir %s : %s (4)\n",
394 dir,strerror(errno));
400 recv_exclude_list(STDIN_FILENO);
402 flist = recv_file_list(STDIN_FILENO);
403 if (!flist || flist->count == 0) {
404 fprintf(FERROR,"nothing to do\n");
409 if (strcmp(dir,".")) {
410 argv[0] += strlen(dir);
411 if (argv[0][0] == '/') argv[0]++;
413 local_name = get_local_name(flist,argv[0]);
416 status = do_recv(STDIN_FILENO,STDOUT_FILENO,flist,local_name);
417 exit_cleanup(status);
421 void start_server(int argc, char *argv[])
423 setup_protocol(STDOUT_FILENO,STDIN_FILENO);
426 recv_exclude_list(STDIN_FILENO);
429 do_server_sender(argc,argv);
431 do_server_recv(argc,argv);
437 static void usage(FILE *f)
439 fprintf(f,"rsync version %s Copyright Andrew Tridgell and Paul Mackerras\n\n",
441 fprintf(f,"Usage:\t%s [options] src user@host:dest\nOR",RSYNC_NAME);
442 fprintf(f,"\t%s [options] user@host:src dest\n\n",RSYNC_NAME);
443 fprintf(f,"Options:\n");
444 fprintf(f,"-v, --verbose increase verbosity\n");
445 fprintf(f,"-c, --checksum always checksum\n");
446 fprintf(f,"-a, --archive archive mode (same as -rlptDog)\n");
447 fprintf(f,"-r, --recursive recurse into directories\n");
448 fprintf(f,"-R, --relative use relative path names\n");
449 fprintf(f,"-b, --backup make backups (default ~ extension)\n");
450 fprintf(f,"-u, --update update only (don't overwrite newer files)\n");
451 fprintf(f,"-l, --links preserve soft links\n");
452 fprintf(f,"-L, --copy-links treat soft links like regular files\n");
453 fprintf(f,"-H, --hard-links preserve hard links\n");
454 fprintf(f,"-p, --perms preserve permissions\n");
455 fprintf(f,"-o, --owner preserve owner (root only)\n");
456 fprintf(f,"-g, --group preserve group\n");
457 fprintf(f,"-D, --devices preserve devices (root only)\n");
458 fprintf(f,"-t, --times preserve times\n");
459 fprintf(f,"-S, --sparse handle sparse files efficiently\n");
460 fprintf(f,"-n, --dry-run show what would have been transferred\n");
461 fprintf(f,"-W, --whole-file copy whole files, no incremental checks\n");
462 fprintf(f,"-x, --one-file-system don't cross filesystem boundaries\n");
463 fprintf(f,"-B, --block-size SIZE checksum blocking size\n");
464 fprintf(f,"-e, --rsh COMMAND specify rsh replacement\n");
465 fprintf(f," --rsync-path PATH specify path to rsync on the remote machine\n");
466 fprintf(f,"-C, --cvs-exclude auto ignore files in the same way CVS does\n");
467 fprintf(f," --delete delete files that don't exist on the sending side\n");
468 fprintf(f," --force force deletion of directories even if not empty\n");
469 fprintf(f," --numeric-ids don't map uid/gid values by user/group name\n");
470 fprintf(f," --timeout TIME set IO timeout in seconds\n");
471 fprintf(f,"-I, --ignore-times don't exclude files that match length and time\n");
472 fprintf(f,"-T --temp-dir DIR create temporary files in directory DIR\n");
473 fprintf(f,"-z, --compress compress file data\n");
474 fprintf(f," --exclude FILE exclude file FILE\n");
475 fprintf(f," --exclude-from FILE exclude files listed in FILE\n");
476 fprintf(f," --suffix SUFFIX override backup suffix\n");
477 fprintf(f," --version print version number\n");
480 fprintf(f,"the backup suffix defaults to %s\n",BACKUP_SUFFIX);
481 fprintf(f,"the block size defaults to %d\n",BLOCK_SIZE);
484 enum {OPT_VERSION,OPT_SUFFIX,OPT_SENDER,OPT_SERVER,OPT_EXCLUDE,
485 OPT_EXCLUDE_FROM,OPT_DELETE,OPT_NUMERIC_IDS,OPT_RSYNC_PATH,
486 OPT_FORCE,OPT_TIMEOUT};
488 static char *short_options = "oblLWHpguDCtcahvrRIxnSe:B:T:z";
490 static struct option long_options[] = {
491 {"version", 0, 0, OPT_VERSION},
492 {"server", 0, 0, OPT_SERVER},
493 {"sender", 0, 0, OPT_SENDER},
494 {"delete", 0, 0, OPT_DELETE},
495 {"force", 0, 0, OPT_FORCE},
496 {"numeric-ids", 0, 0, OPT_NUMERIC_IDS},
497 {"exclude", 1, 0, OPT_EXCLUDE},
498 {"exclude-from",1, 0, OPT_EXCLUDE_FROM},
499 {"rsync-path", 1, 0, OPT_RSYNC_PATH},
500 {"one-file-system",0, 0, 'x'},
501 {"ignore-times",0, 0, 'I'},
503 {"dry-run", 0, 0, 'n'},
504 {"sparse", 0, 0, 'S'},
505 {"cvs-exclude", 0, 0, 'C'},
506 {"archive", 0, 0, 'a'},
507 {"checksum", 0, 0, 'c'},
508 {"backup", 0, 0, 'b'},
509 {"update", 0, 0, 'u'},
510 {"verbose", 0, 0, 'v'},
511 {"recursive", 0, 0, 'r'},
512 {"relative", 0, 0, 'R'},
513 {"devices", 0, 0, 'D'},
514 {"perms", 0, 0, 'p'},
515 {"links", 0, 0, 'l'},
516 {"copy-links", 0, 0, 'L'},
517 {"whole-file", 0, 0, 'W'},
518 {"hard-links", 0, 0, 'H'},
519 {"owner", 0, 0, 'o'},
520 {"group", 0, 0, 'g'},
521 {"times", 0, 0, 't'},
523 {"suffix", 1, 0, OPT_SUFFIX},
524 {"block-size", 1, 0, 'B'},
525 {"timeout", 1, 0, OPT_TIMEOUT},
526 {"temp-dir", 1, 0, 'T'},
527 {"compress", 0, 0, 'z'},
530 RETSIGTYPE sigusr1_handler(int val) {
534 int main(int argc,char *argv[])
536 int pid, status = 0, status2 = 0;
539 char *shell_cmd = NULL;
540 char *shell_machine = NULL;
541 char *shell_path = NULL;
542 char *shell_user = NULL;
545 struct file_list *flist;
546 char *local_name = NULL;
548 signal(SIGUSR1, sigusr1_handler);
550 starttime = time(NULL);
551 am_root = (getuid() == 0);
553 /* we set a 0 umask so that correct file permissions can be
555 orig_umask = (int)umask(0);
557 while ((opt = getopt_long(argc, argv,
558 short_options, long_options, &option_index))
563 printf("rsync version %s protocol version %d\n",
564 VERSION,PROTOCOL_VERSION);
568 backup_suffix = optarg;
591 case OPT_NUMERIC_IDS:
599 case OPT_EXCLUDE_FROM:
600 add_exclude_file(optarg,1);
640 #if SUPPORT_HARD_LINKS
641 preserve_hard_links=1;
643 fprintf(FERROR,"ERROR: hard links not supported on this platform\n");
715 block_size = atoi(optarg);
719 io_timeout = atoi(optarg);
731 /* fprintf(FERROR,"bad option -%c\n",opt); */
741 signal(SIGCHLD,SIG_IGN);
742 signal(SIGINT,SIGNAL_CAST sig_int);
743 signal(SIGPIPE,SIGNAL_CAST sig_int);
744 signal(SIGHUP,SIGNAL_CAST sig_int);
747 verbose = MAX(verbose,1);
749 #ifndef SUPPORT_LINKS
750 if (!am_server && preserve_links) {
751 fprintf(FERROR,"ERROR: symbolic links not supported\n");
757 start_server(argc, argv);
765 p = strchr(argv[0],':');
770 shell_machine = argv[0];
777 p = strchr(argv[argc-1],':');
783 shell_machine = NULL;
784 shell_path = argv[argc-1];
787 shell_machine = argv[argc-1];
794 p = strchr(shell_machine,'@');
797 shell_user = shell_machine;
803 fprintf(FINFO,"cmd=%s machine=%s user=%s path=%s\n",
804 shell_cmd?shell_cmd:"",
805 shell_machine?shell_machine:"",
806 shell_user?shell_user:"",
807 shell_path?shell_path:"");
810 if (!am_sender && argc != 1) {
815 pid = do_cmd(shell_cmd,shell_machine,shell_user,shell_path,&f_in,&f_out);
817 setup_protocol(f_out,f_in);
825 fprintf(FINFO,"parent=%d child=%d sender=%d recurse=%d\n",
826 (int)getpid(),pid,am_sender,recurse);
832 send_exclude_list(f_out);
833 flist = send_file_list(f_out,argc,argv);
835 fprintf(FINFO,"file list sent\n");
836 send_files(flist,f_out,f_in);
838 fprintf(FINFO,"waiting on %d\n",pid);
839 waitpid(pid, &status, 0);
841 exit_cleanup(status);
844 send_exclude_list(f_out);
846 flist = recv_file_list(f_in);
847 if (!flist || flist->count == 0) {
848 fprintf(FINFO,"nothing to do\n");
852 local_name = get_local_name(flist,argv[0]);
854 status2 = do_recv(f_in,f_out,flist,local_name);
858 waitpid(pid, &status, 0);
860 return status | status2;