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