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