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