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