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