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