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