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