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