Moved GID_NONE define from rsync.h to uidlist.c.
[rsync/rsync.git] / generator.c
CommitLineData
0f78b815
WD
1/*
2 * Routines that are exclusive to the generator process.
3 *
4 * Copyright (C) 1996-2000 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2002 Martin Pool <mbp@samba.org>
ba2133d6 7 * Copyright (C) 2003-2007 Wayne Davison
0f78b815
WD
8 *
9 * This program is free software; you can redistribute it and/or modify
ba2133d6
WD
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
0f78b815
WD
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
e7c67065
WD
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
0f78b815 21 */
2f03f956
AT
22
23#include "rsync.h"
24
25extern int verbose;
26extern int dry_run;
beb51aa0 27extern int do_xfers;
17bda2d1 28extern int stdout_format_has_i;
ecc7623e 29extern int logfile_format_has_i;
6c3862fa
WD
30extern int am_root;
31extern int am_server;
32extern int am_daemon;
3ea6e0e7 33extern int inc_recurse;
083acd49 34extern int do_progress;
2f03f956 35extern int relative_paths;
ccb16b46 36extern int implied_dirs;
716e73d4 37extern int keep_dirlinks;
1c3344a1 38extern int preserve_acls;
16edf865 39extern int preserve_xattrs;
2f03f956 40extern int preserve_links;
2f03f956 41extern int preserve_devices;
b5c6a6ae 42extern int preserve_specials;
2f03f956 43extern int preserve_hard_links;
6744b62d
WD
44extern int preserve_perms;
45extern int preserve_uid;
46extern int preserve_gid;
3ea9bbd6
WD
47extern int preserve_times;
48extern int omit_dir_times;
e35d9f2d 49extern int delete_mode;
59faec8b 50extern int delete_before;
fa13f396 51extern int delete_during;
59faec8b 52extern int delete_after;
7730114b 53extern int msgdone_cnt;
59faec8b 54extern int ignore_errors;
47c11975 55extern int remove_source_files;
70352269 56extern int delay_updates;
2f03f956 57extern int update_only;
e90aab49
WD
58extern int ignore_existing;
59extern int ignore_non_existing;
cd6aa5b5 60extern int inplace;
6cc11982 61extern int append_mode;
cd6aa5b5 62extern int make_backups;
2f03f956
AT
63extern int csum_length;
64extern int ignore_times;
f83f0548 65extern int size_only;
7d1bfaf7 66extern OFF_T max_size;
02b5cb23 67extern OFF_T min_size;
59faec8b 68extern int io_error;
f3d6d480 69extern int flist_eof;
ac40b747 70extern int allowed_lull;
ee1d11c4 71extern int sock_f_out;
9ac2395b 72extern int ignore_timeout;
d04e9c51 73extern int protocol_version;
f3d6d480 74extern int file_total;
8e85be0a 75extern int fuzzy_basis;
2f03f956 76extern int always_checksum;
cfe39780 77extern int checksum_len;
a7260c40 78extern char *partial_dir;
b7e8628c 79extern char *basis_dir[];
2be2fb3e 80extern int compare_dest;
967866d4 81extern int copy_dest;
59c95e42 82extern int link_dest;
5774786f 83extern int whole_file;
5774786f 84extern int list_only;
1ef5bf3c 85extern int new_root_dir;
b9f592fb 86extern int read_batch;
5774786f 87extern int safe_symlinks;
a255c592 88extern long block_size; /* "long" because popt can't set an int32. */
59faec8b
WD
89extern int max_delete;
90extern int force_delete;
0e82af2d 91extern int one_file_system;
6c3862fa 92extern struct stats stats;
0e82af2d 93extern dev_t filesystem_dev;
1c3344a1 94extern mode_t orig_umask;
59faec8b
WD
95extern char *backup_dir;
96extern char *backup_suffix;
97extern int backup_suffix_len;
f3d6d480 98extern struct file_list *cur_flist, *first_flist, *dir_flist;
7842418b 99extern struct filter_list_struct server_filter_list;
332cf6df
WD
100#ifdef ICONV_OPTION
101extern int ic_ndx;
102#endif
97f9dcae 103
146c2c36
WD
104int ignore_perishable = 0;
105int non_perishable_cnt = 0;
18979194 106int maybe_ATTRS_REPORT = 0;
146c2c36 107
f3d6d480 108static dev_t dev_zero;
59faec8b 109static int deletion_count = 0; /* used to implement --max-delete */
20319fd9
WD
110static int deldelay_size = 0, deldelay_cnt = 0;
111static char *deldelay_buf = NULL;
112static int deldelay_fd = -1;
7730114b
WD
113static int lull_mod;
114static int dir_tweaking;
115static int need_retouch_dir_times;
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
234 if (!dirlist->count)
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
59faec8b
WD
250 for (j = dirlist->count; j--; ) {
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. */
462 for (i = dirlist->count; 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
332cf6df
WD
497 for (j = 0; j < cur_flist->count; j++) {
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
4ade505c 524 if (am_root && preserve_uid && sxp->st.st_uid != F_OWNER(file))
b7e8628c 525 return 0;
bb24028f 526
4ade505c 527 if (preserve_gid && !(file->flags & FLAG_SKIP_GROUP) && sxp->st.st_gid != 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
556 : S_ISDIR(file->mode) ? !omit_dir_times
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;
4ade505c 568 if (preserve_uid && am_root && F_OWNER(file) != sxp->st.st_uid)
48224e4c 569 iflags |= ITEM_REPORT_OWNER;
4ade505c
WD
570 if (preserve_gid && !(file->flags & FLAG_SKIP_GROUP)
571 && sxp->st.st_gid != 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
787 for (j = 0; j < dirlist->count; j++) {
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 = "";
8e85be0a 1094 static struct file_list *fuzzy_dirlist = NULL;
8470a515 1095 static int need_fuzzy_dirlist = 0;
8e85be0a 1096 struct file_struct *fuzzy_file = NULL;
41cfde6b 1097 int fd = -1, f_copy = -1;
1c3344a1
WD
1098 statx sx, real_sx;
1099 STRUCT_STAT partial_st;
41cfde6b 1100 struct file_struct *back_file = NULL;
1f1d368a 1101 int statret, real_ret, stat_errno;
41cfde6b 1102 char *fnamecmp, *partialptr, *backupptr = NULL;
375a4556 1103 char fnamecmpbuf[MAXPATHLEN];
41cfde6b 1104 uchar fnamecmp_type;
c6fadc0e 1105 int del_opts = delete_mode || force_delete ? DEL_RECURSE : 0;
f7632fc6 1106
dfd5ba6a
WD
1107 if (list_only)
1108 return;
2f03f956 1109
45c49b52
WD
1110 if (verbose > 2)
1111 rprintf(FINFO, "recv_generator(%s,%d)\n", fname, ndx);
2f03f956 1112
8174bc35
WD
1113 if (server_filter_list.head) {
1114 if (excluded_below >= 0) {
2ef03405 1115 if (F_DEPTH(file) > excluded_below)
8174bc35
WD
1116 goto skipping;
1117 excluded_below = -1;
1118 }
1119 if (check_filter(&server_filter_list, fname,
1120 S_ISDIR(file->mode)) < 0) {
1121 if (S_ISDIR(file->mode))
2ef03405 1122 excluded_below = F_DEPTH(file);
b2e7c913 1123 skipping:
8174bc35
WD
1124 if (verbose) {
1125 rprintf(FINFO,
1126 "skipping server-excluded file \"%s\"\n",
45c49b52 1127 fname);
8174bc35
WD
1128 }
1129 return;
3e35c34b 1130 }
3e35c34b 1131 }
97f9dcae 1132
caff3322 1133 if (missing_below >= 0) {
2ef03405 1134 if (F_DEPTH(file) <= missing_below) {
caff3322
WD
1135 if (dry_run)
1136 dry_run--;
1137 missing_below = -1;
f3d6d480
WD
1138 } else if (!dry_run) {
1139 if (S_ISDIR(file->mode))
1140 file->flags |= FLAG_MISSING_DIR;
caff3322 1141 return;
f3d6d480 1142 }
df337831 1143 }
1c3344a1
WD
1144#ifdef SUPPORT_ACLS
1145 sx.acc_acl = sx.def_acl = NULL;
16edf865
WD
1146#endif
1147#ifdef SUPPORT_XATTRS
1148 sx.xattr = NULL;
1c3344a1 1149#endif
73f7af0e 1150 if (dry_run > 1) {
f3d6d480
WD
1151 if (fuzzy_dirlist) {
1152 flist_free(fuzzy_dirlist);
1153 fuzzy_dirlist = NULL;
1154 }
1155 parent_dirname = "";
73f7af0e
WD
1156 statret = -1;
1157 stat_errno = ENOENT;
1158 } else {
4a19c3b2 1159 const char *dn = file->dirname ? file->dirname : ".";
8c9e06d7 1160 if (parent_dirname != dn && strcmp(parent_dirname, dn) != 0) {
aa37022e 1161 if (relative_paths && !implied_dirs
1c3344a1 1162 && do_stat(dn, &sx.st) < 0
904e5af1 1163 && create_directory_path(fname) < 0) {
8c9e06d7
WD
1164 rsyserr(FERROR, errno,
1165 "recv_generator: mkdir %s failed",
1166 full_fname(dn));
8e85be0a 1167 }
8c9e06d7 1168 if (fuzzy_dirlist) {
8470a515 1169 flist_free(fuzzy_dirlist);
8c9e06d7
WD
1170 fuzzy_dirlist = NULL;
1171 }
1172 if (fuzzy_basis)
8470a515 1173 need_fuzzy_dirlist = 1;
1c3344a1
WD
1174#ifdef SUPPORT_ACLS
1175 if (!preserve_perms)
1176 dflt_perms = default_perms_for_dir(dn);
1177#endif
8e85be0a 1178 }
8c9e06d7 1179 parent_dirname = dn;
8e85be0a 1180
6f2a222c 1181 if (need_fuzzy_dirlist && S_ISREG(file->mode)) {
4a19c3b2
WD
1182 strlcpy(fnamecmpbuf, dn, sizeof fnamecmpbuf);
1183 fuzzy_dirlist = get_dirlist(fnamecmpbuf, -1, 1);
6f2a222c
WD
1184 need_fuzzy_dirlist = 0;
1185 }
865c3b5f 1186
1c3344a1 1187 statret = link_stat(fname, &sx.st,
73f7af0e
WD
1188 keep_dirlinks && S_ISDIR(file->mode));
1189 stat_errno = errno;
1190 }
63787382 1191
f3d6d480 1192 if (ignore_non_existing > 0 && statret == -1 && stat_errno == ENOENT) {
ecc81fce 1193 if (verbose > 1) {
15164c0a
WD
1194 rprintf(FINFO, "not creating new %s \"%s\"\n",
1195 S_ISDIR(file->mode) ? "directory" : "file",
45c49b52 1196 fname);
ecc81fce 1197 }
f3d6d480
WD
1198 if (S_ISDIR(file->mode)) {
1199 if (missing_below < 0) {
1200 if (dry_run)
1201 dry_run++;
1202 missing_below = F_DEPTH(file);
1203 }
1204 file->flags |= FLAG_MISSING_DIR;
1205 }
1347d512
AT
1206 return;
1207 }
1208
2f03f956 1209 if (S_ISDIR(file->mode)) {
2cda2560
WD
1210 /* The file to be received is a directory, so we need
1211 * to prepare appropriately. If there is already a
1212 * file of that name and it is *not* a directory, then
1213 * we need to delete it. If it doesn't exist, then
027428eb 1214 * (perhaps recursively) create it. */
1c3344a1
WD
1215 if (statret == 0 && !S_ISDIR(sx.st.st_mode)) {
1216 if (delete_item(fname, sx.st.st_mode, "directory", del_opts) != 0)
1f1d368a 1217 return;
2f03f956
AT
1218 statret = -1;
1219 }
df337831 1220 if (dry_run && statret != 0 && missing_below < 0) {
2ef03405 1221 missing_below = F_DEPTH(file);
df337831
WD
1222 dry_run++;
1223 }
b467495c 1224 real_ret = statret;
1c3344a1 1225 real_sx = sx;
116a4769
WD
1226 if (new_root_dir) {
1227 if (*fname == '.' && fname[1] == '\0')
b467495c 1228 statret = -1;
116a4769
WD
1229 new_root_dir = 0;
1230 }
c4750c2a 1231 if (!preserve_perms) { /* See comment in non-dir code below. */
1c3344a1
WD
1232 file->mode = dest_mode(file->mode, sx.st.st_mode,
1233 dflt_perms, statret == 0);
c4750c2a 1234 }
b467495c 1235 if (statret != 0 && basis_dir[0] != NULL) {
1c3344a1 1236 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
18979194 1237 itemizing, code);
116a4769
WD
1238 if (j == -2) {
1239 itemizing = 0;
1240 code = FNONE;
1241 } else if (j >= 0)
b467495c 1242 statret = 1;
116a4769 1243 }
ee1d11c4 1244 if (itemizing && f_out != -1) {
1c3344a1 1245 itemize(fname, file, ndx, statret, &sx,
b467495c 1246 statret ? ITEM_LOCAL_CHANGE : 0, 0, NULL);
ee1d11c4 1247 }
b467495c 1248 if (real_ret != 0 && do_mkdir(fname,file->mode) < 0 && errno != EEXIST) {
027428eb 1249 if (!relative_paths || errno != ENOENT
904e5af1 1250 || create_directory_path(fname) < 0
09290693 1251 || (do_mkdir(fname, file->mode) < 0 && errno != EEXIST)) {
d62bcc17
WD
1252 rsyserr(FERROR, errno,
1253 "recv_generator: mkdir %s failed",
1254 full_fname(fname));
f3d6d480
WD
1255 rprintf(FERROR,
1256 "*** Skipping any contents from this failed directory ***\n");
1257 missing_below = F_DEPTH(file);
82ad07c4 1258 file->flags |= FLAG_MISSING_DIR;
1c3344a1 1259 goto cleanup;
2f03f956
AT
1260 }
1261 }
16edf865 1262 if (set_file_attrs(fname, file, real_ret ? NULL : &real_sx, NULL, 0)
f4164b73 1263 && verbose && code != FNONE && f_out != -1)
45c49b52 1264 rprintf(code, "%s/\n", fname);
b467495c 1265 if (real_ret != 0 && one_file_system)
1c3344a1 1266 real_sx.st.st_dev = filesystem_dev;
3ea6e0e7 1267 if (inc_recurse) {
f3d6d480
WD
1268 if (one_file_system) {
1269 uint32 *devp = F_DIRDEV_P(file);
1c3344a1
WD
1270 DEV_MAJOR(devp) = major(real_sx.st.st_dev);
1271 DEV_MINOR(devp) = minor(real_sx.st.st_dev);
f3d6d480
WD
1272 }
1273 }
1274 else if (delete_during && f_out != -1 && !phase && dry_run < 2
82ad07c4 1275 && (file->flags & FLAG_XFER_DIR))
332cf6df 1276 delete_in_dir(fname, file, &real_sx.st.st_dev);
1c3344a1 1277 goto cleanup;
06a1dbad 1278 }
6c3862fa 1279
c4750c2a
WD
1280 /* If we're not preserving permissions, change the file-list's
1281 * mode based on the local permissions and some heuristics. */
1282 if (!preserve_perms) {
1c3344a1
WD
1283 int exists = statret == 0 && !S_ISDIR(sx.st.st_mode);
1284 file->mode = dest_mode(file->mode, sx.st.st_mode, dflt_perms,
1285 exists);
c4750c2a
WD
1286 }
1287
1e1ca253 1288#ifdef SUPPORT_HARD_LINKS
b7cfb9e2 1289 if (preserve_hard_links && F_HLINK_NOT_FIRST(file)
1c3344a1
WD
1290 && hard_link_check(file, ndx, fname, statret, &sx, itemizing, code))
1291 goto cleanup;
1e1ca253 1292#endif
273a7ed5 1293
2f03f956 1294 if (preserve_links && S_ISLNK(file->mode)) {
4f5b0756 1295#ifdef SUPPORT_LINKS
82ad07c4
WD
1296 const char *sl = F_SYMLINK(file);
1297 if (safe_symlinks && unsafe_symlink(sl, fname)) {
2f03f956 1298 if (verbose) {
49b86442 1299 if (solo_file)
6cbde57d 1300 fname = f_name(file, NULL);
4875d6b6
WD
1301 rprintf(FINFO,
1302 "ignoring unsafe symlink %s -> \"%s\"\n",
82ad07c4 1303 full_fname(fname), sl);
2f03f956
AT
1304 }
1305 return;
1306 }
1307 if (statret == 0) {
7e38410e
WD
1308 char lnk[MAXPATHLEN];
1309 int len;
1310
1c3344a1 1311 if (!S_ISLNK(sx.st.st_mode))
116a4769
WD
1312 statret = -1;
1313 else if ((len = readlink(fname, lnk, MAXPATHLEN-1)) > 0
82ad07c4 1314 && strncmp(lnk, sl, len) == 0 && sl[len] == '\0') {
116a4769 1315 /* The link is pointing to the right place. */
16edf865 1316 set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
116a4769 1317 if (itemizing)
1c3344a1 1318 itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
1e1ca253 1319#ifdef SUPPORT_HARD_LINKS
112d728f 1320 if (preserve_hard_links && F_IS_HLINKED(file))
1c3344a1 1321 finish_hard_link(file, fname, &sx.st, itemizing, code, -1);
1e1ca253 1322#endif
116a4769
WD
1323 if (remove_source_files == 1)
1324 goto return_with_success;
1c3344a1 1325 goto cleanup;
2cda2560 1326 }
7e38410e
WD
1327 /* Not the right symlink (or not a symlink), so
1328 * delete it. */
1c3344a1
WD
1329 if (delete_item(fname, sx.st.st_mode, "symlink", del_opts) != 0)
1330 goto cleanup;
1c6e9dfa 1331 } else if (basis_dir[0] != NULL) {
1c3344a1 1332 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
18979194 1333 itemizing, code);
116a4769 1334 if (j == -2) {
ba212fe0
WD
1335#ifndef CAN_HARDLINK_SYMLINK
1336 if (link_dest) {
1337 /* Resort to --copy-dest behavior. */
1338 } else
1339#endif
48224e4c 1340 if (!copy_dest)
1c3344a1 1341 goto cleanup;
f4164b73
WD
1342 itemizing = 0;
1343 code = FNONE;
116a4769
WD
1344 } else if (j >= 0)
1345 statret = 1;
2f03f956 1346 }
1e1ca253 1347#ifdef SUPPORT_HARD_LINKS
b7cfb9e2 1348 if (preserve_hard_links && F_HLINK_NOT_LAST(file))
1c3344a1 1349 goto cleanup;
1e1ca253 1350#endif
82ad07c4 1351 if (do_symlink(sl, fname) != 0) {
d62bcc17 1352 rsyserr(FERROR, errno, "symlink %s -> \"%s\" failed",
82ad07c4 1353 full_fname(fname), sl);
2f03f956 1354 } else {
16edf865 1355 set_file_attrs(fname, file, NULL, NULL, 0);
6c3862fa 1356 if (itemizing) {
1c3344a1 1357 itemize(fname, file, ndx, statret, &sx,
ef20efcb 1358 ITEM_LOCAL_CHANGE, 0, NULL);
6c3862fa 1359 }
116a4769 1360 if (code != FNONE && verbose)
82ad07c4 1361 rprintf(code, "%s -> %s\n", fname, sl);
1e1ca253 1362#ifdef SUPPORT_HARD_LINKS
112d728f 1363 if (preserve_hard_links && F_IS_HLINKED(file))
18979194 1364 finish_hard_link(file, fname, NULL, itemizing, code, -1);
1e1ca253 1365#endif
f964ac5e
WD
1366 /* This does not check remove_source_files == 1
1367 * because this is one of the items that the old
1368 * --remove-sent-files option would remove. */
47c11975 1369 if (remove_source_files)
04cd8789 1370 goto return_with_success;
2f03f956
AT
1371 }
1372#endif
1c3344a1 1373 goto cleanup;
2f03f956
AT
1374 }
1375
b5c6a6ae
WD
1376 if ((am_root && preserve_devices && IS_DEVICE(file->mode))
1377 || (preserve_specials && IS_SPECIAL(file->mode))) {
49b86442
WD
1378 uint32 *devp = F_RDEV_P(file);
1379 dev_t rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
116a4769 1380 if (statret == 0) {
c6fadc0e
WD
1381 char *t;
1382 if (IS_DEVICE(file->mode)) {
1c3344a1 1383 if (!IS_DEVICE(sx.st.st_mode))
c6fadc0e
WD
1384 statret = -1;
1385 t = "device file";
1386 } else {
1c3344a1 1387 if (!IS_SPECIAL(sx.st.st_mode))
c6fadc0e
WD
1388 statret = -1;
1389 t = "special file";
1390 }
1391 if (statret == 0
1c3344a1
WD
1392 && BITS_EQUAL(sx.st.st_mode, file->mode, _S_IFMT)
1393 && sx.st.st_rdev == rdev) {
116a4769 1394 /* The device or special file is identical. */
16edf865 1395 set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
116a4769 1396 if (itemizing)
1c3344a1 1397 itemize(fname, file, ndx, 0, &sx, 0, 0, NULL);
1e1ca253 1398#ifdef SUPPORT_HARD_LINKS
112d728f 1399 if (preserve_hard_links && F_IS_HLINKED(file))
1c3344a1 1400 finish_hard_link(file, fname, &sx.st, itemizing, code, -1);
1e1ca253 1401#endif
116a4769
WD
1402 if (remove_source_files == 1)
1403 goto return_with_success;
1c3344a1 1404 goto cleanup;
116a4769 1405 }
1c3344a1
WD
1406 if (delete_item(fname, sx.st.st_mode, t, del_opts) != 0)
1407 goto cleanup;
116a4769 1408 } else if (basis_dir[0] != NULL) {
1c3344a1 1409 int j = try_dests_non(file, fname, ndx, fnamecmpbuf, &sx,
18979194 1410 itemizing, code);
116a4769 1411 if (j == -2) {
ba212fe0
WD
1412#ifndef CAN_HARDLINK_SPECIAL
1413 if (link_dest) {
1414 /* Resort to --copy-dest behavior. */
1415 } else
1416#endif
48224e4c 1417 if (!copy_dest)
1c3344a1 1418 goto cleanup;
f4164b73
WD
1419 itemizing = 0;
1420 code = FNONE;
116a4769
WD
1421 } else if (j >= 0)
1422 statret = 1;
48224e4c 1423 }
1e1ca253 1424#ifdef SUPPORT_HARD_LINKS
b7cfb9e2 1425 if (preserve_hard_links && F_HLINK_NOT_LAST(file))
1c3344a1 1426 goto cleanup;
1e1ca253 1427#endif
116a4769 1428 if (verbose > 2) {
f7d7fb38
WD
1429 rprintf(FINFO, "mknod(%s, 0%o, [%ld,%ld])\n",
1430 fname, (int)file->mode,
1431 (long)major(rdev), (long)minor(rdev));
116a4769 1432 }
82ad07c4 1433 if (do_mknod(fname, file->mode, rdev) < 0) {
116a4769
WD
1434 rsyserr(FERROR, errno, "mknod %s failed",
1435 full_fname(fname));
2f03f956 1436 } else {
16edf865 1437 set_file_attrs(fname, file, NULL, NULL, 0);
116a4769 1438 if (itemizing) {
1c3344a1 1439 itemize(fname, file, ndx, statret, &sx,
116a4769
WD
1440 ITEM_LOCAL_CHANGE, 0, NULL);
1441 }
1442 if (code != FNONE && verbose)
1443 rprintf(code, "%s\n", fname);
1e1ca253 1444#ifdef SUPPORT_HARD_LINKS
112d728f 1445 if (preserve_hard_links && F_IS_HLINKED(file))
18979194 1446 finish_hard_link(file, fname, NULL, itemizing, code, -1);
1e1ca253 1447#endif
47c11975 1448 if (remove_source_files == 1)
04cd8789 1449 goto return_with_success;
2f03f956 1450 }
1c3344a1 1451 goto cleanup;
2f03f956 1452 }
2f03f956 1453
2f03f956 1454 if (!S_ISREG(file->mode)) {
49b86442 1455 if (solo_file)
6cbde57d 1456 fname = f_name(file, NULL);
45c49b52 1457 rprintf(FINFO, "skipping non-regular file \"%s\"\n", fname);
1c3344a1 1458 goto cleanup;
2f03f956
AT
1459 }
1460
f3d6d480 1461 if (max_size > 0 && F_LENGTH(file) > max_size) {
289a3216 1462 if (verbose > 1) {
49b86442 1463 if (solo_file)
6cbde57d 1464 fname = f_name(file, NULL);
45c49b52 1465 rprintf(FINFO, "%s is over max-size\n", fname);
289a3216 1466 }
1c3344a1 1467 goto cleanup;
289a3216 1468 }
f3d6d480 1469 if (min_size > 0 && F_LENGTH(file) < min_size) {
02b5cb23 1470 if (verbose > 1) {
49b86442 1471 if (solo_file)
6cbde57d 1472 fname = f_name(file, NULL);
45c49b52 1473 rprintf(FINFO, "%s is under min-size\n", fname);
02b5cb23 1474 }
1c3344a1 1475 goto cleanup;
02b5cb23 1476 }
289a3216 1477
f3d6d480 1478 if (ignore_existing > 0 && statret == 0) {
c3cbcfb8 1479 if (verbose > 1)
45c49b52 1480 rprintf(FINFO, "%s exists\n", fname);
1c3344a1 1481 goto cleanup;
c3cbcfb8
WD
1482 }
1483
f3d6d480 1484 if (update_only > 0 && statret == 0
1c3344a1 1485 && cmp_time(sx.st.st_mtime, file->modtime) > 0) {
c3cbcfb8 1486 if (verbose > 1)
45c49b52 1487 rprintf(FINFO, "%s is newer\n", fname);
1c3344a1 1488 goto cleanup;
c3cbcfb8
WD
1489 }
1490
375a4556 1491 fnamecmp = fname;
41cfde6b 1492 fnamecmp_type = FNAMECMP_FNAME;
375a4556 1493
1c3344a1
WD
1494 if (statret == 0 && !S_ISREG(sx.st.st_mode)) {
1495 if (delete_item(fname, sx.st.st_mode, "regular file", del_opts) != 0)
1496 goto cleanup;
1f1d368a
WD
1497 statret = -1;
1498 stat_errno = ENOENT;
1499 }
1500
06a1dbad 1501 if (statret != 0 && basis_dir[0] != NULL) {
1c3344a1 1502 int j = try_dests_reg(file, fname, ndx, fnamecmpbuf, &sx,
18979194 1503 itemizing, code);
04cd8789 1504 if (j == -2) {
47c11975 1505 if (remove_source_files == 1)
04cd8789 1506 goto return_with_success;
1c3344a1 1507 goto cleanup;
04cd8789 1508 }
b06050f9 1509 if (j >= 0) {
48224e4c
WD
1510 fnamecmp = fnamecmpbuf;
1511 fnamecmp_type = j;
9ba46343 1512 statret = 0;
e7d13fe5
WD
1513 }
1514 }
1515
1f1d368a 1516 real_ret = statret;
1c3344a1 1517 real_sx = sx;
375a4556 1518
9d954dca 1519 if (partial_dir && (partialptr = partial_dir_fname(fname)) != NULL
72c19bb3
WD
1520 && link_stat(partialptr, &partial_st, 0) == 0
1521 && S_ISREG(partial_st.st_mode)) {
06a1dbad 1522 if (statret != 0)
72c19bb3
WD
1523 goto prepare_to_open;
1524 } else
1525 partialptr = NULL;
89f7eff3 1526
ccb16b46 1527 if (statret != 0 && fuzzy_dirlist && dry_run <= 1) {
8e85be0a
WD
1528 int j = find_fuzzy(file, fuzzy_dirlist);
1529 if (j >= 0) {
1530 fuzzy_file = fuzzy_dirlist->files[j];
6cbde57d 1531 f_name(fuzzy_file, fnamecmpbuf);
8e85be0a
WD
1532 if (verbose > 2) {
1533 rprintf(FINFO, "fuzzy basis selected for %s: %s\n",
45c49b52 1534 fname, fnamecmpbuf);
8e85be0a 1535 }
1c3344a1 1536 sx.st.st_size = F_LENGTH(fuzzy_file);
8e85be0a
WD
1537 statret = 0;
1538 fnamecmp = fnamecmpbuf;
1539 fnamecmp_type = FNAMECMP_FUZZY;
1540 }
1541 }
1542
06a1dbad 1543 if (statret != 0) {
1e1ca253 1544#ifdef SUPPORT_HARD_LINKS
b7cfb9e2 1545 if (preserve_hard_links && F_HLINK_NOT_LAST(file))
1c3344a1 1546 goto cleanup;
1e1ca253 1547#endif
41cfde6b
WD
1548 if (stat_errno == ENOENT)
1549 goto notify_others;
991daf00
WD
1550 rsyserr(FERROR, stat_errno, "recv_generator: failed to stat %s",
1551 full_fname(fname));
1c3344a1 1552 goto cleanup;
2f03f956
AT
1553 }
1554
1c3344a1
WD
1555 if (append_mode > 0 && sx.st.st_size > F_LENGTH(file))
1556 goto cleanup;
6cc11982 1557
48224e4c 1558 if (fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
b7e8628c 1559 ;
8e85be0a
WD
1560 else if (fnamecmp_type == FNAMECMP_FUZZY)
1561 ;
1c3344a1 1562 else if (unchanged_file(fnamecmp, file, &sx.st)) {
d8b108c2
WD
1563 if (partialptr) {
1564 do_unlink(partialptr);
1565 handle_partial_dir(partialptr, PDIR_DELETE);
1566 }
16edf865 1567 set_file_attrs(fname, file, &sx, NULL, maybe_ATTRS_REPORT);
0fddbd8a 1568 if (itemizing)
1c3344a1 1569 itemize(fnamecmp, file, ndx, statret, &sx, 0, 0, NULL);
1e1ca253 1570#ifdef SUPPORT_HARD_LINKS
112d728f 1571 if (preserve_hard_links && F_IS_HLINKED(file))
1c3344a1 1572 finish_hard_link(file, fname, &sx.st, itemizing, code, -1);
1e1ca253 1573#endif
47c11975 1574 if (remove_source_files != 1)
1c3344a1 1575 goto cleanup;
04cd8789 1576 return_with_success:
663b2857 1577 if (!dry_run)
ab3d6c60 1578 send_msg_int(MSG_SUCCESS, ndx);
1c3344a1 1579 goto cleanup;
2f03f956
AT
1580 }
1581
b2e7c913 1582 prepare_to_open:
9d954dca 1583 if (partialptr) {
1c3344a1 1584 sx.st = partial_st;
9d954dca
WD
1585 fnamecmp = partialptr;
1586 fnamecmp_type = FNAMECMP_PARTIAL_DIR;
1587 statret = 0;
1588 }
1589
beb51aa0 1590 if (!do_xfers || read_batch || whole_file)
3841a04e 1591 goto notify_others;
2f03f956 1592
ccb16b46 1593 if (fuzzy_dirlist) {
8e85be0a
WD
1594 int j = flist_find(fuzzy_dirlist, file);
1595 if (j >= 0) /* don't use changing file as future fuzzy basis */
f3d6d480 1596 fuzzy_dirlist->files[j]->flags |= FLAG_FILE_SENT;
8e85be0a
WD
1597 }
1598
2cda2560 1599 /* open the file */
8c9fd200 1600 fd = do_open(fnamecmp, O_RDONLY, 0);
2f03f956
AT
1601
1602 if (fd == -1) {
d62bcc17
WD
1603 rsyserr(FERROR, errno, "failed to open %s, continuing",
1604 full_fname(fnamecmp));
b2e7c913 1605 pretend_missing:
60be6acf 1606 /* pretend the file didn't exist */
1e1ca253 1607#ifdef SUPPORT_HARD_LINKS
b7cfb9e2 1608 if (preserve_hard_links && F_HLINK_NOT_LAST(file))
1c3344a1 1609 goto cleanup;
1e1ca253 1610#endif
1f1d368a 1611 statret = real_ret = -1;
41cfde6b 1612 goto notify_others;
2f03f956
AT
1613 }
1614
f3d6d480 1615 if (inplace && make_backups > 0 && fnamecmp_type == FNAMECMP_FNAME) {
cd6aa5b5
WD
1616 if (!(backupptr = get_backup_name(fname))) {
1617 close(fd);
1c3344a1 1618 goto cleanup;
cd6aa5b5 1619 }
3de73827 1620 if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS))) {
cd6aa5b5
WD
1621 close(fd);
1622 goto pretend_missing;
1623 }
1624 if (robust_unlink(backupptr) && errno != ENOENT) {
1625 rsyserr(FERROR, errno, "unlink %s",
1626 full_fname(backupptr));
82ad07c4 1627 unmake_file(back_file);
cd6aa5b5 1628 close(fd);
1c3344a1 1629 goto cleanup;
cd6aa5b5
WD
1630 }
1631 if ((f_copy = do_open(backupptr,
1632 O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0) {
1633 rsyserr(FERROR, errno, "open %s",
1634 full_fname(backupptr));
82ad07c4 1635 unmake_file(back_file);
cd6aa5b5 1636 close(fd);
1c3344a1 1637 goto cleanup;
cd6aa5b5 1638 }
41cfde6b 1639 fnamecmp_type = FNAMECMP_BACKUP;
cd6aa5b5
WD
1640 }
1641
dfd5ba6a 1642 if (verbose > 3) {
ecc81fce 1643 rprintf(FINFO, "gen mapped %s of size %.0f\n",
1c3344a1 1644 fnamecmp, (double)sx.st.st_size);
dfd5ba6a 1645 }
2f03f956 1646
2f03f956 1647 if (verbose > 2)
0492fdfb 1648 rprintf(FINFO, "generating and sending sums for %d\n", ndx);
2f03f956 1649
b2e7c913 1650 notify_others:
47c11975 1651 if (remove_source_files && !delay_updates && !phase)
04cd8789 1652 increment_active_files(ndx, itemizing, code);
3ea6e0e7 1653 if (inc_recurse && !dry_run)
f3d6d480 1654 cur_flist->in_progress++;
18979194
WD
1655#ifdef SUPPORT_HARD_LINKS
1656 if (preserve_hard_links && F_IS_HLINKED(file))
f3d6d480 1657 file->flags |= FLAG_FILE_SENT;
18979194 1658#endif
ab3d6c60 1659 write_ndx(f_out, ndx);
6c3862fa 1660 if (itemizing) {
ee1d11c4 1661 int iflags = ITEM_TRANSFER;
f3d6d480 1662 if (always_checksum > 0)
a1d23b53 1663 iflags |= ITEM_REPORT_CHECKSUM;
352963dd 1664 if (fnamecmp_type != FNAMECMP_FNAME)
ef20efcb
WD
1665 iflags |= ITEM_BASIS_TYPE_FOLLOWS;
1666 if (fnamecmp_type == FNAMECMP_FUZZY)
1667 iflags |= ITEM_XNAME_FOLLOWS;
1c3344a1 1668 itemize(fnamecmp, file, -1, real_ret, &real_sx, iflags, fnamecmp_type,
c58c1dc4 1669 fuzzy_file ? fuzzy_file->basename : NULL);
1c3344a1
WD
1670#ifdef SUPPORT_ACLS
1671 if (preserve_acls)
1672 free_acl(&real_sx);
16edf865
WD
1673#endif
1674#ifdef SUPPORT_XATTRS
1675 if (preserve_xattrs)
1676 free_xattr(&real_sx);
1c3344a1 1677#endif
8e85be0a 1678 }
cd6aa5b5 1679
beb51aa0 1680 if (!do_xfers) {
1e1ca253 1681#ifdef SUPPORT_HARD_LINKS
112d728f 1682 if (preserve_hard_links && F_IS_HLINKED(file))
1c3344a1 1683 finish_hard_link(file, fname, &sx.st, itemizing, code, -1);
1e1ca253 1684#endif
1c3344a1 1685 goto cleanup;
ee1d11c4
WD
1686 }
1687 if (read_batch)
1c3344a1 1688 goto cleanup;
41cfde6b 1689
33ab4ad8 1690 if (statret != 0 || whole_file) {
e86ae6bc 1691 write_sum_head(f_out, NULL);
1c3344a1 1692 goto cleanup;
e86ae6bc
WD
1693 }
1694
1c3344a1 1695 generate_and_send_sums(fd, sx.st.st_size, f_out, f_copy);
e86ae6bc
WD
1696
1697 if (f_copy >= 0) {
1698 close(f_copy);
16edf865 1699 set_file_attrs(backupptr, back_file, NULL, NULL, 0);
e86ae6bc
WD
1700 if (verbose > 1) {
1701 rprintf(FINFO, "backed up %s to %s\n",
45c49b52 1702 fname, backupptr);
41cfde6b 1703 }
82ad07c4 1704 unmake_file(back_file);
e86ae6bc 1705 }
41cfde6b 1706
e86ae6bc 1707 close(fd);
1c3344a1
WD
1708
1709 cleanup:
1710#ifdef SUPPORT_ACLS
1711 if (preserve_acls)
1712 free_acl(&sx);
16edf865
WD
1713#endif
1714#ifdef SUPPORT_XATTRS
1715 if (preserve_xattrs)
1716 free_xattr(&sx);
1c3344a1
WD
1717#endif
1718 return;
2f03f956
AT
1719}
1720
7730114b 1721static void touch_up_dirs(struct file_list *flist, int ndx)
fd3f5af2
WD
1722{
1723 struct file_struct *file;
1724 char *fname;
1725 int i, j, start, end;
1726
1727 if (ndx < 0) {
1728 start = 0;
1729 end = flist->count - 1;
1730 } else
1731 start = end = ndx;
1732
1733 /* Fix any directory permissions that were modified during the
1734 * transfer and/or re-set any tweaked modified-time values. */
1735 for (i = start, j = 0; i <= end; i++) {
1befb74b 1736 file = flist->files[i];
fd3f5af2
WD
1737 if (!F_IS_ACTIVE(file) || !S_ISDIR(file->mode)
1738 || file->flags & FLAG_MISSING_DIR)
1739 continue;
1740 if (!need_retouch_dir_times && file->mode & S_IWUSR)
1741 continue;
1742 fname = f_name(file, NULL);
1743 if (!(file->mode & S_IWUSR))
1744 do_chmod(fname, file->mode);
1745 if (need_retouch_dir_times)
1746 set_modtime(fname, file->modtime, file->mode);
1747 if (allowed_lull && !(++j % lull_mod))
1748 maybe_send_keepalive();
1749 else if (!(j % 200))
be91bd81 1750 maybe_flush_socket(0);
fd3f5af2
WD
1751 }
1752}
1753
7730114b
WD
1754void check_for_finished_files(int itemizing, enum logcode code, int check_redo)
1755{
1756 struct file_struct *file;
1757 struct file_list *flist;
1758 char fbuf[MAXPATHLEN];
1759 int ndx;
1760
1761#ifdef SUPPORT_HARD_LINKS
1762 while (preserve_hard_links && (ndx = get_hlink_num()) != -1) {
1763 flist = flist_for_ndx(ndx);
1764 assert(flist != NULL);
1765 file = flist->files[ndx];
1766 assert(file->flags & FLAG_HLINKED);
1767 finish_hard_link(file, f_name(file, fbuf), NULL, itemizing, code, -1);
1768 }
1769#endif
1770
1771 while (check_redo && (ndx = get_redo_num()) != -1) {
1772 csum_length = SUM_LENGTH;
1773 max_size = -max_size;
1774 min_size = -min_size;
1775 ignore_existing = -ignore_existing;
1776 ignore_non_existing = -ignore_non_existing;
1777 update_only = -update_only;
1778 always_checksum = -always_checksum;
1779 size_only = -size_only;
1780 append_mode = -append_mode;
1781 make_backups = -make_backups; /* avoid dup backup w/inplace */
1782 ignore_times++;
1783
1784 flist = cur_flist;
1785 cur_flist = flist_for_ndx(ndx);
7730114b 1786
ab3d6c60 1787 file = cur_flist->files[ndx - cur_flist->ndx_start];
7730114b
WD
1788 if (solo_file)
1789 strlcpy(fbuf, solo_file, sizeof fbuf);
1790 else
1791 f_name(file, fbuf);
1792 recv_generator(fbuf, file, ndx, itemizing, code, sock_f_out);
1793 cur_flist->to_redo--;
1794
1795 cur_flist = flist;
1796
1797 csum_length = SHORT_SUM_LENGTH;
1798 max_size = -max_size;
1799 min_size = -min_size;
1800 ignore_existing = -ignore_existing;
1801 ignore_non_existing = -ignore_non_existing;
1802 update_only = -update_only;
1803 always_checksum = -always_checksum;
1804 size_only = -size_only;
1805 append_mode = -append_mode;
1806 make_backups = -make_backups;
1807 ignore_times--;
1808 }
1809
1810 while (cur_flist != first_flist) { /* only possible with inc_recurse */
1811 if (first_flist->in_progress || first_flist->to_redo)
1812 break;
1813
be91bd81 1814 if (!read_batch) {
7730114b 1815 write_ndx(sock_f_out, NDX_DONE);
be91bd81
WD
1816 maybe_flush_socket(1);
1817 }
7730114b
WD
1818
1819 if (delete_during == 2 || !dir_tweaking) {
1820 /* Skip directory touch-up. */
1821 } else if (first_flist->ndx_start != 0)
1822 touch_up_dirs(dir_flist, first_flist->parent_ndx);
1823 else if (relative_paths && implied_dirs)
1824 touch_up_dirs(first_flist, -1);
1825
1826 flist_free(first_flist); /* updates first_flist */
1827 }
1828}
1829
1830void generate_files(int f_out, const char *local_name)
2f03f956 1831{
332cf6df 1832 int i, ndx;
968c8030 1833 char fbuf[MAXPATHLEN];
18979194 1834 int itemizing;
7433d73a 1835 enum logcode code;
3ea9bbd6 1836 int need_retouch_dir_perms = 0;
083acd49 1837 int save_do_progress = do_progress;
ee1d11c4 1838
7433d73a
WD
1839 if (protocol_version >= 29) {
1840 itemizing = 1;
17bda2d1 1841 maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
f4164b73 1842 code = logfile_format_has_i ? FNONE : FLOG;
7433d73a 1843 } else if (am_daemon) {
ecc7623e 1844 itemizing = logfile_format_has_i && do_xfers;
e912bd4d 1845 maybe_ATTRS_REPORT = ATTRS_REPORT;
beb51aa0 1846 code = itemizing || !do_xfers ? FCLIENT : FINFO;
7433d73a 1847 } else if (!am_server) {
17bda2d1
WD
1848 itemizing = stdout_format_has_i;
1849 maybe_ATTRS_REPORT = stdout_format_has_i ? 0 : ATTRS_REPORT;
f4164b73 1850 code = itemizing ? FNONE : FINFO;
7433d73a
WD
1851 } else {
1852 itemizing = 0;
e912bd4d 1853 maybe_ATTRS_REPORT = ATTRS_REPORT;
7433d73a
WD
1854 code = FINFO;
1855 }
7730114b
WD
1856 solo_file = local_name;
1857 dir_tweaking = !(list_only || solo_file || dry_run);
1858 need_retouch_dir_times = preserve_times && !omit_dir_times;
1859 lull_mod = allowed_lull * 5;
2f03f956 1860
f3d6d480
WD
1861 if (verbose > 2)
1862 rprintf(FINFO, "generator starting pid=%ld\n", (long)getpid());
2f03f956 1863
7730114b 1864 if (delete_before && !solo_file && cur_flist->count > 0)
332cf6df 1865 do_delete_pass();
20319fd9
WD
1866 if (delete_during == 2) {
1867 deldelay_size = BIGPATHBUFLEN * 4;
1868 deldelay_buf = new_array(char, deldelay_size);
1869 if (!deldelay_buf)
1870 out_of_memory("delete-delay");
1871 }
083acd49 1872 do_progress = 0;
59faec8b 1873
f3d6d480 1874 if (append_mode > 0 || whole_file < 0)
33ab4ad8 1875 whole_file = 0;
3e7053ac 1876 if (verbose >= 2) {
1b1fef20 1877 rprintf(FINFO, "delta-transmission %s\n",
33ab4ad8 1878 whole_file
1b1fef20
WD
1879 ? "disabled for local transfer or --whole-file"
1880 : "enabled");
3e7053ac 1881 }
2cda2560 1882
513fd04d
WD
1883 /* Since we often fill up the outgoing socket and then just sit around
1884 * waiting for the other 2 processes to do their thing, we don't want
1885 * to exit on a timeout. If the data stops flowing, the receiver will
1886 * notice that and let us know via the redo pipe (or its closing). */
1887 ignore_timeout = 1;
a57873b7 1888
1c3344a1
WD
1889 dflt_perms = (ACCESSPERMS & ~orig_umask);
1890
f3d6d480 1891 do {
3ea6e0e7 1892 if (inc_recurse && delete_during && cur_flist->ndx_start) {
f3d6d480
WD
1893 struct file_struct *fp = dir_flist->files[cur_flist->parent_ndx];
1894 if (BITS_SETnUNSET(fp->flags, FLAG_XFER_DIR, FLAG_MISSING_DIR)) {
1895 dev_t dirdev;
1896 if (one_file_system) {
1897 uint32 *devp = F_DIRDEV_P(fp);
1898 dirdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp));
1899 } else
1900 dirdev = MAKEDEV(0, 0);
332cf6df 1901 delete_in_dir(f_name(fp, fbuf), fp, &dirdev);
f3d6d480
WD
1902 }
1903 }
1904 for (i = cur_flist->low; i <= cur_flist->high; i++) {
332cf6df 1905 struct file_struct *file = cur_flist->sorted[i];
2f03f956 1906
f3d6d480
WD
1907 if (!F_IS_ACTIVE(file))
1908 continue;
0492fdfb 1909
332cf6df
WD
1910#ifdef ICONV_OPTION
1911 if (ic_ndx)
1912 ndx = F_NDX(file);
1913 else
1914#endif
1915 ndx = i + cur_flist->ndx_start;
1916
7730114b
WD
1917 if (solo_file)
1918 strlcpy(fbuf, solo_file, sizeof fbuf);
f3d6d480
WD
1919 else
1920 f_name(file, fbuf);
332cf6df 1921 recv_generator(fbuf, file, ndx, itemizing, code, f_out);
1f7e29b9 1922
f3d6d480
WD
1923 /* We need to ensure that any dirs we create have
1924 * writeable permissions during the time we are putting
1925 * files within them. This is then fixed after the
1926 * transfer is done. */
00b96184 1927#ifdef HAVE_CHMOD
f3d6d480
WD
1928 if (!am_root && S_ISDIR(file->mode)
1929 && !(file->mode & S_IWUSR) && dir_tweaking) {
1930 mode_t mode = file->mode | S_IWUSR;
7730114b 1931 const char *fname = solo_file ? solo_file : fbuf;
f3d6d480
WD
1932 if (do_chmod(fname, mode) < 0) {
1933 rsyserr(FERROR, errno,
1934 "failed to modify permissions on %s",
1935 full_fname(fname));
1936 }
1937 need_retouch_dir_perms = 1;
1f7e29b9 1938 }
00b96184 1939#endif
2f03f956 1940
1ff66d4c 1941 check_for_finished_files(itemizing, code, 0);
9ac2395b 1942
f3d6d480
WD
1943 if (allowed_lull && !(i % lull_mod))
1944 maybe_send_keepalive();
1945 else if (!(i % 200))
be91bd81 1946 maybe_flush_socket(0);
f3d6d480 1947 }
2f03f956 1948
3ea6e0e7 1949 if (!inc_recurse) {
9ae7a2cd 1950 write_ndx(f_out, NDX_DONE);
f3d6d480 1951 break;
7730114b 1952 }
f3d6d480 1953
896f046f 1954 while (1) {
7730114b 1955 check_for_finished_files(itemizing, code, 1);
896f046f
WD
1956 if (cur_flist->next || flist_eof)
1957 break;
f3d6d480 1958 wait_for_receiver();
7730114b
WD
1959 }
1960 } while ((cur_flist = cur_flist->next) != NULL);
0492fdfb 1961
7730114b 1962 if (delete_during)
332cf6df 1963 delete_in_dir(NULL, NULL, &dev_zero);
7730114b
WD
1964 phase++;
1965 if (verbose > 2)
1966 rprintf(FINFO, "generate_files phase=%d\n", phase);
f3d6d480 1967
896f046f 1968 while (1) {
7730114b 1969 check_for_finished_files(itemizing, code, 1);
896f046f
WD
1970 if (msgdone_cnt)
1971 break;
7730114b
WD
1972 wait_for_receiver();
1973 }
f3d6d480
WD
1974
1975 phase++;
bc63ae3f 1976 if (verbose > 2)
f7d7fb38 1977 rprintf(FINFO, "generate_files phase=%d\n", phase);
2f03f956 1978
9ae7a2cd 1979 write_ndx(f_out, NDX_DONE);
70352269
WD
1980 /* Reduce round-trip lag-time for a useless delay-updates phase. */
1981 if (protocol_version >= 29 && !delay_updates)
9ae7a2cd 1982 write_ndx(f_out, NDX_DONE);
6dff5992 1983
0438f100 1984 /* Read MSG_DONE for the redo phase (and any prior messages). */
896f046f 1985 while (1) {
7730114b 1986 check_for_finished_files(itemizing, code, 0);
896f046f
WD
1987 if (msgdone_cnt > 1)
1988 break;
f3d6d480
WD
1989 wait_for_receiver();
1990 }
6dff5992 1991
0438f100
WD
1992 if (protocol_version >= 29) {
1993 phase++;
1994 if (verbose > 2)
1995 rprintf(FINFO, "generate_files phase=%d\n", phase);
70352269 1996 if (delay_updates)
9ae7a2cd 1997 write_ndx(f_out, NDX_DONE);
70352269 1998 /* Read MSG_DONE for delay-updates phase & prior messages. */
7730114b 1999 while (msgdone_cnt == 2)
f3d6d480 2000 wait_for_receiver();
0438f100
WD
2001 }
2002
083acd49 2003 do_progress = save_do_progress;
20319fd9
WD
2004 if (delete_during == 2)
2005 do_delayed_deletions(fbuf);
7730114b 2006 if (delete_after && !solo_file && file_total > 0)
332cf6df 2007 do_delete_pass();
59faec8b 2008
f3d6d480 2009 if ((need_retouch_dir_perms || need_retouch_dir_times)
7730114b 2010 && dir_tweaking && (!inc_recurse || delete_during == 2))
1befb74b 2011 touch_up_dirs(dir_flist, -1);
6dff5992 2012
e5e85283 2013 if (max_delete >= 0 && deletion_count > max_delete) {
f75a53e7
WD
2014 rprintf(FINFO,
2015 "Deletions stopped due to --max-delete limit (%d skipped)\n",
2016 deletion_count - max_delete);
2017 io_error |= IOERR_DEL_LIMIT;
2018 }
2019
6dff5992 2020 if (verbose > 2)
f7d7fb38 2021 rprintf(FINFO, "generate_files finished\n");
2f03f956 2022}