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