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