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