- Renamed FLAG_SUM_FOUND FLAG_SUM_KEEP to make its meaning clearer.
[rsync/rsync-patches.git] / checksum-updating.diff
CommitLineData
ae10e51e 1This adds a sender optimization feature that allows a cache of checksums
56522462
WD
2to be used when the client specifies the --checksum option, and creates
3and/or updates the .rsyncsums files when --checksum-updating is
4specified.
ae10e51e
WD
5
6To use this patch, run these commands for a successful build:
7
8 patch -p1 <patches/checksum-updating.diff
9 ./configure (optional if already run)
10 make
11
12TODO: when sending individual files (as opposed to an entire directory),
56522462
WD
13we should still update the .rsyncsums file if we compute a new checksum.
14(The file is currently only written if we send an entire dir.)
ae10e51e
WD
15
16--- old/clientserver.c
17+++ new/clientserver.c
18@@ -37,6 +37,7 @@ extern int sanitize_paths;
19 extern int filesfrom_fd;
20 extern int remote_protocol;
21 extern int protocol_version;
22+extern int checksum_updating;
23 extern int io_timeout;
24 extern int no_detach;
25 extern int default_af_hint;
26@@ -634,6 +635,8 @@ static int rsync_module(int f_in, int f_
27 else if (am_root < 0) /* Treat --fake-super from client as --super. */
28 am_root = 2;
29
30+ checksum_updating = lp_checksum_updating(i);
31+
32 if (filesfrom_fd == 0)
33 filesfrom_fd = f_in;
34
35--- old/flist.c
36+++ new/flist.c
37@@ -25,6 +25,7 @@
38 #include "io.h"
39
40 extern int verbose;
41+extern int dry_run;
42 extern int list_only;
43 extern int am_root;
44 extern int am_server;
45@@ -57,6 +58,7 @@ extern int implied_dirs;
46 extern int file_extra_cnt;
47 extern int ignore_perishable;
48 extern int non_perishable_cnt;
49+extern int checksum_updating;
50 extern int prune_empty_dirs;
51 extern int copy_links;
52 extern int copy_unsafe_links;
56522462
WD
53@@ -79,6 +81,9 @@ extern iconv_t ic_send, ic_recv;
54
55 #define PTR_SIZE (sizeof (struct file_struct *))
56
edf38a9d
WD
57+#define FLAG_SUM_MISSING (1<<1) /* F_SUM() data is undefined */
58+#define FLAG_SUM_KEEP (1<<2) /* keep entry when rewriting */
56522462
WD
59+
60 int io_error;
61 int checksum_len;
62 dev_t filesystem_dev; /* used to implement -x */
7200c744 63@@ -101,6 +106,9 @@ static char tmp_sum[MAX_DIGEST_LEN];
ae10e51e
WD
64 static char empty_sum[MAX_DIGEST_LEN];
65 static int flist_count_offset; /* for --delete --progress */
66 static int dir_count = 0;
67+static struct file_list *checksum_flist = NULL;
68+static int checksum_matches = 0;
7200c744 69+static int regular_skipped = 0;
ae10e51e
WD
70
71 static void clean_flist(struct file_list *flist, int strip_root);
72 static void output_flist(struct file_list *flist);
edf38a9d 73@@ -317,6 +325,283 @@ static void flist_done_allocating(struct
ae10e51e
WD
74 flist->pool_boundary = ptr;
75 }
76
77+/* The len count is the length of the basename + 1 for the null. */
78+static void add_checksum(const char *dirname, const char *basename, int len,
7200c744
WD
79+ OFF_T file_length, time_t mtime, time_t ctime,
80+ const char *sum, const char *alt_sum, int flags)
ae10e51e
WD
81+{
82+ struct file_struct *file;
83+ int alloc_len, extra_len;
84+ char *bp;
85+
56522462 86+ if (len == 10+1 && *basename == '.' && strcmp(basename, ".rsyncsums") == 0)
ae10e51e
WD
87+ return;
88+
89+ if (len < 0)
90+ len = strlen(basename) + 1;
91+
7200c744
WD
92+ extra_len = (file_extra_cnt + (file_length > 0xFFFFFFFFu)
93+ + SUM_EXTRA_CNT + TIME_EXTRA_CNT)
94+ * EXTRA_LEN;
ae10e51e
WD
95+#if EXTRA_ROUNDING > 0
96+ if (extra_len & (EXTRA_ROUNDING * EXTRA_LEN))
97+ extra_len = (extra_len | (EXTRA_ROUNDING * EXTRA_LEN)) + EXTRA_LEN;
98+#endif
56522462 99+ alloc_len = FILE_STRUCT_LEN + extra_len + len + checksum_len*2 + 1;
ae10e51e
WD
100+ bp = pool_alloc(checksum_flist->file_pool, alloc_len, "add_checksum");
101+
102+ memset(bp, 0, extra_len + FILE_STRUCT_LEN);
103+ bp += extra_len;
104+ file = (struct file_struct *)bp;
105+ bp += FILE_STRUCT_LEN;
106+
107+ memcpy(bp, basename, len);
56522462
WD
108+ if (alt_sum)
109+ strlcpy(bp+len, alt_sum, checksum_len*2 + 1);
110+ else {
111+ memset(bp+len, '=', checksum_len*2);
112+ bp[len+checksum_len*2] = '\0';
113+ }
ae10e51e
WD
114+
115+ file->flags = flags;
116+ file->mode = S_IFREG;
117+ file->modtime = mtime;
118+ file->len32 = (uint32)file_length;
119+ if (file_length > 0xFFFFFFFFu) {
120+ file->flags |= FLAG_LENGTH64;
121+ OPT_EXTRA(file, 0)->unum = (uint32)(file_length >> 32);
122+ }
123+ file->dirname = dirname;
124+ bp = (char*)F_SUM(file);
125+ memcpy(bp, sum, checksum_len);
edf38a9d
WD
126+#if SIZEOF_TIME_T == 4
127+ OPT_EXTRA(file, LEN64_BUMP(file) + SUM_EXTRA_CNT)->num = ctime;
128+#else
7200c744 129+ memcpy(bp - SIZEOF_TIME_T, &ctime, SIZEOF_TIME_T);
edf38a9d 130+#endif
ae10e51e
WD
131+
132+ flist_expand(checksum_flist, 1);
133+ checksum_flist->files[checksum_flist->count++] = file;
134+
135+ checksum_flist->sorted = checksum_flist->files;
136+}
137+
138+/* The direname value must remain unchanged during the lifespan of the
139+ * created checksum_flist object because we use it directly. */
140+static void read_checksums(const char *dirname)
141+{
142+ char line[MAXPATHLEN+1024], fbuf[MAXPATHLEN], sum[MAX_DIGEST_LEN];
56522462 143+ const char *alt_sum = NULL;
ae10e51e 144+ OFF_T file_length;
7200c744 145+ time_t mtime, ctime;
ae10e51e
WD
146+ int len, dlen, i, flags;
147+ char *cp;
148+ FILE *fp;
149+
150+ if (checksum_flist) {
151+ /* Reset the pool memory and empty the file-list array. */
152+ pool_free_old(checksum_flist->file_pool,
153+ pool_boundary(checksum_flist->file_pool, 0));
154+ checksum_flist->count = 0;
155+ } else
156+ checksum_flist = flist_new(FLIST_TEMP, "read_checksums");
157+
158+ checksum_flist->low = 0;
159+ checksum_flist->high = -1;
160+ checksum_matches = 0;
7200c744 161+ regular_skipped = 0;
ae10e51e 162+
ae10e51e
WD
163+ if (dirname) {
164+ dlen = strlcpy(fbuf, dirname, sizeof fbuf);
165+ if (dlen >= (int)sizeof fbuf)
166+ return;
167+ fbuf[dlen++] = '/';
168+ } else
169+ dlen = 0;
56522462 170+ strlcpy(fbuf+dlen, ".rsyncsums", sizeof fbuf - dlen);
ae10e51e
WD
171+ if (!(fp = fopen(fbuf, "r")))
172+ return;
173+
174+ while (fgets(line, sizeof line, fp)) {
56522462
WD
175+ cp = line;
176+ if (protocol_version >= 30) {
177+ alt_sum = cp;
178+ if (*cp == '=')
179+ while (*++cp == '=') {}
180+ else
181+ while (isXDigit(cp)) cp++;
182+ if (cp - alt_sum != MD4_DIGEST_LEN*2 || *cp != ' ')
183+ break;
184+ while (*++cp == ' ') {}
185+ }
186+
187+ if (*cp == '=') {
188+ for (i = 0; i < checksum_len*2; i++, cp++) {
189+ if (*cp != '=') {
ae10e51e
WD
190+ cp = "";
191+ break;
192+ }
ae10e51e 193+ }
56522462
WD
194+ memset(sum, 0, checksum_len);
195+ flags = FLAG_SUM_MISSING;
196+ } else {
197+ for (i = 0; i < checksum_len*2; i++, cp++) {
198+ int x;
199+ if (isXDigit(cp)) {
200+ if (isDigit(cp))
201+ x = *cp - '0';
202+ else
203+ x = (*cp & 0xF) + 9;
204+ } else {
205+ cp = "";
206+ break;
207+ }
208+ if (i & 1)
209+ sum[i/2] |= x;
210+ else
211+ sum[i/2] = x << 4;
212+ }
213+ flags = 0;
ae10e51e 214+ }
ae10e51e 215+ if (*cp != ' ')
56522462 216+ break;
ae10e51e
WD
217+ while (*++cp == ' ') {}
218+
56522462
WD
219+ if (protocol_version < 30) {
220+ alt_sum = cp;
221+ if (*cp == '=')
222+ while (*++cp == '=') {}
223+ else
224+ while (isXDigit(cp)) cp++;
225+ if (cp - alt_sum != MD5_DIGEST_LEN*2 || *cp != ' ')
226+ break;
227+ while (*++cp == ' ') {}
228+ }
229+
ae10e51e
WD
230+ file_length = 0;
231+ while (isDigit(cp))
232+ file_length = file_length * 10 + *cp++ - '0';
ae10e51e 233+ if (*cp != ' ')
56522462 234+ break;
ae10e51e
WD
235+ while (*++cp == ' ') {}
236+
237+ mtime = 0;
238+ while (isDigit(cp))
239+ mtime = mtime * 10 + *cp++ - '0';
ae10e51e 240+ if (*cp != ' ')
56522462 241+ break;
ae10e51e
WD
242+ while (*++cp == ' ') {}
243+
7200c744
WD
244+ ctime = 0;
245+ while (isDigit(cp))
246+ ctime = ctime * 10 + *cp++ - '0';
247+ if (*cp != ' ')
248+ break;
249+ while (*++cp == ' ') {}
250+
ae10e51e
WD
251+ len = strlen(cp);
252+ while (len && (cp[len-1] == '\n' || cp[len-1] == '\r'))
253+ len--;
254+ if (!len)
56522462 255+ break;
ae10e51e
WD
256+ cp[len++] = '\0'; /* len now counts the null */
257+ if (strchr(cp, '/') || len > MAXPATHLEN)
56522462 258+ break;
ae10e51e
WD
259+
260+ strlcpy(fbuf+dlen, cp, sizeof fbuf - dlen);
261+ if (is_excluded(fbuf, 0, ALL_FILTERS)) {
edf38a9d 262+ flags |= FLAG_SUM_KEEP;
ae10e51e 263+ checksum_matches++;
56522462 264+ }
ae10e51e 265+
7200c744 266+ add_checksum(dirname, cp, len, file_length, mtime, ctime,
56522462 267+ sum, alt_sum, flags);
ae10e51e
WD
268+ }
269+ fclose(fp);
270+
271+ clean_flist(checksum_flist, 0);
272+}
273+
274+static void write_checksums(const char *dirname)
275+{
7200c744 276+ char fbuf[MAXPATHLEN];
56522462
WD
277+ int count = checksum_flist->count;
278+ int new_entries = count > checksum_flist->high + 1;
7200c744
WD
279+ int counts_match = count == checksum_matches;
280+ int no_skipped = regular_skipped == 0;
281+ time_t latest_time = 0;
ae10e51e
WD
282+ FILE *out_fp;
283+ int i;
284+
ae10e51e 285+ clean_flist(checksum_flist, 0);
7200c744 286+
56522462
WD
287+ checksum_flist->count = 0;
288+ checksum_matches = 0;
7200c744 289+ regular_skipped = 0;
56522462
WD
290+
291+ if (dry_run)
292+ return;
ae10e51e 293+
ae10e51e 294+ if (dirname) {
7200c744 295+ if (pathjoin(fbuf, sizeof fbuf, dirname, ".rsyncsums") >= sizeof fbuf)
ae10e51e
WD
296+ return;
297+ } else
7200c744 298+ strlcpy(fbuf, ".rsyncsums", sizeof fbuf);
ae10e51e 299+
7200c744
WD
300+ if (checksum_flist->high - checksum_flist->low < 0 && no_skipped) {
301+ unlink(fbuf);
ae10e51e
WD
302+ return;
303+ }
304+
7200c744 305+ if (!new_entries && counts_match)
ae10e51e
WD
306+ return;
307+
7200c744 308+ if (!(out_fp = fopen(fbuf, "w")))
ae10e51e
WD
309+ return;
310+
311+ for (i = checksum_flist->low; i <= checksum_flist->high; i++) {
312+ struct file_struct *file = checksum_flist->sorted[i];
313+ const char *cp = F_SUM(file);
314+ const char *end = cp + checksum_len;
7200c744 315+ time_t ctime;
edf38a9d 316+ if (!(file->flags & FLAG_SUM_KEEP))
ae10e51e 317+ continue;
edf38a9d
WD
318+#if SIZEOF_TIME_T == 4
319+ ctime = OPT_EXTRA(file, LEN64_BUMP(file) + SUM_EXTRA_CNT)->num;
320+#else
7200c744 321+ memcpy(&ctime, cp - SIZEOF_TIME_T, SIZEOF_TIME_T);
edf38a9d 322+#endif
56522462
WD
323+ if (protocol_version >= 30) {
324+ fprintf(out_fp, "%s ",
325+ file->basename + strlen(file->basename) + 1);
326+ }
327+ if (file->flags & FLAG_SUM_MISSING) {
328+ do {
329+ fprintf(out_fp, "==");
330+ } while (++cp != end);
331+ } else {
332+ do {
333+ fprintf(out_fp, "%02x", CVAL(cp, 0));
334+ } while (++cp != end);
335+ }
336+ if (protocol_version < 30) {
337+ fprintf(out_fp, " %s",
338+ file->basename + strlen(file->basename) + 1);
339+ }
edf38a9d
WD
340+ fprintf(out_fp, " %10.0f %10.0f %10.0f %s\n",
341+ (double)F_LENGTH(file), (double)file->modtime,
342+ (double)ctime, file->basename);
7200c744
WD
343+ if (file->modtime > ctime)
344+ ctime = file->modtime;
345+ if (ctime > latest_time)
346+ latest_time = ctime;
ae10e51e
WD
347+ }
348+
349+ fclose(out_fp);
7200c744
WD
350+
351+ set_modtime(fbuf, latest_time, latest_time);
ae10e51e
WD
352+}
353+
354 int push_pathname(const char *dir, int len)
355 {
356 if (dir == pathname)
edf38a9d 357@@ -973,34 +1258,24 @@ static struct file_struct *recv_file_ent
ae10e51e
WD
358 return file;
359 }
360
361-/**
362- * Create a file_struct for a named file by reading its stat()
363- * information and performing extensive checks against global
364- * options.
365- *
366- * @return the new file, or NULL if there was an error or this file
367- * should be excluded.
368+/* Create a file_struct for a named file by reading its stat() information
369+ * and performing extensive checks against global options.
370 *
371- * @todo There is a small optimization opportunity here to avoid
372- * stat()ing the file in some circumstances, which has a certain cost.
373- * We are called immediately after doing readdir(), and so we may
374- * already know the d_type of the file. We could for example avoid
375- * statting directories if we're not recursing, but this is not a very
376- * important case. Some systems may not have d_type.
377- **/
378+ * Returns a pointer to the new file struct, or NULL if there was an error
379+ * or this file should be excluded. */
380 struct file_struct *make_file(const char *fname, struct file_list *flist,
381 STRUCT_STAT *stp, int flags, int filter_level)
382 {
383 static char *lastdir;
384- static int lastdir_len = -1;
385+ static int lastdir_len = -2;
386 struct file_struct *file;
387- STRUCT_STAT st;
388 char thisname[MAXPATHLEN];
389 char linkname[MAXPATHLEN];
390 int alloc_len, basename_len, linkname_len;
391 int extra_len = file_extra_cnt * EXTRA_LEN;
392 const char *basename;
393 alloc_pool_t *pool;
394+ STRUCT_STAT st;
395 char *bp;
396
397 if (strlcpy(thisname, fname, sizeof thisname)
edf38a9d 398@@ -1077,6 +1352,8 @@ struct file_struct *make_file(const char
7200c744
WD
399 if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level)) {
400 if (ignore_perishable)
401 non_perishable_cnt++;
402+ if (S_ISREG(st.st_mode))
403+ regular_skipped++;
404 return NULL;
405 }
406
edf38a9d 407@@ -1115,9 +1392,16 @@ struct file_struct *make_file(const char
ae10e51e
WD
408 memcpy(lastdir, thisname, len);
409 lastdir[len] = '\0';
410 lastdir_len = len;
411+ if (always_checksum && am_sender && flist)
412+ read_checksums(lastdir);
413 }
414- } else
415+ } else {
416 basename = thisname;
417+ if (always_checksum && am_sender && flist && lastdir_len == -2) {
418+ lastdir_len = -1;
419+ read_checksums(NULL);
420+ }
421+ }
422 basename_len = strlen(basename) + 1; /* count the '\0' */
423
424 #ifdef SUPPORT_LINKS
edf38a9d 425@@ -1193,11 +1477,44 @@ struct file_struct *make_file(const char
ae10e51e
WD
426 }
427 #endif
428
429- if (always_checksum && am_sender && S_ISREG(st.st_mode))
430- file_checksum(thisname, tmp_sum, st.st_size);
431-
432 F_PATHNAME(file) = pathname;
433
434+ if (always_checksum && am_sender && S_ISREG(st.st_mode)) {
435+ int j;
436+ if (flist && (j = flist_find(checksum_flist, file)) >= 0) {
437+ struct file_struct *fp = checksum_flist->sorted[j];
7200c744 438+ time_t ctime;
edf38a9d
WD
439+#if SIZEOF_TIME_T == 4
440+ ctime = OPT_EXTRA(fp, LEN64_BUMP(fp) + SUM_EXTRA_CNT)->num;
441+#else
7200c744 442+ memcpy(&ctime, F_SUM(fp) - SIZEOF_TIME_T, SIZEOF_TIME_T);
edf38a9d 443+#endif
7200c744
WD
444+ if (F_LENGTH(fp) == st.st_size
445+ && fp->modtime == st.st_mtime && ctime == st.st_ctime) {
56522462
WD
446+ if (fp->flags & FLAG_SUM_MISSING) {
447+ fp->flags &= ~FLAG_SUM_MISSING;
448+ file_checksum(thisname, tmp_sum, st.st_size);
449+ memcpy((char*)F_SUM(fp), tmp_sum, MAX_DIGEST_LEN);
450+ } else {
451+ checksum_matches++;
452+ memcpy(tmp_sum, F_SUM(fp), MAX_DIGEST_LEN);
453+ }
edf38a9d 454+ fp->flags |= FLAG_SUM_KEEP;
ae10e51e
WD
455+ } else {
456+ clear_file(fp);
edf38a9d 457+ goto compute_new_checksum;
ae10e51e
WD
458+ }
459+ } else {
edf38a9d 460+ compute_new_checksum:
ae10e51e
WD
461+ file_checksum(thisname, tmp_sum, st.st_size);
462+ if (checksum_updating && flist) {
463+ add_checksum(file->dirname, basename, basename_len,
7200c744 464+ st.st_size, st.st_mtime, st.st_ctime,
edf38a9d 465+ tmp_sum, NULL, FLAG_SUM_KEEP);
ae10e51e
WD
466+ }
467+ }
468+ }
469+
470 /* This code is only used by the receiver when it is building
471 * a list of files for a delete pass. */
472 if (keep_dirlinks && linkname_len && flist) {
edf38a9d 473@@ -1241,14 +1558,14 @@ void unmake_file(struct file_struct *fil
ae10e51e
WD
474
475 static struct file_struct *send_file_name(int f, struct file_list *flist,
476 char *fname, STRUCT_STAT *stp,
477- int flags, int filter_flags)
478+ int flags, int filter_level)
479 {
480 struct file_struct *file;
481 #if defined SUPPORT_ACLS || defined SUPPORT_XATTRS
482 statx sx;
483 #endif
484
485- file = make_file(fname, flist, stp, flags, filter_flags);
486+ file = make_file(fname, flist, stp, flags, filter_level);
487 if (!file)
488 return NULL;
489
edf38a9d 490@@ -1442,7 +1759,7 @@ static void send_directory(int f, struct
ae10e51e
WD
491 DIR *d;
492 int divert_dirs = (flags & FLAG_DIVERT_DIRS) != 0;
493 int start = flist->count;
494- int filter_flags = f == -2 ? SERVER_FILTERS : ALL_FILTERS;
495+ int filter_level = f == -2 ? SERVER_FILTERS : ALL_FILTERS;
496
497 assert(flist != NULL);
498
edf38a9d 499@@ -1471,7 +1788,7 @@ static void send_directory(int f, struct
ae10e51e
WD
500 continue;
501 }
502
503- send_file_name(f, flist, fbuf, NULL, flags, filter_flags);
504+ send_file_name(f, flist, fbuf, NULL, flags, filter_level);
505 }
506
507 fbuf[len] = '\0';
edf38a9d 508@@ -1483,6 +1800,9 @@ static void send_directory(int f, struct
ae10e51e
WD
509
510 closedir(d);
511
512+ if (checksum_updating && always_checksum && am_sender && f >= 0)
513+ write_checksums(fbuf);
514+
515 if (f >= 0 && recurse && !divert_dirs) {
516 int i, end = flist->count - 1;
517 /* send_if_directory() bumps flist->count, so use "end". */
edf38a9d 518@@ -2206,7 +2526,7 @@ void flist_free(struct file_list *flist)
ae10e51e
WD
519
520 if (!flist->prev || !flist_cnt)
521 pool_destroy(flist->file_pool);
522- else
523+ else if (flist->pool_boundary)
524 pool_free_old(flist->file_pool, flist->pool_boundary);
525
526 if (flist->sorted && flist->sorted != flist->files)
edf38a9d 527@@ -2225,6 +2545,7 @@ static void clean_flist(struct file_list
ae10e51e
WD
528 if (!flist)
529 return;
530 if (flist->count == 0) {
531+ flist->low = 0;
532 flist->high = -1;
533 return;
534 }
535--- old/loadparm.c
536+++ new/loadparm.c
537@@ -149,6 +149,7 @@ typedef struct
538 int syslog_facility;
539 int timeout;
540
541+ BOOL checksum_updating;
542 BOOL fake_super;
543 BOOL ignore_errors;
544 BOOL ignore_nonreadable;
545@@ -197,6 +198,7 @@ static service sDefault =
546 /* syslog_facility; */ LOG_DAEMON,
547 /* timeout; */ 0,
548
549+ /* checksum_updating; */ False,
550 /* fake_super; */ False,
551 /* ignore_errors; */ False,
552 /* ignore_nonreadable; */ False,
553@@ -313,6 +315,7 @@ static struct parm_struct parm_table[] =
554 {"lock file", P_STRING, P_LOCAL, &sDefault.lock_file, NULL,0},
555 {"log file", P_STRING, P_LOCAL, &sDefault.log_file, NULL,0},
556 {"log format", P_STRING, P_LOCAL, &sDefault.log_format, NULL,0},
557+ {"checksum updating", P_BOOL, P_LOCAL, &sDefault.checksum_updating, NULL,0},
558 {"max connections", P_INTEGER,P_LOCAL, &sDefault.max_connections, NULL,0},
559 {"max verbosity", P_INTEGER,P_LOCAL, &sDefault.max_verbosity, NULL,0},
560 {"name", P_STRING, P_LOCAL, &sDefault.name, NULL,0},
561@@ -418,6 +421,7 @@ FN_LOCAL_BOOL(lp_fake_super, fake_super)
562 FN_LOCAL_BOOL(lp_ignore_errors, ignore_errors)
563 FN_LOCAL_BOOL(lp_ignore_nonreadable, ignore_nonreadable)
564 FN_LOCAL_BOOL(lp_list, list)
565+FN_LOCAL_BOOL(lp_checksum_updating, checksum_updating)
566 FN_LOCAL_BOOL(lp_read_only, read_only)
567 FN_LOCAL_BOOL(lp_strict_modes, strict_modes)
568 FN_LOCAL_BOOL(lp_transfer_logging, transfer_logging)
569--- old/options.c
570+++ new/options.c
571@@ -109,6 +109,7 @@ size_t bwlimit_writemax = 0;
572 int ignore_existing = 0;
573 int ignore_non_existing = 0;
574 int need_messages_from_generator = 0;
575+int checksum_updating = 0;
576 int max_delete = -1;
577 OFF_T max_size = 0;
578 OFF_T min_size = 0;
579@@ -302,6 +303,7 @@ void usage(enum logcode F)
580 rprintf(F," -q, --quiet suppress non-error messages\n");
581 rprintf(F," --no-motd suppress daemon-mode MOTD (see manpage caveat)\n");
582 rprintf(F," -c, --checksum skip based on checksum, not mod-time & size\n");
56522462 583+ rprintf(F," --checksum-updating sender updates .rsyncsums files\n");
ae10e51e
WD
584 rprintf(F," -a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)\n");
585 rprintf(F," --no-OPTION turn off an implied OPTION (e.g. --no-D)\n");
586 rprintf(F," -r, --recursive recurse into directories\n");
587@@ -542,6 +544,7 @@ static struct poptOption long_options[]
588 {"checksum", 'c', POPT_ARG_VAL, &always_checksum, 1, 0, 0 },
589 {"no-checksum", 0, POPT_ARG_VAL, &always_checksum, 0, 0, 0 },
590 {"no-c", 0, POPT_ARG_VAL, &always_checksum, 0, 0, 0 },
591+ {"checksum-updating",0, POPT_ARG_NONE, &checksum_updating, 0, 0, 0 },
592 {"block-size", 'B', POPT_ARG_LONG, &block_size, 0, 0, 0 },
593 {"compare-dest", 0, POPT_ARG_STRING, 0, OPT_COMPARE_DEST, 0, 0 },
594 {"copy-dest", 0, POPT_ARG_STRING, 0, OPT_COPY_DEST, 0, 0 },
595@@ -1896,7 +1899,9 @@ void server_options(char **args,int *arg
596 args[ac++] = basis_dir[i];
597 }
598 }
599- }
600+ } else if (checksum_updating)
601+ args[ac++] = "--checksum-updating";
602+
603
604 if (append_mode)
605 args[ac++] = "--append";
606--- old/rsync.h
607+++ new/rsync.h
7200c744
WD
608@@ -589,6 +589,7 @@ extern int preserve_xattrs;
609 #define EXTRA_LEN (sizeof (union file_extras))
610 #define PTR_EXTRA_LEN ((sizeof (char *) + EXTRA_LEN - 1) / EXTRA_LEN)
611 #define SUM_EXTRA_CNT ((MAX_DIGEST_LEN + EXTRA_LEN - 1) / EXTRA_LEN)
612+#define TIME_EXTRA_CNT ((SIZEOF_TIME_T + EXTRA_LEN - 1) / EXTRA_LEN)
613
614 #define REQ_EXTRA(f,ndx) ((union file_extras*)(f) - (ndx))
615 #define OPT_EXTRA(f,bump) ((union file_extras*)(f) - file_extra_cnt - 1 - (bump))
616@@ -1070,6 +1071,12 @@ isDigit(const char *ptr)
ae10e51e
WD
617 }
618
619 static inline int
56522462 620+isXDigit(const char *ptr)
ae10e51e 621+{
56522462 622+ return isxdigit(*(unsigned char *)ptr);
ae10e51e
WD
623+}
624+
625+static inline int
626 isPrint(const char *ptr)
627 {
628 return isprint(*(unsigned char *)ptr);
629--- old/rsync.yo
630+++ new/rsync.yo
631@@ -307,6 +307,7 @@ to the detailed description below for a
632 -q, --quiet suppress non-error messages
633 --no-motd suppress daemon-mode MOTD (see caveat)
634 -c, --checksum skip based on checksum, not mod-time & size
56522462 635+ --checksum-updating sender updates .rsyncsums files
ae10e51e
WD
636 -a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)
637 --no-OPTION turn off an implied OPTION (e.g. --no-D)
638 -r, --recursive recurse into directories
639@@ -502,9 +503,9 @@ uses a "quick check" that (by default) c
640 of last modification match between the sender and receiver. This option
641 changes this to compare a 128-bit MD4 checksum for each file that has a
642 matching size. Generating the checksums means that both sides will expend
643-a lot of disk I/O reading all the data in the files in the transfer (and
644-this is prior to any reading that will be done to transfer changed files),
645-so this can slow things down significantly.
646+a lot of disk I/O reading the data in all the files in the transfer, so
647+this can slow things down significantly (and this is prior to any reading
648+that will be done to transfer the files that have changed).
649
650 The sending side generates its checksums while it is doing the file-system
651 scan that builds the list of the available files. The receiver generates
56522462 652@@ -512,12 +513,42 @@ its checksums when it is scanning for ch
ae10e51e
WD
653 file that has the same size as the corresponding sender's file: files with
654 either a changed size or a changed checksum are selected for transfer.
655
656+Starting with version 3.0.0, the sending side will look for a checksum
657+summary file and use a pre-generated checksum that it reads out of the file
658+(as long as it matches the file's size and modified time). This allows a
659+server to support the --checksum option to clients without having to
660+recompute the checksums for each client. See the bf(--checksum-updating)
56522462 661+option for a way to have rsync create/update these checksum files.
ae10e51e
WD
662+
663 Note that rsync always verifies that each em(transferred) file was
664 correctly reconstructed on the receiving side by checking a whole-file
665 checksum that is generated when as the file is transferred, but that
666 automatic after-the-transfer verification has nothing to do with this
667 option's before-the-transfer "Does this file need to be updated?" check.
668
669+dit(bf(--checksum-updating)) This option tells the sending side to create
670+and/or update per-directory checksum files that are used by the
56522462
WD
671+bf(--checksum) option. The file that is updated is named .rsyncsums. If
672+pre-transfer checksums are not being computed, this option has no effect.
ae10e51e
WD
673+
674+The checksum files stores the computed checksum, last-known size,
675+modification time, and name for each file in the current directory. If a
676+later transfer finds that a file matches its prior size and modification
677+time, the checksum is assumed to still be correct. Otherwise it is
678+recomputed and udpated in the file.
679+
680+To avoid transferring the system's checksum files, you can use an exclude
56522462 681+(e.g. bf(--exclude=.rsyncsums)). To make this easier to type, you can use
ae10e51e
WD
682+a popt alias. For instance, adding the following line in your ~/.popt file
683+defines a bf(-cc) option that enables checksum updating and excludes the
684+checksum files:
685+
56522462 686+verb( rsync alias --cc --checksum-updating --exclude=.rsyncsums)
ae10e51e
WD
687+
688+An rsync daemon does not allow the client to control this setting, so see
689+the "checksum updating" daemon config option for information on how to make
690+a daemon maintain these checksum files.
691+
692 dit(bf(-a, --archive)) This is equivalent to bf(-rlptgoD). It is a quick
693 way of saying you want recursion and want to preserve almost
694 everything (with -H being a notable omission).
695--- old/rsyncd.conf.yo
696+++ new/rsyncd.conf.yo
56522462 697@@ -198,6 +198,20 @@ locking on this file to ensure that the
ae10e51e
WD
698 exceeded for the modules sharing the lock file.
699 The default is tt(/var/run/rsyncd.lock).
700
701+dit(bf(checksum updating)) This option tells rsync to update/create the
702+checksum information in the per-directory checksum files when users copy
703+files using the bf(--checksum) option. Any file that has changed since it
704+was last checksummed (or is not mentioned) has its data updated in the
56522462 705+.rsyncsums file.
ae10e51e
WD
706+
707+Note that this updating will occur even if the module is listed as being
708+read-only. If you want to hide these files (and you will almost always
56522462 709+want to do), add ".rsyncsums" to the module's exclude setting.
ae10e51e
WD
710+
711+Note also that the client's command-line option, bf(--checksum-updating),
712+has no effect on a daemon. A daemon will only update/create checksum files
713+if this config option is true.
714+
715 dit(bf(read only)) The "read only" option determines whether clients
716 will be able to upload files or not. If "read only" is true then any
717 attempted uploads will fail. If "read only" is false then uploads will
7200c744
WD
718--- old/support/rsyncsums
719+++ new/support/rsyncsums
edf38a9d 720@@ -0,0 +1,185 @@
7200c744
WD
721+#!/usr/bin/perl -w
722+use strict;
723+
724+use Getopt::Long;
725+use Cwd qw(abs_path cwd);
726+use Digest::MD4;
727+use Digest::MD5;
728+
729+our $SUMS_FILE = '.rsyncsums';
730+
731+our($recurse_opt, $force_reading, $help_opt);
732+our $verbosity = 0;
733+
734+&Getopt::Long::Configure('bundling');
735+&usage if !&GetOptions(
736+ 'recurse|r' => \$recurse_opt,
737+ 'force|f' => \$force_reading,
738+ 'verbose|v+' => \$verbosity,
739+ 'help|h' => \$help_opt,
740+) || $help_opt;
741+
742+my $start_dir = cwd();
743+
744+my @dirs = @ARGV;
745+@dirs = '.' unless @dirs;
746+foreach (@dirs) {
747+ $_ = abs_path($_);
748+}
749+
750+$| = 1;
751+
752+my $md4 = Digest::MD4->new;
753+my $md5 = Digest::MD5->new;
754+
755+while (@dirs) {
756+ my $dir = shift @dirs;
757+
758+ if (!chdir($dir)) {
759+ warn "Unable to chdir to $dir: $!\n";
760+ next;
761+ }
762+ if (!opendir(DP, '.')) {
763+ warn "Unable to opendir $dir: $!\n";
764+ next;
765+ }
766+
767+ if ($verbosity) {
768+ my $reldir = $dir;
769+ $reldir =~ s#^$start_dir(/|$)# $1 ? '' : '.' #eo;
770+ print "$reldir ... ";
771+ }
772+
773+ my $sums_mtime = (stat($SUMS_FILE))[9];
774+ my %cache;
775+ my @fp;
776+ my @subdirs;
777+ my $cnt = 0;
778+ my $latest_time = 0;
779+ while (defined(my $fn = readdir(DP))) {
780+ next if $fn =~ /^\.\.?$/ || $fn =~ /^\Q$SUMS_FILE\E$/o || -l $fn;
781+ if (-d _) {
782+ push(@subdirs, "$dir/$fn");
783+ next;
784+ }
785+ next unless -f _;
786+
787+ my($size,$mtime,$ctime) = (stat(_))[7,9,10];
788+
789+ $cache{$fn} = [ $size, $mtime, $ctime ];
790+ $cnt++;
791+
792+ $latest_time = $mtime if $mtime > $latest_time;
793+ $latest_time = $ctime if $ctime > $latest_time;
794+ }
795+
796+ closedir DP;
797+
798+ unshift(@dirs, sort @subdirs) if $recurse_opt;
799+
800+ if (!$cnt) {
801+ if (defined $sums_mtime) {
802+ print "(removed $SUMS_FILE) " if $verbosity;
803+ unlink($SUMS_FILE);
804+ }
805+ print "empty\n" if $verbosity;
806+ next;
807+ }
808+
809+ if (defined($sums_mtime) && $sums_mtime == $latest_time && !$force_reading) {
810+ print "OK\n" if $verbosity;
811+ next;
812+ }
813+
814+ if (open(FP, '+<', $SUMS_FILE)) {
815+ while (<FP>) {
816+ chomp;
817+ my($sum4, $sum5, $size, $mtime, $ctime, $fn) = split(' ', $_, 6);
818+ my $ref = $cache{$fn};
819+ if (defined $ref) {
820+ if ($$ref[0] == $size
821+ && $$ref[1] == $mtime && $$ref[2] == $ctime
822+ && $sum4 !~ /=/ && $sum5 !~ /=/) {
823+ $$ref[3] = $sum4;
824+ $$ref[4] = $sum5;
825+ $cnt--;
826+ } else {
827+ $$ref[3] = $$ref[4] = undef;
828+ }
829+ } else {
830+ $cnt = -1; # Force rewrite due to removed line.
831+ }
832+ }
833+ } else {
834+ open(FP, '>', $SUMS_FILE) or die "Unable to write $dir/$SUMS_FILE: $!\n";
835+ $cnt = -1;
836+ }
837+
838+ if ($cnt) {
839+ print "updating\n" if $verbosity;
840+ while (my($fn, $ref) = each %cache) {
841+ next if defined $$ref[3] && defined $$ref[4];
842+ if (!open(IN, $fn)) {
843+ print STDERR "Unable to read $fn: $!\n";
844+ delete $cache{$fn};
845+ next;
846+ }
847+
848+ my($size,$mtime,$ctime) = (stat(IN))[7,9,10];
849+ my($sum4, $sum5);
850+
851+ while (1) {
852+ while (sysread(IN, $_, 64*1024)) {
853+ $md4->add($_);
854+ $md5->add($_);
855+ }
856+ $sum4 = $md4->hexdigest;
857+ $sum5 = $md5->hexdigest;
edf38a9d
WD
858+ print " $sum4 $sum5" if $verbosity > 2;
859+ print " $fn\n" if $verbosity > 1;
7200c744
WD
860+ my($size2,$mtime2,$ctime2) = (stat(IN))[7,9,10];
861+ last if $size == $size2 && $mtime == $mtime2 && $ctime == $ctime2;
862+ $size = $size2;
863+ $mtime = $mtime2;
864+ $ctime = $ctime2;
865+ sysseek(IN, 0, 0);
866+ }
867+
868+ close IN;
869+
870+ $cache{$fn} = [ $size, $mtime, $ctime, $sum4, $sum5 ];
871+ }
872+
873+ $latest_time = 0;
874+ seek(FP, 0, 0);
875+ foreach my $fn (sort keys %cache) {
876+ my $ref = $cache{$fn};
877+ my($size, $mtime, $ctime, $sum4, $sum5) = @$ref;
53243b17 878+ printf FP '%s %s %10d %10d %10d %s' . "\n", $sum4, $sum5, $size, $mtime, $ctime, $fn;
7200c744
WD
879+
880+ $latest_time = $mtime if $mtime > $latest_time;
881+ $latest_time = $ctime if $ctime > $latest_time;
882+ }
883+ truncate(FP, tell(FP));
884+ } else {
885+ print "OK.\n" if $verbosity;
886+ }
887+
888+ close FP;
889+
890+ utime $latest_time, $latest_time, $SUMS_FILE;
891+}
892+
893+sub usage
894+{
895+ die <<EOT;
896+Usage: rsyncsums [OPTIONS] [DIRS]
897+
898+Options:
899+ -r, --recurse Update $SUMS_FILE files in subdirectories too.
900+ -f, --force Force the reading of an $SUMS_FILE file that looks to be
901+ up-to-date. (Useful for weeding out old entries.)
902+ -v, --verbose Mention what we're doing. Repeat for more info.
903+ -h, --help Display this help message.
904+EOT
905+}