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