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