Fixed the handling of hard-linked symlinks and devices.
[rsync/rsync.git] / generator.c
1 /* -*- c-file-style: "linux" -*-
2
3    rsync -- fast file replication program
4
5    Copyright (C) 1996-2000 by Andrew Tridgell
6    Copyright (C) Paul Mackerras 1996
7    Copyright (C) 2002 by Martin Pool <mbp@samba.org>
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "rsync.h"
25
26 extern int verbose;
27 extern int dry_run;
28 extern int do_xfers;
29 extern int log_format_has_i;
30 extern int log_format_has_o_or_i;
31 extern int daemon_log_format_has_i;
32 extern int am_root;
33 extern int am_server;
34 extern int am_daemon;
35 extern int do_progress;
36 extern int recurse;
37 extern int relative_paths;
38 extern int keep_dirlinks;
39 extern int preserve_links;
40 extern int preserve_devices;
41 extern int preserve_hard_links;
42 extern int preserve_perms;
43 extern int preserve_uid;
44 extern int preserve_gid;
45 extern int preserve_times;
46 extern int omit_dir_times;
47 extern int delete_before;
48 extern int delete_during;
49 extern int delete_after;
50 extern int module_id;
51 extern int ignore_errors;
52 extern int remove_sent_files;
53 extern int delay_updates;
54 extern int update_only;
55 extern int opt_ignore_existing;
56 extern int inplace;
57 extern int append_mode;
58 extern int make_backups;
59 extern int csum_length;
60 extern int ignore_times;
61 extern int size_only;
62 extern OFF_T max_size;
63 extern int io_error;
64 extern int allowed_lull;
65 extern int sock_f_out;
66 extern int ignore_timeout;
67 extern int protocol_version;
68 extern int fuzzy_basis;
69 extern int always_checksum;
70 extern char *partial_dir;
71 extern char *basis_dir[];
72 extern int compare_dest;
73 extern int copy_dest;
74 extern int link_dest;
75 extern int whole_file;
76 extern int list_only;
77 extern int read_batch;
78 extern int only_existing;
79 extern int orig_umask;
80 extern int safe_symlinks;
81 extern long block_size; /* "long" because popt can't set an int32. */
82 extern int max_delete;
83 extern int force_delete;
84 extern int one_file_system;
85 extern struct stats stats;
86 extern dev_t filesystem_dev;
87 extern char *backup_dir;
88 extern char *backup_suffix;
89 extern int backup_suffix_len;
90 extern struct file_list *the_file_list;
91 extern struct filter_list_struct server_filter_list;
92
93 static int deletion_count = 0; /* used to implement --max-delete */
94
95
96 static int is_backup_file(char *fn)
97 {
98         int k = strlen(fn) - backup_suffix_len;
99         return k > 0 && strcmp(fn+k, backup_suffix) == 0;
100 }
101
102
103 /* Delete a file or directory.  If DEL_FORCE_RECURSE is set in the flags, or if
104  * force_delete is set, this will delete recursively as long as DEL_NO_RECURSE
105  * is not set in the flags. */
106 static int delete_item(char *fname, int mode, int flags)
107 {
108         struct file_list *dirlist;
109         char buf[MAXPATHLEN];
110         int j, dlen, zap_dir, ok;
111         void *save_filters;
112
113         if (!S_ISDIR(mode)) {
114                 if (max_delete && ++deletion_count > max_delete)
115                         return 0;
116                 if (make_backups && (backup_dir || !is_backup_file(fname)))
117                         ok = make_backup(fname);
118                 else
119                         ok = robust_unlink(fname) == 0;
120                 if (ok) {
121                         if (!(flags & DEL_TERSE))
122                                 log_delete(fname, mode);
123                         return 0;
124                 }
125                 if (errno == ENOENT) {
126                         deletion_count--;
127                         return 0;
128                 }
129                 rsyserr(FERROR, errno, "delete_file: unlink %s failed",
130                         full_fname(fname));
131                 return -1;
132         }
133
134         zap_dir = (flags & DEL_FORCE_RECURSE || (force_delete && recurse))
135                 && !(flags & DEL_NO_RECURSE);
136         if ((max_delete && ++deletion_count > max_delete)
137             || (dry_run && zap_dir)) {
138                 ok = 0;
139                 errno = ENOTEMPTY;
140         } else if (make_backups && !backup_dir && !is_backup_file(fname)
141             && !(flags & DEL_FORCE_RECURSE))
142                 ok = make_backup(fname);
143         else
144                 ok = do_rmdir(fname) == 0;
145         if (ok) {
146                 if (!(flags & DEL_TERSE))
147                         log_delete(fname, mode);
148                 return 0;
149         }
150         if (errno == ENOENT) {
151                 deletion_count--;
152                 return 0;
153         }
154         if (!zap_dir) {
155                 rsyserr(FERROR, errno, "delete_file: rmdir %s failed",
156                         full_fname(fname));
157                 return -1;
158         }
159         flags |= DEL_FORCE_RECURSE; /* mark subdir dels as not "in the way" */
160         deletion_count--;
161
162         dlen = strlcpy(buf, fname, MAXPATHLEN);
163         save_filters = push_local_filters(buf, dlen);
164
165         dirlist = get_dirlist(buf, dlen, 0);
166         for (j = dirlist->count; j--; ) {
167                 struct file_struct *fp = dirlist->files[j];
168
169                 if (fp->flags & FLAG_MOUNT_POINT)
170                         continue;
171
172                 f_name_to(fp, buf);
173                 delete_item(buf, fp->mode, flags & ~DEL_TERSE);
174         }
175         flist_free(dirlist);
176
177         pop_local_filters(save_filters);
178
179         if (max_delete && ++deletion_count > max_delete)
180                 return 0;
181
182         if (do_rmdir(fname) == 0) {
183                 if (!(flags & DEL_TERSE))
184                         log_delete(fname, mode);
185         } else if (errno != ENOTEMPTY && errno != EEXIST && errno != ENOENT) {
186                 rsyserr(FERROR, errno, "delete_file: rmdir %s failed",
187                         full_fname(fname));
188                 return -1;
189         }
190
191         return 0;
192 }
193
194
195 /* This function is used to implement per-directory deletion, and is used by
196  * all the --delete-WHEN options.  Note that the fbuf pointer must point to a
197  * MAXPATHLEN buffer with the name of the directory in it (the functions we
198  * call will append names onto the end, but the old dir value will be restored
199  * on exit). */
200 static void delete_in_dir(struct file_list *flist, char *fbuf,
201                           struct file_struct *file)
202 {
203         static int min_depth = MAXPATHLEN, cur_depth = -1;
204         static void *filt_array[MAXPATHLEN/2+1];
205         static int already_warned = 0;
206         struct file_list *dirlist;
207         char delbuf[MAXPATHLEN];
208         STRUCT_STAT st;
209         int dlen, i;
210
211         if (!flist) {
212                 while (cur_depth >= min_depth)
213                         pop_local_filters(filt_array[cur_depth--]);
214                 min_depth = MAXPATHLEN;
215                 cur_depth = -1;
216                 return;
217         }
218
219         if (verbose > 2)
220                 rprintf(FINFO, "delete_in_dir(%s)\n", safe_fname(fbuf));
221
222         if (allowed_lull)
223                 maybe_send_keepalive();
224
225         if (file->dir.depth >= MAXPATHLEN/2+1)
226                 return; /* Impossible... */
227
228         if (io_error && !(lp_ignore_errors(module_id) || ignore_errors)) {
229                 if (already_warned)
230                         return;
231                 rprintf(FINFO,
232                         "IO error encountered -- skipping file deletion\n");
233                 already_warned = 1;
234                 return;
235         }
236
237         while (cur_depth >= file->dir.depth && cur_depth >= min_depth)
238                 pop_local_filters(filt_array[cur_depth--]);
239         cur_depth = file->dir.depth;
240         if (min_depth > cur_depth)
241                 min_depth = cur_depth;
242         dlen = strlen(fbuf);
243         filt_array[cur_depth] = push_local_filters(fbuf, dlen);
244
245         if (link_stat(fbuf, &st, keep_dirlinks) < 0)
246                 return;
247
248         if (one_file_system) {
249                 if (file->flags & FLAG_TOP_DIR)
250                         filesystem_dev = st.st_dev;
251                 else if (filesystem_dev != st.st_dev)
252                         return;
253         }
254
255         dirlist = get_dirlist(fbuf, dlen, 0);
256
257         /* If an item in dirlist is not found in flist, delete it
258          * from the filesystem. */
259         for (i = dirlist->count; i--; ) {
260                 struct file_struct *fp = dirlist->files[i];
261                 if (!fp->basename || fp->flags & FLAG_MOUNT_POINT)
262                         continue;
263                 if (flist_find(flist, fp) < 0) {
264                         int mode = fp->mode;
265                         f_name_to(fp, delbuf);
266                         delete_item(delbuf, mode, DEL_FORCE_RECURSE);
267                 }
268         }
269
270         flist_free(dirlist);
271 }
272
273 /* This deletes any files on the receiving side that are not present on the
274  * sending side.  This is used by --delete-before and --delete-after. */
275 static void do_delete_pass(struct file_list *flist)
276 {
277         char fbuf[MAXPATHLEN];
278         int j;
279
280         if (dry_run > 1 /* destination doesn't exist yet */
281          || list_only)
282                 return;
283
284         for (j = 0; j < flist->count; j++) {
285                 struct file_struct *file = flist->files[j];
286
287                 if (!(file->flags & FLAG_DEL_HERE))
288                         continue;
289
290                 f_name_to(file, fbuf);
291                 if (verbose > 1 && file->flags & FLAG_TOP_DIR)
292                         rprintf(FINFO, "deleting in %s\n", safe_fname(fbuf));
293
294                 delete_in_dir(flist, fbuf, file);
295         }
296         if (do_progress && !am_server)
297                 rprintf(FINFO, "                    \r");
298 }
299
300 static int unchanged_attrs(struct file_struct *file, STRUCT_STAT *st)
301 {
302         if (preserve_perms
303          && (st->st_mode & CHMOD_BITS) != (file->mode & CHMOD_BITS))
304                 return 0;
305
306         if (am_root && preserve_uid && st->st_uid != file->uid)
307                 return 0;
308
309         if (preserve_gid && file->gid != GID_NONE && st->st_gid != file->gid)
310                 return 0;
311
312         return 1;
313 }
314
315
316 void itemize(struct file_struct *file, int ndx, int statret, STRUCT_STAT *st,
317              int32 iflags, uchar fnamecmp_type, char *xname)
318 {
319         if (statret == 0) {
320                 if (S_ISREG(file->mode) && file->length != st->st_size)
321                         iflags |= ITEM_REPORT_SIZE;
322                 if (!(iflags & ITEM_NO_DEST_AND_NO_UPDATE)) {
323                         int keep_time = !preserve_times ? 0
324                             : S_ISDIR(file->mode) ? !omit_dir_times
325                             : !S_ISLNK(file->mode);
326
327                         if ((iflags & (ITEM_TRANSFER|ITEM_LOCAL_CHANGE) && !keep_time
328                              && (!(iflags & ITEM_XNAME_FOLLOWS) || *xname))
329                             || (keep_time && cmp_modtime(file->modtime, st->st_mtime) != 0))
330                                 iflags |= ITEM_REPORT_TIME;
331                         if (preserve_perms
332                          && (file->mode & CHMOD_BITS) != (st->st_mode & CHMOD_BITS))
333                                 iflags |= ITEM_REPORT_PERMS;
334                         if (preserve_uid && am_root && file->uid != st->st_uid)
335                                 iflags |= ITEM_REPORT_OWNER;
336                         if (preserve_gid && file->gid != GID_NONE
337                             && st->st_gid != file->gid)
338                                 iflags |= ITEM_REPORT_GROUP;
339                 }
340         } else
341                 iflags |= ITEM_IS_NEW;
342
343         iflags &= 0xffff;
344         if ((iflags & SIGNIFICANT_ITEM_FLAGS || verbose > 1
345           || (xname && *xname)) && !read_batch) {
346                 if (protocol_version >= 29) {
347                         if (ndx >= 0)
348                                 write_int(sock_f_out, ndx);
349                         write_shortint(sock_f_out, iflags);
350                         if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
351                                 write_byte(sock_f_out, fnamecmp_type);
352                         if (iflags & ITEM_XNAME_FOLLOWS)
353                                 write_vstring(sock_f_out, xname, strlen(xname));
354                 } else if (ndx >= 0)
355                         log_item(file, &stats, iflags, xname);
356         }
357 }
358
359
360 /* Perform our quick-check heuristic for determining if a file is unchanged. */
361 static int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st)
362 {
363         if (st->st_size != file->length)
364                 return 0;
365
366         /* if always checksum is set then we use the checksum instead
367            of the file time to determine whether to sync */
368         if (always_checksum && S_ISREG(st->st_mode)) {
369                 char sum[MD4_SUM_LENGTH];
370                 file_checksum(fn, sum, st->st_size);
371                 return memcmp(sum, file->u.sum, protocol_version < 21 ? 2
372                                                         : MD4_SUM_LENGTH) == 0;
373         }
374
375         if (size_only)
376                 return 1;
377
378         if (ignore_times)
379                 return 0;
380
381         return cmp_modtime(st->st_mtime, file->modtime) == 0;
382 }
383
384
385 /*
386  * set (initialize) the size entries in the per-file sum_struct
387  * calculating dynamic block and checksum sizes.
388  *
389  * This is only called from generate_and_send_sums() but is a separate
390  * function to encapsulate the logic.
391  *
392  * The block size is a rounded square root of file length.
393  *
394  * The checksum size is determined according to:
395  *     blocksum_bits = BLOCKSUM_BIAS + 2*log2(file_len) - log2(block_len)
396  * provided by Donovan Baarda which gives a probability of rsync
397  * algorithm corrupting data and falling back using the whole md4
398  * checksums.
399  *
400  * This might be made one of several selectable heuristics.
401  */
402 static void sum_sizes_sqroot(struct sum_struct *sum, int64 len)
403 {
404         int32 blength;
405         int s2length;
406
407         if (block_size)
408                 blength = block_size;
409         else if (len <= BLOCK_SIZE * BLOCK_SIZE)
410                 blength = BLOCK_SIZE;
411         else {
412                 int32 c;
413                 int64 l;
414                 int cnt;
415                 for (c = 1, l = len, cnt = 0; l >>= 2; c <<= 1, cnt++) {}
416                 if (cnt >= 31 || c >= MAX_BLOCK_SIZE)
417                         blength = MAX_BLOCK_SIZE;
418                 else {
419                     blength = 0;
420                     do {
421                             blength |= c;
422                             if (len < (int64)blength * blength)
423                                     blength &= ~c;
424                             c >>= 1;
425                     } while (c >= 8);   /* round to multiple of 8 */
426                     blength = MAX(blength, BLOCK_SIZE);
427                 }
428         }
429
430         if (protocol_version < 27) {
431                 s2length = csum_length;
432         } else if (csum_length == SUM_LENGTH) {
433                 s2length = SUM_LENGTH;
434         } else {
435                 int32 c;
436                 int64 l;
437                 int b = BLOCKSUM_BIAS;
438                 for (l = len; l >>= 1; b += 2) {}
439                 for (c = blength; c >>= 1 && b; b--) {}
440                 /* add a bit, subtract rollsum, round up. */
441                 s2length = (b + 1 - 32 + 7) / 8; /* --optimize in compiler-- */
442                 s2length = MAX(s2length, csum_length);
443                 s2length = MIN(s2length, SUM_LENGTH);
444         }
445
446         sum->flength    = len;
447         sum->blength    = blength;
448         sum->s2length   = s2length;
449         sum->count      = (len + (blength - 1)) / blength;
450         sum->remainder  = (len % blength);
451
452         if (sum->count && verbose > 2) {
453                 rprintf(FINFO,
454                         "count=%.0f rem=%ld blength=%ld s2length=%d flength=%.0f\n",
455                         (double)sum->count, (long)sum->remainder, (long)sum->blength,
456                         sum->s2length, (double)sum->flength);
457         }
458 }
459
460
461 /*
462  * Generate and send a stream of signatures/checksums that describe a buffer
463  *
464  * Generate approximately one checksum every block_len bytes.
465  */
466 static void generate_and_send_sums(int fd, OFF_T len, int f_out, int f_copy)
467 {
468         int32 i;
469         struct map_struct *mapbuf;
470         struct sum_struct sum;
471         OFF_T offset = 0;
472
473         sum_sizes_sqroot(&sum, len);
474         write_sum_head(f_out, &sum);
475
476         if (append_mode > 0 && f_copy < 0)
477                 return;
478
479         if (len > 0)
480                 mapbuf = map_file(fd, len, MAX_MAP_SIZE, sum.blength);
481         else
482                 mapbuf = NULL;
483
484         for (i = 0; i < sum.count; i++) {
485                 int32 n1 = (int32)MIN(len, (OFF_T)sum.blength);
486                 char *map = map_ptr(mapbuf, offset, n1);
487                 char sum2[SUM_LENGTH];
488                 uint32 sum1;
489
490                 len -= n1;
491                 offset += n1;
492
493                 if (f_copy >= 0) {
494                         full_write(f_copy, map, n1);
495                         if (append_mode > 0)
496                                 continue;
497                 }
498
499                 sum1 = get_checksum1(map, n1);
500                 get_checksum2(map, n1, sum2);
501
502                 if (verbose > 3) {
503                         rprintf(FINFO,
504                                 "chunk[%.0f] offset=%.0f len=%ld sum1=%08lx\n",
505                                 (double)i, (double)offset - n1, (long)n1,
506                                 (unsigned long)sum1);
507                 }
508                 write_int(f_out, sum1);
509                 write_buf(f_out, sum2, sum.s2length);
510         }
511
512         if (mapbuf)
513                 unmap_file(mapbuf);
514 }
515
516
517 /* Try to find a filename in the same dir as "fname" with a similar name. */
518 static int find_fuzzy(struct file_struct *file, struct file_list *dirlist)
519 {
520         int fname_len, fname_suf_len;
521         const char *fname_suf, *fname = file->basename;
522         uint32 lowest_dist = 25 << 16; /* ignore a distance greater than 25 */
523         int j, lowest_j = -1;
524
525         fname_len = strlen(fname);
526         fname_suf = find_filename_suffix(fname, fname_len, &fname_suf_len);
527
528         for (j = 0; j < dirlist->count; j++) {
529                 struct file_struct *fp = dirlist->files[j];
530                 const char *suf, *name;
531                 int len, suf_len;
532                 uint32 dist;
533
534                 if (!S_ISREG(fp->mode) || !fp->length
535                     || fp->flags & FLAG_NO_FUZZY)
536                         continue;
537
538                 name = fp->basename;
539
540                 if (fp->length == file->length
541                     && cmp_modtime(fp->modtime, file->modtime) == 0) {
542                         if (verbose > 4) {
543                                 rprintf(FINFO,
544                                         "fuzzy size/modtime match for %s\n",
545                                         name);
546                         }
547                         return j;
548                 }
549
550                 len = strlen(name);
551                 suf = find_filename_suffix(name, len, &suf_len);
552
553                 dist = fuzzy_distance(name, len, fname, fname_len);
554                 /* Add some extra weight to how well the suffixes match. */
555                 dist += fuzzy_distance(suf, suf_len, fname_suf, fname_suf_len)
556                       * 10;
557                 if (verbose > 4) {
558                         rprintf(FINFO, "fuzzy distance for %s = %d.%05d\n",
559                                 name, (int)(dist>>16), (int)(dist&0xFFFF));
560                 }
561                 if (dist <= lowest_dist) {
562                         lowest_dist = dist;
563                         lowest_j = j;
564                 }
565         }
566
567         return lowest_j;
568 }
569
570 void check_for_finished_hlinks(int itemizing, enum logcode code)
571 {
572         struct file_struct *file;
573         int ndx;
574
575         while ((ndx = get_hlink_num()) != -1) {
576                 if (ndx < 0 || ndx >= the_file_list->count)
577                         continue;
578
579                 file = the_file_list->files[ndx];
580                 if (!file->link_u.links)
581                         continue;
582
583                 hard_link_cluster(file, ndx, itemizing, code);
584         }
585 }
586
587 static int phase = 0;
588
589 /* Acts on the_file_list->file's ndx'th item, whose name is fname.  If a dir,
590  * make sure it exists, and has the right permissions/timestamp info.  For
591  * all other non-regular files (symlinks, etc.) we create them here.  For
592  * regular files that have changed, we try to find a basis file and then
593  * start sending checksums.
594  *
595  * Note that f_out is set to -1 when doing final directory-permission and
596  * modification-time repair. */
597 static void recv_generator(char *fname, struct file_struct *file, int ndx,
598                            int itemizing, int maybe_PERMS_REPORT,
599                            enum logcode code, int f_out)
600 {
601         static int missing_below = -1, excluded_below = -1;
602         static char *fuzzy_dirname = "";
603         static struct file_list *fuzzy_dirlist = NULL;
604         struct file_struct *fuzzy_file = NULL;
605         int fd = -1, f_copy = -1;
606         STRUCT_STAT st, real_st, partial_st;
607         struct file_struct *back_file = NULL;
608         int statret, real_ret, stat_errno;
609         char *fnamecmp, *partialptr, *backupptr = NULL;
610         char fnamecmpbuf[MAXPATHLEN];
611         uchar fnamecmp_type;
612
613         if (list_only)
614                 return;
615
616         if (!fname) {
617                 if (fuzzy_dirlist) {
618                         flist_free(fuzzy_dirlist);
619                         fuzzy_dirlist = NULL;
620                         fuzzy_dirname = "";
621                 }
622                 if (missing_below >= 0) {
623                         dry_run--;
624                         missing_below = -1;
625                 }
626                 return;
627         }
628
629         if (verbose > 2) {
630                 rprintf(FINFO, "recv_generator(%s,%d)\n",
631                         safe_fname(fname), ndx);
632         }
633
634         if (server_filter_list.head) {
635                 if (excluded_below >= 0) {
636                         if (file->dir.depth > excluded_below)
637                                 goto skipping;
638                         excluded_below = -1;
639                 }
640                 if (check_filter(&server_filter_list, fname,
641                                  S_ISDIR(file->mode)) < 0) {
642                         if (S_ISDIR(file->mode))
643                                 excluded_below = file->dir.depth;
644                     skipping:
645                         if (verbose) {
646                                 rprintf(FINFO,
647                                         "skipping server-excluded file \"%s\"\n",
648                                         safe_fname(fname));
649                         }
650                         return;
651                 }
652         }
653
654         if (missing_below >= 0 && file->dir.depth <= missing_below) {
655                 dry_run--;
656                 missing_below = -1;
657         }
658         if (dry_run > 1) {
659                 statret = -1;
660                 stat_errno = ENOENT;
661         } else {
662                 if (fuzzy_basis && S_ISREG(file->mode)) {
663                         char *dn = file->dirname ? file->dirname : ".";
664                         if (fuzzy_dirname != dn
665                             && strcmp(fuzzy_dirname, dn) != 0) {
666                                 if (fuzzy_dirlist)
667                                         flist_free(fuzzy_dirlist);
668                                 fuzzy_dirlist = get_dirlist(dn, -1, 1);
669                         }
670                         fuzzy_dirname = dn;
671                 }
672
673                 statret = link_stat(fname, &st,
674                                     keep_dirlinks && S_ISDIR(file->mode));
675                 stat_errno = errno;
676         }
677
678         if (only_existing && statret == -1 && stat_errno == ENOENT) {
679                 /* we only want to update existing files */
680                 if (verbose > 1) {
681                         rprintf(FINFO, "not creating new %s \"%s\"\n",
682                                 S_ISDIR(file->mode) ? "directory" : "file",
683                                 safe_fname(fname));
684                 }
685                 return;
686         }
687
688         if (statret == 0 && !preserve_perms
689             && S_ISDIR(st.st_mode) == S_ISDIR(file->mode)) {
690                 /* if the file exists already and we aren't perserving
691                  * permissions then act as though the remote end sent
692                  * us the file permissions we already have */
693                 file->mode = (file->mode & ~CHMOD_BITS)
694                            | (st.st_mode & CHMOD_BITS);
695         }
696
697         if (S_ISDIR(file->mode)) {
698                 /* The file to be received is a directory, so we need
699                  * to prepare appropriately.  If there is already a
700                  * file of that name and it is *not* a directory, then
701                  * we need to delete it.  If it doesn't exist, then
702                  * (perhaps recursively) create it. */
703                 if (statret == 0 && !S_ISDIR(st.st_mode)) {
704                         if (delete_item(fname, st.st_mode, DEL_TERSE) < 0)
705                                 return;
706                         statret = -1;
707                 }
708                 if (dry_run && statret != 0 && missing_below < 0) {
709                         missing_below = file->dir.depth;
710                         dry_run++;
711                 }
712                 if (itemizing && f_out != -1) {
713                         itemize(file, ndx, statret, &st,
714                                 statret ? ITEM_LOCAL_CHANGE : 0, 0, NULL);
715                 }
716                 if (statret != 0 && do_mkdir(fname,file->mode) < 0 && errno != EEXIST) {
717                         if (!relative_paths || errno != ENOENT
718                             || create_directory_path(fname, orig_umask) < 0
719                             || (do_mkdir(fname, file->mode) < 0 && errno != EEXIST)) {
720                                 rsyserr(FERROR, errno,
721                                         "recv_generator: mkdir %s failed",
722                                         full_fname(fname));
723                         }
724                 }
725                 if (set_perms(fname, file, statret ? NULL : &st, 0)
726                     && verbose && code && f_out != -1)
727                         rprintf(code, "%s/\n", safe_fname(fname));
728                 if (delete_during && f_out != -1 && !phase && dry_run < 2
729                     && (file->flags & FLAG_DEL_HERE))
730                         delete_in_dir(the_file_list, fname, file);
731                 return;
732         }
733
734         if (preserve_hard_links && file->link_u.links
735             && hard_link_check(file, ndx, fname, statret, &st,
736                                itemizing, code, HL_CHECK_MASTER))
737                 return;
738
739         if (preserve_links && S_ISLNK(file->mode)) {
740 #ifdef SUPPORT_LINKS
741                 if (safe_symlinks && unsafe_symlink(file->u.link, fname)) {
742                         if (verbose) {
743                                 if (the_file_list->count == 1)
744                                         fname = f_name(file);
745                                 rprintf(FINFO,
746                                         "ignoring unsafe symlink %s -> \"%s\"\n",
747                                         full_fname(fname),
748                                         safe_fname(file->u.link));
749                         }
750                         return;
751                 }
752                 if (statret == 0) {
753                         char lnk[MAXPATHLEN];
754                         int len;
755
756                         if (!S_ISDIR(st.st_mode)
757                             && (len = readlink(fname, lnk, MAXPATHLEN-1)) > 0) {
758                                 lnk[len] = 0;
759                                 /* A link already pointing to the
760                                  * right place -- no further action
761                                  * required. */
762                                 if (strcmp(lnk, file->u.link) == 0) {
763                                         if (itemizing) {
764                                                 itemize(file, ndx, 0, &st, 0,
765                                                         0, NULL);
766                                         }
767                                         set_perms(fname, file, &st,
768                                                   maybe_PERMS_REPORT);
769                                         if (preserve_hard_links
770                                             && file->link_u.links) {
771                                                 hard_link_cluster(file, ndx,
772                                                                   itemizing,
773                                                                   code);
774                                         }
775                                         return;
776                                 }
777                         }
778                         /* Not the right symlink (or not a symlink), so
779                          * delete it. */
780                         if (delete_item(fname, st.st_mode, DEL_TERSE) < 0)
781                                 return;
782                         if (!S_ISLNK(st.st_mode))
783                                 statret = -1;
784                 }
785                 if (preserve_hard_links && file->link_u.links
786                     && hard_link_check(file, ndx, fname, -1, &st,
787                                        itemizing, code, HL_SKIP))
788                         return;
789                 if (do_symlink(file->u.link,fname) != 0) {
790                         rsyserr(FERROR, errno, "symlink %s -> \"%s\" failed",
791                                 full_fname(fname), safe_fname(file->u.link));
792                 } else {
793                         set_perms(fname,file,NULL,0);
794                         if (itemizing) {
795                                 itemize(file, ndx, statret, &st,
796                                         ITEM_LOCAL_CHANGE, 0, NULL);
797                         }
798                         if (code && verbose) {
799                                 rprintf(code, "%s -> %s\n", safe_fname(fname),
800                                         safe_fname(file->u.link));
801                         }
802                         if (remove_sent_files && !dry_run) {
803                                 char numbuf[4];
804                                 SIVAL(numbuf, 0, ndx);
805                                 send_msg(MSG_SUCCESS, numbuf, 4);
806                         }
807                         if (preserve_hard_links && file->link_u.links)
808                                 hard_link_cluster(file, ndx, itemizing, code);
809                 }
810 #endif
811                 return;
812         }
813
814         if (am_root && preserve_devices && IS_DEVICE(file->mode)) {
815                 if (statret != 0 ||
816                     st.st_mode != file->mode ||
817                     st.st_rdev != file->u.rdev) {
818                         if (delete_item(fname, st.st_mode, DEL_TERSE) < 0)
819                                 return;
820                         if (preserve_hard_links && file->link_u.links
821                             && hard_link_check(file, ndx, fname, -1, &st,
822                                                itemizing, code, HL_SKIP))
823                                 return;
824                         if (!IS_DEVICE(st.st_mode))
825                                 statret = -1;
826                         if (verbose > 2) {
827                                 rprintf(FINFO,"mknod(%s,0%o,0x%x)\n",
828                                         safe_fname(fname),
829                                         (int)file->mode, (int)file->u.rdev);
830                         }
831                         if (do_mknod(fname,file->mode,file->u.rdev) < 0) {
832                                 rsyserr(FERROR, errno, "mknod %s failed",
833                                         full_fname(fname));
834                         } else {
835                                 set_perms(fname,file,NULL,0);
836                                 if (itemizing) {
837                                         itemize(file, ndx, statret, &st,
838                                                 ITEM_LOCAL_CHANGE, 0, NULL);
839                                 }
840                                 if (code && verbose) {
841                                         rprintf(code, "%s\n",
842                                                 safe_fname(fname));
843                                 }
844                                 if (preserve_hard_links && file->link_u.links) {
845                                         hard_link_cluster(file, ndx,
846                                                           itemizing, code);
847                                 }
848                         }
849                 } else {
850                         if (itemizing)
851                                 itemize(file, ndx, statret, &st, 0, 0, NULL);
852                         set_perms(fname, file, &st, maybe_PERMS_REPORT);
853                         if (preserve_hard_links && file->link_u.links)
854                                 hard_link_cluster(file, ndx, itemizing, code);
855                 }
856                 return;
857         }
858
859         if (!S_ISREG(file->mode)) {
860                 if (the_file_list->count == 1)
861                         fname = f_name(file);
862                 rprintf(FINFO, "skipping non-regular file \"%s\"\n",
863                         safe_fname(fname));
864                 return;
865         }
866
867         if (max_size && file->length > max_size) {
868                 if (verbose > 1) {
869                         if (the_file_list->count == 1)
870                                 fname = f_name(file);
871                         rprintf(FINFO, "%s is over max-size\n",
872                                 safe_fname(fname));
873                 }
874                 return;
875         }
876
877         if (opt_ignore_existing && statret == 0) {
878                 if (verbose > 1)
879                         rprintf(FINFO, "%s exists\n", safe_fname(fname));
880                 return;
881         }
882
883         if (update_only && statret == 0
884             && cmp_modtime(st.st_mtime, file->modtime) > 0) {
885                 if (verbose > 1)
886                         rprintf(FINFO, "%s is newer\n", safe_fname(fname));
887                 return;
888         }
889
890         fnamecmp = fname;
891         fnamecmp_type = FNAMECMP_FNAME;
892
893         if (statret == 0 && !S_ISREG(st.st_mode)) {
894                 if (delete_item(fname, st.st_mode, DEL_TERSE) != 0)
895                         return;
896                 statret = -1;
897                 stat_errno = ENOENT;
898         }
899
900         if (statret != 0 && basis_dir[0] != NULL) {
901                 int best_match = -1;
902                 int match_level = 0;
903                 int i = 0;
904                 do {
905                         pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
906                                  basis_dir[i], fname);
907                         if (link_stat(fnamecmpbuf, &st, 0) < 0
908                             || !S_ISREG(st.st_mode))
909                                 continue;
910                         switch (match_level) {
911                         case 0:
912                                 best_match = i;
913                                 match_level = 1;
914                                 /* FALL THROUGH */
915                         case 1:
916                                 if (!unchanged_file(fnamecmpbuf, file, &st))
917                                         continue;
918                                 best_match = i;
919                                 match_level = 2;
920                                 if (copy_dest)
921                                         break;
922                                 /* FALL THROUGH */
923                         case 2:
924                                 if (!unchanged_attrs(file, &st))
925                                         continue;
926                                 best_match = i;
927                                 match_level = 3;
928                                 break;
929                         }
930                         break;
931                 } while (basis_dir[++i] != NULL);
932                 if (match_level) {
933                         statret = 0;
934                         if (i != best_match) {
935                                 i = best_match;
936                                 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
937                                          basis_dir[i], fname);
938                                 if (link_stat(fnamecmpbuf, &st, 0) < 0) {
939                                         match_level = 0;
940                                         statret = -1;
941                                         stat_errno = errno;
942                                 }
943                         }
944 #ifdef HAVE_LINK
945                         if (link_dest && match_level == 3) {
946                                 if (hard_link_one(file, ndx, fname, -1, &st,
947                                                   fnamecmpbuf, 1,
948                                                   itemizing && verbose > 1,
949                                                   code) == 0) {
950                                         if (preserve_hard_links
951                                             && file->link_u.links) {
952                                                 hard_link_cluster(file, ndx,
953                                                                   itemizing,
954                                                                   code);
955                                         }
956                                         return;
957                                 }
958                                 match_level = 2;
959                         }
960 #endif
961                         if (match_level == 2) {
962                                 /* Copy the file locally. */
963                                 if (copy_file(fnamecmpbuf, fname, file->mode) < 0) {
964                                         if (verbose) {
965                                                 rsyserr(FINFO, errno,
966                                                         "copy_file %s => %s",
967                                                         full_fname(fnamecmpbuf),
968                                                         safe_fname(fname));
969                                         }
970                                         match_level = 0;
971                                         statret = -1;
972                                 } else {
973                                         if (itemizing) {
974                                                 itemize(file, ndx, 0, &st,
975                                                         ITEM_LOCAL_CHANGE, 0,
976                                                         NULL);
977                                         } else if (verbose && code) {
978                                                 rprintf(code, "%s\n",
979                                                         safe_fname(fname));
980                                         }
981                                         set_perms(fname, file, NULL,
982                                                   maybe_PERMS_REPORT);
983                                         if (preserve_hard_links
984                                             && file->link_u.links) {
985                                                 hard_link_cluster(file, ndx,
986                                                                   itemizing,
987                                                                   code);
988                                         }
989                                         return;
990                                 }
991                         } else if (compare_dest || match_level == 1) {
992                                 fnamecmp = fnamecmpbuf;
993                                 fnamecmp_type = i;
994                         }
995                 }
996         }
997
998         real_ret = statret;
999         real_st = st;
1000
1001         if (partial_dir && (partialptr = partial_dir_fname(fname)) != NULL
1002             && link_stat(partialptr, &partial_st, 0) == 0
1003             && S_ISREG(partial_st.st_mode)) {
1004                 if (statret != 0)
1005                         goto prepare_to_open;
1006         } else
1007                 partialptr = NULL;
1008
1009         if (statret != 0 && fuzzy_basis && dry_run <= 1) {
1010                 int j = find_fuzzy(file, fuzzy_dirlist);
1011                 if (j >= 0) {
1012                         fuzzy_file = fuzzy_dirlist->files[j];
1013                         f_name_to(fuzzy_file, fnamecmpbuf);
1014                         if (verbose > 2) {
1015                                 rprintf(FINFO, "fuzzy basis selected for %s: %s\n",
1016                                         safe_fname(fname), safe_fname(fnamecmpbuf));
1017                         }
1018                         st.st_size = fuzzy_file->length;
1019                         statret = 0;
1020                         fnamecmp = fnamecmpbuf;
1021                         fnamecmp_type = FNAMECMP_FUZZY;
1022                 }
1023         }
1024
1025         if (statret != 0) {
1026                 if (preserve_hard_links && file->link_u.links
1027                     && hard_link_check(file, ndx, fname, statret, &st,
1028                                        itemizing, code, HL_SKIP))
1029                         return;
1030                 if (stat_errno == ENOENT)
1031                         goto notify_others;
1032                 if (verbose > 1) {
1033                         rsyserr(FERROR, stat_errno,
1034                                 "recv_generator: failed to stat %s",
1035                                 full_fname(fname));
1036                 }
1037                 return;
1038         }
1039
1040         if (append_mode && st.st_size > file->length)
1041                 return;
1042
1043         if (!compare_dest && fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
1044                 ;
1045         else if (fnamecmp_type == FNAMECMP_FUZZY)
1046                 ;
1047         else if (unchanged_file(fnamecmp, file, &st)) {
1048                 if (fnamecmp_type == FNAMECMP_FNAME) {
1049                         if (itemizing) {
1050                                 itemize(file, ndx, real_ret, &real_st,
1051                                         0, 0, NULL);
1052                         }
1053                         set_perms(fname, file, &st, maybe_PERMS_REPORT);
1054                         if (preserve_hard_links && file->link_u.links)
1055                                 hard_link_cluster(file, ndx, itemizing, code);
1056                         return;
1057                 }
1058                 /* Only --compare-dest gets here. */
1059                 itemize(file, ndx, real_ret, &real_st,
1060                         ITEM_NO_DEST_AND_NO_UPDATE, 0, NULL);
1061                 return;
1062         }
1063
1064 prepare_to_open:
1065         if (partialptr) {
1066                 st = partial_st;
1067                 fnamecmp = partialptr;
1068                 fnamecmp_type = FNAMECMP_PARTIAL_DIR;
1069                 statret = 0;
1070         }
1071
1072         if (!do_xfers || read_batch || whole_file)
1073                 goto notify_others;
1074
1075         if (fuzzy_basis) {
1076                 int j = flist_find(fuzzy_dirlist, file);
1077                 if (j >= 0) /* don't use changing file as future fuzzy basis */
1078                         fuzzy_dirlist->files[j]->flags |= FLAG_NO_FUZZY;
1079         }
1080
1081         /* open the file */
1082         fd = do_open(fnamecmp, O_RDONLY, 0);
1083
1084         if (fd == -1) {
1085                 rsyserr(FERROR, errno, "failed to open %s, continuing",
1086                         full_fname(fnamecmp));
1087             pretend_missing:
1088                 /* pretend the file didn't exist */
1089                 if (preserve_hard_links && file->link_u.links
1090                     && hard_link_check(file, ndx, fname, statret, &st,
1091                                        itemizing, code, HL_SKIP))
1092                         return;
1093                 statret = real_ret = -1;
1094                 goto notify_others;
1095         }
1096
1097         if (inplace && make_backups && fnamecmp_type == FNAMECMP_FNAME) {
1098                 if (!(backupptr = get_backup_name(fname))) {
1099                         close(fd);
1100                         return;
1101                 }
1102                 if (!(back_file = make_file(fname, NULL, NO_FILTERS))) {
1103                         close(fd);
1104                         goto pretend_missing;
1105                 }
1106                 if (robust_unlink(backupptr) && errno != ENOENT) {
1107                         rsyserr(FERROR, errno, "unlink %s",
1108                                 full_fname(backupptr));
1109                         free(back_file);
1110                         close(fd);
1111                         return;
1112                 }
1113                 if ((f_copy = do_open(backupptr,
1114                     O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0) {
1115                         rsyserr(FERROR, errno, "open %s",
1116                                 full_fname(backupptr));
1117                         free(back_file);
1118                         close(fd);
1119                         return;
1120                 }
1121                 fnamecmp_type = FNAMECMP_BACKUP;
1122         }
1123
1124         if (verbose > 3) {
1125                 rprintf(FINFO, "gen mapped %s of size %.0f\n",
1126                         safe_fname(fnamecmp), (double)st.st_size);
1127         }
1128
1129         if (verbose > 2)
1130                 rprintf(FINFO, "generating and sending sums for %d\n", ndx);
1131
1132 notify_others:
1133         write_int(f_out, ndx);
1134         if (itemizing) {
1135                 int iflags = ITEM_TRANSFER;
1136                 if (always_checksum)
1137                         iflags |= ITEM_REPORT_CHECKSUM;
1138                 if (fnamecmp_type != FNAMECMP_FNAME)
1139                         iflags |= ITEM_BASIS_TYPE_FOLLOWS;
1140                 if (fnamecmp_type == FNAMECMP_FUZZY)
1141                         iflags |= ITEM_XNAME_FOLLOWS;
1142                 itemize(file, -1, real_ret, &real_st, iflags, fnamecmp_type,
1143                         fuzzy_file ? fuzzy_file->basename : NULL);
1144         }
1145
1146         if (!do_xfers) {
1147                 if (preserve_hard_links && file->link_u.links)
1148                         hard_link_cluster(file, ndx, itemizing, code);
1149                 return;
1150         }
1151         if (read_batch)
1152                 return;
1153
1154         if (statret != 0 || whole_file) {
1155                 write_sum_head(f_out, NULL);
1156                 return;
1157         }
1158
1159         generate_and_send_sums(fd, st.st_size, f_out, f_copy);
1160
1161         if (f_copy >= 0) {
1162                 close(f_copy);
1163                 set_perms(backupptr, back_file, NULL, 0);
1164                 if (verbose > 1) {
1165                         rprintf(FINFO, "backed up %s to %s\n",
1166                                 safe_fname(fname), safe_fname(backupptr));
1167                 }
1168                 free(back_file);
1169         }
1170
1171         close(fd);
1172 }
1173
1174
1175 void generate_files(int f_out, struct file_list *flist, char *local_name)
1176 {
1177         int i;
1178         char fbuf[MAXPATHLEN];
1179         int itemizing, maybe_PERMS_REPORT;
1180         enum logcode code;
1181         int lull_mod = allowed_lull * 5;
1182         int need_retouch_dir_times = preserve_times && !omit_dir_times;
1183         int need_retouch_dir_perms = 0;
1184         int save_only_existing = only_existing;
1185         int save_opt_ignore_existing = opt_ignore_existing;
1186         int save_do_progress = do_progress;
1187         int save_make_backups = make_backups;
1188
1189         if (protocol_version >= 29) {
1190                 itemizing = 1;
1191                 maybe_PERMS_REPORT = log_format_has_i ? 0 : PERMS_REPORT;
1192                 code = daemon_log_format_has_i ? 0 : FLOG;
1193         } else if (am_daemon) {
1194                 itemizing = daemon_log_format_has_i && do_xfers;
1195                 maybe_PERMS_REPORT = PERMS_REPORT;
1196                 code = itemizing || !do_xfers ? FCLIENT : FINFO;
1197         } else if (!am_server) {
1198                 itemizing = log_format_has_i;
1199                 maybe_PERMS_REPORT = log_format_has_i ? 0 : PERMS_REPORT;
1200                 code = itemizing ? 0 : FINFO;
1201         } else {
1202                 itemizing = 0;
1203                 maybe_PERMS_REPORT = PERMS_REPORT;
1204                 code = FINFO;
1205         }
1206
1207         if (verbose > 2) {
1208                 rprintf(FINFO, "generator starting pid=%ld count=%d\n",
1209                         (long)getpid(), flist->count);
1210         }
1211
1212         if (delete_before && !local_name && flist->count > 0)
1213                 do_delete_pass(flist);
1214         do_progress = 0;
1215
1216         if (append_mode || whole_file < 0)
1217                 whole_file = 0;
1218         if (verbose >= 2) {
1219                 rprintf(FINFO, "delta-transmission %s\n",
1220                         whole_file
1221                         ? "disabled for local transfer or --whole-file"
1222                         : "enabled");
1223         }
1224
1225         /* Since we often fill up the outgoing socket and then just sit around
1226          * waiting for the other 2 processes to do their thing, we don't want
1227          * to exit on a timeout.  If the data stops flowing, the receiver will
1228          * notice that and let us know via the redo pipe (or its closing). */
1229         ignore_timeout = 1;
1230
1231         for (i = 0; i < flist->count; i++) {
1232                 struct file_struct *file = flist->files[i];
1233
1234                 if (!file->basename)
1235                         continue;
1236
1237                 recv_generator(local_name ? local_name : f_name_to(file, fbuf),
1238                                file, i, itemizing, maybe_PERMS_REPORT, code,
1239                                f_out);
1240
1241                 /* We need to ensure that any dirs we create have writeable
1242                  * permissions during the time we are putting files within
1243                  * them.  This is then fixed after the transfer is done. */
1244 #ifdef HAVE_CHMOD
1245                 if (!am_root && S_ISDIR(file->mode) && !(file->mode & S_IWUSR)
1246                     && !list_only) {
1247                         int mode = file->mode | S_IWUSR; /* user write */
1248                         char *fname = local_name ? local_name : fbuf;
1249                         if (do_chmod(fname, mode) < 0) {
1250                                 rsyserr(FERROR, errno,
1251                                         "failed to modify permissions on %s",
1252                                         full_fname(fname));
1253                         }
1254                         need_retouch_dir_perms = 1;
1255                 }
1256 #endif
1257
1258                 if (preserve_hard_links)
1259                         check_for_finished_hlinks(itemizing, code);
1260
1261                 if (allowed_lull && !(i % lull_mod))
1262                         maybe_send_keepalive();
1263                 else if (!(i % 200))
1264                         maybe_flush_socket();
1265         }
1266         recv_generator(NULL, NULL, 0, 0, 0, code, -1);
1267         if (delete_during)
1268                 delete_in_dir(NULL, NULL, NULL);
1269
1270         phase++;
1271         csum_length = SUM_LENGTH;
1272         only_existing = max_size = opt_ignore_existing = 0;
1273         update_only = always_checksum = size_only = 0;
1274         ignore_times = 1;
1275         if (append_mode)  /* resend w/o append mode */
1276                 append_mode = -1; /* ... but only longer files */
1277         make_backups = 0; /* avoid a duplicate backup for inplace processing */
1278
1279         if (verbose > 2)
1280                 rprintf(FINFO,"generate_files phase=%d\n",phase);
1281
1282         write_int(f_out, -1);
1283
1284         /* files can cycle through the system more than once
1285          * to catch initial checksum errors */
1286         while ((i = get_redo_num(itemizing, code)) != -1) {
1287                 struct file_struct *file = flist->files[i];
1288                 recv_generator(local_name ? local_name : f_name_to(file, fbuf),
1289                                file, i, itemizing, maybe_PERMS_REPORT, code,
1290                                f_out);
1291         }
1292
1293         phase++;
1294         only_existing = save_only_existing;
1295         opt_ignore_existing = save_opt_ignore_existing;
1296         make_backups = save_make_backups;
1297
1298         if (verbose > 2)
1299                 rprintf(FINFO,"generate_files phase=%d\n",phase);
1300
1301         write_int(f_out, -1);
1302         /* Reduce round-trip lag-time for a useless delay-updates phase. */
1303         if (protocol_version >= 29 && !delay_updates)
1304                 write_int(f_out, -1);
1305
1306         /* Read MSG_DONE for the redo phase (and any prior messages). */
1307         get_redo_num(itemizing, code);
1308
1309         if (protocol_version >= 29) {
1310                 phase++;
1311                 if (verbose > 2)
1312                         rprintf(FINFO, "generate_files phase=%d\n", phase);
1313                 if (delay_updates)
1314                         write_int(f_out, -1);
1315                 /* Read MSG_DONE for delay-updates phase & prior messages. */
1316                 get_redo_num(itemizing, code);
1317         }
1318
1319         do_progress = save_do_progress;
1320         if (delete_after && !local_name && flist->count > 0)
1321                 do_delete_pass(flist);
1322
1323         if ((need_retouch_dir_perms || need_retouch_dir_times)
1324             && !list_only && !local_name && !dry_run) {
1325                 int j = 0;
1326                 /* Now we need to fix any directory permissions that were
1327                  * modified during the transfer and/or re-set any tweaked
1328                  * modified-time values. */
1329                 for (i = 0; i < flist->count; i++) {
1330                         struct file_struct *file = flist->files[i];
1331                         if (!file->basename || !S_ISDIR(file->mode))
1332                                 continue;
1333                         if (!need_retouch_dir_times && file->mode & S_IWUSR)
1334                                 continue;
1335                         recv_generator(f_name(file), file, i, itemizing,
1336                                        maybe_PERMS_REPORT, code, -1);
1337                         if (allowed_lull && !(++j % lull_mod))
1338                                 maybe_send_keepalive();
1339                         else if (!(j % 200))
1340                                 maybe_flush_socket();
1341                 }
1342         }
1343         recv_generator(NULL, NULL, 0, 0, 0, code, -1);
1344
1345         if (max_delete > 0 && deletion_count > max_delete) {
1346                 rprintf(FINFO,
1347                         "Deletions stopped due to --max-delete limit (%d skipped)\n",
1348                         deletion_count - max_delete);
1349                 io_error |= IOERR_DEL_LIMIT;
1350         }
1351
1352         if (verbose > 2)
1353                 rprintf(FINFO,"generate_files finished\n");
1354 }