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