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[];
50 extern int compare_dest;
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 long block_size; /* "long" because popt can't set an int32. */
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)
143 blength = block_size;
144 else if (len <= BLOCK_SIZE * BLOCK_SIZE)
145 blength = BLOCK_SIZE;
150 for (c = 1, l = len, cnt = 0; l >>= 2; c <<= 1, cnt++) {}
151 if (cnt >= 31 || c >= MAX_BLOCK_SIZE)
152 blength = MAX_BLOCK_SIZE;
157 if (len < (uint64)blength * blength)
160 } while (c >= 8); /* round to multiple of 8 */
161 blength = MAX(blength, BLOCK_SIZE);
165 if (protocol_version < 27) {
166 s2length = csum_length;
167 } else if (csum_length == SUM_LENGTH) {
168 s2length = SUM_LENGTH;
172 int b = BLOCKSUM_BIAS;
173 for (l = len; l >>= 1; b += 2) {}
174 for (c = blength; c >>= 1 && b; b--) {}
175 /* add a bit, subtract rollsum, round up. */
176 s2length = (b + 1 - 32 + 7) / 8; /* --optimize in compiler-- */
177 s2length = MAX(s2length, csum_length);
178 s2length = MIN(s2length, SUM_LENGTH);
182 sum->blength = blength;
183 sum->s2length = s2length;
184 sum->count = (len + (blength - 1)) / blength;
185 sum->remainder = (len % blength);
187 if (sum->count && verbose > 2) {
189 "count=%.0f rem=%ld blength=%ld s2length=%d flength=%.0f\n",
190 (double)sum->count, (long)sum->remainder, (long)sum->blength,
191 sum->s2length, (double)sum->flength);
197 * Generate and send a stream of signatures/checksums that describe a buffer
199 * Generate approximately one checksum every block_len bytes.
201 static void generate_and_send_sums(int fd, OFF_T len, int f_out, int f_copy)
204 struct map_struct *mapbuf;
205 struct sum_struct sum;
208 sum_sizes_sqroot(&sum, len);
211 mapbuf = map_file(fd, len, MAX_MAP_SIZE, sum.blength);
215 write_sum_head(f_out, &sum);
217 for (i = 0; i < sum.count; i++) {
218 int32 n1 = (int32)MIN(len, (OFF_T)sum.blength);
219 char *map = map_ptr(mapbuf, offset, n1);
220 uint32 sum1 = get_checksum1(map, n1);
221 char sum2[SUM_LENGTH];
224 full_write(f_copy, map, n1);
226 get_checksum2(map, n1, sum2);
230 "chunk[%.0f] offset=%.0f len=%ld sum1=%08lx\n",
231 (double)i, (double)offset, (long)n1,
232 (unsigned long)sum1);
234 write_int(f_out, sum1);
235 write_buf(f_out, sum2, sum.s2length);
247 * Acts on file number @p i from @p flist, whose name is @p fname.
249 * First fixes up permissions, then generates checksums for the file.
251 * @note This comment was added later by mbp who was trying to work it
252 * out. It might be wrong.
254 static void recv_generator(char *fname, struct file_struct *file, int i,
255 int f_out, int f_out_name)
257 int fd = -1, f_copy = -1;
258 STRUCT_STAT st, partial_st;
259 struct file_struct *back_file = NULL;
260 int statret, stat_errno;
261 char *fnamecmp, *partialptr, *backupptr = NULL;
262 char fnamecmpbuf[MAXPATHLEN];
269 rprintf(FINFO, "recv_generator(%s,%d)\n", safe_fname(fname), i);
271 if (server_exclude_list.head
272 && check_exclude(&server_exclude_list, fname,
273 S_ISDIR(file->mode)) < 0) {
275 rprintf(FINFO, "skipping server-excluded file \"%s\"\n",
285 statret = link_stat(fname, &st,
286 keep_dirlinks && S_ISDIR(file->mode));
290 if (only_existing && statret == -1 && stat_errno == ENOENT) {
291 /* we only want to update existing files */
293 rprintf(FINFO, "not creating new file \"%s\"\n",
299 if (statret == 0 && !preserve_perms
300 && S_ISDIR(st.st_mode) == S_ISDIR(file->mode)) {
301 /* if the file exists already and we aren't perserving
302 * permissions then act as though the remote end sent
303 * us the file permissions we already have */
304 file->mode = (file->mode & ~CHMOD_BITS)
305 | (st.st_mode & CHMOD_BITS);
308 if (S_ISDIR(file->mode)) {
309 /* The file to be received is a directory, so we need
310 * to prepare appropriately. If there is already a
311 * file of that name and it is *not* a directory, then
312 * we need to delete it. If it doesn't exist, then
313 * recursively create it. */
316 return; /* TODO: causes inaccuracies -- fix */
317 if (statret == 0 && !S_ISDIR(st.st_mode)) {
318 if (robust_unlink(fname) != 0) {
319 rsyserr(FERROR, errno,
320 "recv_generator: unlink %s to make room for directory",
326 if (statret != 0 && do_mkdir(fname,file->mode) != 0 && errno != EEXIST) {
327 if (!(relative_paths && errno == ENOENT
328 && create_directory_path(fname, orig_umask) == 0
329 && do_mkdir(fname, file->mode) == 0)) {
330 rsyserr(FERROR, errno,
331 "recv_generator: mkdir %s failed",
335 /* f_out is set to -1 when doing final directory-permission
336 * and modification-time repair. */
337 if (set_perms(fname, file, statret ? NULL : &st, 0)
338 && verbose && f_out != -1)
339 rprintf(FINFO, "%s/\n", safe_fname(fname));
341 } else if (max_size && file->length > max_size) {
343 rprintf(FINFO, "%s is over max-size\n", fname);
347 if (preserve_links && S_ISLNK(file->mode)) {
349 char lnk[MAXPATHLEN];
352 if (safe_symlinks && unsafe_symlink(file->u.link, fname)) {
354 rprintf(FINFO, "ignoring unsafe symlink %s -> \"%s\"\n",
355 full_fname(fname), file->u.link);
360 l = readlink(fname,lnk,MAXPATHLEN-1);
363 /* A link already pointing to the
364 * right place -- no further action
366 if (strcmp(lnk,file->u.link) == 0) {
367 set_perms(fname, file, &st,
372 /* Not a symlink, so delete whatever's
373 * already there and put a new symlink
377 if (do_symlink(file->u.link,fname) != 0) {
378 rsyserr(FERROR, errno, "symlink %s -> \"%s\" failed",
379 full_fname(fname), safe_fname(file->u.link));
381 set_perms(fname,file,NULL,0);
383 rprintf(FINFO, "%s -> %s\n", safe_fname(fname),
384 safe_fname(file->u.link));
391 if (am_root && preserve_devices && IS_DEVICE(file->mode)) {
393 st.st_mode != file->mode ||
394 st.st_rdev != file->u.rdev) {
397 rprintf(FINFO,"mknod(%s,0%o,0x%x)\n",
399 (int)file->mode, (int)file->u.rdev);
401 if (do_mknod(fname,file->mode,file->u.rdev) != 0) {
402 rsyserr(FERROR, errno, "mknod %s failed",
405 set_perms(fname,file,NULL,0);
407 rprintf(FINFO, "%s\n",
412 set_perms(fname, file, &st, PERMS_REPORT);
417 if (preserve_hard_links && hard_link_check(file, HL_CHECK_MASTER))
420 if (!S_ISREG(file->mode)) {
421 rprintf(FINFO, "skipping non-regular file \"%s\"\n",
427 fnamecmp_type = FNAMECMP_FNAME;
429 if (statret == -1 && basis_dir[0] != NULL) {
430 int fallback_match = -1;
434 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
435 basis_dir[i], fname);
436 if (link_stat(fnamecmpbuf, &st, 0) == 0
437 && S_ISREG(st.st_mode)) {
443 } else if (match_level == 2
444 && !unchanged_attrs(file, &st))
446 if (!unchanged_file(fnamecmpbuf, file, &st))
450 if (!unchanged_attrs(file, &st))
456 } while (basis_dir[++i] != NULL);
458 if (match_level < 3) {
460 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
461 basis_dir[i], fname);
464 if (link_dest && match_level == 3 && !dry_run) {
465 if (do_link(fnamecmpbuf, fname) < 0) {
467 rsyserr(FINFO, errno,
472 fnamecmp = fnamecmpbuf;
478 fnamecmp = fnamecmpbuf;
484 if (statret == 0 && !S_ISREG(st.st_mode)) {
485 if (delete_file(fname) != 0)
491 if (partial_dir && (partialptr = partial_dir_fname(fname)) != NULL
492 && link_stat(partialptr, &partial_st, 0) == 0
493 && S_ISREG(partial_st.st_mode)) {
495 goto prepare_to_open;
500 if (preserve_hard_links && hard_link_check(file, HL_SKIP))
502 if (stat_errno == ENOENT)
505 rsyserr(FERROR, stat_errno,
506 "recv_generator: failed to stat %s",
512 if (opt_ignore_existing && fnamecmp_type == FNAMECMP_FNAME) {
514 rprintf(FINFO, "%s exists\n", safe_fname(fname));
518 if (update_only && fnamecmp_type == FNAMECMP_FNAME
519 && cmp_modtime(st.st_mtime, file->modtime) > 0) {
521 rprintf(FINFO, "%s is newer\n", safe_fname(fname));
525 if (!compare_dest && fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
527 else if (unchanged_file(fnamecmp, file, &st)) {
528 if (fnamecmp_type == FNAMECMP_FNAME)
529 set_perms(fname, file, &st, PERMS_REPORT);
536 fnamecmp = partialptr;
537 fnamecmp_type = FNAMECMP_PARTIAL_DIR;
541 if (dry_run || whole_file > 0) {
549 fd = do_open(fnamecmp, O_RDONLY, 0);
552 rsyserr(FERROR, errno, "failed to open %s, continuing",
553 full_fname(fnamecmp));
555 /* pretend the file didn't exist */
556 if (preserve_hard_links && hard_link_check(file, HL_SKIP))
562 if (inplace && make_backups) {
563 if (!(backupptr = get_backup_name(fname))) {
567 if (!(back_file = make_file(fname, NULL, NO_EXCLUDES))) {
569 goto pretend_missing;
571 if (robust_unlink(backupptr) && errno != ENOENT) {
572 rsyserr(FERROR, errno, "unlink %s",
573 full_fname(backupptr));
578 if ((f_copy = do_open(backupptr,
579 O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0) {
580 rsyserr(FERROR, errno, "open %s",
581 full_fname(backupptr));
586 fnamecmp_type = FNAMECMP_BACKUP;
590 rprintf(FINFO, "gen mapped %s of size %.0f\n",
591 safe_fname(fnamecmp), (double)st.st_size);
595 rprintf(FINFO, "generating and sending sums for %d\n", i);
600 write_byte(f_out_name, fnamecmp_type);
602 if (dry_run || read_batch)
606 generate_and_send_sums(fd, st.st_size, f_out, f_copy);
610 set_perms(backupptr, back_file, NULL, 0);
612 rprintf(FINFO, "backed up %s to %s\n",
620 write_sum_head(f_out, NULL);
624 void generate_files(int f_out, struct file_list *flist, char *local_name,
629 char fbuf[MAXPATHLEN];
632 rprintf(FINFO, "generator starting pid=%ld count=%d\n",
633 (long)getpid(), flist->count);
639 ? "delta-transmission disabled for local transfer or --whole-file\n"
640 : "delta transmission enabled\n");
643 /* we expect to just sit around now, so don't exit on a
644 timeout. If we really get a timeout then the other process should
648 for (i = 0; i < flist->count; i++) {
649 struct file_struct *file = flist->files[i];
650 struct file_struct copy;
654 /* we need to ensure that any directories we create have writeable
655 permissions initially so that we can create the files within
656 them. This is then fixed after the files are transferred */
657 if (!am_root && S_ISDIR(file->mode) && !(file->mode & S_IWUSR)) {
659 /* XXX: Could this be causing a problem on SCO? Perhaps their
660 * handling of permissions is strange? */
661 copy.mode |= S_IWUSR; /* user write */
665 recv_generator(local_name ? local_name : f_name_to(file, fbuf),
666 file, i, f_out, f_out_name);
670 csum_length = SUM_LENGTH;
674 rprintf(FINFO,"generate_files phase=%d\n",phase);
676 write_int(f_out, -1);
678 /* files can cycle through the system more than once
679 * to catch initial checksum errors */
680 while ((i = get_redo_num()) != -1) {
681 struct file_struct *file = flist->files[i];
682 recv_generator(local_name ? local_name : f_name_to(file, fbuf),
683 file, i, f_out, f_out_name);
688 rprintf(FINFO,"generate_files phase=%d\n",phase);
690 write_int(f_out, -1);
692 /* Read post-redo-phase MSG_DONE and any prior messages. */
695 if (preserve_hard_links)
698 /* now we need to fix any directory permissions that were
699 * modified during the transfer */
700 for (i = 0; i < flist->count; i++) {
701 struct file_struct *file = flist->files[i];
702 if (!file->basename || !S_ISDIR(file->mode))
704 recv_generator(local_name ? local_name : f_name(file),
709 rprintf(FINFO,"generate_files finished\n");