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