1 This adds a sender optimization feature that allows a cache of checksums
2 to be used when the client specifies the --checksum option, and creates
3 and/or updates the .rsyncsums files when --checksum-updating is
6 To use this patch, run these commands for a successful build:
8 patch -p1 <patches/checksum-updating.diff
9 ./configure (optional if already run)
12 --- old/clientserver.c
13 +++ new/clientserver.c
14 @@ -37,6 +37,7 @@ extern int sanitize_paths;
15 extern int filesfrom_fd;
16 extern int remote_protocol;
17 extern int protocol_version;
18 +extern int checksum_updating;
19 extern int io_timeout;
21 extern int default_af_hint;
22 @@ -641,6 +642,8 @@ static int rsync_module(int f_in, int f_
23 else if (am_root < 0) /* Treat --fake-super from client as --super. */
26 + checksum_updating = lp_checksum_updating(i);
28 if (filesfrom_fd == 0)
41 @@ -57,6 +58,7 @@ extern int implied_dirs;
42 extern int file_extra_cnt;
43 extern int ignore_perishable;
44 extern int non_perishable_cnt;
45 +extern int checksum_updating;
46 extern int prune_empty_dirs;
47 extern int copy_links;
48 extern int copy_unsafe_links;
49 @@ -79,6 +81,9 @@ extern iconv_t ic_send, ic_recv;
51 #define PTR_SIZE (sizeof (struct file_struct *))
53 +#define FLAG_SUM_MISSING (1<<1) /* F_SUM() data is undefined */
54 +#define FLAG_SUM_KEEP (1<<2) /* keep entry when rewriting */
58 dev_t filesystem_dev; /* used to implement -x */
59 @@ -101,6 +106,10 @@ static char tmp_sum[MAX_DIGEST_LEN];
60 static char empty_sum[MAX_DIGEST_LEN];
61 static int flist_count_offset; /* for --delete --progress */
62 static int dir_count = 0;
63 +static struct file_list *checksum_flist = NULL;
64 +static int checksum_matches = 0;
65 +static int checksum_updates = 0;
66 +static int regular_skipped = 0;
68 static void clean_flist(struct file_list *flist, int strip_root);
69 static void output_flist(struct file_list *flist);
70 @@ -317,6 +326,304 @@ static void flist_done_allocating(struct
71 flist->pool_boundary = ptr;
74 +/* The len count is the length of the basename + 1 for the null. */
75 +static void add_checksum(const char *dirname, const char *basename, int len,
76 + OFF_T file_length, time_t mtime, time_t ctime,
77 + const char *sum, const char *alt_sum, int flags)
79 + struct file_struct *file;
80 + int alloc_len, extra_len;
83 + if (len == 10+1 && *basename == '.' && strcmp(basename, ".rsyncsums") == 0)
87 + len = strlen(basename) + 1;
89 + extra_len = (file_extra_cnt + (file_length > 0xFFFFFFFFu)
90 + + SUM_EXTRA_CNT + TIME_EXTRA_CNT)
92 +#if EXTRA_ROUNDING > 0
93 + if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
94 + extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
96 + alloc_len = FILE_STRUCT_LEN + extra_len + len + checksum_len*2 + 1;
97 + bp = pool_alloc(checksum_flist->file_pool, alloc_len, "add_checksum");
99 + memset(bp, 0, extra_len + FILE_STRUCT_LEN);
101 + file = (struct file_struct *)bp;
102 + bp += FILE_STRUCT_LEN;
104 + memcpy(bp, basename, len);
106 + strlcpy(bp+len, alt_sum, checksum_len*2 + 1);
108 + memset(bp+len, '=', checksum_len*2);
109 + bp[len+checksum_len*2] = '\0';
112 + file->flags = flags;
113 + file->mode = S_IFREG;
114 + file->modtime = mtime;
115 + file->len32 = (uint32)file_length;
116 + if (file_length > 0xFFFFFFFFu) {
117 + file->flags |= FLAG_LENGTH64;
118 + OPT_EXTRA(file, 0)->unum = (uint32)(file_length >> 32);
120 + file->dirname = dirname;
122 + memcpy(bp, sum, checksum_len);
123 +#if SIZEOF_TIME_T == 4
124 + OPT_EXTRA(file, LEN64_BUMP(file) + SUM_EXTRA_CNT)->num = ctime;
126 + memcpy(bp - SIZEOF_TIME_T, &ctime, SIZEOF_TIME_T);
129 + flist_expand(checksum_flist, 1);
130 + checksum_flist->files[checksum_flist->count++] = file;
132 + checksum_flist->sorted = checksum_flist->files;
135 +static void write_checksums(const char *next_dirname, int whole_dir)
137 + static const char *dirname_save;
138 + char fbuf[MAXPATHLEN];
139 + const char *dirname;
140 + int count, new_entries, counts_match, no_skipped;
141 + time_t latest_time = 0;
145 + dirname = dirname_save;
146 + dirname_save = next_dirname;
151 + count = checksum_flist->count;
152 + new_entries = checksum_updates != 0;
153 + counts_match = count == checksum_matches;
154 + no_skipped = whole_dir && regular_skipped == 0;
156 + clean_flist(checksum_flist, 0);
158 + checksum_flist->count = 0;
159 + checksum_matches = 0;
160 + checksum_updates = 0;
161 + regular_skipped = 0;
167 + if (pathjoin(fbuf, sizeof fbuf, dirname, ".rsyncsums") >= sizeof fbuf)
170 + strlcpy(fbuf, ".rsyncsums", sizeof fbuf);
172 + if (checksum_flist->high - checksum_flist->low < 0 && no_skipped) {
177 + if (!new_entries && (counts_match || !whole_dir))
180 + if (!(out_fp = fopen(fbuf, "w")))
184 + for (i = checksum_flist->low; i <= checksum_flist->high; i++) {
185 + struct file_struct *file = checksum_flist->sorted[i];
186 + const char *cp = F_SUM(file);
187 + const char *end = cp + checksum_len;
188 + const char *alt_sum = file->basename + strlen(file->basename) + 1;
190 + if (whole_dir && !(file->flags & FLAG_SUM_KEEP))
192 +#if SIZEOF_TIME_T == 4
193 + ctime = OPT_EXTRA(file, LEN64_BUMP(file) + SUM_EXTRA_CNT)->num;
195 + memcpy(&ctime, cp - SIZEOF_TIME_T, SIZEOF_TIME_T);
197 + if (protocol_version >= 30)
198 + fprintf(out_fp, "%s ", alt_sum);
199 + if (file->flags & FLAG_SUM_MISSING) {
202 + fprintf(out_fp, "==");
203 + } while (++cp != end);
206 + fprintf(out_fp, "%02x", CVAL(cp, 0));
207 + } while (++cp != end);
209 + if (protocol_version < 30)
210 + fprintf(out_fp, " %s", alt_sum);
211 + if (*alt_sum == '=')
213 + fprintf(out_fp, " %10.0f %10.0f %10.0f %s\n",
214 + (double)F_LENGTH(file), (double)file->modtime,
215 + (double)ctime, file->basename);
216 + if (file->modtime > ctime)
217 + ctime = file->modtime;
218 + if (ctime > latest_time)
219 + latest_time = ctime;
224 + if (whole_dir && new_entries == 0)
225 + set_modtime(fbuf, latest_time, latest_time);
227 + set_modtime(fbuf, latest_time-1, latest_time-1);
230 +/* The direname value must remain unchanged during the lifespan of the
231 + * created checksum_flist object because we use it directly. */
232 +static void read_checksums(const char *dirname)
234 + char line[MAXPATHLEN+1024], fbuf[MAXPATHLEN], sum[MAX_DIGEST_LEN];
235 + const char *alt_sum = NULL;
237 + time_t mtime, ctime;
238 + int len, dlen, i, flags;
242 + write_checksums(dirname, 0);
244 + if (checksum_flist) {
245 + /* Reset the pool memory and empty the file-list array. */
246 + pool_free_old(checksum_flist->file_pool,
247 + pool_boundary(checksum_flist->file_pool, 0));
248 + checksum_flist->count = 0;
250 + checksum_flist = flist_new(FLIST_TEMP, "read_checksums");
252 + checksum_flist->low = 0;
253 + checksum_flist->high = -1;
254 + checksum_matches = 0;
255 + checksum_updates = 0;
256 + regular_skipped = 0;
261 + dlen = strlcpy(fbuf, dirname, sizeof fbuf);
262 + if (dlen >= (int)sizeof fbuf)
265 + fbuf[dlen++] = '/';
268 + strlcpy(fbuf+dlen, ".rsyncsums", sizeof fbuf - dlen);
269 + if (!(fp = fopen(fbuf, "r")))
272 + while (fgets(line, sizeof line, fp)) {
274 + if (protocol_version >= 30) {
277 + while (*++cp == '=') {}
279 + while (isXDigit(cp)) cp++;
280 + if (cp - alt_sum != MD4_DIGEST_LEN*2 || *cp != ' ')
282 + while (*++cp == ' ') {}
286 + for (i = 0; i < checksum_len*2; i++, cp++) {
292 + memset(sum, 0, checksum_len);
293 + flags = FLAG_SUM_MISSING;
295 + for (i = 0; i < checksum_len*2; i++, cp++) {
297 + if (isXDigit(cp)) {
301 + x = (*cp & 0xF) + 9;
315 + while (*++cp == ' ') {}
317 + if (protocol_version < 30) {
320 + while (*++cp == '=') {}
322 + while (isXDigit(cp)) cp++;
323 + if (cp - alt_sum != MD5_DIGEST_LEN*2 || *cp != ' ')
325 + while (*++cp == ' ') {}
329 + while (isDigit(cp))
330 + file_length = file_length * 10 + *cp++ - '0';
333 + while (*++cp == ' ') {}
336 + while (isDigit(cp))
337 + mtime = mtime * 10 + *cp++ - '0';
340 + while (*++cp == ' ') {}
343 + while (isDigit(cp))
344 + ctime = ctime * 10 + *cp++ - '0';
347 + while (*++cp == ' ') {}
350 + while (len && (cp[len-1] == '\n' || cp[len-1] == '\r'))
354 + cp[len++] = '\0'; /* len now counts the null */
355 + if (strchr(cp, '/') || len > MAXPATHLEN)
358 + strlcpy(fbuf+dlen, cp, sizeof fbuf - dlen);
359 + if (is_excluded(fbuf, 0, ALL_FILTERS)) {
360 + flags |= FLAG_SUM_KEEP;
361 + checksum_matches++;
364 + add_checksum(dirname, cp, len, file_length, mtime, ctime,
365 + sum, alt_sum, flags);
369 + clean_flist(checksum_flist, 0);
372 int push_pathname(const char *dir, int len)
375 @@ -973,34 +1280,24 @@ static struct file_struct *recv_file_ent
380 - * Create a file_struct for a named file by reading its stat()
381 - * information and performing extensive checks against global
384 - * @return the new file, or NULL if there was an error or this file
385 - * should be excluded.
386 +/* Create a file_struct for a named file by reading its stat() information
387 + * and performing extensive checks against global options.
389 - * @todo There is a small optimization opportunity here to avoid
390 - * stat()ing the file in some circumstances, which has a certain cost.
391 - * We are called immediately after doing readdir(), and so we may
392 - * already know the d_type of the file. We could for example avoid
393 - * statting directories if we're not recursing, but this is not a very
394 - * important case. Some systems may not have d_type.
396 + * Returns a pointer to the new file struct, or NULL if there was an error
397 + * or this file should be excluded. */
398 struct file_struct *make_file(const char *fname, struct file_list *flist,
399 STRUCT_STAT *stp, int flags, int filter_level)
401 static char *lastdir;
402 - static int lastdir_len = -1;
403 + static int lastdir_len = -2;
404 struct file_struct *file;
406 char thisname[MAXPATHLEN];
407 char linkname[MAXPATHLEN];
408 int alloc_len, basename_len, linkname_len;
409 int extra_len = file_extra_cnt * EXTRA_LEN;
410 const char *basename;
415 if (strlcpy(thisname, fname, sizeof thisname) >= sizeof thisname) {
416 @@ -1076,6 +1373,8 @@ struct file_struct *make_file(const char
417 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level)) {
418 if (ignore_perishable)
419 non_perishable_cnt++;
420 + if (S_ISREG(st.st_mode))
425 @@ -1114,9 +1413,16 @@ struct file_struct *make_file(const char
426 memcpy(lastdir, thisname, len);
429 + if (always_checksum && am_sender && flist)
430 + read_checksums(lastdir);
435 + if (always_checksum && am_sender && flist && lastdir_len == -2) {
437 + read_checksums("");
440 basename_len = strlen(basename) + 1; /* count the '\0' */
443 @@ -1192,11 +1498,44 @@ struct file_struct *make_file(const char
447 - if (always_checksum && am_sender && S_ISREG(st.st_mode))
448 - file_checksum(thisname, tmp_sum, st.st_size);
450 F_PATHNAME(file) = pathname;
452 + if (always_checksum && am_sender && S_ISREG(st.st_mode)) {
454 + if (flist && (j = flist_find(checksum_flist, file)) >= 0) {
455 + struct file_struct *fp = checksum_flist->sorted[j];
457 +#if SIZEOF_TIME_T == 4
458 + ctime = OPT_EXTRA(fp, LEN64_BUMP(fp) + SUM_EXTRA_CNT)->num;
460 + memcpy(&ctime, F_SUM(fp) - SIZEOF_TIME_T, SIZEOF_TIME_T);
462 + if (F_LENGTH(fp) == st.st_size
463 + && fp->modtime == st.st_mtime && ctime == st.st_ctime) {
464 + if (fp->flags & FLAG_SUM_MISSING) {
465 + fp->flags &= ~FLAG_SUM_MISSING;
466 + file_checksum(thisname, tmp_sum, st.st_size);
467 + memcpy(F_SUM(fp), tmp_sum, MAX_DIGEST_LEN);
469 + checksum_matches++;
470 + memcpy(tmp_sum, F_SUM(fp), MAX_DIGEST_LEN);
472 + fp->flags |= FLAG_SUM_KEEP;
475 + goto compute_new_checksum;
478 + compute_new_checksum:
479 + file_checksum(thisname, tmp_sum, st.st_size);
480 + if (checksum_updating && flist) {
481 + add_checksum(file->dirname, basename, basename_len,
482 + st.st_size, st.st_mtime, st.st_ctime,
483 + tmp_sum, NULL, FLAG_SUM_KEEP);
488 /* This code is only used by the receiver when it is building
489 * a list of files for a delete pass. */
490 if (keep_dirlinks && linkname_len && flist) {
491 @@ -1482,6 +1821,9 @@ static void send_directory(int f, struct
495 + if (checksum_updating && always_checksum && am_sender && f >= 0)
496 + write_checksums(NULL, 1);
498 if (f >= 0 && recurse && !divert_dirs) {
499 int i, end = flist->used - 1;
500 /* send_if_directory() bumps flist->used, so use "end". */
501 @@ -1925,7 +2267,11 @@ struct file_list *send_file_list(int f,
502 * file-list to check if this is a 1-file xfer. */
503 send_extra_file_list(f, 1);
509 + if (checksum_updating && always_checksum && flist_eof)
510 + read_checksums(NULL); /* writes any last updates */
514 @@ -2218,7 +2564,7 @@ void flist_free(struct file_list *flist)
516 if (!flist->prev || !flist_cnt)
517 pool_destroy(flist->file_pool);
519 + else if (flist->pool_boundary)
520 pool_free_old(flist->file_pool, flist->pool_boundary);
522 if (flist->sorted && flist->sorted != flist->files)
525 @@ -149,6 +149,7 @@ typedef struct
529 + BOOL checksum_updating;
532 BOOL ignore_nonreadable;
533 @@ -197,6 +198,7 @@ static service sDefault =
534 /* syslog_facility; */ LOG_DAEMON,
537 + /* checksum_updating; */ False,
538 /* fake_super; */ False,
539 /* ignore_errors; */ False,
540 /* ignore_nonreadable; */ False,
541 @@ -313,6 +315,7 @@ static struct parm_struct parm_table[] =
542 {"lock file", P_STRING, P_LOCAL, &sDefault.lock_file, NULL,0},
543 {"log file", P_STRING, P_LOCAL, &sDefault.log_file, NULL,0},
544 {"log format", P_STRING, P_LOCAL, &sDefault.log_format, NULL,0},
545 + {"checksum updating", P_BOOL, P_LOCAL, &sDefault.checksum_updating, NULL,0},
546 {"max connections", P_INTEGER,P_LOCAL, &sDefault.max_connections, NULL,0},
547 {"max verbosity", P_INTEGER,P_LOCAL, &sDefault.max_verbosity, NULL,0},
548 {"name", P_STRING, P_LOCAL, &sDefault.name, NULL,0},
549 @@ -418,6 +421,7 @@ FN_LOCAL_BOOL(lp_fake_super, fake_super)
550 FN_LOCAL_BOOL(lp_ignore_errors, ignore_errors)
551 FN_LOCAL_BOOL(lp_ignore_nonreadable, ignore_nonreadable)
552 FN_LOCAL_BOOL(lp_list, list)
553 +FN_LOCAL_BOOL(lp_checksum_updating, checksum_updating)
554 FN_LOCAL_BOOL(lp_read_only, read_only)
555 FN_LOCAL_BOOL(lp_strict_modes, strict_modes)
556 FN_LOCAL_BOOL(lp_transfer_logging, transfer_logging)
559 @@ -109,6 +109,7 @@ size_t bwlimit_writemax = 0;
560 int ignore_existing = 0;
561 int ignore_non_existing = 0;
562 int need_messages_from_generator = 0;
563 +int checksum_updating = 0;
567 @@ -302,6 +303,7 @@ void usage(enum logcode F)
568 rprintf(F," -q, --quiet suppress non-error messages\n");
569 rprintf(F," --no-motd suppress daemon-mode MOTD (see manpage caveat)\n");
570 rprintf(F," -c, --checksum skip based on checksum, not mod-time & size\n");
571 + rprintf(F," --checksum-updating sender updates .rsyncsums files\n");
572 rprintf(F," -a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)\n");
573 rprintf(F," --no-OPTION turn off an implied OPTION (e.g. --no-D)\n");
574 rprintf(F," -r, --recursive recurse into directories\n");
575 @@ -542,6 +544,7 @@ static struct poptOption long_options[]
576 {"checksum", 'c', POPT_ARG_VAL, &always_checksum, 1, 0, 0 },
577 {"no-checksum", 0, POPT_ARG_VAL, &always_checksum, 0, 0, 0 },
578 {"no-c", 0, POPT_ARG_VAL, &always_checksum, 0, 0, 0 },
579 + {"checksum-updating",0, POPT_ARG_NONE, &checksum_updating, 0, 0, 0 },
580 {"block-size", 'B', POPT_ARG_LONG, &block_size, 0, 0, 0 },
581 {"compare-dest", 0, POPT_ARG_STRING, 0, OPT_COMPARE_DEST, 0, 0 },
582 {"copy-dest", 0, POPT_ARG_STRING, 0, OPT_COPY_DEST, 0, 0 },
583 @@ -1899,7 +1902,9 @@ void server_options(char **args,int *arg
584 args[ac++] = basis_dir[i];
588 + } else if (checksum_updating)
589 + args[ac++] = "--checksum-updating";
593 args[ac++] = "--append";
596 @@ -591,6 +591,7 @@ extern int xattrs_ndx;
597 #define DEV_EXTRA_CNT 2
598 #define DIRNODE_EXTRA_CNT 3
599 #define SUM_EXTRA_CNT ((MAX_DIGEST_LEN + EXTRA_LEN - 1) / EXTRA_LEN)
600 +#define TIME_EXTRA_CNT ((SIZEOF_TIME_T + EXTRA_LEN - 1) / EXTRA_LEN)
602 #define REQ_EXTRA(f,ndx) ((union file_extras*)(f) - (ndx))
603 #define OPT_EXTRA(f,bump) ((union file_extras*)(f) - file_extra_cnt - 1 - (bump))
604 @@ -1076,6 +1077,12 @@ isDigit(const char *ptr)
608 +isXDigit(const char *ptr)
610 + return isxdigit(*(unsigned char *)ptr);
614 isPrint(const char *ptr)
616 return isprint(*(unsigned char *)ptr);
619 @@ -307,6 +307,7 @@ to the detailed description below for a
620 -q, --quiet suppress non-error messages
621 --no-motd suppress daemon-mode MOTD (see caveat)
622 -c, --checksum skip based on checksum, not mod-time & size
623 + --checksum-updating sender updates .rsyncsums files
624 -a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)
625 --no-OPTION turn off an implied OPTION (e.g. --no-D)
626 -r, --recursive recurse into directories
627 @@ -502,9 +503,9 @@ uses a "quick check" that (by default) c
628 of last modification match between the sender and receiver. This option
629 changes this to compare a 128-bit MD4 checksum for each file that has a
630 matching size. Generating the checksums means that both sides will expend
631 -a lot of disk I/O reading all the data in the files in the transfer (and
632 -this is prior to any reading that will be done to transfer changed files),
633 -so this can slow things down significantly.
634 +a lot of disk I/O reading the data in all the files in the transfer, so
635 +this can slow things down significantly (and this is prior to any reading
636 +that will be done to transfer the files that have changed).
638 The sending side generates its checksums while it is doing the file-system
639 scan that builds the list of the available files. The receiver generates
640 @@ -512,12 +513,42 @@ its checksums when it is scanning for ch
641 file that has the same size as the corresponding sender's file: files with
642 either a changed size or a changed checksum are selected for transfer.
644 +Starting with version 3.0.0, the sending side will look for a checksum
645 +summary file and use a pre-generated checksum that it reads out of the file
646 +(as long as it matches the file's size and modified time). This allows a
647 +server to support the --checksum option to clients without having to
648 +recompute the checksums for each client. See the bf(--checksum-updating)
649 +option for a way to have rsync create/update these checksum files.
651 Note that rsync always verifies that each em(transferred) file was
652 correctly reconstructed on the receiving side by checking a whole-file
653 checksum that is generated when as the file is transferred, but that
654 automatic after-the-transfer verification has nothing to do with this
655 option's before-the-transfer "Does this file need to be updated?" check.
657 +dit(bf(--checksum-updating)) This option tells the sending side to create
658 +and/or update per-directory checksum files that are used by the
659 +bf(--checksum) option. The file that is updated is named .rsyncsums. If
660 +pre-transfer checksums are not being computed, this option has no effect.
662 +The checksum files stores the computed checksum, last-known size,
663 +modification time, and name for each file in the current directory. If a
664 +later transfer finds that a file matches its prior size and modification
665 +time, the checksum is assumed to still be correct. Otherwise it is
666 +recomputed and udpated in the file.
668 +To avoid transferring the system's checksum files, you can use an exclude
669 +(e.g. bf(--exclude=.rsyncsums)). To make this easier to type, you can use
670 +a popt alias. For instance, adding the following line in your ~/.popt file
671 +defines a bf(-cc) option that enables checksum updating and excludes the
674 +verb( rsync alias --cc --checksum-updating --exclude=.rsyncsums)
676 +An rsync daemon does not allow the client to control this setting, so see
677 +the "checksum updating" daemon config option for information on how to make
678 +a daemon maintain these checksum files.
680 dit(bf(-a, --archive)) This is equivalent to bf(-rlptgoD). It is a quick
681 way of saying you want recursion and want to preserve almost
682 everything (with -H being a notable omission).
683 --- old/rsyncd.conf.yo
684 +++ new/rsyncd.conf.yo
685 @@ -198,6 +198,20 @@ locking on this file to ensure that the
686 exceeded for the modules sharing the lock file.
687 The default is tt(/var/run/rsyncd.lock).
689 +dit(bf(checksum updating)) This option tells rsync to update/create the
690 +checksum information in the per-directory checksum files when users copy
691 +files using the bf(--checksum) option. Any file that has changed since it
692 +was last checksummed (or is not mentioned) has its data updated in the
695 +Note that this updating will occur even if the module is listed as being
696 +read-only. If you want to hide these files (and you will almost always
697 +want to do), add ".rsyncsums" to the module's exclude setting.
699 +Note also that the client's command-line option, bf(--checksum-updating),
700 +has no effect on a daemon. A daemon will only update/create checksum files
701 +if this config option is true.
703 dit(bf(read only)) The "read only" option determines whether clients
704 will be able to upload files or not. If "read only" is true then any
705 attempted uploads will fail. If "read only" is false then uploads will
706 --- old/support/rsyncsums
707 +++ new/support/rsyncsums
713 +use Cwd qw(abs_path cwd);
717 +our $SUMS_FILE = '.rsyncsums';
719 +our($recurse_opt, $force_reading, $help_opt);
722 +&Getopt::Long::Configure('bundling');
723 +&usage if !&GetOptions(
724 + 'recurse|r' => \$recurse_opt,
725 + 'force|f' => \$force_reading,
726 + 'verbose|v+' => \$verbosity,
727 + 'help|h' => \$help_opt,
730 +my $start_dir = cwd();
733 +@dirs = '.' unless @dirs;
740 +my $md4 = Digest::MD4->new;
741 +my $md5 = Digest::MD5->new;
744 + my $dir = shift @dirs;
746 + if (!chdir($dir)) {
747 + warn "Unable to chdir to $dir: $!\n";
750 + if (!opendir(DP, '.')) {
751 + warn "Unable to opendir $dir: $!\n";
757 + $reldir =~ s#^$start_dir(/|$)# $1 ? '' : '.' #eo;
758 + print "$reldir ... ";
761 + my $sums_mtime = (stat($SUMS_FILE))[9];
765 + my $latest_time = 0;
766 + while (defined(my $fn = readdir(DP))) {
767 + next if $fn =~ /^\.\.?$/ || $fn =~ /^\Q$SUMS_FILE\E$/o || -l $fn;
769 + push(@subdirs, "$dir/$fn");
774 + my($size,$mtime,$ctime) = (stat(_))[7,9,10];
776 + $cache{$fn} = [ $size, $mtime, $ctime ];
779 + $latest_time = $mtime if $mtime > $latest_time;
780 + $latest_time = $ctime if $ctime > $latest_time;
785 + unshift(@dirs, sort @subdirs) if $recurse_opt;
788 + if (defined $sums_mtime) {
789 + print "(removed $SUMS_FILE) " if $verbosity;
790 + unlink($SUMS_FILE);
792 + print "empty\n" if $verbosity;
796 + if (defined($sums_mtime) && $sums_mtime == $latest_time && !$force_reading) {
797 + print "OK\n" if $verbosity;
801 + if (open(FP, '+<', $SUMS_FILE)) {
804 + my($sum4, $sum5, $size, $mtime, $ctime, $fn) = split(' ', $_, 6);
805 + my $ref = $cache{$fn};
806 + if (defined $ref) {
807 + if ($$ref[0] == $size
808 + && $$ref[1] == $mtime && $$ref[2] == $ctime
809 + && $sum4 !~ /=/ && $sum5 !~ /=/) {
814 + $$ref[3] = $$ref[4] = undef;
817 + $cnt = -1; # Force rewrite due to removed line.
821 + open(FP, '>', $SUMS_FILE) or die "Unable to write $dir/$SUMS_FILE: $!\n";
826 + print "updating\n" if $verbosity;
827 + while (my($fn, $ref) = each %cache) {
828 + next if defined $$ref[3] && defined $$ref[4];
829 + if (!open(IN, $fn)) {
830 + print STDERR "Unable to read $fn: $!\n";
831 + delete $cache{$fn};
835 + my($size,$mtime,$ctime) = (stat(IN))[7,9,10];
839 + while (sysread(IN, $_, 64*1024)) {
843 + $sum4 = $md4->hexdigest;
844 + $sum5 = $md5->hexdigest;
845 + print " $sum4 $sum5" if $verbosity > 2;
846 + print " $fn\n" if $verbosity > 1;
847 + my($size2,$mtime2,$ctime2) = (stat(IN))[7,9,10];
848 + last if $size == $size2 && $mtime == $mtime2 && $ctime == $ctime2;
857 + $cache{$fn} = [ $size, $mtime, $ctime, $sum4, $sum5 ];
862 + foreach my $fn (sort keys %cache) {
863 + my $ref = $cache{$fn};
864 + my($size, $mtime, $ctime, $sum4, $sum5) = @$ref;
865 + printf FP '%s %s %10d %10d %10d %s' . "\n", $sum4, $sum5, $size, $mtime, $ctime, $fn;
867 + $latest_time = $mtime if $mtime > $latest_time;
868 + $latest_time = $ctime if $ctime > $latest_time;
870 + truncate(FP, tell(FP));
872 + print "OK.\n" if $verbosity;
877 + utime $latest_time, $latest_time, $SUMS_FILE;
883 +Usage: rsyncsums [OPTIONS] [DIRS]
886 + -r, --recurse Update $SUMS_FILE files in subdirectories too.
887 + -f, --force Force the reading of an $SUMS_FILE file that looks to be
888 + up-to-date. (Useful for weeding out old entries.)
889 + -v, --verbose Mention what we're doing. Repeat for more info.
890 + -h, --help Display this help message.