- hard links
[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
406   count = read_int(f_in);
407   n = read_int(f_in);
408   remainder = read_int(f_in);
409
410   for (i=read_int(f_in); i != 0; i=read_int(f_in)) {
411     if (i > 0) {
412       if (verbose > 3)
413         fprintf(FERROR,"data recv %d at %d\n",i,(int)offset);
414
415       if (read_write(f_in,fd,i) != i) {
416         fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
417         exit_cleanup(1);
418       }
419       offset += i;
420     } else {
421       i = -(i+1);
422       offset2 = i*n;
423       len = n;
424       if (i == count-1 && remainder != 0)
425         len = remainder;
426
427       if (verbose > 3)
428         fprintf(FERROR,"chunk[%d] of size %d at %d offset=%d\n",
429                 i,len,(int)offset2,(int)offset);
430
431       if (write_sparse(fd,map_ptr(buf,offset2,len),len) != len) {
432         fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
433         exit_cleanup(1);
434       }
435       offset += len;
436     }
437   }
438
439   if (offset > 0 && sparse_end(fd) != 0) {
440     fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
441     exit_cleanup(1);
442   }
443 }
444
445
446 static void delete_one(struct file_struct *f)
447 {
448   if (!S_ISDIR(f->mode)) {
449     if (!dry_run && unlink(f->name) != 0) {
450       fprintf(FERROR,"unlink %s : %s\n",f->name,strerror(errno));
451     } else if (verbose) {
452       fprintf(FERROR,"deleting %s\n",f->name);
453     }
454   } else {    
455     if (!dry_run && rmdir(f->name) != 0) {
456       if (errno != ENOTEMPTY)
457         fprintf(FERROR,"rmdir %s : %s\n",f->name,strerror(errno));
458     } else if (verbose) {
459       fprintf(FERROR,"deleting directory %s\n",f->name);      
460     }
461   }
462 }
463
464
465 static void delete_files(struct file_list *flist)
466 {
467   struct file_list *local_file_list;
468   char *dot=".";
469   int i;
470
471   if (cvs_exclude)
472     add_cvs_excludes();
473
474   if (!(local_file_list = send_file_list(-1,recurse,1,&dot)))
475     return;
476
477   for (i=local_file_list->count;i>=0;i--) {
478     if (!local_file_list->files[i].name) continue;
479     if (-1 == flist_find(flist,&local_file_list->files[i])) {
480       delete_one(&local_file_list->files[i]);
481     }    
482   }
483 }
484
485 static char *cleanup_fname = NULL;
486
487 void exit_cleanup(int code)
488 {
489   if (cleanup_fname)
490     unlink(cleanup_fname);
491   exit(code);
492 }
493
494 void sig_int(void)
495 {
496   exit_cleanup(1);
497 }
498
499
500 int recv_files(int f_in,struct file_list *flist,char *local_name)
501 {  
502   int fd1,fd2;
503   struct stat st;
504   char *fname;
505   char fnametmp[MAXPATHLEN];
506   char *buf;
507   int i;
508   struct file_struct *file;
509
510   if (verbose > 2) {
511     fprintf(FERROR,"recv_files(%d) starting\n",flist->count);
512   }
513
514   if (recurse && delete_mode && !local_name && flist->count>0) {
515     delete_files(flist);
516   }
517
518   while (1) 
519     {
520       i = read_int(f_in);
521       if (i == -1) break;
522
523       file = &flist->files[i];
524       fname = file->name;
525
526       if (local_name)
527         fname = local_name;
528
529       if (dry_run) {
530         if (!am_server && verbose)
531           printf("%s\n",fname);
532         continue;
533       }
534
535       if (verbose > 2)
536         fprintf(FERROR,"recv_files(%s)\n",fname);
537
538       /* open the file */  
539       fd1 = open(fname,O_RDONLY);
540
541       if (fd1 != -1 && fstat(fd1,&st) != 0) {
542         fprintf(FERROR,"fstat %s : %s\n",fname,strerror(errno));
543         close(fd1);
544         return -1;
545       }
546
547       if (fd1 != -1 && !S_ISREG(st.st_mode)) {
548         fprintf(FERROR,"%s : not a regular file\n",fname);
549         close(fd1);
550         return -1;
551       }
552
553       if (fd1 != -1 && st.st_size > 0) {
554         buf = map_file(fd1,st.st_size);
555       } else {
556         buf = NULL;
557       }
558
559       if (verbose > 2)
560         fprintf(FERROR,"mapped %s of size %d\n",fname,(int)st.st_size);
561
562       /* open tmp file */
563       sprintf(fnametmp,"%s.XXXXXX",fname);
564       if (NULL == mktemp(fnametmp)) {
565         fprintf(FERROR,"mktemp %s failed\n",fnametmp);
566         return -1;
567       }
568       fd2 = open(fnametmp,O_WRONLY|O_CREAT,file->mode);
569       if (fd2 == -1) {
570         fprintf(FERROR,"open %s : %s\n",fnametmp,strerror(errno));
571         return -1;
572       }
573       
574       cleanup_fname = fnametmp;
575
576       if (!am_server && verbose)
577         printf("%s\n",fname);
578
579       /* recv file data */
580       receive_data(f_in,buf,fd2,fname);
581
582       if (fd1 != -1) {
583         unmap_file(buf,st.st_size);
584         close(fd1);
585       }
586       close(fd2);
587
588       if (verbose > 2)
589         fprintf(FERROR,"renaming %s to %s\n",fnametmp,fname);
590
591       if (make_backups) {
592         char fnamebak[MAXPATHLEN];
593         sprintf(fnamebak,"%s%s",fname,backup_suffix);
594         if (rename(fname,fnamebak) != 0 && errno != ENOENT) {
595           fprintf(FERROR,"rename %s %s : %s\n",fname,fnamebak,strerror(errno));
596           exit_cleanup(1);
597         }
598       }
599
600       /* move tmp file over real file */
601       if (rename(fnametmp,fname) != 0) {
602         fprintf(FERROR,"rename %s -> %s : %s\n",
603                 fnametmp,fname,strerror(errno));
604       }
605
606       cleanup_fname = NULL;
607
608       set_perms(fname,file,NULL,0);
609     }
610
611   if (verbose > 2)
612     fprintf(FERROR,"recv_files finished\n");
613   
614   return 0;
615 }
616
617
618
619 off_t send_files(struct file_list *flist,int f_out,int f_in)
620
621   int fd;
622   struct sum_struct *s;
623   char *buf;
624   struct stat st;
625   char fname[MAXPATHLEN];  
626   off_t total=0;
627   int i;
628   struct file_struct *file;
629
630   if (verbose > 2)
631     fprintf(FERROR,"send_files starting\n");
632
633   setup_nonblocking(f_in,f_out);
634
635   while (1) 
636     {
637       i = read_int(f_in);
638       if (i == -1) break;
639
640       file = &flist->files[i];
641
642       fname[0] = 0;
643       if (file->dir) {
644         strcpy(fname,file->dir);
645         strcat(fname,"/");
646       }
647       strcat(fname,file->name);
648
649       if (verbose > 2) 
650         fprintf(FERROR,"send_files(%d,%s)\n",i,fname);
651
652       if (dry_run) {    
653         if (!am_server && verbose)
654           printf("%s\n",fname);
655         write_int(f_out,i);
656         continue;
657       }
658
659       s = receive_sums(f_in);
660       if (!s) {
661         fprintf(FERROR,"receive_sums failed\n");
662         return -1;
663       }
664
665       fd = open(fname,O_RDONLY);
666       if (fd == -1) {
667         fprintf(FERROR,"send_files failed to open %s: %s\n",
668                 fname,strerror(errno));
669         continue;
670       }
671   
672       /* map the local file */
673       if (fstat(fd,&st) != 0) {
674         fprintf(FERROR,"fstat failed : %s\n",strerror(errno));
675         return -1;
676       }
677       
678       if (st.st_size > 0) {
679         buf = map_file(fd,st.st_size);
680       } else {
681         buf = NULL;
682       }
683
684       if (verbose > 2)
685         fprintf(FERROR,"send_files mapped %s of size %d\n",
686                 fname,(int)st.st_size);
687
688       write_int(f_out,i);
689
690       write_int(f_out,s->count);
691       write_int(f_out,s->n);
692       write_int(f_out,s->remainder);
693
694       if (verbose > 2)
695         fprintf(FERROR,"calling match_sums %s\n",fname);
696
697       if (!am_server && verbose)
698         printf("%s\n",fname);
699       
700       match_sums(f_out,s,buf,st.st_size);
701       write_flush(f_out);
702       
703       unmap_file(buf,st.st_size);
704       close(fd);
705
706       free_sums(s);
707
708       if (verbose > 2)
709         fprintf(FERROR,"sender finished %s\n",fname);
710
711       total += st.st_size;
712     }
713
714   if (verbose > 2)
715     fprintf(FERROR,"send files finished\n");
716
717   match_report();
718
719   write_int(f_out,-1);
720   write_flush(f_out);
721
722   return total;
723 }
724
725
726
727 void generate_files(int f,struct file_list *flist,char *local_name)
728 {
729   int i;
730
731   if (verbose > 2)
732     fprintf(FERROR,"generator starting pid=%d count=%d\n",
733             (int)getpid(),flist->count);
734
735   for (i = 0; i < flist->count; i++) {
736     struct file_struct *file = &flist->files[i];
737     if (!file->name) continue;
738     if (S_ISDIR(file->mode)) {
739       if (dry_run) continue;
740       if (mkdir(file->name,file->mode) != 0 &&
741           errno != EEXIST) {
742         fprintf(FERROR,"mkdir %s : %s\n",
743                 file->name,strerror(errno));
744       }
745       continue;
746     }
747     recv_generator(local_name?local_name:file->name,
748                    flist,i,f);
749   }
750   write_int(f,-1);
751   write_flush(f);
752   if (verbose > 2)
753     fprintf(FERROR,"generator wrote %d\n",write_total());
754 }
755
756