Commit | Line | Data |
---|---|---|
1e4f48d6 | 1 | /* |
c627d613 AT |
2 | Copyright (C) Andrew Tridgell 1996 |
3 | Copyright (C) Paul Mackerras 1996 | |
1e4f48d6 | 4 | |
c627d613 AT |
5 | This program is free software; you can redistribute it and/or modify |
6 | it under the terms of the GNU General Public License as published by | |
7 | the Free Software Foundation; either version 2 of the License, or | |
8 | (at your option) any later version. | |
1e4f48d6 | 9 | |
c627d613 AT |
10 | This program is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU General Public License for more details. | |
1e4f48d6 | 14 | |
c627d613 AT |
15 | You should have received a copy of the GNU General Public License |
16 | along with this program; if not, write to the Free Software | |
17 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
18 | */ | |
19 | ||
20 | #include "rsync.h" | |
21 | ||
22 | extern int verbose; | |
16417f8b | 23 | extern int do_progress; |
ba582f75 | 24 | extern int checksum_seed; |
6cc11982 | 25 | extern int append_mode; |
a36ffd39 WD |
26 | |
27 | int updating_basis_file; | |
c627d613 | 28 | |
c627d613 | 29 | static int false_alarms; |
900cfcb5 | 30 | static int hash_hits; |
c627d613 | 31 | static int matches; |
a800434a | 32 | static int64 data_transfer; |
c627d613 | 33 | |
3a6a366f | 34 | static int total_false_alarms; |
900cfcb5 | 35 | static int total_hash_hits; |
3a6a366f | 36 | static int total_matches; |
c627d613 | 37 | |
a800434a | 38 | extern struct stats stats; |
c627d613 | 39 | |
d9bca0c3 WD |
40 | static uint32 tablesize; |
41 | static int32 *sum_table; | |
c627d613 | 42 | |
900cfcb5 | 43 | #define GETTAG(sum) ((sum)%tablesize) |
c627d613 AT |
44 | |
45 | static void build_hash_table(struct sum_struct *s) | |
46 | { | |
7cacd47e | 47 | int32 i; |
d9bca0c3 WD |
48 | uint32 prior_size = tablesize; |
49 | ||
50 | /* Dynamically calculate the hash table size so that the hash load | |
51 | * for big files is about 80%. This number must be odd or s2 will | |
52 | * not be able to span the entire set. */ | |
53 | tablesize = (uint32)(s->count/8) * 10 + 11; | |
54 | if (tablesize < 65537) | |
55 | tablesize = 65537; /* a prime number */ | |
56 | if (tablesize != prior_size) { | |
57 | free(sum_table); | |
58 | sum_table = new_array(int32, tablesize); | |
59 | if (!sum_table) | |
60 | out_of_memory("build_hash_table"); | |
61 | } | |
c627d613 | 62 | |
d9bca0c3 | 63 | memset(sum_table, 0xFF, tablesize * sizeof sum_table[0]); |
c627d613 | 64 | |
0e36d9da | 65 | for (i = 0; i < s->count; i++) { |
900cfcb5 | 66 | uint32 t = GETTAG(s->sums[i].sum1); |
d9bca0c3 WD |
67 | s->sums[i].chain = sum_table[t]; |
68 | sum_table[t] = i; | |
1e4f48d6 | 69 | } |
c627d613 AT |
70 | } |
71 | ||
72 | ||
bcacc18b | 73 | static OFF_T last_match; |
c627d613 AT |
74 | |
75 | ||
f5f95a38 MP |
76 | /** |
77 | * Transmit a literal and/or match token. | |
78 | * | |
79 | * This delightfully-named function is called either when we find a | |
80 | * match and need to transmit all the unmatched data leading up to it, | |
81 | * or when we get bored of accumulating literal data and just need to | |
82 | * transmit it. As a result of this second case, it is called even if | |
83 | * we have not matched at all! | |
84 | * | |
85 | * @param i If >0, the number of a matched token. If 0, indicates we | |
86 | * have only literal data. | |
87 | **/ | |
a06b419d | 88 | static void matched(int f, struct sum_struct *s, struct map_struct *buf, |
37a56445 | 89 | OFF_T offset, int32 i) |
c627d613 | 90 | { |
a06b419d WD |
91 | int32 n = offset - last_match; /* max value: block_size (int32) */ |
92 | int32 j; | |
93 | ||
94 | if (verbose > 2 && i >= 0) { | |
95 | rprintf(FINFO, | |
96 | "match at %.0f last_match=%.0f j=%d len=%ld n=%ld\n", | |
97 | (double)offset, (double)last_match, i, | |
98 | (long)s->sums[i].len, (long)n); | |
99 | } | |
c627d613 | 100 | |
a06b419d | 101 | send_token(f, i, buf, last_match, n, i < 0 ? 0 : s->sums[i].len); |
e7ebc36c | 102 | data_transfer += n; |
70d794dc | 103 | |
a800434a AT |
104 | if (i >= 0) { |
105 | stats.matched_data += s->sums[i].len; | |
e7ebc36c | 106 | n += s->sums[i].len; |
a800434a | 107 | } |
1e4f48d6 WD |
108 | |
109 | for (j = 0; j < n; j += CHUNK_SIZE) { | |
a06b419d WD |
110 | int32 n1 = MIN(CHUNK_SIZE, n - j); |
111 | sum_update(map_ptr(buf, last_match + j, n1), n1); | |
e7ebc36c | 112 | } |
9e31c482 | 113 | |
0d0e2e93 | 114 | if (i >= 0) |
e7ebc36c | 115 | last_match = offset + s->sums[i].len; |
0d0e2e93 AT |
116 | else |
117 | last_match = offset; | |
eb86d661 | 118 | |
0394e34a | 119 | if (buf && do_progress) |
4440b8aa | 120 | show_progress(last_match, buf->file_size); |
9e31c482 AT |
121 | } |
122 | ||
123 | ||
c6e7fcb4 | 124 | static void hash_search(int f,struct sum_struct *s, |
da9d12f5 | 125 | struct map_struct *buf, OFF_T len) |
c627d613 | 126 | { |
7560c17a | 127 | OFF_T offset, end, backup; |
7cacd47e | 128 | int32 k, want_i; |
e7ebc36c | 129 | char sum2[SUM_LENGTH]; |
1e4f48d6 | 130 | uint32 s1, s2, sum; |
7560c17a | 131 | int more; |
debb4505 | 132 | schar *map; |
e7ebc36c | 133 | |
a04d77bc WD |
134 | /* want_i is used to encourage adjacent matches, allowing the RLL |
135 | * coding of the output to work more efficiently. */ | |
136 | want_i = 0; | |
923fa978 | 137 | |
0e36d9da | 138 | if (verbose > 2) { |
a06b419d WD |
139 | rprintf(FINFO, "hash search b=%ld len=%.0f\n", |
140 | (long)s->blength, (double)len); | |
0e36d9da | 141 | } |
e7ebc36c | 142 | |
a06b419d | 143 | k = (int32)MIN(len, (OFF_T)s->blength); |
1e4f48d6 | 144 | |
0e36d9da | 145 | map = (schar *)map_ptr(buf, 0, k); |
1e4f48d6 | 146 | |
e7ebc36c AT |
147 | sum = get_checksum1((char *)map, k); |
148 | s1 = sum & 0xFFFF; | |
149 | s2 = sum >> 16; | |
150 | if (verbose > 3) | |
a06b419d | 151 | rprintf(FINFO, "sum=%.8x k=%ld\n", sum, (long)k); |
1e4f48d6 | 152 | |
e7ebc36c | 153 | offset = 0; |
1e4f48d6 | 154 | |
e7ebc36c | 155 | end = len + 1 - s->sums[s->count-1].len; |
1e4f48d6 | 156 | |
0e36d9da | 157 | if (verbose > 3) { |
a06b419d WD |
158 | rprintf(FINFO, "hash search s->blength=%ld len=%.0f count=%.0f\n", |
159 | (long)s->blength, (double)len, (double)s->count); | |
0e36d9da | 160 | } |
1e4f48d6 | 161 | |
e7ebc36c | 162 | do { |
e7ebc36c | 163 | int done_csum2 = 0; |
d9bca0c3 | 164 | int32 i; |
1e4f48d6 | 165 | |
e7ebc36c | 166 | if (verbose > 4) |
5f808dfb | 167 | rprintf(FINFO,"offset=%.0f sum=%08x\n",(double)offset,sum); |
1e4f48d6 | 168 | |
c64ca7ef | 169 | i = sum_table[GETTAG(sum)]; |
900cfcb5 WD |
170 | if (i < 0) |
171 | goto null_hash; | |
172 | ||
173 | hash_hits++; | |
174 | do { | |
d9bca0c3 | 175 | int32 l; |
1e4f48d6 | 176 | |
0e36d9da WD |
177 | if (sum != s->sums[i].sum1) |
178 | continue; | |
1e4f48d6 | 179 | |
536541d5 | 180 | /* also make sure the two blocks are the same length */ |
a06b419d | 181 | l = (int32)MIN((OFF_T)s->blength, len-offset); |
0e36d9da WD |
182 | if (l != s->sums[i].len) |
183 | continue; | |
536541d5 | 184 | |
a36ffd39 | 185 | /* in-place: ensure chunk's offset is either >= our |
a3221d2a | 186 | * offset or that the data didn't move. */ |
a36ffd39 | 187 | if (updating_basis_file && s->sums[i].offset < offset |
a3221d2a WD |
188 | && !(s->sums[i].flags & SUMFLG_SAME_OFFSET)) |
189 | continue; | |
190 | ||
d9bca0c3 WD |
191 | if (verbose > 3) { |
192 | rprintf(FINFO, | |
193 | "potential match at %.0f i=%ld sum=%08x\n", | |
194 | (double)offset, (long)i, sum); | |
195 | } | |
1e4f48d6 | 196 | |
e7ebc36c | 197 | if (!done_csum2) { |
debb4505 | 198 | map = (schar *)map_ptr(buf,offset,l); |
e7ebc36c AT |
199 | get_checksum2((char *)map,l,sum2); |
200 | done_csum2 = 1; | |
201 | } | |
1e4f48d6 | 202 | |
fc0257c9 | 203 | if (memcmp(sum2,s->sums[i].sum2,s->s2length) != 0) { |
e7ebc36c AT |
204 | false_alarms++; |
205 | continue; | |
206 | } | |
923fa978 | 207 | |
a36ffd39 | 208 | /* When updating in-place, the best possible match is |
a3221d2a WD |
209 | * one with an identical offset, so we prefer that over |
210 | * the following want_i optimization. */ | |
a36ffd39 | 211 | if (updating_basis_file) { |
d9bca0c3 WD |
212 | int32 i2; |
213 | for (i2 = i; i2 >= 0; i2 = s->sums[i2].chain) { | |
a3221d2a WD |
214 | if (s->sums[i2].offset != offset) |
215 | continue; | |
216 | if (i2 != i) { | |
217 | if (sum != s->sums[i2].sum1) | |
218 | break; | |
219 | if (memcmp(sum2, s->sums[i2].sum2, | |
220 | s->s2length) != 0) | |
221 | break; | |
222 | i = i2; | |
223 | } | |
224 | /* This chunk was at the same offset on | |
225 | * both the sender and the receiver. */ | |
226 | s->sums[i].flags |= SUMFLG_SAME_OFFSET; | |
227 | goto set_want_i; | |
d9bca0c3 | 228 | } |
a3221d2a WD |
229 | } |
230 | ||
923fa978 | 231 | /* we've found a match, but now check to see |
a04d77bc WD |
232 | * if want_i can hint at a better match. */ |
233 | if (i != want_i && want_i < s->count | |
a36ffd39 | 234 | && (!updating_basis_file || s->sums[want_i].offset >= offset |
a3221d2a | 235 | || s->sums[want_i].flags & SUMFLG_SAME_OFFSET) |
a04d77bc WD |
236 | && sum == s->sums[want_i].sum1 |
237 | && memcmp(sum2, s->sums[want_i].sum2, s->s2length) == 0) { | |
5e252dea WD |
238 | /* we've found an adjacent match - the RLL coder |
239 | * will be happy */ | |
a04d77bc | 240 | i = want_i; |
923fa978 | 241 | } |
a3221d2a | 242 | set_want_i: |
a04d77bc | 243 | want_i = i + 1; |
1e4f48d6 | 244 | |
e7ebc36c AT |
245 | matched(f,s,buf,offset,i); |
246 | offset += s->sums[i].len - 1; | |
a06b419d | 247 | k = (int32)MIN((OFF_T)s->blength, len-offset); |
0e36d9da | 248 | map = (schar *)map_ptr(buf, offset, k); |
e7ebc36c AT |
249 | sum = get_checksum1((char *)map, k); |
250 | s1 = sum & 0xFFFF; | |
251 | s2 = sum >> 16; | |
252 | matches++; | |
253 | break; | |
900cfcb5 | 254 | } while ((i = s->sums[i].chain) >= 0); |
1e4f48d6 | 255 | |
900cfcb5 | 256 | null_hash: |
7560c17a WD |
257 | backup = offset - last_match; |
258 | /* We sometimes read 1 byte prior to last_match... */ | |
259 | if (backup < 0) | |
260 | backup = 0; | |
261 | ||
e7ebc36c | 262 | /* Trim off the first byte from the checksum */ |
7560c17a WD |
263 | more = offset + k < len; |
264 | map = (schar *)map_ptr(buf, offset - backup, k + more + backup) | |
265 | + backup; | |
e7ebc36c AT |
266 | s1 -= map[0] + CHAR_OFFSET; |
267 | s2 -= k * (map[0]+CHAR_OFFSET); | |
1e4f48d6 | 268 | |
e7ebc36c | 269 | /* Add on the next byte (if there is one) to the checksum */ |
7560c17a WD |
270 | if (more) { |
271 | s1 += map[k] + CHAR_OFFSET; | |
e7ebc36c | 272 | s2 += s1; |
0e36d9da | 273 | } else |
e7ebc36c | 274 | --k; |
900cfcb5 | 275 | sum = (s1 & 0xffff) | (s2 << 16); |
45f133b9 | 276 | |
5914bf15 PM |
277 | /* By matching early we avoid re-reading the |
278 | data 3 times in the case where a token | |
279 | match comes a long way after last | |
280 | match. The 3 reads are caused by the | |
281 | running match, the checksum update and the | |
282 | literal send. */ | |
a06b419d WD |
283 | if (backup >= s->blength+CHUNK_SIZE && end-offset > CHUNK_SIZE) |
284 | matched(f, s, buf, offset - s->blength, -2); | |
e7ebc36c | 285 | } while (++offset < end); |
1e4f48d6 | 286 | |
a06b419d WD |
287 | matched(f, s, buf, len, -1); |
288 | map_ptr(buf, len-1, 1); | |
c627d613 AT |
289 | } |
290 | ||
291 | ||
f5f95a38 MP |
292 | /** |
293 | * Scan through a origin file, looking for sections that match | |
294 | * checksums from the generator, and transmit either literal or token | |
295 | * data. | |
296 | * | |
538ba24f MP |
297 | * Also calculates the MD4 checksum of the whole file, using the md |
298 | * accumulator. This is transmitted with the file as protection | |
299 | * against corruption on the wire. | |
300 | * | |
f5f95a38 MP |
301 | * @param s Checksums received from the generator. If <tt>s->count == |
302 | * 0</tt>, then there are actually no checksums for this file. | |
303 | * | |
304 | * @param len Length of the file to send. | |
305 | **/ | |
306 | void match_sums(int f, struct sum_struct *s, struct map_struct *buf, OFF_T len) | |
c627d613 | 307 | { |
e7ebc36c AT |
308 | char file_sum[MD4_SUM_LENGTH]; |
309 | ||
310 | last_match = 0; | |
311 | false_alarms = 0; | |
900cfcb5 | 312 | hash_hits = 0; |
1e4f48d6 WD |
313 | matches = 0; |
314 | data_transfer = 0; | |
e7ebc36c | 315 | |
ba582f75 | 316 | sum_init(checksum_seed); |
e7ebc36c | 317 | |
6cc11982 WD |
318 | if (append_mode) { |
319 | OFF_T j = 0; | |
320 | for (j = CHUNK_SIZE; j < s->flength; j += CHUNK_SIZE) { | |
1f86fcf5 WD |
321 | if (buf && do_progress) |
322 | show_progress(last_match, buf->file_size); | |
6cc11982 WD |
323 | sum_update(map_ptr(buf, last_match, CHUNK_SIZE), |
324 | CHUNK_SIZE); | |
325 | last_match = j; | |
326 | } | |
327 | if (last_match < s->flength) { | |
328 | int32 len = s->flength - last_match; | |
1f86fcf5 WD |
329 | if (buf && do_progress) |
330 | show_progress(last_match, buf->file_size); | |
6cc11982 WD |
331 | sum_update(map_ptr(buf, last_match, len), len); |
332 | last_match = s->flength; | |
333 | } | |
334 | s->count = 0; | |
335 | } | |
336 | ||
a06b419d | 337 | if (len > 0 && s->count > 0) { |
e7ebc36c | 338 | build_hash_table(s); |
1e4f48d6 WD |
339 | |
340 | if (verbose > 2) | |
9486289c | 341 | rprintf(FINFO,"built hash table\n"); |
1e4f48d6 | 342 | |
e7ebc36c | 343 | hash_search(f,s,buf,len); |
1e4f48d6 WD |
344 | |
345 | if (verbose > 2) | |
9486289c | 346 | rprintf(FINFO,"done hash search\n"); |
e7ebc36c | 347 | } else { |
eb86d661 AT |
348 | OFF_T j; |
349 | /* by doing this in pieces we avoid too many seeks */ | |
6cc11982 | 350 | for (j = last_match + CHUNK_SIZE; j < len; j += CHUNK_SIZE) |
a06b419d WD |
351 | matched(f, s, buf, j, -2); |
352 | matched(f, s, buf, len, -1); | |
e7ebc36c | 353 | } |
9e31c482 | 354 | |
e7ebc36c | 355 | sum_end(file_sum); |
edecdad5 WD |
356 | /* If we had a read error, send a bad checksum. */ |
357 | if (buf && buf->status != 0) | |
358 | file_sum[0]++; | |
c627d613 | 359 | |
bc63ae3f S |
360 | if (verbose > 2) |
361 | rprintf(FINFO,"sending file_sum\n"); | |
362 | write_buf(f,file_sum,MD4_SUM_LENGTH); | |
c627d613 | 363 | |
e7ebc36c | 364 | if (verbose > 2) |
900cfcb5 WD |
365 | rprintf(FINFO, "false_alarms=%d hash_hits=%d matches=%d\n", |
366 | false_alarms, hash_hits, matches); | |
1e4f48d6 | 367 | |
900cfcb5 | 368 | total_hash_hits += hash_hits; |
e7ebc36c AT |
369 | total_false_alarms += false_alarms; |
370 | total_matches += matches; | |
a800434a | 371 | stats.literal_data += data_transfer; |
c627d613 AT |
372 | } |
373 | ||
374 | void match_report(void) | |
375 | { | |
e7ebc36c AT |
376 | if (verbose <= 1) |
377 | return; | |
c627d613 | 378 | |
9486289c | 379 | rprintf(FINFO, |
900cfcb5 WD |
380 | "total: matches=%d hash_hits=%d false_alarms=%d data=%.0f\n", |
381 | total_matches, total_hash_hits, total_false_alarms, | |
a800434a | 382 | (double)stats.literal_data); |
c627d613 | 383 | } |