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 && 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);
95 printf("wrote %lld bytes read %lld bytes %g bytes/sec\n",
96 (long long)out,(long long)in,(in+out)/(0.5 + (t-starttime)));
97 printf("total size is %lld speedup is %g\n",
98 (long long)tsize,(1.0*tsize)/(in+out));
100 printf("wrote %ld bytes read %ld bytes %g bytes/sec\n",
101 (long)out,(long)in,(in+out)/(0.5 + (t-starttime)));
102 printf("total size is %ld speedup is %g\n",
103 (long)tsize,(1.0*tsize)/(in+out));
108 static void server_options(char **args,int *argc)
111 static char argstr[50];
112 static char bsize[30];
113 static char iotime[30];
116 args[ac++] = "--server";
119 args[ac++] = "--sender";
123 for (i=0;i<verbose;i++)
137 if (preserve_hard_links)
143 if (preserve_devices)
167 if (x != 1) args[ac++] = argstr;
169 if (block_size != BLOCK_SIZE) {
170 sprintf(bsize,"-B%d",block_size);
175 sprintf(iotime,"--timeout=%d",io_timeout);
179 if (strcmp(backup_suffix, BACKUP_SUFFIX)) {
180 args[ac++] = "--suffix";
181 args[ac++] = backup_suffix;
185 args[ac++] = "--delete";
188 args[ac++] = "--force";
191 args[ac++] = "--numeric-ids";
194 args[ac++] = "--temp-dir";
203 static int do_cmd(char *cmd,char *machine,char *user,char *path,int *f_in,int *f_out)
211 cmd = getenv(RSYNC_RSH_ENV);
218 for (tok=strtok(cmd," ");tok;tok=strtok(NULL," ")) {
223 /* remsh (on HPUX) takes the arguments the other way around */
224 args[argc++] = machine;
234 args[argc++] = machine;
238 args[argc++] = rsync_path;
240 server_options(args,&argc);
250 fprintf(FINFO,"cmd=");
252 fprintf(FINFO,"%s ",args[i]);
256 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 (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 (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 static void usage(FILE *f)
425 fprintf(f,"rsync version %s Copyright Andrew Tridgell and Paul Mackerras\n\n",
427 fprintf(f,"Usage:\t%s [options] src user@host:dest\nOR",RSYNC_NAME);
428 fprintf(f,"\t%s [options] user@host:src dest\n\n",RSYNC_NAME);
429 fprintf(f,"Options:\n");
430 fprintf(f,"-v, --verbose increase verbosity\n");
431 fprintf(f,"-c, --checksum always checksum\n");
432 fprintf(f,"-a, --archive archive mode (same as -rlptDog)\n");
433 fprintf(f,"-r, --recursive recurse into directories\n");
434 fprintf(f,"-R, --relative use relative path names\n");
435 fprintf(f,"-b, --backup make backups (default ~ extension)\n");
436 fprintf(f,"-u, --update update only (don't overwrite newer files)\n");
437 fprintf(f,"-l, --links preserve soft links\n");
438 fprintf(f,"-L, --copy-links treat soft links like regular files\n");
439 fprintf(f,"-H, --hard-links preserve hard links\n");
440 fprintf(f,"-p, --perms preserve permissions\n");
441 fprintf(f,"-o, --owner preserve owner (root only)\n");
442 fprintf(f,"-g, --group preserve group\n");
443 fprintf(f,"-D, --devices preserve devices (root only)\n");
444 fprintf(f,"-t, --times preserve times\n");
445 fprintf(f,"-S, --sparse handle sparse files efficiently\n");
446 fprintf(f,"-n, --dry-run show what would have been transferred\n");
447 fprintf(f,"-W, --whole-file copy whole files, no incremental checks\n");
448 fprintf(f,"-x, --one-file-system don't cross filesystem boundaries\n");
449 fprintf(f,"-B, --block-size SIZE checksum blocking size\n");
450 fprintf(f,"-e, --rsh COMMAND specify rsh replacement\n");
451 fprintf(f," --rsync-path PATH specify path to rsync on the remote machine\n");
452 fprintf(f,"-C, --cvs-exclude auto ignore files in the same way CVS does\n");
453 fprintf(f," --delete delete files that don't exist on the sending side\n");
454 fprintf(f," --force force deletion of directories even if not empty\n");
455 fprintf(f," --numeric-ids don't map uid/gid values by user/group name\n");
456 fprintf(f," --timeout TIME set IO timeout in seconds\n");
457 fprintf(f,"-I, --ignore-times don't exclude files that match length and time\n");
458 fprintf(f,"-T --temp-dir DIR create temporary files in directory DIR\n");
459 fprintf(f,"-z, --compress compress file data\n");
460 fprintf(f," --exclude FILE exclude file FILE\n");
461 fprintf(f," --exclude-from FILE exclude files listed in FILE\n");
462 fprintf(f," --suffix SUFFIX override backup suffix\n");
463 fprintf(f," --version print version number\n");
466 fprintf(f,"the backup suffix defaults to %s\n",BACKUP_SUFFIX);
467 fprintf(f,"the block size defaults to %d\n",BLOCK_SIZE);
470 enum {OPT_VERSION,OPT_SUFFIX,OPT_SENDER,OPT_SERVER,OPT_EXCLUDE,
471 OPT_EXCLUDE_FROM,OPT_DELETE,OPT_NUMERIC_IDS,OPT_RSYNC_PATH,
472 OPT_FORCE,OPT_TIMEOUT};
474 static char *short_options = "oblLWHpguDCtcahvrRIxnSe:B:T:z";
476 static struct option long_options[] = {
477 {"version", 0, 0, OPT_VERSION},
478 {"server", 0, 0, OPT_SERVER},
479 {"sender", 0, 0, OPT_SENDER},
480 {"delete", 0, 0, OPT_DELETE},
481 {"force", 0, 0, OPT_FORCE},
482 {"numeric-ids", 0, 0, OPT_NUMERIC_IDS},
483 {"exclude", 1, 0, OPT_EXCLUDE},
484 {"exclude-from",1, 0, OPT_EXCLUDE_FROM},
485 {"rsync-path", 1, 0, OPT_RSYNC_PATH},
486 {"one-file-system",0, 0, 'x'},
487 {"ignore-times",0, 0, 'I'},
489 {"dry-run", 0, 0, 'n'},
490 {"sparse", 0, 0, 'S'},
491 {"cvs-exclude", 0, 0, 'C'},
492 {"archive", 0, 0, 'a'},
493 {"checksum", 0, 0, 'c'},
494 {"backup", 0, 0, 'b'},
495 {"update", 0, 0, 'u'},
496 {"verbose", 0, 0, 'v'},
497 {"recursive", 0, 0, 'r'},
498 {"relative", 0, 0, 'R'},
499 {"devices", 0, 0, 'D'},
500 {"perms", 0, 0, 'p'},
501 {"links", 0, 0, 'l'},
502 {"copy-links", 0, 0, 'L'},
503 {"whole-file", 0, 0, 'W'},
504 {"hard-links", 0, 0, 'H'},
505 {"owner", 0, 0, 'o'},
506 {"group", 0, 0, 'g'},
507 {"times", 0, 0, 't'},
509 {"suffix", 1, 0, OPT_SUFFIX},
510 {"block-size", 1, 0, 'B'},
511 {"timeout", 1, 0, OPT_TIMEOUT},
512 {"temp-dir", 1, 0, 'T'},
513 {"compress", 0, 0, 'z'},
516 RETSIGTYPE sigusr1_handler(int val) {
520 int main(int argc,char *argv[])
522 int pid, status = 0, status2 = 0;
525 char *shell_cmd = NULL;
526 char *shell_machine = NULL;
527 char *shell_path = NULL;
528 char *shell_user = NULL;
531 struct file_list *flist;
532 char *local_name = NULL;
534 signal(SIGUSR1, sigusr1_handler);
536 starttime = time(NULL);
537 am_root = (getuid() == 0);
539 /* we set a 0 umask so that correct file permissions can be
541 orig_umask = (int)umask(0);
543 while ((opt = getopt_long(argc, argv,
544 short_options, long_options, &option_index))
549 printf("rsync version %s protocol version %d\n",
550 VERSION,PROTOCOL_VERSION);
554 backup_suffix = optarg;
577 case OPT_NUMERIC_IDS:
585 case OPT_EXCLUDE_FROM:
586 add_exclude_file(optarg,1);
626 #if SUPPORT_HARD_LINKS
627 preserve_hard_links=1;
629 fprintf(FERROR,"ERROR: hard links not supported on this platform\n");
701 block_size = atoi(optarg);
705 io_timeout = atoi(optarg);
717 /* fprintf(FERROR,"bad option -%c\n",opt); */
727 signal(SIGCHLD,SIG_IGN);
728 signal(SIGINT,SIGNAL_CAST sig_int);
729 signal(SIGPIPE,SIGNAL_CAST sig_int);
730 signal(SIGHUP,SIGNAL_CAST sig_int);
733 verbose = MAX(verbose,1);
735 #ifndef SUPPORT_LINKS
736 if (!am_server && preserve_links) {
737 fprintf(FERROR,"ERROR: symbolic links not supported\n");
743 setup_protocol(STDOUT_FILENO,STDIN_FILENO);
746 recv_exclude_list(STDIN_FILENO);
749 do_server_sender(argc,argv);
751 do_server_recv(argc,argv);
761 p = strchr(argv[0],':');
766 shell_machine = argv[0];
773 p = strchr(argv[argc-1],':');
779 shell_machine = NULL;
780 shell_path = argv[argc-1];
783 shell_machine = argv[argc-1];
790 p = strchr(shell_machine,'@');
793 shell_user = shell_machine;
799 fprintf(FINFO,"cmd=%s machine=%s user=%s path=%s\n",
800 shell_cmd?shell_cmd:"",
801 shell_machine?shell_machine:"",
802 shell_user?shell_user:"",
803 shell_path?shell_path:"");
806 if (!sender && argc != 1) {
811 pid = do_cmd(shell_cmd,shell_machine,shell_user,shell_path,&f_in,&f_out);
813 setup_protocol(f_out,f_in);
821 fprintf(FINFO,"parent=%d child=%d sender=%d recurse=%d\n",
822 (int)getpid(),pid,sender,recurse);
828 send_exclude_list(f_out);
829 flist = send_file_list(f_out,argc,argv);
831 fprintf(FINFO,"file list sent\n");
832 send_files(flist,f_out,f_in);
834 fprintf(FINFO,"waiting on %d\n",pid);
835 waitpid(pid, &status, 0);
837 exit_cleanup(status);
840 send_exclude_list(f_out);
842 flist = recv_file_list(f_in);
843 if (!flist || flist->count == 0) {
844 fprintf(FINFO,"nothing to do\n");
848 local_name = get_local_name(flist,argv[0]);
850 status2 = do_recv(f_in,f_out,flist,local_name);
854 waitpid(pid, &status, 0);
856 return status | status2;