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