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