Call delete_in_dir(NULL, NULL, NULL) in do_delete_pass().
[rsync/rsync.git] / generator.c
... / ...
CommitLineData
1/* -*- c-file-style: "linux" -*-
2
3 rsync -- fast file replication program
4
5 Copyright (C) 1996-2000 by Andrew Tridgell
6 Copyright (C) Paul Mackerras 1996
7 Copyright (C) 2002 by Martin Pool <mbp@samba.org>
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
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22*/
23
24#include "rsync.h"
25
26extern int verbose;
27extern int dry_run;
28extern int do_xfers;
29extern int log_format_has_i;
30extern int log_format_has_o_or_i;
31extern int daemon_log_format_has_i;
32extern int am_root;
33extern int am_server;
34extern int am_daemon;
35extern int do_progress;
36extern int recurse;
37extern int relative_paths;
38extern int keep_dirlinks;
39extern int preserve_links;
40extern int preserve_devices;
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_before;
48extern int delete_during;
49extern int delete_after;
50extern int module_id;
51extern int ignore_errors;
52extern int remove_sent_files;
53extern int delay_updates;
54extern int update_only;
55extern int ignore_existing;
56extern int ignore_non_existing;
57extern int inplace;
58extern int append_mode;
59extern int make_backups;
60extern int csum_length;
61extern int ignore_times;
62extern int size_only;
63extern OFF_T max_size;
64extern OFF_T min_size;
65extern int io_error;
66extern int allowed_lull;
67extern int sock_f_out;
68extern int ignore_timeout;
69extern int protocol_version;
70extern int fuzzy_basis;
71extern int always_checksum;
72extern char *partial_dir;
73extern char *basis_dir[];
74extern int compare_dest;
75extern int copy_dest;
76extern int link_dest;
77extern int whole_file;
78extern int list_only;
79extern int read_batch;
80extern int orig_umask;
81extern int safe_symlinks;
82extern long block_size; /* "long" because popt can't set an int32. */
83extern int max_delete;
84extern int force_delete;
85extern int one_file_system;
86extern struct stats stats;
87extern dev_t filesystem_dev;
88extern char *backup_dir;
89extern char *backup_suffix;
90extern int backup_suffix_len;
91extern struct file_list *the_file_list;
92extern struct filter_list_struct server_filter_list;
93
94static int deletion_count = 0; /* used to implement --max-delete */
95
96
97static int is_backup_file(char *fn)
98{
99 int k = strlen(fn) - backup_suffix_len;
100 return k > 0 && strcmp(fn+k, backup_suffix) == 0;
101}
102
103
104/* Delete a file or directory. If DEL_FORCE_RECURSE is set in the flags, or if
105 * force_delete is set, this will delete recursively as long as DEL_NO_RECURSE
106 * is not set in the flags.
107 *
108 * Note that fname must point to a MAXPATHLEN buffer if the mode indicates it's
109 * a directory! (The buffer is used for recursion, but returned unchanged.)
110 */
111static int delete_item(char *fname, int mode, int flags)
112{
113 struct file_list *dirlist;
114 int j, dlen, zap_dir, ok;
115 unsigned remainder;
116 void *save_filters;
117 char *p;
118
119 if (!S_ISDIR(mode)) {
120 if (max_delete && ++deletion_count > max_delete)
121 return 0;
122 if (make_backups && (backup_dir || !is_backup_file(fname)))
123 ok = make_backup(fname);
124 else
125 ok = robust_unlink(fname) == 0;
126 if (ok) {
127 if (!(flags & DEL_TERSE))
128 log_delete(fname, mode);
129 return 0;
130 }
131 if (errno == ENOENT) {
132 deletion_count--;
133 return 0;
134 }
135 rsyserr(FERROR, errno, "delete_file: unlink %s failed",
136 full_fname(fname));
137 return -1;
138 }
139
140 zap_dir = (flags & DEL_FORCE_RECURSE || (force_delete && recurse))
141 && !(flags & DEL_NO_RECURSE);
142 if ((max_delete && ++deletion_count > max_delete)
143 || (dry_run && zap_dir)) {
144 ok = 0;
145 errno = ENOTEMPTY;
146 } else if (make_backups && !backup_dir && !is_backup_file(fname)
147 && !(flags & DEL_FORCE_RECURSE))
148 ok = make_backup(fname);
149 else
150 ok = do_rmdir(fname) == 0;
151 if (ok) {
152 if (!(flags & DEL_TERSE))
153 log_delete(fname, mode);
154 return 0;
155 }
156 if (errno == ENOENT) {
157 deletion_count--;
158 return 0;
159 }
160 if (!zap_dir) {
161 rsyserr(FERROR, errno, "delete_file: rmdir %s failed",
162 full_fname(fname));
163 return -1;
164 }
165 flags |= DEL_FORCE_RECURSE; /* mark subdir dels as not "in the way" */
166 deletion_count--;
167
168 dlen = strlen(fname);
169 save_filters = push_local_filters(fname, dlen);
170
171 dirlist = get_dirlist(fname, dlen, 0);
172
173 p = fname + dlen;
174 if (dlen != 1 || *fname != '/')
175 *p++ = '/';
176 remainder = MAXPATHLEN - (p - fname);
177
178 for (j = dirlist->count; j--; ) {
179 struct file_struct *fp = dirlist->files[j];
180
181 if (fp->flags & FLAG_MOUNT_POINT)
182 continue;
183
184 strlcpy(p, fp->basename, remainder);
185 delete_item(fname, fp->mode, flags & ~DEL_TERSE);
186 }
187 flist_free(dirlist);
188
189 fname[dlen] = '\0';
190
191 pop_local_filters(save_filters);
192
193 if (max_delete && ++deletion_count > max_delete)
194 return 0;
195
196 if (do_rmdir(fname) == 0) {
197 if (!(flags & DEL_TERSE))
198 log_delete(fname, mode);
199 } else if (errno != ENOTEMPTY && errno != EEXIST && errno != ENOENT) {
200 rsyserr(FERROR, errno, "delete_file: rmdir %s failed",
201 full_fname(fname));
202 return -1;
203 }
204
205 return 0;
206}
207
208
209/* This function is used to implement per-directory deletion, and is used by
210 * all the --delete-WHEN options. Note that the fbuf pointer must point to a
211 * MAXPATHLEN buffer with the name of the directory in it (the functions we
212 * call will append names onto the end, but the old dir value will be restored
213 * on exit). */
214static void delete_in_dir(struct file_list *flist, char *fbuf,
215 struct file_struct *file)
216{
217 static int min_depth = MAXPATHLEN, cur_depth = -1;
218 static void *filt_array[MAXPATHLEN/2+1];
219 static int already_warned = 0;
220 struct file_list *dirlist;
221 char delbuf[MAXPATHLEN];
222 STRUCT_STAT st;
223 int dlen, i;
224
225 if (!flist) {
226 while (cur_depth >= min_depth)
227 pop_local_filters(filt_array[cur_depth--]);
228 min_depth = MAXPATHLEN;
229 cur_depth = -1;
230 return;
231 }
232
233 if (verbose > 2)
234 rprintf(FINFO, "delete_in_dir(%s)\n", safe_fname(fbuf));
235
236 if (allowed_lull)
237 maybe_send_keepalive();
238
239 if (file->dir.depth >= MAXPATHLEN/2+1)
240 return; /* Impossible... */
241
242 if (io_error && !(lp_ignore_errors(module_id) || ignore_errors)) {
243 if (already_warned)
244 return;
245 rprintf(FINFO,
246 "IO error encountered -- skipping file deletion\n");
247 already_warned = 1;
248 return;
249 }
250
251 while (cur_depth >= file->dir.depth && cur_depth >= min_depth)
252 pop_local_filters(filt_array[cur_depth--]);
253 cur_depth = file->dir.depth;
254 if (min_depth > cur_depth)
255 min_depth = cur_depth;
256 dlen = strlen(fbuf);
257 filt_array[cur_depth] = push_local_filters(fbuf, dlen);
258
259 if (link_stat(fbuf, &st, keep_dirlinks) < 0)
260 return;
261
262 if (one_file_system) {
263 if (file->flags & FLAG_TOP_DIR)
264 filesystem_dev = st.st_dev;
265 else if (filesystem_dev != st.st_dev)
266 return;
267 }
268
269 dirlist = get_dirlist(fbuf, dlen, 0);
270
271 /* If an item in dirlist is not found in flist, delete it
272 * from the filesystem. */
273 for (i = dirlist->count; i--; ) {
274 struct file_struct *fp = dirlist->files[i];
275 if (!fp->basename || fp->flags & FLAG_MOUNT_POINT)
276 continue;
277 if (flist_find(flist, fp) < 0) {
278 int mode = fp->mode;
279 f_name_to(fp, delbuf);
280 delete_item(delbuf, mode, DEL_FORCE_RECURSE);
281 }
282 }
283
284 flist_free(dirlist);
285}
286
287/* This deletes any files on the receiving side that are not present on the
288 * sending side. This is used by --delete-before and --delete-after. */
289static void do_delete_pass(struct file_list *flist)
290{
291 char fbuf[MAXPATHLEN];
292 int j;
293
294 if (dry_run > 1 /* destination doesn't exist yet */
295 || list_only)
296 return;
297
298 for (j = 0; j < flist->count; j++) {
299 struct file_struct *file = flist->files[j];
300
301 if (!(file->flags & FLAG_DEL_HERE))
302 continue;
303
304 f_name_to(file, fbuf);
305 if (verbose > 1 && file->flags & FLAG_TOP_DIR)
306 rprintf(FINFO, "deleting in %s\n", safe_fname(fbuf));
307
308 delete_in_dir(flist, fbuf, file);
309 }
310 delete_in_dir(NULL, NULL, NULL);
311
312 if (do_progress && !am_server)
313 rprintf(FINFO, " \r");
314}
315
316static int unchanged_attrs(struct file_struct *file, STRUCT_STAT *st)
317{
318 if (preserve_perms
319 && (st->st_mode & CHMOD_BITS) != (file->mode & CHMOD_BITS))
320 return 0;
321
322 if (am_root && preserve_uid && st->st_uid != file->uid)
323 return 0;
324
325 if (preserve_gid && file->gid != GID_NONE && st->st_gid != file->gid)
326 return 0;
327
328 return 1;
329}
330
331
332void itemize(struct file_struct *file, int ndx, int statret, STRUCT_STAT *st,
333 int32 iflags, uchar fnamecmp_type, char *xname)
334{
335 if (statret == 0) {
336 if (S_ISREG(file->mode) && file->length != st->st_size)
337 iflags |= ITEM_REPORT_SIZE;
338 if (!(iflags & ITEM_NO_DEST_AND_NO_UPDATE)) {
339 int keep_time = !preserve_times ? 0
340 : S_ISDIR(file->mode) ? !omit_dir_times
341 : !S_ISLNK(file->mode);
342
343 if ((iflags & (ITEM_TRANSFER|ITEM_LOCAL_CHANGE) && !keep_time
344 && (!(iflags & ITEM_XNAME_FOLLOWS) || *xname))
345 || (keep_time && cmp_modtime(file->modtime, st->st_mtime) != 0))
346 iflags |= ITEM_REPORT_TIME;
347 if (preserve_perms
348 && (file->mode & CHMOD_BITS) != (st->st_mode & CHMOD_BITS))
349 iflags |= ITEM_REPORT_PERMS;
350 if (preserve_uid && am_root && file->uid != st->st_uid)
351 iflags |= ITEM_REPORT_OWNER;
352 if (preserve_gid && file->gid != GID_NONE
353 && st->st_gid != file->gid)
354 iflags |= ITEM_REPORT_GROUP;
355 }
356 } else
357 iflags |= ITEM_IS_NEW;
358
359 iflags &= 0xffff;
360 if ((iflags & SIGNIFICANT_ITEM_FLAGS || verbose > 1
361 || (xname && *xname)) && !read_batch) {
362 if (protocol_version >= 29) {
363 if (ndx >= 0)
364 write_int(sock_f_out, ndx);
365 write_shortint(sock_f_out, iflags);
366 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
367 write_byte(sock_f_out, fnamecmp_type);
368 if (iflags & ITEM_XNAME_FOLLOWS)
369 write_vstring(sock_f_out, xname, strlen(xname));
370 } else if (ndx >= 0)
371 log_item(file, &stats, iflags, xname);
372 }
373}
374
375
376/* Perform our quick-check heuristic for determining if a file is unchanged. */
377static int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st)
378{
379 if (st->st_size != file->length)
380 return 0;
381
382 /* if always checksum is set then we use the checksum instead
383 of the file time to determine whether to sync */
384 if (always_checksum && S_ISREG(st->st_mode)) {
385 char sum[MD4_SUM_LENGTH];
386 file_checksum(fn, sum, st->st_size);
387 return memcmp(sum, file->u.sum, protocol_version < 21 ? 2
388 : MD4_SUM_LENGTH) == 0;
389 }
390
391 if (size_only)
392 return 1;
393
394 if (ignore_times)
395 return 0;
396
397 return cmp_modtime(st->st_mtime, file->modtime) == 0;
398}
399
400
401/*
402 * set (initialize) the size entries in the per-file sum_struct
403 * calculating dynamic block and checksum sizes.
404 *
405 * This is only called from generate_and_send_sums() but is a separate
406 * function to encapsulate the logic.
407 *
408 * The block size is a rounded square root of file length.
409 *
410 * The checksum size is determined according to:
411 * blocksum_bits = BLOCKSUM_BIAS + 2*log2(file_len) - log2(block_len)
412 * provided by Donovan Baarda which gives a probability of rsync
413 * algorithm corrupting data and falling back using the whole md4
414 * checksums.
415 *
416 * This might be made one of several selectable heuristics.
417 */
418static void sum_sizes_sqroot(struct sum_struct *sum, int64 len)
419{
420 int32 blength;
421 int s2length;
422
423 if (block_size)
424 blength = block_size;
425 else if (len <= BLOCK_SIZE * BLOCK_SIZE)
426 blength = BLOCK_SIZE;
427 else {
428 int32 c;
429 int64 l;
430 int cnt;
431 for (c = 1, l = len, cnt = 0; l >>= 2; c <<= 1, cnt++) {}
432 if (cnt >= 31 || c >= MAX_BLOCK_SIZE)
433 blength = MAX_BLOCK_SIZE;
434 else {
435 blength = 0;
436 do {
437 blength |= c;
438 if (len < (int64)blength * blength)
439 blength &= ~c;
440 c >>= 1;
441 } while (c >= 8); /* round to multiple of 8 */
442 blength = MAX(blength, BLOCK_SIZE);
443 }
444 }
445
446 if (protocol_version < 27) {
447 s2length = csum_length;
448 } else if (csum_length == SUM_LENGTH) {
449 s2length = SUM_LENGTH;
450 } else {
451 int32 c;
452 int64 l;
453 int b = BLOCKSUM_BIAS;
454 for (l = len; l >>= 1; b += 2) {}
455 for (c = blength; c >>= 1 && b; b--) {}
456 /* add a bit, subtract rollsum, round up. */
457 s2length = (b + 1 - 32 + 7) / 8; /* --optimize in compiler-- */
458 s2length = MAX(s2length, csum_length);
459 s2length = MIN(s2length, SUM_LENGTH);
460 }
461
462 sum->flength = len;
463 sum->blength = blength;
464 sum->s2length = s2length;
465 sum->remainder = len % blength;
466 sum->count = len / blength + (sum->remainder != 0);
467
468 if (sum->count && verbose > 2) {
469 rprintf(FINFO,
470 "count=%.0f rem=%ld blength=%ld s2length=%d flength=%.0f\n",
471 (double)sum->count, (long)sum->remainder, (long)sum->blength,
472 sum->s2length, (double)sum->flength);
473 }
474}
475
476
477/*
478 * Generate and send a stream of signatures/checksums that describe a buffer
479 *
480 * Generate approximately one checksum every block_len bytes.
481 */
482static void generate_and_send_sums(int fd, OFF_T len, int f_out, int f_copy)
483{
484 int32 i;
485 struct map_struct *mapbuf;
486 struct sum_struct sum;
487 OFF_T offset = 0;
488
489 sum_sizes_sqroot(&sum, len);
490 write_sum_head(f_out, &sum);
491
492 if (append_mode > 0 && f_copy < 0)
493 return;
494
495 if (len > 0)
496 mapbuf = map_file(fd, len, MAX_MAP_SIZE, sum.blength);
497 else
498 mapbuf = NULL;
499
500 for (i = 0; i < sum.count; i++) {
501 int32 n1 = (int32)MIN(len, (OFF_T)sum.blength);
502 char *map = map_ptr(mapbuf, offset, n1);
503 char sum2[SUM_LENGTH];
504 uint32 sum1;
505
506 len -= n1;
507 offset += n1;
508
509 if (f_copy >= 0) {
510 full_write(f_copy, map, n1);
511 if (append_mode > 0)
512 continue;
513 }
514
515 sum1 = get_checksum1(map, n1);
516 get_checksum2(map, n1, sum2);
517
518 if (verbose > 3) {
519 rprintf(FINFO,
520 "chunk[%.0f] offset=%.0f len=%ld sum1=%08lx\n",
521 (double)i, (double)offset - n1, (long)n1,
522 (unsigned long)sum1);
523 }
524 write_int(f_out, sum1);
525 write_buf(f_out, sum2, sum.s2length);
526 }
527
528 if (mapbuf)
529 unmap_file(mapbuf);
530}
531
532
533/* Try to find a filename in the same dir as "fname" with a similar name. */
534static int find_fuzzy(struct file_struct *file, struct file_list *dirlist)
535{
536 int fname_len, fname_suf_len;
537 const char *fname_suf, *fname = file->basename;
538 uint32 lowest_dist = 25 << 16; /* ignore a distance greater than 25 */
539 int j, lowest_j = -1;
540
541 fname_len = strlen(fname);
542 fname_suf = find_filename_suffix(fname, fname_len, &fname_suf_len);
543
544 for (j = 0; j < dirlist->count; j++) {
545 struct file_struct *fp = dirlist->files[j];
546 const char *suf, *name;
547 int len, suf_len;
548 uint32 dist;
549
550 if (!S_ISREG(fp->mode) || !fp->length
551 || fp->flags & FLAG_NO_FUZZY)
552 continue;
553
554 name = fp->basename;
555
556 if (fp->length == file->length
557 && cmp_modtime(fp->modtime, file->modtime) == 0) {
558 if (verbose > 4) {
559 rprintf(FINFO,
560 "fuzzy size/modtime match for %s\n",
561 name);
562 }
563 return j;
564 }
565
566 len = strlen(name);
567 suf = find_filename_suffix(name, len, &suf_len);
568
569 dist = fuzzy_distance(name, len, fname, fname_len);
570 /* Add some extra weight to how well the suffixes match. */
571 dist += fuzzy_distance(suf, suf_len, fname_suf, fname_suf_len)
572 * 10;
573 if (verbose > 4) {
574 rprintf(FINFO, "fuzzy distance for %s = %d.%05d\n",
575 name, (int)(dist>>16), (int)(dist&0xFFFF));
576 }
577 if (dist <= lowest_dist) {
578 lowest_dist = dist;
579 lowest_j = j;
580 }
581 }
582
583 return lowest_j;
584}
585
586void check_for_finished_hlinks(int itemizing, enum logcode code)
587{
588 struct file_struct *file;
589 int ndx;
590
591 while ((ndx = get_hlink_num()) != -1) {
592 if (ndx < 0 || ndx >= the_file_list->count)
593 continue;
594
595 file = the_file_list->files[ndx];
596 if (!file->link_u.links)
597 continue;
598
599 hard_link_cluster(file, ndx, itemizing, code);
600 }
601}
602
603static int phase = 0;
604
605/* Acts on the_file_list->file's ndx'th item, whose name is fname. If a dir,
606 * make sure it exists, and has the right permissions/timestamp info. For
607 * all other non-regular files (symlinks, etc.) we create them here. For
608 * regular files that have changed, we try to find a basis file and then
609 * start sending checksums.
610 *
611 * When fname is non-null, it must point to a MAXPATHLEN buffer!
612 *
613 * Note that f_out is set to -1 when doing final directory-permission and
614 * modification-time repair. */
615static void recv_generator(char *fname, struct file_struct *file, int ndx,
616 int itemizing, int maybe_PERMS_REPORT,
617 enum logcode code, int f_out)
618{
619 static int missing_below = -1, excluded_below = -1;
620 static char *fuzzy_dirname = "";
621 static struct file_list *fuzzy_dirlist = NULL;
622 struct file_struct *fuzzy_file = NULL;
623 int fd = -1, f_copy = -1;
624 STRUCT_STAT st, real_st, partial_st;
625 struct file_struct *back_file = NULL;
626 int statret, real_ret, stat_errno;
627 char *fnamecmp, *partialptr, *backupptr = NULL;
628 char fnamecmpbuf[MAXPATHLEN];
629 uchar fnamecmp_type;
630
631 if (list_only)
632 return;
633
634 if (!fname) {
635 if (fuzzy_dirlist) {
636 flist_free(fuzzy_dirlist);
637 fuzzy_dirlist = NULL;
638 fuzzy_dirname = "";
639 }
640 if (missing_below >= 0) {
641 dry_run--;
642 missing_below = -1;
643 }
644 return;
645 }
646
647 if (verbose > 2) {
648 rprintf(FINFO, "recv_generator(%s,%d)\n",
649 safe_fname(fname), ndx);
650 }
651
652 if (server_filter_list.head) {
653 if (excluded_below >= 0) {
654 if (file->dir.depth > excluded_below)
655 goto skipping;
656 excluded_below = -1;
657 }
658 if (check_filter(&server_filter_list, fname,
659 S_ISDIR(file->mode)) < 0) {
660 if (S_ISDIR(file->mode))
661 excluded_below = file->dir.depth;
662 skipping:
663 if (verbose) {
664 rprintf(FINFO,
665 "skipping server-excluded file \"%s\"\n",
666 safe_fname(fname));
667 }
668 return;
669 }
670 }
671
672 if (missing_below >= 0 && file->dir.depth <= missing_below) {
673 dry_run--;
674 missing_below = -1;
675 }
676 if (dry_run > 1) {
677 statret = -1;
678 stat_errno = ENOENT;
679 } else {
680 if (fuzzy_basis && S_ISREG(file->mode)) {
681 char *dn = file->dirname ? file->dirname : ".";
682 if (fuzzy_dirname != dn
683 && strcmp(fuzzy_dirname, dn) != 0) {
684 if (fuzzy_dirlist)
685 flist_free(fuzzy_dirlist);
686 fuzzy_dirlist = get_dirlist(dn, -1, 1);
687 }
688 fuzzy_dirname = dn;
689 }
690
691 statret = link_stat(fname, &st,
692 keep_dirlinks && S_ISDIR(file->mode));
693 stat_errno = errno;
694 }
695
696 if (ignore_non_existing && statret == -1 && stat_errno == ENOENT) {
697 if (verbose > 1) {
698 rprintf(FINFO, "not creating new %s \"%s\"\n",
699 S_ISDIR(file->mode) ? "directory" : "file",
700 safe_fname(fname));
701 }
702 return;
703 }
704
705 if (statret == 0 && !preserve_perms
706 && S_ISDIR(st.st_mode) == S_ISDIR(file->mode)) {
707 /* if the file exists already and we aren't perserving
708 * permissions then act as though the remote end sent
709 * us the file permissions we already have */
710 file->mode = (file->mode & ~CHMOD_BITS)
711 | (st.st_mode & CHMOD_BITS);
712 }
713
714 if (S_ISDIR(file->mode)) {
715 /* The file to be received is a directory, so we need
716 * to prepare appropriately. If there is already a
717 * file of that name and it is *not* a directory, then
718 * we need to delete it. If it doesn't exist, then
719 * (perhaps recursively) create it. */
720 if (statret == 0 && !S_ISDIR(st.st_mode)) {
721 if (delete_item(fname, st.st_mode, DEL_TERSE) < 0)
722 return;
723 statret = -1;
724 }
725 if (dry_run && statret != 0 && missing_below < 0) {
726 missing_below = file->dir.depth;
727 dry_run++;
728 }
729 if (itemizing && f_out != -1) {
730 itemize(file, ndx, statret, &st,
731 statret ? ITEM_LOCAL_CHANGE : 0, 0, NULL);
732 }
733 if (statret != 0 && do_mkdir(fname,file->mode) < 0 && errno != EEXIST) {
734 if (!relative_paths || errno != ENOENT
735 || create_directory_path(fname, orig_umask) < 0
736 || (do_mkdir(fname, file->mode) < 0 && errno != EEXIST)) {
737 rsyserr(FERROR, errno,
738 "recv_generator: mkdir %s failed",
739 full_fname(fname));
740 }
741 }
742 if (set_perms(fname, file, statret ? NULL : &st, 0)
743 && verbose && code && f_out != -1)
744 rprintf(code, "%s/\n", safe_fname(fname));
745 if (delete_during && f_out != -1 && !phase && dry_run < 2
746 && (file->flags & FLAG_DEL_HERE))
747 delete_in_dir(the_file_list, fname, file);
748 return;
749 }
750
751 if (preserve_hard_links && file->link_u.links
752 && hard_link_check(file, ndx, fname, statret, &st,
753 itemizing, code, HL_CHECK_MASTER))
754 return;
755
756 if (preserve_links && S_ISLNK(file->mode)) {
757#ifdef SUPPORT_LINKS
758 if (safe_symlinks && unsafe_symlink(file->u.link, fname)) {
759 if (verbose) {
760 if (the_file_list->count == 1)
761 fname = f_name(file);
762 rprintf(FINFO,
763 "ignoring unsafe symlink %s -> \"%s\"\n",
764 full_fname(fname),
765 safe_fname(file->u.link));
766 }
767 return;
768 }
769 if (statret == 0) {
770 char lnk[MAXPATHLEN];
771 int len;
772
773 if (!S_ISDIR(st.st_mode)
774 && (len = readlink(fname, lnk, MAXPATHLEN-1)) > 0) {
775 lnk[len] = 0;
776 /* A link already pointing to the
777 * right place -- no further action
778 * required. */
779 if (strcmp(lnk, file->u.link) == 0) {
780 if (itemizing) {
781 itemize(file, ndx, 0, &st, 0,
782 0, NULL);
783 }
784 set_perms(fname, file, &st,
785 maybe_PERMS_REPORT);
786 if (preserve_hard_links
787 && file->link_u.links) {
788 hard_link_cluster(file, ndx,
789 itemizing,
790 code);
791 }
792 return;
793 }
794 }
795 /* Not the right symlink (or not a symlink), so
796 * delete it. */
797 if (delete_item(fname, st.st_mode, DEL_TERSE) < 0)
798 return;
799 if (!S_ISLNK(st.st_mode))
800 statret = -1;
801 }
802 if (preserve_hard_links && file->link_u.links
803 && hard_link_check(file, ndx, fname, -1, &st,
804 itemizing, code, HL_SKIP))
805 return;
806 if (do_symlink(file->u.link,fname) != 0) {
807 rsyserr(FERROR, errno, "symlink %s -> \"%s\" failed",
808 full_fname(fname), safe_fname(file->u.link));
809 } else {
810 set_perms(fname,file,NULL,0);
811 if (itemizing) {
812 itemize(file, ndx, statret, &st,
813 ITEM_LOCAL_CHANGE, 0, NULL);
814 }
815 if (code && verbose) {
816 rprintf(code, "%s -> %s\n", safe_fname(fname),
817 safe_fname(file->u.link));
818 }
819 if (remove_sent_files && !dry_run) {
820 char numbuf[4];
821 SIVAL(numbuf, 0, ndx);
822 send_msg(MSG_SUCCESS, numbuf, 4);
823 }
824 if (preserve_hard_links && file->link_u.links)
825 hard_link_cluster(file, ndx, itemizing, code);
826 }
827#endif
828 return;
829 }
830
831 if (am_root && preserve_devices && IS_DEVICE(file->mode)) {
832 if (statret != 0
833 || (st.st_mode & ~CHMOD_BITS) != (file->mode & ~CHMOD_BITS)
834 || st.st_rdev != file->u.rdev) {
835 if (statret == 0
836 && delete_item(fname, st.st_mode, DEL_TERSE) < 0)
837 return;
838 if (preserve_hard_links && file->link_u.links
839 && hard_link_check(file, ndx, fname, -1, &st,
840 itemizing, code, HL_SKIP))
841 return;
842 if (!IS_DEVICE(st.st_mode))
843 statret = -1;
844 if (verbose > 2) {
845 rprintf(FINFO,"mknod(%s,0%o,0x%x)\n",
846 safe_fname(fname),
847 (int)file->mode, (int)file->u.rdev);
848 }
849 if (do_mknod(fname,file->mode,file->u.rdev) < 0) {
850 rsyserr(FERROR, errno, "mknod %s failed",
851 full_fname(fname));
852 } else {
853 set_perms(fname,file,NULL,0);
854 if (itemizing) {
855 itemize(file, ndx, statret, &st,
856 ITEM_LOCAL_CHANGE, 0, NULL);
857 }
858 if (code && verbose) {
859 rprintf(code, "%s\n",
860 safe_fname(fname));
861 }
862 if (preserve_hard_links && file->link_u.links) {
863 hard_link_cluster(file, ndx,
864 itemizing, code);
865 }
866 }
867 } else {
868 if (itemizing)
869 itemize(file, ndx, statret, &st, 0, 0, NULL);
870 set_perms(fname, file, &st, maybe_PERMS_REPORT);
871 if (preserve_hard_links && file->link_u.links)
872 hard_link_cluster(file, ndx, itemizing, code);
873 }
874 return;
875 }
876
877 if (!S_ISREG(file->mode)) {
878 if (the_file_list->count == 1)
879 fname = f_name(file);
880 rprintf(FINFO, "skipping non-regular file \"%s\"\n",
881 safe_fname(fname));
882 return;
883 }
884
885 if (max_size && file->length > max_size) {
886 if (verbose > 1) {
887 if (the_file_list->count == 1)
888 fname = f_name(file);
889 rprintf(FINFO, "%s is over max-size\n",
890 safe_fname(fname));
891 }
892 return;
893 }
894 if (min_size && file->length < min_size) {
895 if (verbose > 1) {
896 if (the_file_list->count == 1)
897 fname = f_name(file);
898 rprintf(FINFO, "%s is under min-size\n",
899 safe_fname(fname));
900 }
901 return;
902 }
903
904 if (ignore_existing && statret == 0) {
905 if (verbose > 1)
906 rprintf(FINFO, "%s exists\n", safe_fname(fname));
907 return;
908 }
909
910 if (update_only && statret == 0
911 && cmp_modtime(st.st_mtime, file->modtime) > 0) {
912 if (verbose > 1)
913 rprintf(FINFO, "%s is newer\n", safe_fname(fname));
914 return;
915 }
916
917 fnamecmp = fname;
918 fnamecmp_type = FNAMECMP_FNAME;
919
920 if (statret == 0 && !S_ISREG(st.st_mode)) {
921 if (delete_item(fname, st.st_mode, DEL_TERSE) != 0)
922 return;
923 statret = -1;
924 stat_errno = ENOENT;
925 }
926
927 if (statret != 0 && basis_dir[0] != NULL) {
928 int best_match = -1;
929 int match_level = 0;
930 int i = 0;
931 do {
932 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
933 basis_dir[i], fname);
934 if (link_stat(fnamecmpbuf, &st, 0) < 0
935 || !S_ISREG(st.st_mode))
936 continue;
937 switch (match_level) {
938 case 0:
939 best_match = i;
940 match_level = 1;
941 /* FALL THROUGH */
942 case 1:
943 if (!unchanged_file(fnamecmpbuf, file, &st))
944 continue;
945 best_match = i;
946 match_level = 2;
947 if (copy_dest)
948 break;
949 /* FALL THROUGH */
950 case 2:
951 if (!unchanged_attrs(file, &st))
952 continue;
953 best_match = i;
954 match_level = 3;
955 break;
956 }
957 break;
958 } while (basis_dir[++i] != NULL);
959 if (match_level) {
960 statret = 0;
961 if (i != best_match) {
962 i = best_match;
963 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
964 basis_dir[i], fname);
965 if (link_stat(fnamecmpbuf, &st, 0) < 0) {
966 match_level = 0;
967 statret = -1;
968 stat_errno = errno;
969 }
970 }
971#ifdef HAVE_LINK
972 if (link_dest && match_level == 3) {
973 if (hard_link_one(file, ndx, fname, -1, &st,
974 fnamecmpbuf, 1,
975 itemizing && verbose > 1,
976 code) == 0) {
977 if (preserve_hard_links
978 && file->link_u.links) {
979 hard_link_cluster(file, ndx,
980 itemizing,
981 code);
982 }
983 return;
984 }
985 match_level = 2;
986 }
987#endif
988 if (match_level == 2) {
989 /* Copy the file locally. */
990 if (copy_file(fnamecmpbuf, fname, file->mode) < 0) {
991 if (verbose) {
992 rsyserr(FINFO, errno,
993 "copy_file %s => %s",
994 full_fname(fnamecmpbuf),
995 safe_fname(fname));
996 }
997 match_level = 0;
998 statret = -1;
999 } else {
1000 if (itemizing) {
1001 itemize(file, ndx, 0, &st,
1002 ITEM_LOCAL_CHANGE, 0,
1003 NULL);
1004 } else if (verbose && code) {
1005 rprintf(code, "%s\n",
1006 safe_fname(fname));
1007 }
1008 set_perms(fname, file, NULL,
1009 maybe_PERMS_REPORT);
1010 if (preserve_hard_links
1011 && file->link_u.links) {
1012 hard_link_cluster(file, ndx,
1013 itemizing,
1014 code);
1015 }
1016 return;
1017 }
1018 } else if (compare_dest || match_level == 1) {
1019 fnamecmp = fnamecmpbuf;
1020 fnamecmp_type = i;
1021 }
1022 }
1023 }
1024
1025 real_ret = statret;
1026 real_st = st;
1027
1028 if (partial_dir && (partialptr = partial_dir_fname(fname)) != NULL
1029 && link_stat(partialptr, &partial_st, 0) == 0
1030 && S_ISREG(partial_st.st_mode)) {
1031 if (statret != 0)
1032 goto prepare_to_open;
1033 } else
1034 partialptr = NULL;
1035
1036 if (statret != 0 && fuzzy_basis && dry_run <= 1) {
1037 int j = find_fuzzy(file, fuzzy_dirlist);
1038 if (j >= 0) {
1039 fuzzy_file = fuzzy_dirlist->files[j];
1040 f_name_to(fuzzy_file, fnamecmpbuf);
1041 if (verbose > 2) {
1042 rprintf(FINFO, "fuzzy basis selected for %s: %s\n",
1043 safe_fname(fname), safe_fname(fnamecmpbuf));
1044 }
1045 st.st_size = fuzzy_file->length;
1046 statret = 0;
1047 fnamecmp = fnamecmpbuf;
1048 fnamecmp_type = FNAMECMP_FUZZY;
1049 }
1050 }
1051
1052 if (statret != 0) {
1053 if (preserve_hard_links && file->link_u.links
1054 && hard_link_check(file, ndx, fname, statret, &st,
1055 itemizing, code, HL_SKIP))
1056 return;
1057 if (stat_errno == ENOENT)
1058 goto notify_others;
1059 rsyserr(FERROR, stat_errno, "recv_generator: failed to stat %s",
1060 full_fname(fname));
1061 return;
1062 }
1063
1064 if (append_mode && st.st_size > file->length)
1065 return;
1066
1067 if (!compare_dest && fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
1068 ;
1069 else if (fnamecmp_type == FNAMECMP_FUZZY)
1070 ;
1071 else if (unchanged_file(fnamecmp, file, &st)) {
1072 if (fnamecmp_type == FNAMECMP_FNAME) {
1073 if (itemizing) {
1074 itemize(file, ndx, real_ret, &real_st,
1075 0, 0, NULL);
1076 }
1077 set_perms(fname, file, &st, maybe_PERMS_REPORT);
1078 if (preserve_hard_links && file->link_u.links)
1079 hard_link_cluster(file, ndx, itemizing, code);
1080 return;
1081 }
1082 /* Only --compare-dest gets here. */
1083 itemize(file, ndx, real_ret, &real_st,
1084 ITEM_NO_DEST_AND_NO_UPDATE, 0, NULL);
1085 return;
1086 }
1087
1088prepare_to_open:
1089 if (partialptr) {
1090 st = partial_st;
1091 fnamecmp = partialptr;
1092 fnamecmp_type = FNAMECMP_PARTIAL_DIR;
1093 statret = 0;
1094 }
1095
1096 if (!do_xfers || read_batch || whole_file)
1097 goto notify_others;
1098
1099 if (fuzzy_basis) {
1100 int j = flist_find(fuzzy_dirlist, file);
1101 if (j >= 0) /* don't use changing file as future fuzzy basis */
1102 fuzzy_dirlist->files[j]->flags |= FLAG_NO_FUZZY;
1103 }
1104
1105 /* open the file */
1106 fd = do_open(fnamecmp, O_RDONLY, 0);
1107
1108 if (fd == -1) {
1109 rsyserr(FERROR, errno, "failed to open %s, continuing",
1110 full_fname(fnamecmp));
1111 pretend_missing:
1112 /* pretend the file didn't exist */
1113 if (preserve_hard_links && file->link_u.links
1114 && hard_link_check(file, ndx, fname, statret, &st,
1115 itemizing, code, HL_SKIP))
1116 return;
1117 statret = real_ret = -1;
1118 goto notify_others;
1119 }
1120
1121 if (inplace && make_backups && fnamecmp_type == FNAMECMP_FNAME) {
1122 if (!(backupptr = get_backup_name(fname))) {
1123 close(fd);
1124 return;
1125 }
1126 if (!(back_file = make_file(fname, NULL, NO_FILTERS))) {
1127 close(fd);
1128 goto pretend_missing;
1129 }
1130 if (robust_unlink(backupptr) && errno != ENOENT) {
1131 rsyserr(FERROR, errno, "unlink %s",
1132 full_fname(backupptr));
1133 free(back_file);
1134 close(fd);
1135 return;
1136 }
1137 if ((f_copy = do_open(backupptr,
1138 O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0) {
1139 rsyserr(FERROR, errno, "open %s",
1140 full_fname(backupptr));
1141 free(back_file);
1142 close(fd);
1143 return;
1144 }
1145 fnamecmp_type = FNAMECMP_BACKUP;
1146 }
1147
1148 if (verbose > 3) {
1149 rprintf(FINFO, "gen mapped %s of size %.0f\n",
1150 safe_fname(fnamecmp), (double)st.st_size);
1151 }
1152
1153 if (verbose > 2)
1154 rprintf(FINFO, "generating and sending sums for %d\n", ndx);
1155
1156notify_others:
1157 write_int(f_out, ndx);
1158 if (itemizing) {
1159 int iflags = ITEM_TRANSFER;
1160 if (always_checksum)
1161 iflags |= ITEM_REPORT_CHECKSUM;
1162 if (fnamecmp_type != FNAMECMP_FNAME)
1163 iflags |= ITEM_BASIS_TYPE_FOLLOWS;
1164 if (fnamecmp_type == FNAMECMP_FUZZY)
1165 iflags |= ITEM_XNAME_FOLLOWS;
1166 itemize(file, -1, real_ret, &real_st, iflags, fnamecmp_type,
1167 fuzzy_file ? fuzzy_file->basename : NULL);
1168 }
1169
1170 if (!do_xfers) {
1171 if (preserve_hard_links && file->link_u.links)
1172 hard_link_cluster(file, ndx, itemizing, code);
1173 return;
1174 }
1175 if (read_batch)
1176 return;
1177
1178 if (statret != 0 || whole_file) {
1179 write_sum_head(f_out, NULL);
1180 return;
1181 }
1182
1183 generate_and_send_sums(fd, st.st_size, f_out, f_copy);
1184
1185 if (f_copy >= 0) {
1186 close(f_copy);
1187 set_perms(backupptr, back_file, NULL, 0);
1188 if (verbose > 1) {
1189 rprintf(FINFO, "backed up %s to %s\n",
1190 safe_fname(fname), safe_fname(backupptr));
1191 }
1192 free(back_file);
1193 }
1194
1195 close(fd);
1196}
1197
1198
1199void generate_files(int f_out, struct file_list *flist, char *local_name)
1200{
1201 int i;
1202 char fbuf[MAXPATHLEN];
1203 int itemizing, maybe_PERMS_REPORT;
1204 enum logcode code;
1205 int lull_mod = allowed_lull * 5;
1206 int need_retouch_dir_times = preserve_times && !omit_dir_times;
1207 int need_retouch_dir_perms = 0;
1208 int save_ignore_existing = ignore_existing;
1209 int save_ignore_non_existing = ignore_non_existing;
1210 int save_do_progress = do_progress;
1211 int save_make_backups = make_backups;
1212
1213 if (protocol_version >= 29) {
1214 itemizing = 1;
1215 maybe_PERMS_REPORT = log_format_has_i ? 0 : PERMS_REPORT;
1216 code = daemon_log_format_has_i ? 0 : FLOG;
1217 } else if (am_daemon) {
1218 itemizing = daemon_log_format_has_i && do_xfers;
1219 maybe_PERMS_REPORT = PERMS_REPORT;
1220 code = itemizing || !do_xfers ? FCLIENT : FINFO;
1221 } else if (!am_server) {
1222 itemizing = log_format_has_i;
1223 maybe_PERMS_REPORT = log_format_has_i ? 0 : PERMS_REPORT;
1224 code = itemizing ? 0 : FINFO;
1225 } else {
1226 itemizing = 0;
1227 maybe_PERMS_REPORT = PERMS_REPORT;
1228 code = FINFO;
1229 }
1230
1231 if (verbose > 2) {
1232 rprintf(FINFO, "generator starting pid=%ld count=%d\n",
1233 (long)getpid(), flist->count);
1234 }
1235
1236 if (delete_before && !local_name && flist->count > 0)
1237 do_delete_pass(flist);
1238 do_progress = 0;
1239
1240 if (append_mode || whole_file < 0)
1241 whole_file = 0;
1242 if (verbose >= 2) {
1243 rprintf(FINFO, "delta-transmission %s\n",
1244 whole_file
1245 ? "disabled for local transfer or --whole-file"
1246 : "enabled");
1247 }
1248
1249 /* Since we often fill up the outgoing socket and then just sit around
1250 * waiting for the other 2 processes to do their thing, we don't want
1251 * to exit on a timeout. If the data stops flowing, the receiver will
1252 * notice that and let us know via the redo pipe (or its closing). */
1253 ignore_timeout = 1;
1254
1255 for (i = 0; i < flist->count; i++) {
1256 struct file_struct *file = flist->files[i];
1257
1258 if (!file->basename)
1259 continue;
1260
1261 if (local_name)
1262 strlcpy(fbuf, local_name, sizeof fbuf);
1263 else
1264 f_name_to(file, fbuf);
1265 recv_generator(fbuf, file, i, itemizing, maybe_PERMS_REPORT,
1266 code, f_out);
1267
1268 /* We need to ensure that any dirs we create have writeable
1269 * permissions during the time we are putting files within
1270 * them. This is then fixed after the transfer is done. */
1271#ifdef HAVE_CHMOD
1272 if (!am_root && S_ISDIR(file->mode) && !(file->mode & S_IWUSR)
1273 && !list_only) {
1274 int mode = file->mode | S_IWUSR; /* user write */
1275 char *fname = local_name ? local_name : fbuf;
1276 if (do_chmod(fname, mode) < 0) {
1277 rsyserr(FERROR, errno,
1278 "failed to modify permissions on %s",
1279 full_fname(fname));
1280 }
1281 need_retouch_dir_perms = 1;
1282 }
1283#endif
1284
1285 if (preserve_hard_links)
1286 check_for_finished_hlinks(itemizing, code);
1287
1288 if (allowed_lull && !(i % lull_mod))
1289 maybe_send_keepalive();
1290 else if (!(i % 200))
1291 maybe_flush_socket();
1292 }
1293 recv_generator(NULL, NULL, 0, 0, 0, code, -1);
1294 if (delete_during)
1295 delete_in_dir(NULL, NULL, NULL);
1296
1297 phase++;
1298 csum_length = SUM_LENGTH;
1299 max_size = min_size = ignore_existing = ignore_non_existing = 0;
1300 update_only = always_checksum = size_only = 0;
1301 ignore_times = 1;
1302 if (append_mode) /* resend w/o append mode */
1303 append_mode = -1; /* ... but only longer files */
1304 make_backups = 0; /* avoid a duplicate backup for inplace processing */
1305
1306 if (verbose > 2)
1307 rprintf(FINFO,"generate_files phase=%d\n",phase);
1308
1309 write_int(f_out, -1);
1310
1311 /* files can cycle through the system more than once
1312 * to catch initial checksum errors */
1313 while ((i = get_redo_num(itemizing, code)) != -1) {
1314 struct file_struct *file = flist->files[i];
1315 if (local_name)
1316 strlcpy(fbuf, local_name, sizeof fbuf);
1317 else
1318 f_name_to(file, fbuf);
1319 recv_generator(fbuf, file, i, itemizing, maybe_PERMS_REPORT,
1320 code, f_out);
1321 }
1322
1323 phase++;
1324 ignore_non_existing = save_ignore_non_existing;
1325 ignore_existing = save_ignore_existing;
1326 make_backups = save_make_backups;
1327
1328 if (verbose > 2)
1329 rprintf(FINFO,"generate_files phase=%d\n",phase);
1330
1331 write_int(f_out, -1);
1332 /* Reduce round-trip lag-time for a useless delay-updates phase. */
1333 if (protocol_version >= 29 && !delay_updates)
1334 write_int(f_out, -1);
1335
1336 /* Read MSG_DONE for the redo phase (and any prior messages). */
1337 get_redo_num(itemizing, code);
1338
1339 if (protocol_version >= 29) {
1340 phase++;
1341 if (verbose > 2)
1342 rprintf(FINFO, "generate_files phase=%d\n", phase);
1343 if (delay_updates)
1344 write_int(f_out, -1);
1345 /* Read MSG_DONE for delay-updates phase & prior messages. */
1346 get_redo_num(itemizing, code);
1347 }
1348
1349 do_progress = save_do_progress;
1350 if (delete_after && !local_name && flist->count > 0)
1351 do_delete_pass(flist);
1352
1353 if ((need_retouch_dir_perms || need_retouch_dir_times)
1354 && !list_only && !local_name && !dry_run) {
1355 int j = 0;
1356 /* Now we need to fix any directory permissions that were
1357 * modified during the transfer and/or re-set any tweaked
1358 * modified-time values. */
1359 for (i = 0; i < flist->count; i++) {
1360 struct file_struct *file = flist->files[i];
1361 if (!file->basename || !S_ISDIR(file->mode))
1362 continue;
1363 if (!need_retouch_dir_times && file->mode & S_IWUSR)
1364 continue;
1365 recv_generator(f_name(file), file, i, itemizing,
1366 maybe_PERMS_REPORT, code, -1);
1367 if (allowed_lull && !(++j % lull_mod))
1368 maybe_send_keepalive();
1369 else if (!(j % 200))
1370 maybe_flush_socket();
1371 }
1372 }
1373 recv_generator(NULL, NULL, 0, 0, 0, code, -1);
1374
1375 if (max_delete > 0 && deletion_count > max_delete) {
1376 rprintf(FINFO,
1377 "Deletions stopped due to --max-delete limit (%d skipped)\n",
1378 deletion_count - max_delete);
1379 io_error |= IOERR_DEL_LIMIT;
1380 }
1381
1382 if (verbose > 2)
1383 rprintf(FINFO,"generate_files finished\n");
1384}