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