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