1 /* -*- c-file-style: "linux" -*-
3 rsync -- fast file replication program
5 Copyright (C) 1996-2000 by Andrew Tridgell
6 Copyright (C) Paul Mackerras 1996
7 Copyright (C) 2002 by Martin Pool <mbp@samba.org>
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.
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.
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.
28 extern int relative_paths;
29 extern int keep_dirlinks;
30 extern int preserve_links;
32 extern int preserve_devices;
33 extern int preserve_hard_links;
34 extern int preserve_perms;
35 extern int preserve_uid;
36 extern int preserve_gid;
37 extern int update_only;
38 extern int opt_ignore_existing;
40 extern int make_backups;
41 extern int csum_length;
42 extern int ignore_times;
44 extern OFF_T max_size;
45 extern int io_timeout;
46 extern int protocol_version;
47 extern int always_checksum;
48 extern char *partial_dir;
49 extern char *basis_dir[];
52 extern int whole_file;
53 extern int local_server;
55 extern int read_batch;
56 extern int only_existing;
57 extern int orig_umask;
58 extern int safe_symlinks;
59 extern unsigned int block_size;
61 extern struct exclude_list_struct server_exclude_list;
63 static int unchanged_attrs(struct file_struct *file, STRUCT_STAT *st)
66 && (st->st_mode & CHMOD_BITS) != (file->mode & CHMOD_BITS))
69 if (am_root && preserve_uid && st->st_uid != file->uid)
72 if (preserve_gid && file->gid != GID_NONE && st->st_gid != file->gid)
78 /* Perform our quick-check heuristic for determining if a file is unchanged. */
79 static int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st)
81 if (st->st_size != file->length)
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;
99 return cmp_modtime(st->st_mtime, file->modtime) == 0;
104 * NULL sum_struct means we have no checksums
106 void write_sum_head(int f, struct sum_struct *sum)
108 static struct sum_struct null_sum;
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);
121 * set (initialize) the size entries in the per-file sum_struct
122 * calculating dynamic block and checksum sizes.
124 * This is only called from generate_and_send_sums() but is a separate
125 * function to encapsulate the logic.
127 * The block size is a rounded square root of file length.
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
135 * This might be made one of several selectable heuristics.
137 static void sum_sizes_sqroot(struct sum_struct *sum, uint64 len)
139 unsigned int blength;
145 blength = block_size;
146 } else if (len <= BLOCK_SIZE * BLOCK_SIZE) {
147 blength = BLOCK_SIZE;
157 if (len < (uint64)blength * blength)
160 } while (c >= 8); /* round to multiple of 8 */
161 blength = MAX(blength, BLOCK_SIZE);
164 if (protocol_version < 27) {
165 s2length = csum_length;
166 } else if (csum_length == SUM_LENGTH) {
167 s2length = SUM_LENGTH;
169 int b = BLOCKSUM_BIAS;
175 while (c >>= 1 && b) {
178 s2length = (b + 1 - 32 + 7) / 8; /* add a bit,
181 * --optimize in compiler--
183 s2length = MAX(s2length, csum_length);
184 s2length = MIN(s2length, SUM_LENGTH);
188 sum->blength = blength;
189 sum->s2length = s2length;
190 sum->count = (len + (blength - 1)) / blength;
191 sum->remainder = (len % blength);
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);
202 * Generate and send a stream of signatures/checksums that describe a buffer
204 * Generate approximately one checksum every block_len bytes.
206 static void generate_and_send_sums(int fd, OFF_T len, int f_out, int f_copy)
209 struct map_struct *mapbuf;
210 struct sum_struct sum;
213 sum_sizes_sqroot(&sum, len);
216 mapbuf = map_file(fd, len, MAX_MAP_SIZE, sum.blength);
220 write_sum_head(f_out, &sum);
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];
229 full_write(f_copy, map, n1);
231 get_checksum2(map, n1, sum2);
235 "chunk[%.0f] offset=%.0f len=%u sum1=%08lx\n",
236 (double)i, (double)offset, n1,
237 (unsigned long)sum1);
239 write_int(f_out, sum1);
240 write_buf(f_out, sum2, sum.s2length);
252 * Acts on file number @p i from @p flist, whose name is @p fname.
254 * First fixes up permissions, then generates checksums for the file.
256 * @note This comment was added later by mbp who was trying to work it
257 * out. It might be wrong.
259 static void recv_generator(char *fname, struct file_struct *file, int i,
260 int f_out, int f_out_name)
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];
274 rprintf(FINFO, "recv_generator(%s,%d)\n", safe_fname(fname), i);
276 if (server_exclude_list.head
277 && check_exclude(&server_exclude_list, fname,
278 S_ISDIR(file->mode)) < 0) {
280 rprintf(FINFO, "skipping server-excluded file \"%s\"\n",
290 statret = link_stat(fname, &st,
291 keep_dirlinks && S_ISDIR(file->mode));
295 if (only_existing && statret == -1 && stat_errno == ENOENT) {
296 /* we only want to update existing files */
298 rprintf(FINFO, "not creating new file \"%s\"\n",
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);
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. */
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",
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",
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));
346 } else if (max_size && file->length > max_size) {
348 rprintf(FINFO, "%s is over max-size\n", fname);
352 if (preserve_links && S_ISLNK(file->mode)) {
354 char lnk[MAXPATHLEN];
357 if (safe_symlinks && unsafe_symlink(file->u.link, fname)) {
359 rprintf(FINFO, "ignoring unsafe symlink %s -> \"%s\"\n",
360 full_fname(fname), file->u.link);
365 l = readlink(fname,lnk,MAXPATHLEN-1);
368 /* A link already pointing to the
369 * right place -- no further action
371 if (strcmp(lnk,file->u.link) == 0) {
372 set_perms(fname, file, &st,
377 /* Not a symlink, so delete whatever's
378 * already there and put a new symlink
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));
386 set_perms(fname,file,NULL,0);
388 rprintf(FINFO, "%s -> %s\n", safe_fname(fname),
389 safe_fname(file->u.link));
396 if (am_root && preserve_devices && IS_DEVICE(file->mode)) {
398 st.st_mode != file->mode ||
399 st.st_rdev != file->u.rdev) {
402 rprintf(FINFO,"mknod(%s,0%o,0x%x)\n",
404 (int)file->mode, (int)file->u.rdev);
406 if (do_mknod(fname,file->mode,file->u.rdev) != 0) {
407 rsyserr(FERROR, errno, "mknod %s failed",
410 set_perms(fname,file,NULL,0);
412 rprintf(FINFO, "%s\n",
417 set_perms(fname, file, &st, PERMS_REPORT);
422 if (preserve_hard_links && hard_link_check(file, HL_CHECK_MASTER))
425 if (!S_ISREG(file->mode)) {
426 rprintf(FINFO, "skipping non-regular file \"%s\"\n",
432 fnamecmp_type = FNAMECMP_FNAME;
434 if (statret == -1 && basis_dir[0] != NULL) {
435 int fallback_match = -1;
439 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
440 basis_dir[i], fname);
441 if (link_stat(fnamecmpbuf, &st, 0) == 0
442 && S_ISREG(st.st_mode)) {
448 } else if (match_level == 2
449 && !unchanged_attrs(file, &st))
451 if (!unchanged_file(fnamecmpbuf, file, &st))
455 if (!unchanged_attrs(file, &st))
461 } while (basis_dir[++i] != NULL);
463 if (match_level < 3) {
465 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
466 basis_dir[i], fname);
469 if (link_dest && match_level == 3 && !dry_run) {
470 if (do_link(fnamecmpbuf, fname) < 0) {
472 rsyserr(FINFO, errno,
477 fnamecmp = fnamecmpbuf;
478 fnamecmp_type = FNAMECMP_BASIS_DIR + i;
483 fnamecmp = fnamecmpbuf;
484 fnamecmp_type = FNAMECMP_BASIS_DIR + i;
489 if (statret == 0 && !S_ISREG(st.st_mode)) {
490 if (delete_file(fname) != 0)
496 if (partial_dir && (partialptr = partial_dir_fname(fname))
497 && link_stat(partialptr, &partial_st, 0) == 0
498 && S_ISREG(partial_st.st_mode)) {
500 goto prepare_to_open;
505 if (preserve_hard_links && hard_link_check(file, HL_SKIP))
507 if (stat_errno == ENOENT)
510 rsyserr(FERROR, stat_errno,
511 "recv_generator: failed to stat %s",
517 if (opt_ignore_existing && fnamecmp_type == FNAMECMP_FNAME) {
519 rprintf(FINFO, "%s exists\n", safe_fname(fname));
523 if (update_only && fnamecmp_type == FNAMECMP_FNAME
524 && cmp_modtime(st.st_mtime, file->modtime) > 0) {
526 rprintf(FINFO, "%s is newer\n", safe_fname(fname));
530 if ((link_dest || copy_dest) && fnamecmp_type != FNAMECMP_FNAME)
532 else if (unchanged_file(fnamecmp, file, &st)) {
533 if (fnamecmp_type == FNAMECMP_FNAME)
534 set_perms(fname, file, &st, PERMS_REPORT);
539 if (dry_run || whole_file > 0) {
548 fnamecmp = partialptr;
549 fnamecmp_type = FNAMECMP_PARTIAL_DIR;
553 fd = do_open(fnamecmp, O_RDONLY, 0);
556 rsyserr(FERROR, errno, "failed to open %s, continuing",
557 full_fname(fnamecmp));
559 /* pretend the file didn't exist */
560 if (preserve_hard_links && hard_link_check(file, HL_SKIP))
566 if (inplace && make_backups) {
567 if (!(backupptr = get_backup_name(fname))) {
571 if (!(back_file = make_file(fname, NULL, NO_EXCLUDES))) {
573 goto pretend_missing;
575 if (robust_unlink(backupptr) && errno != ENOENT) {
576 rsyserr(FERROR, errno, "unlink %s",
577 full_fname(backupptr));
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));
590 fnamecmp_type = FNAMECMP_BACKUP;
594 rprintf(FINFO, "gen mapped %s of size %.0f\n",
595 safe_fname(fnamecmp), (double)st.st_size);
599 rprintf(FINFO, "generating and sending sums for %d\n", i);
604 write_byte(f_out_name, fnamecmp_type);
606 if (dry_run || read_batch)
610 generate_and_send_sums(fd, st.st_size, f_out, f_copy);
614 set_perms(backupptr, back_file, NULL, 0);
616 rprintf(FINFO, "backed up %s to %s\n",
624 write_sum_head(f_out, NULL);
628 void generate_files(int f_out, struct file_list *flist, char *local_name,
633 char fbuf[MAXPATHLEN];
636 rprintf(FINFO, "generator starting pid=%ld count=%d\n",
637 (long)getpid(), flist->count);
643 ? "delta-transmission disabled for local transfer or --whole-file\n"
644 : "delta transmission enabled\n");
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
652 for (i = 0; i < flist->count; i++) {
653 struct file_struct *file = flist->files[i];
654 struct file_struct copy;
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)) {
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 */
669 recv_generator(local_name ? local_name : f_name_to(file, fbuf),
670 file, i, f_out, f_out_name);
674 csum_length = SUM_LENGTH;
678 rprintf(FINFO,"generate_files phase=%d\n",phase);
680 write_int(f_out, -1);
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);
692 rprintf(FINFO,"generate_files phase=%d\n",phase);
694 write_int(f_out, -1);
696 if (preserve_hard_links)
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))
705 recv_generator(local_name ? local_name : f_name(file),
710 rprintf(FINFO,"generate_files finished\n");