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