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