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