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;
59 extern int csum_length;
65 static void usage(FILE *f);
67 static void report(int f)
70 time_t t = time(NULL);
74 if (am_server && sender) {
75 write_longint(f,read_total());
76 write_longint(f,write_total());
77 write_longint(f,total_size);
88 out = read_longint(f);
89 tsize = read_longint(f);
93 printf("wrote %lld bytes read %lld bytes %g bytes/sec\n",
94 (long long)out,(long long)in,(in+out)/(0.5 + (t-starttime)));
95 printf("total size is %lld speedup is %g\n",
96 (long long)tsize,(1.0*tsize)/(in+out));
98 printf("wrote %ld bytes read %ld bytes %g bytes/sec\n",
99 (long)out,(long)in,(in+out)/(0.5 + (t-starttime)));
100 printf("total size is %ld speedup is %g\n",
101 (long)tsize,(1.0*tsize)/(in+out));
106 static void server_options(char **args,int *argc)
109 static char argstr[50];
110 static char bsize[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);
171 if (strcmp(backup_suffix, BACKUP_SUFFIX)) {
172 args[ac++] = "--suffix";
173 args[ac++] = backup_suffix;
177 args[ac++] = "--delete";
180 args[ac++] = "--force";
183 args[ac++] = "--numeric-ids";
186 args[ac++] = "--temp-dir";
195 static int do_cmd(char *cmd,char *machine,char *user,char *path,int *f_in,int *f_out)
203 cmd = getenv(RSYNC_RSH_ENV);
210 for (tok=strtok(cmd," ");tok;tok=strtok(NULL," ")) {
215 /* remsh (on HPUX) takes the arguments the other way around */
216 args[argc++] = machine;
226 args[argc++] = machine;
230 args[argc++] = rsync_path;
232 server_options(args,&argc);
242 fprintf(FERROR,"cmd=");
244 fprintf(FERROR,"%s ",args[i]);
245 fprintf(FERROR,"\n");
248 ret = piped_child(args,f_in,f_out);
254 out_of_memory("do_cmd");
255 return 0; /* not reached */
261 static char *get_local_name(struct file_list *flist,char *name)
265 if (stat(name,&st) == 0) {
266 if (S_ISDIR(st.st_mode)) {
267 if (chdir(name) != 0) {
268 fprintf(FERROR,"chdir %s : %s (1)\n",name,strerror(errno));
273 if (flist->count > 1) {
274 fprintf(FERROR,"ERROR: destination must be a directory when copying more than 1 file\n");
280 if (flist->count == 1)
286 if (mkdir(name,0777 & ~orig_umask) != 0) {
287 fprintf(FERROR,"mkdir %s : %s (1)\n",name,strerror(errno));
290 fprintf(FINFO,"created directory %s\n",name);
293 if (chdir(name) != 0) {
294 fprintf(FERROR,"chdir %s : %s (2)\n",name,strerror(errno));
304 void do_server_sender(int argc,char *argv[])
307 struct file_list *flist;
311 fprintf(FERROR,"server_sender starting pid=%d\n",(int)getpid());
313 if (!relative_paths && chdir(dir) != 0) {
314 fprintf(FERROR,"chdir %s: %s (3)\n",dir,strerror(errno));
320 if (strcmp(dir,".")) {
322 if (strcmp(dir,"/") == 0)
328 if (argc == 0 && recurse) {
335 flist = send_file_list(STDOUT_FILENO,argc,argv);
336 send_files(flist,STDOUT_FILENO,STDIN_FILENO);
337 report(STDOUT_FILENO);
342 static int do_recv(int f_in,int f_out,struct file_list *flist,char *local_name)
348 if (preserve_hard_links)
349 init_hard_links(flist);
351 if (pipe(recv_pipe) < 0) {
352 fprintf(FERROR,"pipe failed in do_recv\n");
357 if ((pid=do_fork()) == 0) {
358 recv_files(f_in,flist,local_name,recv_pipe[1]);
360 fprintf(FERROR,"receiver read %ld\n",(long)read_total());
364 generate_files(f_out,flist,local_name,recv_pipe[0]);
366 waitpid(pid, &status, 0);
372 void do_server_recv(int argc,char *argv[])
375 struct file_list *flist;
376 char *local_name=NULL;
380 fprintf(FERROR,"server_recv(%d) starting pid=%d\n",argc,(int)getpid());
386 if (chdir(dir) != 0) {
387 fprintf(FERROR,"chdir %s : %s (4)\n",
388 dir,strerror(errno));
394 recv_exclude_list(STDIN_FILENO);
396 flist = recv_file_list(STDIN_FILENO);
397 if (!flist || flist->count == 0) {
398 fprintf(FERROR,"nothing to do\n");
403 if (strcmp(dir,".")) {
404 argv[0] += strlen(dir);
405 if (argv[0][0] == '/') argv[0]++;
407 local_name = get_local_name(flist,argv[0]);
410 status = do_recv(STDIN_FILENO,STDOUT_FILENO,flist,local_name);
411 exit_cleanup(status);
415 static void usage(FILE *f)
417 fprintf(f,"rsync version %s Copyright Andrew Tridgell and Paul Mackerras\n\n",
419 fprintf(f,"Usage:\t%s [options] src user@host:dest\nOR",RSYNC_NAME);
420 fprintf(f,"\t%s [options] user@host:src dest\n\n",RSYNC_NAME);
421 fprintf(f,"Options:\n");
422 fprintf(f,"-v, --verbose increase verbosity\n");
423 fprintf(f,"-c, --checksum always checksum\n");
424 fprintf(f,"-a, --archive archive mode (same as -rlptDog)\n");
425 fprintf(f,"-r, --recursive recurse into directories\n");
426 fprintf(f,"-R, --relative use relative path names\n");
427 fprintf(f,"-b, --backup make backups (default ~ extension)\n");
428 fprintf(f,"-u, --update update only (don't overwrite newer files)\n");
429 fprintf(f,"-l, --links preserve soft links\n");
430 fprintf(f,"-L, --copy-links treat soft links like regular files\n");
431 fprintf(f,"-H, --hard-links preserve hard links\n");
432 fprintf(f,"-p, --perms preserve permissions\n");
433 fprintf(f,"-o, --owner preserve owner (root only)\n");
434 fprintf(f,"-g, --group preserve group\n");
435 fprintf(f,"-D, --devices preserve devices (root only)\n");
436 fprintf(f,"-t, --times preserve times\n");
437 fprintf(f,"-S, --sparse handle sparse files efficiently\n");
438 fprintf(f,"-n, --dry-run show what would have been transferred\n");
439 fprintf(f,"-W, --whole-file copy whole files, no incremental checks\n");
440 fprintf(f,"-x, --one-file-system don't cross filesystem boundaries\n");
441 fprintf(f,"-B, --block-size SIZE checksum blocking size\n");
442 fprintf(f,"-e, --rsh COMMAND specify rsh replacement\n");
443 fprintf(f," --rsync-path PATH specify path to rsync on the remote machine\n");
444 fprintf(f,"-C, --cvs-exclude auto ignore files in the same way CVS does\n");
445 fprintf(f," --delete delete files that don't exist on the sending side\n");
446 fprintf(f," --force force deletion of directories even if not empty\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");
457 fprintf(f,"the backup suffix defaults to %s\n",BACKUP_SUFFIX);
458 fprintf(f,"the block size defaults to %d\n",BLOCK_SIZE);
461 enum {OPT_VERSION,OPT_SUFFIX,OPT_SENDER,OPT_SERVER,OPT_EXCLUDE,
462 OPT_EXCLUDE_FROM,OPT_DELETE,OPT_NUMERIC_IDS,OPT_RSYNC_PATH,OPT_FORCE};
464 static char *short_options = "oblLWHpguDCtcahvrRIxnSe:B:T:z";
466 static 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 {"force", 0, 0, OPT_FORCE},
472 {"numeric-ids", 0, 0, OPT_NUMERIC_IDS},
473 {"exclude", 1, 0, OPT_EXCLUDE},
474 {"exclude-from",1, 0, OPT_EXCLUDE_FROM},
475 {"rsync-path", 1, 0, OPT_RSYNC_PATH},
476 {"one-file-system",0, 0, 'x'},
477 {"ignore-times",0, 0, 'I'},
479 {"dry-run", 0, 0, 'n'},
480 {"sparse", 0, 0, 'S'},
481 {"cvs-exclude", 0, 0, 'C'},
482 {"archive", 0, 0, 'a'},
483 {"checksum", 0, 0, 'c'},
484 {"backup", 0, 0, 'b'},
485 {"update", 0, 0, 'u'},
486 {"verbose", 0, 0, 'v'},
487 {"recursive", 0, 0, 'r'},
488 {"relative", 0, 0, 'R'},
489 {"devices", 0, 0, 'D'},
490 {"perms", 0, 0, 'p'},
491 {"links", 0, 0, 'l'},
492 {"copy-links", 0, 0, 'L'},
493 {"whole-file", 0, 0, 'W'},
494 {"hard-links", 0, 0, 'H'},
495 {"owner", 0, 0, 'o'},
496 {"group", 0, 0, 'g'},
497 {"times", 0, 0, 't'},
499 {"suffix", 1, 0, OPT_SUFFIX},
500 {"block-size", 1, 0, 'B'},
501 {"temp-dir", 1, 0, 'T'},
502 {"compress", 0, 0, 'z'},
505 RETSIGTYPE sigusr1_handler(int val) {
509 int main(int argc,char *argv[])
511 int pid, status = 0, status2 = 0;
514 char *shell_cmd = NULL;
515 char *shell_machine = NULL;
516 char *shell_path = NULL;
517 char *shell_user = NULL;
520 struct file_list *flist;
521 char *local_name = NULL;
523 signal(SIGUSR1, sigusr1_handler);
525 starttime = time(NULL);
526 am_root = (getuid() == 0);
528 /* we set a 0 umask so that correct file permissions can be
530 orig_umask = (int)umask(0);
532 while ((opt = getopt_long(argc, argv,
533 short_options, long_options, &option_index))
538 printf("rsync version %s protocol version %d\n",
539 VERSION,PROTOCOL_VERSION);
543 backup_suffix = optarg;
566 case OPT_NUMERIC_IDS:
574 case OPT_EXCLUDE_FROM:
575 add_exclude_file(optarg,1);
615 #if SUPPORT_HARD_LINKS
616 preserve_hard_links=1;
618 fprintf(FERROR,"ERROR: hard links not supported on this platform\n");
690 block_size = atoi(optarg);
702 /* fprintf(FERROR,"bad option -%c\n",opt); */
712 signal(SIGCHLD,SIG_IGN);
713 signal(SIGINT,SIGNAL_CAST sig_int);
714 signal(SIGPIPE,SIGNAL_CAST sig_int);
715 signal(SIGHUP,SIGNAL_CAST sig_int);
718 verbose = MAX(verbose,1);
720 #ifndef SUPPORT_LINKS
721 if (!am_server && preserve_links) {
722 fprintf(FERROR,"ERROR: symbolic links not supported\n");
728 setup_protocol(STDOUT_FILENO,STDIN_FILENO);
731 recv_exclude_list(STDIN_FILENO);
734 do_server_sender(argc,argv);
736 do_server_recv(argc,argv);
746 p = strchr(argv[0],':');
751 shell_machine = argv[0];
758 p = strchr(argv[argc-1],':');
764 shell_machine = NULL;
765 shell_path = argv[argc-1];
768 shell_machine = argv[argc-1];
775 p = strchr(shell_machine,'@');
778 shell_user = shell_machine;
784 fprintf(FERROR,"cmd=%s machine=%s user=%s path=%s\n",
785 shell_cmd?shell_cmd:"",
786 shell_machine?shell_machine:"",
787 shell_user?shell_user:"",
788 shell_path?shell_path:"");
791 if (!sender && argc != 1) {
796 pid = do_cmd(shell_cmd,shell_machine,shell_user,shell_path,&f_in,&f_out);
798 setup_protocol(f_out,f_in);
806 fprintf(FERROR,"parent=%d child=%d sender=%d recurse=%d\n",
807 (int)getpid(),pid,sender,recurse);
813 send_exclude_list(f_out);
814 flist = send_file_list(f_out,argc,argv);
816 fprintf(FERROR,"file list sent\n");
817 send_files(flist,f_out,f_in);
819 fprintf(FERROR,"waiting on %d\n",pid);
820 waitpid(pid, &status, 0);
822 exit_cleanup(status);
825 send_exclude_list(f_out);
827 flist = recv_file_list(f_in);
828 if (!flist || flist->count == 0) {
829 fprintf(FERROR,"nothing to do\n");
833 local_name = get_local_name(flist,argv[0]);
835 status2 = do_recv(f_in,f_out,flist,local_name);
839 waitpid(pid, &status, 0);
841 return status | status2;