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