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;
51 extern int csum_length;
54 static int sender = 0;
57 static void usage(FILE *f);
59 static void report(int f)
62 time_t t = time(NULL);
66 if (am_server && sender) {
67 write_int(f,read_total());
68 write_int(f,write_total());
69 write_int(f,total_size);
77 tsize = (int)total_size;
84 printf("wrote %d bytes read %d bytes %g bytes/sec\n",
85 out,in,(in+out)/(0.5 + (t-starttime)));
86 printf("total size is %d speedup is %g\n",
87 tsize,(1.0*tsize)/(in+out));
91 static void server_options(char **args,int *argc)
94 static char argstr[50];
95 static char bsize[30];
96 static char slength[30];
99 args[ac++] = "--server";
102 args[ac++] = "--sender";
106 for (i=0;i<verbose;i++)
116 if (preserve_hard_links)
122 if (preserve_devices)
142 if (x != 1) args[ac++] = argstr;
144 if (block_size != BLOCK_SIZE) {
145 sprintf(bsize,"-B%d",block_size);
149 if (csum_length != SUM_LENGTH) {
150 sprintf(slength,"--csum-length=%d",csum_length);
151 args[ac++] = slength;
155 args[ac++] = "--delete";
162 int do_cmd(char *cmd,char *machine,char *user,char *path,int *f_in,int *f_out)
170 cmd = getenv(RSYNC_RSH_ENV);
177 for (tok=strtok(cmd," ");tok;tok=strtok(NULL," ")) {
185 args[argc++] = machine;
188 args[argc++] = rsync_path;
190 server_options(args,&argc);
193 char *dir = strdup(path);
194 p = strrchr(dir,'/');
210 fprintf(FERROR,"cmd=");
212 fprintf(FERROR,"%s ",args[i]);
213 fprintf(FERROR,"\n");
216 return piped_child(args,f_in,f_out);
219 out_of_memory("do_cmd");
220 return 0; /* not reached */
226 static char *get_local_name(struct file_list *flist,char *name)
230 if (stat(name,&st) == 0) {
231 if (S_ISDIR(st.st_mode)) {
232 if (chdir(name) != 0) {
233 fprintf(FERROR,"chdir %s : %s\n",name,strerror(errno));
238 if (flist->count > 1) {
239 fprintf(FERROR,"ERROR: destination must be a directory when copying more than 1 file\n");
245 if (flist->count == 1)
251 if (mkdir(name,0777) != 0) {
252 fprintf(FERROR,"mkdir %s : %s\n",name,strerror(errno));
255 fprintf(FINFO,"created directory %s\n",name);
258 if (chdir(name) != 0) {
259 fprintf(FERROR,"chdir %s : %s\n",name,strerror(errno));
269 void do_server_sender(int argc,char *argv[])
273 struct file_list *flist;
276 fprintf(FERROR,"server_sender starting pid=%d\n",(int)getpid());
278 if (chdir(dir) != 0) {
279 fprintf(FERROR,"chdir %s: %s\n",dir,strerror(errno));
285 if (strcmp(dir,".")) {
291 if (argc == 0 && recurse) {
298 flist = send_file_list(STDOUT_FILENO,recurse,argc,argv);
299 send_files(flist,STDOUT_FILENO,STDIN_FILENO);
300 report(STDOUT_FILENO);
305 static int do_recv(int f_in,int f_out,struct file_list *flist,char *local_name)
311 if (preserve_hard_links)
312 init_hard_links(flist);
314 if (pipe(recv_pipe) < 0) {
315 fprintf(FERROR,"pipe failed in do_recv\n");
320 if ((pid=fork()) == 0) {
321 recv_files(f_in,flist,local_name,recv_pipe[1]);
322 if (preserve_hard_links)
323 do_hard_links(flist);
325 fprintf(FERROR,"receiver read %d\n",read_total());
329 generate_files(f_out,flist,local_name,recv_pipe[0]);
331 waitpid(pid, &status, 0);
337 void do_server_recv(int argc,char *argv[])
341 struct file_list *flist;
342 char *local_name=NULL;
345 fprintf(FERROR,"server_recv(%d) starting pid=%d\n",argc,(int)getpid());
351 if (chdir(dir) != 0) {
352 fprintf(FERROR,"chdir %s : %s\n",dir,strerror(errno));
358 recv_exclude_list(STDIN_FILENO);
360 flist = recv_file_list(STDIN_FILENO);
361 if (!flist || flist->count == 0) {
362 fprintf(FERROR,"nothing to do\n");
367 if (strcmp(dir,".")) {
368 argv[0] += strlen(dir);
369 if (argv[0][0] == '/') argv[0]++;
371 local_name = get_local_name(flist,argv[0]);
374 status = do_recv(STDIN_FILENO,STDOUT_FILENO,flist,local_name);
375 exit_cleanup(status);
379 static void usage(FILE *f)
381 fprintf(f,"rsync version %s Copyright Andrew Tridgell and Paul Mackerras\n\n",
383 fprintf(f,"Usage:\t%s [options] src user@host:dest\nOR",RSYNC_NAME);
384 fprintf(f,"\t%s [options] user@host:src dest\n\n",RSYNC_NAME);
385 fprintf(f,"Options:\n");
386 fprintf(f,"-v, --verbose increase verbosity\n");
387 fprintf(f,"-c, --checksum always checksum\n");
388 fprintf(f,"-a, --archive archive mode (same as -rlptDog)\n");
389 fprintf(f,"-r, --recursive recurse into directories\n");
390 fprintf(f,"-b, --backup make backups (default ~ extension)\n");
391 fprintf(f,"-u, --update update only (don't overwrite newer files)\n");
392 fprintf(f,"-l, --links preserve soft links\n");
393 fprintf(f,"-H, --hard-links preserve hard links\n");
394 fprintf(f,"-p, --perms preserve permissions\n");
395 fprintf(f,"-o, --owner preserve owner (root only)\n");
396 fprintf(f,"-g, --group preserve group\n");
397 fprintf(f,"-D, --devices preserve devices (root only)\n");
398 fprintf(f,"-t, --times preserve times\n");
399 fprintf(f,"-S, --sparse handle sparse files efficiently\n");
400 fprintf(f,"-n, --dry-run show what would have been transferred\n");
401 fprintf(f,"-x, --one-file-system don't cross filesystem boundaries\n");
402 fprintf(f,"-B, --block-size SIZE checksum blocking size\n");
403 fprintf(f,"-e, --rsh COMMAND specify rsh replacement\n");
404 fprintf(f," --rsync-path PATH specify path to rsync on the remote machine\n");
405 fprintf(f,"-C, --cvs-exclude auto ignore files in the same way CVS does\n");
406 fprintf(f," --delete delete files that don't exist on the sending side\n");
407 fprintf(f,"-I, --ignore-times don't exclude files that match length and time\n");
408 fprintf(f," --exclude FILE exclude file FILE\n");
409 fprintf(f," --exclude-from FILE exclude files listed in FILE\n");
410 fprintf(f," --suffix SUFFIX override backup suffix\n");
411 fprintf(f," --csum-length LENGTH set the checksum length\n");
412 fprintf(f," --version print version number\n");
415 fprintf(f,"the backup suffix defaults to %s\n",BACKUP_SUFFIX);
416 fprintf(f,"the block size defaults to %d\n",BLOCK_SIZE);
419 enum {OPT_VERSION,OPT_SUFFIX,OPT_SENDER,OPT_SERVER,OPT_EXCLUDE,
420 OPT_EXCLUDE_FROM,OPT_DELETE,OPT_RSYNC_PATH,OPT_CSUM_LENGTH};
422 static char *short_options = "oblHpguDCtcahvrIxnSe:B:";
424 static struct option long_options[] = {
425 {"version", 0, 0, OPT_VERSION},
426 {"server", 0, 0, OPT_SERVER},
427 {"sender", 0, 0, OPT_SENDER},
428 {"delete", 0, 0, OPT_DELETE},
429 {"exclude", 1, 0, OPT_EXCLUDE},
430 {"exclude-from",1, 0, OPT_EXCLUDE_FROM},
431 {"rsync-path", 1, 0, OPT_RSYNC_PATH},
432 {"csum-length", 1, 0, OPT_CSUM_LENGTH},
433 {"one-file-system",0, 0, 'x'},
434 {"ignore-times",0, 0, 'I'},
436 {"dry-run", 0, 0, 'n'},
437 {"sparse", 0, 0, 'S'},
438 {"cvs-exclude", 0, 0, 'C'},
439 {"archive", 0, 0, 'a'},
440 {"checksum", 0, 0, 'c'},
441 {"backup", 0, 0, 'b'},
442 {"update", 0, 0, 'u'},
443 {"verbose", 0, 0, 'v'},
444 {"recursive", 0, 0, 'r'},
445 {"devices", 0, 0, 'D'},
446 {"perms", 0, 0, 'p'},
447 {"links", 0, 0, 'l'},
448 {"hard-links", 0, 0, 'H'},
449 {"owner", 0, 0, 'o'},
450 {"group", 0, 0, 'g'},
451 {"times", 0, 0, 't'},
453 {"suffix", 1, 0, OPT_SUFFIX},
454 {"block-size", 1, 0, 'B'},
457 int main(int argc,char *argv[])
459 int pid, status, status2;
462 char *shell_cmd = NULL;
463 char *shell_machine = NULL;
464 char *shell_path = NULL;
465 char *shell_user = NULL;
468 struct file_list *flist;
469 char *local_name = NULL;
471 starttime = time(NULL);
475 while ((opt = getopt_long(argc, argv,
476 short_options, long_options, &option_index))
481 printf("rsync version %s protocol version %d\n",
482 VERSION,PROTOCOL_VERSION);
486 backup_suffix = optarg;
493 case OPT_CSUM_LENGTH:
494 csum_length = atoi(optarg);
495 csum_length = MIN(csum_length,SUM_LENGTH);
514 case OPT_EXCLUDE_FROM:
515 add_exclude_file(optarg,1);
549 #if SUPPORT_HARD_LINKS
550 preserve_hard_links=1;
562 fprintf(FERROR,"-o only allowed for root\n");
575 fprintf(FERROR,"-D only allowed for root\n");
627 block_size = atoi(optarg);
631 fprintf(FERROR,"bad option -%c\n",opt);
641 signal(SIGCHLD,SIG_IGN);
642 signal(SIGINT,SIGNAL_CAST sig_int);
643 signal(SIGPIPE,SIGNAL_CAST sig_int);
644 signal(SIGHUP,SIGNAL_CAST sig_int);
647 verbose = MAX(verbose,1);
650 setup_protocol(STDOUT_FILENO,STDIN_FILENO);
653 recv_exclude_list(STDIN_FILENO);
656 do_server_sender(argc,argv);
658 do_server_recv(argc,argv);
668 p = strchr(argv[0],':');
673 shell_machine = argv[0];
680 p = strchr(argv[argc-1],':');
686 shell_machine = NULL;
687 shell_path = argv[argc-1];
690 shell_machine = argv[argc-1];
697 p = strchr(shell_machine,'@');
700 shell_user = shell_machine;
706 fprintf(FERROR,"cmd=%s machine=%s user=%s path=%s\n",
707 shell_cmd?shell_cmd:"",
708 shell_machine?shell_machine:"",
709 shell_user?shell_user:"",
710 shell_path?shell_path:"");
713 if (!sender && argc != 1) {
718 pid = do_cmd(shell_cmd,shell_machine,shell_user,shell_path,&f_in,&f_out);
720 setup_protocol(f_out,f_in);
723 fprintf(FERROR,"parent=%d child=%d sender=%d recurse=%d\n",
724 (int)getpid(),pid,sender,recurse);
730 send_exclude_list(f_out);
731 flist = send_file_list(f_out,recurse,argc,argv);
733 fprintf(FERROR,"file list sent\n");
734 send_files(flist,f_out,f_in);
736 fprintf(FERROR,"waiting on %d\n",pid);
737 waitpid(pid, &status, 0);
739 exit_cleanup(status);
742 send_exclude_list(f_out);
744 flist = recv_file_list(f_in);
745 if (!flist || flist->count == 0) {
746 fprintf(FERROR,"nothing to do\n");
750 local_name = get_local_name(flist,argv[0]);
752 status2 = do_recv(f_in,f_out,flist,local_name);
756 waitpid(pid, &status, 0);
758 return status | status2;