if rsync fails to update the group of a file but nothing else then
[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;
70d794dc 58
34ccb63e 59extern int csum_length;
c627d613
AT
60
61int am_server = 0;
3a6a366f 62static int sender;
c627d613
AT
63int recurse = 0;
64
65static void usage(FILE *f);
66
67static void report(int f)
68{
71c46176 69 int64 in,out,tsize;
c627d613
AT
70 time_t t = time(NULL);
71
72 if (!verbose) return;
73
74 if (am_server && sender) {
3a6a366f
AT
75 write_longint(f,read_total());
76 write_longint(f,write_total());
77 write_longint(f,total_size);
c627d613
AT
78 write_flush(f);
79 return;
80 }
81
82 if (sender) {
83 in = read_total();
84 out = write_total();
3a6a366f 85 tsize = total_size;
c627d613 86 } else {
3a6a366f
AT
87 in = read_longint(f);
88 out = read_longint(f);
89 tsize = read_longint(f);
c627d613
AT
90 }
91
0d0e2e93
AT
92#if HAVE_LONGLONG
93 printf("wrote %lld bytes read %lld bytes %g bytes/sec\n",
94 (long long)out,(long long)in,(in+out)/(0.5 + (t-starttime)));
95 printf("total size is %lld speedup is %g\n",
96 (long long)tsize,(1.0*tsize)/(in+out));
97#else
3a6a366f
AT
98 printf("wrote %ld bytes read %ld bytes %g bytes/sec\n",
99 (long)out,(long)in,(in+out)/(0.5 + (t-starttime)));
100 printf("total size is %ld speedup is %g\n",
101 (long)tsize,(1.0*tsize)/(in+out));
0d0e2e93 102#endif
c627d613
AT
103}
104
105
106static void server_options(char **args,int *argc)
107{
108 int ac = *argc;
109 static char argstr[50];
110 static char bsize[30];
111 int i, x;
112
113 args[ac++] = "--server";
114
115 if (!sender)
116 args[ac++] = "--sender";
117
118 x = 1;
119 argstr[0] = '-';
120 for (i=0;i<verbose;i++)
121 argstr[x++] = 'v';
122 if (make_backups)
123 argstr[x++] = 'b';
124 if (update_only)
125 argstr[x++] = 'u';
126 if (dry_run)
127 argstr[x++] = 'n';
128 if (preserve_links)
129 argstr[x++] = 'l';
82306bf6
AT
130 if (copy_links)
131 argstr[x++] = 'L';
132 if (whole_file)
133 argstr[x++] = 'W';
dc5ddbcc
AT
134 if (preserve_hard_links)
135 argstr[x++] = 'H';
c627d613
AT
136 if (preserve_uid)
137 argstr[x++] = 'o';
138 if (preserve_gid)
139 argstr[x++] = 'g';
140 if (preserve_devices)
141 argstr[x++] = 'D';
142 if (preserve_times)
143 argstr[x++] = 't';
144 if (preserve_perms)
145 argstr[x++] = 'p';
146 if (recurse)
147 argstr[x++] = 'r';
148 if (always_checksum)
149 argstr[x++] = 'c';
150 if (cvs_exclude)
151 argstr[x++] = 'C';
152 if (ignore_times)
153 argstr[x++] = 'I';
6574b4f7
AT
154 if (relative_paths)
155 argstr[x++] = 'R';
c627d613
AT
156 if (one_file_system)
157 argstr[x++] = 'x';
dc5ddbcc
AT
158 if (sparse_files)
159 argstr[x++] = 'S';
861c20b4
PM
160 if (do_compression)
161 argstr[x++] = 'z';
c627d613
AT
162 argstr[x] = 0;
163
164 if (x != 1) args[ac++] = argstr;
165
166 if (block_size != BLOCK_SIZE) {
167 sprintf(bsize,"-B%d",block_size);
168 args[ac++] = bsize;
169 }
43a481dc 170
dc7a9478
AT
171 if (strcmp(backup_suffix, BACKUP_SUFFIX)) {
172 args[ac++] = "--suffix";
173 args[ac++] = backup_suffix;
174 }
175
c627d613
AT
176 if (delete_mode)
177 args[ac++] = "--delete";
178
3cb6f5d6
AT
179 if (force_delete)
180 args[ac++] = "--force";
181
f6c34742
AT
182 if (numeric_ids)
183 args[ac++] = "--numeric-ids";
184
950ab32d
AT
185 if (tmpdir) {
186 args[ac++] = "--temp-dir";
187 args[ac++] = tmpdir;
188 }
189
c627d613
AT
190 *argc = ac;
191}
192
193
194
e3cd198f 195static int do_cmd(char *cmd,char *machine,char *user,char *path,int *f_in,int *f_out)
c627d613
AT
196{
197 char *args[100];
82306bf6 198 int i,argc=0, ret;
71c46176 199 char *tok,*dir=NULL;
c627d613
AT
200
201 if (!local_server) {
202 if (!cmd)
203 cmd = getenv(RSYNC_RSH_ENV);
204 if (!cmd)
205 cmd = RSYNC_RSH;
206 cmd = strdup(cmd);
207 if (!cmd)
208 goto oom;
209
210 for (tok=strtok(cmd," ");tok;tok=strtok(NULL," ")) {
211 args[argc++] = tok;
212 }
213
7b8356d0
AT
214#if HAVE_REMSH
215 /* remsh (on HPUX) takes the arguments the other way around */
216 args[argc++] = machine;
217 if (user) {
218 args[argc++] = "-l";
219 args[argc++] = user;
220 }
221#else
c627d613
AT
222 if (user) {
223 args[argc++] = "-l";
224 args[argc++] = user;
225 }
226 args[argc++] = machine;
7b8356d0 227#endif
c627d613
AT
228 }
229
230 args[argc++] = rsync_path;
231
232 server_options(args,&argc);
233
76076c4b
AT
234 args[argc++] = ".";
235
236 if (path && *path)
237 args[argc++] = path;
c627d613
AT
238
239 args[argc] = NULL;
240
241 if (verbose > 3) {
dc5ddbcc 242 fprintf(FERROR,"cmd=");
c627d613 243 for (i=0;i<argc;i++)
dc5ddbcc
AT
244 fprintf(FERROR,"%s ",args[i]);
245 fprintf(FERROR,"\n");
c627d613
AT
246 }
247
82306bf6
AT
248 ret = piped_child(args,f_in,f_out);
249 if (dir) free(dir);
250
251 return ret;
c627d613
AT
252
253oom:
254 out_of_memory("do_cmd");
255 return 0; /* not reached */
256}
257
258
259
260
261static char *get_local_name(struct file_list *flist,char *name)
262{
263 struct stat st;
264
265 if (stat(name,&st) == 0) {
266 if (S_ISDIR(st.st_mode)) {
267 if (chdir(name) != 0) {
6574b4f7 268 fprintf(FERROR,"chdir %s : %s (1)\n",name,strerror(errno));
34ccb63e 269 exit_cleanup(1);
c627d613
AT
270 }
271 return NULL;
272 }
273 if (flist->count > 1) {
dc5ddbcc 274 fprintf(FERROR,"ERROR: destination must be a directory when copying more than 1 file\n");
34ccb63e 275 exit_cleanup(1);
c627d613
AT
276 }
277 return name;
278 }
279
280 if (flist->count == 1)
281 return name;
282
283 if (!name)
284 return NULL;
285
7b8356d0 286 if (mkdir(name,0777 & ~orig_umask) != 0) {
6574b4f7 287 fprintf(FERROR,"mkdir %s : %s (1)\n",name,strerror(errno));
34ccb63e 288 exit_cleanup(1);
94481d91 289 } else {
dc5ddbcc 290 fprintf(FINFO,"created directory %s\n",name);
c627d613
AT
291 }
292
293 if (chdir(name) != 0) {
6574b4f7 294 fprintf(FERROR,"chdir %s : %s (2)\n",name,strerror(errno));
34ccb63e 295 exit_cleanup(1);
c627d613
AT
296 }
297
298 return NULL;
299}
300
301
302
303
304void do_server_sender(int argc,char *argv[])
305{
306 int i;
c627d613 307 struct file_list *flist;
76076c4b 308 char *dir = argv[0];
c627d613
AT
309
310 if (verbose > 2)
dc5ddbcc 311 fprintf(FERROR,"server_sender starting pid=%d\n",(int)getpid());
c627d613 312
6574b4f7 313 if (!relative_paths && chdir(dir) != 0) {
76076c4b
AT
314 fprintf(FERROR,"chdir %s: %s (3)\n",dir,strerror(errno));
315 exit_cleanup(1);
c627d613
AT
316 }
317 argc--;
318 argv++;
319
320 if (strcmp(dir,".")) {
76076c4b
AT
321 int l = strlen(dir);
322 if (strcmp(dir,"/") == 0)
323 l = 0;
324 for (i=0;i<argc;i++)
325 argv[i] += l+1;
c627d613
AT
326 }
327
328 if (argc == 0 && recurse) {
76076c4b
AT
329 argc=1;
330 argv--;
331 argv[0] = ".";
c627d613
AT
332 }
333
334
a06d19e3 335 flist = send_file_list(STDOUT_FILENO,argc,argv);
c627d613
AT
336 send_files(flist,STDOUT_FILENO,STDIN_FILENO);
337 report(STDOUT_FILENO);
34ccb63e 338 exit_cleanup(0);
c627d613
AT
339}
340
341
dc5ddbcc
AT
342static int do_recv(int f_in,int f_out,struct file_list *flist,char *local_name)
343{
344 int pid;
345 int status=0;
c6e7fcb4 346 int recv_pipe[2];
dc5ddbcc
AT
347
348 if (preserve_hard_links)
349 init_hard_links(flist);
350
c6e7fcb4
AT
351 if (pipe(recv_pipe) < 0) {
352 fprintf(FERROR,"pipe failed in do_recv\n");
353 exit(1);
354 }
355
356
3ba62a83 357 if ((pid=do_fork()) == 0) {
c6e7fcb4 358 recv_files(f_in,flist,local_name,recv_pipe[1]);
dc5ddbcc 359 if (verbose > 2)
3a6a366f 360 fprintf(FERROR,"receiver read %ld\n",(long)read_total());
dc5ddbcc
AT
361 exit_cleanup(0);
362 }
363
c6e7fcb4 364 generate_files(f_out,flist,local_name,recv_pipe[0]);
dc5ddbcc
AT
365
366 waitpid(pid, &status, 0);
367
368 return status;
369}
370
c627d613
AT
371
372void do_server_recv(int argc,char *argv[])
373{
dc5ddbcc 374 int status;
c627d613
AT
375 struct file_list *flist;
376 char *local_name=NULL;
76076c4b 377 char *dir = NULL;
c627d613
AT
378
379 if (verbose > 2)
dc5ddbcc 380 fprintf(FERROR,"server_recv(%d) starting pid=%d\n",argc,(int)getpid());
c627d613
AT
381
382 if (argc > 0) {
76076c4b
AT
383 dir = argv[0];
384 argc--;
385 argv++;
386 if (chdir(dir) != 0) {
387 fprintf(FERROR,"chdir %s : %s (4)\n",
388 dir,strerror(errno));
389 exit_cleanup(1);
390 }
c627d613
AT
391 }
392
393 if (delete_mode)
394 recv_exclude_list(STDIN_FILENO);
395
396 flist = recv_file_list(STDIN_FILENO);
397 if (!flist || flist->count == 0) {
dc5ddbcc 398 fprintf(FERROR,"nothing to do\n");
34ccb63e 399 exit_cleanup(1);
c627d613
AT
400 }
401
402 if (argc > 0) {
76076c4b
AT
403 if (strcmp(dir,".")) {
404 argv[0] += strlen(dir);
405 if (argv[0][0] == '/') argv[0]++;
406 }
407 local_name = get_local_name(flist,argv[0]);
c627d613
AT
408 }
409
dc5ddbcc 410 status = do_recv(STDIN_FILENO,STDOUT_FILENO,flist,local_name);
34ccb63e 411 exit_cleanup(status);
c627d613
AT
412}
413
414
415static void usage(FILE *f)
416{
417 fprintf(f,"rsync version %s Copyright Andrew Tridgell and Paul Mackerras\n\n",
418 VERSION);
419 fprintf(f,"Usage:\t%s [options] src user@host:dest\nOR",RSYNC_NAME);
420 fprintf(f,"\t%s [options] user@host:src dest\n\n",RSYNC_NAME);
421 fprintf(f,"Options:\n");
422 fprintf(f,"-v, --verbose increase verbosity\n");
423 fprintf(f,"-c, --checksum always checksum\n");
424 fprintf(f,"-a, --archive archive mode (same as -rlptDog)\n");
425 fprintf(f,"-r, --recursive recurse into directories\n");
6574b4f7 426 fprintf(f,"-R, --relative use relative path names\n");
c627d613
AT
427 fprintf(f,"-b, --backup make backups (default ~ extension)\n");
428 fprintf(f,"-u, --update update only (don't overwrite newer files)\n");
429 fprintf(f,"-l, --links preserve soft links\n");
82306bf6 430 fprintf(f,"-L, --copy-links treat soft links like regular files\n");
dc5ddbcc 431 fprintf(f,"-H, --hard-links preserve hard links\n");
c627d613
AT
432 fprintf(f,"-p, --perms preserve permissions\n");
433 fprintf(f,"-o, --owner preserve owner (root only)\n");
434 fprintf(f,"-g, --group preserve group\n");
435 fprintf(f,"-D, --devices preserve devices (root only)\n");
436 fprintf(f,"-t, --times preserve times\n");
dc5ddbcc 437 fprintf(f,"-S, --sparse handle sparse files efficiently\n");
c627d613 438 fprintf(f,"-n, --dry-run show what would have been transferred\n");
82306bf6 439 fprintf(f,"-W, --whole-file copy whole files, no incremental checks\n");
c627d613
AT
440 fprintf(f,"-x, --one-file-system don't cross filesystem boundaries\n");
441 fprintf(f,"-B, --block-size SIZE checksum blocking size\n");
442 fprintf(f,"-e, --rsh COMMAND specify rsh replacement\n");
443 fprintf(f," --rsync-path PATH specify path to rsync on the remote machine\n");
444 fprintf(f,"-C, --cvs-exclude auto ignore files in the same way CVS does\n");
445 fprintf(f," --delete delete files that don't exist on the sending side\n");
3cb6f5d6 446 fprintf(f," --force force deletion of directories even if not empty\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,
3cb6f5d6 462 OPT_EXCLUDE_FROM,OPT_DELETE,OPT_NUMERIC_IDS,OPT_RSYNC_PATH,OPT_FORCE};
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},
3cb6f5d6 471 {"force", 0, 0, OPT_FORCE},
f6c34742 472 {"numeric-ids", 0, 0, OPT_NUMERIC_IDS},
c627d613
AT
473 {"exclude", 1, 0, OPT_EXCLUDE},
474 {"exclude-from",1, 0, OPT_EXCLUDE_FROM},
475 {"rsync-path", 1, 0, OPT_RSYNC_PATH},
476 {"one-file-system",0, 0, 'x'},
477 {"ignore-times",0, 0, 'I'},
478 {"help", 0, 0, 'h'},
479 {"dry-run", 0, 0, 'n'},
dc5ddbcc 480 {"sparse", 0, 0, 'S'},
c627d613
AT
481 {"cvs-exclude", 0, 0, 'C'},
482 {"archive", 0, 0, 'a'},
483 {"checksum", 0, 0, 'c'},
484 {"backup", 0, 0, 'b'},
485 {"update", 0, 0, 'u'},
486 {"verbose", 0, 0, 'v'},
487 {"recursive", 0, 0, 'r'},
6574b4f7 488 {"relative", 0, 0, 'R'},
c627d613
AT
489 {"devices", 0, 0, 'D'},
490 {"perms", 0, 0, 'p'},
491 {"links", 0, 0, 'l'},
82306bf6
AT
492 {"copy-links", 0, 0, 'L'},
493 {"whole-file", 0, 0, 'W'},
dc5ddbcc 494 {"hard-links", 0, 0, 'H'},
c627d613
AT
495 {"owner", 0, 0, 'o'},
496 {"group", 0, 0, 'g'},
497 {"times", 0, 0, 't'},
498 {"rsh", 1, 0, 'e'},
499 {"suffix", 1, 0, OPT_SUFFIX},
500 {"block-size", 1, 0, 'B'},
950ab32d 501 {"temp-dir", 1, 0, 'T'},
861c20b4 502 {"compress", 0, 0, 'z'},
c627d613
AT
503 {0,0,0,0}};
504
82306bf6
AT
505RETSIGTYPE sigusr1_handler(int val) {
506 exit_cleanup(1);
507}
508
c627d613
AT
509int main(int argc,char *argv[])
510{
774ef68f 511 int pid, status = 0, status2 = 0;
c627d613
AT
512 int opt;
513 int option_index;
514 char *shell_cmd = NULL;
515 char *shell_machine = NULL;
516 char *shell_path = NULL;
517 char *shell_user = NULL;
518 char *p;
519 int f_in,f_out;
520 struct file_list *flist;
521 char *local_name = NULL;
522
82306bf6
AT
523 signal(SIGUSR1, sigusr1_handler);
524
c627d613 525 starttime = time(NULL);
7b8356d0
AT
526 am_root = (getuid() == 0);
527
528 /* we set a 0 umask so that correct file permissions can be
529 carried across */
3a6a366f 530 orig_umask = (int)umask(0);
c627d613
AT
531
532 while ((opt = getopt_long(argc, argv,
533 short_options, long_options, &option_index))
534 != -1) {
535 switch (opt)
536 {
537 case OPT_VERSION:
538 printf("rsync version %s protocol version %d\n",
539 VERSION,PROTOCOL_VERSION);
34ccb63e 540 exit_cleanup(0);
c627d613
AT
541
542 case OPT_SUFFIX:
543 backup_suffix = optarg;
544 break;
545
546 case OPT_RSYNC_PATH:
547 rsync_path = optarg;
548 break;
549
550 case 'I':
551 ignore_times = 1;
552 break;
553
554 case 'x':
555 one_file_system=1;
556 break;
557
558 case OPT_DELETE:
559 delete_mode = 1;
560 break;
561
3cb6f5d6
AT
562 case OPT_FORCE:
563 force_delete = 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