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