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