- Use an int32 for the each block-size variable.
[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 relative_paths;
29extern int keep_dirlinks;
30extern int preserve_links;
31extern int am_root;
32extern int preserve_devices;
33extern int preserve_hard_links;
34extern int preserve_perms;
35extern int preserve_uid;
36extern int preserve_gid;
37extern int update_only;
38extern int opt_ignore_existing;
39extern int inplace;
40extern int make_backups;
41extern int csum_length;
42extern int ignore_times;
43extern int size_only;
44extern OFF_T max_size;
45extern int io_timeout;
46extern int protocol_version;
47extern int always_checksum;
48extern char *partial_dir;
49extern char *basis_dir[];
50extern int copy_dest;
51extern int link_dest;
52extern int whole_file;
53extern int local_server;
54extern int list_only;
55extern int read_batch;
56extern int only_existing;
57extern int orig_umask;
58extern int safe_symlinks;
59extern unsigned int block_size;
60
61extern struct exclude_list_struct server_exclude_list;
62
63static int unchanged_attrs(struct file_struct *file, STRUCT_STAT *st)
64{
65 if (preserve_perms
66 && (st->st_mode & CHMOD_BITS) != (file->mode & CHMOD_BITS))
67 return 0;
68
69 if (am_root && preserve_uid && st->st_uid != file->uid)
70 return 0;
71
72 if (preserve_gid && file->gid != GID_NONE && st->st_gid != file->gid)
73 return 0;
74
75 return 1;
76}
77
78/* Perform our quick-check heuristic for determining if a file is unchanged. */
79static int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st)
80{
81 if (st->st_size != file->length)
82 return 0;
83
84 /* if always checksum is set then we use the checksum instead
85 of the file time to determine whether to sync */
86 if (always_checksum && S_ISREG(st->st_mode)) {
87 char sum[MD4_SUM_LENGTH];
88 file_checksum(fn, sum, st->st_size);
89 return memcmp(sum, file->u.sum, protocol_version < 21 ? 2
90 : MD4_SUM_LENGTH) == 0;
91 }
92
93 if (size_only)
94 return 1;
95
96 if (ignore_times)
97 return 0;
98
99 return cmp_modtime(st->st_mtime, file->modtime) == 0;
100}
101
102
103/*
104 * NULL sum_struct means we have no checksums
105 */
106void write_sum_head(int f, struct sum_struct *sum)
107{
108 static struct sum_struct null_sum;
109
110 if (sum == NULL)
111 sum = &null_sum;
112
113 write_int(f, sum->count);
114 write_int(f, sum->blength);
115 if (protocol_version >= 27)
116 write_int(f, sum->s2length);
117 write_int(f, sum->remainder);
118}
119
120/*
121 * set (initialize) the size entries in the per-file sum_struct
122 * calculating dynamic block and checksum sizes.
123 *
124 * This is only called from generate_and_send_sums() but is a separate
125 * function to encapsulate the logic.
126 *
127 * The block size is a rounded square root of file length.
128 *
129 * The checksum size is determined according to:
130 * blocksum_bits = BLOCKSUM_EXP + 2*log2(file_len) - log2(block_len)
131 * provided by Donovan Baarda which gives a probability of rsync
132 * algorithm corrupting data and falling back using the whole md4
133 * checksums.
134 *
135 * This might be made one of several selectable heuristics.
136 */
137static void sum_sizes_sqroot(struct sum_struct *sum, uint64 len)
138{
139 unsigned int blength;
140 int s2length;
141 uint32 c;
142 uint64 l;
143
144 if (block_size) {
145 blength = block_size;
146 } else if (len <= BLOCK_SIZE * BLOCK_SIZE) {
147 blength = BLOCK_SIZE;
148 } else {
149 l = len;
150 c = 1;
151 while (l >>= 2) {
152 c <<= 1;
153 }
154 blength = 0;
155 do {
156 blength |= c;
157 if (len < (uint64)blength * blength)
158 blength &= ~c;
159 c >>= 1;
160 } while (c >= 8); /* round to multiple of 8 */
161 blength = MAX(blength, BLOCK_SIZE);
162 }
163
164 if (protocol_version < 27) {
165 s2length = csum_length;
166 } else if (csum_length == SUM_LENGTH) {
167 s2length = SUM_LENGTH;
168 } else {
169 int b = BLOCKSUM_BIAS;
170 l = len;
171 while (l >>= 1) {
172 b += 2;
173 }
174 c = blength;
175 while (c >>= 1 && b) {
176 b--;
177 }
178 s2length = (b + 1 - 32 + 7) / 8; /* add a bit,
179 * subtract rollsum,
180 * round up
181 * --optimize in compiler--
182 */
183 s2length = MAX(s2length, csum_length);
184 s2length = MIN(s2length, SUM_LENGTH);
185 }
186
187 sum->flength = len;
188 sum->blength = blength;
189 sum->s2length = s2length;
190 sum->count = (len + (blength - 1)) / blength;
191 sum->remainder = (len % blength);
192
193 if (sum->count && verbose > 2) {
194 rprintf(FINFO, "count=%.0f rem=%u blength=%u s2length=%d flength=%.0f\n",
195 (double)sum->count, sum->remainder, sum->blength,
196 sum->s2length, (double)sum->flength);
197 }
198}
199
200
201/*
202 * Generate and send a stream of signatures/checksums that describe a buffer
203 *
204 * Generate approximately one checksum every block_len bytes.
205 */
206static void generate_and_send_sums(int fd, OFF_T len, int f_out, int f_copy)
207{
208 size_t i;
209 struct map_struct *mapbuf;
210 struct sum_struct sum;
211 OFF_T offset = 0;
212
213 sum_sizes_sqroot(&sum, len);
214
215 if (len > 0)
216 mapbuf = map_file(fd, len, MAX_MAP_SIZE, sum.blength);
217 else
218 mapbuf = NULL;
219
220 write_sum_head(f_out, &sum);
221
222 for (i = 0; i < sum.count; i++) {
223 unsigned int n1 = MIN(len, sum.blength);
224 char *map = map_ptr(mapbuf, offset, n1);
225 uint32 sum1 = get_checksum1(map, n1);
226 char sum2[SUM_LENGTH];
227
228 if (f_copy >= 0)
229 full_write(f_copy, map, n1);
230
231 get_checksum2(map, n1, sum2);
232
233 if (verbose > 3) {
234 rprintf(FINFO,
235 "chunk[%.0f] offset=%.0f len=%u sum1=%08lx\n",
236 (double)i, (double)offset, n1,
237 (unsigned long)sum1);
238 }
239 write_int(f_out, sum1);
240 write_buf(f_out, sum2, sum.s2length);
241 len -= n1;
242 offset += n1;
243 }
244
245 if (mapbuf)
246 unmap_file(mapbuf);
247}
248
249
250
251/*
252 * Acts on file number @p i from @p flist, whose name is @p fname.
253 *
254 * First fixes up permissions, then generates checksums for the file.
255 *
256 * @note This comment was added later by mbp who was trying to work it
257 * out. It might be wrong.
258 */
259static void recv_generator(char *fname, struct file_struct *file, int i,
260 int f_out, int f_out_name)
261{
262 int fd = -1, f_copy = -1;
263 STRUCT_STAT st, partial_st;
264 struct file_struct *back_file = NULL;
265 int statret, stat_errno;
266 char *fnamecmp, *partialptr, *backupptr = NULL;
267 char fnamecmpbuf[MAXPATHLEN];
268 uchar fnamecmp_type;
269
270 if (list_only)
271 return;
272
273 if (verbose > 2)
274 rprintf(FINFO, "recv_generator(%s,%d)\n", safe_fname(fname), i);
275
276 if (server_exclude_list.head
277 && check_exclude(&server_exclude_list, fname,
278 S_ISDIR(file->mode)) < 0) {
279 if (verbose) {
280 rprintf(FINFO, "skipping server-excluded file \"%s\"\n",
281 safe_fname(fname));
282 }
283 return;
284 }
285
286 if (dry_run > 1) {
287 statret = -1;
288 stat_errno = ENOENT;
289 } else {
290 statret = link_stat(fname, &st,
291 keep_dirlinks && S_ISDIR(file->mode));
292 stat_errno = errno;
293 }
294
295 if (only_existing && statret == -1 && stat_errno == ENOENT) {
296 /* we only want to update existing files */
297 if (verbose > 1) {
298 rprintf(FINFO, "not creating new file \"%s\"\n",
299 safe_fname(fname));
300 }
301 return;
302 }
303
304 if (statret == 0 && !preserve_perms
305 && S_ISDIR(st.st_mode) == S_ISDIR(file->mode)) {
306 /* if the file exists already and we aren't perserving
307 * permissions then act as though the remote end sent
308 * us the file permissions we already have */
309 file->mode = (file->mode & ~CHMOD_BITS)
310 | (st.st_mode & CHMOD_BITS);
311 }
312
313 if (S_ISDIR(file->mode)) {
314 /* The file to be received is a directory, so we need
315 * to prepare appropriately. If there is already a
316 * file of that name and it is *not* a directory, then
317 * we need to delete it. If it doesn't exist, then
318 * recursively create it. */
319
320 if (dry_run)
321 return; /* TODO: causes inaccuracies -- fix */
322 if (statret == 0 && !S_ISDIR(st.st_mode)) {
323 if (robust_unlink(fname) != 0) {
324 rsyserr(FERROR, errno,
325 "recv_generator: unlink %s to make room for directory",
326 full_fname(fname));
327 return;
328 }
329 statret = -1;
330 }
331 if (statret != 0 && do_mkdir(fname,file->mode) != 0 && errno != EEXIST) {
332 if (!(relative_paths && errno == ENOENT
333 && create_directory_path(fname, orig_umask) == 0
334 && do_mkdir(fname, file->mode) == 0)) {
335 rsyserr(FERROR, errno,
336 "recv_generator: mkdir %s failed",
337 full_fname(fname));
338 }
339 }
340 /* f_out is set to -1 when doing final directory-permission
341 * and modification-time repair. */
342 if (set_perms(fname, file, statret ? NULL : &st, 0)
343 && verbose && f_out != -1)
344 rprintf(FINFO, "%s/\n", safe_fname(fname));
345 return;
346 } else if (max_size && file->length > max_size) {
347 if (verbose > 1)
348 rprintf(FINFO, "%s is over max-size\n", fname);
349 return;
350 }
351
352 if (preserve_links && S_ISLNK(file->mode)) {
353#if SUPPORT_LINKS
354 char lnk[MAXPATHLEN];
355 int l;
356
357 if (safe_symlinks && unsafe_symlink(file->u.link, fname)) {
358 if (verbose) {
359 rprintf(FINFO, "ignoring unsafe symlink %s -> \"%s\"\n",
360 full_fname(fname), file->u.link);
361 }
362 return;
363 }
364 if (statret == 0) {
365 l = readlink(fname,lnk,MAXPATHLEN-1);
366 if (l > 0) {
367 lnk[l] = 0;
368 /* A link already pointing to the
369 * right place -- no further action
370 * required. */
371 if (strcmp(lnk,file->u.link) == 0) {
372 set_perms(fname, file, &st,
373 PERMS_REPORT);
374 return;
375 }
376 }
377 /* Not a symlink, so delete whatever's
378 * already there and put a new symlink
379 * in place. */
380 delete_file(fname);
381 }
382 if (do_symlink(file->u.link,fname) != 0) {
383 rsyserr(FERROR, errno, "symlink %s -> \"%s\" failed",
384 full_fname(fname), safe_fname(file->u.link));
385 } else {
386 set_perms(fname,file,NULL,0);
387 if (verbose) {
388 rprintf(FINFO, "%s -> %s\n", safe_fname(fname),
389 safe_fname(file->u.link));
390 }
391 }
392#endif
393 return;
394 }
395
396 if (am_root && preserve_devices && IS_DEVICE(file->mode)) {
397 if (statret != 0 ||
398 st.st_mode != file->mode ||
399 st.st_rdev != file->u.rdev) {
400 delete_file(fname);
401 if (verbose > 2) {
402 rprintf(FINFO,"mknod(%s,0%o,0x%x)\n",
403 safe_fname(fname),
404 (int)file->mode, (int)file->u.rdev);
405 }
406 if (do_mknod(fname,file->mode,file->u.rdev) != 0) {
407 rsyserr(FERROR, errno, "mknod %s failed",
408 full_fname(fname));
409 } else {
410 set_perms(fname,file,NULL,0);
411 if (verbose) {
412 rprintf(FINFO, "%s\n",
413 safe_fname(fname));
414 }
415 }
416 } else {
417 set_perms(fname, file, &st, PERMS_REPORT);
418 }
419 return;
420 }
421
422 if (preserve_hard_links && hard_link_check(file, HL_CHECK_MASTER))
423 return;
424
425 if (!S_ISREG(file->mode)) {
426 rprintf(FINFO, "skipping non-regular file \"%s\"\n",
427 safe_fname(fname));
428 return;
429 }
430
431 fnamecmp = fname;
432 fnamecmp_type = FNAMECMP_FNAME;
433
434 if (statret == -1 && basis_dir[0] != NULL) {
435 int fallback_match = -1;
436 int match_level = 0;
437 int i = 0;
438 do {
439 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
440 basis_dir[i], fname);
441 if (link_stat(fnamecmpbuf, &st, 0) == 0
442 && S_ISREG(st.st_mode)) {
443 statret = 0;
444 if (link_dest) {
445 if (!match_level) {
446 fallback_match = i;
447 match_level = 1;
448 } else if (match_level == 2
449 && !unchanged_attrs(file, &st))
450 continue;
451 if (!unchanged_file(fnamecmpbuf, file, &st))
452 continue;
453 fallback_match = i;
454 match_level = 2;
455 if (!unchanged_attrs(file, &st))
456 continue;
457 }
458 match_level = 3;
459 break;
460 }
461 } while (basis_dir[++i] != NULL);
462 if (statret == 0) {
463 if (match_level < 3) {
464 i = fallback_match;
465 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
466 basis_dir[i], fname);
467 }
468#if HAVE_LINK
469 if (link_dest && match_level == 3 && !dry_run) {
470 if (do_link(fnamecmpbuf, fname) < 0) {
471 if (verbose) {
472 rsyserr(FINFO, errno,
473 "link %s => %s",
474 fnamecmpbuf,
475 safe_fname(fname));
476 }
477 fnamecmp = fnamecmpbuf;
478 fnamecmp_type = FNAMECMP_BASIS_DIR + i;
479 }
480 } else
481#endif
482 {
483 fnamecmp = fnamecmpbuf;
484 fnamecmp_type = FNAMECMP_BASIS_DIR + i;
485 }
486 }
487 }
488
489 if (statret == 0 && !S_ISREG(st.st_mode)) {
490 if (delete_file(fname) != 0)
491 return;
492 statret = -1;
493 stat_errno = ENOENT;
494 }
495
496 if (partial_dir && (partialptr = partial_dir_fname(fname))
497 && link_stat(partialptr, &partial_st, 0) == 0
498 && S_ISREG(partial_st.st_mode)) {
499 if (statret == -1)
500 goto prepare_to_open;
501 } else
502 partialptr = NULL;
503
504 if (statret == -1) {
505 if (preserve_hard_links && hard_link_check(file, HL_SKIP))
506 return;
507 if (stat_errno == ENOENT)
508 goto notify_others;
509 if (verbose > 1) {
510 rsyserr(FERROR, stat_errno,
511 "recv_generator: failed to stat %s",
512 full_fname(fname));
513 }
514 return;
515 }
516
517 if (opt_ignore_existing && fnamecmp_type == FNAMECMP_FNAME) {
518 if (verbose > 1)
519 rprintf(FINFO, "%s exists\n", safe_fname(fname));
520 return;
521 }
522
523 if (update_only && fnamecmp_type == FNAMECMP_FNAME
524 && cmp_modtime(st.st_mtime, file->modtime) > 0) {
525 if (verbose > 1)
526 rprintf(FINFO, "%s is newer\n", safe_fname(fname));
527 return;
528 }
529
530 if ((link_dest || copy_dest) && fnamecmp_type != FNAMECMP_FNAME)
531 ;
532 else if (unchanged_file(fnamecmp, file, &st)) {
533 if (fnamecmp_type == FNAMECMP_FNAME)
534 set_perms(fname, file, &st, PERMS_REPORT);
535 return;
536 }
537
538prepare_to_open:
539 if (dry_run || whole_file > 0) {
540 statret = -1;
541 goto notify_others;
542 }
543 if (read_batch)
544 goto notify_others;
545
546 if (partialptr) {
547 st = partial_st;
548 fnamecmp = partialptr;
549 fnamecmp_type = FNAMECMP_PARTIAL_DIR;
550 }
551
552 /* open the file */
553 fd = do_open(fnamecmp, O_RDONLY, 0);
554
555 if (fd == -1) {
556 rsyserr(FERROR, errno, "failed to open %s, continuing",
557 full_fname(fnamecmp));
558 pretend_missing:
559 /* pretend the file didn't exist */
560 if (preserve_hard_links && hard_link_check(file, HL_SKIP))
561 return;
562 statret = -1;
563 goto notify_others;
564 }
565
566 if (inplace && make_backups) {
567 if (!(backupptr = get_backup_name(fname))) {
568 close(fd);
569 return;
570 }
571 if (!(back_file = make_file(fname, NULL, NO_EXCLUDES))) {
572 close(fd);
573 goto pretend_missing;
574 }
575 if (robust_unlink(backupptr) && errno != ENOENT) {
576 rsyserr(FERROR, errno, "unlink %s",
577 full_fname(backupptr));
578 free(back_file);
579 close(fd);
580 return;
581 }
582 if ((f_copy = do_open(backupptr,
583 O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0) {
584 rsyserr(FERROR, errno, "open %s",
585 full_fname(backupptr));
586 free(back_file);
587 close(fd);
588 return;
589 }
590 fnamecmp_type = FNAMECMP_BACKUP;
591 }
592
593 if (verbose > 3) {
594 rprintf(FINFO, "gen mapped %s of size %.0f\n",
595 safe_fname(fnamecmp), (double)st.st_size);
596 }
597
598 if (verbose > 2)
599 rprintf(FINFO, "generating and sending sums for %d\n", i);
600
601notify_others:
602 write_int(f_out, i);
603 if (f_out_name >= 0)
604 write_byte(f_out_name, fnamecmp_type);
605
606 if (dry_run || read_batch)
607 return;
608
609 if (statret == 0) {
610 generate_and_send_sums(fd, st.st_size, f_out, f_copy);
611
612 if (f_copy >= 0) {
613 close(f_copy);
614 set_perms(backupptr, back_file, NULL, 0);
615 if (verbose > 1) {
616 rprintf(FINFO, "backed up %s to %s\n",
617 fname, backupptr);
618 }
619 free(back_file);
620 }
621
622 close(fd);
623 } else
624 write_sum_head(f_out, NULL);
625}
626
627
628void generate_files(int f_out, struct file_list *flist, char *local_name,
629 int f_out_name)
630{
631 int i;
632 int phase = 0;
633 char fbuf[MAXPATHLEN];
634
635 if (verbose > 2) {
636 rprintf(FINFO, "generator starting pid=%ld count=%d\n",
637 (long)getpid(), flist->count);
638 }
639
640 if (verbose >= 2) {
641 rprintf(FINFO,
642 whole_file > 0
643 ? "delta-transmission disabled for local transfer or --whole-file\n"
644 : "delta transmission enabled\n");
645 }
646
647 /* we expect to just sit around now, so don't exit on a
648 timeout. If we really get a timeout then the other process should
649 exit */
650 io_timeout = 0;
651
652 for (i = 0; i < flist->count; i++) {
653 struct file_struct *file = flist->files[i];
654 struct file_struct copy;
655
656 if (!file->basename)
657 continue;
658 /* we need to ensure that any directories we create have writeable
659 permissions initially so that we can create the files within
660 them. This is then fixed after the files are transferred */
661 if (!am_root && S_ISDIR(file->mode) && !(file->mode & S_IWUSR)) {
662 copy = *file;
663 /* XXX: Could this be causing a problem on SCO? Perhaps their
664 * handling of permissions is strange? */
665 copy.mode |= S_IWUSR; /* user write */
666 file = &copy;
667 }
668
669 recv_generator(local_name ? local_name : f_name_to(file, fbuf),
670 file, i, f_out, f_out_name);
671 }
672
673 phase++;
674 csum_length = SUM_LENGTH;
675 ignore_times = 1;
676
677 if (verbose > 2)
678 rprintf(FINFO,"generate_files phase=%d\n",phase);
679
680 write_int(f_out, -1);
681
682 /* files can cycle through the system more than once
683 * to catch initial checksum errors */
684 while ((i = get_redo_num()) != -1) {
685 struct file_struct *file = flist->files[i];
686 recv_generator(local_name ? local_name : f_name_to(file, fbuf),
687 file, i, f_out, f_out_name);
688 }
689
690 phase++;
691 if (verbose > 2)
692 rprintf(FINFO,"generate_files phase=%d\n",phase);
693
694 write_int(f_out, -1);
695
696 if (preserve_hard_links)
697 do_hard_links();
698
699 /* now we need to fix any directory permissions that were
700 * modified during the transfer */
701 for (i = 0; i < flist->count; i++) {
702 struct file_struct *file = flist->files[i];
703 if (!file->basename || !S_ISDIR(file->mode))
704 continue;
705 recv_generator(local_name ? local_name : f_name(file),
706 file, i, -1, -1);
707 }
708
709 if (verbose > 2)
710 rprintf(FINFO,"generate_files finished\n");
711}