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;
30 static char *rsync_path = RSYNC_NAME;
35 int preserve_links = 0;
36 int preserve_hard_links = 0;
37 int preserve_perms = 0;
38 int preserve_devices = 0;
41 int preserve_times = 0;
48 int one_file_system=0;
56 extern int csum_length;
59 static int sender = 0;
62 static void usage(FILE *f);
64 static void report(int f)
67 time_t t = time(NULL);
71 if (am_server && sender) {
72 write_int(f,read_total());
73 write_int(f,write_total());
74 write_int(f,total_size);
82 tsize = (int)total_size;
89 printf("wrote %d bytes read %d bytes %g bytes/sec\n",
90 out,in,(in+out)/(0.5 + (t-starttime)));
91 printf("total size is %d speedup is %g\n",
92 tsize,(1.0*tsize)/(in+out));
96 static void server_options(char **args,int *argc)
99 static char argstr[50];
100 static char bsize[30];
103 args[ac++] = "--server";
106 args[ac++] = "--sender";
110 for (i=0;i<verbose;i++)
124 if (preserve_hard_links)
130 if (preserve_devices)
154 if (x != 1) args[ac++] = argstr;
156 if (block_size != BLOCK_SIZE) {
157 sprintf(bsize,"-B%d",block_size);
161 if (strcmp(backup_suffix, BACKUP_SUFFIX)) {
162 args[ac++] = "--suffix";
163 args[ac++] = backup_suffix;
167 args[ac++] = "--delete";
174 static int do_cmd(char *cmd,char *machine,char *user,char *path,int *f_in,int *f_out)
178 char *tok,*p,*dir=NULL;
182 cmd = getenv(RSYNC_RSH_ENV);
189 for (tok=strtok(cmd," ");tok;tok=strtok(NULL," ")) {
194 /* remsh (on HPUX) takes the arguments the other way around */
195 args[argc++] = machine;
205 args[argc++] = machine;
209 args[argc++] = rsync_path;
211 server_options(args,&argc);
215 p = strrchr(dir,'/');
216 if (p && !relative_paths) {
234 fprintf(FERROR,"cmd=");
236 fprintf(FERROR,"%s ",args[i]);
237 fprintf(FERROR,"\n");
240 ret = piped_child(args,f_in,f_out);
246 out_of_memory("do_cmd");
247 return 0; /* not reached */
253 static char *get_local_name(struct file_list *flist,char *name)
257 if (stat(name,&st) == 0) {
258 if (S_ISDIR(st.st_mode)) {
259 if (chdir(name) != 0) {
260 fprintf(FERROR,"chdir %s : %s (1)\n",name,strerror(errno));
265 if (flist->count > 1) {
266 fprintf(FERROR,"ERROR: destination must be a directory when copying more than 1 file\n");
272 if (flist->count == 1)
278 if (mkdir(name,0777 & ~orig_umask) != 0) {
279 fprintf(FERROR,"mkdir %s : %s (1)\n",name,strerror(errno));
282 fprintf(FINFO,"created directory %s\n",name);
285 if (chdir(name) != 0) {
286 fprintf(FERROR,"chdir %s : %s (2)\n",name,strerror(errno));
296 void do_server_sender(int argc,char *argv[])
300 struct file_list *flist;
303 fprintf(FERROR,"server_sender starting pid=%d\n",(int)getpid());
305 if (!relative_paths && chdir(dir) != 0) {
306 fprintf(FERROR,"chdir %s: %s (3)\n",dir,strerror(errno));
312 if (strcmp(dir,".")) {
314 if (strcmp(dir,"/") == 0)
320 if (argc == 0 && recurse) {
327 flist = send_file_list(STDOUT_FILENO,argc,argv);
328 send_files(flist,STDOUT_FILENO,STDIN_FILENO);
329 report(STDOUT_FILENO);
334 static int do_recv(int f_in,int f_out,struct file_list *flist,char *local_name)
340 if (preserve_hard_links)
341 init_hard_links(flist);
343 if (pipe(recv_pipe) < 0) {
344 fprintf(FERROR,"pipe failed in do_recv\n");
349 if ((pid=fork()) == 0) {
350 recv_files(f_in,flist,local_name,recv_pipe[1]);
352 fprintf(FERROR,"receiver read %d\n",read_total());
356 generate_files(f_out,flist,local_name,recv_pipe[0]);
358 waitpid(pid, &status, 0);
364 void do_server_recv(int argc,char *argv[])
368 struct file_list *flist;
369 char *local_name=NULL;
372 fprintf(FERROR,"server_recv(%d) starting pid=%d\n",argc,(int)getpid());
378 if (chdir(dir) != 0) {
379 fprintf(FERROR,"chdir %s : %s (4)\n",dir,strerror(errno));
385 recv_exclude_list(STDIN_FILENO);
387 flist = recv_file_list(STDIN_FILENO);
388 if (!flist || flist->count == 0) {
389 fprintf(FERROR,"nothing to do\n");
394 if (strcmp(dir,".")) {
395 argv[0] += strlen(dir);
396 if (argv[0][0] == '/') argv[0]++;
398 local_name = get_local_name(flist,argv[0]);
401 status = do_recv(STDIN_FILENO,STDOUT_FILENO,flist,local_name);
402 exit_cleanup(status);
406 static void usage(FILE *f)
408 fprintf(f,"rsync version %s Copyright Andrew Tridgell and Paul Mackerras\n\n",
410 fprintf(f,"Usage:\t%s [options] src user@host:dest\nOR",RSYNC_NAME);
411 fprintf(f,"\t%s [options] user@host:src dest\n\n",RSYNC_NAME);
412 fprintf(f,"Options:\n");
413 fprintf(f,"-v, --verbose increase verbosity\n");
414 fprintf(f,"-c, --checksum always checksum\n");
415 fprintf(f,"-a, --archive archive mode (same as -rlptDog)\n");
416 fprintf(f,"-r, --recursive recurse into directories\n");
417 fprintf(f,"-R, --relative use relative path names\n");
418 fprintf(f,"-b, --backup make backups (default ~ extension)\n");
419 fprintf(f,"-u, --update update only (don't overwrite newer files)\n");
420 fprintf(f,"-l, --links preserve soft links\n");
421 fprintf(f,"-L, --copy-links treat soft links like regular files\n");
422 fprintf(f,"-H, --hard-links preserve hard links\n");
423 fprintf(f,"-p, --perms preserve permissions\n");
424 fprintf(f,"-o, --owner preserve owner (root only)\n");
425 fprintf(f,"-g, --group preserve group\n");
426 fprintf(f,"-D, --devices preserve devices (root only)\n");
427 fprintf(f,"-t, --times preserve times\n");
428 fprintf(f,"-S, --sparse handle sparse files efficiently\n");
429 fprintf(f,"-n, --dry-run show what would have been transferred\n");
430 fprintf(f,"-W, --whole-file copy whole files, no incremental checks\n");
431 fprintf(f,"-x, --one-file-system don't cross filesystem boundaries\n");
432 fprintf(f,"-B, --block-size SIZE checksum blocking size\n");
433 fprintf(f,"-e, --rsh COMMAND specify rsh replacement\n");
434 fprintf(f," --rsync-path PATH specify path to rsync on the remote machine\n");
435 fprintf(f,"-C, --cvs-exclude auto ignore files in the same way CVS does\n");
436 fprintf(f," --delete delete files that don't exist on the sending side\n");
437 fprintf(f,"-I, --ignore-times don't exclude files that match length and time\n");
438 fprintf(f,"-z, --compress compress file data\n");
439 fprintf(f," --exclude FILE exclude file FILE\n");
440 fprintf(f," --exclude-from FILE exclude files listed in FILE\n");
441 fprintf(f," --suffix SUFFIX override backup suffix\n");
442 fprintf(f," --version print version number\n");
445 fprintf(f,"the backup suffix defaults to %s\n",BACKUP_SUFFIX);
446 fprintf(f,"the block size defaults to %d\n",BLOCK_SIZE);
449 enum {OPT_VERSION,OPT_SUFFIX,OPT_SENDER,OPT_SERVER,OPT_EXCLUDE,
450 OPT_EXCLUDE_FROM,OPT_DELETE,OPT_RSYNC_PATH};
452 static char *short_options = "oblLWHpguDCtcahvrRIxnSe:B:z";
454 static struct option long_options[] = {
455 {"version", 0, 0, OPT_VERSION},
456 {"server", 0, 0, OPT_SERVER},
457 {"sender", 0, 0, OPT_SENDER},
458 {"delete", 0, 0, OPT_DELETE},
459 {"exclude", 1, 0, OPT_EXCLUDE},
460 {"exclude-from",1, 0, OPT_EXCLUDE_FROM},
461 {"rsync-path", 1, 0, OPT_RSYNC_PATH},
462 {"one-file-system",0, 0, 'x'},
463 {"ignore-times",0, 0, 'I'},
465 {"dry-run", 0, 0, 'n'},
466 {"sparse", 0, 0, 'S'},
467 {"cvs-exclude", 0, 0, 'C'},
468 {"archive", 0, 0, 'a'},
469 {"checksum", 0, 0, 'c'},
470 {"backup", 0, 0, 'b'},
471 {"update", 0, 0, 'u'},
472 {"verbose", 0, 0, 'v'},
473 {"recursive", 0, 0, 'r'},
474 {"relative", 0, 0, 'R'},
475 {"devices", 0, 0, 'D'},
476 {"perms", 0, 0, 'p'},
477 {"links", 0, 0, 'l'},
478 {"copy-links", 0, 0, 'L'},
479 {"whole-file", 0, 0, 'W'},
480 {"hard-links", 0, 0, 'H'},
481 {"owner", 0, 0, 'o'},
482 {"group", 0, 0, 'g'},
483 {"times", 0, 0, 't'},
485 {"suffix", 1, 0, OPT_SUFFIX},
486 {"block-size", 1, 0, 'B'},
487 {"compress", 0, 0, 'z'},
490 RETSIGTYPE sigusr1_handler(int val) {
494 int main(int argc,char *argv[])
496 int pid, status = 0, status2 = 0;
499 char *shell_cmd = NULL;
500 char *shell_machine = NULL;
501 char *shell_path = NULL;
502 char *shell_user = NULL;
505 struct file_list *flist;
506 char *local_name = NULL;
513 signal(SIGUSR1, sigusr1_handler);
515 starttime = time(NULL);
516 am_root = (getuid() == 0);
518 /* we set a 0 umask so that correct file permissions can be
520 orig_umask = umask(0);
522 while ((opt = getopt_long(argc, argv,
523 short_options, long_options, &option_index))
528 printf("rsync version %s protocol version %d\n",
529 VERSION,PROTOCOL_VERSION);
533 backup_suffix = optarg;
556 case OPT_EXCLUDE_FROM:
557 add_exclude_file(optarg,1);
597 #if SUPPORT_HARD_LINKS
598 preserve_hard_links=1;
600 fprintf(FERROR,"ERROR: hard links not supported on this platform\n");
672 block_size = atoi(optarg);
680 /* fprintf(FERROR,"bad option -%c\n",opt); */
690 signal(SIGCHLD,SIG_IGN);
691 signal(SIGINT,SIGNAL_CAST sig_int);
692 signal(SIGPIPE,SIGNAL_CAST sig_int);
693 signal(SIGHUP,SIGNAL_CAST sig_int);
696 verbose = MAX(verbose,1);
698 #ifndef SUPPORT_LINKS
699 if (!am_server && preserve_links) {
700 fprintf(FERROR,"ERROR: symbolic links not supported\n");
706 setup_protocol(STDOUT_FILENO,STDIN_FILENO);
709 recv_exclude_list(STDIN_FILENO);
712 do_server_sender(argc,argv);
714 do_server_recv(argc,argv);
724 p = strchr(argv[0],':');
729 shell_machine = argv[0];
736 p = strchr(argv[argc-1],':');
742 shell_machine = NULL;
743 shell_path = argv[argc-1];
746 shell_machine = argv[argc-1];
753 p = strchr(shell_machine,'@');
756 shell_user = shell_machine;
762 fprintf(FERROR,"cmd=%s machine=%s user=%s path=%s\n",
763 shell_cmd?shell_cmd:"",
764 shell_machine?shell_machine:"",
765 shell_user?shell_user:"",
766 shell_path?shell_path:"");
769 if (!sender && argc != 1) {
774 pid = do_cmd(shell_cmd,shell_machine,shell_user,shell_path,&f_in,&f_out);
776 setup_protocol(f_out,f_in);
784 fprintf(FERROR,"parent=%d child=%d sender=%d recurse=%d\n",
785 (int)getpid(),pid,sender,recurse);
791 send_exclude_list(f_out);
792 flist = send_file_list(f_out,argc,argv);
794 fprintf(FERROR,"file list sent\n");
795 send_files(flist,f_out,f_in);
797 fprintf(FERROR,"waiting on %d\n",pid);
798 waitpid(pid, &status, 0);
800 exit_cleanup(status);
803 send_exclude_list(f_out);
805 flist = recv_file_list(f_in);
806 if (!flist || flist->count == 0) {
807 fprintf(FERROR,"nothing to do\n");
811 local_name = get_local_name(flist,argv[0]);
813 status2 = do_recv(f_in,f_out,flist,local_name);
817 waitpid(pid, &status, 0);
819 return status | status2;