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