check a few HAVE_* macros
[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
230 if (path && *path) {
82306bf6 231 dir = strdup(path);
c627d613 232 p = strrchr(dir,'/');
6574b4f7 233 if (p && !relative_paths) {
c627d613 234 *p = 0;
7b8356d0
AT
235 if (!dir[0])
236 args[argc++] = "/";
237 else
238 args[argc++] = dir;
c627d613
AT
239 p++;
240 } else {
241 args[argc++] = ".";
242 p = dir;
243 }
244 if (p[0])
245 args[argc++] = path;
246 }
247
248 args[argc] = NULL;
249
250 if (verbose > 3) {
dc5ddbcc 251 fprintf(FERROR,"cmd=");
c627d613 252 for (i=0;i<argc;i++)
dc5ddbcc
AT
253 fprintf(FERROR,"%s ",args[i]);
254 fprintf(FERROR,"\n");
c627d613
AT
255 }
256
82306bf6
AT
257 ret = piped_child(args,f_in,f_out);
258 if (dir) free(dir);
259
260 return ret;
c627d613
AT
261
262oom:
263 out_of_memory("do_cmd");
264 return 0; /* not reached */
265}
266
267
268
269
270static char *get_local_name(struct file_list *flist,char *name)
271{
272 struct stat st;
273
274 if (stat(name,&st) == 0) {
275 if (S_ISDIR(st.st_mode)) {
276 if (chdir(name) != 0) {
6574b4f7 277 fprintf(FERROR,"chdir %s : %s (1)\n",name,strerror(errno));
34ccb63e 278 exit_cleanup(1);
c627d613
AT
279 }
280 return NULL;
281 }
282 if (flist->count > 1) {
dc5ddbcc 283 fprintf(FERROR,"ERROR: destination must be a directory when copying more than 1 file\n");
34ccb63e 284 exit_cleanup(1);
c627d613
AT
285 }
286 return name;
287 }
288
289 if (flist->count == 1)
290 return name;
291
292 if (!name)
293 return NULL;
294
7b8356d0 295 if (mkdir(name,0777 & ~orig_umask) != 0) {
6574b4f7 296 fprintf(FERROR,"mkdir %s : %s (1)\n",name,strerror(errno));
34ccb63e 297 exit_cleanup(1);
94481d91 298 } else {
dc5ddbcc 299 fprintf(FINFO,"created directory %s\n",name);
c627d613
AT
300 }
301
302 if (chdir(name) != 0) {
6574b4f7 303 fprintf(FERROR,"chdir %s : %s (2)\n",name,strerror(errno));
34ccb63e 304 exit_cleanup(1);
c627d613
AT
305 }
306
307 return NULL;
308}
309
310
311
312
313void do_server_sender(int argc,char *argv[])
314{
315 int i;
316 char *dir = argv[0];
317 struct file_list *flist;
318
319 if (verbose > 2)
dc5ddbcc 320 fprintf(FERROR,"server_sender starting pid=%d\n",(int)getpid());
c627d613 321
6574b4f7
AT
322 if (!relative_paths && chdir(dir) != 0) {
323 fprintf(FERROR,"chdir %s: %s (3)\n",dir,strerror(errno));
34ccb63e 324 exit_cleanup(1);
c627d613
AT
325 }
326 argc--;
327 argv++;
328
329 if (strcmp(dir,".")) {
330 int l = strlen(dir);
8bf73749
AT
331 if (strcmp(dir,"/") == 0)
332 l = 0;
c627d613
AT
333 for (i=0;i<argc;i++)
334 argv[i] += l+1;
335 }
336
337 if (argc == 0 && recurse) {
338 argc=1;
339 argv--;
340 argv[0] = ".";
341 }
342
343
a06d19e3 344 flist = send_file_list(STDOUT_FILENO,argc,argv);
c627d613
AT
345 send_files(flist,STDOUT_FILENO,STDIN_FILENO);
346 report(STDOUT_FILENO);
34ccb63e 347 exit_cleanup(0);
c627d613
AT
348}
349
350
dc5ddbcc
AT
351static int do_recv(int f_in,int f_out,struct file_list *flist,char *local_name)
352{
353 int pid;
354 int status=0;
c6e7fcb4 355 int recv_pipe[2];
dc5ddbcc
AT
356
357 if (preserve_hard_links)
358 init_hard_links(flist);
359
c6e7fcb4
AT
360 if (pipe(recv_pipe) < 0) {
361 fprintf(FERROR,"pipe failed in do_recv\n");
362 exit(1);
363 }
364
365
3ba62a83 366 if ((pid=do_fork()) == 0) {
c6e7fcb4 367 recv_files(f_in,flist,local_name,recv_pipe[1]);
dc5ddbcc 368 if (verbose > 2)
3a6a366f 369 fprintf(FERROR,"receiver read %ld\n",(long)read_total());
dc5ddbcc
AT
370 exit_cleanup(0);
371 }
372
c6e7fcb4 373 generate_files(f_out,flist,local_name,recv_pipe[0]);
dc5ddbcc
AT
374
375 waitpid(pid, &status, 0);
376
377 return status;
378}
379
c627d613
AT
380
381void do_server_recv(int argc,char *argv[])
382{
dc5ddbcc 383 int status;
c627d613
AT
384 char *dir = NULL;
385 struct file_list *flist;
386 char *local_name=NULL;
387
388 if (verbose > 2)
dc5ddbcc 389 fprintf(FERROR,"server_recv(%d) starting pid=%d\n",argc,(int)getpid());
c627d613
AT
390
391 if (argc > 0) {
392 dir = argv[0];
393 argc--;
394 argv++;
395 if (chdir(dir) != 0) {
6574b4f7 396 fprintf(FERROR,"chdir %s : %s (4)\n",dir,strerror(errno));
34ccb63e 397 exit_cleanup(1);
c627d613
AT
398 }
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) {
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]);
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");
f6c34742 454 fprintf(f," --numeric-ids don't map uid/gid values by user/group name\n");
c627d613 455 fprintf(f,"-I, --ignore-times don't exclude files that match length and time\n");
950ab32d 456 fprintf(f,"-T --temp-dir DIR create temporary files in directory DIR\n");
861c20b4 457 fprintf(f,"-z, --compress compress file data\n");
c627d613
AT
458 fprintf(f," --exclude FILE exclude file FILE\n");
459 fprintf(f," --exclude-from FILE exclude files listed in FILE\n");
460 fprintf(f," --suffix SUFFIX override backup suffix\n");
461 fprintf(f," --version print version number\n");
462
463 fprintf(f,"\n");
464 fprintf(f,"the backup suffix defaults to %s\n",BACKUP_SUFFIX);
465 fprintf(f,"the block size defaults to %d\n",BLOCK_SIZE);
466}
467
468enum {OPT_VERSION,OPT_SUFFIX,OPT_SENDER,OPT_SERVER,OPT_EXCLUDE,
f6c34742 469 OPT_EXCLUDE_FROM,OPT_DELETE,OPT_NUMERIC_IDS,OPT_RSYNC_PATH};
c627d613 470
950ab32d 471static char *short_options = "oblLWHpguDCtcahvrRIxnSe:B:T:z";
c627d613
AT
472
473static struct option long_options[] = {
474 {"version", 0, 0, OPT_VERSION},
475 {"server", 0, 0, OPT_SERVER},
476 {"sender", 0, 0, OPT_SENDER},
477 {"delete", 0, 0, OPT_DELETE},
f6c34742 478 {"numeric-ids", 0, 0, OPT_NUMERIC_IDS},
c627d613
AT
479 {"exclude", 1, 0, OPT_EXCLUDE},
480 {"exclude-from",1, 0, OPT_EXCLUDE_FROM},
481 {"rsync-path", 1, 0, OPT_RSYNC_PATH},
482 {"one-file-system",0, 0, 'x'},
483 {"ignore-times",0, 0, 'I'},
484 {"help", 0, 0, 'h'},
485 {"dry-run", 0, 0, 'n'},
dc5ddbcc 486 {"sparse", 0, 0, 'S'},
c627d613
AT
487 {"cvs-exclude", 0, 0, 'C'},
488 {"archive", 0, 0, 'a'},
489 {"checksum", 0, 0, 'c'},
490 {"backup", 0, 0, 'b'},
491 {"update", 0, 0, 'u'},
492 {"verbose", 0, 0, 'v'},
493 {"recursive", 0, 0, 'r'},
6574b4f7 494 {"relative", 0, 0, 'R'},
c627d613
AT
495 {"devices", 0, 0, 'D'},
496 {"perms", 0, 0, 'p'},
497 {"links", 0, 0, 'l'},
82306bf6
AT
498 {"copy-links", 0, 0, 'L'},
499 {"whole-file", 0, 0, 'W'},
dc5ddbcc 500 {"hard-links", 0, 0, 'H'},
c627d613
AT
501 {"owner", 0, 0, 'o'},
502 {"group", 0, 0, 'g'},
503 {"times", 0, 0, 't'},
504 {"rsh", 1, 0, 'e'},
505 {"suffix", 1, 0, OPT_SUFFIX},
506 {"block-size", 1, 0, 'B'},
950ab32d 507 {"temp-dir", 1, 0, 'T'},
861c20b4 508 {"compress", 0, 0, 'z'},
c627d613
AT
509 {0,0,0,0}};
510
82306bf6
AT
511RETSIGTYPE sigusr1_handler(int val) {
512 exit_cleanup(1);
513}
514
c627d613
AT
515int main(int argc,char *argv[])
516{
774ef68f 517 int pid, status = 0, status2 = 0;
c627d613
AT
518 int opt;
519 int option_index;
520 char *shell_cmd = NULL;
521 char *shell_machine = NULL;
522 char *shell_path = NULL;
523 char *shell_user = NULL;
524 char *p;
525 int f_in,f_out;
526 struct file_list *flist;
527 char *local_name = NULL;
528
82306bf6
AT
529 signal(SIGUSR1, sigusr1_handler);
530
c627d613 531 starttime = time(NULL);
7b8356d0
AT
532 am_root = (getuid() == 0);
533
534 /* we set a 0 umask so that correct file permissions can be
535 carried across */
3a6a366f 536 orig_umask = (int)umask(0);
c627d613
AT
537
538 while ((opt = getopt_long(argc, argv,
539 short_options, long_options, &option_index))
540 != -1) {
541 switch (opt)
542 {
543 case OPT_VERSION:
544 printf("rsync version %s protocol version %d\n",
545 VERSION,PROTOCOL_VERSION);
34ccb63e 546 exit_cleanup(0);
c627d613
AT
547
548 case OPT_SUFFIX:
549 backup_suffix = optarg;
550 break;
551
552 case OPT_RSYNC_PATH:
553 rsync_path = optarg;
554 break;
555
556 case 'I':
557 ignore_times = 1;
558 break;
559
560 case 'x':
561 one_file_system=1;
562 break;
563
564 case OPT_DELETE:
565 delete_mode = 1;
566 break;
567
f6c34742
AT
568 case OPT_NUMERIC_IDS:
569 numeric_ids = 1;
570 break;
571
c627d613
AT
572 case OPT_EXCLUDE:
573 add_exclude(optarg);
574 break;
575
576 case OPT_EXCLUDE_FROM:
577 add_exclude_file(optarg,1);
578 break;
579
580 case 'h':
dc5ddbcc 581 usage(FINFO);
34ccb63e 582 exit_cleanup(0);
c627d613
AT
583
584 case 'b':
585 make_backups=1;
586 break;
587
588 case 'n':
589 dry_run=1;
590 break;
591
dc5ddbcc
AT
592 case 'S':
593 sparse_files=1;
594 break;
595
c627d613
AT
596 case 'C':
597 cvs_exclude=1;
598 break;
599
600 case 'u':
601 update_only=1;
602 break;
603
c627d613
AT
604 case 'l':
605 preserve_links=1;
606 break;
dc5ddbcc 607
82306bf6
AT
608 case 'L':
609 copy_links=1;
610 break;
611
612 case 'W':
613 whole_file=1;
614 break;
615
dc5ddbcc
AT
616 case 'H':
617#if SUPPORT_HARD_LINKS
618 preserve_hard_links=1;
cbbe4892
AT
619#else
620 fprintf(FERROR,"ERROR: hard links not supported on this platform\n");
621 exit_cleanup(1);
c627d613 622#endif
dc5ddbcc 623 break;
c627d613
AT
624
625 case 'p':
626 preserve_perms=1;
627 break;
628
629 case 'o':
7b8356d0 630 preserve_uid=1;
c627d613
AT
631 break;
632
633 case 'g':
634 preserve_gid=1;
635 break;
636
637 case 'D':
7b8356d0 638 preserve_devices=1;
c627d613
AT
639 break;
640
641 case 't':
642 preserve_times=1;
643 break;
644
645 case 'c':
646 always_checksum=1;
647 break;
648
649 case 'v':
650 verbose++;
651 break;
652
653 case 'a':
654 recurse=1;
655#if SUPPORT_LINKS
656 preserve_links=1;
657#endif
658 preserve_perms=1;
659 preserve_times=1;
660 preserve_gid=1;
7b8356d0 661 if (am_root) {
c627d613
AT
662 preserve_devices=1;
663 preserve_uid=1;
7b8356d0 664 }
c627d613
AT
665 break;
666
667 case OPT_SERVER:
668 am_server = 1;
669 break;
670
671 case OPT_SENDER:
672 if (!am_server) {
dc5ddbcc 673 usage(FERROR);
34ccb63e 674 exit_cleanup(1);
c627d613
AT
675 }
676 sender = 1;
677 break;
678
679 case 'r':
680 recurse = 1;
681 break;
682
6574b4f7
AT
683 case 'R':
684 relative_paths = 1;
685 break;
686
c627d613
AT
687 case 'e':
688 shell_cmd = optarg;
689 break;
690
691 case 'B':
692 block_size = atoi(optarg);
693 break;
694
950ab32d
AT
695 case 'T':
696 tmpdir = optarg;
697 break;
698
861c20b4
PM
699 case 'z':
700 do_compression = 1;
701 break;
702
c627d613 703 default:
861c20b4 704 /* fprintf(FERROR,"bad option -%c\n",opt); */
34ccb63e 705 exit_cleanup(1);
c627d613
AT
706 }
707 }
708
709 while (optind--) {
710 argc--;
711 argv++;
712 }
713
6b83141d
AT
714 signal(SIGCHLD,SIG_IGN);
715 signal(SIGINT,SIGNAL_CAST sig_int);
716 signal(SIGPIPE,SIGNAL_CAST sig_int);
717 signal(SIGHUP,SIGNAL_CAST sig_int);
718
c627d613
AT
719 if (dry_run)
720 verbose = MAX(verbose,1);
721
cbbe4892
AT
722#ifndef SUPPORT_LINKS
723 if (!am_server && preserve_links) {
724 fprintf(FERROR,"ERROR: symbolic links not supported\n");
725 exit_cleanup(1);
726 }
727#endif
728
c627d613 729 if (am_server) {
aae43eb3 730 setup_protocol(STDOUT_FILENO,STDIN_FILENO);
c627d613
AT
731
732 if (sender) {
733 recv_exclude_list(STDIN_FILENO);
734 if (cvs_exclude)
735 add_cvs_excludes();
736 do_server_sender(argc,argv);
737 } else {
738 do_server_recv(argc,argv);
739 }
34ccb63e 740 exit_cleanup(0);
c627d613
AT
741 }
742
743 if (argc < 2) {
dc5ddbcc 744 usage(FERROR);
34ccb63e 745 exit_cleanup(1);
c627d613
AT
746 }
747
748 p = strchr(argv[0],':');
749
750 if (p) {
751 sender = 0;
752 *p = 0;
753 shell_machine = argv[0];
754 shell_path = p+1;
755 argc--;
756 argv++;
757 } else {
758 sender = 1;
759
760 p = strchr(argv[argc-1],':');
761 if (!p) {
762 local_server = 1;
763 }
764
765 if (local_server) {
766 shell_machine = NULL;
767 shell_path = argv[argc-1];
768 } else {
769 *p = 0;
770 shell_machine = argv[argc-1];
771 shell_path = p+1;
772 }
773 argc--;
774 }
775
776 if (shell_machine) {
777 p = strchr(shell_machine,'@');
778 if (p) {
779 *p = 0;
780 shell_user = shell_machine;
781 shell_machine = p+1;
782 }
783 }
784
785 if (verbose > 3) {
dc5ddbcc 786 fprintf(FERROR,"cmd=%s machine=%s user=%s path=%s\n",
c627d613
AT
787 shell_cmd?shell_cmd:"",
788 shell_machine?shell_machine:"",
789 shell_user?shell_user:"",
790 shell_path?shell_path:"");
791 }
792
c627d613 793 if (!sender && argc != 1) {
dc5ddbcc 794 usage(FERROR);
34ccb63e 795 exit_cleanup(1);
c627d613
AT
796 }
797
798 pid = do_cmd(shell_cmd,shell_machine,shell_user,shell_path,&f_in,&f_out);
799
aae43eb3 800 setup_protocol(f_out,f_in);
4fe159a8 801
6dd1782c 802#if HAVE_SETLINEBUF
aa7ed201
AT
803 setlinebuf(FINFO);
804 setlinebuf(FERROR);
6dd1782c 805#endif
aa7ed201 806
c627d613 807 if (verbose > 3)
dc5ddbcc 808 fprintf(FERROR,"parent=%d child=%d sender=%d recurse=%d\n",
c627d613
AT
809 (int)getpid(),pid,sender,recurse);
810
811 if (sender) {
812 if (cvs_exclude)
813 add_cvs_excludes();
814 if (delete_mode)
815 send_exclude_list(f_out);
a06d19e3 816 flist = send_file_list(f_out,argc,argv);
c627d613 817 if (verbose > 3)
dc5ddbcc 818 fprintf(FERROR,"file list sent\n");
c627d613
AT
819 send_files(flist,f_out,f_in);
820 if (verbose > 3)
dc5ddbcc 821 fprintf(FERROR,"waiting on %d\n",pid);
c627d613
AT
822 waitpid(pid, &status, 0);
823 report(-1);
34ccb63e 824 exit_cleanup(status);
c627d613
AT
825 }
826
827 send_exclude_list(f_out);
828
829 flist = recv_file_list(f_in);
830 if (!flist || flist->count == 0) {
dc5ddbcc 831 fprintf(FERROR,"nothing to do\n");
34ccb63e 832 exit_cleanup(0);
c627d613
AT
833 }
834
835 local_name = get_local_name(flist,argv[0]);
836
dc5ddbcc 837 status2 = do_recv(f_in,f_out,flist,local_name);
c627d613
AT
838
839 report(f_in);
840
841 waitpid(pid, &status, 0);
842
843 return status | status2;
844}
82306bf6 845