- Moved file-deletion code into generator.c.
[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 log_format_has_i;
29extern int log_format_has_o_or_i;
30extern int daemon_log_format_has_i;
31extern int am_root;
32extern int am_server;
33extern int am_daemon;
34extern int relative_paths;
35extern int keep_dirlinks;
36extern int preserve_links;
37extern int preserve_devices;
38extern int preserve_hard_links;
39extern int preserve_perms;
40extern int preserve_uid;
41extern int preserve_gid;
42extern int preserve_times;
43extern int omit_dir_times;
44extern int delete_during;
45extern int remove_sent_files;
46extern int update_only;
47extern int opt_ignore_existing;
48extern int inplace;
49extern int make_backups;
50extern int csum_length;
51extern int ignore_times;
52extern int size_only;
53extern OFF_T max_size;
54extern int io_timeout;
55extern int ignore_timeout;
56extern int protocol_version;
57extern int fuzzy_basis;
58extern int always_checksum;
59extern char *partial_dir;
60extern char *basis_dir[];
61extern int compare_dest;
62extern int link_dest;
63extern int whole_file;
64extern int local_server;
65extern int list_only;
66extern int read_batch;
67extern int only_existing;
68extern int orig_umask;
69extern int safe_symlinks;
70extern long block_size; /* "long" because popt can't set an int32. */
71extern struct stats stats;
72
73extern struct filter_list_struct server_filter_list;
74
75static int unchanged_attrs(struct file_struct *file, STRUCT_STAT *st)
76{
77 if (preserve_perms
78 && (st->st_mode & CHMOD_BITS) != (file->mode & CHMOD_BITS))
79 return 0;
80
81 if (am_root && preserve_uid && st->st_uid != file->uid)
82 return 0;
83
84 if (preserve_gid && file->gid != GID_NONE && st->st_gid != file->gid)
85 return 0;
86
87 return 1;
88}
89
90
91#define SID_UPDATING ITEM_UPDATING
92#define SID_REPORT_CHECKSUM ITEM_REPORT_CHECKSUM
93#define SID_USING_ALT_BASIS ITEM_USING_ALT_BASIS
94/* This flag doesn't get sent, so it must be outside 0xffff. */
95#define SID_NO_DEST_AND_NO_UPDATE (1<<16)
96
97static void itemize(struct file_struct *file, int statret, STRUCT_STAT *st,
98 int32 sflags, int f_out, int ndx)
99{
100 int iflags = sflags & 0xffff;
101
102 if (statret >= 0) {
103 if (S_ISREG(file->mode) && file->length != st->st_size)
104 iflags |= ITEM_REPORT_SIZE;
105 if (!(sflags & SID_NO_DEST_AND_NO_UPDATE)) {
106 int keep_time = !preserve_times ? 0
107 : S_ISDIR(file->mode) ? !omit_dir_times
108 : !S_ISLNK(file->mode);
109
110 if ((iflags & ITEM_UPDATING && !keep_time)
111 || (keep_time && file->modtime != st->st_mtime))
112 iflags |= ITEM_REPORT_TIME;
113 if (preserve_perms && file->mode != st->st_mode)
114 iflags |= ITEM_REPORT_PERMS;
115 if (preserve_uid && am_root && file->uid != st->st_uid)
116 iflags |= ITEM_REPORT_OWNER;
117 if (preserve_gid && file->gid != GID_NONE
118 && st->st_gid != file->gid)
119 iflags |= ITEM_REPORT_GROUP;
120 }
121 } else
122 iflags |= ITEM_IS_NEW | ITEM_UPDATING;
123
124 if ((iflags || verbose > 1) && !read_batch) {
125 if (protocol_version >= 29) {
126 if (ndx >= 0)
127 write_int(f_out, ndx);
128 write_shortint(f_out, iflags);
129 } else if (ndx >= 0)
130 log_recv(file, &stats, iflags);
131 }
132}
133
134
135/* Perform our quick-check heuristic for determining if a file is unchanged. */
136static int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st)
137{
138 if (st->st_size != file->length)
139 return 0;
140
141 /* if always checksum is set then we use the checksum instead
142 of the file time to determine whether to sync */
143 if (always_checksum && S_ISREG(st->st_mode)) {
144 char sum[MD4_SUM_LENGTH];
145 file_checksum(fn, sum, st->st_size);
146 return memcmp(sum, file->u.sum, protocol_version < 21 ? 2
147 : MD4_SUM_LENGTH) == 0;
148 }
149
150 if (size_only)
151 return 1;
152
153 if (ignore_times)
154 return 0;
155
156 return cmp_modtime(st->st_mtime, file->modtime) == 0;
157}
158
159
160/*
161 * set (initialize) the size entries in the per-file sum_struct
162 * calculating dynamic block and checksum sizes.
163 *
164 * This is only called from generate_and_send_sums() but is a separate
165 * function to encapsulate the logic.
166 *
167 * The block size is a rounded square root of file length.
168 *
169 * The checksum size is determined according to:
170 * blocksum_bits = BLOCKSUM_EXP + 2*log2(file_len) - log2(block_len)
171 * provided by Donovan Baarda which gives a probability of rsync
172 * algorithm corrupting data and falling back using the whole md4
173 * checksums.
174 *
175 * This might be made one of several selectable heuristics.
176 */
177static void sum_sizes_sqroot(struct sum_struct *sum, int64 len)
178{
179 int32 blength;
180 int s2length;
181
182 if (block_size)
183 blength = block_size;
184 else if (len <= BLOCK_SIZE * BLOCK_SIZE)
185 blength = BLOCK_SIZE;
186 else {
187 int32 c;
188 int64 l;
189 int cnt;
190 for (c = 1, l = len, cnt = 0; l >>= 2; c <<= 1, cnt++) {}
191 if (cnt >= 31 || c >= MAX_BLOCK_SIZE)
192 blength = MAX_BLOCK_SIZE;
193 else {
194 blength = 0;
195 do {
196 blength |= c;
197 if (len < (int64)blength * blength)
198 blength &= ~c;
199 c >>= 1;
200 } while (c >= 8); /* round to multiple of 8 */
201 blength = MAX(blength, BLOCK_SIZE);
202 }
203 }
204
205 if (protocol_version < 27) {
206 s2length = csum_length;
207 } else if (csum_length == SUM_LENGTH) {
208 s2length = SUM_LENGTH;
209 } else {
210 int32 c;
211 int64 l;
212 int b = BLOCKSUM_BIAS;
213 for (l = len; l >>= 1; b += 2) {}
214 for (c = blength; c >>= 1 && b; b--) {}
215 /* add a bit, subtract rollsum, round up. */
216 s2length = (b + 1 - 32 + 7) / 8; /* --optimize in compiler-- */
217 s2length = MAX(s2length, csum_length);
218 s2length = MIN(s2length, SUM_LENGTH);
219 }
220
221 sum->flength = len;
222 sum->blength = blength;
223 sum->s2length = s2length;
224 sum->count = (len + (blength - 1)) / blength;
225 sum->remainder = (len % blength);
226
227 if (sum->count && verbose > 2) {
228 rprintf(FINFO,
229 "count=%.0f rem=%ld blength=%ld s2length=%d flength=%.0f\n",
230 (double)sum->count, (long)sum->remainder, (long)sum->blength,
231 sum->s2length, (double)sum->flength);
232 }
233}
234
235
236/*
237 * Generate and send a stream of signatures/checksums that describe a buffer
238 *
239 * Generate approximately one checksum every block_len bytes.
240 */
241static void generate_and_send_sums(int fd, OFF_T len, int f_out, int f_copy)
242{
243 int32 i;
244 struct map_struct *mapbuf;
245 struct sum_struct sum;
246 OFF_T offset = 0;
247
248 sum_sizes_sqroot(&sum, len);
249
250 if (len > 0)
251 mapbuf = map_file(fd, len, MAX_MAP_SIZE, sum.blength);
252 else
253 mapbuf = NULL;
254
255 write_sum_head(f_out, &sum);
256
257 for (i = 0; i < sum.count; i++) {
258 int32 n1 = (int32)MIN(len, (OFF_T)sum.blength);
259 char *map = map_ptr(mapbuf, offset, n1);
260 uint32 sum1 = get_checksum1(map, n1);
261 char sum2[SUM_LENGTH];
262
263 if (f_copy >= 0)
264 full_write(f_copy, map, n1);
265
266 get_checksum2(map, n1, sum2);
267
268 if (verbose > 3) {
269 rprintf(FINFO,
270 "chunk[%.0f] offset=%.0f len=%ld sum1=%08lx\n",
271 (double)i, (double)offset, (long)n1,
272 (unsigned long)sum1);
273 }
274 write_int(f_out, sum1);
275 write_buf(f_out, sum2, sum.s2length);
276 len -= n1;
277 offset += n1;
278 }
279
280 if (mapbuf)
281 unmap_file(mapbuf);
282}
283
284
285/* Try to find a filename in the same dir as "fname" with a similar name. */
286static int find_fuzzy(struct file_struct *file, struct file_list *dirlist)
287{
288 int fname_len, fname_suf_len;
289 const char *fname_suf, *fname = file->basename;
290 uint32 lowest_dist = 0x7FFFFFFF;
291 int j, lowest_j = -1;
292
293 fname_len = strlen(fname);
294 fname_suf = find_filename_suffix(fname, fname_len, &fname_suf_len);
295
296 for (j = 0; j < dirlist->count; j++) {
297 struct file_struct *fp = dirlist->files[j];
298 const char *suf, *name;
299 int len, suf_len;
300 uint32 dist;
301
302 if (!S_ISREG(fp->mode) || !fp->length
303 || fp->flags & FLAG_NO_FUZZY)
304 continue;
305
306 name = fp->basename;
307
308 if (fp->length == file->length
309 && fp->modtime == file->modtime) {
310 if (verbose > 4) {
311 rprintf(FINFO,
312 "fuzzy size/modtime match for %s\n",
313 name);
314 }
315 return j;
316 }
317
318 len = strlen(name);
319 suf = find_filename_suffix(name, len, &suf_len);
320
321 dist = fuzzy_distance(name, len, fname, fname_len);
322 /* Add some extra weight to how well the suffixes match. */
323 dist += fuzzy_distance(suf, suf_len, fname_suf, fname_suf_len)
324 * 10;
325 if (verbose > 4) {
326 rprintf(FINFO, "fuzzy distance for %s = %d.%05d\n",
327 name, (int)(dist>>16), (int)(dist&0xFFFF));
328 }
329 if (dist <= lowest_dist) {
330 lowest_dist = dist;
331 lowest_j = j;
332 }
333 }
334
335 return lowest_j;
336}
337
338
339/* Acts on flist->file's ndx'th item, whose name is fname. If a directory,
340 * make sure it exists, and has the right permissions/timestamp info. For
341 * all other non-regular files (symlinks, etc.) we create them here. For
342 * regular files that have changed, we try to find a basis file and then
343 * start sending checksums.
344 *
345 * Note that f_out is set to -1 when doing final directory-permission and
346 * modification-time repair. */
347static void recv_generator(char *fname, struct file_list *flist,
348 struct file_struct *file, int ndx,
349 int itemizing, int maybe_PERMS_REPORT,
350 enum logcode code,
351 int f_out, int f_out_name)
352{
353 static int missing_below = -1, excluded_below = -1;
354 static char *fuzzy_dirname = NULL;
355 static struct file_list *fuzzy_dirlist = NULL;
356 struct file_struct *fuzzy_file = NULL;
357 int fd = -1, f_copy = -1;
358 STRUCT_STAT st, partial_st;
359 struct file_struct *back_file = NULL;
360 int statret, stat_errno;
361 char *fnamecmp, *partialptr, *backupptr = NULL;
362 char fnamecmpbuf[MAXPATHLEN];
363 uchar fnamecmp_type;
364
365 if (list_only)
366 return;
367
368 if (!fname) {
369 if (fuzzy_dirlist) {
370 flist_free(fuzzy_dirlist);
371 fuzzy_dirlist = NULL;
372 fuzzy_dirname = NULL;
373 }
374 if (missing_below >= 0) {
375 dry_run--;
376 missing_below = -1;
377 }
378 return;
379 }
380
381 if (verbose > 2) {
382 rprintf(FINFO, "recv_generator(%s,%d)\n",
383 safe_fname(fname), ndx);
384 }
385
386 if (server_filter_list.head) {
387 if (excluded_below >= 0) {
388 if (file->dir.depth > excluded_below)
389 goto skipping;
390 excluded_below = -1;
391 }
392 if (check_filter(&server_filter_list, fname,
393 S_ISDIR(file->mode)) < 0) {
394 if (S_ISDIR(file->mode))
395 excluded_below = file->dir.depth;
396 skipping:
397 if (verbose) {
398 rprintf(FINFO,
399 "skipping server-excluded file \"%s\"\n",
400 safe_fname(fname));
401 }
402 return;
403 }
404 }
405
406 if (missing_below >= 0 && file->dir.depth <= missing_below) {
407 dry_run--;
408 missing_below = -1;
409 }
410 if (dry_run > 1) {
411 statret = -1;
412 stat_errno = ENOENT;
413 } else {
414 if (fuzzy_basis && S_ISREG(file->mode)) {
415 char *dn = file->dirname ? file->dirname : ".";
416 /* Yes, identical dirnames are guaranteed to have
417 * identical pointers at this point. */
418 if (fuzzy_dirname != dn) {
419 if (fuzzy_dirlist)
420 flist_free(fuzzy_dirlist);
421 fuzzy_dirname = dn;
422 fuzzy_dirlist = get_dirlist(fuzzy_dirname, 1);
423 }
424 }
425
426 statret = link_stat(fname, &st,
427 keep_dirlinks && S_ISDIR(file->mode));
428 stat_errno = errno;
429 }
430
431 if (only_existing && statret == -1 && stat_errno == ENOENT) {
432 /* we only want to update existing files */
433 if (verbose > 1) {
434 rprintf(FINFO, "not creating new file \"%s\"\n",
435 safe_fname(fname));
436 }
437 return;
438 }
439
440 if (statret == 0 && !preserve_perms
441 && S_ISDIR(st.st_mode) == S_ISDIR(file->mode)) {
442 /* if the file exists already and we aren't perserving
443 * permissions then act as though the remote end sent
444 * us the file permissions we already have */
445 file->mode = (file->mode & ~CHMOD_BITS)
446 | (st.st_mode & CHMOD_BITS);
447 }
448
449 if (S_ISDIR(file->mode)) {
450 /* The file to be received is a directory, so we need
451 * to prepare appropriately. If there is already a
452 * file of that name and it is *not* a directory, then
453 * we need to delete it. If it doesn't exist, then
454 * (perhaps recursively) create it. */
455 if (statret == 0 && !S_ISDIR(st.st_mode)) {
456 delete_file(fname, st.st_mode, DEL_TERSE);
457 statret = -1;
458 }
459 if (dry_run && statret != 0 && missing_below < 0) {
460 missing_below = file->dir.depth;
461 dry_run++;
462 }
463 if (itemizing && f_out != -1)
464 itemize(file, statret, &st, 0, f_out, ndx);
465 if (statret != 0 && do_mkdir(fname,file->mode) != 0 && errno != EEXIST) {
466 if (!relative_paths || errno != ENOENT
467 || create_directory_path(fname, orig_umask) < 0
468 || do_mkdir(fname, file->mode) < 0) {
469 rsyserr(FERROR, errno,
470 "recv_generator: mkdir %s failed",
471 full_fname(fname));
472 }
473 }
474 if (set_perms(fname, file, statret ? NULL : &st, 0)
475 && verbose && code && f_out != -1)
476 rprintf(code, "%s/\n", safe_fname(fname));
477 if (delete_during && f_out != -1 && csum_length != SUM_LENGTH
478 && (file->flags & FLAG_DEL_HERE))
479 delete_in_dir(flist, fname, file);
480 return;
481 }
482
483 if (max_size && file->length > max_size) {
484 if (verbose > 1) {
485 rprintf(FINFO, "%s is over max-size\n",
486 safe_fname(fname));
487 }
488 return;
489 }
490
491 if (preserve_links && S_ISLNK(file->mode)) {
492#ifdef SUPPORT_LINKS
493 if (safe_symlinks && unsafe_symlink(file->u.link, fname)) {
494 if (verbose) {
495 rprintf(FINFO,
496 "ignoring unsafe symlink %s -> \"%s\"\n",
497 full_fname(fname),
498 safe_fname(file->u.link));
499 }
500 return;
501 }
502 if (statret == 0) {
503 char lnk[MAXPATHLEN];
504 int len;
505
506 if (!S_ISDIR(st.st_mode)
507 && (len = readlink(fname, lnk, MAXPATHLEN-1)) > 0) {
508 lnk[len] = 0;
509 /* A link already pointing to the
510 * right place -- no further action
511 * required. */
512 if (strcmp(lnk, file->u.link) == 0) {
513 if (itemizing) {
514 itemize(file, 0, &st, 0,
515 f_out, ndx);
516 }
517 set_perms(fname, file, &st,
518 maybe_PERMS_REPORT);
519 return;
520 }
521 }
522 /* Not the right symlink (or not a symlink), so
523 * delete it. */
524 if (S_ISLNK(st.st_mode))
525 delete_file(fname, st.st_mode, DEL_TERSE);
526 else {
527 delete_file(fname, st.st_mode, DEL_TERSE);
528 statret = -1;
529 }
530 }
531 if (do_symlink(file->u.link,fname) != 0) {
532 rsyserr(FERROR, errno, "symlink %s -> \"%s\" failed",
533 full_fname(fname), safe_fname(file->u.link));
534 } else {
535 set_perms(fname,file,NULL,0);
536 if (itemizing) {
537 itemize(file, statret, &st, SID_UPDATING,
538 f_out, ndx);
539 }
540 if (code && verbose) {
541 rprintf(code, "%s -> %s\n", safe_fname(fname),
542 safe_fname(file->u.link));
543 }
544 if (remove_sent_files && !dry_run) {
545 char numbuf[4];
546 SIVAL(numbuf, 0, ndx);
547 send_msg(MSG_SUCCESS, numbuf, 4);
548 }
549 }
550#endif
551 return;
552 }
553
554 if (am_root && preserve_devices && IS_DEVICE(file->mode)) {
555 if (statret != 0 ||
556 st.st_mode != file->mode ||
557 st.st_rdev != file->u.rdev) {
558 if (IS_DEVICE(st.st_mode))
559 delete_file(fname, st.st_mode, DEL_TERSE);
560 else {
561 delete_file(fname, st.st_mode, DEL_TERSE);
562 statret = -1;
563 }
564 if (verbose > 2) {
565 rprintf(FINFO,"mknod(%s,0%o,0x%x)\n",
566 safe_fname(fname),
567 (int)file->mode, (int)file->u.rdev);
568 }
569 if (do_mknod(fname,file->mode,file->u.rdev) != 0) {
570 rsyserr(FERROR, errno, "mknod %s failed",
571 full_fname(fname));
572 } else {
573 set_perms(fname,file,NULL,0);
574 if (itemizing) {
575 itemize(file, statret, &st, SID_UPDATING,
576 f_out, ndx);
577 }
578 if (code && verbose) {
579 rprintf(code, "%s\n",
580 safe_fname(fname));
581 }
582 }
583 } else {
584 if (itemizing) {
585 itemize(file, statret, &st, 0,
586 f_out, ndx);
587 }
588 set_perms(fname, file, &st, maybe_PERMS_REPORT);
589 }
590 return;
591 }
592
593 if (preserve_hard_links && hard_link_check(file, HL_CHECK_MASTER))
594 return;
595
596 if (!S_ISREG(file->mode)) {
597 rprintf(FINFO, "skipping non-regular file \"%s\"\n",
598 safe_fname(fname));
599 return;
600 }
601
602 fnamecmp = fname;
603 fnamecmp_type = FNAMECMP_FNAME;
604
605 if (statret != 0 && basis_dir[0] != NULL) {
606 int fallback_match = -1;
607 int match_level = 0;
608 int i = 0;
609 do {
610 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
611 basis_dir[i], fname);
612 if (link_stat(fnamecmpbuf, &st, 0) == 0
613 && S_ISREG(st.st_mode)) {
614 statret = 0;
615 if (link_dest) {
616 if (!match_level) {
617 fallback_match = i;
618 match_level = 1;
619 } else if (match_level == 2
620 && !unchanged_attrs(file, &st))
621 continue;
622 if (!unchanged_file(fnamecmpbuf, file, &st))
623 continue;
624 fallback_match = i;
625 match_level = 2;
626 if (!unchanged_attrs(file, &st))
627 continue;
628 }
629 match_level = 3;
630 break;
631 }
632 } while (basis_dir[++i] != NULL);
633 if (statret == 0) {
634 if (match_level < 3) {
635 i = fallback_match;
636 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
637 basis_dir[i], fname);
638 }
639#ifdef HAVE_LINK
640 if (link_dest && match_level == 3 && !dry_run) {
641 if (do_link(fnamecmpbuf, fname) < 0) {
642 if (verbose) {
643 rsyserr(FINFO, errno,
644 "link %s => %s",
645 full_fname(fnamecmpbuf),
646 safe_fname(fname));
647 }
648 fnamecmp = fnamecmpbuf;
649 fnamecmp_type = i;
650 }
651 } else
652#endif
653 {
654 fnamecmp = fnamecmpbuf;
655 fnamecmp_type = i;
656 }
657 }
658 }
659
660 if (statret == 0 && !S_ISREG(st.st_mode)) {
661 if (delete_file(fname, st.st_mode, DEL_TERSE) != 0)
662 return;
663 statret = -1;
664 stat_errno = ENOENT;
665 }
666
667 if (partial_dir && (partialptr = partial_dir_fname(fname)) != NULL
668 && link_stat(partialptr, &partial_st, 0) == 0
669 && S_ISREG(partial_st.st_mode)) {
670 if (statret != 0)
671 goto prepare_to_open;
672 } else
673 partialptr = NULL;
674
675 if (statret != 0 && fuzzy_basis && dry_run <= 1) {
676 int j = find_fuzzy(file, fuzzy_dirlist);
677 if (j >= 0) {
678 fuzzy_file = fuzzy_dirlist->files[j];
679 f_name_to(fuzzy_file, fnamecmpbuf);
680 if (verbose > 2) {
681 rprintf(FINFO, "fuzzy basis selected for %s: %s\n",
682 safe_fname(fname), safe_fname(fnamecmpbuf));
683 }
684 st.st_mode = fuzzy_file->mode;
685 st.st_size = fuzzy_file->length;
686 st.st_mtime = fuzzy_file->modtime;
687 statret = 0;
688 fnamecmp = fnamecmpbuf;
689 fnamecmp_type = FNAMECMP_FUZZY;
690 }
691 }
692
693 if (statret != 0) {
694 if (preserve_hard_links && hard_link_check(file, HL_SKIP))
695 return;
696 if (stat_errno == ENOENT)
697 goto notify_others;
698 if (verbose > 1) {
699 rsyserr(FERROR, stat_errno,
700 "recv_generator: failed to stat %s",
701 full_fname(fname));
702 }
703 return;
704 }
705
706 if (opt_ignore_existing && fnamecmp_type == FNAMECMP_FNAME) {
707 if (verbose > 1)
708 rprintf(FINFO, "%s exists\n", safe_fname(fname));
709 return;
710 }
711
712 if (update_only && fnamecmp_type == FNAMECMP_FNAME
713 && cmp_modtime(st.st_mtime, file->modtime) > 0) {
714 if (verbose > 1)
715 rprintf(FINFO, "%s is newer\n", safe_fname(fname));
716 return;
717 }
718
719 if (!compare_dest && fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
720 ;
721 else if (fnamecmp_type == FNAMECMP_FUZZY)
722 ;
723 else if (unchanged_file(fnamecmp, file, &st)) {
724 if (itemizing) {
725 itemize(file, statret, &st,
726 fnamecmp_type == FNAMECMP_FNAME
727 ? 0 : SID_NO_DEST_AND_NO_UPDATE,
728 f_out, ndx);
729 }
730 if (fnamecmp_type == FNAMECMP_FNAME)
731 set_perms(fname, file, &st, maybe_PERMS_REPORT);
732 return;
733 }
734
735prepare_to_open:
736 if (partialptr) {
737 st = partial_st;
738 fnamecmp = partialptr;
739 fnamecmp_type = FNAMECMP_PARTIAL_DIR;
740 statret = 0;
741 }
742
743 if (dry_run || read_batch)
744 goto notify_others;
745 if (whole_file > 0) {
746 if (statret == 0)
747 statret = 1;
748 goto notify_others;
749 }
750
751 if (fuzzy_basis) {
752 int j = flist_find(fuzzy_dirlist, file);
753 if (j >= 0) /* don't use changing file as future fuzzy basis */
754 fuzzy_dirlist->files[j]->flags |= FLAG_NO_FUZZY;
755 }
756
757 /* open the file */
758 fd = do_open(fnamecmp, O_RDONLY, 0);
759
760 if (fd == -1) {
761 rsyserr(FERROR, errno, "failed to open %s, continuing",
762 full_fname(fnamecmp));
763 pretend_missing:
764 /* pretend the file didn't exist */
765 if (preserve_hard_links && hard_link_check(file, HL_SKIP))
766 return;
767 statret = -1;
768 goto notify_others;
769 }
770
771 if (inplace && make_backups) {
772 if (!(backupptr = get_backup_name(fname))) {
773 close(fd);
774 return;
775 }
776 if (!(back_file = make_file(fname, NULL, NO_FILTERS))) {
777 close(fd);
778 goto pretend_missing;
779 }
780 if (robust_unlink(backupptr) && errno != ENOENT) {
781 rsyserr(FERROR, errno, "unlink %s",
782 full_fname(backupptr));
783 free(back_file);
784 close(fd);
785 return;
786 }
787 if ((f_copy = do_open(backupptr,
788 O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0) {
789 rsyserr(FERROR, errno, "open %s",
790 full_fname(backupptr));
791 free(back_file);
792 close(fd);
793 return;
794 }
795 fnamecmp_type = FNAMECMP_BACKUP;
796 }
797
798 if (verbose > 3) {
799 rprintf(FINFO, "gen mapped %s of size %.0f\n",
800 safe_fname(fnamecmp), (double)st.st_size);
801 }
802
803 if (verbose > 2)
804 rprintf(FINFO, "generating and sending sums for %d\n", ndx);
805
806notify_others:
807 write_int(f_out, ndx);
808 if (itemizing) {
809 int iflags = SID_UPDATING;
810 if (always_checksum)
811 iflags |= SID_REPORT_CHECKSUM;
812 if (fnamecmp_type != FNAMECMP_FNAME)
813 iflags |= SID_USING_ALT_BASIS;
814 itemize(file, statret, &st, iflags, f_out, -1);
815 }
816 if (f_out_name >= 0) {
817 write_byte(f_out_name, fnamecmp_type);
818 if (fnamecmp_type == FNAMECMP_FUZZY) {
819 uchar lenbuf[3], *lb = lenbuf;
820 int len = strlen(fuzzy_file->basename);
821 if (len > 0x7F) {
822#if MAXPATHLEN > 0x7FFF
823 *lb++ = len / 0x10000 + 0x80;
824 *lb++ = len / 0x100;
825#else
826 *lb++ = len / 0x100 + 0x80;
827#endif
828 }
829 *lb = len;
830 write_buf(f_out_name, (char*)lenbuf, lb - lenbuf + 1);
831 write_buf(f_out_name, fuzzy_file->basename, len);
832 }
833 }
834
835 if (dry_run || read_batch)
836 return;
837
838 if (statret == 0) {
839 generate_and_send_sums(fd, st.st_size, f_out, f_copy);
840
841 if (f_copy >= 0) {
842 close(f_copy);
843 set_perms(backupptr, back_file, NULL, 0);
844 if (verbose > 1) {
845 rprintf(FINFO, "backed up %s to %s\n",
846 safe_fname(fname), safe_fname(backupptr));
847 }
848 free(back_file);
849 }
850
851 close(fd);
852 } else
853 write_sum_head(f_out, NULL);
854}
855
856
857void generate_files(int f_out, struct file_list *flist, char *local_name,
858 int f_out_name)
859{
860 int i;
861 int phase = 0;
862 char fbuf[MAXPATHLEN];
863 int itemizing, maybe_PERMS_REPORT;
864 enum logcode code;
865 int need_retouch_dir_times = preserve_times && !omit_dir_times;
866 int need_retouch_dir_perms = 0;
867 int save_only_existing = only_existing;
868 int save_opt_ignore_existing = opt_ignore_existing;
869 int allowed_lull = io_timeout / 2;
870
871 if (protocol_version >= 29) {
872 itemizing = 1;
873 maybe_PERMS_REPORT = log_format_has_i ? 0 : PERMS_REPORT;
874 code = daemon_log_format_has_i ? 0 : FLOG;
875 } else if (am_daemon) {
876 itemizing = daemon_log_format_has_i && !dry_run;
877 maybe_PERMS_REPORT = PERMS_REPORT;
878 code = itemizing || dry_run ? FCLIENT : FINFO;
879 } else if (!am_server) {
880 itemizing = log_format_has_i;
881 maybe_PERMS_REPORT = log_format_has_i ? 0 : PERMS_REPORT;
882 code = itemizing ? 0 : FINFO;
883 } else {
884 itemizing = 0;
885 maybe_PERMS_REPORT = PERMS_REPORT;
886 code = FINFO;
887 }
888
889 if (verbose > 2) {
890 rprintf(FINFO, "generator starting pid=%ld count=%d\n",
891 (long)getpid(), flist->count);
892 }
893
894 if (verbose >= 2) {
895 rprintf(FINFO, "delta-transmission %s\n",
896 whole_file > 0
897 ? "disabled for local transfer or --whole-file"
898 : "enabled");
899 }
900
901 if (protocol_version < 29)
902 ignore_timeout = 1;
903
904 for (i = 0; i < flist->count; i++) {
905 struct file_struct *file = flist->files[i];
906 struct file_struct copy;
907
908 if (!file->basename)
909 continue;
910
911 /* We need to ensure that any dirs we create have writeable
912 * permissions during the time we are putting files within
913 * them. This is then fixed after the transfer is done. */
914 if (!am_root && S_ISDIR(file->mode) && !(file->mode & S_IWUSR)) {
915 copy = *file;
916 copy.mode |= S_IWUSR; /* user write */
917 file = &copy;
918 need_retouch_dir_perms = 1;
919 }
920
921 recv_generator(local_name ? local_name : f_name_to(file, fbuf),
922 flist, file, i, itemizing, maybe_PERMS_REPORT,
923 code, f_out, f_out_name);
924
925 if (allowed_lull && !(i % 100))
926 maybe_send_keepalive(allowed_lull, flist->count);
927 }
928 recv_generator(NULL, NULL, NULL, 0, 0, 0, code, -1, -1);
929 if (delete_during)
930 delete_in_dir(NULL, NULL, NULL);
931
932 phase++;
933 csum_length = SUM_LENGTH;
934 only_existing = max_size = opt_ignore_existing = 0;
935 update_only = always_checksum = size_only = 0;
936 ignore_times = 1;
937
938 /* We expect to just sit around now, so don't exit on a timeout.
939 * If we really get a timeout then the other process should exit. */
940 ignore_timeout = 1;
941
942 if (verbose > 2)
943 rprintf(FINFO,"generate_files phase=%d\n",phase);
944
945 write_int(f_out, -1);
946
947 /* files can cycle through the system more than once
948 * to catch initial checksum errors */
949 while ((i = get_redo_num()) != -1) {
950 struct file_struct *file = flist->files[i];
951 recv_generator(local_name ? local_name : f_name_to(file, fbuf),
952 flist, file, i, itemizing, maybe_PERMS_REPORT,
953 code, f_out, f_out_name);
954 if (allowed_lull)
955 maybe_send_keepalive(allowed_lull, flist->count);
956 }
957
958 phase++;
959 only_existing = save_only_existing;
960 opt_ignore_existing = save_opt_ignore_existing;
961
962 if (verbose > 2)
963 rprintf(FINFO,"generate_files phase=%d\n",phase);
964
965 write_int(f_out, -1);
966
967 /* Read post-redo-phase MSG_DONE and any prior messages. */
968 get_redo_num();
969
970 if (preserve_hard_links)
971 do_hard_links();
972
973 if ((need_retouch_dir_perms || need_retouch_dir_times)
974 && !list_only && !local_name && !dry_run) {
975 /* Now we need to fix any directory permissions that were
976 * modified during the transfer and/or re-set any tweaked
977 * modified-time values. */
978 for (i = 0; i < flist->count; i++) {
979 struct file_struct *file = flist->files[i];
980 if (!file->basename || !S_ISDIR(file->mode))
981 continue;
982 if (!need_retouch_dir_times && file->mode & S_IWUSR)
983 continue;
984 recv_generator(local_name ? local_name : f_name(file),
985 flist, file, i, itemizing,
986 maybe_PERMS_REPORT, code, -1, -1);
987 }
988 }
989 recv_generator(NULL, NULL, NULL, 0, 0, 0, code, -1, -1);
990
991 if (verbose > 2)
992 rprintf(FINFO,"generate_files finished\n");
993}