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