- Fixed a problem with the new gettag2() macro by getting rid of the
[rsync/rsync.git] / match.c
1 /*
2    Copyright (C) Andrew Tridgell 1996
3    Copyright (C) Paul Mackerras 1996
4
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.
9
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.
14
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;
23 extern int do_progress;
24 extern int checksum_seed;
25 extern int append_mode;
26
27 int updating_basis_file;
28
29 static int false_alarms;
30 static int hash_hits;
31 static int matches;
32 static int64 data_transfer;
33
34 static int total_false_alarms;
35 static int total_hash_hits;
36 static int total_matches;
37
38 extern struct stats stats;
39
40 static uint32 tablesize;
41 static int32 *sum_table;
42
43 #define GETTAG(sum) ((sum)%tablesize)
44
45 static void build_hash_table(struct sum_struct *s)
46 {
47         int32 i;
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         }
62
63         memset(sum_table, 0xFF, tablesize * sizeof sum_table[0]);
64
65         for (i = 0; i < s->count; i++) {
66                 uint32 t = GETTAG(s->sums[i].sum1);
67                 s->sums[i].chain = sum_table[t];
68                 sum_table[t] = i;
69         }
70 }
71
72
73 static OFF_T last_match;
74
75
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  **/
88 static void matched(int f, struct sum_struct *s, struct map_struct *buf,
89                     OFF_T offset, int32 i)
90 {
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         }
100
101         send_token(f, i, buf, last_match, n, i < 0 ? 0 : s->sums[i].len);
102         data_transfer += n;
103
104         if (i >= 0) {
105                 stats.matched_data += s->sums[i].len;
106                 n += s->sums[i].len;
107         }
108
109         for (j = 0; j < n; j += CHUNK_SIZE) {
110                 int32 n1 = MIN(CHUNK_SIZE, n - j);
111                 sum_update(map_ptr(buf, last_match + j, n1), n1);
112         }
113
114         if (i >= 0)
115                 last_match = offset + s->sums[i].len;
116         else
117                 last_match = offset;
118
119         if (buf && do_progress)
120                 show_progress(last_match, buf->file_size);
121 }
122
123
124 static void hash_search(int f,struct sum_struct *s,
125                         struct map_struct *buf, OFF_T len)
126 {
127         OFF_T offset, end, backup;
128         int32 k, want_i;
129         char sum2[SUM_LENGTH];
130         uint32 s1, s2, sum;
131         int more;
132         schar *map;
133
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;
137
138         if (verbose > 2) {
139                 rprintf(FINFO, "hash search b=%ld len=%.0f\n",
140                         (long)s->blength, (double)len);
141         }
142
143         k = (int32)MIN(len, (OFF_T)s->blength);
144
145         map = (schar *)map_ptr(buf, 0, k);
146
147         sum = get_checksum1((char *)map, k);
148         s1 = sum & 0xFFFF;
149         s2 = sum >> 16;
150         if (verbose > 3)
151                 rprintf(FINFO, "sum=%.8x k=%ld\n", sum, (long)k);
152
153         offset = 0;
154
155         end = len + 1 - s->sums[s->count-1].len;
156
157         if (verbose > 3) {
158                 rprintf(FINFO, "hash search s->blength=%ld len=%.0f count=%.0f\n",
159                         (long)s->blength, (double)len, (double)s->count);
160         }
161
162         do {
163                 int done_csum2 = 0;
164                 int32 i;
165                 uint32 t;
166
167                 if (verbose > 4)
168                         rprintf(FINFO,"offset=%.0f sum=%08x\n",(double)offset,sum);
169
170                 t = GETTAG(sum);
171                 i = sum_table[t];
172                 if (i < 0)
173                         goto null_hash;
174
175                 hash_hits++;
176                 do {
177                         int32 l;
178
179                         if (sum != s->sums[i].sum1)
180                                 continue;
181
182                         /* also make sure the two blocks are the same length */
183                         l = (int32)MIN((OFF_T)s->blength, len-offset);
184                         if (l != s->sums[i].len)
185                                 continue;
186
187                         /* in-place: ensure chunk's offset is either >= our
188                          * offset or that the data didn't move. */
189                         if (updating_basis_file && s->sums[i].offset < offset
190                             && !(s->sums[i].flags & SUMFLG_SAME_OFFSET))
191                                 continue;
192
193                         if (verbose > 3) {
194                                 rprintf(FINFO,
195                                         "potential match at %.0f i=%ld sum=%08x\n",
196                                         (double)offset, (long)i, sum);
197                         }
198
199                         if (!done_csum2) {
200                                 map = (schar *)map_ptr(buf,offset,l);
201                                 get_checksum2((char *)map,l,sum2);
202                                 done_csum2 = 1;
203                         }
204
205                         if (memcmp(sum2,s->sums[i].sum2,s->s2length) != 0) {
206                                 false_alarms++;
207                                 continue;
208                         }
209
210                         /* When updating in-place, the best possible match is
211                          * one with an identical offset, so we prefer that over
212                          * the following want_i optimization. */
213                         if (updating_basis_file) {
214                                 int32 i2;
215                                 for (i2 = i; i2 >= 0; i2 = s->sums[i2].chain) {
216                                         if (s->sums[i2].offset != offset)
217                                                 continue;
218                                         if (i2 != i) {
219                                                 if (sum != s->sums[i2].sum1)
220                                                         break;
221                                                 if (memcmp(sum2, s->sums[i2].sum2,
222                                                            s->s2length) != 0)
223                                                         break;
224                                                 i = i2;
225                                         }
226                                         /* This chunk was at the same offset on
227                                          * both the sender and the receiver. */
228                                         s->sums[i].flags |= SUMFLG_SAME_OFFSET;
229                                         goto set_want_i;
230                                 }
231                         }
232
233                         /* we've found a match, but now check to see
234                          * if want_i can hint at a better match. */
235                         if (i != want_i && want_i < s->count
236                             && (!updating_basis_file || s->sums[want_i].offset >= offset
237                              || s->sums[want_i].flags & SUMFLG_SAME_OFFSET)
238                             && sum == s->sums[want_i].sum1
239                             && memcmp(sum2, s->sums[want_i].sum2, s->s2length) == 0) {
240                                 /* we've found an adjacent match - the RLL coder
241                                  * will be happy */
242                                 i = want_i;
243                         }
244                     set_want_i:
245                         want_i = i + 1;
246
247                         matched(f,s,buf,offset,i);
248                         offset += s->sums[i].len - 1;
249                         k = (int32)MIN((OFF_T)s->blength, len-offset);
250                         map = (schar *)map_ptr(buf, offset, k);
251                         sum = get_checksum1((char *)map, k);
252                         s1 = sum & 0xFFFF;
253                         s2 = sum >> 16;
254                         matches++;
255                         break;
256                 } while ((i = s->sums[i].chain) >= 0);
257
258           null_hash:
259                 backup = offset - last_match;
260                 /* We sometimes read 1 byte prior to last_match... */
261                 if (backup < 0)
262                         backup = 0;
263
264                 /* Trim off the first byte from the checksum */
265                 more = offset + k < len;
266                 map = (schar *)map_ptr(buf, offset - backup, k + more + backup)
267                     + backup;
268                 s1 -= map[0] + CHAR_OFFSET;
269                 s2 -= k * (map[0]+CHAR_OFFSET);
270
271                 /* Add on the next byte (if there is one) to the checksum */
272                 if (more) {
273                         s1 += map[k] + CHAR_OFFSET;
274                         s2 += s1;
275                 } else
276                         --k;
277                 sum = (s1 & 0xffff) | (s2 << 16);
278
279                 /* By matching early we avoid re-reading the
280                    data 3 times in the case where a token
281                    match comes a long way after last
282                    match. The 3 reads are caused by the
283                    running match, the checksum update and the
284                    literal send. */
285                 if (backup >= s->blength+CHUNK_SIZE && end-offset > CHUNK_SIZE)
286                         matched(f, s, buf, offset - s->blength, -2);
287         } while (++offset < end);
288
289         matched(f, s, buf, len, -1);
290         map_ptr(buf, len-1, 1);
291 }
292
293
294 /**
295  * Scan through a origin file, looking for sections that match
296  * checksums from the generator, and transmit either literal or token
297  * data.
298  *
299  * Also calculates the MD4 checksum of the whole file, using the md
300  * accumulator.  This is transmitted with the file as protection
301  * against corruption on the wire.
302  *
303  * @param s Checksums received from the generator.  If <tt>s->count ==
304  * 0</tt>, then there are actually no checksums for this file.
305  *
306  * @param len Length of the file to send.
307  **/
308 void match_sums(int f, struct sum_struct *s, struct map_struct *buf, OFF_T len)
309 {
310         char file_sum[MD4_SUM_LENGTH];
311
312         last_match = 0;
313         false_alarms = 0;
314         hash_hits = 0;
315         matches = 0;
316         data_transfer = 0;
317
318         sum_init(checksum_seed);
319
320         if (append_mode) {
321                 OFF_T j = 0;
322                 for (j = CHUNK_SIZE; j < s->flength; j += CHUNK_SIZE) {
323                         if (buf && do_progress)
324                                 show_progress(last_match, buf->file_size);
325                         sum_update(map_ptr(buf, last_match, CHUNK_SIZE),
326                                    CHUNK_SIZE);
327                         last_match = j;
328                 }
329                 if (last_match < s->flength) {
330                         int32 len = s->flength - last_match;
331                         if (buf && do_progress)
332                                 show_progress(last_match, buf->file_size);
333                         sum_update(map_ptr(buf, last_match, len), len);
334                         last_match = s->flength;
335                 }
336                 s->count = 0;
337         }
338
339         if (len > 0 && s->count > 0) {
340                 build_hash_table(s);
341
342                 if (verbose > 2)
343                         rprintf(FINFO,"built hash table\n");
344
345                 hash_search(f,s,buf,len);
346
347                 if (verbose > 2)
348                         rprintf(FINFO,"done hash search\n");
349         } else {
350                 OFF_T j;
351                 /* by doing this in pieces we avoid too many seeks */
352                 for (j = last_match + CHUNK_SIZE; j < len; j += CHUNK_SIZE)
353                         matched(f, s, buf, j, -2);
354                 matched(f, s, buf, len, -1);
355         }
356
357         sum_end(file_sum);
358         /* If we had a read error, send a bad checksum. */
359         if (buf && buf->status != 0)
360                 file_sum[0]++;
361
362         if (verbose > 2)
363                 rprintf(FINFO,"sending file_sum\n");
364         write_buf(f,file_sum,MD4_SUM_LENGTH);
365
366         if (verbose > 2)
367                 rprintf(FINFO, "false_alarms=%d hash_hits=%d matches=%d\n",
368                         false_alarms, hash_hits, matches);
369
370         total_hash_hits += hash_hits;
371         total_false_alarms += false_alarms;
372         total_matches += matches;
373         stats.literal_data += data_transfer;
374 }
375
376 void match_report(void)
377 {
378         if (verbose <= 1)
379                 return;
380
381         rprintf(FINFO,
382                 "total: matches=%d  hash_hits=%d  false_alarms=%d data=%.0f\n",
383                 total_matches, total_hash_hits, total_false_alarms,
384                 (double)stats.literal_data);
385 }