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));
240 if (chdir(name) != 0) {
241 fprintf(stderr,"chdir %s : %s\n",name,strerror(errno));
251 void do_server_sender(int argc,char *argv[])
255 struct file_list *flist;
258 fprintf(stderr,"server_sender starting pid=%d\n",(int)getpid());
260 if (chdir(dir) != 0) {
261 fprintf(stderr,"chdir %s: %s\n",dir,strerror(errno));
267 if (strcmp(dir,".")) {
273 if (argc == 0 && recurse) {
280 flist = send_file_list(STDOUT_FILENO,recurse,argc,argv);
281 send_files(flist,STDOUT_FILENO,STDIN_FILENO);
282 report(STDOUT_FILENO);
288 void do_server_recv(int argc,char *argv[])
292 struct file_list *flist;
293 char *local_name=NULL;
296 fprintf(stderr,"server_recv(%d) starting pid=%d\n",argc,(int)getpid());
302 if (chdir(dir) != 0) {
303 fprintf(stderr,"chdir %s : %s\n",dir,strerror(errno));
309 recv_exclude_list(STDIN_FILENO);
311 flist = recv_file_list(STDIN_FILENO);
312 if (!flist || flist->count == 0) {
313 fprintf(stderr,"nothing to do\n");
318 if (strcmp(dir,".")) {
319 argv[0] += strlen(dir);
320 if (argv[0][0] == '/') argv[0]++;
322 local_name = get_local_name(flist,argv[0]);
325 if ((pid=fork()) == 0) {
326 recv_files(STDIN_FILENO,flist,local_name);
328 fprintf(stderr,"receiver read %d\n",read_total());
332 generate_files(STDOUT_FILENO,flist,local_name);
334 waitpid(pid, &status, 0);
339 static void usage(FILE *f)
341 fprintf(f,"rsync version %s Copyright Andrew Tridgell and Paul Mackerras\n\n",
343 fprintf(f,"Usage:\t%s [options] src user@host:dest\nOR",RSYNC_NAME);
344 fprintf(f,"\t%s [options] user@host:src dest\n\n",RSYNC_NAME);
345 fprintf(f,"Options:\n");
346 fprintf(f,"-v, --verbose increase verbosity\n");
347 fprintf(f,"-c, --checksum always checksum\n");
348 fprintf(f,"-a, --archive archive mode (same as -rlptDog)\n");
349 fprintf(f,"-r, --recursive recurse into directories\n");
350 fprintf(f,"-b, --backup make backups (default ~ extension)\n");
351 fprintf(f,"-u, --update update only (don't overwrite newer files)\n");
352 fprintf(f,"-l, --links preserve soft links\n");
353 fprintf(f,"-p, --perms preserve permissions\n");
354 fprintf(f,"-o, --owner preserve owner (root only)\n");
355 fprintf(f,"-g, --group preserve group\n");
356 fprintf(f,"-D, --devices preserve devices (root only)\n");
357 fprintf(f,"-t, --times preserve times\n");
358 fprintf(f,"-n, --dry-run show what would have been transferred\n");
359 fprintf(f,"-x, --one-file-system don't cross filesystem boundaries\n");
360 fprintf(f,"-B, --block-size SIZE checksum blocking size\n");
361 fprintf(f,"-e, --rsh COMMAND specify rsh replacement\n");
362 fprintf(f," --rsync-path PATH specify path to rsync on the remote machine\n");
363 fprintf(f,"-C, --cvs-exclude auto ignore files in the same way CVS does\n");
364 fprintf(f," --delete delete files that don't exist on the sending side\n");
365 fprintf(f,"-I, --ignore-times don't exclude files that match length and time\n");
366 fprintf(f," --exclude FILE exclude file FILE\n");
367 fprintf(f," --exclude-from FILE exclude files listed in FILE\n");
368 fprintf(f," --suffix SUFFIX override backup suffix\n");
369 fprintf(f," --version print version number\n");
372 fprintf(f,"the backup suffix defaults to %s\n",BACKUP_SUFFIX);
373 fprintf(f,"the block size defaults to %d\n",BLOCK_SIZE);
376 enum {OPT_VERSION,OPT_SUFFIX,OPT_SENDER,OPT_SERVER,OPT_EXCLUDE,
377 OPT_EXCLUDE_FROM,OPT_DELETE,OPT_RSYNC_PATH};
379 static char *short_options = "oblpguDCtcahvrIxne:B:";
381 static struct option long_options[] = {
382 {"version", 0, 0, OPT_VERSION},
383 {"server", 0, 0, OPT_SERVER},
384 {"sender", 0, 0, OPT_SENDER},
385 {"delete", 0, 0, OPT_DELETE},
386 {"exclude", 1, 0, OPT_EXCLUDE},
387 {"exclude-from",1, 0, OPT_EXCLUDE_FROM},
388 {"rsync-path", 1, 0, OPT_RSYNC_PATH},
389 {"one-file-system",0, 0, 'x'},
390 {"ignore-times",0, 0, 'I'},
392 {"dry-run", 0, 0, 'n'},
393 {"cvs-exclude", 0, 0, 'C'},
394 {"archive", 0, 0, 'a'},
395 {"checksum", 0, 0, 'c'},
396 {"backup", 0, 0, 'b'},
397 {"update", 0, 0, 'u'},
398 {"verbose", 0, 0, 'v'},
399 {"recursive", 0, 0, 'r'},
400 {"devices", 0, 0, 'D'},
401 {"perms", 0, 0, 'p'},
402 {"links", 0, 0, 'l'},
403 {"owner", 0, 0, 'o'},
404 {"group", 0, 0, 'g'},
405 {"times", 0, 0, 't'},
407 {"suffix", 1, 0, OPT_SUFFIX},
408 {"block-size", 1, 0, 'B'},
411 int main(int argc,char *argv[])
413 int pid, status, pid2, status2;
416 char *shell_cmd = NULL;
417 char *shell_machine = NULL;
418 char *shell_path = NULL;
419 char *shell_user = NULL;
422 struct file_list *flist;
423 char *local_name = NULL;
425 starttime = time(NULL);
427 while ((opt = getopt_long(argc, argv,
428 short_options, long_options, &option_index))
433 printf("rsync version %s protocol version %d\n",
434 VERSION,PROTOCOL_VERSION);
438 backup_suffix = optarg;
461 case OPT_EXCLUDE_FROM:
462 add_exclude_file(optarg,1);
499 fprintf(stderr,"-o only allowed for root\n");
512 fprintf(stderr,"-D only allowed for root\n");
564 block_size = atoi(optarg);
568 fprintf(stderr,"bad option -%c\n",opt);
579 verbose = MAX(verbose,1);
582 int version = read_int(STDIN_FILENO);
583 if (version != PROTOCOL_VERSION) {
584 fprintf(stderr,"protocol version mismatch %d %d\n",
585 version,PROTOCOL_VERSION);
588 write_int(STDOUT_FILENO,PROTOCOL_VERSION);
589 write_flush(STDOUT_FILENO);
592 recv_exclude_list(STDIN_FILENO);
595 do_server_sender(argc,argv);
597 do_server_recv(argc,argv);
607 p = strchr(argv[0],':');
612 shell_machine = argv[0];
619 p = strchr(argv[argc-1],':');
625 shell_machine = NULL;
626 shell_path = argv[argc-1];
629 shell_machine = argv[argc-1];
636 p = strchr(shell_machine,'@');
639 shell_user = shell_machine;
645 fprintf(stderr,"cmd=%s machine=%s user=%s path=%s\n",
646 shell_cmd?shell_cmd:"",
647 shell_machine?shell_machine:"",
648 shell_user?shell_user:"",
649 shell_path?shell_path:"");
652 signal(SIGCHLD,SIG_IGN);
653 signal(SIGINT,sig_int);
655 if (!sender && argc != 1) {
660 pid = do_cmd(shell_cmd,shell_machine,shell_user,shell_path,&f_in,&f_out);
662 write_int(f_out,PROTOCOL_VERSION);
665 int version = read_int(f_in);
666 if (version != PROTOCOL_VERSION) {
667 fprintf(stderr,"protocol version mismatch\n");
673 fprintf(stderr,"parent=%d child=%d sender=%d recurse=%d\n",
674 (int)getpid(),pid,sender,recurse);
680 send_exclude_list(f_out);
681 flist = send_file_list(f_out,recurse,argc,argv);
683 fprintf(stderr,"file list sent\n");
684 send_files(flist,f_out,f_in);
686 fprintf(stderr,"waiting on %d\n",pid);
687 waitpid(pid, &status, 0);
692 send_exclude_list(f_out);
694 flist = recv_file_list(f_in);
695 if (!flist || flist->count == 0) {
696 fprintf(stderr,"nothing to do\n");
700 local_name = get_local_name(flist,argv[0]);
702 if ((pid2=fork()) == 0) {
703 recv_files(f_in,flist,local_name);
705 fprintf(stderr,"receiver read %d\n",read_total());
709 generate_files(f_out,flist,local_name);
711 waitpid(pid2, &status2, 0);
715 waitpid(pid, &status, 0);
717 return status | status2;