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