don't adapt the block size to above half the chunk size
[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 int remote_version;
30
31 extern char *backup_suffix;
32 extern char *tmpdir;
33
34 extern int whole_file;
35 extern int block_size;
36 extern int update_only;
37 extern int make_backups;
38 extern int preserve_links;
39 extern int preserve_hard_links;
40 extern int preserve_perms;
41 extern int preserve_devices;
42 extern int preserve_uid;
43 extern int preserve_gid;
44 extern int preserve_times;
45 extern int dry_run;
46 extern int ignore_times;
47 extern int recurse;
48 extern int delete_mode;
49 extern int cvs_exclude;
50 extern int am_root;
51 extern int relative_paths;
52
53 /*
54   free a sums struct
55   */
56 static void free_sums(struct sum_struct *s)
57 {
58   if (s->sums) free(s->sums);
59   free(s);
60 }
61
62
63
64 /*
65   send a sums struct down a fd
66   */
67 static void send_sums(struct sum_struct *s,int f_out)
68 {
69   int i;
70
71   /* tell the other guy how many we are going to be doing and how many
72      bytes there are in the last chunk */
73   write_int(f_out,s?s->count:0);
74   write_int(f_out,s?s->n:block_size);
75   write_int(f_out,s?s->remainder:0);
76   if (s)
77     for (i=0;i<s->count;i++) {
78       write_int(f_out,s->sums[i].sum1);
79       write_buf(f_out,s->sums[i].sum2,csum_length);
80     }
81   write_flush(f_out);
82 }
83
84
85 /*
86   generate a stream of signatures/checksums that describe a buffer
87
88   generate approximately one checksum every n bytes
89   */
90 static struct sum_struct *generate_sums(struct map_struct *buf,off_t len,int n)
91 {
92   int i;
93   struct sum_struct *s;
94   int count;
95   int block_len = n;
96   int remainder = (len%block_len);
97   off_t offset = 0;
98
99   count = (len+(block_len-1))/block_len;
100
101   s = (struct sum_struct *)malloc(sizeof(*s));
102   if (!s) out_of_memory("generate_sums");
103
104   s->count = count;
105   s->remainder = remainder;
106   s->n = n;
107   s->flength = len;
108
109   if (count==0) {
110     s->sums = NULL;
111     return s;
112   }
113
114   if (verbose > 3)
115     fprintf(FERROR,"count=%d rem=%d n=%d flength=%d\n",
116             s->count,s->remainder,s->n,(int)s->flength);
117
118   s->sums = (struct sum_buf *)malloc(sizeof(s->sums[0])*s->count);
119   if (!s->sums) out_of_memory("generate_sums");
120   
121   for (i=0;i<count;i++) {
122     int n1 = MIN(len,n);
123     char *map = map_ptr(buf,offset,n1);
124
125     s->sums[i].sum1 = get_checksum1(map,n1);
126     get_checksum2(map,n1,s->sums[i].sum2);
127
128     s->sums[i].offset = offset;
129     s->sums[i].len = n1;
130     s->sums[i].i = i;
131
132     if (verbose > 3)
133       fprintf(FERROR,"chunk[%d] offset=%d len=%d sum1=%08x\n",
134               i,(int)s->sums[i].offset,s->sums[i].len,s->sums[i].sum1);
135
136     len -= n1;
137     offset += n1;
138   }
139
140   return s;
141 }
142
143
144 /*
145   receive the checksums for a buffer
146   */
147 static struct sum_struct *receive_sums(int f)
148 {
149   struct sum_struct *s;
150   int i;
151   off_t offset = 0;
152
153   s = (struct sum_struct *)malloc(sizeof(*s));
154   if (!s) out_of_memory("receive_sums");
155
156   s->count = read_int(f);
157   s->n = read_int(f);
158   s->remainder = read_int(f);  
159   s->sums = NULL;
160
161   if (verbose > 3)
162     fprintf(FERROR,"count=%d n=%d rem=%d\n",
163             s->count,s->n,s->remainder);
164
165   if (s->count == 0) 
166     return(s);
167
168   s->sums = (struct sum_buf *)malloc(sizeof(s->sums[0])*s->count);
169   if (!s->sums) out_of_memory("receive_sums");
170
171   for (i=0;i<s->count;i++) {
172     s->sums[i].sum1 = read_int(f);
173     read_buf(f,s->sums[i].sum2,csum_length);
174
175     s->sums[i].offset = offset;
176     s->sums[i].i = i;
177
178     if (i == s->count-1 && s->remainder != 0) {
179       s->sums[i].len = s->remainder;
180     } else {
181       s->sums[i].len = s->n;
182     }
183     offset += s->sums[i].len;
184
185     if (verbose > 3)
186       fprintf(FERROR,"chunk[%d] len=%d offset=%d sum1=%08x\n",
187               i,s->sums[i].len,(int)s->sums[i].offset,s->sums[i].sum1);
188   }
189
190   s->flength = offset;
191
192   return s;
193 }
194
195
196 static int set_perms(char *fname,struct file_struct *file,struct stat *st,
197                      int report)
198 {
199   int updated = 0;
200   struct stat st2;
201
202   if (dry_run) return 0;
203
204   if (!st) {
205     if (link_stat(fname,&st2) != 0) {
206       fprintf(FERROR,"stat %s : %s\n",fname,strerror(errno));
207       return 0;
208     }
209     st = &st2;
210   }
211
212   if (preserve_times && !S_ISLNK(st->st_mode) &&
213       st->st_mtime != file->modtime) {
214     updated = 1;
215     if (set_modtime(fname,file->modtime) != 0) {
216       fprintf(FERROR,"failed to set times on %s : %s\n",
217               fname,strerror(errno));
218       return 0;
219     }
220   }
221
222 #ifdef HAVE_CHMOD
223   if (preserve_perms && !S_ISLNK(st->st_mode) &&
224       st->st_mode != file->mode) {
225     updated = 1;
226     if (chmod(fname,file->mode) != 0) {
227       fprintf(FERROR,"failed to set permissions on %s : %s\n",
228               fname,strerror(errno));
229       return 0;
230     }
231   }
232 #endif
233
234   if ((am_root && preserve_uid && st->st_uid != file->uid) || 
235       (preserve_gid && st->st_gid != file->gid)) {
236     updated = 1;
237     if (lchown(fname,
238                (am_root&&preserve_uid)?file->uid:-1,
239                preserve_gid?file->gid:-1) != 0) {
240       if (verbose>1 || preserve_uid)
241         fprintf(FERROR,"chown %s : %s\n",fname,strerror(errno));
242       return updated;
243     }
244   }
245     
246   if (verbose > 1 && report) {
247     if (updated)
248       fprintf(FINFO,"%s\n",fname);
249     else
250       fprintf(FINFO,"%s is uptodate\n",fname);
251   }
252   return updated;
253 }
254
255
256 /* choose whether to skip a particular file */
257 static int skip_file(char *fname,
258                      struct file_struct *file, struct stat *st)
259 {
260         if (st->st_size != file->length) {
261                 return 0;
262         }
263         
264         /* if always checksum is set then we use the checksum instead 
265            of the file time to determine whether to sync */
266         if (always_checksum && S_ISREG(st->st_mode)) {
267                 char sum[MD4_SUM_LENGTH];
268                 file_checksum(fname,sum,st->st_size);
269                 return (memcmp(sum,file->sum,csum_length) == 0);
270         }
271
272         if (ignore_times) {
273                 return 0;
274         }
275
276         return (st->st_mtime == file->modtime);
277 }
278
279
280 /* use a larger block size for really big files */
281 int adapt_block_size(struct file_struct *file, int bsize)
282 {
283         int ret = file->length / (10000); /* rough heuristic */
284         ret = ret & ~15; /* multiple of 16 */
285         if (ret < bsize) ret = bsize;
286         if (ret > CHUNK_SIZE/2) ret = CHUNK_SIZE/2;
287         return ret;
288 }
289
290 void recv_generator(char *fname,struct file_list *flist,int i,int f_out)
291 {  
292   int fd;
293   struct stat st;
294   struct map_struct *buf;
295   struct sum_struct *s;
296   int statret;
297   struct file_struct *file = flist->files[i];
298
299   if (verbose > 2)
300     fprintf(FERROR,"recv_generator(%s,%d)\n",fname,i);
301
302   statret = link_stat(fname,&st);
303
304   if (S_ISDIR(file->mode)) {
305     if (dry_run) return;
306     if (statret == 0 && !S_ISDIR(st.st_mode)) {
307       if (unlink(fname) != 0) {
308         fprintf(FERROR,"unlink %s : %s\n",fname,strerror(errno));
309         return;
310       }
311       statret = -1;
312     }
313     if (statret != 0 && mkdir(fname,file->mode) != 0 && errno != EEXIST) {
314             if (!(relative_paths && errno==ENOENT && 
315                   create_directory_path(fname)==0 && 
316                   mkdir(fname,file->mode)==0)) {
317                     fprintf(FERROR,"mkdir %s : %s (2)\n",
318                             fname,strerror(errno));
319             }
320     }
321     if (set_perms(fname,file,NULL,0) && verbose) 
322       fprintf(FINFO,"%s/\n",fname);
323     return;
324   }
325
326   if (preserve_links && S_ISLNK(file->mode)) {
327 #if SUPPORT_LINKS
328     char lnk[MAXPATHLEN];
329     int l;
330     if (statret == 0) {
331       l = readlink(fname,lnk,MAXPATHLEN-1);
332       if (l > 0) {
333         lnk[l] = 0;
334         if (strcmp(lnk,file->link) == 0) {
335           set_perms(fname,file,&st,1);
336           return;
337         }
338       }
339     }
340     if (!dry_run) unlink(fname);
341     if (!dry_run && symlink(file->link,fname) != 0) {
342       fprintf(FERROR,"link %s -> %s : %s\n",
343               fname,file->link,strerror(errno));
344     } else {
345       set_perms(fname,file,NULL,0);
346       if (verbose) 
347         fprintf(FINFO,"%s -> %s\n",
348                 fname,file->link);
349     }
350 #endif
351     return;
352   }
353
354 #ifdef HAVE_MKNOD
355   if (am_root && preserve_devices && IS_DEVICE(file->mode)) {
356     if (statret != 0 || 
357         st.st_mode != file->mode ||
358         st.st_rdev != file->rdev) {     
359       if (!dry_run) unlink(fname);
360       if (verbose > 2)
361         fprintf(FERROR,"mknod(%s,0%o,0x%x)\n",
362                 fname,(int)file->mode,(int)file->rdev);
363       if (!dry_run && 
364           mknod(fname,file->mode,file->rdev) != 0) {
365         fprintf(FERROR,"mknod %s : %s\n",fname,strerror(errno));
366       } else {
367         set_perms(fname,file,NULL,0);
368         if (verbose)
369           fprintf(FINFO,"%s\n",fname);
370       }
371     } else {
372       set_perms(fname,file,&st,1);
373     }
374     return;
375   }
376 #endif
377
378   if (preserve_hard_links && check_hard_link(file)) {
379     if (verbose > 1)
380       fprintf(FINFO,"%s is a hard link\n",f_name(file));
381     return;
382   }
383
384   if (!S_ISREG(file->mode)) {
385     fprintf(FERROR,"skipping non-regular file %s\n",fname);
386     return;
387   }
388
389   if (statret == -1) {
390     if (errno == ENOENT) {
391       write_int(f_out,i);
392       if (!dry_run) send_sums(NULL,f_out);
393     } else {
394       if (verbose > 1)
395         fprintf(FERROR,"recv_generator failed to open %s\n",fname);
396     }
397     return;
398   }
399
400   if (!S_ISREG(st.st_mode)) {
401     /* its not a regular file on the receiving end, but it is on the
402        sending end. If its a directory then skip it (too dangerous to
403        do a recursive deletion??) otherwise try to unlink it */
404     if (S_ISDIR(st.st_mode)) {
405       fprintf(FERROR,"ERROR: %s is a directory\n",fname);
406       return;
407     }
408     if (unlink(fname) != 0) {
409       fprintf(FERROR,"%s : not a regular file (generator)\n",fname);
410       return;
411     }
412
413     /* now pretend the file didn't exist */
414     write_int(f_out,i);
415     if (!dry_run) send_sums(NULL,f_out);    
416     return;
417   }
418
419   if (update_only && st.st_mtime > file->modtime) {
420     if (verbose > 1)
421       fprintf(FERROR,"%s is newer\n",fname);
422     return;
423   }
424
425   if (skip_file(fname, file, &st)) {
426     set_perms(fname,file,&st,1);
427     return;
428   }
429
430   if (dry_run) {
431     write_int(f_out,i);
432     return;
433   }
434
435   if (whole_file) {
436     write_int(f_out,i);
437     send_sums(NULL,f_out);    
438     return;
439   }
440
441   /* open the file */  
442   fd = open(fname,O_RDONLY);
443
444   if (fd == -1) {
445     fprintf(FERROR,"failed to open %s : %s\n",fname,strerror(errno));
446     fprintf(FERROR,"skipping %s\n",fname);
447     return;
448   }
449
450   if (st.st_size > 0) {
451     buf = map_file(fd,st.st_size);
452   } else {
453     buf = NULL;
454   }
455
456   if (verbose > 3)
457     fprintf(FERROR,"gen mapped %s of size %d\n",fname,(int)st.st_size);
458
459   s = generate_sums(buf,st.st_size,adapt_block_size(file, block_size));
460
461   if (verbose > 2)
462     fprintf(FERROR,"sending sums for %d\n",i);
463
464   write_int(f_out,i);
465   send_sums(s,f_out);
466   write_flush(f_out);
467
468   close(fd);
469   if (buf) unmap_file(buf);
470
471   free_sums(s);
472 }
473
474
475
476 static int receive_data(int f_in,struct map_struct *buf,int fd,char *fname)
477 {
478   int i,n,remainder,len,count;
479   off_t offset = 0;
480   off_t offset2;
481   char *data;
482   static char file_sum1[MD4_SUM_LENGTH];
483   static char file_sum2[MD4_SUM_LENGTH];
484   char *map=NULL;
485
486   count = read_int(f_in);
487   n = read_int(f_in);
488   remainder = read_int(f_in);
489
490   sum_init();
491
492   for (i=recv_token(f_in,&data); i != 0; i=recv_token(f_in,&data)) {
493     if (i > 0) {
494       if (verbose > 3)
495         fprintf(FERROR,"data recv %d at %d\n",i,(int)offset);
496
497       sum_update(data,i);
498
499       if (fd != -1 && write_sparse(fd,data,i) != i) {
500         fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
501         exit_cleanup(1);
502       }
503       offset += i;
504     } else {
505       i = -(i+1);
506       offset2 = i*n;
507       len = n;
508       if (i == count-1 && remainder != 0)
509         len = remainder;
510
511       if (verbose > 3)
512         fprintf(FERROR,"chunk[%d] of size %d at %d offset=%d\n",
513                 i,len,(int)offset2,(int)offset);
514
515       map = map_ptr(buf,offset2,len);
516
517       see_token(map, len);
518       sum_update(map,len);
519
520       if (fd != -1 && write_sparse(fd,map,len) != len) {
521         fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
522         exit_cleanup(1);
523       }
524       offset += len;
525     }
526   }
527
528   if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
529     fprintf(FERROR,"write failed on %s : %s\n",fname,strerror(errno));
530     exit_cleanup(1);
531   }
532
533   sum_end(file_sum1);
534
535   if (remote_version >= 14) {
536     read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
537     if (verbose > 2)
538       fprintf(FERROR,"got file_sum\n");
539     if (fd != -1 && memcmp(file_sum1,file_sum2,MD4_SUM_LENGTH) != 0)
540       return 0;
541   }
542   return 1;
543 }
544
545
546 static void delete_one(struct file_struct *f)
547 {
548   if (!S_ISDIR(f->mode)) {
549     if (!dry_run && unlink(f_name(f)) != 0) {
550       fprintf(FERROR,"unlink %s : %s\n",f_name(f),strerror(errno));
551     } else if (verbose) {
552       fprintf(FERROR,"deleting %s\n",f_name(f));
553     }
554   } else {    
555     if (!dry_run && rmdir(f_name(f)) != 0) {
556       if (errno != ENOTEMPTY)
557         fprintf(FERROR,"rmdir %s : %s\n",f_name(f),strerror(errno));
558     } else if (verbose) {
559       fprintf(FERROR,"deleting directory %s\n",f_name(f));      
560     }
561   }
562 }
563
564
565 /* yuck! This function wouldn't have been necessary if I had the sorting
566    algorithm right. Unfortunately fixing the sorting algorithm would introduce
567    a backward incompatibility as file list indexes are sent over the link.
568
569    The aim is to see if a directory has already had the deletion algorithm applied
570    to it (due to recursion), and if so to skip it. The bisection is to 
571    prevent this being an n^2 algorithm */
572 static int delete_already_done(struct file_list *flist,int j)
573 {
574         int low=0,high=j-1;
575         char *name;
576         char *p;
577
578         if (j == 0) return 0;
579
580         name = strdup(f_name(flist->files[j]));
581
582         if (!name) {
583                 fprintf(FERROR,"out of memory in delete_already_done");
584                 exit_cleanup(1);
585         }
586
587         p = strrchr(name,'/');
588         if (!p) {
589                 free(name);
590                 return 0;
591         }
592         *p = 0;
593
594         while (low != high) {
595                 int mid = (low+high)/2;
596                 int ret = strcmp(f_name(flist->files[flist_up(flist, mid)]),name);
597                 if (ret == 0) {
598                         free(name);
599                         return 1;
600                 }
601                 if (ret > 0) {
602                         high=mid;
603                 } else {
604                         low=mid+1;
605                 }
606         }
607
608         low = flist_up(flist, low);
609
610         if (strcmp(f_name(flist->files[low]),name) == 0) {
611                 free(name);
612                 return 1;
613         }
614
615         free(name);
616         return 0;
617 }
618
619
620 /* this deletes any files on the receiving side that are not present
621    on the sending side. For version 1.6.4 I have changed the behaviour
622    to match more closely what most people seem to expect of this option */
623 static void delete_files(struct file_list *flist)
624 {
625   struct file_list *local_file_list;
626   int i, j;
627
628   if (cvs_exclude)
629     add_cvs_excludes();
630
631   for (j=0;j<flist->count;j++) {
632           char *name = f_name(flist->files[j]);
633
634           if (!S_ISDIR(flist->files[j]->mode)) continue;
635
636           if (delete_already_done(flist, j)) continue;
637
638           if (!(local_file_list = send_file_list(-1,1,&name)))
639                   continue;
640
641           if (verbose > 1)
642                   fprintf(FINFO,"deleting in %s\n", name);
643
644           for (i=local_file_list->count-1;i>=0;i--) {
645                   if (!local_file_list->files[i]->basename) continue;
646                   if (-1 == flist_find(flist,local_file_list->files[i])) {
647                           delete_one(local_file_list->files[i]);
648                   }    
649           }
650           flist_free(local_file_list);
651   }
652 }
653
654 static char *cleanup_fname = NULL;
655
656 void exit_cleanup(int code)
657 {
658         if (cleanup_fname)
659                 unlink(cleanup_fname);
660         signal(SIGUSR1, SIG_IGN);
661         if (code) {
662                 kill_all(SIGUSR1);
663         }
664         exit(code);
665 }
666
667 void sig_int(void)
668 {
669   exit_cleanup(1);
670 }
671
672
673 int recv_files(int f_in,struct file_list *flist,char *local_name,int f_gen)
674 {  
675   int fd1,fd2;
676   struct stat st;
677   char *fname;
678   char fnametmp[MAXPATHLEN];
679   struct map_struct *buf;
680   int i;
681   struct file_struct *file;
682   int phase=0;
683   int recv_ok;
684
685   if (verbose > 2) {
686     fprintf(FERROR,"recv_files(%d) starting\n",flist->count);
687   }
688
689   if (recurse && delete_mode && !local_name && flist->count>0) {
690     delete_files(flist);
691   }
692
693   while (1) 
694     {      
695       i = read_int(f_in);
696       if (i == -1) {
697         if (phase==0 && remote_version >= 13) {
698           phase++;
699           csum_length = SUM_LENGTH;
700           if (verbose > 2)
701             fprintf(FERROR,"recv_files phase=%d\n",phase);
702           write_int(f_gen,-1);
703           write_flush(f_gen);
704           continue;
705         }
706         break;
707       }
708
709       file = flist->files[i];
710       fname = f_name(file);
711
712       if (local_name)
713         fname = local_name;
714
715       if (dry_run) {
716         if (!am_server && verbose)
717           printf("%s\n",fname);
718         continue;
719       }
720
721       if (verbose > 2)
722         fprintf(FERROR,"recv_files(%s)\n",fname);
723
724       /* open the file */  
725       fd1 = open(fname,O_RDONLY);
726
727       if (fd1 != -1 && fstat(fd1,&st) != 0) {
728         fprintf(FERROR,"fstat %s : %s\n",fname,strerror(errno));
729         receive_data(f_in,NULL,-1,NULL);
730         close(fd1);
731         continue;
732       }
733
734       if (fd1 != -1 && !S_ISREG(st.st_mode)) {
735         fprintf(FERROR,"%s : not a regular file (recv_files)\n",fname);
736         receive_data(f_in,NULL,-1,NULL);
737         close(fd1);
738         continue;
739       }
740
741       if (fd1 != -1 && st.st_size > 0) {
742         buf = map_file(fd1,st.st_size);
743         if (verbose > 2)
744           fprintf(FERROR,"recv mapped %s of size %d\n",fname,(int)st.st_size);
745       } else {
746         buf = NULL;
747       }
748
749       /* open tmp file */
750       if (strlen(fname) > (MAXPATHLEN-8)) {
751         fprintf(FERROR,"filename too long\n");
752         if (buf) unmap_file(buf);
753         close(fd1);
754         continue;
755       }
756       if (tmpdir) {
757               char *f;
758               f = strrchr(fname,'/');
759               if (f == NULL) 
760                       f = fname;
761               else 
762                       f++;
763               sprintf(fnametmp,"%s/%s.XXXXXX",tmpdir,f);
764       } else {
765               sprintf(fnametmp,"%s.XXXXXX",fname);
766       }
767       if (NULL == mktemp(fnametmp)) {
768         fprintf(FERROR,"mktemp %s failed\n",fnametmp);
769         receive_data(f_in,buf,-1,NULL);
770         if (buf) unmap_file(buf);
771         close(fd1);
772         continue;
773       }
774       fd2 = open(fnametmp,O_WRONLY|O_CREAT|O_EXCL,file->mode);
775       if (fd2 == -1 && relative_paths && errno == ENOENT && 
776           create_directory_path(fnametmp) == 0) {
777               fd2 = open(fnametmp,O_WRONLY|O_CREAT|O_EXCL,file->mode);
778       }
779       if (fd2 == -1) {
780         fprintf(FERROR,"open %s : %s\n",fnametmp,strerror(errno));
781         receive_data(f_in,buf,-1,NULL);
782         if (buf) unmap_file(buf);
783         close(fd1);
784         continue;
785       }
786       
787       cleanup_fname = fnametmp;
788
789       if (!am_server && verbose)
790         printf("%s\n",fname);
791
792       /* recv file data */
793       recv_ok = receive_data(f_in,buf,fd2,fname);
794
795       if (buf) unmap_file(buf);
796       if (fd1 != -1) {
797         close(fd1);
798       }
799       close(fd2);
800
801       if (verbose > 2)
802         fprintf(FERROR,"renaming %s to %s\n",fnametmp,fname);
803
804       if (make_backups) {
805         char fnamebak[MAXPATHLEN];
806         if (strlen(fname) + strlen(backup_suffix) > (MAXPATHLEN-1)) {
807                 fprintf(FERROR,"backup filename too long\n");
808                 continue;
809         }
810         sprintf(fnamebak,"%s%s",fname,backup_suffix);
811         if (rename(fname,fnamebak) != 0 && errno != ENOENT) {
812           fprintf(FERROR,"rename %s %s : %s\n",fname,fnamebak,strerror(errno));
813           continue;
814         }
815       }
816
817       /* move tmp file over real file */
818       if (rename(fnametmp,fname) != 0) {
819               if (errno == EXDEV) {
820                       /* rename failed on cross-filesystem link.  
821                          Copy the file instead. */
822                       if (copy_file(fnametmp,fname, file->mode)) {
823                               fprintf(FERROR,"copy %s -> %s : %s\n",
824                                       fnametmp,fname,strerror(errno));
825                       } else {
826                               set_perms(fname,file,NULL,0);
827                       }
828                       unlink(fnametmp);
829               } else {
830                       fprintf(FERROR,"rename %s -> %s : %s\n",
831                               fnametmp,fname,strerror(errno));
832                       unlink(fnametmp);
833               }
834       } else {
835               set_perms(fname,file,NULL,0);
836       }
837
838       cleanup_fname = NULL;
839
840
841       if (!recv_ok) {
842         if (verbose > 1)
843           fprintf(FERROR,"redoing %s(%d)\n",fname,i);
844         if (csum_length == SUM_LENGTH)
845           fprintf(FERROR,"ERROR: file corruption in %s\n",fname);
846         write_int(f_gen,i);
847       }
848     }
849
850   if (preserve_hard_links)
851           do_hard_links(flist);
852
853   /* now we need to fix any directory permissions that were 
854      modified during the transfer */
855   for (i = 0; i < flist->count; i++) {
856           struct file_struct *file = flist->files[i];
857           if (!file->basename || !S_ISDIR(file->mode)) continue;
858           recv_generator(f_name(file),flist,i,-1);
859   }
860
861   if (verbose > 2)
862     fprintf(FERROR,"recv_files finished\n");
863   
864   return 0;
865 }
866
867
868
869 off_t send_files(struct file_list *flist,int f_out,int f_in)
870
871   int fd;
872   struct sum_struct *s;
873   struct map_struct *buf;
874   struct stat st;
875   char fname[MAXPATHLEN];  
876   off_t total=0;
877   int i;
878   struct file_struct *file;
879   int phase = 0;
880
881   if (verbose > 2)
882     fprintf(FERROR,"send_files starting\n");
883
884   setup_nonblocking(f_in,f_out);
885
886   while (1) 
887     {
888       i = read_int(f_in);
889       if (i == -1) {
890         if (phase==0 && remote_version >= 13) {
891           phase++;
892           csum_length = SUM_LENGTH;
893           write_int(f_out,-1);
894           write_flush(f_out);
895           if (verbose > 2)
896             fprintf(FERROR,"send_files phase=%d\n",phase);
897           continue;
898         }
899         break;
900       }
901
902       file = flist->files[i];
903
904       fname[0] = 0;
905       if (file->basedir) {
906               strncpy(fname,file->basedir,MAXPATHLEN-1);
907               fname[MAXPATHLEN-1] = 0;
908               if (strlen(fname) == MAXPATHLEN-1) {
909                       fprintf(FERROR, "send_files failed on long-named directory %s\n",
910                               fname);
911                       return -1;
912               }
913               strcat(fname,"/");
914       }
915       strncat(fname,f_name(file),MAXPATHLEN-strlen(fname));
916
917       if (verbose > 2) 
918         fprintf(FERROR,"send_files(%d,%s)\n",i,fname);
919
920       if (dry_run) {    
921         if (!am_server && verbose)
922           printf("%s\n",fname);
923         write_int(f_out,i);
924         continue;
925       }
926
927       s = receive_sums(f_in);
928       if (!s) {
929         fprintf(FERROR,"receive_sums failed\n");
930         return -1;
931       }
932
933       fd = open(fname,O_RDONLY);
934       if (fd == -1) {
935         fprintf(FERROR,"send_files failed to open %s: %s\n",
936                 fname,strerror(errno));
937         continue;
938       }
939   
940       /* map the local file */
941       if (fstat(fd,&st) != 0) {
942         fprintf(FERROR,"fstat failed : %s\n",strerror(errno));
943         close(fd);
944         return -1;
945       }
946       
947       if (st.st_size > 0) {
948         buf = map_file(fd,st.st_size);
949       } else {
950         buf = NULL;
951       }
952
953       if (verbose > 2)
954         fprintf(FERROR,"send_files mapped %s of size %d\n",
955                 fname,(int)st.st_size);
956
957       write_int(f_out,i);
958
959       write_int(f_out,s->count);
960       write_int(f_out,s->n);
961       write_int(f_out,s->remainder);
962
963       if (verbose > 2)
964         fprintf(FERROR,"calling match_sums %s\n",fname);
965
966       if (!am_server && verbose)
967         printf("%s\n",fname);
968       
969       match_sums(f_out,s,buf,st.st_size);
970       write_flush(f_out);
971       
972       if (buf) unmap_file(buf);
973       close(fd);
974
975       free_sums(s);
976
977       if (verbose > 2)
978         fprintf(FERROR,"sender finished %s\n",fname);
979
980       total += st.st_size;
981     }
982
983   if (verbose > 2)
984     fprintf(FERROR,"send files finished\n");
985
986   match_report();
987
988   write_int(f_out,-1);
989   write_flush(f_out);
990
991   return total;
992 }
993
994
995
996 void generate_files(int f,struct file_list *flist,char *local_name,int f_recv)
997 {
998   int i;
999   int phase=0;
1000
1001   if (verbose > 2)
1002     fprintf(FERROR,"generator starting pid=%d count=%d\n",
1003             (int)getpid(),flist->count);
1004
1005   for (i = 0; i < flist->count; i++) {
1006     struct file_struct *file = flist->files[i];
1007     mode_t saved_mode = file->mode;
1008     if (!file->basename) continue;
1009
1010     /* we need to ensure that any directories we create have writeable
1011        permissions initially so that we can create the files within
1012        them. This is then fixed after the files are transferred */
1013     if (!am_root && S_ISDIR(file->mode)) {
1014       file->mode |= S_IWUSR; /* user write */
1015     }
1016
1017     recv_generator(local_name?local_name:f_name(file),
1018                    flist,i,f);
1019
1020     file->mode = saved_mode;
1021   }
1022
1023   phase++;
1024   csum_length = SUM_LENGTH;
1025   ignore_times=1;
1026
1027   if (verbose > 2)
1028     fprintf(FERROR,"generate_files phase=%d\n",phase);
1029
1030   write_int(f,-1);
1031   write_flush(f);
1032
1033   if (remote_version >= 13) {
1034     /* in newer versions of the protocol the files can cycle through
1035        the system more than once to catch initial checksum errors */
1036     for (i=read_int(f_recv); i != -1; i=read_int(f_recv)) {
1037       struct file_struct *file = flist->files[i];
1038       recv_generator(local_name?local_name:f_name(file),
1039                      flist,i,f);    
1040     }
1041
1042     phase++;
1043     if (verbose > 2)
1044       fprintf(FERROR,"generate_files phase=%d\n",phase);
1045
1046     write_int(f,-1);
1047     write_flush(f);
1048   }
1049
1050
1051   if (verbose > 2)
1052     fprintf(FERROR,"generator wrote %d\n",write_total());
1053 }
1054
1055