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