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