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