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