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