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 *compare_dest;
51 extern int whole_file;
52 extern int local_server;
54 extern int read_batch;
55 extern int only_existing;
56 extern int orig_umask;
57 extern int safe_symlinks;
58 extern unsigned int block_size;
60 extern struct exclude_list_struct server_exclude_list;
63 /* choose whether to skip a particular file */
64 static int skip_file(char *fname, struct file_struct *file, STRUCT_STAT *st)
66 if (st->st_size != file->length)
70 && (st->st_mode & CHMOD_BITS) != (file->mode & CHMOD_BITS))
73 if (am_root && preserve_uid && st->st_uid != file->uid)
76 if (preserve_gid && file->gid != GID_NONE
77 && st->st_gid != file->gid)
81 /* if always checksum is set then we use the checksum instead
82 of the file time to determine whether to sync */
83 if (always_checksum && S_ISREG(st->st_mode)) {
84 char sum[MD4_SUM_LENGTH];
85 file_checksum(fname,sum,st->st_size);
86 return memcmp(sum, file->u.sum, protocol_version < 21 ? 2
87 : MD4_SUM_LENGTH) == 0;
96 return cmp_modtime(st->st_mtime, file->modtime) == 0;
101 * NULL sum_struct means we have no checksums
103 void write_sum_head(int f, struct sum_struct *sum)
105 static struct sum_struct null_sum;
110 write_int(f, sum->count);
111 write_int(f, sum->blength);
112 if (protocol_version >= 27)
113 write_int(f, sum->s2length);
114 write_int(f, sum->remainder);
118 * set (initialize) the size entries in the per-file sum_struct
119 * calculating dynamic block and checksum sizes.
121 * This is only called from generate_and_send_sums() but is a separate
122 * function to encapsulate the logic.
124 * The block size is a rounded square root of file length.
126 * The checksum size is determined according to:
127 * blocksum_bits = BLOCKSUM_EXP + 2*log2(file_len) - log2(block_len)
128 * provided by Donovan Baarda which gives a probability of rsync
129 * algorithm corrupting data and falling back using the whole md4
132 * This might be made one of several selectable heuristics.
135 static void sum_sizes_sqroot(struct sum_struct *sum, uint64 len)
137 unsigned int blength;
143 blength = block_size;
144 } else if (len <= BLOCK_SIZE * BLOCK_SIZE) {
145 blength = BLOCK_SIZE;
155 if (len < (uint64)blength * blength)
158 } while (c >= 8); /* round to multiple of 8 */
159 blength = MAX(blength, BLOCK_SIZE);
162 if (protocol_version < 27) {
163 s2length = csum_length;
164 } else if (csum_length == SUM_LENGTH) {
165 s2length = SUM_LENGTH;
167 int b = BLOCKSUM_BIAS;
173 while (c >>= 1 && b) {
176 s2length = (b + 1 - 32 + 7) / 8; /* add a bit,
179 * --optimize in compiler--
181 s2length = MAX(s2length, csum_length);
182 s2length = MIN(s2length, SUM_LENGTH);
186 sum->blength = blength;
187 sum->s2length = s2length;
188 sum->count = (len + (blength - 1)) / blength;
189 sum->remainder = (len % blength);
191 if (sum->count && verbose > 2) {
192 rprintf(FINFO, "count=%.0f rem=%u blength=%u s2length=%d flength=%.0f\n",
193 (double)sum->count, sum->remainder, sum->blength,
194 sum->s2length, (double)sum->flength);
200 * Generate and send a stream of signatures/checksums that describe a buffer
202 * Generate approximately one checksum every block_len bytes.
204 static void generate_and_send_sums(int fd, OFF_T len, int f_out, int f_copy)
207 struct map_struct *mapbuf;
208 struct sum_struct sum;
211 sum_sizes_sqroot(&sum, len);
214 mapbuf = map_file(fd, len, MAX_MAP_SIZE, sum.blength);
218 write_sum_head(f_out, &sum);
220 for (i = 0; i < sum.count; i++) {
221 unsigned int n1 = MIN(len, sum.blength);
222 char *map = map_ptr(mapbuf, offset, n1);
223 uint32 sum1 = get_checksum1(map, n1);
224 char sum2[SUM_LENGTH];
227 full_write(f_copy, map, n1);
229 get_checksum2(map, n1, sum2);
233 "chunk[%.0f] offset=%.0f len=%u sum1=%08lx\n",
234 (double)i, (double)offset, n1,
235 (unsigned long)sum1);
237 write_int(f_out, sum1);
238 write_buf(f_out, sum2, sum.s2length);
250 * Acts on file number @p i from @p flist, whose name is @p fname.
252 * First fixes up permissions, then generates checksums for the file.
254 * @note This comment was added later by mbp who was trying to work it
255 * out. It might be wrong.
257 static void recv_generator(char *fname, struct file_struct *file, int i,
258 int f_out, int f_out_name)
260 int fd = -1, f_copy = -1;
261 STRUCT_STAT st, partial_st;
262 struct file_struct *back_file = NULL;
263 int statret, stat_errno;
264 char *fnamecmp, *partialptr, *backupptr = NULL;
265 char fnamecmpbuf[MAXPATHLEN];
272 rprintf(FINFO, "recv_generator(%s,%d)\n", safe_fname(fname), i);
274 if (server_exclude_list.head
275 && check_exclude(&server_exclude_list, fname,
276 S_ISDIR(file->mode)) < 0) {
278 rprintf(FINFO, "skipping server-excluded file \"%s\"\n",
288 statret = link_stat(fname, &st,
289 keep_dirlinks && S_ISDIR(file->mode));
293 if (only_existing && statret == -1 && stat_errno == ENOENT) {
294 /* we only want to update existing files */
296 rprintf(FINFO, "not creating new file \"%s\"\n",
302 if (statret == 0 && !preserve_perms
303 && S_ISDIR(st.st_mode) == S_ISDIR(file->mode)) {
304 /* if the file exists already and we aren't perserving
305 * permissions then act as though the remote end sent
306 * us the file permissions we already have */
307 file->mode = (file->mode & ~CHMOD_BITS)
308 | (st.st_mode & CHMOD_BITS);
311 if (S_ISDIR(file->mode)) {
312 /* The file to be received is a directory, so we need
313 * to prepare appropriately. If there is already a
314 * file of that name and it is *not* a directory, then
315 * we need to delete it. If it doesn't exist, then
316 * recursively create it. */
319 return; /* TODO: causes inaccuracies -- fix */
320 if (statret == 0 && !S_ISDIR(st.st_mode)) {
321 if (robust_unlink(fname) != 0) {
322 rsyserr(FERROR, errno,
323 "recv_generator: unlink %s to make room for directory",
329 if (statret != 0 && do_mkdir(fname,file->mode) != 0 && errno != EEXIST) {
330 if (!(relative_paths && errno == ENOENT
331 && create_directory_path(fname, orig_umask) == 0
332 && do_mkdir(fname, file->mode) == 0)) {
333 rsyserr(FERROR, errno,
334 "recv_generator: mkdir %s failed",
338 /* f_out is set to -1 when doing final directory-permission
339 * and modification-time repair. */
340 if (set_perms(fname, file, statret ? NULL : &st, 0)
341 && verbose && f_out != -1)
342 rprintf(FINFO, "%s/\n", safe_fname(fname));
344 } else if (max_size && file->length > max_size) {
346 rprintf(FINFO, "%s is over max-size\n", fname);
350 if (preserve_links && S_ISLNK(file->mode)) {
352 char lnk[MAXPATHLEN];
355 if (safe_symlinks && unsafe_symlink(file->u.link, fname)) {
357 rprintf(FINFO, "ignoring unsafe symlink %s -> \"%s\"\n",
358 full_fname(fname), file->u.link);
363 l = readlink(fname,lnk,MAXPATHLEN-1);
366 /* A link already pointing to the
367 * right place -- no further action
369 if (strcmp(lnk,file->u.link) == 0) {
370 set_perms(fname, file, &st,
375 /* Not a symlink, so delete whatever's
376 * already there and put a new symlink
380 if (do_symlink(file->u.link,fname) != 0) {
381 rsyserr(FERROR, errno, "symlink %s -> \"%s\" failed",
382 full_fname(fname), safe_fname(file->u.link));
384 set_perms(fname,file,NULL,0);
386 rprintf(FINFO, "%s -> %s\n", safe_fname(fname),
387 safe_fname(file->u.link));
394 if (am_root && preserve_devices && IS_DEVICE(file->mode)) {
396 st.st_mode != file->mode ||
397 st.st_rdev != file->u.rdev) {
400 rprintf(FINFO,"mknod(%s,0%o,0x%x)\n",
402 (int)file->mode, (int)file->u.rdev);
404 if (do_mknod(fname,file->mode,file->u.rdev) != 0) {
405 rsyserr(FERROR, errno, "mknod %s failed",
408 set_perms(fname,file,NULL,0);
410 rprintf(FINFO, "%s\n",
415 set_perms(fname, file, &st, PERMS_REPORT);
420 if (preserve_hard_links && hard_link_check(file, HL_CHECK_MASTER))
423 if (!S_ISREG(file->mode)) {
424 rprintf(FINFO, "skipping non-regular file \"%s\"\n",
430 fnamecmp_type = FNAMECMP_FNAME;
432 if (statret == -1 && compare_dest != NULL) {
433 /* try the file at compare_dest instead */
434 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf, compare_dest, fname);
435 if (link_stat(fnamecmpbuf, &st, 0) == 0
436 && S_ISREG(st.st_mode)) {
438 if (link_dest && !dry_run) {
439 if (do_link(fnamecmpbuf, fname) < 0) {
441 rsyserr(FINFO, errno,
446 fnamecmp = fnamecmpbuf;
447 fnamecmp_type = FNAMECMP_CMPDEST;
452 fnamecmp = fnamecmpbuf;
453 fnamecmp_type = FNAMECMP_CMPDEST;
459 if (statret == 0 && !S_ISREG(st.st_mode)) {
460 if (delete_file(fname) != 0)
466 if (partial_dir && (partialptr = partial_dir_fname(fname))
467 && link_stat(partialptr, &partial_st, 0) == 0
468 && S_ISREG(partial_st.st_mode)) {
470 goto prepare_to_open;
475 if (preserve_hard_links && hard_link_check(file, HL_SKIP))
477 if (stat_errno == ENOENT)
480 rsyserr(FERROR, stat_errno,
481 "recv_generator: failed to stat %s",
487 if (opt_ignore_existing && fnamecmp_type == FNAMECMP_FNAME) {
489 rprintf(FINFO, "%s exists\n", safe_fname(fname));
493 if (update_only && fnamecmp_type == FNAMECMP_FNAME
494 && cmp_modtime(st.st_mtime, file->modtime) > 0) {
496 rprintf(FINFO, "%s is newer\n", safe_fname(fname));
500 if (skip_file(fnamecmp, file, &st)) {
501 if (fnamecmp_type == FNAMECMP_FNAME)
502 set_perms(fname, file, &st, PERMS_REPORT);
507 if (dry_run || whole_file > 0) {
516 fnamecmp = partialptr;
517 fnamecmp_type = FNAMECMP_PARTIAL_DIR;
521 fd = do_open(fnamecmp, O_RDONLY, 0);
524 rsyserr(FERROR, errno, "failed to open %s, continuing",
525 full_fname(fnamecmp));
527 /* pretend the file didn't exist */
528 if (preserve_hard_links && hard_link_check(file, HL_SKIP))
534 if (inplace && make_backups) {
535 if (!(backupptr = get_backup_name(fname))) {
539 if (!(back_file = make_file(fname, NULL, NO_EXCLUDES))) {
541 goto pretend_missing;
543 if (robust_unlink(backupptr) && errno != ENOENT) {
544 rsyserr(FERROR, errno, "unlink %s",
545 full_fname(backupptr));
550 if ((f_copy = do_open(backupptr,
551 O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600)) < 0) {
552 rsyserr(FERROR, errno, "open %s",
553 full_fname(backupptr));
558 fnamecmp_type = FNAMECMP_BACKUP;
562 rprintf(FINFO, "gen mapped %s of size %.0f\n",
563 safe_fname(fnamecmp), (double)st.st_size);
567 rprintf(FINFO, "generating and sending sums for %d\n", i);
572 write_byte(f_out_name, fnamecmp_type);
574 if (dry_run || read_batch)
578 generate_and_send_sums(fd, st.st_size, f_out, f_copy);
582 set_perms(backupptr, back_file, NULL, 0);
584 rprintf(FINFO, "backed up %s to %s\n",
592 write_sum_head(f_out, NULL);
596 void generate_files(int f_out, struct file_list *flist, char *local_name,
601 char fbuf[MAXPATHLEN];
604 rprintf(FINFO, "generator starting pid=%ld count=%d\n",
605 (long)getpid(), flist->count);
611 ? "delta-transmission disabled for local transfer or --whole-file\n"
612 : "delta transmission enabled\n");
615 /* we expect to just sit around now, so don't exit on a
616 timeout. If we really get a timeout then the other process should
620 for (i = 0; i < flist->count; i++) {
621 struct file_struct *file = flist->files[i];
622 struct file_struct copy;
626 /* we need to ensure that any directories we create have writeable
627 permissions initially so that we can create the files within
628 them. This is then fixed after the files are transferred */
629 if (!am_root && S_ISDIR(file->mode) && !(file->mode & S_IWUSR)) {
631 /* XXX: Could this be causing a problem on SCO? Perhaps their
632 * handling of permissions is strange? */
633 copy.mode |= S_IWUSR; /* user write */
637 recv_generator(local_name ? local_name : f_name_to(file, fbuf),
638 file, i, f_out, f_out_name);
642 csum_length = SUM_LENGTH;
646 rprintf(FINFO,"generate_files phase=%d\n",phase);
648 write_int(f_out, -1);
650 /* files can cycle through the system more than once
651 * to catch initial checksum errors */
652 while ((i = get_redo_num()) != -1) {
653 struct file_struct *file = flist->files[i];
654 recv_generator(local_name ? local_name : f_name_to(file, fbuf),
655 file, i, f_out, f_out_name);
660 rprintf(FINFO,"generate_files phase=%d\n",phase);
662 write_int(f_out, -1);
664 if (preserve_hard_links)
667 /* now we need to fix any directory permissions that were
668 * modified during the transfer */
669 for (i = 0; i < flist->count; i++) {
670 struct file_struct *file = flist->files[i];
671 if (!file->basename || !S_ISDIR(file->mode))
673 recv_generator(local_name ? local_name : f_name(file),
678 rprintf(FINFO,"generate_files finished\n");