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;
33 int preserve_links = 0;
34 int preserve_hard_links = 0;
35 int preserve_perms = 0;
36 int preserve_devices = 0;
39 int preserve_times = 0;
46 int one_file_system=0;
54 extern int csum_length;
57 static int sender = 0;
60 static void usage(FILE *f);
62 static void report(int f)
65 time_t t = time(NULL);
69 if (am_server && sender) {
70 write_int(f,read_total());
71 write_int(f,write_total());
72 write_int(f,total_size);
80 tsize = (int)total_size;
87 printf("wrote %d bytes read %d bytes %g bytes/sec\n",
88 out,in,(in+out)/(0.5 + (t-starttime)));
89 printf("total size is %d speedup is %g\n",
90 tsize,(1.0*tsize)/(in+out));
94 static void server_options(char **args,int *argc)
97 static char argstr[50];
98 static char bsize[30];
101 args[ac++] = "--server";
104 args[ac++] = "--sender";
108 for (i=0;i<verbose;i++)
118 if (preserve_hard_links)
124 if (preserve_devices)
148 if (x != 1) args[ac++] = argstr;
150 if (block_size != BLOCK_SIZE) {
151 sprintf(bsize,"-B%d",block_size);
156 args[ac++] = "--delete";
163 int do_cmd(char *cmd,char *machine,char *user,char *path,int *f_in,int *f_out)
171 cmd = getenv(RSYNC_RSH_ENV);
178 for (tok=strtok(cmd," ");tok;tok=strtok(NULL," ")) {
183 /* remsh (on HPUX) takes the arguments the other way around */
184 args[argc++] = machine;
194 args[argc++] = machine;
198 args[argc++] = rsync_path;
200 server_options(args,&argc);
203 char *dir = strdup(path);
204 p = strrchr(dir,'/');
205 if (p && !relative_paths) {
223 fprintf(FERROR,"cmd=");
225 fprintf(FERROR,"%s ",args[i]);
226 fprintf(FERROR,"\n");
229 return piped_child(args,f_in,f_out);
232 out_of_memory("do_cmd");
233 return 0; /* not reached */
239 static char *get_local_name(struct file_list *flist,char *name)
243 if (stat(name,&st) == 0) {
244 if (S_ISDIR(st.st_mode)) {
245 if (chdir(name) != 0) {
246 fprintf(FERROR,"chdir %s : %s (1)\n",name,strerror(errno));
251 if (flist->count > 1) {
252 fprintf(FERROR,"ERROR: destination must be a directory when copying more than 1 file\n");
258 if (flist->count == 1)
264 if (mkdir(name,0777 & ~orig_umask) != 0) {
265 fprintf(FERROR,"mkdir %s : %s (1)\n",name,strerror(errno));
268 fprintf(FINFO,"created directory %s\n",name);
271 if (chdir(name) != 0) {
272 fprintf(FERROR,"chdir %s : %s (2)\n",name,strerror(errno));
282 void do_server_sender(int argc,char *argv[])
286 struct file_list *flist;
289 fprintf(FERROR,"server_sender starting pid=%d\n",(int)getpid());
291 if (!relative_paths && chdir(dir) != 0) {
292 fprintf(FERROR,"chdir %s: %s (3)\n",dir,strerror(errno));
298 if (strcmp(dir,".")) {
300 if (strcmp(dir,"/") == 0)
306 if (argc == 0 && recurse) {
313 flist = send_file_list(STDOUT_FILENO,argc,argv);
314 send_files(flist,STDOUT_FILENO,STDIN_FILENO);
315 report(STDOUT_FILENO);
320 static int do_recv(int f_in,int f_out,struct file_list *flist,char *local_name)
326 if (preserve_hard_links)
327 init_hard_links(flist);
329 if (pipe(recv_pipe) < 0) {
330 fprintf(FERROR,"pipe failed in do_recv\n");
335 if ((pid=fork()) == 0) {
336 recv_files(f_in,flist,local_name,recv_pipe[1]);
337 if (preserve_hard_links)
338 do_hard_links(flist);
340 fprintf(FERROR,"receiver read %d\n",read_total());
344 generate_files(f_out,flist,local_name,recv_pipe[0]);
346 waitpid(pid, &status, 0);
352 void do_server_recv(int argc,char *argv[])
356 struct file_list *flist;
357 char *local_name=NULL;
360 fprintf(FERROR,"server_recv(%d) starting pid=%d\n",argc,(int)getpid());
366 if (chdir(dir) != 0) {
367 fprintf(FERROR,"chdir %s : %s (4)\n",dir,strerror(errno));
373 recv_exclude_list(STDIN_FILENO);
375 flist = recv_file_list(STDIN_FILENO);
376 if (!flist || flist->count == 0) {
377 fprintf(FERROR,"nothing to do\n");
382 if (strcmp(dir,".")) {
383 argv[0] += strlen(dir);
384 if (argv[0][0] == '/') argv[0]++;
386 local_name = get_local_name(flist,argv[0]);
389 status = do_recv(STDIN_FILENO,STDOUT_FILENO,flist,local_name);
390 exit_cleanup(status);
394 static void usage(FILE *f)
396 fprintf(f,"rsync version %s Copyright Andrew Tridgell and Paul Mackerras\n\n",
398 fprintf(f,"Usage:\t%s [options] src user@host:dest\nOR",RSYNC_NAME);
399 fprintf(f,"\t%s [options] user@host:src dest\n\n",RSYNC_NAME);
400 fprintf(f,"Options:\n");
401 fprintf(f,"-v, --verbose increase verbosity\n");
402 fprintf(f,"-c, --checksum always checksum\n");
403 fprintf(f,"-a, --archive archive mode (same as -rlptDog)\n");
404 fprintf(f,"-r, --recursive recurse into directories\n");
405 fprintf(f,"-R, --relative use relative path names\n");
406 fprintf(f,"-b, --backup make backups (default ~ extension)\n");
407 fprintf(f,"-u, --update update only (don't overwrite newer files)\n");
408 fprintf(f,"-l, --links preserve soft links\n");
409 fprintf(f,"-H, --hard-links preserve hard links\n");
410 fprintf(f,"-p, --perms preserve permissions\n");
411 fprintf(f,"-o, --owner preserve owner (root only)\n");
412 fprintf(f,"-g, --group preserve group\n");
413 fprintf(f,"-D, --devices preserve devices (root only)\n");
414 fprintf(f,"-t, --times preserve times\n");
415 fprintf(f,"-S, --sparse handle sparse files efficiently\n");
416 fprintf(f,"-n, --dry-run show what would have been transferred\n");
417 fprintf(f,"-x, --one-file-system don't cross filesystem boundaries\n");
418 fprintf(f,"-B, --block-size SIZE checksum blocking size\n");
419 fprintf(f,"-e, --rsh COMMAND specify rsh replacement\n");
420 fprintf(f," --rsync-path PATH specify path to rsync on the remote machine\n");
421 fprintf(f,"-C, --cvs-exclude auto ignore files in the same way CVS does\n");
422 fprintf(f," --delete delete files that don't exist on the sending side\n");
423 fprintf(f,"-I, --ignore-times don't exclude files that match length and time\n");
424 fprintf(f,"-z, --compress compress file data\n");
425 fprintf(f," --exclude FILE exclude file FILE\n");
426 fprintf(f," --exclude-from FILE exclude files listed in FILE\n");
427 fprintf(f," --suffix SUFFIX override backup suffix\n");
428 fprintf(f," --version print version number\n");
431 fprintf(f,"the backup suffix defaults to %s\n",BACKUP_SUFFIX);
432 fprintf(f,"the block size defaults to %d\n",BLOCK_SIZE);
435 enum {OPT_VERSION,OPT_SUFFIX,OPT_SENDER,OPT_SERVER,OPT_EXCLUDE,
436 OPT_EXCLUDE_FROM,OPT_DELETE,OPT_RSYNC_PATH};
438 static char *short_options = "oblHpguDCtcahvrRIxnSe:B:z";
440 static struct option long_options[] = {
441 {"version", 0, 0, OPT_VERSION},
442 {"server", 0, 0, OPT_SERVER},
443 {"sender", 0, 0, OPT_SENDER},
444 {"delete", 0, 0, OPT_DELETE},
445 {"exclude", 1, 0, OPT_EXCLUDE},
446 {"exclude-from",1, 0, OPT_EXCLUDE_FROM},
447 {"rsync-path", 1, 0, OPT_RSYNC_PATH},
448 {"one-file-system",0, 0, 'x'},
449 {"ignore-times",0, 0, 'I'},
451 {"dry-run", 0, 0, 'n'},
452 {"sparse", 0, 0, 'S'},
453 {"cvs-exclude", 0, 0, 'C'},
454 {"archive", 0, 0, 'a'},
455 {"checksum", 0, 0, 'c'},
456 {"backup", 0, 0, 'b'},
457 {"update", 0, 0, 'u'},
458 {"verbose", 0, 0, 'v'},
459 {"recursive", 0, 0, 'r'},
460 {"relative", 0, 0, 'R'},
461 {"devices", 0, 0, 'D'},
462 {"perms", 0, 0, 'p'},
463 {"links", 0, 0, 'l'},
464 {"hard-links", 0, 0, 'H'},
465 {"owner", 0, 0, 'o'},
466 {"group", 0, 0, 'g'},
467 {"times", 0, 0, 't'},
469 {"suffix", 1, 0, OPT_SUFFIX},
470 {"block-size", 1, 0, 'B'},
471 {"compress", 0, 0, 'z'},
474 int main(int argc,char *argv[])
476 int pid, status = 0, status2 = 0;
479 char *shell_cmd = NULL;
480 char *shell_machine = NULL;
481 char *shell_path = NULL;
482 char *shell_user = NULL;
485 struct file_list *flist;
486 char *local_name = NULL;
488 starttime = time(NULL);
489 am_root = (getuid() == 0);
491 /* we set a 0 umask so that correct file permissions can be
493 orig_umask = umask(0);
495 while ((opt = getopt_long(argc, argv,
496 short_options, long_options, &option_index))
501 printf("rsync version %s protocol version %d\n",
502 VERSION,PROTOCOL_VERSION);
506 backup_suffix = optarg;
529 case OPT_EXCLUDE_FROM:
530 add_exclude_file(optarg,1);
564 #if SUPPORT_HARD_LINKS
565 preserve_hard_links=1;
636 block_size = atoi(optarg);
644 /* fprintf(FERROR,"bad option -%c\n",opt); */
654 signal(SIGCHLD,SIG_IGN);
655 signal(SIGINT,SIGNAL_CAST sig_int);
656 signal(SIGPIPE,SIGNAL_CAST sig_int);
657 signal(SIGHUP,SIGNAL_CAST sig_int);
660 verbose = MAX(verbose,1);
663 setup_protocol(STDOUT_FILENO,STDIN_FILENO);
666 recv_exclude_list(STDIN_FILENO);
669 do_server_sender(argc,argv);
671 do_server_recv(argc,argv);
681 p = strchr(argv[0],':');
686 shell_machine = argv[0];
693 p = strchr(argv[argc-1],':');
699 shell_machine = NULL;
700 shell_path = argv[argc-1];
703 shell_machine = argv[argc-1];
710 p = strchr(shell_machine,'@');
713 shell_user = shell_machine;
719 fprintf(FERROR,"cmd=%s machine=%s user=%s path=%s\n",
720 shell_cmd?shell_cmd:"",
721 shell_machine?shell_machine:"",
722 shell_user?shell_user:"",
723 shell_path?shell_path:"");
726 if (!sender && argc != 1) {
731 pid = do_cmd(shell_cmd,shell_machine,shell_user,shell_path,&f_in,&f_out);
733 setup_protocol(f_out,f_in);
739 fprintf(FERROR,"parent=%d child=%d sender=%d recurse=%d\n",
740 (int)getpid(),pid,sender,recurse);
746 send_exclude_list(f_out);
747 flist = send_file_list(f_out,argc,argv);
749 fprintf(FERROR,"file list sent\n");
750 send_files(flist,f_out,f_in);
752 fprintf(FERROR,"waiting on %d\n",pid);
753 waitpid(pid, &status, 0);
755 exit_cleanup(status);
758 send_exclude_list(f_out);
760 flist = recv_file_list(f_in);
761 if (!flist || flist->count == 0) {
762 fprintf(FERROR,"nothing to do\n");
766 local_name = get_local_name(flist,argv[0]);
768 status2 = do_recv(f_in,f_out,flist,local_name);
772 waitpid(pid, &status, 0);
774 return status | status2;