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