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