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 based-on: fe2ef556d9ef11e5dd549e19a06a7a924f7ddfa1
20 diff --git a/clientserver.c b/clientserver.c
23 @@ -43,6 +43,8 @@ extern int numeric_ids;
24 extern int filesfrom_fd;
25 extern int remote_protocol;
26 extern int protocol_version;
27 +extern int always_checksum;
28 +extern int checksum_files;
29 extern int io_timeout;
31 extern int write_batch;
32 @@ -1015,6 +1017,9 @@ static int rsync_module(int f_in, int f_out, int i, const char *addr, const char
33 } else if (am_root < 0) /* Treat --fake-super from client as --super. */
36 + checksum_files = always_checksum ? lp_checksum_files(i)
39 if (filesfrom_fd == 0)
42 diff --git a/flist.c b/flist.c
53 @@ -33,6 +34,7 @@ extern int am_sender;
54 extern int am_generator;
55 extern int inc_recurse;
56 extern int always_checksum;
57 +extern int basis_dir_cnt;
58 extern int checksum_type;
60 extern int ignore_errors;
61 @@ -61,6 +63,7 @@ extern int implied_dirs;
62 extern int ignore_perishable;
63 extern int non_perishable_cnt;
64 extern int prune_empty_dirs;
65 +extern int checksum_files;
66 extern int copy_links;
67 extern int copy_unsafe_links;
68 extern int protocol_version;
69 @@ -72,6 +75,7 @@ extern int sender_symlink_iconv;
70 extern int output_needs_newline;
71 extern int sender_keeps_checksum;
72 extern int unsort_ndx;
73 +extern char *basis_dir[];
75 extern struct stats stats;
76 extern char *filesfrom_host;
77 @@ -89,6 +93,20 @@ extern int filesfrom_convert;
78 extern iconv_t ic_send, ic_recv;
81 +#ifdef HAVE_UTIMENSAT
82 +#ifdef HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
83 +#define ST_MTIME_NSEC st_mtim.tv_nsec
84 +#elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
85 +#define ST_MTIME_NSEC st_mtimensec
89 +#define RSYNCSUMS_FILE ".rsyncsums"
90 +#define RSYNCSUMS_LEN (sizeof RSYNCSUMS_FILE-1)
92 +#define CLEAN_STRIP_ROOT (1<<0)
93 +#define CLEAN_KEEP_LAST (1<<1)
95 #define PTR_SIZE (sizeof (struct file_struct *))
98 @@ -133,7 +151,11 @@ static char empty_sum[MAX_DIGEST_LEN];
99 static int flist_count_offset; /* for --delete --progress */
100 static int show_filelist_progress;
102 -static void flist_sort_and_clean(struct file_list *flist, int strip_root);
103 +static struct csum_cache {
104 + struct file_list *flist;
105 +} *csum_cache = NULL;
107 +static void flist_sort_and_clean(struct file_list *flist, int flags);
108 static void output_flist(struct file_list *flist);
110 void init_flist(void)
111 @@ -322,6 +344,235 @@ static void flist_done_allocating(struct file_list *flist)
112 flist->pool_boundary = ptr;
115 +void reset_checksum_cache()
117 + int slot, slots = am_sender ? 1 : basis_dir_cnt + 1;
120 + csum_cache = new_array0(struct csum_cache, slots);
122 + for (slot = 0; slot < slots; slot++) {
123 + struct file_list *flist = csum_cache[slot].flist;
126 + /* Reset the pool memory and empty the file-list array. */
127 + pool_free_old(flist->file_pool,
128 + pool_boundary(flist->file_pool, 0));
131 + flist = csum_cache[slot].flist = flist_new(FLIST_TEMP, "reset_checksum_cache");
135 + flist->next = NULL;
139 +/* The basename_len count is the length of the basename + 1 for the '\0'. */
140 +static int add_checksum(struct file_list *flist, const char *dirname,
141 + const char *basename, int basename_len, OFF_T file_length,
142 + time_t mtime, uint32 ctime, uint32 inode,
145 + struct file_struct *file;
146 + int alloc_len, extra_len;
149 + if (basename_len == RSYNCSUMS_LEN+1 && *basename == '.'
150 + && strcmp(basename, RSYNCSUMS_FILE) == 0)
153 + /* "2" is for a 32-bit ctime num and an 32-bit inode num. */
154 + extra_len = (file_extra_cnt + (file_length > 0xFFFFFFFFu) + SUM_EXTRA_CNT + 2)
156 +#if EXTRA_ROUNDING > 0
157 + if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
158 + extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
160 + alloc_len = FILE_STRUCT_LEN + extra_len + basename_len;
161 + bp = pool_alloc(flist->file_pool, alloc_len, "add_checksum");
163 + memset(bp, 0, extra_len + FILE_STRUCT_LEN);
165 + file = (struct file_struct *)bp;
166 + bp += FILE_STRUCT_LEN;
168 + memcpy(bp, basename, basename_len);
170 + file->mode = S_IFREG;
171 + file->modtime = mtime;
172 + file->len32 = (uint32)file_length;
173 + if (file_length > 0xFFFFFFFFu) {
174 + file->flags |= FLAG_LENGTH64;
175 + OPT_EXTRA(file, 0)->unum = (uint32)(file_length >> 32);
177 + file->dirname = dirname;
178 + F_CTIME(file) = ctime;
179 + F_INODE(file) = inode;
181 + memcpy(bp, sum, flist_csum_len);
183 + flist_expand(flist, 1);
184 + flist->files[flist->used++] = file;
186 + flist->sorted = flist->files;
191 +/* The "dirname" arg's data must remain unchanged during the lifespan of
192 + * the created csum_cache[].flist object because we use it directly. */
193 +static void read_checksums(int slot, struct file_list *flist, const char *dirname)
195 + char line[MAXPATHLEN+1024], fbuf[MAXPATHLEN], sum[MAX_DIGEST_LEN];
201 + uint32 ctime, inode;
202 + int dlen = dirname ? strlcpy(fbuf, dirname, sizeof fbuf) : 0;
204 + if (dlen >= (int)(sizeof fbuf - 1 - RSYNCSUMS_LEN))
207 + fbuf[dlen++] = '/';
210 + strlcpy(fbuf+dlen, RSYNCSUMS_FILE, sizeof fbuf - dlen);
212 + pathjoin(line, sizeof line, basis_dir[slot-1], fbuf);
216 + if (!(fp = fopen(cp, "r")))
219 + while (fgets(line, sizeof line, fp)) {
221 + if (checksum_type == 5) {
222 + char *alt_sum = cp;
224 + while (*++cp == '=') {}
226 + while (isHexDigit(cp)) cp++;
227 + if (cp - alt_sum != MD4_DIGEST_LEN*2 || *cp != ' ')
229 + while (*++cp == ' ') {}
235 + for (i = 0; i < flist_csum_len*2; i++, cp++) {
237 + if (isHexDigit(cp)) {
241 + x = (*cp & 0xF) + 9;
254 + while (*++cp == ' ') {}
256 + if (checksum_type != 5) {
257 + char *alt_sum = cp;
259 + while (*++cp == '=') {}
261 + while (isHexDigit(cp)) cp++;
262 + if (cp - alt_sum != MD5_DIGEST_LEN*2 || *cp != ' ')
264 + while (*++cp == ' ') {}
268 + while (isDigit(cp))
269 + file_length = file_length * 10 + *cp++ - '0';
272 + while (*++cp == ' ') {}
275 + while (isDigit(cp))
276 + mtime = mtime * 10 + *cp++ - '0';
279 + while (*++cp == ' ') {}
282 + while (isDigit(cp))
283 + ctime = ctime * 10 + *cp++ - '0';
286 + while (*++cp == ' ') {}
289 + while (isDigit(cp))
290 + inode = inode * 10 + *cp++ - '0';
293 + while (*++cp == ' ') {}
296 + while (len && (cp[len-1] == '\n' || cp[len-1] == '\r'))
300 + cp[len++] = '\0'; /* len now counts the null */
301 + if (strchr(cp, '/'))
303 + if (len > MAXPATHLEN)
306 + strlcpy(fbuf+dlen, cp, sizeof fbuf - dlen);
308 + add_checksum(flist, dirname, cp, len, file_length,
309 + mtime, ctime, inode,
314 + flist_sort_and_clean(flist, CLEAN_KEEP_LAST);
317 +void get_cached_checksum(int slot, const char *fname, struct file_struct *file,
318 + STRUCT_STAT *stp, char *sum_buf)
320 + struct file_list *flist = csum_cache[slot].flist;
323 + if (!flist->next) {
324 + flist->next = cur_flist; /* next points from checksum flist to file flist */
325 + read_checksums(slot, flist, file->dirname);
328 + if ((j = flist_find(flist, file)) >= 0) {
329 + struct file_struct *fp = flist->sorted[j];
331 + if (F_LENGTH(fp) == stp->st_size
332 + && fp->modtime == stp->st_mtime
333 + && (checksum_files & CSF_LAX
334 + || (F_CTIME(fp) == (uint32)stp->st_ctime
335 + && F_INODE(fp) == (uint32)stp->st_ino))) {
336 + memcpy(sum_buf, F_SUM(fp), MAX_DIGEST_LEN);
341 + file_checksum(fname, stp, sum_buf);
344 /* Call this with EITHER (1) "file, NULL, 0" to chdir() to the file's
345 * F_PATHNAME(), or (2) "NULL, dir, dirlen" to chdir() to the supplied dir,
346 * with dir == NULL taken to be the starting directory, and dirlen < 0
347 @@ -1150,7 +1401,7 @@ struct file_struct *make_file(const char *fname, struct file_list *flist,
348 STRUCT_STAT *stp, int flags, int filter_level)
350 static char *lastdir;
351 - static int lastdir_len = -1;
352 + static int lastdir_len = -2;
353 struct file_struct *file;
354 char thisname[MAXPATHLEN];
355 char linkname[MAXPATHLEN];
356 @@ -1296,9 +1547,16 @@ struct file_struct *make_file(const char *fname, struct file_list *flist,
357 memcpy(lastdir, thisname, len);
360 + if (checksum_files && am_sender && flist)
361 + reset_checksum_cache();
366 + if (checksum_files && am_sender && flist && lastdir_len == -2) {
368 + reset_checksum_cache();
371 basename_len = strlen(basename) + 1; /* count the '\0' */
374 @@ -1316,11 +1574,8 @@ struct file_struct *make_file(const char *fname, struct file_list *flist,
375 extra_len += EXTRA_LEN;
378 - if (always_checksum && am_sender && S_ISREG(st.st_mode)) {
379 - file_checksum(thisname, &st, tmp_sum);
380 - if (sender_keeps_checksum)
381 - extra_len += SUM_EXTRA_CNT * EXTRA_LEN;
383 + if (sender_keeps_checksum && S_ISREG(st.st_mode))
384 + extra_len += SUM_EXTRA_CNT * EXTRA_LEN;
386 #if EXTRA_ROUNDING > 0
387 if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
388 @@ -1405,8 +1660,14 @@ struct file_struct *make_file(const char *fname, struct file_list *flist,
392 - if (sender_keeps_checksum && S_ISREG(st.st_mode))
393 - memcpy(F_SUM(file), tmp_sum, flist_csum_len);
394 + if (always_checksum && am_sender && S_ISREG(st.st_mode)) {
395 + if (flist && checksum_files)
396 + get_cached_checksum(0, thisname, file, &st, tmp_sum);
398 + file_checksum(thisname, &st, tmp_sum);
399 + if (sender_keeps_checksum)
400 + memcpy(F_SUM(file), tmp_sum, flist_csum_len);
404 F_NDX(file) = stats.num_dirs;
405 @@ -2621,7 +2882,7 @@ struct file_list *recv_file_list(int f, int dir_ndx)
406 /* The --relative option sends paths with a leading slash, so we need
407 * to specify the strip_root option here. We rejected leading slashes
408 * for a non-relative transfer in recv_file_entry(). */
409 - flist_sort_and_clean(flist, relative_paths);
410 + flist_sort_and_clean(flist, relative_paths ? CLEAN_STRIP_ROOT : 0);
412 if (protocol_version < 30) {
413 /* Recv the io_error flag */
414 @@ -2871,7 +3132,7 @@ void flist_free(struct file_list *flist)
416 /* This routine ensures we don't have any duplicate names in our file list.
417 * duplicate names can cause corruption because of the pipelining. */
418 -static void flist_sort_and_clean(struct file_list *flist, int strip_root)
419 +static void flist_sort_and_clean(struct file_list *flist, int flags)
421 char fbuf[MAXPATHLEN];
423 @@ -2922,7 +3183,7 @@ static void flist_sort_and_clean(struct file_list *flist, int strip_root)
424 /* If one is a dir and the other is not, we want to
425 * keep the dir because it might have contents in the
426 * list. Otherwise keep the first one. */
427 - if (S_ISDIR(file->mode)) {
428 + if (S_ISDIR(file->mode) || flags & CLEAN_KEEP_LAST) {
429 struct file_struct *fp = flist->sorted[j];
430 if (!S_ISDIR(fp->mode))
432 @@ -2938,8 +3199,8 @@ static void flist_sort_and_clean(struct file_list *flist, int strip_root)
437 - if (DEBUG_GTE(DUP, 1)) {
438 + if (!am_sender || flags & CLEAN_KEEP_LAST) {
439 + if (DEBUG_GTE(DUP, 1) && !(flags & CLEAN_KEEP_LAST)) {
441 "removing duplicate name %s from file list (%d)\n",
442 f_name(file, fbuf), drop + flist->ndx_start);
443 @@ -2961,7 +3222,7 @@ static void flist_sort_and_clean(struct file_list *flist, int strip_root)
445 flist->high = prev_i;
448 + if (flags & CLEAN_STRIP_ROOT) {
449 /* We need to strip off the leading slashes for relative
450 * paths, but this must be done _after_ the sorting phase. */
451 for (i = flist->low; i <= flist->high; i++) {
452 diff --git a/generator.c b/generator.c
455 @@ -52,6 +52,7 @@ extern int delete_after;
456 extern int missing_args;
457 extern int msgdone_cnt;
458 extern int ignore_errors;
459 +extern int checksum_files;
460 extern int remove_source_files;
461 extern int delay_updates;
462 extern int update_only;
463 @@ -580,7 +581,7 @@ void itemize(const char *fnamecmp, struct file_struct *file, int ndx, int statre
466 /* Perform our quick-check heuristic for determining if a file is unchanged. */
467 -int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st)
468 +int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st, int slot)
470 if (st->st_size != F_LENGTH(file))
472 @@ -589,7 +590,10 @@ int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st)
473 of the file time to determine whether to sync */
474 if (always_checksum > 0 && S_ISREG(st->st_mode)) {
475 char sum[MAX_DIGEST_LEN];
476 - file_checksum(fn, st, sum);
477 + if (checksum_files && slot >= 0)
478 + get_cached_checksum(slot, fn, file, st, sum);
480 + file_checksum(fn, st, sum);
481 return memcmp(sum, F_SUM(file), flist_csum_len) == 0;
484 @@ -886,7 +890,7 @@ static int try_dests_reg(struct file_struct *file, char *fname, int ndx,
488 - if (!unchanged_file(cmpbuf, file, &sxp->st))
489 + if (!unchanged_file(cmpbuf, file, &sxp->st, j+1))
491 if (match_level == 1) {
493 @@ -1197,7 +1201,7 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx,
494 * --ignore-non-existing, daemon exclude, or mkdir failure. */
495 static struct file_struct *skip_dir = NULL;
496 static struct file_list *fuzzy_dirlist[MAX_BASIS_DIRS+1];
497 - static int need_fuzzy_dirlist = 0;
498 + static int need_new_dirscan = 0;
499 struct file_struct *fuzzy_file = NULL;
500 int fd = -1, f_copy = -1;
502 @@ -1308,8 +1312,9 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx,
503 fuzzy_dirlist[i] = NULL;
506 - need_fuzzy_dirlist = 1;
508 + need_new_dirscan = 1;
509 + } else if (checksum_files)
510 + need_new_dirscan = 1;
513 dflt_perms = default_perms_for_dir(dn);
514 @@ -1317,6 +1322,24 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx,
518 + if (need_new_dirscan && S_ISREG(file->mode)) {
520 + strlcpy(fnamecmpbuf, dn, sizeof fnamecmpbuf);
521 + for (i = 0; i < fuzzy_basis; i++) {
522 + if (i && pathjoin(fnamecmpbuf, MAXPATHLEN, basis_dir[i-1], dn) >= MAXPATHLEN)
524 + fuzzy_dirlist[i] = get_dirlist(fnamecmpbuf, -1, GDL_IGNORE_FILTER_RULES | GDL_PERHAPS_DIR);
525 + if (fuzzy_dirlist[i] && fuzzy_dirlist[i]->used == 0) {
526 + flist_free(fuzzy_dirlist[i]);
527 + fuzzy_dirlist[i] = NULL;
530 + if (checksum_files) {
531 + reset_checksum_cache();
533 + need_new_dirscan = 0;
536 statret = link_stat(fname, &sx.st, keep_dirlinks && is_dir);
539 @@ -1720,22 +1743,6 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx,
542 if (statret != 0 && fuzzy_basis) {
543 - if (need_fuzzy_dirlist && S_ISREG(file->mode)) {
544 - const char *dn = file->dirname ? file->dirname : ".";
546 - strlcpy(fnamecmpbuf, dn, sizeof fnamecmpbuf);
547 - for (i = 0; i < fuzzy_basis; i++) {
548 - if (i && pathjoin(fnamecmpbuf, MAXPATHLEN, basis_dir[i-1], dn) >= MAXPATHLEN)
550 - fuzzy_dirlist[i] = get_dirlist(fnamecmpbuf, -1, GDL_IGNORE_FILTER_RULES | GDL_PERHAPS_DIR);
551 - if (fuzzy_dirlist[i] && fuzzy_dirlist[i]->used == 0) {
552 - flist_free(fuzzy_dirlist[i]);
553 - fuzzy_dirlist[i] = NULL;
556 - need_fuzzy_dirlist = 0;
559 /* Sets fnamecmp_type to FNAMECMP_FUZZY or above. */
560 fuzzy_file = find_fuzzy(file, fuzzy_dirlist, &fnamecmp_type);
562 @@ -1768,7 +1775,7 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx,
564 else if (fnamecmp_type >= FNAMECMP_FUZZY)
566 - else if (unchanged_file(fnamecmp, file, &sx.st)) {
567 + else if (unchanged_file(fnamecmp, file, &sx.st, fnamecmp_type == FNAMECMP_FNAME ? 0 : -1)) {
569 do_unlink(partialptr);
570 handle_partial_dir(partialptr, PDIR_DELETE);
571 diff --git a/hlink.c b/hlink.c
574 @@ -406,7 +406,7 @@ int hard_link_check(struct file_struct *file, int ndx, char *fname,
578 - if (!unchanged_file(cmpbuf, file, &alt_sx.st))
579 + if (!unchanged_file(cmpbuf, file, &alt_sx.st, j+1))
582 if (unchanged_attrs(cmpbuf, file, &alt_sx))
583 diff --git a/loadparm.c b/loadparm.c
586 @@ -174,6 +174,7 @@ typedef struct {
590 + int checksum_files;
594 @@ -291,6 +292,7 @@ static const all_vars Defaults = {
595 /* temp_dir_EXP; */ False,
596 /* uid_EXP; */ False,
598 + /* checksum_files; */ CSF_IGNORE_FILES,
599 /* max_connections; */ 0,
600 /* max_verbosity; */ 1,
601 /* syslog_facility; */ LOG_DAEMON,
602 @@ -393,6 +395,13 @@ static struct enum_list enum_facilities[] = {
606 +static struct enum_list enum_csum_modes[] = {
607 + { CSF_IGNORE_FILES, "none" },
608 + { CSF_LAX_MODE, "lax" },
609 + { CSF_STRICT_MODE, "strict" },
613 static struct parm_struct parm_table[] =
615 {"address", P_STRING, P_GLOBAL,&Vars.g.bind_address, NULL,0},
616 @@ -408,6 +417,7 @@ static struct parm_struct parm_table[] =
618 {"auth users", P_STRING, P_LOCAL, &Vars.l.auth_users, NULL,0},
619 {"charset", P_STRING, P_LOCAL, &Vars.l.charset, NULL,0},
620 + {"checksum files", P_ENUM, P_LOCAL, &Vars.l.checksum_files, enum_csum_modes,0},
621 {"comment", P_STRING, P_LOCAL, &Vars.l.comment, NULL,0},
622 {"dont compress", P_STRING, P_LOCAL, &Vars.l.dont_compress, NULL,0},
623 {"early exec", P_STRING, P_LOCAL, &Vars.l.early_exec, NULL,0},
624 @@ -575,6 +585,7 @@ FN_LOCAL_STRING(lp_syslog_tag, syslog_tag)
625 FN_LOCAL_STRING(lp_temp_dir, temp_dir)
626 FN_LOCAL_STRING(lp_uid, uid)
628 +FN_LOCAL_INTEGER(lp_checksum_files, checksum_files)
629 FN_LOCAL_INTEGER(lp_max_connections, max_connections)
630 FN_LOCAL_INTEGER(lp_max_verbosity, max_verbosity)
631 FN_LOCAL_INTEGER(lp_syslog_facility, syslog_facility)
632 diff --git a/options.c b/options.c
635 @@ -119,6 +119,7 @@ size_t bwlimit_writemax = 0;
636 int ignore_existing = 0;
637 int ignore_non_existing = 0;
638 int need_messages_from_generator = 0;
639 +int checksum_files = CSF_IGNORE_FILES;
640 int max_delete = INT_MIN;
643 @@ -782,7 +783,7 @@ enum {OPT_SERVER = 1000, OPT_DAEMON, OPT_SENDER, OPT_EXCLUDE, OPT_EXCLUDE_FROM,
644 OPT_FILTER, OPT_COMPARE_DEST, OPT_COPY_DEST, OPT_LINK_DEST, OPT_HELP,
645 OPT_INCLUDE, OPT_INCLUDE_FROM, OPT_MODIFY_WINDOW, OPT_MIN_SIZE, OPT_CHMOD,
646 OPT_READ_BATCH, OPT_WRITE_BATCH, OPT_ONLY_WRITE_BATCH, OPT_MAX_SIZE,
647 - OPT_NO_D, OPT_APPEND, OPT_NO_ICONV, OPT_INFO, OPT_DEBUG,
648 + OPT_NO_D, OPT_APPEND, OPT_NO_ICONV, OPT_INFO, OPT_DEBUG, OPT_SUMFILES,
649 OPT_USERMAP, OPT_GROUPMAP, OPT_CHOWN, OPT_BWLIMIT,
650 OPT_OLD_COMPRESS, OPT_NEW_COMPRESS, OPT_NO_COMPRESS,
651 OPT_REFUSED_BASE = 9000};
652 @@ -934,6 +935,7 @@ static struct poptOption long_options[] = {
653 {"no-c", 0, POPT_ARG_VAL, &always_checksum, 0, 0, 0 },
654 {"checksum-choice", 0, POPT_ARG_STRING, &checksum_choice, 0, 0, 0 },
655 {"cc", 0, POPT_ARG_STRING, &checksum_choice, 0, 0, 0 },
656 + {"sumfiles", 0, POPT_ARG_STRING, 0, OPT_SUMFILES, 0, 0 },
657 {"block-size", 'B', POPT_ARG_LONG, &block_size, 0, 0, 0 },
658 {"compare-dest", 0, POPT_ARG_STRING, 0, OPT_COMPARE_DEST, 0, 0 },
659 {"copy-dest", 0, POPT_ARG_STRING, 0, OPT_COPY_DEST, 0, 0 },
660 @@ -1757,6 +1759,23 @@ int parse_arguments(int *argc_p, const char ***argv_p)
665 + arg = poptGetOptArg(pc);
666 + checksum_files = 0;
667 + if (strcmp(arg, "lax") == 0)
668 + checksum_files |= CSF_LAX_MODE;
669 + else if (strcmp(arg, "strict") == 0)
670 + checksum_files |= CSF_STRICT_MODE;
671 + else if (strcmp(arg, "none") == 0)
672 + checksum_files = CSF_IGNORE_FILES;
674 + snprintf(err_buf, sizeof err_buf,
675 + "Invalid argument passed to --sumfiles (%s)\n",
682 arg = poptGetOptArg(pc);
683 parse_output_words(info_words, info_levels, arg, USER_PRIORITY);
684 @@ -2043,6 +2062,9 @@ int parse_arguments(int *argc_p, const char ***argv_p)
688 + if (!always_checksum)
689 + checksum_files = CSF_IGNORE_FILES;
692 /* We may not know the real protocol_version at this point if this is the client
693 * option parsing, but we still want to check it so that the client can specify
694 diff --git a/rsync.1.md b/rsync.1.md
697 @@ -338,6 +338,7 @@ detailed description below for a complete description.
698 --quiet, -q suppress non-error messages
699 --no-motd suppress daemon-mode MOTD
700 --checksum, -c skip based on checksum, not mod-time & size
701 +--sumfiles=MODE use .rsyncsums to speedup --checksum mode
702 --archive, -a archive mode; equals -rlptgoD (no -H,-A,-X)
703 --no-OPTION turn off an implied OPTION (e.g. --no-D)
704 --recursive, -r recurse into directories
705 @@ -684,6 +685,8 @@ your home directory (remove the '=' for that).
706 file that has the same size as the corresponding sender's file: files with
707 either a changed size or a changed checksum are selected for transfer.
709 + See also the `--sumfiles` option for a way to use cached checksum data.
711 Note that rsync always verifies that each _transferred_ file was correctly
712 reconstructed on the receiving side by checking a whole-file checksum that
713 is generated as the file is transferred, but that automatic
714 @@ -694,6 +697,38 @@ your home directory (remove the '=' for that).
715 can be overridden using either the `--checksum-choice` option or an
716 environment variable that is discussed in that option's section.
718 +0. `--sumfiles=MODE`
720 + This option tells rsync to make use of any cached checksum information it
721 + finds in per-directory .rsyncsums files when the current transfer is using
722 + the `--checksum` option. If the checksum data is up-to-date, it is used
723 + instead of recomputing it, saving both disk I/O and CPU time. If the
724 + checksum data is missing or outdated, the checksum is computed just as it
725 + would be if `--sumfiles` was not specified.
727 + The MODE value is either "lax", for relaxed checking (which compares size
728 + and mtime), "strict" (which also compares ctime and inode), or "none" to
729 + ignore any .rsyncsums files ("none" is the default). Rsync does not create
730 + or update these files, but there is a perl script in the support directory
731 + named "rsyncsums" that can be used for that.
733 + This option has no effect unless `--checksum`, `-c` was also specified. It
734 + also only affects the current side of the transfer, so if you want the
735 + remote side to parse its own .rsyncsums files, specify the option via the
736 + `--rsync-path` option (e.g. "--rsync-path="rsync --sumfiles=lax").
738 + To avoid transferring the system's checksum files, you can use an exclude
739 + (e.g. `--exclude=.rsyncsums`). To make this easier to type, you can use a
740 + popt alias. For instance, adding the following line in your ~/.popt file
741 + defines a `--cc` option that enables lax checksum files and excludes the
744 + > rsync alias --cc -c --sumfiles=lax --exclude=.rsyncsums
746 + An rsync daemon does not allow the client to control this setting, so see
747 + the "checksum files" daemon parameter for information on how to make a
748 + daemon use cached checksum data.
752 This is equivalent to `-rlptgoD`. It is a quick way of saying you want
753 diff --git a/rsync.h b/rsync.h
756 @@ -834,6 +834,10 @@ extern int xattrs_ndx;
757 #define F_SUM(f) ((char*)OPT_EXTRA(f, START_BUMP(f) + HLINK_BUMP(f) \
758 + SUM_EXTRA_CNT - 1))
760 +/* These are only valid on an entry read from a checksum file. */
761 +#define F_CTIME(f) OPT_EXTRA(f, LEN64_BUMP(f) + SUM_EXTRA_CNT)->unum
762 +#define F_INODE(f) OPT_EXTRA(f, LEN64_BUMP(f) + SUM_EXTRA_CNT + 1)->unum
764 /* Some utility defines: */
765 #define F_IS_ACTIVE(f) (f)->basename[0]
766 #define F_IS_HLINKED(f) ((f)->flags & FLAG_HLINKED)
767 @@ -1036,6 +1040,13 @@ typedef struct {
768 char fname[1]; /* has variable size */
771 +#define CSF_ENABLE (1<<1)
772 +#define CSF_LAX (1<<2)
774 +#define CSF_IGNORE_FILES 0
775 +#define CSF_LAX_MODE (CSF_ENABLE|CSF_LAX)
776 +#define CSF_STRICT_MODE (CSF_ENABLE)
778 #include "byteorder.h"
779 #include "lib/mdigest.h"
780 #include "lib/wildmatch.h"
781 diff --git a/rsyncd.conf.5.md b/rsyncd.conf.5.md
782 --- a/rsyncd.conf.5.md
783 +++ b/rsyncd.conf.5.md
784 @@ -404,6 +404,19 @@ the values of parameters. See the GLOBAL PARAMETERS section for more details.
785 the max connections limit is not exceeded for the modules sharing the lock
786 file. The default is `/var/run/rsyncd.lock`.
790 + This parameter tells rsync to make use of any cached checksum information
791 + it finds in per-directory .rsyncsums files when the current transfer is
792 + using the `--checksum` option. The value can be set to either "lax",
793 + "strict", or "none" -- see the client's `--sumfiles` option for what these
796 + Note also that the client's command-line option, `--sumfiles`, has no
797 + effect on a daemon. A daemon will only access checksum files if this
798 + config option tells it to. See also the `exclude` directive for a way to
799 + hide the .rsyncsums files from the user.
803 This parameter determines whether clients will be able to upload files or
804 diff --git a/support/rsyncsums b/support/rsyncsums
807 +++ b/support/rsyncsums
813 +use Cwd qw(abs_path cwd);
817 +our $SUMS_FILE = '.rsyncsums';
819 +&Getopt::Long::Configure('bundling');
820 +&usage if !&GetOptions(
821 + 'recurse|r' => \( my $recurse_opt ),
822 + 'mode|m=s' => \( my $cmp_mode = 'strict' ),
823 + 'check|c' => \( my $check_opt ),
824 + 'verbose|v+' => \( my $verbosity = 0 ),
825 + 'help|h' => \( my $help_opt ),
827 +&usage if $help_opt || $cmp_mode !~ /^(lax|strict)$/;
829 +my $ignore_ctime_and_inode = $cmp_mode eq 'lax' ? 0 : 1;
831 +my $start_dir = cwd();
834 +@dirs = '.' unless @dirs;
843 +my $md4 = Digest::MD4->new;
844 +my $md5 = Digest::MD5->new;
847 + my $dir = shift @dirs;
849 + if (!chdir($dir)) {
850 + warn "Unable to chdir to $dir: $!\n";
853 + if (!opendir(DP, '.')) {
854 + warn "Unable to opendir $dir: $!\n";
859 + $reldir =~ s#^$start_dir(/|$)# $1 ? '' : '.' #eo;
861 + print "$reldir ... ";
862 + print "\n" if $check_opt;
867 + if (open(FP, '<', $SUMS_FILE)) {
870 + my($sum4, $sum5, $size, $mtime, $ctime, $inode, $fn) = split(' ', $_, 7);
871 + $cache{$fn} = [ 0, $sum4, $sum5, $size, $mtime, $ctime & 0xFFFFFFFF, $inode & 0xFFFFFFFF ];
879 + my $update_cnt = 0;
880 + while (defined(my $fn = readdir(DP))) {
881 + next if $fn =~ /^\.\.?$/ || $fn =~ /^\Q$SUMS_FILE\E$/o || -l $fn;
883 + push(@subdirs, "$dir/$fn") unless $fn =~ /^(CVS|\.svn|\.git|\.bzr)$/;
888 + my($size,$mtime,$ctime,$inode) = (stat(_))[7,9,10,1];
889 + $ctime &= 0xFFFFFFFF;
890 + $inode &= 0xFFFFFFFF;
891 + my $ref = $cache{$fn};
895 + if (defined $ref) {
897 + if ($$ref[3] == $size
898 + && $$ref[4] == $mtime
899 + && ($ignore_ctime_and_inode || ($$ref[5] == $ctime && $$ref[6] == $inode))
900 + && $$ref[1] !~ /=/ && $$ref[2] !~ /=/) {
904 + if (!$update_cnt++) {
905 + print "UPDATING\n" if $verbosity;
909 + if (!open(IN, $fn)) {
910 + print STDERR "Unable to read $fn: $!\n";
911 + if (defined $ref) {
912 + delete $cache{$fn};
920 + while (sysread(IN, $_, 64*1024)) {
924 + $sum4 = $md4->hexdigest;
925 + $sum5 = $md5->hexdigest;
926 + print " $sum4 $sum5" if $verbosity > 2;
927 + print " $fn" if $verbosity > 1;
928 + my($size2,$mtime2,$ctime2,$inode2) = (stat(IN))[7,9,10,1];
929 + $ctime2 &= 0xFFFFFFFF;
930 + $inode2 &= 0xFFFFFFFF;
931 + last if $size == $size2 && $mtime == $mtime2
932 + && ($ignore_ctime_and_inode || ($ctime == $ctime2 && $inode == $inode2));
938 + print " REREADING\n" if $verbosity > 1;
945 + if (!defined $ref) {
947 + } elsif ($sum4 ne $$ref[1] || $sum5 ne $$ref[2]) {
950 + print " OK\n" if $verbosity > 1;
953 + if ($verbosity < 2) {
954 + print $verbosity ? ' ' : "$reldir/";
960 + print "\n" if $verbosity > 1;
961 + $cache{$fn} = [ 1, $sum4, $sum5, $size, $mtime, $ctime, $inode ];
967 + unshift(@dirs, sort @subdirs) if $recurse_opt;
971 + } elsif ($d_cnt == 0) {
973 + print "(removed $SUMS_FILE) " if $verbosity;
974 + unlink($SUMS_FILE);
976 + print "empty\n" if $verbosity;
977 + } elsif ($update_cnt || $d_cnt != $f_cnt) {
978 + print "UPDATING\n" if $verbosity && !$update_cnt;
979 + open(FP, '>', $SUMS_FILE) or die "Unable to write $dir/$SUMS_FILE: $!\n";
981 + foreach my $fn (sort keys %cache) {
982 + my $ref = $cache{$fn};
983 + my($found, $sum4, $sum5, $size, $mtime, $ctime, $inode) = @$ref;
984 + next unless $found;
985 + printf FP '%s %s %10d %10d %10d %10d %s' . "\n", $sum4, $sum5, $size, $mtime, $ctime, $inode, $fn;
989 + print "ok\n" if $verbosity;
998 +Usage: rsyncsums [OPTIONS] [DIRS]
1001 + -r, --recurse Update $SUMS_FILE files in subdirectories too.
1002 + -m, --mode=MODE Compare entries in either "lax" or "strict" mode. Using
1003 + "lax" compares size and mtime, while "strict" additionally
1004 + compares ctime and inode. Default: strict.
1005 + -c, --check Check if the checksums are right (doesn't update).
1006 + -v, --verbose Mention what we're doing. Repeat for more info.
1007 + -h, --help Display this help message.