1 Optimize the --checksum option using externally created .rsyncsums files.
3 This adds a new option, --sumfiles=MODE, that allows you to use a cache of
4 checksums when performing a --checksum transfer. These checksum files
5 (.rsyncsums) must be created by some other process -- see the perl script,
6 rsyncsums, in the support dir for one way.
8 This option can be particularly helpful to a public mirror that wants to
9 pre-compute their .rsyncsums files, set the "checksum files = strict" option
10 in their daemon config file, and thus make it quite efficient for a client
11 rsync to make use of the --checksum option on their server.
13 To use this patch, run these commands for a successful build:
15 patch -p1 <patches/checksum-reading.diff
16 ./configure (optional if already run)
19 diff --git a/checksum.c b/checksum.c
22 @@ -100,7 +100,7 @@ void get_checksum2(char *buf, int32 len, char *sum)
26 -void file_checksum(char *fname, char *sum, OFF_T size)
27 +void file_checksum(const char *fname, OFF_T size, char *sum)
29 struct map_struct *buf;
31 diff --git a/clientserver.c b/clientserver.c
34 @@ -39,6 +39,8 @@ extern int numeric_ids;
35 extern int filesfrom_fd;
36 extern int remote_protocol;
37 extern int protocol_version;
38 +extern int always_checksum;
39 +extern int checksum_files;
40 extern int io_timeout;
42 extern int write_batch;
43 @@ -750,6 +752,9 @@ static int rsync_module(int f_in, int f_out, int i, char *addr, char *host)
44 else if (am_root < 0) /* Treat --fake-super from client as --super. */
47 + checksum_files = always_checksum ? lp_checksum_files(i)
50 if (filesfrom_fd == 0)
53 diff --git a/flist.c b/flist.c
56 @@ -34,6 +34,7 @@ extern int am_generator;
57 extern int inc_recurse;
58 extern int do_progress;
59 extern int always_checksum;
60 +extern int basis_dir_cnt;
62 extern int ignore_errors;
63 extern int numeric_ids;
64 @@ -61,6 +62,7 @@ extern int file_extra_cnt;
65 extern int ignore_perishable;
66 extern int non_perishable_cnt;
67 extern int prune_empty_dirs;
68 +extern int checksum_files;
69 extern int copy_links;
70 extern int copy_unsafe_links;
71 extern int protocol_version;
72 @@ -68,6 +70,7 @@ extern int sanitize_paths;
73 extern int munge_symlinks;
74 extern int need_unsorted_flist;
75 extern int unsort_ndx;
76 +extern char *basis_dir[];
77 extern struct stats stats;
78 extern char *filesfrom_host;
80 @@ -83,6 +86,11 @@ extern int filesfrom_convert;
81 extern iconv_t ic_send, ic_recv;
84 +#define RSYNCSUMS_FILE ".rsyncsums"
86 +#define CLEAN_STRIP_ROOT (1<<0)
87 +#define CLEAN_KEEP_LAST (1<<1)
89 #define PTR_SIZE (sizeof (struct file_struct *))
92 @@ -124,7 +132,11 @@ static char empty_sum[MAX_DIGEST_LEN];
93 static int flist_count_offset; /* for --delete --progress */
94 static int dir_count = 0;
96 -static void flist_sort_and_clean(struct file_list *flist, int strip_root);
97 +static struct csum_cache {
98 + struct file_list *flist;
99 +} *csum_cache = NULL;
101 +static void flist_sort_and_clean(struct file_list *flist, int flags);
102 static void output_flist(struct file_list *flist);
104 void init_flist(void)
105 @@ -315,6 +327,238 @@ static void flist_done_allocating(struct file_list *flist)
106 flist->pool_boundary = ptr;
109 +void reset_checksum_cache()
111 + int slot, slots = am_sender ? 1 : basis_dir_cnt + 1;
114 + csum_cache = new_array0(struct csum_cache, slots);
116 + out_of_memory("reset_checksum_cache");
119 + for (slot = 0; slot < slots; slot++) {
120 + struct file_list *flist = csum_cache[slot].flist;
123 + /* Reset the pool memory and empty the file-list array. */
124 + pool_free_old(flist->file_pool,
125 + pool_boundary(flist->file_pool, 0));
128 + flist = csum_cache[slot].flist = flist_new(FLIST_TEMP, "reset_checksum_cache");
132 + flist->next = NULL;
137 +/* The basename_len count is the length of the basename + 1 for the null. */
138 +static int add_checksum(struct file_list *flist, const char *dirname,
139 + const char *basename, int basename_len, OFF_T file_length,
140 + time_t mtime, uint32 ctime, uint32 inode,
143 + struct file_struct *file;
144 + int alloc_len, extra_len;
147 + if (basename_len == 10+1 && *basename == '.' && strcmp(basename, RSYNCSUMS_FILE) == 0)
150 + /* "2" is for a 32-bit ctime num and an 32-bit inode num. */
151 + extra_len = (file_extra_cnt + (file_length > 0xFFFFFFFFu) + SUM_EXTRA_CNT + 2)
153 +#if EXTRA_ROUNDING > 0
154 + if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
155 + extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
157 + alloc_len = FILE_STRUCT_LEN + extra_len + basename_len;
158 + bp = pool_alloc(flist->file_pool, alloc_len, "add_checksum");
160 + memset(bp, 0, extra_len + FILE_STRUCT_LEN);
162 + file = (struct file_struct *)bp;
163 + bp += FILE_STRUCT_LEN;
165 + memcpy(bp, basename, basename_len);
167 + file->mode = S_IFREG;
168 + file->modtime = mtime;
169 + file->len32 = (uint32)file_length;
170 + if (file_length > 0xFFFFFFFFu) {
171 + file->flags |= FLAG_LENGTH64;
172 + OPT_EXTRA(file, 0)->unum = (uint32)(file_length >> 32);
174 + file->dirname = dirname;
175 + F_CTIME(file) = ctime;
176 + F_INODE(file) = inode;
178 + memcpy(bp, sum, checksum_len);
180 + flist_expand(flist, 1);
181 + flist->files[flist->used++] = file;
183 + flist->sorted = flist->files;
188 +/* The "dirname" arg's data must remain unchanged during the lifespan of
189 + * the created csum_cache[].flist object because we use it directly. */
190 +static void read_checksums(int slot, struct file_list *flist, const char *dirname)
192 + char line[MAXPATHLEN+1024], fbuf[MAXPATHLEN], sum[MAX_DIGEST_LEN];
198 + uint32 ctime, inode;
199 + int dlen = dirname ? strlcpy(fbuf, dirname, sizeof fbuf) : 0;
201 + if (dlen >= (int)(sizeof fbuf - sizeof RSYNCSUMS_FILE))
204 + fbuf[dlen++] = '/';
207 + strlcpy(fbuf+dlen, RSYNCSUMS_FILE, sizeof fbuf - dlen);
209 + pathjoin(line, sizeof line, basis_dir[slot-1], fbuf);
213 + if (!(fp = fopen(cp, "r")))
216 + while (fgets(line, sizeof line, fp)) {
218 + if (protocol_version >= 30) {
219 + char *alt_sum = cp;
221 + while (*++cp == '=') {}
223 + while (isXDigit(cp)) cp++;
224 + if (cp - alt_sum != MD4_DIGEST_LEN*2 || *cp != ' ')
226 + while (*++cp == ' ') {}
232 + for (i = 0; i < checksum_len*2; i++, cp++) {
234 + if (isXDigit(cp)) {
238 + x = (*cp & 0xF) + 9;
251 + while (*++cp == ' ') {}
253 + if (protocol_version < 30) {
254 + char *alt_sum = cp;
256 + while (*++cp == '=') {}
258 + while (isXDigit(cp)) cp++;
259 + if (cp - alt_sum != MD5_DIGEST_LEN*2 || *cp != ' ')
261 + while (*++cp == ' ') {}
265 + while (isDigit(cp))
266 + file_length = file_length * 10 + *cp++ - '0';
269 + while (*++cp == ' ') {}
272 + while (isDigit(cp))
273 + mtime = mtime * 10 + *cp++ - '0';
276 + while (*++cp == ' ') {}
279 + while (isDigit(cp))
280 + ctime = ctime * 10 + *cp++ - '0';
283 + while (*++cp == ' ') {}
286 + while (isDigit(cp))
287 + inode = inode * 10 + *cp++ - '0';
290 + while (*++cp == ' ') {}
293 + while (len && (cp[len-1] == '\n' || cp[len-1] == '\r'))
297 + cp[len++] = '\0'; /* len now counts the null */
298 + if (strchr(cp, '/'))
300 + if (len > MAXPATHLEN)
303 + strlcpy(fbuf+dlen, cp, sizeof fbuf - dlen);
305 + add_checksum(flist, dirname, cp, len, file_length,
306 + mtime, ctime, inode,
311 + flist_sort_and_clean(flist, CLEAN_KEEP_LAST);
314 +void get_cached_checksum(int slot, const char *fname, struct file_struct *file,
315 + STRUCT_STAT *stp, char *sum_buf)
317 + struct file_list *flist = csum_cache[slot].flist;
320 + if (!flist->next) {
321 + flist->next = cur_flist; /* next points from checksum flist to file flist */
322 + read_checksums(slot, flist, file->dirname);
325 + if ((j = flist_find(flist, file)) >= 0) {
326 + struct file_struct *fp = flist->sorted[j];
328 + if (F_LENGTH(fp) == stp->st_size
329 + && fp->modtime == stp->st_mtime
330 + && (checksum_files & CSF_LAX
331 + || (F_CTIME(fp) == (uint32)stp->st_ctime
332 + && F_INODE(fp) == (uint32)stp->st_ino))) {
333 + memcpy(sum_buf, F_SUM(fp), MAX_DIGEST_LEN);
338 + file_checksum(fname, stp->st_size, sum_buf);
341 int push_pathname(const char *dir, int len)
344 @@ -1005,7 +1249,7 @@ struct file_struct *make_file(const char *fname, struct file_list *flist,
345 STRUCT_STAT *stp, int flags, int filter_level)
347 static char *lastdir;
348 - static int lastdir_len = -1;
349 + static int lastdir_len = -2;
350 struct file_struct *file;
351 char thisname[MAXPATHLEN];
352 char linkname[MAXPATHLEN];
353 @@ -1138,9 +1382,16 @@ struct file_struct *make_file(const char *fname, struct file_list *flist,
354 memcpy(lastdir, thisname, len);
357 + if (checksum_files && am_sender && flist)
358 + reset_checksum_cache();
363 + if (checksum_files && am_sender && flist && lastdir_len == -2) {
365 + reset_checksum_cache();
368 basename_len = strlen(basename) + 1; /* count the '\0' */
371 @@ -1216,11 +1467,15 @@ struct file_struct *make_file(const char *fname, struct file_list *flist,
375 - if (always_checksum && am_sender && S_ISREG(st.st_mode))
376 - file_checksum(thisname, tmp_sum, st.st_size);
378 F_PATHNAME(file) = pathname;
380 + if (always_checksum && am_sender && S_ISREG(st.st_mode)) {
381 + if (flist && checksum_files)
382 + get_cached_checksum(0, thisname, file, &st, tmp_sum);
384 + file_checksum(thisname, st.st_size, tmp_sum);
387 /* This code is only used by the receiver when it is building
388 * a list of files for a delete pass. */
389 if (keep_dirlinks && linkname_len && flist) {
390 @@ -2076,7 +2331,8 @@ struct file_list *send_file_list(int f, int argc, char *argv[])
391 * file-list to check if this is a 1-file xfer. */
392 send_extra_file_list(f, 1);
400 @@ -2178,7 +2434,7 @@ struct file_list *recv_file_list(int f)
402 recv_id_list(f, flist);
404 - flist_sort_and_clean(flist, relative_paths);
405 + flist_sort_and_clean(flist, relative_paths ? CLEAN_STRIP_ROOT : 0);
407 if (protocol_version < 30) {
408 /* Recv the io_error flag */
409 @@ -2376,7 +2632,7 @@ void flist_free(struct file_list *flist)
411 /* This routine ensures we don't have any duplicate names in our file list.
412 * duplicate names can cause corruption because of the pipelining. */
413 -static void flist_sort_and_clean(struct file_list *flist, int strip_root)
414 +static void flist_sort_and_clean(struct file_list *flist, int flags)
416 char fbuf[MAXPATHLEN];
418 @@ -2427,7 +2683,7 @@ static void flist_sort_and_clean(struct file_list *flist, int strip_root)
419 /* If one is a dir and the other is not, we want to
420 * keep the dir because it might have contents in the
421 * list. Otherwise keep the first one. */
422 - if (S_ISDIR(file->mode)) {
423 + if (S_ISDIR(file->mode) || flags & CLEAN_KEEP_LAST) {
424 struct file_struct *fp = flist->sorted[j];
425 if (!S_ISDIR(fp->mode))
427 @@ -2443,8 +2699,8 @@ static void flist_sort_and_clean(struct file_list *flist, int strip_root)
433 + if (!am_sender || flags & CLEAN_KEEP_LAST) {
434 + if (verbose > 1 && !(flags & CLEAN_KEEP_LAST)) {
436 "removing duplicate name %s from file list (%d)\n",
437 f_name(file, fbuf), drop + flist->ndx_start);
438 @@ -2466,7 +2722,7 @@ static void flist_sort_and_clean(struct file_list *flist, int strip_root)
440 flist->high = prev_i;
443 + if (flags & CLEAN_STRIP_ROOT) {
444 /* We need to strip off the leading slashes for relative
445 * paths, but this must be done _after_ the sorting phase. */
446 for (i = flist->low; i <= flist->high; i++) {
447 diff --git a/generator.c b/generator.c
450 @@ -53,6 +53,7 @@ extern int delete_during;
451 extern int delete_after;
452 extern int msgdone_cnt;
453 extern int ignore_errors;
454 +extern int checksum_files;
455 extern int remove_source_files;
456 extern int delay_updates;
457 extern int update_only;
458 @@ -694,7 +695,7 @@ void itemize(const char *fnamecmp, struct file_struct *file, int ndx, int statre
461 /* Perform our quick-check heuristic for determining if a file is unchanged. */
462 -int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st)
463 +int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st, int slot)
465 if (st->st_size != F_LENGTH(file))
467 @@ -703,7 +704,10 @@ int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st)
468 of the file time to determine whether to sync */
469 if (always_checksum > 0 && S_ISREG(st->st_mode)) {
470 char sum[MAX_DIGEST_LEN];
471 - file_checksum(fn, sum, st->st_size);
472 + if (checksum_files && slot >= 0)
473 + get_cached_checksum(slot, fn, file, st, sum);
475 + file_checksum(fn, st->st_size, sum);
476 return memcmp(sum, F_SUM(file), checksum_len) == 0;
479 @@ -957,7 +961,7 @@ static int try_dests_reg(struct file_struct *file, char *fname, int ndx,
483 - if (!unchanged_file(cmpbuf, file, &sxp->st))
484 + if (!unchanged_file(cmpbuf, file, &sxp->st, j+1))
488 @@ -1219,7 +1223,7 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx,
489 static const char *parent_dirname = "";
490 static struct file_struct *missing_dir = NULL, *excluded_dir = NULL;
491 static struct file_list *fuzzy_dirlist = NULL;
492 - static int need_fuzzy_dirlist = 0;
493 + static int need_new_dirscan = 0;
494 struct file_struct *fuzzy_file = NULL;
495 int fd = -1, f_copy = -1;
497 @@ -1309,8 +1313,8 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx,
498 flist_free(fuzzy_dirlist);
499 fuzzy_dirlist = NULL;
502 - need_fuzzy_dirlist = 1;
503 + if (fuzzy_basis || checksum_files)
504 + need_new_dirscan = 1;
507 dflt_perms = default_perms_for_dir(dn);
508 @@ -1318,10 +1322,15 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx,
512 - if (need_fuzzy_dirlist && S_ISREG(file->mode)) {
513 - strlcpy(fnamecmpbuf, dn, sizeof fnamecmpbuf);
514 - fuzzy_dirlist = get_dirlist(fnamecmpbuf, -1, 1);
515 - need_fuzzy_dirlist = 0;
516 + if (need_new_dirscan && S_ISREG(file->mode)) {
518 + strlcpy(fnamecmpbuf, dn, sizeof fnamecmpbuf);
519 + fuzzy_dirlist = get_dirlist(fnamecmpbuf, -1, 1);
521 + if (checksum_files) {
522 + reset_checksum_cache();
524 + need_new_dirscan = 0;
527 statret = link_stat(fname, &sx.st, keep_dirlinks && is_dir);
528 @@ -1742,7 +1751,7 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx,
530 else if (fnamecmp_type == FNAMECMP_FUZZY)
532 - else if (unchanged_file(fnamecmp, file, &sx.st)) {
533 + else if (unchanged_file(fnamecmp, file, &sx.st, fnamecmp_type == FNAMECMP_FNAME ? 0 : -1)) {
535 do_unlink(partialptr);
536 handle_partial_dir(partialptr, PDIR_DELETE);
537 diff --git a/hlink.c b/hlink.c
540 @@ -351,7 +351,7 @@ int hard_link_check(struct file_struct *file, int ndx, const char *fname,
544 - if (!unchanged_file(cmpbuf, file, &alt_sx.st))
545 + if (!unchanged_file(cmpbuf, file, &alt_sx.st, j+1))
548 if (unchanged_attrs(cmpbuf, file, &alt_sx))
549 diff --git a/ifuncs.h b/ifuncs.h
552 @@ -64,6 +64,12 @@ isDigit(const char *ptr)
556 +isXDigit(const char *ptr)
558 + return isxdigit(*(unsigned char *)ptr);
562 isPrint(const char *ptr)
564 return isprint(*(unsigned char *)ptr);
565 diff --git a/loadparm.c b/loadparm.c
568 @@ -149,6 +149,7 @@ typedef struct
572 + int checksum_files;
576 @@ -200,6 +201,7 @@ static service sDefault =
577 /* temp_dir; */ NULL,
578 /* uid; */ NOBODY_USER,
580 + /* checksum_files; */ CSF_IGNORE_FILES,
581 /* max_connections; */ 0,
582 /* max_verbosity; */ 1,
583 /* syslog_facility; */ LOG_DAEMON,
584 @@ -294,6 +296,12 @@ static struct enum_list enum_facilities[] = {
588 +static struct enum_list enum_csum_modes[] = {
589 + { CSF_IGNORE_FILES, "none" },
590 + { CSF_LAX_MODE, "lax" },
591 + { CSF_STRICT_MODE, "strict" },
595 /* note that we do not initialise the defaults union - it is not allowed in ANSI C */
596 static struct parm_struct parm_table[] =
597 @@ -306,6 +314,7 @@ static struct parm_struct parm_table[] =
599 {"auth users", P_STRING, P_LOCAL, &sDefault.auth_users, NULL,0},
600 {"charset", P_STRING, P_LOCAL, &sDefault.charset, NULL,0},
601 + {"checksum files", P_ENUM, P_LOCAL, &sDefault.checksum_files, enum_csum_modes,0},
602 {"comment", P_STRING, P_LOCAL, &sDefault.comment, NULL,0},
603 {"dont compress", P_STRING, P_LOCAL, &sDefault.dont_compress, NULL,0},
604 {"exclude from", P_STRING, P_LOCAL, &sDefault.exclude_from, NULL,0},
605 @@ -423,6 +432,7 @@ FN_LOCAL_STRING(lp_secrets_file, secrets_file)
606 FN_LOCAL_STRING(lp_temp_dir, temp_dir)
607 FN_LOCAL_STRING(lp_uid, uid)
609 +FN_LOCAL_INTEGER(lp_checksum_files, checksum_files)
610 FN_LOCAL_INTEGER(lp_max_connections, max_connections)
611 FN_LOCAL_INTEGER(lp_max_verbosity, max_verbosity)
612 FN_LOCAL_INTEGER(lp_syslog_facility, syslog_facility)
613 diff --git a/options.c b/options.c
616 @@ -112,6 +112,7 @@ size_t bwlimit_writemax = 0;
617 int ignore_existing = 0;
618 int ignore_non_existing = 0;
619 int need_messages_from_generator = 0;
620 +int checksum_files = CSF_IGNORE_FILES;
621 int max_delete = INT_MIN;
624 @@ -316,6 +317,7 @@ void usage(enum logcode F)
625 rprintf(F," -q, --quiet suppress non-error messages\n");
626 rprintf(F," --no-motd suppress daemon-mode MOTD (see manpage caveat)\n");
627 rprintf(F," -c, --checksum skip based on checksum, not mod-time & size\n");
628 + rprintf(F," --sumfiles=MODE use .rsyncsums to speedup --checksum mode\n");
629 rprintf(F," -a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)\n");
630 rprintf(F," --no-OPTION turn off an implied OPTION (e.g. --no-D)\n");
631 rprintf(F," -r, --recursive recurse into directories\n");
632 @@ -445,7 +447,7 @@ enum {OPT_VERSION = 1000, OPT_DAEMON, OPT_SENDER, OPT_EXCLUDE, OPT_EXCLUDE_FROM,
633 OPT_FILTER, OPT_COMPARE_DEST, OPT_COPY_DEST, OPT_LINK_DEST, OPT_HELP,
634 OPT_INCLUDE, OPT_INCLUDE_FROM, OPT_MODIFY_WINDOW, OPT_MIN_SIZE, OPT_CHMOD,
635 OPT_READ_BATCH, OPT_WRITE_BATCH, OPT_ONLY_WRITE_BATCH, OPT_MAX_SIZE,
636 - OPT_NO_D, OPT_APPEND, OPT_NO_ICONV,
637 + OPT_NO_D, OPT_APPEND, OPT_NO_ICONV, OPT_SUMFILES,
638 OPT_SERVER, OPT_REFUSED_BASE = 9000};
640 static struct poptOption long_options[] = {
641 @@ -566,6 +568,7 @@ static struct poptOption long_options[] = {
642 {"checksum", 'c', POPT_ARG_VAL, &always_checksum, 1, 0, 0 },
643 {"no-checksum", 0, POPT_ARG_VAL, &always_checksum, 0, 0, 0 },
644 {"no-c", 0, POPT_ARG_VAL, &always_checksum, 0, 0, 0 },
645 + {"sumfiles", 0, POPT_ARG_STRING, 0, OPT_SUMFILES, 0, 0 },
646 {"block-size", 'B', POPT_ARG_LONG, &block_size, 0, 0, 0 },
647 {"compare-dest", 0, POPT_ARG_STRING, 0, OPT_COMPARE_DEST, 0, 0 },
648 {"copy-dest", 0, POPT_ARG_STRING, 0, OPT_COPY_DEST, 0, 0 },
649 @@ -1212,6 +1215,23 @@ int parse_arguments(int *argc_p, const char ***argv_p, int frommain)
654 + arg = poptGetOptArg(pc);
655 + checksum_files = 0;
656 + if (strcmp(arg, "lax") == 0)
657 + checksum_files |= CSF_LAX_MODE;
658 + else if (strcmp(arg, "strict") == 0)
659 + checksum_files |= CSF_STRICT_MODE;
660 + else if (strcmp(arg, "none") == 0)
661 + checksum_files = CSF_IGNORE_FILES;
663 + snprintf(err_buf, sizeof err_buf,
664 + "Invalid argument passed to --sumfiles (%s)\n",
673 @@ -1311,6 +1331,9 @@ int parse_arguments(int *argc_p, const char ***argv_p, int frommain)
677 + if (checksum_files && !always_checksum)
678 + checksum_files = CSF_IGNORE_FILES;
680 if (write_batch && read_batch) {
681 snprintf(err_buf, sizeof err_buf,
682 "--write-batch and --read-batch can not be used together\n");
683 diff --git a/rsync.h b/rsync.h
686 @@ -680,6 +680,10 @@ extern int xattrs_ndx;
687 #define F_SUM(f) ((char*)OPT_EXTRA(f, LEN64_BUMP(f) + HLINK_BUMP(f) \
688 + SUM_EXTRA_CNT - 1))
690 +/* These are only valid on an entry read from a checksum file. */
691 +#define F_CTIME(f) OPT_EXTRA(f, LEN64_BUMP(f) + SUM_EXTRA_CNT)->unum
692 +#define F_INODE(f) OPT_EXTRA(f, LEN64_BUMP(f) + SUM_EXTRA_CNT + 1)->unum
694 /* Some utility defines: */
695 #define F_IS_ACTIVE(f) (f)->basename[0]
696 #define F_IS_HLINKED(f) ((f)->flags & FLAG_HLINKED)
697 @@ -858,6 +862,13 @@ typedef struct {
698 char fname[1]; /* has variable size */
701 +#define CSF_ENABLE (1<<1)
702 +#define CSF_LAX (1<<2)
704 +#define CSF_IGNORE_FILES 0
705 +#define CSF_LAX_MODE (CSF_ENABLE|CSF_LAX)
706 +#define CSF_STRICT_MODE (CSF_ENABLE)
708 #include "byteorder.h"
709 #include "lib/mdigest.h"
710 #include "lib/wildmatch.h"
711 diff --git a/rsync.yo b/rsync.yo
714 @@ -317,6 +317,7 @@ to the detailed description below for a complete description. verb(
715 -q, --quiet suppress non-error messages
716 --no-motd suppress daemon-mode MOTD (see caveat)
717 -c, --checksum skip based on checksum, not mod-time & size
718 + --sumfiles=MODE use .rsyncsums to speedup --checksum mode
719 -a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)
720 --no-OPTION turn off an implied OPTION (e.g. --no-D)
721 -r, --recursive recurse into directories
722 @@ -516,9 +517,9 @@ uses a "quick check" that (by default) checks if each file's size and time
723 of last modification match between the sender and receiver. This option
724 changes this to compare a 128-bit MD4 checksum for each file that has a
725 matching size. Generating the checksums means that both sides will expend
726 -a lot of disk I/O reading all the data in the files in the transfer (and
727 -this is prior to any reading that will be done to transfer changed files),
728 -so this can slow things down significantly.
729 +a lot of disk I/O reading the data in all the files in the transfer, so
730 +this can slow things down significantly (and this is prior to any reading
731 +that will be done to transfer the files that have changed).
733 The sending side generates its checksums while it is doing the file-system
734 scan that builds the list of the available files. The receiver generates
735 @@ -526,12 +527,44 @@ its checksums when it is scanning for changed files, and will checksum any
736 file that has the same size as the corresponding sender's file: files with
737 either a changed size or a changed checksum are selected for transfer.
739 +See also the bf(--sumfiles) option for a way to use cached checksum data.
741 Note that rsync always verifies that each em(transferred) file was
742 correctly reconstructed on the receiving side by checking a whole-file
743 checksum that is generated as the file is transferred, but that
744 automatic after-the-transfer verification has nothing to do with this
745 option's before-the-transfer "Does this file need to be updated?" check.
747 +dit(bf(--sumfiles=MODE)) This option tells rsync to make use of any cached
748 +checksum information it finds in per-directory .rsyncsums files when the
749 +current transfer is using the bf(--checksum) option. If the checksum data
750 +is up-to-date, it is used instead of recomputing it, saving both disk I/O
751 +and CPU time. If the checksum data is missing or outdated, the checksum is
752 +computed just as it would be if bf(--sumfiles) was not specified.
754 +The MODE value is either "lax", for relaxed checking (which compares size
755 +and mtime), "strict" (which also compares ctime and inode), or "none" to
756 +ignore any .rsyncsums files ("none" is the default). Rsync does not create
757 +or update these files, but there is a perl script in the support directory
758 +named "rsyncsums" that can be used for that.
760 +This option has no effect unless bf(--checksum, -c) was also specified. It
761 +also only affects the current side of the transfer, so if you want the
762 +remote side to parse its own .rsyncsums files, specify the option via the
763 +bf(--rsync-path) option (e.g. "--rsync-path="rsync --sumfiles=lax").
765 +To avoid transferring the system's checksum files, you can use an exclude
766 +(e.g. bf(--exclude=.rsyncsums)). To make this easier to type, you can use
767 +a popt alias. For instance, adding the following line in your ~/.popt file
768 +defines a bf(--cc) option that enables lax checksum files and excludes the
771 +verb( rsync alias --cc -c --sumfiles=lax --exclude=.rsyncsums)
773 +An rsync daemon does not allow the client to control this setting, so see
774 +the "checksum files" daemon parameter for information on how to make a
775 +daemon use cached checksum data.
777 dit(bf(-a, --archive)) This is equivalent to bf(-rlptgoD). It is a quick
778 way of saying you want recursion and want to preserve almost
779 everything (with -H being a notable omission).
780 diff --git a/rsyncd.conf.yo b/rsyncd.conf.yo
783 @@ -281,6 +281,17 @@ locking on this file to ensure that the max connections limit is not
784 exceeded for the modules sharing the lock file.
785 The default is tt(/var/run/rsyncd.lock).
787 +dit(bf(checksum files)) This option tells rsync to make use of any cached
788 +checksum information it finds in per-directory .rsyncsums files when the
789 +current transfer is using the bf(--checksum) option. The value can be set
790 +to either "lax", "strict", or "none" -- see the client's bf(--sumfiles)
791 +option for what these choices do.
793 +Note also that the client's command-line option, bf(--sumfiles), has no
794 +effect on a daemon. A daemon will only access checksum files if this
795 +config option tells it to. See also the bf(exclude) directive for a way
796 +to hide the .rsyncsums files from the user.
798 dit(bf(read only)) The "read only" option determines whether clients
799 will be able to upload files or not. If "read only" is true then any
800 attempted uploads will fail. If "read only" is false then uploads will
801 diff --git a/support/rsyncsums b/support/rsyncsums
804 +++ b/support/rsyncsums
810 +use Cwd qw(abs_path cwd);
814 +our $SUMS_FILE = '.rsyncsums';
816 +&Getopt::Long::Configure('bundling');
817 +&usage if !&GetOptions(
818 + 'recurse|r' => \( my $recurse_opt ),
819 + 'mode|m=s' => \( my $cmp_mode = 'strict' ),
820 + 'check|c' => \( my $check_opt ),
821 + 'verbose|v+' => \( my $verbosity = 0 ),
822 + 'help|h' => \( my $help_opt ),
824 +&usage if $help_opt || $cmp_mode !~ /^(lax|strict)$/;
826 +my $ignore_ctime_and_inode = $cmp_mode eq 'lax' ? 0 : 1;
828 +my $start_dir = cwd();
831 +@dirs = '.' unless @dirs;
840 +my $md4 = Digest::MD4->new;
841 +my $md5 = Digest::MD5->new;
844 + my $dir = shift @dirs;
846 + if (!chdir($dir)) {
847 + warn "Unable to chdir to $dir: $!\n";
850 + if (!opendir(DP, '.')) {
851 + warn "Unable to opendir $dir: $!\n";
856 + $reldir =~ s#^$start_dir(/|$)# $1 ? '' : '.' #eo;
858 + print "$reldir ... ";
859 + print "\n" if $check_opt;
864 + if (open(FP, '<', $SUMS_FILE)) {
867 + my($sum4, $sum5, $size, $mtime, $ctime, $inode, $fn) = split(' ', $_, 7);
868 + $cache{$fn} = [ 0, $sum4, $sum5, $size, $mtime, $ctime & 0xFFFFFFFF, $inode & 0xFFFFFFFF ];
876 + my $update_cnt = 0;
877 + while (defined(my $fn = readdir(DP))) {
878 + next if $fn =~ /^\.\.?$/ || $fn =~ /^\Q$SUMS_FILE\E$/o || -l $fn;
880 + push(@subdirs, "$dir/$fn") unless $fn =~ /^(CVS|\.svn|\.git|\.bzr)$/;
885 + my($size,$mtime,$ctime,$inode) = (stat(_))[7,9,10,1];
886 + $ctime &= 0xFFFFFFFF;
887 + $inode &= 0xFFFFFFFF;
888 + my $ref = $cache{$fn};
892 + if (defined $ref) {
894 + if ($$ref[3] == $size
895 + && $$ref[4] == $mtime
896 + && ($ignore_ctime_and_inode || ($$ref[5] == $ctime && $$ref[6] == $inode))
897 + && $$ref[1] !~ /=/ && $$ref[2] !~ /=/) {
901 + if (!$update_cnt++) {
902 + print "UPDATING\n" if $verbosity;
906 + if (!open(IN, $fn)) {
907 + print STDERR "Unable to read $fn: $!\n";
908 + if (defined $ref) {
909 + delete $cache{$fn};
917 + while (sysread(IN, $_, 64*1024)) {
921 + $sum4 = $md4->hexdigest;
922 + $sum5 = $md5->hexdigest;
923 + print " $sum4 $sum5" if $verbosity > 2;
924 + print " $fn" if $verbosity > 1;
925 + my($size2,$mtime2,$ctime2,$inode2) = (stat(IN))[7,9,10,1];
926 + $ctime2 &= 0xFFFFFFFF;
927 + $inode2 &= 0xFFFFFFFF;
928 + last if $size == $size2 && $mtime == $mtime2
929 + && ($ignore_ctime_and_inode || ($ctime == $ctime2 && $inode == $inode2));
935 + print " REREADING\n" if $verbosity > 1;
942 + if (!defined $ref) {
944 + } elsif ($sum4 ne $$ref[1] || $sum5 ne $$ref[2]) {
947 + print " OK\n" if $verbosity > 1;
950 + if ($verbosity < 2) {
951 + print $verbosity ? ' ' : "$reldir/";
957 + print "\n" if $verbosity > 1;
958 + $cache{$fn} = [ 1, $sum4, $sum5, $size, $mtime, $ctime, $inode ];
964 + unshift(@dirs, sort @subdirs) if $recurse_opt;
968 + } elsif ($d_cnt == 0) {
970 + print "(removed $SUMS_FILE) " if $verbosity;
971 + unlink($SUMS_FILE);
973 + print "empty\n" if $verbosity;
974 + } elsif ($update_cnt || $d_cnt != $f_cnt) {
975 + print "UPDATING\n" if $verbosity && !$update_cnt;
976 + open(FP, '>', $SUMS_FILE) or die "Unable to write $dir/$SUMS_FILE: $!\n";
978 + foreach my $fn (sort keys %cache) {
979 + my $ref = $cache{$fn};
980 + my($found, $sum4, $sum5, $size, $mtime, $ctime, $inode) = @$ref;
981 + next unless $found;
982 + printf FP '%s %s %10d %10d %10d %10d %s' . "\n", $sum4, $sum5, $size, $mtime, $ctime, $inode, $fn;
986 + print "ok\n" if $verbosity;
995 +Usage: rsyncsums [OPTIONS] [DIRS]
998 + -r, --recurse Update $SUMS_FILE files in subdirectories too.
999 + -m, --mode=MODE Compare entries in either "lax" or "strict" mode. Using
1000 + "lax" compares size and mtime, while "strict" additionally
1001 + compares ctime and inode. Default: strict.
1002 + -c, --check Check if the checksums are right (doesn't update).
1003 + -v, --verbose Mention what we're doing. Repeat for more info.
1004 + -h, --help Display this help message.