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_perms = 0;
35 int preserve_devices = 0;
38 int preserve_times = 0;
45 int one_file_system=0;
47 extern int csum_length;
50 static int sender = 0;
53 static void usage(FILE *f);
55 static void report(int f)
58 time_t t = time(NULL);
62 if (am_server && sender) {
63 write_int(f,read_total());
64 write_int(f,write_total());
65 write_int(f,total_size);
73 tsize = (int)total_size;
80 printf("wrote %d bytes read %d bytes %g bytes/sec\n",
81 out,in,(in+out)/(0.5 + (t-starttime)));
82 printf("total size is %d speedup is %g\n",
83 tsize,(1.0*tsize)/(in+out));
87 static void server_options(char **args,int *argc)
90 static char argstr[50];
91 static char bsize[30];
92 static char slength[30];
95 args[ac++] = "--server";
98 args[ac++] = "--sender";
102 for (i=0;i<verbose;i++)
116 if (preserve_devices)
134 if (x != 1) args[ac++] = argstr;
136 if (block_size != BLOCK_SIZE) {
137 sprintf(bsize,"-B%d",block_size);
141 if (csum_length != SUM_LENGTH) {
142 sprintf(slength,"--csum-length=%d",csum_length);
143 args[ac++] = slength;
147 args[ac++] = "--delete";
154 int do_cmd(char *cmd,char *machine,char *user,char *path,int *f_in,int *f_out)
162 cmd = getenv(RSYNC_RSH_ENV);
169 for (tok=strtok(cmd," ");tok;tok=strtok(NULL," ")) {
177 args[argc++] = machine;
180 args[argc++] = rsync_path;
182 server_options(args,&argc);
185 char *dir = strdup(path);
186 p = strrchr(dir,'/');
202 fprintf(stderr,"cmd=");
204 fprintf(stderr,"%s ",args[i]);
205 fprintf(stderr,"\n");
208 return piped_child(args,f_in,f_out);
211 out_of_memory("do_cmd");
212 return 0; /* not reached */
218 static char *get_local_name(struct file_list *flist,char *name)
222 if (stat(name,&st) == 0) {
223 if (S_ISDIR(st.st_mode)) {
224 if (chdir(name) != 0) {
225 fprintf(stderr,"chdir %s : %s\n",name,strerror(errno));
230 if (flist->count > 1) {
231 fprintf(stderr,"ERROR: destination must be a directory when copying more than 1 file\n");
237 if (flist->count == 1)
243 if (mkdir(name,0777) != 0) {
244 fprintf(stderr,"mkdir %s : %s\n",name,strerror(errno));
247 fprintf(am_server?stderr:stdout,"created directory %s\n",name);
250 if (chdir(name) != 0) {
251 fprintf(stderr,"chdir %s : %s\n",name,strerror(errno));
261 void do_server_sender(int argc,char *argv[])
265 struct file_list *flist;
268 fprintf(stderr,"server_sender starting pid=%d\n",(int)getpid());
270 if (chdir(dir) != 0) {
271 fprintf(stderr,"chdir %s: %s\n",dir,strerror(errno));
277 if (strcmp(dir,".")) {
283 if (argc == 0 && recurse) {
290 flist = send_file_list(STDOUT_FILENO,recurse,argc,argv);
291 send_files(flist,STDOUT_FILENO,STDIN_FILENO);
292 report(STDOUT_FILENO);
298 void do_server_recv(int argc,char *argv[])
302 struct file_list *flist;
303 char *local_name=NULL;
306 fprintf(stderr,"server_recv(%d) starting pid=%d\n",argc,(int)getpid());
312 if (chdir(dir) != 0) {
313 fprintf(stderr,"chdir %s : %s\n",dir,strerror(errno));
319 recv_exclude_list(STDIN_FILENO);
321 flist = recv_file_list(STDIN_FILENO);
322 if (!flist || flist->count == 0) {
323 fprintf(stderr,"nothing to do\n");
328 if (strcmp(dir,".")) {
329 argv[0] += strlen(dir);
330 if (argv[0][0] == '/') argv[0]++;
332 local_name = get_local_name(flist,argv[0]);
335 if ((pid=fork()) == 0) {
336 recv_files(STDIN_FILENO,flist,local_name);
338 fprintf(stderr,"receiver read %d\n",read_total());
342 generate_files(STDOUT_FILENO,flist,local_name);
344 waitpid(pid, &status, 0);
345 exit_cleanup(status);
349 static void usage(FILE *f)
351 fprintf(f,"rsync version %s Copyright Andrew Tridgell and Paul Mackerras\n\n",
353 fprintf(f,"Usage:\t%s [options] src user@host:dest\nOR",RSYNC_NAME);
354 fprintf(f,"\t%s [options] user@host:src dest\n\n",RSYNC_NAME);
355 fprintf(f,"Options:\n");
356 fprintf(f,"-v, --verbose increase verbosity\n");
357 fprintf(f,"-c, --checksum always checksum\n");
358 fprintf(f,"-a, --archive archive mode (same as -rlptDog)\n");
359 fprintf(f,"-r, --recursive recurse into directories\n");
360 fprintf(f,"-b, --backup make backups (default ~ extension)\n");
361 fprintf(f,"-u, --update update only (don't overwrite newer files)\n");
362 fprintf(f,"-l, --links preserve soft links\n");
363 fprintf(f,"-p, --perms preserve permissions\n");
364 fprintf(f,"-o, --owner preserve owner (root only)\n");
365 fprintf(f,"-g, --group preserve group\n");
366 fprintf(f,"-D, --devices preserve devices (root only)\n");
367 fprintf(f,"-t, --times preserve times\n");
368 fprintf(f,"-n, --dry-run show what would have been transferred\n");
369 fprintf(f,"-x, --one-file-system don't cross filesystem boundaries\n");
370 fprintf(f,"-B, --block-size SIZE checksum blocking size\n");
371 fprintf(f,"-e, --rsh COMMAND specify rsh replacement\n");
372 fprintf(f," --rsync-path PATH specify path to rsync on the remote machine\n");
373 fprintf(f,"-C, --cvs-exclude auto ignore files in the same way CVS does\n");
374 fprintf(f," --delete delete files that don't exist on the sending side\n");
375 fprintf(f,"-I, --ignore-times don't exclude files that match length and time\n");
376 fprintf(f," --exclude FILE exclude file FILE\n");
377 fprintf(f," --exclude-from FILE exclude files listed in FILE\n");
378 fprintf(f," --suffix SUFFIX override backup suffix\n");
379 fprintf(f," --csum-length LENGTH set the checksum length\n");
380 fprintf(f," --version print version number\n");
383 fprintf(f,"the backup suffix defaults to %s\n",BACKUP_SUFFIX);
384 fprintf(f,"the block size defaults to %d\n",BLOCK_SIZE);
387 enum {OPT_VERSION,OPT_SUFFIX,OPT_SENDER,OPT_SERVER,OPT_EXCLUDE,
388 OPT_EXCLUDE_FROM,OPT_DELETE,OPT_RSYNC_PATH,OPT_CSUM_LENGTH};
390 static char *short_options = "oblpguDCtcahvrIxne:B:";
392 static struct option long_options[] = {
393 {"version", 0, 0, OPT_VERSION},
394 {"server", 0, 0, OPT_SERVER},
395 {"sender", 0, 0, OPT_SENDER},
396 {"delete", 0, 0, OPT_DELETE},
397 {"exclude", 1, 0, OPT_EXCLUDE},
398 {"exclude-from",1, 0, OPT_EXCLUDE_FROM},
399 {"rsync-path", 1, 0, OPT_RSYNC_PATH},
400 {"csum-length", 1, 0, OPT_CSUM_LENGTH},
401 {"one-file-system",0, 0, 'x'},
402 {"ignore-times",0, 0, 'I'},
404 {"dry-run", 0, 0, 'n'},
405 {"cvs-exclude", 0, 0, 'C'},
406 {"archive", 0, 0, 'a'},
407 {"checksum", 0, 0, 'c'},
408 {"backup", 0, 0, 'b'},
409 {"update", 0, 0, 'u'},
410 {"verbose", 0, 0, 'v'},
411 {"recursive", 0, 0, 'r'},
412 {"devices", 0, 0, 'D'},
413 {"perms", 0, 0, 'p'},
414 {"links", 0, 0, 'l'},
415 {"owner", 0, 0, 'o'},
416 {"group", 0, 0, 'g'},
417 {"times", 0, 0, 't'},
419 {"suffix", 1, 0, OPT_SUFFIX},
420 {"block-size", 1, 0, 'B'},
423 int main(int argc,char *argv[])
425 int pid, status, pid2, status2;
428 char *shell_cmd = NULL;
429 char *shell_machine = NULL;
430 char *shell_path = NULL;
431 char *shell_user = NULL;
434 struct file_list *flist;
435 char *local_name = NULL;
437 starttime = time(NULL);
441 while ((opt = getopt_long(argc, argv,
442 short_options, long_options, &option_index))
447 printf("rsync version %s protocol version %d\n",
448 VERSION,PROTOCOL_VERSION);
452 backup_suffix = optarg;
459 case OPT_CSUM_LENGTH:
460 csum_length = atoi(optarg);
461 csum_length = MIN(csum_length,SUM_LENGTH);
480 case OPT_EXCLUDE_FROM:
481 add_exclude_file(optarg,1);
518 fprintf(stderr,"-o only allowed for root\n");
531 fprintf(stderr,"-D only allowed for root\n");
583 block_size = atoi(optarg);
587 fprintf(stderr,"bad option -%c\n",opt);
597 signal(SIGCHLD,SIG_IGN);
598 signal(SIGINT,SIGNAL_CAST sig_int);
599 signal(SIGPIPE,SIGNAL_CAST sig_int);
600 signal(SIGHUP,SIGNAL_CAST sig_int);
603 verbose = MAX(verbose,1);
606 remote_version = read_int(STDIN_FILENO);
607 if (remote_version < MIN_PROTOCOL_VERSION ||
608 remote_version > MAX_PROTOCOL_VERSION) {
609 fprintf(stderr,"protocol version mismatch - is your shell clean?\n");
612 write_int(STDOUT_FILENO,PROTOCOL_VERSION);
613 write_flush(STDOUT_FILENO);
618 recv_exclude_list(STDIN_FILENO);
621 do_server_sender(argc,argv);
623 do_server_recv(argc,argv);
633 p = strchr(argv[0],':');
638 shell_machine = argv[0];
645 p = strchr(argv[argc-1],':');
651 shell_machine = NULL;
652 shell_path = argv[argc-1];
655 shell_machine = argv[argc-1];
662 p = strchr(shell_machine,'@');
665 shell_user = shell_machine;
671 fprintf(stderr,"cmd=%s machine=%s user=%s path=%s\n",
672 shell_cmd?shell_cmd:"",
673 shell_machine?shell_machine:"",
674 shell_user?shell_user:"",
675 shell_path?shell_path:"");
678 if (!sender && argc != 1) {
683 pid = do_cmd(shell_cmd,shell_machine,shell_user,shell_path,&f_in,&f_out);
685 write_int(f_out,PROTOCOL_VERSION);
688 remote_version = read_int(f_in);
689 if (remote_version < MIN_PROTOCOL_VERSION ||
690 remote_version > MAX_PROTOCOL_VERSION) {
691 fprintf(stderr,"protocol version mismatch - is your shell clean?\n");
699 fprintf(stderr,"parent=%d child=%d sender=%d recurse=%d\n",
700 (int)getpid(),pid,sender,recurse);
706 send_exclude_list(f_out);
707 flist = send_file_list(f_out,recurse,argc,argv);
709 fprintf(stderr,"file list sent\n");
710 send_files(flist,f_out,f_in);
712 fprintf(stderr,"waiting on %d\n",pid);
713 waitpid(pid, &status, 0);
715 exit_cleanup(status);
718 send_exclude_list(f_out);
720 flist = recv_file_list(f_in);
721 if (!flist || flist->count == 0) {
722 fprintf(stderr,"nothing to do\n");
726 local_name = get_local_name(flist,argv[0]);
728 if ((pid2=fork()) == 0) {
729 recv_files(f_in,flist,local_name);
731 fprintf(stderr,"receiver read %d\n",read_total());
735 generate_files(f_out,flist,local_name);
737 waitpid(pid2, &status2, 0);
741 waitpid(pid, &status, 0);
743 return status | status2;