added hooks for compression in token.c
[rsync/rsync.git] / rsync.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 extern int csum_length;
23
24 extern int verbose;
25 extern int am_server;
26 extern int always_checksum;
27 extern time_t starttime;
28
29 extern char *backup_suffix;
30
31 extern int block_size;
32 extern int update_only;
33 extern int make_backups;
34 extern int preserve_links;
35 extern int preserve_hard_links;
36 extern int preserve_perms;
37 extern int preserve_devices;
38 extern int preserve_uid;
39 extern int preserve_gid;
40 extern int preserve_times;
41 extern int dry_run;
42 extern int ignore_times;
43 extern int recurse;
44 extern int delete_mode;
45 extern int cvs_exclude;
46
47 /*
48   free a sums struct
49   */
50 static void free_sums(struct sum_struct *s)
51 {
52   if (s->sums) free(s->sums);
53   free(s);
54 }
55
56
57
58 /*
59   send a sums struct down a fd
60   */
61 static void send_sums(struct sum_struct *s,int f_out)
62 {
63   int i;
64
65   /* tell the other guy how many we are going to be doing and how many
66      bytes there are in the last chunk */
67   write_int(f_out,s?s->count:0);
68   write_int(f_out,s?s->n:block_size);
69   write_int(f_out,s?s->remainder:0);
70   if (s)
71     for (i=0;i<s->count;i++) {
72       write_int(f_out,s->sums[i].sum1);
73       write_buf(f_out,s->sums[i].sum2,csum_length);
74     }
75   write_flush(f_out);
76 }
77
78
79 /*
80   generate a stream of signatures/checksums that describe a buffer
81
82   generate approximately one checksum every n bytes
83   */
84 static struct sum_struct *generate_sums(char *buf,off_t len,int n)
85 {
86   int i;
87   struct sum_struct *s;
88   int count;
89   int block_len = n;
90   int remainder = (len%block_len);
91   off_t offset = 0;
92
93   count = (len+(block_len-1))/block_len;
94
95   s = (struct sum_struct *)malloc(sizeof(*s));
96   if (!s) out_of_memory("generate_sums");
97
98   s->count = count;
99   s->remainder = remainder;
100   s->n = n;
101   s->flength = len;
102
103   if (count==0) {
104     s->sums = NULL;
105     return s;
106   }
107
108   if (verbose > 3)
109     fprintf(FERROR,"count=%d rem=%d n=%d flength=%d\n",
110             s->count,s->remainder,s->n,(int)s->flength);
111
112   s->sums = (struct sum_buf *)malloc(sizeof(s->sums[0])*s->count);
113   if (!s->sums) out_of_memory("generate_sums");
114   
115   for (i=0;i<count;i++) {
116     int n1 = MIN(len,n);
117     char *map = map_ptr(buf,offset,n1);
118
119     s->sums[i].sum1 = get_checksum1(map,n1);
120     get_checksum2(map,n1,s->sums[i].sum2);
121
122     s->sums[i].offset = offset;
123     s->sums[i].len = n1;
124     s->sums[i].i = i;
125
126     if (verbose > 3)
127       fprintf(FERROR,"chunk[%d] offset=%d len=%d sum1=%08x\n",
128               i,(int)s->sums[i].offset,s->sums[i].len,s->sums[i].sum1);
129
130     len -= n1;
131     offset += n1;
132   }
133
134   return s;
135 }
136
137
138 /*
139   receive the checksums for a buffer
140   */
141 static struct sum_struct *receive_sums(int f)
142 {
143   struct sum_struct *s;
144   int i;
145   off_t offset = 0;
146   int block_len;
147
148   s = (struct sum_struct *)malloc(sizeof(*s));
149   if (!s) out_of_memory("receive_sums");
150
151   s->count = read_int(f);
152   s->n = read_int(f);
153   s->remainder = read_int(f);  
154   s->sums = NULL;
155
156   if (verbose > 3)
157     fprintf(FERROR,"count=%d n=%d rem=%d\n",
158             s->count,s->n,s->remainder);
159
160   block_len = s->n;
161
162   if (s->count == 0) 
163     return(s);
164
165   s->sums = (struct sum_buf *)malloc(sizeof(s->sums[0])*s->count);
166   if (!s->sums) out_of_memory("receive_sums");
167
168   for (i=0;i<s->count;i++) {
169     s->sums[i].sum1 = read_int(f);
170     read_buf(f,s->sums[i].sum2,csum_length);
171
172     s->sums[i].offset = offset;
173     s->sums[i].i = i;
174
175     if (i == s->count-1 && s->remainder != 0) {
176       s->sums[i].len = s->remainder;
177     } else {
178       s->sums[i].len = s->n;
179     }
180     offset += s->sums[i].len;
181
182     if (verbose > 3)
183       fprintf(FERROR,"chunk[%d] len=%d offset=%d sum1=%08x\n",
184               i,s->sums[i].len,(int)s->sums[i].offset,s->sums[i].sum1);
185   }
186
187   s->flength = offset;
188
189   return s;
190 }
191
192
193 static void set_perms(char *fname,struct file_struct *file,struct stat *st,
194                       int report)
195 {
196   int updated = 0;
197   struct stat st2;
198
199   if (dry_run) return;
200
201   if (!st) {
202     if (stat(fname,&st2) != 0) {
203       fprintf(FERROR,"stat %s : %s\n",fname,strerror(errno));
204       return;
205     }
206     st = &st2;
207   }
208
209   if (preserve_times && !S_ISLNK(st->st_mode) &&
210       st->st_mtime != file->modtime) {
211     updated = 1;
212     if (set_modtime(fname,file->modtime) != 0) {
213       fprintf(FERROR,"failed to set times on %s : %s\n",
214               fname,strerror(errno));
215       return;
216     }
217   }
218
219 #ifdef HAVE_CHMOD
220   if (preserve_perms && !S_ISLNK(st->st_mode) &&
221       st->st_mode != file->mode) {
222     updated = 1;
223     if (chmod(fname,file->mode) != 0) {
224       fprintf(FERROR,"failed to set permissions on %s : %s\n",
225               fname,strerror(errno));
226       return;
227     }
228   }
229 #endif
230
231   if ((preserve_uid && st->st_uid != file->uid) || 
232       (preserve_gid && st->st_gid != file->gid)) {
233     updated = 1;
234     if (chown(fname,
235               preserve_uid?file->uid:-1,
236               preserve_gid?file->gid:-1) != 0) {
237       if (verbose>1 || preserve_uid)
238         fprintf(FERROR,"chown %s : %s\n",fname,strerror(errno));
239       return;
240     }
241   }
242     
243   if (verbose > 1 && report) {
244     if (updated)
245       fprintf(FINFO,"%s\n",fname);
246     else
247       fprintf(FINFO,"%s is uptodate\n",fname);
248   }
249 }
250
251
252 void recv_generator(char *fname,struct file_list *flist,int i,int f_out)
253 {  
254   int fd;
255   struct stat st;
256   char *buf;
257   struct sum_struct *s;
258   char sum[SUM_LENGTH];
259   int statret;
260   struct file_struct *file = &flist->files[i];
261
262   if (verbose > 2)
263     fprintf(FERROR,"recv_generator(%s)\n",fname);
264
265   statret = lstat(fname,&st);
266
267 #if SUPPORT_LINKS
268   if (preserve_links && S_ISLNK(file->mode)) {
269     char lnk[MAXPATHLEN];
270     int l;
271     if (statret == 0) {
272       l = readlink(fname,lnk,MAXPATHLEN-1);
273       if (l > 0) {
274         lnk[l] = 0;
275         if (strcmp(lnk,file->link) == 0) {
276           set_perms(fname,file,&st,1);
277           return;
278         }
279       }
280     }
281     if (!dry_run) unlink(fname);
282     if (!dry_run && symlink(file->link,fname) != 0) {
283       fprintf(FERROR,"link %s -> %s : %s\n",
284               fname,file->link,strerror(errno));
285     } else {
286       set_perms(fname,file,NULL,0);
287       if (verbose) 
288         fprintf(FINFO,"%s -> %s\n",
289                 fname,file->link);
290     }
291     return;
292   }
293 #endif
294
295 #ifdef HAVE_MKNOD
296   if (preserve_devices && IS_DEVICE(file->mode)) {
297     if (statret != 0 || 
298         st.st_mode != file->mode ||
299         st.st_rdev != file->rdev) {     
300       if (!dry_run) unlink(fname);
301       if (verbose > 2)
302         fprintf(FERROR,"mknod(%s,0%o,0x%x)\n",
303                 fname,(int)file->mode,(int)file->rdev);
304       if (!dry_run && 
305           mknod(fname,file->mode,file->rdev) != 0) {
306         fprintf(FERROR,"mknod %s : %s\n",fname,strerror(errno));
307       } else {
308         set_perms(fname,file,NULL,0);
309         if (verbose)
310           fprintf(FINFO,"%s\n",fname);
311       }
312     } else {
313       set_perms(fname,file,&st,1);
314     }
315     return;
316   }
317 #endif
318
319   if (preserve_hard_links && check_hard_link(file)) {
320     if (verbose > 1)
321       fprintf(FINFO,"%s is a hard link\n",file->name);
322     return;
323   }
324
325   if (!S_ISREG(file->mode)) {
326     fprintf(FERROR,"skipping non-regular file %s\n",fname);
327     return;
328   }
329
330   if (statret == -1) {
331     if (errno == ENOENT) {
332       write_int(f_out,i);
333       if (!dry_run) send_sums(NULL,f_out);
334     } else {
335       if (verbose > 1)
336         fprintf(FERROR,"recv_generator failed to open %s\n",fname);
337     }
338     return;
339   }
340
341   if (!S_ISREG(st.st_mode)) {
342     fprintf(FERROR,"%s : not a regular file\n",fname);
343     return;
344   }
345
346   if (update_only && st.st_mtime >= file->modtime) {
347     if (verbose > 1)
348       fprintf(FERROR,"%s is newer\n",fname);
349     return;
350   }
351
352   if (always_checksum && S_ISREG(st.st_mode)) {
353     file_checksum(fname,sum,st.st_size);
354   }
355
356   if (st.st_size == file->length &&
357       ((!ignore_times && st.st_mtime == file->modtime) ||
358        (always_checksum && S_ISREG(st.st_mode) &&         
359         memcmp(sum,file->sum,csum_length) == 0))) {
360     set_perms(fname,file,&st,1);
361     return;
362   }
363
364   if (dry_run) {
365     write_int(f_out,i);
366     return;
367   }
368
369   /* open the file */  
370   fd = open(fname,O_RDONLY);
371
372   if (fd == -1) {
373     fprintf(FERROR,"failed to open %s : %s\n",fname,strerror(errno));
374     return;
375   }
376
377   if (st.st_size > 0) {
378     buf = map_file(fd,st.st_size);
379   } else {
380     buf = NULL;
381   }
382
383   if (verbose > 3)
384     fprintf(FERROR,"mapped %s of size %d\n",fname,(int)st.st_size);
385
386   s = generate_sums(buf,st.st_size,block_size);
387
388   write_int(f_out,i);
389   send_sums(s,f_out);
390   write_flush(f_out);
391
392   close(fd);
393   unmap_file(buf,st.st_size);
394
395   free_sums(s);
396 }
397
398
399
400 static void receive_data(int f_in,char *buf,int fd,char *fname)
401 {
402   int i,n,remainder,len,count;
403   off_t offset = 0;
404   off_t offset2;
405   char *data;
406
407   count = read_int(f_in);
408   n = read_int(f_in);
409   remainder = read_int(f_in);
410
411   for (i=recv_token(f_in,&data); i != 0; i=recv_token(f_in,&data)) {
412     if (i > 0) {
413       if (verbose > 3)
414         fprintf(FERROR,"data recv %d at %d\n",i,(int)offset);
415
416       if (write_sparse(fd,data,i) != i) {
417         fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
418         exit_cleanup(1);
419       }
420       offset += i;
421     } else {
422       i = -(i+1);
423       offset2 = i*n;
424       len = n;
425       if (i == count-1 && remainder != 0)
426         len = remainder;
427
428       if (verbose > 3)
429         fprintf(FERROR,"chunk[%d] of size %d at %d offset=%d\n",
430                 i,len,(int)offset2,(int)offset);
431
432       if (write_sparse(fd,map_ptr(buf,offset2,len),len) != len) {
433         fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
434         exit_cleanup(1);
435       }
436       offset += len;
437     }
438   }
439
440   if (offset > 0 && sparse_end(fd) != 0) {
441     fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
442     exit_cleanup(1);
443   }
444 }
445
446
447 static void delete_one(struct file_struct *f)
448 {
449   if (!S_ISDIR(f->mode)) {
450     if (!dry_run && unlink(f->name) != 0) {
451       fprintf(FERROR,"unlink %s : %s\n",f->name,strerror(errno));
452     } else if (verbose) {
453       fprintf(FERROR,"deleting %s\n",f->name);
454     }
455   } else {    
456     if (!dry_run && rmdir(f->name) != 0) {
457       if (errno != ENOTEMPTY)
458         fprintf(FERROR,"rmdir %s : %s\n",f->name,strerror(errno));
459     } else if (verbose) {
460       fprintf(FERROR,"deleting directory %s\n",f->name);      
461     }
462   }
463 }
464
465
466 static void delete_files(struct file_list *flist)
467 {
468   struct file_list *local_file_list;
469   char *dot=".";
470   int i;
471
472   if (cvs_exclude)
473     add_cvs_excludes();
474
475   if (!(local_file_list = send_file_list(-1,recurse,1,&dot)))
476     return;
477
478   for (i=local_file_list->count;i>=0;i--) {
479     if (!local_file_list->files[i].name) continue;
480     if (-1 == flist_find(flist,&local_file_list->files[i])) {
481       delete_one(&local_file_list->files[i]);
482     }    
483   }
484 }
485
486 static char *cleanup_fname = NULL;
487
488 void exit_cleanup(int code)
489 {
490   if (cleanup_fname)
491     unlink(cleanup_fname);
492   exit(code);
493 }
494
495 void sig_int(void)
496 {
497   exit_cleanup(1);
498 }
499
500
501 int recv_files(int f_in,struct file_list *flist,char *local_name)
502 {  
503   int fd1,fd2;
504   struct stat st;
505   char *fname;
506   char fnametmp[MAXPATHLEN];
507   char *buf;
508   int i;
509   struct file_struct *file;
510
511   if (verbose > 2) {
512     fprintf(FERROR,"recv_files(%d) starting\n",flist->count);
513   }
514
515   if (recurse && delete_mode && !local_name && flist->count>0) {
516     delete_files(flist);
517   }
518
519   while (1) 
520     {
521       i = read_int(f_in);
522       if (i == -1) break;
523
524       file = &flist->files[i];
525       fname = file->name;
526
527       if (local_name)
528         fname = local_name;
529
530       if (dry_run) {
531         if (!am_server && verbose)
532           printf("%s\n",fname);
533         continue;
534       }
535
536       if (verbose > 2)
537         fprintf(FERROR,"recv_files(%s)\n",fname);
538
539       /* open the file */  
540       fd1 = open(fname,O_RDONLY);
541
542       if (fd1 != -1 && fstat(fd1,&st) != 0) {
543         fprintf(FERROR,"fstat %s : %s\n",fname,strerror(errno));
544         close(fd1);
545         return -1;
546       }
547
548       if (fd1 != -1 && !S_ISREG(st.st_mode)) {
549         fprintf(FERROR,"%s : not a regular file\n",fname);
550         close(fd1);
551         return -1;
552       }
553
554       if (fd1 != -1 && st.st_size > 0) {
555         buf = map_file(fd1,st.st_size);
556       } else {
557         buf = NULL;
558       }
559
560       if (verbose > 2)
561         fprintf(FERROR,"mapped %s of size %d\n",fname,(int)st.st_size);
562
563       /* open tmp file */
564       sprintf(fnametmp,"%s.XXXXXX",fname);
565       if (NULL == mktemp(fnametmp)) {
566         fprintf(FERROR,"mktemp %s failed\n",fnametmp);
567         return -1;
568       }
569       fd2 = open(fnametmp,O_WRONLY|O_CREAT,file->mode);
570       if (fd2 == -1) {
571         fprintf(FERROR,"open %s : %s\n",fnametmp,strerror(errno));
572         return -1;
573       }
574       
575       cleanup_fname = fnametmp;
576
577       if (!am_server && verbose)
578         printf("%s\n",fname);
579
580       /* recv file data */
581       receive_data(f_in,buf,fd2,fname);
582
583       if (fd1 != -1) {
584         unmap_file(buf,st.st_size);
585         close(fd1);
586       }
587       close(fd2);
588
589       if (verbose > 2)
590         fprintf(FERROR,"renaming %s to %s\n",fnametmp,fname);
591
592       if (make_backups) {
593         char fnamebak[MAXPATHLEN];
594         sprintf(fnamebak,"%s%s",fname,backup_suffix);
595         if (rename(fname,fnamebak) != 0 && errno != ENOENT) {
596           fprintf(FERROR,"rename %s %s : %s\n",fname,fnamebak,strerror(errno));
597           exit_cleanup(1);
598         }
599       }
600
601       /* move tmp file over real file */
602       if (rename(fnametmp,fname) != 0) {
603         fprintf(FERROR,"rename %s -> %s : %s\n",
604                 fnametmp,fname,strerror(errno));
605       }
606
607       cleanup_fname = NULL;
608
609       set_perms(fname,file,NULL,0);
610     }
611
612   if (verbose > 2)
613     fprintf(FERROR,"recv_files finished\n");
614   
615   return 0;
616 }
617
618
619
620 off_t send_files(struct file_list *flist,int f_out,int f_in)
621
622   int fd;
623   struct sum_struct *s;
624   char *buf;
625   struct stat st;
626   char fname[MAXPATHLEN];  
627   off_t total=0;
628   int i;
629   struct file_struct *file;
630
631   if (verbose > 2)
632     fprintf(FERROR,"send_files starting\n");
633
634   setup_nonblocking(f_in,f_out);
635
636   while (1) 
637     {
638       i = read_int(f_in);
639       if (i == -1) break;
640
641       file = &flist->files[i];
642
643       fname[0] = 0;
644       if (file->dir) {
645         strcpy(fname,file->dir);
646         strcat(fname,"/");
647       }
648       strcat(fname,file->name);
649
650       if (verbose > 2) 
651         fprintf(FERROR,"send_files(%d,%s)\n",i,fname);
652
653       if (dry_run) {    
654         if (!am_server && verbose)
655           printf("%s\n",fname);
656         write_int(f_out,i);
657         continue;
658       }
659
660       s = receive_sums(f_in);
661       if (!s) {
662         fprintf(FERROR,"receive_sums failed\n");
663         return -1;
664       }
665
666       fd = open(fname,O_RDONLY);
667       if (fd == -1) {
668         fprintf(FERROR,"send_files failed to open %s: %s\n",
669                 fname,strerror(errno));
670         continue;
671       }
672   
673       /* map the local file */
674       if (fstat(fd,&st) != 0) {
675         fprintf(FERROR,"fstat failed : %s\n",strerror(errno));
676         return -1;
677       }
678       
679       if (st.st_size > 0) {
680         buf = map_file(fd,st.st_size);
681       } else {
682         buf = NULL;
683       }
684
685       if (verbose > 2)
686         fprintf(FERROR,"send_files mapped %s of size %d\n",
687                 fname,(int)st.st_size);
688
689       write_int(f_out,i);
690
691       write_int(f_out,s->count);
692       write_int(f_out,s->n);
693       write_int(f_out,s->remainder);
694
695       if (verbose > 2)
696         fprintf(FERROR,"calling match_sums %s\n",fname);
697
698       if (!am_server && verbose)
699         printf("%s\n",fname);
700       
701       match_sums(f_out,s,buf,st.st_size);
702       write_flush(f_out);
703       
704       unmap_file(buf,st.st_size);
705       close(fd);
706
707       free_sums(s);
708
709       if (verbose > 2)
710         fprintf(FERROR,"sender finished %s\n",fname);
711
712       total += st.st_size;
713     }
714
715   if (verbose > 2)
716     fprintf(FERROR,"send files finished\n");
717
718   match_report();
719
720   write_int(f_out,-1);
721   write_flush(f_out);
722
723   return total;
724 }
725
726
727
728 void generate_files(int f,struct file_list *flist,char *local_name)
729 {
730   int i;
731
732   if (verbose > 2)
733     fprintf(FERROR,"generator starting pid=%d count=%d\n",
734             (int)getpid(),flist->count);
735
736   for (i = 0; i < flist->count; i++) {
737     struct file_struct *file = &flist->files[i];
738     if (!file->name) continue;
739     if (S_ISDIR(file->mode)) {
740       if (dry_run) continue;
741       if (mkdir(file->name,file->mode) != 0 &&
742           errno != EEXIST) {
743         fprintf(FERROR,"mkdir %s : %s\n",
744                 file->name,strerror(errno));
745       }
746       continue;
747     }
748     recv_generator(local_name?local_name:file->name,
749                    flist,i,f);
750   }
751   write_int(f,-1);
752   write_flush(f);
753   if (verbose > 2)
754     fprintf(FERROR,"generator wrote %d\n",write_total());
755 }
756
757