- handle no mmap for munmap
[rsync/rsync.git] / main.c
CommitLineData
c627d613
AT
1/*
2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
4
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.
9
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.
14
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.
18*/
19
20#include "rsync.h"
21
22int verbose = 0;
23int always_checksum = 0;
24time_t starttime;
25off_t total_size = 0;
26int block_size=BLOCK_SIZE;
27
28char *backup_suffix = BACKUP_SUFFIX;
29
30static char *rsync_path = RSYNC_NAME;
31
32int make_backups = 0;
33int preserve_links = 0;
34int preserve_perms = 0;
35int preserve_devices = 0;
36int preserve_uid = 0;
37int preserve_gid = 0;
38int preserve_times = 0;
39int update_only = 0;
40int cvs_exclude = 0;
41int dry_run=0;
42int local_server=0;
43int ignore_times=0;
44int delete_mode=0;
45int one_file_system=0;
4fe159a8 46int remote_version=0;
43a481dc 47int csum_length=SUM_LENGTH;
c627d613
AT
48
49int am_server = 0;
50static int sender = 0;
51int recurse = 0;
52
53static void usage(FILE *f);
54
55static void report(int f)
56{
57 int in,out,tsize;
58 time_t t = time(NULL);
59
60 if (!verbose) return;
61
62 if (am_server && sender) {
63 write_int(f,read_total());
64 write_int(f,write_total());
65 write_int(f,total_size);
66 write_flush(f);
67 return;
68 }
69
70 if (sender) {
71 in = read_total();
72 out = write_total();
73 tsize = (int)total_size;
74 } else {
75 in = read_int(f);
76 out = read_int(f);
77 tsize = read_int(f);
78 }
79
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));
84}
85
86
87static void server_options(char **args,int *argc)
88{
89 int ac = *argc;
90 static char argstr[50];
91 static char bsize[30];
43a481dc 92 static char slength[30];
c627d613
AT
93 int i, x;
94
95 args[ac++] = "--server";
96
97 if (!sender)
98 args[ac++] = "--sender";
99
100 x = 1;
101 argstr[0] = '-';
102 for (i=0;i<verbose;i++)
103 argstr[x++] = 'v';
104 if (make_backups)
105 argstr[x++] = 'b';
106 if (update_only)
107 argstr[x++] = 'u';
108 if (dry_run)
109 argstr[x++] = 'n';
110 if (preserve_links)
111 argstr[x++] = 'l';
112 if (preserve_uid)
113 argstr[x++] = 'o';
114 if (preserve_gid)
115 argstr[x++] = 'g';
116 if (preserve_devices)
117 argstr[x++] = 'D';
118 if (preserve_times)
119 argstr[x++] = 't';
120 if (preserve_perms)
121 argstr[x++] = 'p';
122 if (recurse)
123 argstr[x++] = 'r';
124 if (always_checksum)
125 argstr[x++] = 'c';
126 if (cvs_exclude)
127 argstr[x++] = 'C';
128 if (ignore_times)
129 argstr[x++] = 'I';
130 if (one_file_system)
131 argstr[x++] = 'x';
132 argstr[x] = 0;
133
134 if (x != 1) args[ac++] = argstr;
135
136 if (block_size != BLOCK_SIZE) {
137 sprintf(bsize,"-B%d",block_size);
138 args[ac++] = bsize;
139 }
43a481dc
AT
140
141 if (csum_length != SUM_LENGTH) {
142 sprintf(slength,"--csum-length=%d",csum_length);
143 args[ac++] = slength;
144 }
c627d613
AT
145
146 if (delete_mode)
147 args[ac++] = "--delete";
148
149 *argc = ac;
150}
151
152
153
154int do_cmd(char *cmd,char *machine,char *user,char *path,int *f_in,int *f_out)
155{
156 char *args[100];
157 int i,argc=0;
158 char *tok,*p;
159
160 if (!local_server) {
161 if (!cmd)
162 cmd = getenv(RSYNC_RSH_ENV);
163 if (!cmd)
164 cmd = RSYNC_RSH;
165 cmd = strdup(cmd);
166 if (!cmd)
167 goto oom;
168
169 for (tok=strtok(cmd," ");tok;tok=strtok(NULL," ")) {
170 args[argc++] = tok;
171 }
172
173 if (user) {
174 args[argc++] = "-l";
175 args[argc++] = user;
176 }
177 args[argc++] = machine;
178 }
179
180 args[argc++] = rsync_path;
181
182 server_options(args,&argc);
183
184 if (path && *path) {
185 char *dir = strdup(path);
186 p = strrchr(dir,'/');
187 if (p) {
188 *p = 0;
189 args[argc++] = dir;
190 p++;
191 } else {
192 args[argc++] = ".";
193 p = dir;
194 }
195 if (p[0])
196 args[argc++] = path;
197 }
198
199 args[argc] = NULL;
200
201 if (verbose > 3) {
202 fprintf(stderr,"cmd=");
203 for (i=0;i<argc;i++)
204 fprintf(stderr,"%s ",args[i]);
205 fprintf(stderr,"\n");
206 }
207
208 return piped_child(args,f_in,f_out);
209
210oom:
211 out_of_memory("do_cmd");
212 return 0; /* not reached */
213}
214
215
216
217
218static char *get_local_name(struct file_list *flist,char *name)
219{
220 struct stat st;
221
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));
226 exit(1);
227 }
228 return NULL;
229 }
230 if (flist->count > 1) {
231 fprintf(stderr,"ERROR: destination must be a directory when copying more than 1 file\n");
232 exit(1);
233 }
234 return name;
235 }
236
237 if (flist->count == 1)
238 return name;
239
240 if (!name)
241 return NULL;
242
243 if (mkdir(name,0777) != 0) {
244 fprintf(stderr,"mkdir %s : %s\n",name,strerror(errno));
245 exit(1);
94481d91
AT
246 } else {
247 fprintf(am_server?stderr:stdout,"created directory %s\n",name);
c627d613
AT
248 }
249
250 if (chdir(name) != 0) {
251 fprintf(stderr,"chdir %s : %s\n",name,strerror(errno));
252 exit(1);
253 }
254
255 return NULL;
256}
257
258
259
260
261void do_server_sender(int argc,char *argv[])
262{
263 int i;
264 char *dir = argv[0];
265 struct file_list *flist;
266
267 if (verbose > 2)
268 fprintf(stderr,"server_sender starting pid=%d\n",(int)getpid());
269
270 if (chdir(dir) != 0) {
271 fprintf(stderr,"chdir %s: %s\n",dir,strerror(errno));
272 exit(1);
273 }
274 argc--;
275 argv++;
276
277 if (strcmp(dir,".")) {
278 int l = strlen(dir);
279 for (i=0;i<argc;i++)
280 argv[i] += l+1;
281 }
282
283 if (argc == 0 && recurse) {
284 argc=1;
285 argv--;
286 argv[0] = ".";
287 }
288
289
290 flist = send_file_list(STDOUT_FILENO,recurse,argc,argv);
291 send_files(flist,STDOUT_FILENO,STDIN_FILENO);
292 report(STDOUT_FILENO);
293 exit(0);
294}
295
296
297
298void do_server_recv(int argc,char *argv[])
299{
300 int pid,status;
301 char *dir = NULL;
302 struct file_list *flist;
303 char *local_name=NULL;
304
305 if (verbose > 2)
306 fprintf(stderr,"server_recv(%d) starting pid=%d\n",argc,(int)getpid());
307
308 if (argc > 0) {
309 dir = argv[0];
310 argc--;
311 argv++;
312 if (chdir(dir) != 0) {
313 fprintf(stderr,"chdir %s : %s\n",dir,strerror(errno));
314 exit(1);
315 }
316 }
317
318 if (delete_mode)
319 recv_exclude_list(STDIN_FILENO);
320
321 flist = recv_file_list(STDIN_FILENO);
322 if (!flist || flist->count == 0) {
323 fprintf(stderr,"nothing to do\n");
324 exit(1);
325 }
326
327 if (argc > 0) {
328 if (strcmp(dir,".")) {
329 argv[0] += strlen(dir);
330 if (argv[0][0] == '/') argv[0]++;
331 }
332 local_name = get_local_name(flist,argv[0]);
333 }
334
335 if ((pid=fork()) == 0) {
336 recv_files(STDIN_FILENO,flist,local_name);
337 if (verbose > 2)
338 fprintf(stderr,"receiver read %d\n",read_total());
339 exit(0);
340 }
341
342 generate_files(STDOUT_FILENO,flist,local_name);
343
344 waitpid(pid, &status, 0);
345 exit(status);
346}
347
348
349static void usage(FILE *f)
350{
351 fprintf(f,"rsync version %s Copyright Andrew Tridgell and Paul Mackerras\n\n",
352 VERSION);
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");
43a481dc 379 fprintf(f," --csum-length LENGTH set the checksum length\n");
c627d613
AT
380 fprintf(f," --version print version number\n");
381
382 fprintf(f,"\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);
385}
386
387enum {OPT_VERSION,OPT_SUFFIX,OPT_SENDER,OPT_SERVER,OPT_EXCLUDE,
43a481dc 388 OPT_EXCLUDE_FROM,OPT_DELETE,OPT_RSYNC_PATH,OPT_CSUM_LENGTH};
c627d613
AT
389
390static char *short_options = "oblpguDCtcahvrIxne:B:";
391
392static 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},
43a481dc 400 {"csum-length", 1, 0, OPT_CSUM_LENGTH},
c627d613
AT
401 {"one-file-system",0, 0, 'x'},
402 {"ignore-times",0, 0, 'I'},
403 {"help", 0, 0, 'h'},
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'},
418 {"rsh", 1, 0, 'e'},
419 {"suffix", 1, 0, OPT_SUFFIX},
420 {"block-size", 1, 0, 'B'},
421 {0,0,0,0}};
422
423int main(int argc,char *argv[])
424{
425 int pid, status, pid2, status2;
426 int opt;
427 int option_index;
428 char *shell_cmd = NULL;
429 char *shell_machine = NULL;
430 char *shell_path = NULL;
431 char *shell_user = NULL;
432 char *p;
433 int f_in,f_out;
434 struct file_list *flist;
435 char *local_name = NULL;
436
437 starttime = time(NULL);
438
439 while ((opt = getopt_long(argc, argv,
440 short_options, long_options, &option_index))
441 != -1) {
442 switch (opt)
443 {
444 case OPT_VERSION:
445 printf("rsync version %s protocol version %d\n",
446 VERSION,PROTOCOL_VERSION);
447 exit(0);
448
449 case OPT_SUFFIX:
450 backup_suffix = optarg;
451 break;
452
453 case OPT_RSYNC_PATH:
454 rsync_path = optarg;
455 break;
456
43a481dc
AT
457 case OPT_CSUM_LENGTH:
458 csum_length = atoi(optarg);
459 csum_length = MIN(csum_length,SUM_LENGTH);
460 break;
461
c627d613
AT
462 case 'I':
463 ignore_times = 1;
464 break;
465
466 case 'x':
467 one_file_system=1;
468 break;
469
470 case OPT_DELETE:
471 delete_mode = 1;
472 break;
473
474 case OPT_EXCLUDE:
475 add_exclude(optarg);
476 break;
477
478 case OPT_EXCLUDE_FROM:
479 add_exclude_file(optarg,1);
480 break;
481
482 case 'h':
483 usage(stdout);
484 exit(0);
485
486 case 'b':
487 make_backups=1;
488 break;
489
490 case 'n':
491 dry_run=1;
492 break;
493
494 case 'C':
495 cvs_exclude=1;
496 break;
497
498 case 'u':
499 update_only=1;
500 break;
501
502#if SUPPORT_LINKS
503 case 'l':
504 preserve_links=1;
505 break;
506#endif
507
508 case 'p':
509 preserve_perms=1;
510 break;
511
512 case 'o':
513 if (getuid() == 0) {
514 preserve_uid=1;
515 } else {
516 fprintf(stderr,"-o only allowed for root\n");
517 exit(1);
518 }
519 break;
520
521 case 'g':
522 preserve_gid=1;
523 break;
524
525 case 'D':
526 if (getuid() == 0) {
527 preserve_devices=1;
528 } else {
529 fprintf(stderr,"-D only allowed for root\n");
530 exit(1);
531 }
532 break;
533
534 case 't':
535 preserve_times=1;
536 break;
537
538 case 'c':
539 always_checksum=1;
540 break;
541
542 case 'v':
543 verbose++;
544 break;
545
546 case 'a':
547 recurse=1;
548#if SUPPORT_LINKS
549 preserve_links=1;
550#endif
551 preserve_perms=1;
552 preserve_times=1;
553 preserve_gid=1;
554 if (getuid() == 0) {
555 preserve_devices=1;
556 preserve_uid=1;
557 }
558 break;
559
560 case OPT_SERVER:
561 am_server = 1;
562 break;
563
564 case OPT_SENDER:
565 if (!am_server) {
566 usage(stderr);
567 exit(1);
568 }
569 sender = 1;
570 break;
571
572 case 'r':
573 recurse = 1;
574 break;
575
576 case 'e':
577 shell_cmd = optarg;
578 break;
579
580 case 'B':
581 block_size = atoi(optarg);
582 break;
583
584 default:
585 fprintf(stderr,"bad option -%c\n",opt);
586 exit(1);
587 }
588 }
589
590 while (optind--) {
591 argc--;
592 argv++;
593 }
594
595 if (dry_run)
596 verbose = MAX(verbose,1);
597
598 if (am_server) {
4fe159a8 599 remote_version = read_int(STDIN_FILENO);
7bec6a5c
AT
600 if (remote_version < MIN_PROTOCOL_VERSION ||
601 remote_version > MAX_PROTOCOL_VERSION) {
602 fprintf(stderr,"protocol version mismatch - is your shell clean?\n");
c627d613
AT
603 exit(1);
604 }
605 write_int(STDOUT_FILENO,PROTOCOL_VERSION);
606 write_flush(STDOUT_FILENO);
4fe159a8
AT
607
608 setup_protocol();
c627d613
AT
609
610 if (sender) {
611 recv_exclude_list(STDIN_FILENO);
612 if (cvs_exclude)
613 add_cvs_excludes();
614 do_server_sender(argc,argv);
615 } else {
616 do_server_recv(argc,argv);
617 }
618 exit(0);
619 }
620
621 if (argc < 2) {
622 usage(stderr);
623 exit(1);
624 }
625
626 p = strchr(argv[0],':');
627
628 if (p) {
629 sender = 0;
630 *p = 0;
631 shell_machine = argv[0];
632 shell_path = p+1;
633 argc--;
634 argv++;
635 } else {
636 sender = 1;
637
638 p = strchr(argv[argc-1],':');
639 if (!p) {
640 local_server = 1;
641 }
642
643 if (local_server) {
644 shell_machine = NULL;
645 shell_path = argv[argc-1];
646 } else {
647 *p = 0;
648 shell_machine = argv[argc-1];
649 shell_path = p+1;
650 }
651 argc--;
652 }
653
654 if (shell_machine) {
655 p = strchr(shell_machine,'@');
656 if (p) {
657 *p = 0;
658 shell_user = shell_machine;
659 shell_machine = p+1;
660 }
661 }
662
663 if (verbose > 3) {
664 fprintf(stderr,"cmd=%s machine=%s user=%s path=%s\n",
665 shell_cmd?shell_cmd:"",
666 shell_machine?shell_machine:"",
667 shell_user?shell_user:"",
668 shell_path?shell_path:"");
669 }
670
671 signal(SIGCHLD,SIG_IGN);
ac1eb754 672 signal(SIGINT,SIGNAL_CAST sig_int);
58d433ab 673 signal(SIGPIPE,SIGNAL_CAST sig_int);
c627d613
AT
674
675 if (!sender && argc != 1) {
676 usage(stderr);
677 exit(1);
678 }
679
680 pid = do_cmd(shell_cmd,shell_machine,shell_user,shell_path,&f_in,&f_out);
681
682 write_int(f_out,PROTOCOL_VERSION);
683 write_flush(f_out);
684 {
4fe159a8 685 remote_version = read_int(f_in);
7bec6a5c
AT
686 if (remote_version < MIN_PROTOCOL_VERSION ||
687 remote_version > MAX_PROTOCOL_VERSION) {
688 fprintf(stderr,"protocol version mismatch - is your shell clean?\n");
c627d613
AT
689 exit(1);
690 }
691 }
692
4fe159a8
AT
693 setup_protocol();
694
c627d613
AT
695 if (verbose > 3)
696 fprintf(stderr,"parent=%d child=%d sender=%d recurse=%d\n",
697 (int)getpid(),pid,sender,recurse);
698
699 if (sender) {
700 if (cvs_exclude)
701 add_cvs_excludes();
702 if (delete_mode)
703 send_exclude_list(f_out);
704 flist = send_file_list(f_out,recurse,argc,argv);
705 if (verbose > 3)
706 fprintf(stderr,"file list sent\n");
707 send_files(flist,f_out,f_in);
708 if (verbose > 3)
709 fprintf(stderr,"waiting on %d\n",pid);
710 waitpid(pid, &status, 0);
711 report(-1);
712 exit(status);
713 }
714
715 send_exclude_list(f_out);
716
717 flist = recv_file_list(f_in);
718 if (!flist || flist->count == 0) {
719 fprintf(stderr,"nothing to do\n");
720 exit(0);
721 }
722
723 local_name = get_local_name(flist,argv[0]);
724
725 if ((pid2=fork()) == 0) {
726 recv_files(f_in,flist,local_name);
727 if (verbose > 1)
728 fprintf(stderr,"receiver read %d\n",read_total());
729 exit(0);
730 }
731
732 generate_files(f_out,flist,local_name);
733
734 waitpid(pid2, &status2, 0);
735
736 report(f_in);
737
738 waitpid(pid, &status, 0);
739
740 return status | status2;
741}