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;
49 static int sender = 0;
52 static void usage(FILE *f);
54 static void report(int f)
57 time_t t = time(NULL);
61 if (am_server && sender) {
62 write_int(f,read_total());
63 write_int(f,write_total());
64 write_int(f,total_size);
72 tsize = (int)total_size;
79 printf("wrote %d bytes read %d bytes %g bytes/sec\n",
80 out,in,(in+out)/(0.5 + (t-starttime)));
81 printf("total size is %d speedup is %g\n",
82 tsize,(1.0*tsize)/(in+out));
86 static void server_options(char **args,int *argc)
89 static char argstr[50];
90 static char bsize[30];
93 args[ac++] = "--server";
96 args[ac++] = "--sender";
100 for (i=0;i<verbose;i++)
114 if (preserve_devices)
132 if (x != 1) args[ac++] = argstr;
134 if (block_size != BLOCK_SIZE) {
135 sprintf(bsize,"-B%d",block_size);
140 args[ac++] = "--delete";
147 int do_cmd(char *cmd,char *machine,char *user,char *path,int *f_in,int *f_out)
155 cmd = getenv(RSYNC_RSH_ENV);
162 for (tok=strtok(cmd," ");tok;tok=strtok(NULL," ")) {
170 args[argc++] = machine;
173 args[argc++] = rsync_path;
175 server_options(args,&argc);
178 char *dir = strdup(path);
179 p = strrchr(dir,'/');
195 fprintf(stderr,"cmd=");
197 fprintf(stderr,"%s ",args[i]);
198 fprintf(stderr,"\n");
201 return piped_child(args,f_in,f_out);
204 out_of_memory("do_cmd");
205 return 0; /* not reached */
211 static char *get_local_name(struct file_list *flist,char *name)
215 if (stat(name,&st) == 0) {
216 if (S_ISDIR(st.st_mode)) {
217 if (chdir(name) != 0) {
218 fprintf(stderr,"chdir %s : %s\n",name,strerror(errno));
223 if (flist->count > 1) {
224 fprintf(stderr,"ERROR: destination must be a directory when copying more than 1 file\n");
230 if (flist->count == 1)
236 if (mkdir(name,0777) != 0) {
237 fprintf(stderr,"mkdir %s : %s\n",name,strerror(errno));
240 fprintf(am_server?stderr:stdout,"created directory %s\n",name);
243 if (chdir(name) != 0) {
244 fprintf(stderr,"chdir %s : %s\n",name,strerror(errno));
254 void do_server_sender(int argc,char *argv[])
258 struct file_list *flist;
261 fprintf(stderr,"server_sender starting pid=%d\n",(int)getpid());
263 if (chdir(dir) != 0) {
264 fprintf(stderr,"chdir %s: %s\n",dir,strerror(errno));
270 if (strcmp(dir,".")) {
276 if (argc == 0 && recurse) {
283 flist = send_file_list(STDOUT_FILENO,recurse,argc,argv);
284 send_files(flist,STDOUT_FILENO,STDIN_FILENO);
285 report(STDOUT_FILENO);
291 void do_server_recv(int argc,char *argv[])
295 struct file_list *flist;
296 char *local_name=NULL;
299 fprintf(stderr,"server_recv(%d) starting pid=%d\n",argc,(int)getpid());
305 if (chdir(dir) != 0) {
306 fprintf(stderr,"chdir %s : %s\n",dir,strerror(errno));
312 recv_exclude_list(STDIN_FILENO);
314 flist = recv_file_list(STDIN_FILENO);
315 if (!flist || flist->count == 0) {
316 fprintf(stderr,"nothing to do\n");
321 if (strcmp(dir,".")) {
322 argv[0] += strlen(dir);
323 if (argv[0][0] == '/') argv[0]++;
325 local_name = get_local_name(flist,argv[0]);
328 if ((pid=fork()) == 0) {
329 recv_files(STDIN_FILENO,flist,local_name);
331 fprintf(stderr,"receiver read %d\n",read_total());
335 generate_files(STDOUT_FILENO,flist,local_name);
337 waitpid(pid, &status, 0);
342 static void usage(FILE *f)
344 fprintf(f,"rsync version %s Copyright Andrew Tridgell and Paul Mackerras\n\n",
346 fprintf(f,"Usage:\t%s [options] src user@host:dest\nOR",RSYNC_NAME);
347 fprintf(f,"\t%s [options] user@host:src dest\n\n",RSYNC_NAME);
348 fprintf(f,"Options:\n");
349 fprintf(f,"-v, --verbose increase verbosity\n");
350 fprintf(f,"-c, --checksum always checksum\n");
351 fprintf(f,"-a, --archive archive mode (same as -rlptDog)\n");
352 fprintf(f,"-r, --recursive recurse into directories\n");
353 fprintf(f,"-b, --backup make backups (default ~ extension)\n");
354 fprintf(f,"-u, --update update only (don't overwrite newer files)\n");
355 fprintf(f,"-l, --links preserve soft links\n");
356 fprintf(f,"-p, --perms preserve permissions\n");
357 fprintf(f,"-o, --owner preserve owner (root only)\n");
358 fprintf(f,"-g, --group preserve group\n");
359 fprintf(f,"-D, --devices preserve devices (root only)\n");
360 fprintf(f,"-t, --times preserve times\n");
361 fprintf(f,"-n, --dry-run show what would have been transferred\n");
362 fprintf(f,"-x, --one-file-system don't cross filesystem boundaries\n");
363 fprintf(f,"-B, --block-size SIZE checksum blocking size\n");
364 fprintf(f,"-e, --rsh COMMAND specify rsh replacement\n");
365 fprintf(f," --rsync-path PATH specify path to rsync on the remote machine\n");
366 fprintf(f,"-C, --cvs-exclude auto ignore files in the same way CVS does\n");
367 fprintf(f," --delete delete files that don't exist on the sending side\n");
368 fprintf(f,"-I, --ignore-times don't exclude files that match length and time\n");
369 fprintf(f," --exclude FILE exclude file FILE\n");
370 fprintf(f," --exclude-from FILE exclude files listed in FILE\n");
371 fprintf(f," --suffix SUFFIX override backup suffix\n");
372 fprintf(f," --version print version number\n");
375 fprintf(f,"the backup suffix defaults to %s\n",BACKUP_SUFFIX);
376 fprintf(f,"the block size defaults to %d\n",BLOCK_SIZE);
379 enum {OPT_VERSION,OPT_SUFFIX,OPT_SENDER,OPT_SERVER,OPT_EXCLUDE,
380 OPT_EXCLUDE_FROM,OPT_DELETE,OPT_RSYNC_PATH};
382 static char *short_options = "oblpguDCtcahvrIxne:B:";
384 static struct option long_options[] = {
385 {"version", 0, 0, OPT_VERSION},
386 {"server", 0, 0, OPT_SERVER},
387 {"sender", 0, 0, OPT_SENDER},
388 {"delete", 0, 0, OPT_DELETE},
389 {"exclude", 1, 0, OPT_EXCLUDE},
390 {"exclude-from",1, 0, OPT_EXCLUDE_FROM},
391 {"rsync-path", 1, 0, OPT_RSYNC_PATH},
392 {"one-file-system",0, 0, 'x'},
393 {"ignore-times",0, 0, 'I'},
395 {"dry-run", 0, 0, 'n'},
396 {"cvs-exclude", 0, 0, 'C'},
397 {"archive", 0, 0, 'a'},
398 {"checksum", 0, 0, 'c'},
399 {"backup", 0, 0, 'b'},
400 {"update", 0, 0, 'u'},
401 {"verbose", 0, 0, 'v'},
402 {"recursive", 0, 0, 'r'},
403 {"devices", 0, 0, 'D'},
404 {"perms", 0, 0, 'p'},
405 {"links", 0, 0, 'l'},
406 {"owner", 0, 0, 'o'},
407 {"group", 0, 0, 'g'},
408 {"times", 0, 0, 't'},
410 {"suffix", 1, 0, OPT_SUFFIX},
411 {"block-size", 1, 0, 'B'},
414 int main(int argc,char *argv[])
416 int pid, status, pid2, status2;
419 char *shell_cmd = NULL;
420 char *shell_machine = NULL;
421 char *shell_path = NULL;
422 char *shell_user = NULL;
425 struct file_list *flist;
426 char *local_name = NULL;
428 starttime = time(NULL);
430 while ((opt = getopt_long(argc, argv,
431 short_options, long_options, &option_index))
436 printf("rsync version %s protocol version %d\n",
437 VERSION,PROTOCOL_VERSION);
441 backup_suffix = optarg;
464 case OPT_EXCLUDE_FROM:
465 add_exclude_file(optarg,1);
502 fprintf(stderr,"-o only allowed for root\n");
515 fprintf(stderr,"-D only allowed for root\n");
567 block_size = atoi(optarg);
571 fprintf(stderr,"bad option -%c\n",opt);
582 verbose = MAX(verbose,1);
585 remote_version = read_int(STDIN_FILENO);
586 if (remote_version < MIN_PROTOCOL_VERSION) {
587 fprintf(stderr,"protocol version mismatch %d %d\n",
588 remote_version,PROTOCOL_VERSION);
591 write_int(STDOUT_FILENO,PROTOCOL_VERSION);
592 write_flush(STDOUT_FILENO);
597 recv_exclude_list(STDIN_FILENO);
600 do_server_sender(argc,argv);
602 do_server_recv(argc,argv);
612 p = strchr(argv[0],':');
617 shell_machine = argv[0];
624 p = strchr(argv[argc-1],':');
630 shell_machine = NULL;
631 shell_path = argv[argc-1];
634 shell_machine = argv[argc-1];
641 p = strchr(shell_machine,'@');
644 shell_user = shell_machine;
650 fprintf(stderr,"cmd=%s machine=%s user=%s path=%s\n",
651 shell_cmd?shell_cmd:"",
652 shell_machine?shell_machine:"",
653 shell_user?shell_user:"",
654 shell_path?shell_path:"");
657 signal(SIGCHLD,SIG_IGN);
658 signal(SIGINT,SIGNAL_CAST sig_int);
659 signal(SIGPIPE,SIGNAL_CAST sig_int);
661 if (!sender && argc != 1) {
666 pid = do_cmd(shell_cmd,shell_machine,shell_user,shell_path,&f_in,&f_out);
668 write_int(f_out,PROTOCOL_VERSION);
671 remote_version = read_int(f_in);
672 if (remote_version < MIN_PROTOCOL_VERSION) {
673 fprintf(stderr,"protocol version mismatch\n");
681 fprintf(stderr,"parent=%d child=%d sender=%d recurse=%d\n",
682 (int)getpid(),pid,sender,recurse);
688 send_exclude_list(f_out);
689 flist = send_file_list(f_out,recurse,argc,argv);
691 fprintf(stderr,"file list sent\n");
692 send_files(flist,f_out,f_in);
694 fprintf(stderr,"waiting on %d\n",pid);
695 waitpid(pid, &status, 0);
700 send_exclude_list(f_out);
702 flist = recv_file_list(f_in);
703 if (!flist || flist->count == 0) {
704 fprintf(stderr,"nothing to do\n");
708 local_name = get_local_name(flist,argv[0]);
710 if ((pid2=fork()) == 0) {
711 recv_files(f_in,flist,local_name);
713 fprintf(stderr,"receiver read %d\n",read_total());
717 generate_files(f_out,flist,local_name);
719 waitpid(pid2, &status2, 0);
723 waitpid(pid, &status, 0);
725 return status | status2;