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