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