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