This should fix the bug where file transfer with compression failed with
[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
a06d19e3 294 flist = send_file_list(STDOUT_FILENO,argc,argv);
c627d613
AT
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");
408 fprintf(f," --version print version number\n");
409
410 fprintf(f,"\n");
411 fprintf(f,"the backup suffix defaults to %s\n",BACKUP_SUFFIX);
412 fprintf(f,"the block size defaults to %d\n",BLOCK_SIZE);
413}
414
415enum {OPT_VERSION,OPT_SUFFIX,OPT_SENDER,OPT_SERVER,OPT_EXCLUDE,
b98c7b81 416 OPT_EXCLUDE_FROM,OPT_DELETE,OPT_RSYNC_PATH};
c627d613 417
861c20b4 418static char *short_options = "oblHpguDCtcahvrIxnSe:B:z";
c627d613
AT
419
420static struct option long_options[] = {
421 {"version", 0, 0, OPT_VERSION},
422 {"server", 0, 0, OPT_SERVER},
423 {"sender", 0, 0, OPT_SENDER},
424 {"delete", 0, 0, OPT_DELETE},
425 {"exclude", 1, 0, OPT_EXCLUDE},
426 {"exclude-from",1, 0, OPT_EXCLUDE_FROM},
427 {"rsync-path", 1, 0, OPT_RSYNC_PATH},
428 {"one-file-system",0, 0, 'x'},
429 {"ignore-times",0, 0, 'I'},
430 {"help", 0, 0, 'h'},
431 {"dry-run", 0, 0, 'n'},
dc5ddbcc 432 {"sparse", 0, 0, 'S'},
c627d613
AT
433 {"cvs-exclude", 0, 0, 'C'},
434 {"archive", 0, 0, 'a'},
435 {"checksum", 0, 0, 'c'},
436 {"backup", 0, 0, 'b'},
437 {"update", 0, 0, 'u'},
438 {"verbose", 0, 0, 'v'},
439 {"recursive", 0, 0, 'r'},
440 {"devices", 0, 0, 'D'},
441 {"perms", 0, 0, 'p'},
442 {"links", 0, 0, 'l'},
dc5ddbcc 443 {"hard-links", 0, 0, 'H'},
c627d613
AT
444 {"owner", 0, 0, 'o'},
445 {"group", 0, 0, 'g'},
446 {"times", 0, 0, 't'},
447 {"rsh", 1, 0, 'e'},
448 {"suffix", 1, 0, OPT_SUFFIX},
449 {"block-size", 1, 0, 'B'},
861c20b4 450 {"compress", 0, 0, 'z'},
c627d613
AT
451 {0,0,0,0}};
452
453int main(int argc,char *argv[])
454{
dc5ddbcc 455 int pid, status, status2;
c627d613
AT
456 int opt;
457 int option_index;
458 char *shell_cmd = NULL;
459 char *shell_machine = NULL;
460 char *shell_path = NULL;
461 char *shell_user = NULL;
462 char *p;
463 int f_in,f_out;
464 struct file_list *flist;
465 char *local_name = NULL;
466
467 starttime = time(NULL);
468
469 while ((opt = getopt_long(argc, argv,
470 short_options, long_options, &option_index))
471 != -1) {
472 switch (opt)
473 {
474 case OPT_VERSION:
475 printf("rsync version %s protocol version %d\n",
476 VERSION,PROTOCOL_VERSION);
34ccb63e 477 exit_cleanup(0);
c627d613
AT
478
479 case OPT_SUFFIX:
480 backup_suffix = optarg;
481 break;
482
483 case OPT_RSYNC_PATH:
484 rsync_path = optarg;
485 break;
486
487 case 'I':
488 ignore_times = 1;
489 break;
490
491 case 'x':
492 one_file_system=1;
493 break;
494
495 case OPT_DELETE:
496 delete_mode = 1;
497 break;
498
499 case OPT_EXCLUDE:
500 add_exclude(optarg);
501 break;
502
503 case OPT_EXCLUDE_FROM:
504 add_exclude_file(optarg,1);
505 break;
506
507 case 'h':
dc5ddbcc 508 usage(FINFO);
34ccb63e 509 exit_cleanup(0);
c627d613
AT
510
511 case 'b':
512 make_backups=1;
513 break;
514
515 case 'n':
516 dry_run=1;
517 break;
518
dc5ddbcc
AT
519 case 'S':
520 sparse_files=1;
521 break;
522
c627d613
AT
523 case 'C':
524 cvs_exclude=1;
525 break;
526
527 case 'u':
528 update_only=1;
529 break;
530
c627d613 531 case 'l':
dc5ddbcc 532#if SUPPORT_LINKS
c627d613 533 preserve_links=1;
dc5ddbcc 534#endif
c627d613 535 break;
dc5ddbcc
AT
536
537 case 'H':
538#if SUPPORT_HARD_LINKS
539 preserve_hard_links=1;
c627d613 540#endif
dc5ddbcc 541 break;
c627d613
AT
542
543 case 'p':
544 preserve_perms=1;
545 break;
546
547 case 'o':
548 if (getuid() == 0) {
549 preserve_uid=1;
550 } else {
dc5ddbcc 551 fprintf(FERROR,"-o only allowed for root\n");
34ccb63e 552 exit_cleanup(1);
c627d613
AT
553 }
554 break;
555
556 case 'g':
557 preserve_gid=1;
558 break;
559
560 case 'D':
561 if (getuid() == 0) {
562 preserve_devices=1;
563 } else {
dc5ddbcc 564 fprintf(FERROR,"-D only allowed for root\n");
34ccb63e 565 exit_cleanup(1);
c627d613
AT
566 }
567 break;
568
569 case 't':
570 preserve_times=1;
571 break;
572
573 case 'c':
574 always_checksum=1;
575 break;
576
577 case 'v':
578 verbose++;
579 break;
580
581 case 'a':
582 recurse=1;
583#if SUPPORT_LINKS
584 preserve_links=1;
585#endif
586 preserve_perms=1;
587 preserve_times=1;
588 preserve_gid=1;
589 if (getuid() == 0) {
590 preserve_devices=1;
591 preserve_uid=1;
592 }
593 break;
594
595 case OPT_SERVER:
596 am_server = 1;
597 break;
598
599 case OPT_SENDER:
600 if (!am_server) {
dc5ddbcc 601 usage(FERROR);
34ccb63e 602 exit_cleanup(1);
c627d613
AT
603 }
604 sender = 1;
605 break;
606
607 case 'r':
608 recurse = 1;
609 break;
610
611 case 'e':
612 shell_cmd = optarg;
613 break;
614
615 case 'B':
616 block_size = atoi(optarg);
617 break;
618
861c20b4
PM
619 case 'z':
620 do_compression = 1;
621 break;
622
c627d613 623 default:
861c20b4 624 /* fprintf(FERROR,"bad option -%c\n",opt); */
34ccb63e 625 exit_cleanup(1);
c627d613
AT
626 }
627 }
628
629 while (optind--) {
630 argc--;
631 argv++;
632 }
633
6b83141d
AT
634 signal(SIGCHLD,SIG_IGN);
635 signal(SIGINT,SIGNAL_CAST sig_int);
636 signal(SIGPIPE,SIGNAL_CAST sig_int);
637 signal(SIGHUP,SIGNAL_CAST sig_int);
638
c627d613
AT
639 if (dry_run)
640 verbose = MAX(verbose,1);
641
642 if (am_server) {
aae43eb3 643 setup_protocol(STDOUT_FILENO,STDIN_FILENO);
c627d613
AT
644
645 if (sender) {
646 recv_exclude_list(STDIN_FILENO);
647 if (cvs_exclude)
648 add_cvs_excludes();
649 do_server_sender(argc,argv);
650 } else {
651 do_server_recv(argc,argv);
652 }
34ccb63e 653 exit_cleanup(0);
c627d613
AT
654 }
655
656 if (argc < 2) {
dc5ddbcc 657 usage(FERROR);
34ccb63e 658 exit_cleanup(1);
c627d613
AT
659 }
660
661 p = strchr(argv[0],':');
662
663 if (p) {
664 sender = 0;
665 *p = 0;
666 shell_machine = argv[0];
667 shell_path = p+1;
668 argc--;
669 argv++;
670 } else {
671 sender = 1;
672
673 p = strchr(argv[argc-1],':');
674 if (!p) {
675 local_server = 1;
676 }
677
678 if (local_server) {
679 shell_machine = NULL;
680 shell_path = argv[argc-1];
681 } else {
682 *p = 0;
683 shell_machine = argv[argc-1];
684 shell_path = p+1;
685 }
686 argc--;
687 }
688
689 if (shell_machine) {
690 p = strchr(shell_machine,'@');
691 if (p) {
692 *p = 0;
693 shell_user = shell_machine;
694 shell_machine = p+1;
695 }
696 }
697
698 if (verbose > 3) {
dc5ddbcc 699 fprintf(FERROR,"cmd=%s machine=%s user=%s path=%s\n",
c627d613
AT
700 shell_cmd?shell_cmd:"",
701 shell_machine?shell_machine:"",
702 shell_user?shell_user:"",
703 shell_path?shell_path:"");
704 }
705
c627d613 706 if (!sender && argc != 1) {
dc5ddbcc 707 usage(FERROR);
34ccb63e 708 exit_cleanup(1);
c627d613
AT
709 }
710
711 pid = do_cmd(shell_cmd,shell_machine,shell_user,shell_path,&f_in,&f_out);
712
aae43eb3 713 setup_protocol(f_out,f_in);
4fe159a8 714
c627d613 715 if (verbose > 3)
dc5ddbcc 716 fprintf(FERROR,"parent=%d child=%d sender=%d recurse=%d\n",
c627d613
AT
717 (int)getpid(),pid,sender,recurse);
718
719 if (sender) {
720 if (cvs_exclude)
721 add_cvs_excludes();
722 if (delete_mode)
723 send_exclude_list(f_out);
a06d19e3 724 flist = send_file_list(f_out,argc,argv);
c627d613 725 if (verbose > 3)
dc5ddbcc 726 fprintf(FERROR,"file list sent\n");
c627d613
AT
727 send_files(flist,f_out,f_in);
728 if (verbose > 3)
dc5ddbcc 729 fprintf(FERROR,"waiting on %d\n",pid);
c627d613
AT
730 waitpid(pid, &status, 0);
731 report(-1);
34ccb63e 732 exit_cleanup(status);
c627d613
AT
733 }
734
735 send_exclude_list(f_out);
736
737 flist = recv_file_list(f_in);
738 if (!flist || flist->count == 0) {
dc5ddbcc 739 fprintf(FERROR,"nothing to do\n");
34ccb63e 740 exit_cleanup(0);
c627d613
AT
741 }
742
743 local_name = get_local_name(flist,argv[0]);
744
dc5ddbcc 745 status2 = do_recv(f_in,f_out,flist,local_name);
c627d613
AT
746
747 report(f_in);
748
749 waitpid(pid, &status, 0);
750
751 return status | status2;
752}