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;
48 static int sender = 0;
51 static void usage(FILE *f);
53 static void report(int f)
56 time_t t = time(NULL);
60 if (am_server && sender) {
61 write_int(f,read_total());
62 write_int(f,write_total());
63 write_int(f,total_size);
71 tsize = (int)total_size;
78 printf("wrote %d bytes read %d bytes %g bytes/sec\n",
79 out,in,(in+out)/(0.5 + (t-starttime)));
80 printf("total size is %d speedup is %g\n",
81 tsize,(1.0*tsize)/(in+out));
85 static void server_options(char **args,int *argc)
88 static char argstr[50];
89 static char bsize[30];
92 args[ac++] = "--server";
95 args[ac++] = "--sender";
99 for (i=0;i<verbose;i++)
113 if (preserve_devices)
131 if (x != 1) args[ac++] = argstr;
133 if (block_size != BLOCK_SIZE) {
134 sprintf(bsize,"-B%d",block_size);
139 args[ac++] = "--delete";
146 int do_cmd(char *cmd,char *machine,char *user,char *path,int *f_in,int *f_out)
154 cmd = getenv(RSYNC_RSH_ENV);
161 for (tok=strtok(cmd," ");tok;tok=strtok(NULL," ")) {
169 args[argc++] = machine;
172 args[argc++] = rsync_path;
174 server_options(args,&argc);
177 char *dir = strdup(path);
178 p = strrchr(dir,'/');
194 fprintf(stderr,"cmd=");
196 fprintf(stderr,"%s ",args[i]);
197 fprintf(stderr,"\n");
200 return piped_child(args,f_in,f_out);
203 out_of_memory("do_cmd");
204 return 0; /* not reached */
210 static char *get_local_name(struct file_list *flist,char *name)
214 if (stat(name,&st) == 0) {
215 if (S_ISDIR(st.st_mode)) {
216 if (chdir(name) != 0) {
217 fprintf(stderr,"chdir %s : %s\n",name,strerror(errno));
222 if (flist->count > 1) {
223 fprintf(stderr,"ERROR: destination must be a directory when copying more than 1 file\n");
229 if (flist->count == 1)
235 if (mkdir(name,0777) != 0) {
236 fprintf(stderr,"mkdir %s : %s\n",name,strerror(errno));
239 fprintf(am_server?stderr:stdout,"created directory %s\n",name);
242 if (chdir(name) != 0) {
243 fprintf(stderr,"chdir %s : %s\n",name,strerror(errno));
253 void do_server_sender(int argc,char *argv[])
257 struct file_list *flist;
260 fprintf(stderr,"server_sender starting pid=%d\n",(int)getpid());
262 if (chdir(dir) != 0) {
263 fprintf(stderr,"chdir %s: %s\n",dir,strerror(errno));
269 if (strcmp(dir,".")) {
275 if (argc == 0 && recurse) {
282 flist = send_file_list(STDOUT_FILENO,recurse,argc,argv);
283 send_files(flist,STDOUT_FILENO,STDIN_FILENO);
284 report(STDOUT_FILENO);
290 void do_server_recv(int argc,char *argv[])
294 struct file_list *flist;
295 char *local_name=NULL;
298 fprintf(stderr,"server_recv(%d) starting pid=%d\n",argc,(int)getpid());
304 if (chdir(dir) != 0) {
305 fprintf(stderr,"chdir %s : %s\n",dir,strerror(errno));
311 recv_exclude_list(STDIN_FILENO);
313 flist = recv_file_list(STDIN_FILENO);
314 if (!flist || flist->count == 0) {
315 fprintf(stderr,"nothing to do\n");
320 if (strcmp(dir,".")) {
321 argv[0] += strlen(dir);
322 if (argv[0][0] == '/') argv[0]++;
324 local_name = get_local_name(flist,argv[0]);
327 if ((pid=fork()) == 0) {
328 recv_files(STDIN_FILENO,flist,local_name);
330 fprintf(stderr,"receiver read %d\n",read_total());
334 generate_files(STDOUT_FILENO,flist,local_name);
336 waitpid(pid, &status, 0);
341 static void usage(FILE *f)
343 fprintf(f,"rsync version %s Copyright Andrew Tridgell and Paul Mackerras\n\n",
345 fprintf(f,"Usage:\t%s [options] src user@host:dest\nOR",RSYNC_NAME);
346 fprintf(f,"\t%s [options] user@host:src dest\n\n",RSYNC_NAME);
347 fprintf(f,"Options:\n");
348 fprintf(f,"-v, --verbose increase verbosity\n");
349 fprintf(f,"-c, --checksum always checksum\n");
350 fprintf(f,"-a, --archive archive mode (same as -rlptDog)\n");
351 fprintf(f,"-r, --recursive recurse into directories\n");
352 fprintf(f,"-b, --backup make backups (default ~ extension)\n");
353 fprintf(f,"-u, --update update only (don't overwrite newer files)\n");
354 fprintf(f,"-l, --links preserve soft links\n");
355 fprintf(f,"-p, --perms preserve permissions\n");
356 fprintf(f,"-o, --owner preserve owner (root only)\n");
357 fprintf(f,"-g, --group preserve group\n");
358 fprintf(f,"-D, --devices preserve devices (root only)\n");
359 fprintf(f,"-t, --times preserve times\n");
360 fprintf(f,"-n, --dry-run show what would have been transferred\n");
361 fprintf(f,"-x, --one-file-system don't cross filesystem boundaries\n");
362 fprintf(f,"-B, --block-size SIZE checksum blocking size\n");
363 fprintf(f,"-e, --rsh COMMAND specify rsh replacement\n");
364 fprintf(f," --rsync-path PATH specify path to rsync on the remote machine\n");
365 fprintf(f,"-C, --cvs-exclude auto ignore files in the same way CVS does\n");
366 fprintf(f," --delete delete files that don't exist on the sending side\n");
367 fprintf(f,"-I, --ignore-times don't exclude files that match length and time\n");
368 fprintf(f," --exclude FILE exclude file FILE\n");
369 fprintf(f," --exclude-from FILE exclude files listed in FILE\n");
370 fprintf(f," --suffix SUFFIX override backup suffix\n");
371 fprintf(f," --version print version number\n");
374 fprintf(f,"the backup suffix defaults to %s\n",BACKUP_SUFFIX);
375 fprintf(f,"the block size defaults to %d\n",BLOCK_SIZE);
378 enum {OPT_VERSION,OPT_SUFFIX,OPT_SENDER,OPT_SERVER,OPT_EXCLUDE,
379 OPT_EXCLUDE_FROM,OPT_DELETE,OPT_RSYNC_PATH};
381 static char *short_options = "oblpguDCtcahvrIxne:B:";
383 static struct option long_options[] = {
384 {"version", 0, 0, OPT_VERSION},
385 {"server", 0, 0, OPT_SERVER},
386 {"sender", 0, 0, OPT_SENDER},
387 {"delete", 0, 0, OPT_DELETE},
388 {"exclude", 1, 0, OPT_EXCLUDE},
389 {"exclude-from",1, 0, OPT_EXCLUDE_FROM},
390 {"rsync-path", 1, 0, OPT_RSYNC_PATH},
391 {"one-file-system",0, 0, 'x'},
392 {"ignore-times",0, 0, 'I'},
394 {"dry-run", 0, 0, 'n'},
395 {"cvs-exclude", 0, 0, 'C'},
396 {"archive", 0, 0, 'a'},
397 {"checksum", 0, 0, 'c'},
398 {"backup", 0, 0, 'b'},
399 {"update", 0, 0, 'u'},
400 {"verbose", 0, 0, 'v'},
401 {"recursive", 0, 0, 'r'},
402 {"devices", 0, 0, 'D'},
403 {"perms", 0, 0, 'p'},
404 {"links", 0, 0, 'l'},
405 {"owner", 0, 0, 'o'},
406 {"group", 0, 0, 'g'},
407 {"times", 0, 0, 't'},
409 {"suffix", 1, 0, OPT_SUFFIX},
410 {"block-size", 1, 0, 'B'},
413 int main(int argc,char *argv[])
415 int pid, status, pid2, status2;
418 char *shell_cmd = NULL;
419 char *shell_machine = NULL;
420 char *shell_path = NULL;
421 char *shell_user = NULL;
424 struct file_list *flist;
425 char *local_name = NULL;
427 starttime = time(NULL);
429 while ((opt = getopt_long(argc, argv,
430 short_options, long_options, &option_index))
435 printf("rsync version %s protocol version %d\n",
436 VERSION,PROTOCOL_VERSION);
440 backup_suffix = optarg;
463 case OPT_EXCLUDE_FROM:
464 add_exclude_file(optarg,1);
501 fprintf(stderr,"-o only allowed for root\n");
514 fprintf(stderr,"-D only allowed for root\n");
566 block_size = atoi(optarg);
570 fprintf(stderr,"bad option -%c\n",opt);
581 verbose = MAX(verbose,1);
584 int version = read_int(STDIN_FILENO);
585 if (version < MIN_PROTOCOL_VERSION) {
586 fprintf(stderr,"protocol version mismatch %d %d\n",
587 version,PROTOCOL_VERSION);
590 write_int(STDOUT_FILENO,PROTOCOL_VERSION);
591 write_flush(STDOUT_FILENO);
594 recv_exclude_list(STDIN_FILENO);
597 do_server_sender(argc,argv);
599 do_server_recv(argc,argv);
609 p = strchr(argv[0],':');
614 shell_machine = argv[0];
621 p = strchr(argv[argc-1],':');
627 shell_machine = NULL;
628 shell_path = argv[argc-1];
631 shell_machine = argv[argc-1];
638 p = strchr(shell_machine,'@');
641 shell_user = shell_machine;
647 fprintf(stderr,"cmd=%s machine=%s user=%s path=%s\n",
648 shell_cmd?shell_cmd:"",
649 shell_machine?shell_machine:"",
650 shell_user?shell_user:"",
651 shell_path?shell_path:"");
654 signal(SIGCHLD,SIG_IGN);
655 signal(SIGINT,SIGNAL_CAST sig_int);
656 signal(SIGPIPE,SIGNAL_CAST sig_int);
658 if (!sender && argc != 1) {
663 pid = do_cmd(shell_cmd,shell_machine,shell_user,shell_path,&f_in,&f_out);
665 write_int(f_out,PROTOCOL_VERSION);
668 int version = read_int(f_in);
669 if (version < MIN_PROTOCOL_VERSION) {
670 fprintf(stderr,"protocol version mismatch\n");
676 fprintf(stderr,"parent=%d child=%d sender=%d recurse=%d\n",
677 (int)getpid(),pid,sender,recurse);
683 send_exclude_list(f_out);
684 flist = send_file_list(f_out,recurse,argc,argv);
686 fprintf(stderr,"file list sent\n");
687 send_files(flist,f_out,f_in);
689 fprintf(stderr,"waiting on %d\n",pid);
690 waitpid(pid, &status, 0);
695 send_exclude_list(f_out);
697 flist = recv_file_list(f_in);
698 if (!flist || flist->count == 0) {
699 fprintf(stderr,"nothing to do\n");
703 local_name = get_local_name(flist,argv[0]);
705 if ((pid2=fork()) == 0) {
706 recv_files(f_in,flist,local_name);
708 fprintf(stderr,"receiver read %d\n",read_total());
712 generate_files(f_out,flist,local_name);
714 waitpid(pid2, &status2, 0);
718 waitpid(pid, &status, 0);
720 return status | status2;