- Got rid of a superfluous empty line.
[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 int checksum_len;
73extern char *partial_dir;
74extern char *basis_dir[];
75extern int compare_dest;
76extern int copy_dest;
77extern int link_dest;
78extern int whole_file;
79extern int list_only;
80extern int read_batch;
81extern int orig_umask;
82extern int safe_symlinks;
83extern long block_size; /* "long" because popt can't set an int32. */
84extern int max_delete;
85extern int force_delete;
86extern int one_file_system;
87extern struct stats stats;
88extern dev_t filesystem_dev;
89extern char *backup_dir;
90extern char *backup_suffix;
91extern int backup_suffix_len;
92extern struct file_list *the_file_list;
93extern struct filter_list_struct server_filter_list;
94
95static int deletion_count = 0; /* used to implement --max-delete */
96
97
98static int is_backup_file(char *fn)
99{
100 int k = strlen(fn) - backup_suffix_len;
101 return k > 0 && strcmp(fn+k, backup_suffix) == 0;
102}
103
104
105/* Delete a file or directory. If DEL_FORCE_RECURSE is set in the flags, or if
106 * force_delete is set, this will delete recursively as long as DEL_NO_RECURSE
107 * is not set in the flags.
108 *
109 * Note that fname must point to a MAXPATHLEN buffer if the mode indicates it's
110 * a directory! (The buffer is used for recursion, but returned unchanged.)
111 */
112static int delete_item(char *fname, int mode, int flags)
113{
114 struct file_list *dirlist;
115 int j, dlen, zap_dir, ok;
116 unsigned remainder;
117 void *save_filters;
118 char *p;
119
120 if (!S_ISDIR(mode)) {
121 if (max_delete && ++deletion_count > max_delete)
122 return 0;
123 if (make_backups && (backup_dir || !is_backup_file(fname)))
124 ok = make_backup(fname);
125 else
126 ok = robust_unlink(fname) == 0;
127 if (ok) {
128 if (!(flags & DEL_TERSE))
129 log_delete(fname, mode);
130 return 0;
131 }
132 if (errno == ENOENT) {
133 deletion_count--;
134 return 0;
135 }
136 rsyserr(FERROR, errno, "delete_file: unlink %s failed",
137 full_fname(fname));
138 return -1;
139 }
140
141 zap_dir = (flags & DEL_FORCE_RECURSE || (force_delete && recurse))
142 && !(flags & DEL_NO_RECURSE);
143 if ((max_delete && ++deletion_count > max_delete)
144 || (dry_run && zap_dir)) {
145 ok = 0;
146 errno = ENOTEMPTY;
147 } else if (make_backups && !backup_dir && !is_backup_file(fname)
148 && !(flags & DEL_FORCE_RECURSE))
149 ok = make_backup(fname);
150 else
151 ok = do_rmdir(fname) == 0;
152 if (ok) {
153 if (!(flags & DEL_TERSE))
154 log_delete(fname, mode);
155 return 0;
156 }
157 if (errno == ENOENT) {
158 deletion_count--;
159 return 0;
160 }
161 if (!zap_dir) {
162 rsyserr(FERROR, errno, "delete_file: rmdir %s failed",
163 full_fname(fname));
164 return -1;
165 }
166 flags |= DEL_FORCE_RECURSE; /* mark subdir dels as not "in the way" */
167 deletion_count--;
168
169 dlen = strlen(fname);
170 save_filters = push_local_filters(fname, dlen);
171
172 dirlist = get_dirlist(fname, dlen, 0);
173
174 p = fname + dlen;
175 if (dlen != 1 || *fname != '/')
176 *p++ = '/';
177 remainder = MAXPATHLEN - (p - fname);
178
179 for (j = dirlist->count; j--; ) {
180 struct file_struct *fp = dirlist->files[j];
181
182 if (fp->flags & FLAG_MOUNT_POINT)
183 continue;
184
185 strlcpy(p, fp->basename, remainder);
186 delete_item(fname, fp->mode, flags & ~DEL_TERSE);
187 }
188 flist_free(dirlist);
189
190 fname[dlen] = '\0';
191
192 pop_local_filters(save_filters);
193
194 if (max_delete && ++deletion_count > max_delete)
195 return 0;
196
197 if (do_rmdir(fname) == 0) {
198 if (!(flags & DEL_TERSE))
199 log_delete(fname, mode);
200 } else if (errno != ENOTEMPTY && errno != EEXIST && errno != ENOENT) {
201 rsyserr(FERROR, errno, "delete_file: rmdir %s failed",
202 full_fname(fname));
203 return -1;
204 }
205
206 return 0;
207}
208
209
210/* This function is used to implement per-directory deletion, and is used by
211 * all the --delete-WHEN options. Note that the fbuf pointer must point to a
212 * MAXPATHLEN buffer with the name of the directory in it (the functions we
213 * call will append names onto the end, but the old dir value will be restored
214 * on exit). */
215static void delete_in_dir(struct file_list *flist, char *fbuf,
216 struct file_struct *file)
217{
218 static int min_depth = MAXPATHLEN, cur_depth = -1;
219 static void *filt_array[MAXPATHLEN/2+1];
220 static int already_warned = 0;
221 struct file_list *dirlist;
222 char delbuf[MAXPATHLEN];
223 STRUCT_STAT st;
224 int dlen, i;
225
226 if (!flist) {
227 while (cur_depth >= min_depth)
228 pop_local_filters(filt_array[cur_depth--]);
229 min_depth = MAXPATHLEN;
230 cur_depth = -1;
231 return;
232 }
233
234 if (verbose > 2)
235 rprintf(FINFO, "delete_in_dir(%s)\n", safe_fname(fbuf));
236
237 if (allowed_lull)
238 maybe_send_keepalive();
239
240 if (file->dir.depth >= MAXPATHLEN/2+1)
241 return; /* Impossible... */
242
243 if (io_error && !(lp_ignore_errors(module_id) || ignore_errors)) {
244 if (already_warned)
245 return;
246 rprintf(FINFO,
247 "IO error encountered -- skipping file deletion\n");
248 already_warned = 1;
249 return;
250 }
251
252 while (cur_depth >= file->dir.depth && cur_depth >= min_depth)
253 pop_local_filters(filt_array[cur_depth--]);
254 cur_depth = file->dir.depth;
255 if (min_depth > cur_depth)
256 min_depth = cur_depth;
257 dlen = strlen(fbuf);
258 filt_array[cur_depth] = push_local_filters(fbuf, dlen);
259
260 if (link_stat(fbuf, &st, keep_dirlinks) < 0)
261 return;
262
263 if (one_file_system) {
264 if (file->flags & FLAG_TOP_DIR)
265 filesystem_dev = st.st_dev;
266 else if (filesystem_dev != st.st_dev)
267 return;
268 }
269
270 dirlist = get_dirlist(fbuf, dlen, 0);
271
272 /* If an item in dirlist is not found in flist, delete it
273 * from the filesystem. */
274 for (i = dirlist->count; i--; ) {
275 struct file_struct *fp = dirlist->files[i];
276 if (!fp->basename || fp->flags & FLAG_MOUNT_POINT)
277 continue;
278 if (flist_find(flist, fp) < 0) {
279 int mode = fp->mode;
280 f_name_to(fp, delbuf);
281 delete_item(delbuf, mode, DEL_FORCE_RECURSE);
282 }
283 }
284
285 flist_free(dirlist);
286}
287
288/* This deletes any files on the receiving side that are not present on the
289 * sending side. This is used by --delete-before and --delete-after. */
290static void do_delete_pass(struct file_list *flist)
291{
292 char fbuf[MAXPATHLEN];
293 int j;
294
295 if (dry_run > 1 /* destination doesn't exist yet */
296 || list_only)
297 return;
298
299 for (j = 0; j < flist->count; j++) {
300 struct file_struct *file = flist->files[j];
301
302 if (!(file->flags & FLAG_DEL_HERE))
303 continue;
304
305 f_name_to(file, fbuf);
306 if (verbose > 1 && file->flags & FLAG_TOP_DIR)
307 rprintf(FINFO, "deleting in %s\n", safe_fname(fbuf));
308
309 delete_in_dir(flist, fbuf, file);
310 }
311 delete_in_dir(NULL, NULL, NULL);
312
313 if (do_progress && !am_server)
314 rprintf(FINFO, " \r");
315}
316
317static int unchanged_attrs(struct file_struct *file, STRUCT_STAT *st)
318{
319 if (preserve_perms
320 && (st->st_mode & CHMOD_BITS) != (file->mode & CHMOD_BITS))
321 return 0;
322
323 if (am_root && preserve_uid && st->st_uid != file->uid)
324 return 0;
325
326 if (preserve_gid && file->gid != GID_NONE && st->st_gid != file->gid)
327 return 0;
328
329 return 1;
330}
331
332
333void itemize(struct file_struct *file, int ndx, int statret, STRUCT_STAT *st,
334 int32 iflags, uchar fnamecmp_type, char *xname)
335{
336 if (statret == 0) {
337 if (S_ISREG(file->mode) && file->length != st->st_size)
338 iflags |= ITEM_REPORT_SIZE;
339 if (!(iflags & ITEM_NO_DEST_AND_NO_UPDATE)) {
340 int keep_time = !preserve_times ? 0
341 : S_ISDIR(file->mode) ? !omit_dir_times
342 : !S_ISLNK(file->mode);
343
344 if ((iflags & (ITEM_TRANSFER|ITEM_LOCAL_CHANGE) && !keep_time
345 && (!(iflags & ITEM_XNAME_FOLLOWS) || *xname))
346 || (keep_time && cmp_modtime(file->modtime, st->st_mtime) != 0))
347 iflags |= ITEM_REPORT_TIME;
348 if (preserve_perms
349 && (file->mode & CHMOD_BITS) != (st->st_mode & CHMOD_BITS))
350 iflags |= ITEM_REPORT_PERMS;
351 if (preserve_uid && am_root && file->uid != st->st_uid)
352 iflags |= ITEM_REPORT_OWNER;
353 if (preserve_gid && file->gid != GID_NONE
354 && st->st_gid != file->gid)
355 iflags |= ITEM_REPORT_GROUP;
356 }
357 } else
358 iflags |= ITEM_IS_NEW;
359
360 iflags &= 0xffff;
361 if ((iflags & SIGNIFICANT_ITEM_FLAGS || verbose > 1
362 || (xname && *xname)) && !read_batch) {
363 if (protocol_version >= 29) {
364 if (ndx >= 0)
365 write_int(sock_f_out, ndx);
366 write_shortint(sock_f_out, iflags);
367 if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
368 write_byte(sock_f_out, fnamecmp_type);
369 if (iflags & ITEM_XNAME_FOLLOWS)
370 write_vstring(sock_f_out, xname, strlen(xname));
371 } else if (ndx >= 0)
372 log_item(file, &stats, iflags, xname);
373 }
374}
375
376
377/* Perform our quick-check heuristic for determining if a file is unchanged. */
378static int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st)
379{
380 if (st->st_size != file->length)
381 return 0;
382
383 /* if always checksum is set then we use the checksum instead
384 of the file time to determine whether to sync */
385 if (always_checksum && S_ISREG(st->st_mode)) {
386 char sum[MD4_SUM_LENGTH];
387 file_checksum(fn, sum, st->st_size);
388 return memcmp(sum, file->u.sum, checksum_len) == 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 (partialptr) {
1073 do_unlink(partialptr);
1074 handle_partial_dir(partialptr, PDIR_DELETE);
1075 }
1076 if (fnamecmp_type == FNAMECMP_FNAME) {
1077 if (itemizing) {
1078 itemize(file, ndx, real_ret, &real_st,
1079 0, 0, NULL);
1080 }
1081 set_perms(fname, file, &st, maybe_PERMS_REPORT);
1082 if (preserve_hard_links && file->link_u.links)
1083 hard_link_cluster(file, ndx, itemizing, code);
1084 return;
1085 }
1086 /* Only --compare-dest gets here. */
1087 itemize(file, ndx, real_ret, &real_st,
1088 ITEM_NO_DEST_AND_NO_UPDATE, 0, NULL);
1089 return;
1090 }
1091
1092prepare_to_open:
1093 if (partialptr) {
1094 st = partial_st;
1095 fnamecmp = partialptr;
1096 fnamecmp_type = FNAMECMP_PARTIAL_DIR;
1097 statret = 0;
1098 }
1099
1100 if (!do_xfers || read_batch || whole_file)
1101 goto notify_others;
1102
1103 if (fuzzy_basis) {
1104 int j = flist_find(fuzzy_dirlist, file);
1105 if (j >= 0) /* don't use changing file as future fuzzy basis */
1106 fuzzy_dirlist->files[j]->flags |= FLAG_NO_FUZZY;
1107 }
1108
1109 /* open the file */
1110 fd = do_open(fnamecmp, O_RDONLY, 0);
1111
1112 if (fd == -1) {
1113 rsyserr(FERROR, errno, "failed to open %s, continuing",
1114 full_fname(fnamecmp));
1115 pretend_missing:
1116 /* pretend the file didn't exist */
1117 if (preserve_hard_links && file->link_u.links
1118 && hard_link_check(file, ndx, fname, statret, &st,
1119 itemizing, code, HL_SKIP))
1120 return;
1121 statret = real_ret = -1;
1122 goto notify_others;
1123 }
1124
1125 if (inplace && make_backups && fnamecmp_type == FNAMECMP_FNAME) {
1126 if (!(backupptr = get_backup_name(fname))) {
1127 close(fd);
1128 return;
1129 }
1130 if (!(back_file = make_file(fname, NULL, NO_FILTERS))) {
1131 close(fd);
1132 goto pretend_missing;
1133 }
1134 if (robust_unlink(backupptr) && errno != ENOENT) {
1135 rsyserr(FERROR, errno, "unlink %s",
1136 full_fname(backupptr));
1137 free(back_file);
1138 close(fd);
1139 return;
1140 }
1141 if ((f_copy = do_open(backupptr,
1142 O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0) {
1143 rsyserr(FERROR, errno, "open %s",
1144 full_fname(backupptr));
1145 free(back_file);
1146 close(fd);
1147 return;
1148 }
1149 fnamecmp_type = FNAMECMP_BACKUP;
1150 }
1151
1152 if (verbose > 3) {
1153 rprintf(FINFO, "gen mapped %s of size %.0f\n",
1154 safe_fname(fnamecmp), (double)st.st_size);
1155 }
1156
1157 if (verbose > 2)
1158 rprintf(FINFO, "generating and sending sums for %d\n", ndx);
1159
1160notify_others:
1161 write_int(f_out, ndx);
1162 if (itemizing) {
1163 int iflags = ITEM_TRANSFER;
1164 if (always_checksum)
1165 iflags |= ITEM_REPORT_CHECKSUM;
1166 if (fnamecmp_type != FNAMECMP_FNAME)
1167 iflags |= ITEM_BASIS_TYPE_FOLLOWS;
1168 if (fnamecmp_type == FNAMECMP_FUZZY)
1169 iflags |= ITEM_XNAME_FOLLOWS;
1170 itemize(file, -1, real_ret, &real_st, iflags, fnamecmp_type,
1171 fuzzy_file ? fuzzy_file->basename : NULL);
1172 }
1173
1174 if (!do_xfers) {
1175 if (preserve_hard_links && file->link_u.links)
1176 hard_link_cluster(file, ndx, itemizing, code);
1177 return;
1178 }
1179 if (read_batch)
1180 return;
1181
1182 if (statret != 0 || whole_file) {
1183 write_sum_head(f_out, NULL);
1184 return;
1185 }
1186
1187 generate_and_send_sums(fd, st.st_size, f_out, f_copy);
1188
1189 if (f_copy >= 0) {
1190 close(f_copy);
1191 set_perms(backupptr, back_file, NULL, 0);
1192 if (verbose > 1) {
1193 rprintf(FINFO, "backed up %s to %s\n",
1194 safe_fname(fname), safe_fname(backupptr));
1195 }
1196 free(back_file);
1197 }
1198
1199 close(fd);
1200}
1201
1202
1203void generate_files(int f_out, struct file_list *flist, char *local_name)
1204{
1205 int i;
1206 char fbuf[MAXPATHLEN];
1207 int itemizing, maybe_PERMS_REPORT;
1208 enum logcode code;
1209 int lull_mod = allowed_lull * 5;
1210 int need_retouch_dir_times = preserve_times && !omit_dir_times;
1211 int need_retouch_dir_perms = 0;
1212 int save_ignore_existing = ignore_existing;
1213 int save_ignore_non_existing = ignore_non_existing;
1214 int save_do_progress = do_progress;
1215 int save_make_backups = make_backups;
1216
1217 if (protocol_version >= 29) {
1218 itemizing = 1;
1219 maybe_PERMS_REPORT = log_format_has_i ? 0 : PERMS_REPORT;
1220 code = daemon_log_format_has_i ? 0 : FLOG;
1221 } else if (am_daemon) {
1222 itemizing = daemon_log_format_has_i && do_xfers;
1223 maybe_PERMS_REPORT = PERMS_REPORT;
1224 code = itemizing || !do_xfers ? FCLIENT : FINFO;
1225 } else if (!am_server) {
1226 itemizing = log_format_has_i;
1227 maybe_PERMS_REPORT = log_format_has_i ? 0 : PERMS_REPORT;
1228 code = itemizing ? 0 : FINFO;
1229 } else {
1230 itemizing = 0;
1231 maybe_PERMS_REPORT = PERMS_REPORT;
1232 code = FINFO;
1233 }
1234
1235 if (verbose > 2) {
1236 rprintf(FINFO, "generator starting pid=%ld count=%d\n",
1237 (long)getpid(), flist->count);
1238 }
1239
1240 if (delete_before && !local_name && flist->count > 0)
1241 do_delete_pass(flist);
1242 do_progress = 0;
1243
1244 if (append_mode || whole_file < 0)
1245 whole_file = 0;
1246 if (verbose >= 2) {
1247 rprintf(FINFO, "delta-transmission %s\n",
1248 whole_file
1249 ? "disabled for local transfer or --whole-file"
1250 : "enabled");
1251 }
1252
1253 /* Since we often fill up the outgoing socket and then just sit around
1254 * waiting for the other 2 processes to do their thing, we don't want
1255 * to exit on a timeout. If the data stops flowing, the receiver will
1256 * notice that and let us know via the redo pipe (or its closing). */
1257 ignore_timeout = 1;
1258
1259 for (i = 0; i < flist->count; i++) {
1260 struct file_struct *file = flist->files[i];
1261
1262 if (!file->basename)
1263 continue;
1264
1265 if (local_name)
1266 strlcpy(fbuf, local_name, sizeof fbuf);
1267 else
1268 f_name_to(file, fbuf);
1269 recv_generator(fbuf, file, i, itemizing, maybe_PERMS_REPORT,
1270 code, f_out);
1271
1272 /* We need to ensure that any dirs we create have writeable
1273 * permissions during the time we are putting files within
1274 * them. This is then fixed after the transfer is done. */
1275#ifdef HAVE_CHMOD
1276 if (!am_root && S_ISDIR(file->mode) && !(file->mode & S_IWUSR)
1277 && !list_only) {
1278 int mode = file->mode | S_IWUSR; /* user write */
1279 char *fname = local_name ? local_name : fbuf;
1280 if (do_chmod(fname, mode) < 0) {
1281 rsyserr(FERROR, errno,
1282 "failed to modify permissions on %s",
1283 full_fname(fname));
1284 }
1285 need_retouch_dir_perms = 1;
1286 }
1287#endif
1288
1289 if (preserve_hard_links)
1290 check_for_finished_hlinks(itemizing, code);
1291
1292 if (allowed_lull && !(i % lull_mod))
1293 maybe_send_keepalive();
1294 else if (!(i % 200))
1295 maybe_flush_socket();
1296 }
1297 recv_generator(NULL, NULL, 0, 0, 0, code, -1);
1298 if (delete_during)
1299 delete_in_dir(NULL, NULL, NULL);
1300
1301 phase++;
1302 csum_length = SUM_LENGTH;
1303 max_size = min_size = ignore_existing = ignore_non_existing = 0;
1304 update_only = always_checksum = size_only = 0;
1305 ignore_times = 1;
1306 if (append_mode) /* resend w/o append mode */
1307 append_mode = -1; /* ... but only longer files */
1308 make_backups = 0; /* avoid a duplicate backup for inplace processing */
1309
1310 if (verbose > 2)
1311 rprintf(FINFO,"generate_files phase=%d\n",phase);
1312
1313 write_int(f_out, -1);
1314
1315 /* files can cycle through the system more than once
1316 * to catch initial checksum errors */
1317 while ((i = get_redo_num(itemizing, code)) != -1) {
1318 struct file_struct *file = flist->files[i];
1319 if (local_name)
1320 strlcpy(fbuf, local_name, sizeof fbuf);
1321 else
1322 f_name_to(file, fbuf);
1323 recv_generator(fbuf, file, i, itemizing, maybe_PERMS_REPORT,
1324 code, f_out);
1325 }
1326
1327 phase++;
1328 ignore_non_existing = save_ignore_non_existing;
1329 ignore_existing = save_ignore_existing;
1330 make_backups = save_make_backups;
1331
1332 if (verbose > 2)
1333 rprintf(FINFO,"generate_files phase=%d\n",phase);
1334
1335 write_int(f_out, -1);
1336 /* Reduce round-trip lag-time for a useless delay-updates phase. */
1337 if (protocol_version >= 29 && !delay_updates)
1338 write_int(f_out, -1);
1339
1340 /* Read MSG_DONE for the redo phase (and any prior messages). */
1341 get_redo_num(itemizing, code);
1342
1343 if (protocol_version >= 29) {
1344 phase++;
1345 if (verbose > 2)
1346 rprintf(FINFO, "generate_files phase=%d\n", phase);
1347 if (delay_updates)
1348 write_int(f_out, -1);
1349 /* Read MSG_DONE for delay-updates phase & prior messages. */
1350 get_redo_num(itemizing, code);
1351 }
1352
1353 do_progress = save_do_progress;
1354 if (delete_after && !local_name && flist->count > 0)
1355 do_delete_pass(flist);
1356
1357 if ((need_retouch_dir_perms || need_retouch_dir_times)
1358 && !list_only && !local_name && !dry_run) {
1359 int j = 0;
1360 /* Now we need to fix any directory permissions that were
1361 * modified during the transfer and/or re-set any tweaked
1362 * modified-time values. */
1363 for (i = 0; i < flist->count; i++) {
1364 struct file_struct *file = flist->files[i];
1365 if (!file->basename || !S_ISDIR(file->mode))
1366 continue;
1367 if (!need_retouch_dir_times && file->mode & S_IWUSR)
1368 continue;
1369 recv_generator(f_name(file), file, i, itemizing,
1370 maybe_PERMS_REPORT, code, -1);
1371 if (allowed_lull && !(++j % lull_mod))
1372 maybe_send_keepalive();
1373 else if (!(j % 200))
1374 maybe_flush_socket();
1375 }
1376 }
1377 recv_generator(NULL, NULL, 0, 0, 0, code, -1);
1378
1379 if (max_delete > 0 && deletion_count > max_delete) {
1380 rprintf(FINFO,
1381 "Deletions stopped due to --max-delete limit (%d skipped)\n",
1382 deletion_count - max_delete);
1383 io_error |= IOERR_DEL_LIMIT;
1384 }
1385
1386 if (verbose > 2)
1387 rprintf(FINFO,"generate_files finished\n");
1388}