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