Handle the new incremental-recursion mode.
[rsync/rsync.git] / generator.c
1 /*
2  * Routines that are exclusive to the generator process.
3  *
4  * Copyright (C) 1996-2000 Andrew Tridgell
5  * Copyright (C) 1996 Paul Mackerras
6  * Copyright (C) 2002 Martin Pool <mbp@samba.org>
7  * Copyright (C) 2003, 2004, 2005, 2006 Wayne Davison
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 along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, 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 stdout_format_has_i;
30 extern int logfile_format_has_i;
31 extern int am_root;
32 extern int am_server;
33 extern int am_daemon;
34 extern int incremental;
35 extern int do_progress;
36 extern int relative_paths;
37 extern int implied_dirs;
38 extern int keep_dirlinks;
39 extern int preserve_links;
40 extern int preserve_devices;
41 extern int preserve_specials;
42 extern int preserve_hard_links;
43 extern int preserve_perms;
44 extern int preserve_uid;
45 extern int preserve_gid;
46 extern int preserve_times;
47 extern int omit_dir_times;
48 extern int delete_mode;
49 extern int delete_before;
50 extern int delete_during;
51 extern int delete_after;
52 extern int done_cnt;
53 extern int ignore_errors;
54 extern int remove_source_files;
55 extern int delay_updates;
56 extern int update_only;
57 extern int ignore_existing;
58 extern int ignore_non_existing;
59 extern int inplace;
60 extern int append_mode;
61 extern int make_backups;
62 extern int csum_length;
63 extern int ignore_times;
64 extern int size_only;
65 extern OFF_T max_size;
66 extern OFF_T min_size;
67 extern int io_error;
68 extern int flist_eof;
69 extern int allowed_lull;
70 extern int sock_f_out;
71 extern int ignore_timeout;
72 extern int protocol_version;
73 extern int file_total;
74 extern int fuzzy_basis;
75 extern int always_checksum;
76 extern int checksum_len;
77 extern char *partial_dir;
78 extern char *basis_dir[];
79 extern int compare_dest;
80 extern int copy_dest;
81 extern int link_dest;
82 extern int whole_file;
83 extern int list_only;
84 extern int new_root_dir;
85 extern int read_batch;
86 extern int safe_symlinks;
87 extern long block_size; /* "long" because popt can't set an int32. */
88 extern int max_delete;
89 extern int force_delete;
90 extern int one_file_system;
91 extern struct stats stats;
92 extern dev_t filesystem_dev;
93 extern char *backup_dir;
94 extern char *backup_suffix;
95 extern int backup_suffix_len;
96 extern struct file_list *cur_flist, *first_flist, *dir_flist;
97 extern struct filter_list_struct server_filter_list;
98
99 int ignore_perishable = 0;
100 int non_perishable_cnt = 0;
101 int maybe_ATTRS_REPORT = 0;
102
103 static dev_t dev_zero;
104 static int deletion_count = 0; /* used to implement --max-delete */
105 static int deldelay_size = 0, deldelay_cnt = 0;
106 static char *deldelay_buf = NULL;
107 static int deldelay_fd = -1;
108 static BOOL solo_file = 0;
109
110 /* For calling delete_item() and delete_dir_contents(). */
111 #define DEL_RECURSE             (1<<1) /* recurse */
112 #define DEL_DIR_IS_EMPTY        (1<<2) /* internal delete_FUNCTIONS use only */
113
114 enum nonregtype {
115     TYPE_DIR, TYPE_SPECIAL, TYPE_DEVICE, TYPE_SYMLINK
116 };
117
118 enum delret {
119     DR_SUCCESS = 0, DR_FAILURE, DR_AT_LIMIT, DR_NOT_EMPTY
120 };
121
122 /* Forward declaration for delete_item(). */
123 static enum delret delete_dir_contents(char *fname, int flags);
124
125
126 static int is_backup_file(char *fn)
127 {
128         int k = strlen(fn) - backup_suffix_len;
129         return k > 0 && strcmp(fn+k, backup_suffix) == 0;
130 }
131
132 /* Delete a file or directory.  If DEL_RECURSE is set in the flags, this will
133  * delete recursively.
134  *
135  * Note that fbuf must point to a MAXPATHLEN buffer if the mode indicates it's
136  * a directory! (The buffer is used for recursion, but returned unchanged.)
137  */
138 static enum delret delete_item(char *fbuf, int mode, char *replace, int flags)
139 {
140         enum delret ret;
141         char *what;
142         int ok;
143
144         if (verbose > 2) {
145                 rprintf(FINFO, "delete_item(%s) mode=%o flags=%d\n",
146                         fbuf, mode, flags);
147         }
148
149         if (S_ISDIR(mode) && !(flags & DEL_DIR_IS_EMPTY)) {
150                 ignore_perishable = 1;
151                 /* If DEL_RECURSE is not set, this just reports emptiness. */
152                 ret = delete_dir_contents(fbuf, flags);
153                 ignore_perishable = 0;
154                 if (ret == DR_NOT_EMPTY || ret == DR_AT_LIMIT)
155                         goto check_ret;
156                 /* OK: try to delete the directory. */
157         }
158
159         if (!replace && max_delete >= 0 && ++deletion_count > max_delete)
160                 return DR_AT_LIMIT;
161
162         if (S_ISDIR(mode)) {
163                 what = "rmdir";
164                 ok = do_rmdir(fbuf) == 0;
165         } else if (make_backups > 0 && (backup_dir || !is_backup_file(fbuf))) {
166                 what = "make_backup";
167                 ok = make_backup(fbuf);
168         } else {
169                 what = "unlink";
170                 ok = robust_unlink(fbuf) == 0;
171         }
172
173         if (ok) {
174                 if (!replace)
175                         log_delete(fbuf, mode);
176                 ret = DR_SUCCESS;
177         } else {
178                 if (S_ISDIR(mode) && errno == ENOTEMPTY) {
179                         rprintf(FINFO, "cannot delete non-empty directory: %s\n",
180                                 fbuf);
181                         ret = DR_NOT_EMPTY;
182                 } else if (errno != ENOENT) {
183                         rsyserr(FERROR, errno, "delete_file: %s(%s) failed",
184                                 what, fbuf);
185                         ret = DR_FAILURE;
186                 } else {
187                         deletion_count--;
188                         ret = DR_SUCCESS;
189                 }
190         }
191
192   check_ret:
193         if (replace && ret != DR_SUCCESS) {
194                 rprintf(FERROR, "could not make way for new %s: %s\n",
195                         replace, fbuf);
196         }
197         return ret;
198 }
199
200 /* The directory is about to be deleted: if DEL_RECURSE is given, delete all
201  * its contents, otherwise just checks for content.  Returns DR_SUCCESS or
202  * DR_NOT_EMPTY.  Note that fname must point to a MAXPATHLEN buffer!  (The
203  * buffer is used for recursion, but returned unchanged.)
204  */
205 static enum delret delete_dir_contents(char *fname, int flags)
206 {
207         struct file_list *dirlist;
208         enum delret ret;
209         unsigned remainder;
210         void *save_filters;
211         int j, dlen;
212         char *p;
213
214         if (verbose > 3) {
215                 rprintf(FINFO, "delete_dir_contents(%s) flags=%d\n",
216                         fname, flags);
217         }
218
219         dlen = strlen(fname);
220         save_filters = push_local_filters(fname, dlen);
221
222         non_perishable_cnt = 0;
223         dirlist = get_dirlist(fname, dlen, 0);
224         ret = non_perishable_cnt ? DR_NOT_EMPTY : DR_SUCCESS;
225
226         if (!dirlist->count)
227                 goto done;
228
229         if (!(flags & DEL_RECURSE)) {
230                 ret = DR_NOT_EMPTY;
231                 goto done;
232         }
233
234         p = fname + dlen;
235         if (dlen != 1 || *fname != '/')
236                 *p++ = '/';
237         remainder = MAXPATHLEN - (p - fname);
238
239         /* We do our own recursion, so make delete_item() non-recursive. */
240         flags = (flags & ~DEL_RECURSE) | DEL_DIR_IS_EMPTY;
241
242         for (j = dirlist->count; j--; ) {
243                 struct file_struct *fp = dirlist->files[j];
244
245                 if (fp->flags & FLAG_MOUNT_DIR) {
246                         if (verbose > 1) {
247                                 rprintf(FINFO,
248                                     "mount point, %s, pins parent directory\n",
249                                     f_name(fp, NULL));
250                         }
251                         ret = DR_NOT_EMPTY;
252                         continue;
253                 }
254
255                 strlcpy(p, fp->basename, remainder);
256                 /* Save stack by recursing to ourself directly. */
257                 if (S_ISDIR(fp->mode)
258                  && delete_dir_contents(fname, flags | DEL_RECURSE) != DR_SUCCESS)
259                         ret = DR_NOT_EMPTY;
260                 if (delete_item(fname, fp->mode, NULL, flags) != DR_SUCCESS)
261                         ret = DR_NOT_EMPTY;
262         }
263
264         fname[dlen] = '\0';
265
266   done:
267         flist_free(dirlist);
268         pop_local_filters(save_filters);
269
270         if (ret == DR_NOT_EMPTY) {
271                 rprintf(FINFO, "cannot delete non-empty directory: %s\n",
272                         fname);
273         }
274         return ret;
275 }
276
277 static int start_delete_delay_temp(void)
278 {
279         char fnametmp[MAXPATHLEN];
280         int save_dry_run = dry_run;
281
282         dry_run = 0;
283         if (!get_tmpname(fnametmp, "deldelay")
284          || (deldelay_fd = do_mkstemp(fnametmp, 0600)) < 0) {
285                 rprintf(FINFO, "NOTE: Unable to create delete-delay temp file--"
286                         "switching to --delete-after.\n");
287                 delete_during = 0;
288                 delete_after = 1;
289                 dry_run = save_dry_run;
290                 return 0;
291         }
292         unlink(fnametmp);
293         dry_run = save_dry_run;
294         return 1;
295 }
296
297 static int flush_delete_delay(void)
298 {
299         if (write(deldelay_fd, deldelay_buf, deldelay_cnt) != deldelay_cnt) {
300                 rsyserr(FERROR, errno, "flush of delete-delay buffer");
301                 delete_during = 0;
302                 delete_after = 1;
303                 close(deldelay_fd);
304                 return 0;
305         }
306         deldelay_cnt = 0;
307         return 1;
308 }
309
310 static int remember_delete(struct file_struct *file, const char *fname)
311 {
312         int len;
313         
314         while (1) {
315                 len = snprintf(deldelay_buf + deldelay_cnt,
316                                deldelay_size - deldelay_cnt,
317                                "%x %s%c", (int)file->mode, fname, '\0');
318                 if ((deldelay_cnt += len) <= deldelay_size)
319                         break;
320                 if (deldelay_fd < 0 && !start_delete_delay_temp())
321                         return 0;
322                 deldelay_cnt -= len;
323                 if (!flush_delete_delay())
324                         return 0;
325         }
326
327         return 1;
328 }
329
330 static int read_delay_line(char *buf)
331 {
332         static int read_pos = 0;
333         int j, len, mode;
334         char *bp, *past_space;
335
336         while (1) {
337                 for (j = read_pos; j < deldelay_cnt && deldelay_buf[j]; j++) {}
338                 if (j < deldelay_cnt)
339                         break;
340                 if (deldelay_fd < 0) {
341                         if (j > read_pos)
342                                 goto invalid_data;
343                         return -1;
344                 }
345                 deldelay_cnt -= read_pos;
346                 if (deldelay_cnt == deldelay_size)
347                         goto invalid_data;
348                 if (deldelay_cnt && read_pos) {
349                         memmove(deldelay_buf, deldelay_buf + read_pos,
350                                 deldelay_cnt);
351                 }
352                 len = read(deldelay_fd, deldelay_buf + deldelay_cnt,
353                            deldelay_size - deldelay_cnt);
354                 if (len == 0) {
355                         if (deldelay_cnt) {
356                                 rprintf(FERROR,
357                                     "ERROR: unexpected EOF in delete-delay file.\n");
358                         }
359                         return -1;
360                 }
361                 if (len < 0) {
362                         rsyserr(FERROR, errno,
363                                 "reading delete-delay file");
364                         return -1;
365                 }
366                 deldelay_cnt += len;
367                 read_pos = 0;
368         }
369
370         bp = deldelay_buf + read_pos;
371
372         if (sscanf(bp, "%x ", &mode) != 1) {
373           invalid_data:
374                 rprintf(FERROR, "ERROR: invalid data in delete-delay file.\n");
375                 return -1;
376         }
377         past_space = strchr(bp, ' ') + 1;
378         len = j - read_pos - (past_space - bp) + 1; /* count the '\0' */
379         read_pos = j + 1;
380
381         if (len > MAXPATHLEN) {
382                 rprintf(FERROR, "ERROR: filename too long in delete-delay file.\n");
383                 return -1;
384         }
385
386         /* The caller needs the name in a MAXPATHLEN buffer, so we copy it
387          * instead of returning a pointer to our buffer. */
388         memcpy(buf, past_space, len);
389
390         return mode;
391 }
392
393 static void do_delayed_deletions(char *delbuf)
394 {
395         int mode;
396
397         if (deldelay_fd >= 0) {
398                 if (deldelay_cnt && !flush_delete_delay())
399                         return;
400                 lseek(deldelay_fd, 0, 0);
401         }
402         while ((mode = read_delay_line(delbuf)) >= 0)
403                 delete_item(delbuf, mode, NULL, DEL_RECURSE);
404         if (deldelay_fd >= 0)
405                 close(deldelay_fd);
406 }
407
408 /* This function is used to implement per-directory deletion, and is used by
409  * all the --delete-WHEN options.  Note that the fbuf pointer must point to a
410  * MAXPATHLEN buffer with the name of the directory in it (the functions we
411  * call will append names onto the end, but the old dir value will be restored
412  * on exit). */
413 static void delete_in_dir(struct file_list *flist, char *fbuf,
414                           struct file_struct *file, dev_t *fs_dev)
415 {
416         static int already_warned = 0;
417         struct file_list *dirlist;
418         char delbuf[MAXPATHLEN];
419         int dlen, i;
420
421         if (!flist) {
422                 change_local_filter_dir(NULL, 0, 0);
423                 return;
424         }
425
426         if (verbose > 2)
427                 rprintf(FINFO, "delete_in_dir(%s)\n", fbuf);
428
429         if (allowed_lull)
430                 maybe_send_keepalive();
431
432         if (io_error && !ignore_errors) {
433                 if (already_warned)
434                         return;
435                 rprintf(FINFO,
436                         "IO error encountered -- skipping file deletion\n");
437                 already_warned = 1;
438                 return;
439         }
440
441         dlen = strlen(fbuf);
442         change_local_filter_dir(fbuf, dlen, F_DEPTH(file));
443
444         if (one_file_system) {
445                 if (file->flags & FLAG_TOP_DIR)
446                         filesystem_dev = *fs_dev;
447                 else if (filesystem_dev != *fs_dev)
448                         return;
449         }
450
451         dirlist = get_dirlist(fbuf, dlen, 0);
452
453         /* If an item in dirlist is not found in flist, delete it
454          * from the filesystem. */
455         for (i = dirlist->count; i--; ) {
456                 struct file_struct *fp = dirlist->files[i];
457                 if (!F_IS_ACTIVE(fp))
458                         continue;
459                 if (fp->flags & FLAG_MOUNT_DIR) {
460                         if (verbose > 1)
461                                 rprintf(FINFO, "cannot delete mount point: %s\n",
462                                         f_name(fp, NULL));
463                         continue;
464                 }
465                 if (flist_find(flist, fp) < 0) {
466                         f_name(fp, delbuf);
467                         if (delete_during == 2) {
468                                 if (!remember_delete(fp, delbuf))
469                                         break;
470                         } else
471                                 delete_item(delbuf, fp->mode, NULL, DEL_RECURSE);
472                 }
473         }
474
475         flist_free(dirlist);
476 }
477
478 /* This deletes any files on the receiving side that are not present on the
479  * sending side.  This is used by --delete-before and --delete-after. */
480 static void do_delete_pass(struct file_list *flist)
481 {
482         char fbuf[MAXPATHLEN];
483         STRUCT_STAT st;
484         int j;
485
486         /* dry_run is incremented when the destination doesn't exist yet. */
487         if (dry_run > 1 || list_only)
488                 return;
489
490         for (j = 0; j < flist->count; j++) {
491                 struct file_struct *file = flist->files[j];
492
493                 if (!(file->flags & FLAG_XFER_DIR))
494                         continue;
495
496                 f_name(file, fbuf);
497                 if (verbose > 1 && file->flags & FLAG_TOP_DIR)
498                         rprintf(FINFO, "deleting in %s\n", fbuf);
499
500                 if (link_stat(fbuf, &st, keep_dirlinks) < 0
501                  || !S_ISDIR(st.st_mode))
502                         continue;
503
504                 delete_in_dir(flist, fbuf, file, &st.st_dev);
505         }
506         delete_in_dir(NULL, NULL, NULL, &dev_zero);
507
508         if (do_progress && !am_server)
509                 rprintf(FINFO, "                    \r");
510 }
511
512 int unchanged_attrs(struct file_struct *file, STRUCT_STAT *st)
513 {
514         if (preserve_perms && !BITS_EQUAL(st->st_mode, file->mode, CHMOD_BITS))
515                 return 0;
516
517         if (am_root && preserve_uid && st->st_uid != F_UID(file))
518                 return 0;
519
520         if (preserve_gid && F_GID(file) != GID_NONE && st->st_gid != F_GID(file))
521                 return 0;
522
523         return 1;
524 }
525
526 void itemize(struct file_struct *file, int ndx, int statret,
527              STRUCT_STAT *st, int32 iflags, uchar fnamecmp_type,
528              const char *xname)
529 {
530         if (statret >= 0) { /* A from-dest-dir statret can == 1! */
531                 int keep_time = !preserve_times ? 0
532                     : S_ISDIR(file->mode) ? !omit_dir_times
533                     : !S_ISLNK(file->mode);
534
535                 if (S_ISREG(file->mode) && F_LENGTH(file) != st->st_size)
536                         iflags |= ITEM_REPORT_SIZE;
537                 if ((iflags & (ITEM_TRANSFER|ITEM_LOCAL_CHANGE) && !keep_time
538                   && !(iflags & ITEM_MATCHED)
539                   && (!(iflags & ITEM_XNAME_FOLLOWS) || *xname))
540                  || (keep_time && cmp_time(file->modtime, st->st_mtime) != 0))
541                         iflags |= ITEM_REPORT_TIME;
542                 if (!BITS_EQUAL(st->st_mode, file->mode, CHMOD_BITS))
543                         iflags |= ITEM_REPORT_PERMS;
544                 if (preserve_uid && am_root && F_UID(file) != st->st_uid)
545                         iflags |= ITEM_REPORT_OWNER;
546                 if (preserve_gid && F_GID(file) != GID_NONE
547                     && st->st_gid != F_GID(file))
548                         iflags |= ITEM_REPORT_GROUP;
549         } else
550                 iflags |= ITEM_IS_NEW;
551
552         iflags &= 0xffff;
553         if ((iflags & SIGNIFICANT_ITEM_FLAGS || verbose > 1
554           || stdout_format_has_i > 1 || (xname && *xname)) && !read_batch) {
555                 if (protocol_version >= 29) {
556                         if (ndx >= 0)
557                                 write_int(sock_f_out, ndx + cur_flist->ndx_start);
558                         write_shortint(sock_f_out, iflags);
559                         if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
560                                 write_byte(sock_f_out, fnamecmp_type);
561                         if (iflags & ITEM_XNAME_FOLLOWS)
562                                 write_vstring(sock_f_out, xname, strlen(xname));
563                 } else if (ndx >= 0) {
564                         enum logcode code = logfile_format_has_i ? FINFO : FCLIENT;
565                         log_item(code, file, &stats, iflags, xname);
566                 }
567         }
568 }
569
570
571 /* Perform our quick-check heuristic for determining if a file is unchanged. */
572 int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st)
573 {
574         if (st->st_size != F_LENGTH(file))
575                 return 0;
576
577         /* if always checksum is set then we use the checksum instead
578            of the file time to determine whether to sync */
579         if (always_checksum > 0 && S_ISREG(st->st_mode)) {
580                 char sum[MD4_SUM_LENGTH];
581                 file_checksum(fn, sum, st->st_size);
582                 return memcmp(sum, F_SUM(file), checksum_len) == 0;
583         }
584
585         if (size_only > 0)
586                 return 1;
587
588         if (ignore_times)
589                 return 0;
590
591         return cmp_time(st->st_mtime, file->modtime) == 0;
592 }
593
594
595 /*
596  * set (initialize) the size entries in the per-file sum_struct
597  * calculating dynamic block and checksum sizes.
598  *
599  * This is only called from generate_and_send_sums() but is a separate
600  * function to encapsulate the logic.
601  *
602  * The block size is a rounded square root of file length.
603  *
604  * The checksum size is determined according to:
605  *     blocksum_bits = BLOCKSUM_BIAS + 2*log2(file_len) - log2(block_len)
606  * provided by Donovan Baarda which gives a probability of rsync
607  * algorithm corrupting data and falling back using the whole md4
608  * checksums.
609  *
610  * This might be made one of several selectable heuristics.
611  */
612 static void sum_sizes_sqroot(struct sum_struct *sum, int64 len)
613 {
614         int32 blength;
615         int s2length;
616
617         if (block_size)
618                 blength = block_size;
619         else if (len <= BLOCK_SIZE * BLOCK_SIZE)
620                 blength = BLOCK_SIZE;
621         else {
622                 int32 c;
623                 int64 l;
624                 int cnt;
625                 for (c = 1, l = len, cnt = 0; l >>= 2; c <<= 1, cnt++) {}
626                 if (cnt >= 31 || c >= MAX_BLOCK_SIZE)
627                         blength = MAX_BLOCK_SIZE;
628                 else {
629                     blength = 0;
630                     do {
631                             blength |= c;
632                             if (len < (int64)blength * blength)
633                                     blength &= ~c;
634                             c >>= 1;
635                     } while (c >= 8);   /* round to multiple of 8 */
636                     blength = MAX(blength, BLOCK_SIZE);
637                 }
638         }
639
640         if (protocol_version < 27) {
641                 s2length = csum_length;
642         } else if (csum_length == SUM_LENGTH) {
643                 s2length = SUM_LENGTH;
644         } else {
645                 int32 c;
646                 int64 l;
647                 int b = BLOCKSUM_BIAS;
648                 for (l = len; l >>= 1; b += 2) {}
649                 for (c = blength; (c >>= 1) && b; b--) {}
650                 /* add a bit, subtract rollsum, round up. */
651                 s2length = (b + 1 - 32 + 7) / 8; /* --optimize in compiler-- */
652                 s2length = MAX(s2length, csum_length);
653                 s2length = MIN(s2length, SUM_LENGTH);
654         }
655
656         sum->flength    = len;
657         sum->blength    = blength;
658         sum->s2length   = s2length;
659         sum->remainder  = (int32)(len % blength);
660         sum->count      = (int32)(len / blength) + (sum->remainder != 0);
661
662         if (sum->count && verbose > 2) {
663                 rprintf(FINFO,
664                         "count=%.0f rem=%ld blength=%ld s2length=%d flength=%.0f\n",
665                         (double)sum->count, (long)sum->remainder, (long)sum->blength,
666                         sum->s2length, (double)sum->flength);
667         }
668 }
669
670
671 /*
672  * Generate and send a stream of signatures/checksums that describe a buffer
673  *
674  * Generate approximately one checksum every block_len bytes.
675  */
676 static void generate_and_send_sums(int fd, OFF_T len, int f_out, int f_copy)
677 {
678         int32 i;
679         struct map_struct *mapbuf;
680         struct sum_struct sum;
681         OFF_T offset = 0;
682
683         sum_sizes_sqroot(&sum, len);
684         write_sum_head(f_out, &sum);
685
686         if (append_mode > 0 && f_copy < 0)
687                 return;
688
689         if (len > 0)
690                 mapbuf = map_file(fd, len, MAX_MAP_SIZE, sum.blength);
691         else
692                 mapbuf = NULL;
693
694         for (i = 0; i < sum.count; i++) {
695                 int32 n1 = (int32)MIN(len, (OFF_T)sum.blength);
696                 char *map = map_ptr(mapbuf, offset, n1);
697                 char sum2[SUM_LENGTH];
698                 uint32 sum1;
699
700                 len -= n1;
701                 offset += n1;
702
703                 if (f_copy >= 0) {
704                         full_write(f_copy, map, n1);
705                         if (append_mode > 0)
706                                 continue;
707                 }
708
709                 sum1 = get_checksum1(map, n1);
710                 get_checksum2(map, n1, sum2);
711
712                 if (verbose > 3) {
713                         rprintf(FINFO,
714                                 "chunk[%.0f] offset=%.0f len=%ld sum1=%08lx\n",
715                                 (double)i, (double)offset - n1, (long)n1,
716                                 (unsigned long)sum1);
717                 }
718                 write_int(f_out, sum1);
719                 write_buf(f_out, sum2, sum.s2length);
720         }
721
722         if (mapbuf)
723                 unmap_file(mapbuf);
724 }
725
726
727 /* Try to find a filename in the same dir as "fname" with a similar name. */
728 static int find_fuzzy(struct file_struct *file, struct file_list *dirlist)
729 {
730         int fname_len, fname_suf_len;
731         const char *fname_suf, *fname = file->basename;
732         uint32 lowest_dist = 25 << 16; /* ignore a distance greater than 25 */
733         int j, lowest_j = -1;
734
735         fname_len = strlen(fname);
736         fname_suf = find_filename_suffix(fname, fname_len, &fname_suf_len);
737
738         for (j = 0; j < dirlist->count; j++) {
739                 struct file_struct *fp = dirlist->files[j];
740                 const char *suf, *name;
741                 int len, suf_len;
742                 uint32 dist;
743
744                 if (!S_ISREG(fp->mode) || !F_LENGTH(fp)
745                  || fp->flags & FLAG_FILE_SENT)
746                         continue;
747
748                 name = fp->basename;
749
750                 if (F_LENGTH(fp) == F_LENGTH(file)
751                     && cmp_time(fp->modtime, file->modtime) == 0) {
752                         if (verbose > 4) {
753                                 rprintf(FINFO,
754                                         "fuzzy size/modtime match for %s\n",
755                                         name);
756                         }
757                         return j;
758                 }
759
760                 len = strlen(name);
761                 suf = find_filename_suffix(name, len, &suf_len);
762
763                 dist = fuzzy_distance(name, len, fname, fname_len);
764                 /* Add some extra weight to how well the suffixes match. */
765                 dist += fuzzy_distance(suf, suf_len, fname_suf, fname_suf_len)
766                       * 10;
767                 if (verbose > 4) {
768                         rprintf(FINFO, "fuzzy distance for %s = %d.%05d\n",
769                                 name, (int)(dist>>16), (int)(dist&0xFFFF));
770                 }
771                 if (dist <= lowest_dist) {
772                         lowest_dist = dist;
773                         lowest_j = j;
774                 }
775         }
776
777         return lowest_j;
778 }
779
780 #ifdef SUPPORT_HARD_LINKS
781 void check_for_finished_hlinks(int itemizing, enum logcode code)
782 {
783         struct file_struct *file;
784         struct file_list *flist;
785         char fbuf[MAXPATHLEN];
786         int ndx;
787
788         while ((ndx = get_hlink_num()) != -1) {
789                 flist = flist_for_ndx(ndx);
790                 assert(flist != NULL);
791                 file = flist->files[ndx];
792                 assert(file->flags & FLAG_HLINKED);
793                 finish_hard_link(file, f_name(file, fbuf), NULL, itemizing, code, -1);
794         }
795 }
796 #endif
797
798 /* This is only called for regular files.  We return -2 if we've finished
799  * handling the file, -1 if no dest-linking occurred, or a non-negative
800  * value if we found an alternate basis file. */
801 static int try_dests_reg(struct file_struct *file, char *fname, int ndx,
802                          char *cmpbuf, STRUCT_STAT *stp, int itemizing,
803                          enum logcode code)
804 {
805         int best_match = -1;
806         int match_level = 0;
807         int j = 0;
808
809         do {
810                 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
811                 if (link_stat(cmpbuf, stp, 0) < 0 || !S_ISREG(stp->st_mode))
812                         continue;
813                 switch (match_level) {
814                 case 0:
815                         best_match = j;
816                         match_level = 1;
817                         /* FALL THROUGH */
818                 case 1:
819                         if (!unchanged_file(cmpbuf, file, stp))
820                                 continue;
821                         best_match = j;
822                         match_level = 2;
823                         /* FALL THROUGH */
824                 case 2:
825                         if (!unchanged_attrs(file, stp))
826                                 continue;
827                         if (always_checksum > 0 && preserve_times
828                          && cmp_time(stp->st_mtime, file->modtime))
829                                 continue;
830                         best_match = j;
831                         match_level = 3;
832                         break;
833                 }
834                 break;
835         } while (basis_dir[++j] != NULL);
836
837         if (!match_level)
838                 return -1;
839
840         if (j != best_match) {
841                 j = best_match;
842                 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
843                 if (link_stat(cmpbuf, stp, 0) < 0)
844                         return -1;
845         }
846
847         if (match_level == 3 && !copy_dest) {
848 #ifdef SUPPORT_HARD_LINKS
849                 if (link_dest) {
850                         if (!hard_link_one(file, fname, cmpbuf, 1))
851                                 goto try_a_copy;
852                         if (preserve_hard_links && F_IS_HLINKED(file))
853                                 finish_hard_link(file, fname, stp, itemizing, code, j);
854                         if (itemizing && (verbose > 1 || stdout_format_has_i > 1)) {
855                                 itemize(file, ndx, 1, stp,
856                                         ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
857                                         0, "");
858                         }
859                 } else
860 #endif
861                 if (itemizing)
862                         itemize(file, ndx, 0, stp, 0, 0, NULL);
863                 if (verbose > 1 && maybe_ATTRS_REPORT)
864                         rprintf(FCLIENT, "%s is uptodate\n", fname);
865                 return -2;
866         }
867
868         if (match_level >= 2) {
869 #ifdef SUPPORT_HARD_LINKS
870           try_a_copy: /* Copy the file locally. */
871 #endif
872                 if (copy_file(cmpbuf, fname, file->mode) < 0) {
873                         if (verbose) {
874                                 rsyserr(FINFO, errno, "copy_file %s => %s",
875                                         full_fname(cmpbuf), fname);
876                         }
877                         return -1;
878                 }
879                 if (itemizing)
880                         itemize(file, ndx, 0, stp, ITEM_LOCAL_CHANGE, 0, NULL);
881                 set_file_attrs(fname, file, NULL, 0);
882                 if (maybe_ATTRS_REPORT
883                  && ((!itemizing && verbose && match_level == 2)
884                   || (verbose > 1 && match_level == 3))) {
885                         code = match_level == 3 ? FCLIENT : FINFO;
886                         rprintf(code, "%s%s\n", fname,
887                                 match_level == 3 ? " is uptodate" : "");
888                 }
889 #ifdef SUPPORT_HARD_LINKS
890                 if (preserve_hard_links && F_IS_HLINKED(file))
891                         finish_hard_link(file, fname, stp, itemizing, code, -1);
892 #endif
893                 return -2;
894         }
895
896         return FNAMECMP_BASIS_DIR_LOW + j;
897 }
898
899 /* This is only called for non-regular files.  We return -2 if we've finished
900  * handling the file, or -1 if no dest-linking occurred, or a non-negative
901  * value if we found an alternate basis file. */
902 static int try_dests_non(struct file_struct *file, char *fname, int ndx,
903                          char *cmpbuf, STRUCT_STAT *stp, int itemizing,
904                          enum logcode code)
905 {
906         char lnk[MAXPATHLEN];
907         int best_match = -1;
908         int match_level = 0;
909         enum nonregtype type;
910         uint32 *devp;
911         int len, j = 0;
912
913 #ifndef SUPPORT_LINKS
914         if (S_ISLNK(file->mode))
915                 return -1;
916 #endif
917         if (S_ISDIR(file->mode)) {
918                 type = TYPE_DIR;
919         } else if (IS_SPECIAL(file->mode))
920                 type = TYPE_SPECIAL;
921         else if (IS_DEVICE(file->mode))
922                 type = TYPE_DEVICE;
923 #ifdef SUPPORT_LINKS
924         else if (S_ISLNK(file->mode))
925                 type = TYPE_SYMLINK;
926 #endif
927         else {
928                 rprintf(FERROR,
929                         "internal: try_dests_non() called with invalid mode (%o)\n",
930                         (int)file->mode);
931                 exit_cleanup(RERR_UNSUPPORTED);
932         }
933
934         do {
935                 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
936                 if (link_stat(cmpbuf, stp, 0) < 0)
937                         continue;
938                 switch (type) {
939                 case TYPE_DIR:
940                         if (!S_ISDIR(stp->st_mode))
941                                 continue;
942                         break;
943                 case TYPE_SPECIAL:
944                         if (!IS_SPECIAL(stp->st_mode))
945                                 continue;
946                         break;
947                 case TYPE_DEVICE:
948                         if (!IS_DEVICE(stp->st_mode))
949                                 continue;
950                         break;
951 #ifdef SUPPORT_LINKS
952                 case TYPE_SYMLINK:
953                         if (!S_ISLNK(stp->st_mode))
954                                 continue;
955                         break;
956 #endif
957                 }
958                 if (match_level < 1) {
959                         match_level = 1;
960                         best_match = j;
961                 }
962                 switch (type) {
963                 case TYPE_DIR:
964                         break;
965                 case TYPE_SPECIAL:
966                 case TYPE_DEVICE:
967                         devp = F_RDEV_P(file);
968                         if (stp->st_rdev != MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp)))
969                                 continue;
970                         break;
971 #ifdef SUPPORT_LINKS
972                 case TYPE_SYMLINK:
973                         if ((len = readlink(cmpbuf, lnk, MAXPATHLEN-1)) <= 0)
974                                 continue;
975                         lnk[len] = '\0';
976                         if (strcmp(lnk, F_SYMLINK(file)) != 0)
977                                 continue;
978                         break;
979 #endif
980                 }
981                 if (match_level < 2) {
982                         match_level = 2;
983                         best_match = j;
984                 }
985                 if (unchanged_attrs(file, stp)) {
986                         match_level = 3;
987                         best_match = j;
988                         break;
989                 }
990         } while (basis_dir[++j] != NULL);
991
992         if (!match_level)
993                 return -1;
994
995         if (j != best_match) {
996                 j = best_match;
997                 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
998                 if (link_stat(cmpbuf, stp, 0) < 0)
999                         return -1;
1000         }
1001
1002         if (match_level == 3) {
1003 #ifdef SUPPORT_HARD_LINKS
1004                 if (link_dest
1005 #ifndef CAN_HARDLINK_SYMLINK
1006                  && !S_ISLNK(file->mode)
1007 #endif
1008 #ifndef CAN_HARDLINK_SPECIAL
1009                  && !IS_SPECIAL(file->mode) && !IS_DEVICE(file->mode)
1010 #endif
1011                  && !S_ISDIR(file->mode)) {
1012                         if (do_link(cmpbuf, fname) < 0) {
1013                                 rsyserr(FERROR, errno,
1014                                         "failed to hard-link %s with %s",
1015                                         cmpbuf, fname);
1016                                 return j;
1017                         }
1018                         if (preserve_hard_links && F_IS_HLINKED(file))
1019                                 finish_hard_link(file, fname, NULL, itemizing, code, -1);
1020                 } else
1021 #endif
1022                         match_level = 2;
1023                 if (itemizing && stdout_format_has_i
1024                  && (verbose > 1 || stdout_format_has_i > 1)) {
1025                         int chg = compare_dest && type != TYPE_DIR ? 0
1026                             : ITEM_LOCAL_CHANGE
1027                              + (match_level == 3 ? ITEM_XNAME_FOLLOWS : 0);
1028                         char *lp = match_level == 3 ? "" : NULL;
1029                         itemize(file, ndx, 0, stp, chg + ITEM_MATCHED, 0, lp);
1030                 }
1031                 if (verbose > 1 && maybe_ATTRS_REPORT) {
1032                         rprintf(FCLIENT, "%s%s is uptodate\n",
1033                                 fname, type == TYPE_DIR ? "/" : "");
1034                 }
1035                 return -2;
1036         }
1037
1038         return j;
1039 }
1040
1041 static int phase = 0;
1042
1043 /* Acts on cur_flist->file's ndx'th item, whose name is fname.  If a dir,
1044  * make sure it exists, and has the right permissions/timestamp info.  For
1045  * all other non-regular files (symlinks, etc.) we create them here.  For
1046  * regular files that have changed, we try to find a basis file and then
1047  * start sending checksums.
1048  *
1049  * When fname is non-null, it must point to a MAXPATHLEN buffer!
1050  *
1051  * Note that f_out is set to -1 when doing final directory-permission and
1052  * modification-time repair. */
1053 static void recv_generator(char *fname, struct file_struct *file, int ndx,
1054                            int itemizing, enum logcode code, int f_out)
1055 {
1056         static int missing_below = -1, excluded_below = -1;
1057         static const char *parent_dirname = "";
1058         static struct file_list *fuzzy_dirlist = NULL;
1059         static int need_fuzzy_dirlist = 0;
1060         struct file_struct *fuzzy_file = NULL;
1061         int fd = -1, f_copy = -1;
1062         STRUCT_STAT st, real_st, partial_st;
1063         struct file_struct *back_file = NULL;
1064         int statret, real_ret, stat_errno;
1065         char *fnamecmp, *partialptr, *backupptr = NULL;
1066         char fnamecmpbuf[MAXPATHLEN];
1067         uchar fnamecmp_type;
1068         int del_opts = delete_mode || force_delete ? DEL_RECURSE : 0;
1069
1070         if (list_only)
1071                 return;
1072
1073         if (verbose > 2)
1074                 rprintf(FINFO, "recv_generator(%s,%d)\n", fname, ndx);
1075
1076         if (server_filter_list.head) {
1077                 if (excluded_below >= 0) {
1078                         if (F_DEPTH(file) > excluded_below)
1079                                 goto skipping;
1080                         excluded_below = -1;
1081                 }
1082                 if (check_filter(&server_filter_list, fname,
1083                                  S_ISDIR(file->mode)) < 0) {
1084                         if (S_ISDIR(file->mode))
1085                                 excluded_below = F_DEPTH(file);
1086                   skipping:
1087                         if (verbose) {
1088                                 rprintf(FINFO,
1089                                         "skipping server-excluded file \"%s\"\n",
1090                                         fname);
1091                         }
1092                         return;
1093                 }
1094         }
1095
1096         if (missing_below >= 0) {
1097                 if (F_DEPTH(file) <= missing_below) {
1098                         if (dry_run)
1099                                 dry_run--;
1100                         missing_below = -1;
1101                 } else if (!dry_run) {
1102                         if (S_ISDIR(file->mode))
1103                                 file->flags |= FLAG_MISSING_DIR;
1104                         return;
1105                 }
1106         }
1107         if (dry_run > 1) {
1108                 if (fuzzy_dirlist) {
1109                         flist_free(fuzzy_dirlist);
1110                         fuzzy_dirlist = NULL;
1111                 }
1112                 parent_dirname = "";
1113                 statret = -1;
1114                 stat_errno = ENOENT;
1115         } else {
1116                 const char *dn = file->dirname ? file->dirname : ".";
1117                 if (parent_dirname != dn && strcmp(parent_dirname, dn) != 0) {
1118                         if (relative_paths && !implied_dirs
1119                          && do_stat(dn, &st) < 0
1120                          && create_directory_path(fname) < 0) {
1121                                 rsyserr(FERROR, errno,
1122                                         "recv_generator: mkdir %s failed",
1123                                         full_fname(dn));
1124                         }
1125                         if (fuzzy_dirlist) {
1126                                 flist_free(fuzzy_dirlist);
1127                                 fuzzy_dirlist = NULL;
1128                         }
1129                         if (fuzzy_basis)
1130                                 need_fuzzy_dirlist = 1;
1131                 }
1132                 parent_dirname = dn;
1133
1134                 if (need_fuzzy_dirlist && S_ISREG(file->mode)) {
1135                         strlcpy(fnamecmpbuf, dn, sizeof fnamecmpbuf);
1136                         fuzzy_dirlist = get_dirlist(fnamecmpbuf, -1, 1);
1137                         need_fuzzy_dirlist = 0;
1138                 }
1139
1140                 statret = link_stat(fname, &st,
1141                                     keep_dirlinks && S_ISDIR(file->mode));
1142                 stat_errno = errno;
1143         }
1144
1145         if (ignore_non_existing > 0 && statret == -1 && stat_errno == ENOENT) {
1146                 if (verbose > 1) {
1147                         rprintf(FINFO, "not creating new %s \"%s\"\n",
1148                                 S_ISDIR(file->mode) ? "directory" : "file",
1149                                 fname);
1150                 }
1151                 if (S_ISDIR(file->mode)) {
1152                         if (missing_below < 0) {
1153                                 if (dry_run)
1154                                         dry_run++;
1155                                 missing_below = F_DEPTH(file);
1156                         }
1157                         file->flags |= FLAG_MISSING_DIR;
1158                 }
1159                 return;
1160         }
1161
1162         /* If we're not preserving permissions, change the file-list's
1163          * mode based on the local permissions and some heuristics. */
1164         if (!preserve_perms) {
1165                 int exists = statret == 0
1166                           && S_ISDIR(st.st_mode) == S_ISDIR(file->mode);
1167                 file->mode = dest_mode(file->mode, st.st_mode, exists);
1168         }
1169
1170         if (S_ISDIR(file->mode)) {
1171                 /* The file to be received is a directory, so we need
1172                  * to prepare appropriately.  If there is already a
1173                  * file of that name and it is *not* a directory, then
1174                  * we need to delete it.  If it doesn't exist, then
1175                  * (perhaps recursively) create it. */
1176                 if (statret == 0 && !S_ISDIR(st.st_mode)) {
1177                         if (delete_item(fname, st.st_mode, "directory", del_opts) != 0)
1178                                 return;
1179                         statret = -1;
1180                 }
1181                 if (dry_run && statret != 0 && missing_below < 0) {
1182                         missing_below = F_DEPTH(file);
1183                         dry_run++;
1184                 }
1185                 real_ret = statret;
1186                 real_st = st;
1187                 if (new_root_dir) {
1188                         if (*fname == '.' && fname[1] == '\0')
1189                                 statret = -1;
1190                         new_root_dir = 0;
1191                 }
1192                 if (statret != 0 && basis_dir[0] != NULL) {
1193                         int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &st,
1194                                               itemizing, code);
1195                         if (j == -2) {
1196                                 itemizing = 0;
1197                                 code = FNONE;
1198                         } else if (j >= 0)
1199                                 statret = 1;
1200                 }
1201                 if (itemizing && f_out != -1) {
1202                         itemize(file, ndx, statret, &st,
1203                                 statret ? ITEM_LOCAL_CHANGE : 0, 0, NULL);
1204                 }
1205                 if (real_ret != 0 && do_mkdir(fname,file->mode) < 0 && errno != EEXIST) {
1206                         if (!relative_paths || errno != ENOENT
1207                             || create_directory_path(fname) < 0
1208                             || (do_mkdir(fname, file->mode) < 0 && errno != EEXIST)) {
1209                                 rsyserr(FERROR, errno,
1210                                         "recv_generator: mkdir %s failed",
1211                                         full_fname(fname));
1212                                 rprintf(FERROR,
1213                                     "*** Skipping any contents from this failed directory ***\n");
1214                                 missing_below = F_DEPTH(file);
1215                                 file->flags |= FLAG_MISSING_DIR;
1216                                 return;
1217                         }
1218                 }
1219                 if (set_file_attrs(fname, file, real_ret ? NULL : &real_st, 0)
1220                     && verbose && code != FNONE && f_out != -1)
1221                         rprintf(code, "%s/\n", fname);
1222                 if (real_ret != 0 && one_file_system)
1223                         real_st.st_dev = filesystem_dev;
1224                 if (incremental) {
1225                         if (one_file_system) {
1226                                 uint32 *devp = F_DIRDEV_P(file);
1227                                 DEV_MAJOR(devp) = major(real_st.st_dev);
1228                                 DEV_MINOR(devp) = minor(real_st.st_dev);
1229                         }
1230                 }
1231                 else if (delete_during && f_out != -1 && !phase && dry_run < 2
1232                     && (file->flags & FLAG_XFER_DIR))
1233                         delete_in_dir(cur_flist, fname, file, &real_st.st_dev);
1234                 return;
1235         }
1236
1237 #ifdef SUPPORT_HARD_LINKS
1238         if (preserve_hard_links && F_HLINK_NOT_FIRST(file)
1239          && hard_link_check(file, ndx, fname, statret, &st, itemizing, code))
1240                 return;
1241 #endif
1242
1243         if (preserve_links && S_ISLNK(file->mode)) {
1244 #ifdef SUPPORT_LINKS
1245                 const char *sl = F_SYMLINK(file);
1246                 if (safe_symlinks && unsafe_symlink(sl, fname)) {
1247                         if (verbose) {
1248                                 if (solo_file)
1249                                         fname = f_name(file, NULL);
1250                                 rprintf(FINFO,
1251                                         "ignoring unsafe symlink %s -> \"%s\"\n",
1252                                         full_fname(fname), sl);
1253                         }
1254                         return;
1255                 }
1256                 if (statret == 0) {
1257                         char lnk[MAXPATHLEN];
1258                         int len;
1259
1260                         if (!S_ISLNK(st.st_mode))
1261                                 statret = -1;
1262                         else if ((len = readlink(fname, lnk, MAXPATHLEN-1)) > 0
1263                               && strncmp(lnk, sl, len) == 0 && sl[len] == '\0') {
1264                                 /* The link is pointing to the right place. */
1265                                 if (itemizing)
1266                                         itemize(file, ndx, 0, &st, 0, 0, NULL);
1267                                 set_file_attrs(fname, file, &st, maybe_ATTRS_REPORT);
1268 #ifdef SUPPORT_HARD_LINKS
1269                                 if (preserve_hard_links && F_IS_HLINKED(file))
1270                                         finish_hard_link(file, fname, &st, itemizing, code, -1);
1271 #endif
1272                                 if (remove_source_files == 1)
1273                                         goto return_with_success;
1274                                 return;
1275                         }
1276                         /* Not the right symlink (or not a symlink), so
1277                          * delete it. */
1278                         if (delete_item(fname, st.st_mode, "symlink", del_opts) != 0)
1279                                 return;
1280                 } else if (basis_dir[0] != NULL) {
1281                         int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &st,
1282                                               itemizing, code);
1283                         if (j == -2) {
1284 #ifndef CAN_HARDLINK_SYMLINK
1285                                 if (link_dest) {
1286                                         /* Resort to --copy-dest behavior. */
1287                                 } else
1288 #endif
1289                                 if (!copy_dest)
1290                                         return;
1291                                 itemizing = 0;
1292                                 code = FNONE;
1293                         } else if (j >= 0)
1294                                 statret = 1;
1295                 }
1296 #ifdef SUPPORT_HARD_LINKS
1297                 if (preserve_hard_links && F_HLINK_NOT_LAST(file))
1298                         return;
1299 #endif
1300                 if (do_symlink(sl, fname) != 0) {
1301                         rsyserr(FERROR, errno, "symlink %s -> \"%s\" failed",
1302                                 full_fname(fname), sl);
1303                 } else {
1304                         set_file_attrs(fname, file, NULL, 0);
1305                         if (itemizing) {
1306                                 itemize(file, ndx, statret, &st,
1307                                         ITEM_LOCAL_CHANGE, 0, NULL);
1308                         }
1309                         if (code != FNONE && verbose)
1310                                 rprintf(code, "%s -> %s\n", fname, sl);
1311 #ifdef SUPPORT_HARD_LINKS
1312                         if (preserve_hard_links && F_IS_HLINKED(file))
1313                                 finish_hard_link(file, fname, NULL, itemizing, code, -1);
1314 #endif
1315                         /* This does not check remove_source_files == 1
1316                          * because this is one of the items that the old
1317                          * --remove-sent-files option would remove. */
1318                         if (remove_source_files)
1319                                 goto return_with_success;
1320                 }
1321 #endif
1322                 return;
1323         }
1324
1325         if ((am_root && preserve_devices && IS_DEVICE(file->mode))
1326          || (preserve_specials && IS_SPECIAL(file->mode))) {
1327                 uint32 *devp = F_RDEV_P(file);
1328                 dev_t rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
1329                 if (statret == 0) {
1330                         char *t;
1331                         if (IS_DEVICE(file->mode)) {
1332                                 if (!IS_DEVICE(st.st_mode))
1333                                         statret = -1;
1334                                 t = "device file";
1335                         } else {
1336                                 if (!IS_SPECIAL(st.st_mode))
1337                                         statret = -1;
1338                                 t = "special file";
1339                         }
1340                         if (statret == 0
1341                          && BITS_EQUAL(st.st_mode, file->mode, _S_IFMT)
1342                          && st.st_rdev == rdev) {
1343                                 /* The device or special file is identical. */
1344                                 if (itemizing)
1345                                         itemize(file, ndx, 0, &st, 0, 0, NULL);
1346                                 set_file_attrs(fname, file, &st, maybe_ATTRS_REPORT);
1347 #ifdef SUPPORT_HARD_LINKS
1348                                 if (preserve_hard_links && F_IS_HLINKED(file))
1349                                         finish_hard_link(file, fname, &st, itemizing, code, -1);
1350 #endif
1351                                 if (remove_source_files == 1)
1352                                         goto return_with_success;
1353                                 return;
1354                         }
1355                         if (delete_item(fname, st.st_mode, t, del_opts) != 0)
1356                                 return;
1357                 } else if (basis_dir[0] != NULL) {
1358                         int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &st,
1359                                               itemizing, code);
1360                         if (j == -2) {
1361 #ifndef CAN_HARDLINK_SPECIAL
1362                                 if (link_dest) {
1363                                         /* Resort to --copy-dest behavior. */
1364                                 } else
1365 #endif
1366                                 if (!copy_dest)
1367                                         return;
1368                                 itemizing = 0;
1369                                 code = FNONE;
1370                         } else if (j >= 0)
1371                                 statret = 1;
1372                 }
1373 #ifdef SUPPORT_HARD_LINKS
1374                 if (preserve_hard_links && F_HLINK_NOT_LAST(file))
1375                         return;
1376 #endif
1377                 if (verbose > 2) {
1378                         rprintf(FINFO, "mknod(%s, 0%o, [%ld,%ld])\n",
1379                                 fname, (int)file->mode,
1380                                 (long)major(rdev), (long)minor(rdev));
1381                 }
1382                 if (do_mknod(fname, file->mode, rdev) < 0) {
1383                         rsyserr(FERROR, errno, "mknod %s failed",
1384                                 full_fname(fname));
1385                 } else {
1386                         set_file_attrs(fname, file, NULL, 0);
1387                         if (itemizing) {
1388                                 itemize(file, ndx, statret, &st,
1389                                         ITEM_LOCAL_CHANGE, 0, NULL);
1390                         }
1391                         if (code != FNONE && verbose)
1392                                 rprintf(code, "%s\n", fname);
1393 #ifdef SUPPORT_HARD_LINKS
1394                         if (preserve_hard_links && F_IS_HLINKED(file))
1395                                 finish_hard_link(file, fname, NULL, itemizing, code, -1);
1396 #endif
1397                         if (remove_source_files == 1)
1398                                 goto return_with_success;
1399                 }
1400                 return;
1401         }
1402
1403         if (!S_ISREG(file->mode)) {
1404                 if (solo_file)
1405                         fname = f_name(file, NULL);
1406                 rprintf(FINFO, "skipping non-regular file \"%s\"\n", fname);
1407                 return;
1408         }
1409
1410         if (max_size > 0 && F_LENGTH(file) > max_size) {
1411                 if (verbose > 1) {
1412                         if (solo_file)
1413                                 fname = f_name(file, NULL);
1414                         rprintf(FINFO, "%s is over max-size\n", fname);
1415                 }
1416                 return;
1417         }
1418         if (min_size > 0 && F_LENGTH(file) < min_size) {
1419                 if (verbose > 1) {
1420                         if (solo_file)
1421                                 fname = f_name(file, NULL);
1422                         rprintf(FINFO, "%s is under min-size\n", fname);
1423                 }
1424                 return;
1425         }
1426
1427         if (ignore_existing > 0 && statret == 0) {
1428                 if (verbose > 1)
1429                         rprintf(FINFO, "%s exists\n", fname);
1430                 return;
1431         }
1432
1433         if (update_only > 0 && statret == 0
1434             && cmp_time(st.st_mtime, file->modtime) > 0) {
1435                 if (verbose > 1)
1436                         rprintf(FINFO, "%s is newer\n", fname);
1437                 return;
1438         }
1439
1440         fnamecmp = fname;
1441         fnamecmp_type = FNAMECMP_FNAME;
1442
1443         if (statret == 0 && !S_ISREG(st.st_mode)) {
1444                 if (delete_item(fname, st.st_mode, "regular file", del_opts) != 0)
1445                         return;
1446                 statret = -1;
1447                 stat_errno = ENOENT;
1448         }
1449
1450         if (statret != 0 && basis_dir[0] != NULL) {
1451                 int j = try_dests_reg(file, fname, ndx, fnamecmpbuf, &st,
1452                                       itemizing, code);
1453                 if (j == -2) {
1454                         if (remove_source_files == 1)
1455                                 goto return_with_success;
1456                         return;
1457                 }
1458                 if (j >= 0) {
1459                         fnamecmp = fnamecmpbuf;
1460                         fnamecmp_type = j;
1461                         statret = 0;
1462                 }
1463         }
1464
1465         real_ret = statret;
1466         real_st = st;
1467
1468         if (partial_dir && (partialptr = partial_dir_fname(fname)) != NULL
1469             && link_stat(partialptr, &partial_st, 0) == 0
1470             && S_ISREG(partial_st.st_mode)) {
1471                 if (statret != 0)
1472                         goto prepare_to_open;
1473         } else
1474                 partialptr = NULL;
1475
1476         if (statret != 0 && fuzzy_dirlist && dry_run <= 1) {
1477                 int j = find_fuzzy(file, fuzzy_dirlist);
1478                 if (j >= 0) {
1479                         fuzzy_file = fuzzy_dirlist->files[j];
1480                         f_name(fuzzy_file, fnamecmpbuf);
1481                         if (verbose > 2) {
1482                                 rprintf(FINFO, "fuzzy basis selected for %s: %s\n",
1483                                         fname, fnamecmpbuf);
1484                         }
1485                         st.st_size = F_LENGTH(fuzzy_file);
1486                         statret = 0;
1487                         fnamecmp = fnamecmpbuf;
1488                         fnamecmp_type = FNAMECMP_FUZZY;
1489                 }
1490         }
1491
1492         if (statret != 0) {
1493 #ifdef SUPPORT_HARD_LINKS
1494                 if (preserve_hard_links && F_HLINK_NOT_LAST(file))
1495                         return;
1496 #endif
1497                 if (stat_errno == ENOENT)
1498                         goto notify_others;
1499                 rsyserr(FERROR, stat_errno, "recv_generator: failed to stat %s",
1500                         full_fname(fname));
1501                 return;
1502         }
1503
1504         if (append_mode > 0 && st.st_size > F_LENGTH(file))
1505                 return;
1506
1507         if (fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
1508                 ;
1509         else if (fnamecmp_type == FNAMECMP_FUZZY)
1510                 ;
1511         else if (unchanged_file(fnamecmp, file, &st)) {
1512                 if (partialptr) {
1513                         do_unlink(partialptr);
1514                         handle_partial_dir(partialptr, PDIR_DELETE);
1515                 }
1516                 if (itemizing)
1517                         itemize(file, ndx, statret, &st, 0, 0, NULL);
1518                 set_file_attrs(fname, file, &st, maybe_ATTRS_REPORT);
1519 #ifdef SUPPORT_HARD_LINKS
1520                 if (preserve_hard_links && F_IS_HLINKED(file))
1521                         finish_hard_link(file, fname, &st, itemizing, code, -1);
1522 #endif
1523                 if (remove_source_files != 1)
1524                         return;
1525           return_with_success:
1526                 if (!dry_run)
1527                         send_msg_int(MSG_SUCCESS, ndx + cur_flist->ndx_start);
1528                 return;
1529         }
1530
1531   prepare_to_open:
1532         if (partialptr) {
1533                 st = partial_st;
1534                 fnamecmp = partialptr;
1535                 fnamecmp_type = FNAMECMP_PARTIAL_DIR;
1536                 statret = 0;
1537         }
1538
1539         if (!do_xfers || read_batch || whole_file)
1540                 goto notify_others;
1541
1542         if (fuzzy_dirlist) {
1543                 int j = flist_find(fuzzy_dirlist, file);
1544                 if (j >= 0) /* don't use changing file as future fuzzy basis */
1545                         fuzzy_dirlist->files[j]->flags |= FLAG_FILE_SENT;
1546         }
1547
1548         /* open the file */
1549         fd = do_open(fnamecmp, O_RDONLY, 0);
1550
1551         if (fd == -1) {
1552                 rsyserr(FERROR, errno, "failed to open %s, continuing",
1553                         full_fname(fnamecmp));
1554           pretend_missing:
1555                 /* pretend the file didn't exist */
1556 #ifdef SUPPORT_HARD_LINKS
1557                 if (preserve_hard_links && F_HLINK_NOT_LAST(file))
1558                         return;
1559 #endif
1560                 statret = real_ret = -1;
1561                 goto notify_others;
1562         }
1563
1564         if (inplace && make_backups > 0 && fnamecmp_type == FNAMECMP_FNAME) {
1565                 if (!(backupptr = get_backup_name(fname))) {
1566                         close(fd);
1567                         return;
1568                 }
1569                 if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS))) {
1570                         close(fd);
1571                         goto pretend_missing;
1572                 }
1573                 if (robust_unlink(backupptr) && errno != ENOENT) {
1574                         rsyserr(FERROR, errno, "unlink %s",
1575                                 full_fname(backupptr));
1576                         unmake_file(back_file);
1577                         close(fd);
1578                         return;
1579                 }
1580                 if ((f_copy = do_open(backupptr,
1581                     O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0) {
1582                         rsyserr(FERROR, errno, "open %s",
1583                                 full_fname(backupptr));
1584                         unmake_file(back_file);
1585                         close(fd);
1586                         return;
1587                 }
1588                 fnamecmp_type = FNAMECMP_BACKUP;
1589         }
1590
1591         if (verbose > 3) {
1592                 rprintf(FINFO, "gen mapped %s of size %.0f\n",
1593                         fnamecmp, (double)st.st_size);
1594         }
1595
1596         if (verbose > 2)
1597                 rprintf(FINFO, "generating and sending sums for %d\n", ndx);
1598
1599   notify_others:
1600         if (remove_source_files && !delay_updates && !phase)
1601                 increment_active_files(ndx, itemizing, code);
1602         if (incremental && !dry_run)
1603                 cur_flist->in_progress++;
1604 #ifdef SUPPORT_HARD_LINKS
1605         if (preserve_hard_links && F_IS_HLINKED(file))
1606                 file->flags |= FLAG_FILE_SENT;
1607 #endif
1608         write_int(f_out, ndx + cur_flist->ndx_start);
1609         if (itemizing) {
1610                 int iflags = ITEM_TRANSFER;
1611                 if (always_checksum > 0)
1612                         iflags |= ITEM_REPORT_CHECKSUM;
1613                 if (fnamecmp_type != FNAMECMP_FNAME)
1614                         iflags |= ITEM_BASIS_TYPE_FOLLOWS;
1615                 if (fnamecmp_type == FNAMECMP_FUZZY)
1616                         iflags |= ITEM_XNAME_FOLLOWS;
1617                 itemize(file, -1, real_ret, &real_st, iflags, fnamecmp_type,
1618                         fuzzy_file ? fuzzy_file->basename : NULL);
1619         }
1620
1621         if (!do_xfers) {
1622 #ifdef SUPPORT_HARD_LINKS
1623                 if (preserve_hard_links && F_IS_HLINKED(file))
1624                         finish_hard_link(file, fname, &st, itemizing, code, -1);
1625 #endif
1626                 return;
1627         }
1628         if (read_batch)
1629                 return;
1630
1631         if (statret != 0 || whole_file) {
1632                 write_sum_head(f_out, NULL);
1633                 return;
1634         }
1635
1636         generate_and_send_sums(fd, st.st_size, f_out, f_copy);
1637
1638         if (f_copy >= 0) {
1639                 close(f_copy);
1640                 set_file_attrs(backupptr, back_file, NULL, 0);
1641                 if (verbose > 1) {
1642                         rprintf(FINFO, "backed up %s to %s\n",
1643                                 fname, backupptr);
1644                 }
1645                 unmake_file(back_file);
1646         }
1647
1648         close(fd);
1649 }
1650
1651 void generate_files(int f_out, char *local_name)
1652 {
1653         int i;
1654         char fbuf[MAXPATHLEN];
1655         int itemizing;
1656         enum logcode code;
1657         struct file_list *next_flist;
1658         int lull_mod = allowed_lull * 5;
1659         int need_retouch_dir_times = preserve_times && !omit_dir_times;
1660         int need_retouch_dir_perms = 0;
1661         int save_do_progress = do_progress;
1662         int dir_tweaking = !(list_only || local_name || dry_run);
1663
1664         if (protocol_version >= 29) {
1665                 itemizing = 1;
1666                 maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
1667                 code = logfile_format_has_i ? FNONE : FLOG;
1668         } else if (am_daemon) {
1669                 itemizing = logfile_format_has_i && do_xfers;
1670                 maybe_ATTRS_REPORT = ATTRS_REPORT;
1671                 code = itemizing || !do_xfers ? FCLIENT : FINFO;
1672         } else if (!am_server) {
1673                 itemizing = stdout_format_has_i;
1674                 maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
1675                 code = itemizing ? FNONE : FINFO;
1676         } else {
1677                 itemizing = 0;
1678                 maybe_ATTRS_REPORT = ATTRS_REPORT;
1679                 code = FINFO;
1680         }
1681         solo_file = local_name != NULL;
1682
1683         if (verbose > 2)
1684                 rprintf(FINFO, "generator starting pid=%ld\n", (long)getpid());
1685
1686         if (delete_before && !local_name && cur_flist->count > 0)
1687                 do_delete_pass(cur_flist);
1688         if (delete_during == 2) {
1689                 deldelay_size = BIGPATHBUFLEN * 4;
1690                 deldelay_buf = new_array(char, deldelay_size);
1691                 if (!deldelay_buf)
1692                         out_of_memory("delete-delay");
1693         }
1694         do_progress = 0;
1695
1696         if (append_mode > 0 || whole_file < 0)
1697                 whole_file = 0;
1698         if (verbose >= 2) {
1699                 rprintf(FINFO, "delta-transmission %s\n",
1700                         whole_file
1701                         ? "disabled for local transfer or --whole-file"
1702                         : "enabled");
1703         }
1704
1705         /* Since we often fill up the outgoing socket and then just sit around
1706          * waiting for the other 2 processes to do their thing, we don't want
1707          * to exit on a timeout.  If the data stops flowing, the receiver will
1708          * notice that and let us know via the redo pipe (or its closing). */
1709         ignore_timeout = 1;
1710
1711         do {
1712                 if (incremental && delete_during && cur_flist->ndx_start) {
1713                         struct file_struct *fp = dir_flist->files[cur_flist->parent_ndx];
1714                         if (BITS_SETnUNSET(fp->flags, FLAG_XFER_DIR, FLAG_MISSING_DIR)) {
1715                                 dev_t dirdev;
1716                                 if (one_file_system) {
1717                                         uint32 *devp = F_DIRDEV_P(fp);
1718                                         dirdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
1719                                 } else
1720                                         dirdev = MAKEDEV(0, 0);
1721                                 delete_in_dir(cur_flist, f_name(fp, fbuf), fp, &dirdev);
1722                         }
1723                 }
1724                 for (i = cur_flist->low; i <= cur_flist->high; i++) {
1725                         struct file_struct *file = cur_flist->files[i];
1726
1727                         if (!F_IS_ACTIVE(file))
1728                                 continue;
1729
1730                         if (local_name)
1731                                 strlcpy(fbuf, local_name, sizeof fbuf);
1732                         else
1733                                 f_name(file, fbuf);
1734                         recv_generator(fbuf, file, i, itemizing, code, f_out);
1735
1736                         /* We need to ensure that any dirs we create have
1737                          * writeable permissions during the time we are putting
1738                          * files within them.  This is then fixed after the
1739                          * transfer is done. */
1740 #ifdef HAVE_CHMOD
1741                         if (!am_root && S_ISDIR(file->mode)
1742                          && !(file->mode & S_IWUSR) && dir_tweaking) {
1743                                 mode_t mode = file->mode | S_IWUSR;
1744                                 char *fname = local_name ? local_name : fbuf;
1745                                 if (do_chmod(fname, mode) < 0) {
1746                                         rsyserr(FERROR, errno,
1747                                             "failed to modify permissions on %s",
1748                                             full_fname(fname));
1749                                 }
1750                                 need_retouch_dir_perms = 1;
1751                         }
1752 #endif
1753
1754 #ifdef SUPPORT_HARD_LINKS
1755                         if (preserve_hard_links)
1756                                 check_for_finished_hlinks(itemizing, code);
1757 #endif
1758
1759                         if (allowed_lull && !(i % lull_mod))
1760                                 maybe_send_keepalive();
1761                         else if (!(i % 200))
1762                                 maybe_flush_socket();
1763                 }
1764
1765                 if (!incremental) {
1766                         if (delete_during)
1767                                 delete_in_dir(NULL, NULL, NULL, &dev_zero);
1768                         phase++;
1769                         if (verbose > 2) {
1770                                 rprintf(FINFO, "generate_files phase=%d\n",
1771                                         phase);
1772                         }
1773
1774                         write_int(f_out, NDX_DONE);
1775                 }
1776
1777                 csum_length = SUM_LENGTH;
1778                 max_size = -max_size;
1779                 min_size = -min_size;
1780                 ignore_existing = -ignore_existing;
1781                 ignore_non_existing = -ignore_non_existing;
1782                 update_only = -update_only;
1783                 always_checksum = -always_checksum;
1784                 size_only = -size_only;
1785                 append_mode = -append_mode;
1786                 make_backups = -make_backups; /* avoid dup backup w/inplace */
1787                 ignore_times++;
1788
1789                 /* Files can cycle through the system more than once
1790                  * to catch initial checksum errors. */
1791                 while (!done_cnt) {
1792                         struct file_struct *file;
1793                         struct file_list *save_flist;
1794
1795                         check_for_finished_hlinks(itemizing, code);
1796
1797                         if ((i = get_redo_num()) == -1) {
1798                                 if (incremental)
1799                                         break;
1800                                 wait_for_receiver();
1801                                 continue;
1802                         }
1803
1804                         save_flist = cur_flist;
1805                         cur_flist = flist_for_ndx(i);
1806                         file = cur_flist->files[i];
1807                         if (local_name)
1808                                 strlcpy(fbuf, local_name, sizeof fbuf);
1809                         else
1810                                 f_name(file, fbuf);
1811                         recv_generator(fbuf, file, i, itemizing, code, f_out);
1812                         cur_flist->to_redo--;
1813                         cur_flist = save_flist;
1814                 }
1815
1816                 csum_length = SHORT_SUM_LENGTH;
1817                 max_size = -max_size;
1818                 min_size = -min_size;
1819                 ignore_existing = -ignore_existing;
1820                 ignore_non_existing = -ignore_non_existing;
1821                 update_only = -update_only;
1822                 always_checksum = -always_checksum;
1823                 size_only = -size_only;
1824                 append_mode = -append_mode;
1825                 make_backups = -make_backups;
1826                 ignore_times--;
1827
1828                 if (!incremental)
1829                         break;
1830
1831                 while (!cur_flist->next && !flist_eof)
1832                         wait_for_receiver();
1833                 next_flist = cur_flist->next;
1834                 while (first_flist != next_flist) {
1835                         struct file_struct *fp;
1836                         if (first_flist->in_progress || first_flist->to_redo) {
1837                                 if (next_flist)
1838                                         break;
1839                                 wait_for_receiver();
1840                                 continue;
1841                         }
1842
1843                         cur_flist = first_flist;
1844                         if (cur_flist->ndx_start != 0) {
1845                                 fp = dir_flist->files[cur_flist->parent_ndx];
1846                                 if (!(fp->flags & FLAG_MISSING_DIR)) {
1847                                         f_name(fp, fbuf);
1848                                         if (!(fp->mode & S_IWUSR))
1849                                                 do_chmod(fbuf, fp->mode);
1850                                         if (preserve_times && !omit_dir_times) {
1851                                                 set_modtime(fbuf, fp->modtime,
1852                                                             fp->mode);
1853                                         }
1854                                 }
1855                         } else if (relative_paths && implied_dirs
1856                             && preserve_times && !omit_dir_times) {
1857                                 /* Set mtime on implied dirs */
1858                                 for (i = 0; i < cur_flist->count; i++) {
1859                                         fp = cur_flist->files[i];
1860                                         if (!S_ISDIR(fp->mode)
1861                                          || fp->flags & (FLAG_XFER_DIR|FLAG_MISSING_DIR))
1862                                                 continue;
1863                                         f_name(fp, fbuf);
1864                                         set_modtime(fbuf, fp->modtime, fp->mode);
1865                                 }
1866                         }
1867
1868                         flist_free(cur_flist);
1869
1870                         if (!read_batch)
1871                                 write_int(f_out, NDX_DONE);
1872                 }
1873         } while ((cur_flist = next_flist) != NULL);
1874
1875         phase++;
1876         if (verbose > 2)
1877                 rprintf(FINFO, "generate_files phase=%d\n", phase);
1878
1879         write_int(f_out, NDX_DONE);
1880         /* Reduce round-trip lag-time for a useless delay-updates phase. */
1881         if (protocol_version >= 29 && !delay_updates)
1882                 write_int(f_out, NDX_DONE);
1883
1884         /* Read MSG_DONE for the redo phase (and any prior messages). */
1885         while (done_cnt <= 1) {
1886                 check_for_finished_hlinks(itemizing, code);
1887                 wait_for_receiver();
1888         }
1889
1890         if (protocol_version >= 29) {
1891                 phase++;
1892                 if (verbose > 2)
1893                         rprintf(FINFO, "generate_files phase=%d\n", phase);
1894                 if (delay_updates)
1895                         write_int(f_out, NDX_DONE);
1896                 /* Read MSG_DONE for delay-updates phase & prior messages. */
1897                 while (done_cnt == 2)
1898                         wait_for_receiver();
1899         }
1900
1901         do_progress = save_do_progress;
1902         if (delete_during == 2)
1903                 do_delayed_deletions(fbuf);
1904         if (delete_after && !local_name && file_total > 0)
1905                 do_delete_pass(cur_flist);
1906
1907         if ((need_retouch_dir_perms || need_retouch_dir_times)
1908          && dir_tweaking && !incremental) {
1909                 int j = 0;
1910                 /* Now we need to fix any directory permissions that were
1911                  * modified during the transfer and/or re-set any tweaked
1912                  * modified-time values. */
1913                 for (i = 0; i < cur_flist->count; i++) {
1914                         struct file_struct *file = cur_flist->files[i];
1915                         if (!F_IS_ACTIVE(file) || !S_ISDIR(file->mode))
1916                                 continue;
1917                         if (!need_retouch_dir_times && file->mode & S_IWUSR)
1918                                 continue;
1919                         if (file->flags & FLAG_MISSING_DIR) {
1920                                 int missing = F_DEPTH(file);
1921                                 while (++i < cur_flist->count) {
1922                                         file = cur_flist->files[i];
1923                                         if (F_DEPTH(file) <= missing)
1924                                                 break;
1925                                 }
1926                                 i--;
1927                                 continue;
1928                         }
1929                         recv_generator(f_name(file, NULL), file, i, itemizing,
1930                                        code, -1);
1931                         if (allowed_lull && !(++j % lull_mod))
1932                                 maybe_send_keepalive();
1933                         else if (!(j % 200))
1934                                 maybe_flush_socket();
1935                 }
1936         }
1937
1938         if (max_delete >= 0 && deletion_count > max_delete) {
1939                 rprintf(FINFO,
1940                         "Deletions stopped due to --max-delete limit (%d skipped)\n",
1941                         deletion_count - max_delete);
1942                 io_error |= IOERR_DEL_LIMIT;
1943         }
1944
1945         if (verbose > 2)
1946                 rprintf(FINFO, "generate_files finished\n");
1947 }