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;
58 extern int csum_length;
64 static void usage(FILE *f);
66 static void report(int f)
69 time_t t = time(NULL);
73 if (am_server && sender) {
74 write_longint(f,read_total());
75 write_longint(f,write_total());
76 write_longint(f,total_size);
87 out = read_longint(f);
88 tsize = read_longint(f);
92 printf("wrote %lld bytes read %lld bytes %g bytes/sec\n",
93 (long long)out,(long long)in,(in+out)/(0.5 + (t-starttime)));
94 printf("total size is %lld speedup is %g\n",
95 (long long)tsize,(1.0*tsize)/(in+out));
97 printf("wrote %ld bytes read %ld bytes %g bytes/sec\n",
98 (long)out,(long)in,(in+out)/(0.5 + (t-starttime)));
99 printf("total size is %ld speedup is %g\n",
100 (long)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];
112 args[ac++] = "--server";
115 args[ac++] = "--sender";
119 for (i=0;i<verbose;i++)
133 if (preserve_hard_links)
139 if (preserve_devices)
163 if (x != 1) args[ac++] = argstr;
165 if (block_size != BLOCK_SIZE) {
166 sprintf(bsize,"-B%d",block_size);
170 if (strcmp(backup_suffix, BACKUP_SUFFIX)) {
171 args[ac++] = "--suffix";
172 args[ac++] = backup_suffix;
176 args[ac++] = "--delete";
179 args[ac++] = "--numeric-ids";
182 args[ac++] = "--temp-dir";
191 static int do_cmd(char *cmd,char *machine,char *user,char *path,int *f_in,int *f_out)
195 char *tok,*p,*dir=NULL;
199 cmd = getenv(RSYNC_RSH_ENV);
206 for (tok=strtok(cmd," ");tok;tok=strtok(NULL," ")) {
211 /* remsh (on HPUX) takes the arguments the other way around */
212 args[argc++] = machine;
222 args[argc++] = machine;
226 args[argc++] = rsync_path;
228 server_options(args,&argc);
238 fprintf(FERROR,"cmd=");
240 fprintf(FERROR,"%s ",args[i]);
241 fprintf(FERROR,"\n");
244 ret = piped_child(args,f_in,f_out);
250 out_of_memory("do_cmd");
251 return 0; /* not reached */
257 static char *get_local_name(struct file_list *flist,char *name)
261 if (stat(name,&st) == 0) {
262 if (S_ISDIR(st.st_mode)) {
263 if (chdir(name) != 0) {
264 fprintf(FERROR,"chdir %s : %s (1)\n",name,strerror(errno));
269 if (flist->count > 1) {
270 fprintf(FERROR,"ERROR: destination must be a directory when copying more than 1 file\n");
276 if (flist->count == 1)
282 if (mkdir(name,0777 & ~orig_umask) != 0) {
283 fprintf(FERROR,"mkdir %s : %s (1)\n",name,strerror(errno));
286 fprintf(FINFO,"created directory %s\n",name);
289 if (chdir(name) != 0) {
290 fprintf(FERROR,"chdir %s : %s (2)\n",name,strerror(errno));
300 void do_server_sender(int argc,char *argv[])
303 struct file_list *flist;
307 fprintf(FERROR,"server_sender starting pid=%d\n",(int)getpid());
309 if (!relative_paths && chdir(dir) != 0) {
310 fprintf(FERROR,"chdir %s: %s (3)\n",dir,strerror(errno));
316 if (strcmp(dir,".")) {
318 if (strcmp(dir,"/") == 0)
324 if (argc == 0 && recurse) {
331 flist = send_file_list(STDOUT_FILENO,argc,argv);
332 send_files(flist,STDOUT_FILENO,STDIN_FILENO);
333 report(STDOUT_FILENO);
338 static int do_recv(int f_in,int f_out,struct file_list *flist,char *local_name)
344 if (preserve_hard_links)
345 init_hard_links(flist);
347 if (pipe(recv_pipe) < 0) {
348 fprintf(FERROR,"pipe failed in do_recv\n");
353 if ((pid=do_fork()) == 0) {
354 recv_files(f_in,flist,local_name,recv_pipe[1]);
356 fprintf(FERROR,"receiver read %ld\n",(long)read_total());
360 generate_files(f_out,flist,local_name,recv_pipe[0]);
362 waitpid(pid, &status, 0);
368 void do_server_recv(int argc,char *argv[])
371 struct file_list *flist;
372 char *local_name=NULL;
376 fprintf(FERROR,"server_recv(%d) starting pid=%d\n",argc,(int)getpid());
382 if (chdir(dir) != 0) {
383 fprintf(FERROR,"chdir %s : %s (4)\n",
384 dir,strerror(errno));
390 recv_exclude_list(STDIN_FILENO);
392 flist = recv_file_list(STDIN_FILENO);
393 if (!flist || flist->count == 0) {
394 fprintf(FERROR,"nothing to do\n");
399 if (strcmp(dir,".")) {
400 argv[0] += strlen(dir);
401 if (argv[0][0] == '/') argv[0]++;
403 local_name = get_local_name(flist,argv[0]);
406 status = do_recv(STDIN_FILENO,STDOUT_FILENO,flist,local_name);
407 exit_cleanup(status);
411 static void usage(FILE *f)
413 fprintf(f,"rsync version %s Copyright Andrew Tridgell and Paul Mackerras\n\n",
415 fprintf(f,"Usage:\t%s [options] src user@host:dest\nOR",RSYNC_NAME);
416 fprintf(f,"\t%s [options] user@host:src dest\n\n",RSYNC_NAME);
417 fprintf(f,"Options:\n");
418 fprintf(f,"-v, --verbose increase verbosity\n");
419 fprintf(f,"-c, --checksum always checksum\n");
420 fprintf(f,"-a, --archive archive mode (same as -rlptDog)\n");
421 fprintf(f,"-r, --recursive recurse into directories\n");
422 fprintf(f,"-R, --relative use relative path names\n");
423 fprintf(f,"-b, --backup make backups (default ~ extension)\n");
424 fprintf(f,"-u, --update update only (don't overwrite newer files)\n");
425 fprintf(f,"-l, --links preserve soft links\n");
426 fprintf(f,"-L, --copy-links treat soft links like regular files\n");
427 fprintf(f,"-H, --hard-links preserve hard links\n");
428 fprintf(f,"-p, --perms preserve permissions\n");
429 fprintf(f,"-o, --owner preserve owner (root only)\n");
430 fprintf(f,"-g, --group preserve group\n");
431 fprintf(f,"-D, --devices preserve devices (root only)\n");
432 fprintf(f,"-t, --times preserve times\n");
433 fprintf(f,"-S, --sparse handle sparse files efficiently\n");
434 fprintf(f,"-n, --dry-run show what would have been transferred\n");
435 fprintf(f,"-W, --whole-file copy whole files, no incremental checks\n");
436 fprintf(f,"-x, --one-file-system don't cross filesystem boundaries\n");
437 fprintf(f,"-B, --block-size SIZE checksum blocking size\n");
438 fprintf(f,"-e, --rsh COMMAND specify rsh replacement\n");
439 fprintf(f," --rsync-path PATH specify path to rsync on the remote machine\n");
440 fprintf(f,"-C, --cvs-exclude auto ignore files in the same way CVS does\n");
441 fprintf(f," --delete delete files that don't exist on the sending side\n");
442 fprintf(f," --numeric-ids don't map uid/gid values by user/group name\n");
443 fprintf(f,"-I, --ignore-times don't exclude files that match length and time\n");
444 fprintf(f,"-T --temp-dir DIR create temporary files in directory DIR\n");
445 fprintf(f,"-z, --compress compress file data\n");
446 fprintf(f," --exclude FILE exclude file FILE\n");
447 fprintf(f," --exclude-from FILE exclude files listed in FILE\n");
448 fprintf(f," --suffix SUFFIX override backup suffix\n");
449 fprintf(f," --version print version number\n");
452 fprintf(f,"the backup suffix defaults to %s\n",BACKUP_SUFFIX);
453 fprintf(f,"the block size defaults to %d\n",BLOCK_SIZE);
456 enum {OPT_VERSION,OPT_SUFFIX,OPT_SENDER,OPT_SERVER,OPT_EXCLUDE,
457 OPT_EXCLUDE_FROM,OPT_DELETE,OPT_NUMERIC_IDS,OPT_RSYNC_PATH};
459 static char *short_options = "oblLWHpguDCtcahvrRIxnSe:B:T:z";
461 static struct option long_options[] = {
462 {"version", 0, 0, OPT_VERSION},
463 {"server", 0, 0, OPT_SERVER},
464 {"sender", 0, 0, OPT_SENDER},
465 {"delete", 0, 0, OPT_DELETE},
466 {"numeric-ids", 0, 0, OPT_NUMERIC_IDS},
467 {"exclude", 1, 0, OPT_EXCLUDE},
468 {"exclude-from",1, 0, OPT_EXCLUDE_FROM},
469 {"rsync-path", 1, 0, OPT_RSYNC_PATH},
470 {"one-file-system",0, 0, 'x'},
471 {"ignore-times",0, 0, 'I'},
473 {"dry-run", 0, 0, 'n'},
474 {"sparse", 0, 0, 'S'},
475 {"cvs-exclude", 0, 0, 'C'},
476 {"archive", 0, 0, 'a'},
477 {"checksum", 0, 0, 'c'},
478 {"backup", 0, 0, 'b'},
479 {"update", 0, 0, 'u'},
480 {"verbose", 0, 0, 'v'},
481 {"recursive", 0, 0, 'r'},
482 {"relative", 0, 0, 'R'},
483 {"devices", 0, 0, 'D'},
484 {"perms", 0, 0, 'p'},
485 {"links", 0, 0, 'l'},
486 {"copy-links", 0, 0, 'L'},
487 {"whole-file", 0, 0, 'W'},
488 {"hard-links", 0, 0, 'H'},
489 {"owner", 0, 0, 'o'},
490 {"group", 0, 0, 'g'},
491 {"times", 0, 0, 't'},
493 {"suffix", 1, 0, OPT_SUFFIX},
494 {"block-size", 1, 0, 'B'},
495 {"temp-dir", 1, 0, 'T'},
496 {"compress", 0, 0, 'z'},
499 RETSIGTYPE sigusr1_handler(int val) {
503 int main(int argc,char *argv[])
505 int pid, status = 0, status2 = 0;
508 char *shell_cmd = NULL;
509 char *shell_machine = NULL;
510 char *shell_path = NULL;
511 char *shell_user = NULL;
514 struct file_list *flist;
515 char *local_name = NULL;
517 signal(SIGUSR1, sigusr1_handler);
519 starttime = time(NULL);
520 am_root = (getuid() == 0);
522 /* we set a 0 umask so that correct file permissions can be
524 orig_umask = (int)umask(0);
526 while ((opt = getopt_long(argc, argv,
527 short_options, long_options, &option_index))
532 printf("rsync version %s protocol version %d\n",
533 VERSION,PROTOCOL_VERSION);
537 backup_suffix = optarg;
556 case OPT_NUMERIC_IDS:
564 case OPT_EXCLUDE_FROM:
565 add_exclude_file(optarg,1);
605 #if SUPPORT_HARD_LINKS
606 preserve_hard_links=1;
608 fprintf(FERROR,"ERROR: hard links not supported on this platform\n");
680 block_size = atoi(optarg);
692 /* fprintf(FERROR,"bad option -%c\n",opt); */
702 signal(SIGCHLD,SIG_IGN);
703 signal(SIGINT,SIGNAL_CAST sig_int);
704 signal(SIGPIPE,SIGNAL_CAST sig_int);
705 signal(SIGHUP,SIGNAL_CAST sig_int);
708 verbose = MAX(verbose,1);
710 #ifndef SUPPORT_LINKS
711 if (!am_server && preserve_links) {
712 fprintf(FERROR,"ERROR: symbolic links not supported\n");
718 setup_protocol(STDOUT_FILENO,STDIN_FILENO);
721 recv_exclude_list(STDIN_FILENO);
724 do_server_sender(argc,argv);
726 do_server_recv(argc,argv);
736 p = strchr(argv[0],':');
741 shell_machine = argv[0];
748 p = strchr(argv[argc-1],':');
754 shell_machine = NULL;
755 shell_path = argv[argc-1];
758 shell_machine = argv[argc-1];
765 p = strchr(shell_machine,'@');
768 shell_user = shell_machine;
774 fprintf(FERROR,"cmd=%s machine=%s user=%s path=%s\n",
775 shell_cmd?shell_cmd:"",
776 shell_machine?shell_machine:"",
777 shell_user?shell_user:"",
778 shell_path?shell_path:"");
781 if (!sender && argc != 1) {
786 pid = do_cmd(shell_cmd,shell_machine,shell_user,shell_path,&f_in,&f_out);
788 setup_protocol(f_out,f_in);
796 fprintf(FERROR,"parent=%d child=%d sender=%d recurse=%d\n",
797 (int)getpid(),pid,sender,recurse);
803 send_exclude_list(f_out);
804 flist = send_file_list(f_out,argc,argv);
806 fprintf(FERROR,"file list sent\n");
807 send_files(flist,f_out,f_in);
809 fprintf(FERROR,"waiting on %d\n",pid);
810 waitpid(pid, &status, 0);
812 exit_cleanup(status);
815 send_exclude_list(f_out);
817 flist = recv_file_list(f_in);
818 if (!flist || flist->count == 0) {
819 fprintf(FERROR,"nothing to do\n");
823 local_name = get_local_name(flist,argv[0]);
825 status2 = do_recv(f_in,f_out,flist,local_name);
829 waitpid(pid, &status, 0);
831 return status | status2;