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