Debian Codegraph @

internal/sender/match.go

raw symbols SCIP
1 package sender
2
3 import (
4 "bytes"
5 "encoding/binary"
6 "fmt"
7 "hash"
8
9 "github.com/gokrazy/rsync"
10 "github.com/gokrazy/rsync/internal/rsyncchecksum"
11 "github.com/gokrazy/rsync/internal/rsyncopts"
12 "github.com/mmcloughlin/md4"
13 )
14
15 type target struct {
16 index int32
17 tag uint16
18 }
19
20 // rsync/match.c:hash_search
21 func (st *Transfer) hashSearch(targets []target, tagTable map[uint16]int, head rsync.SumHead, fileIndex int32, fl file) error {
22 st.Logger.Printf("hashSearch(path=%s, len(sums)=%d)", fl.path, len(head.Sums))
23 f, err := fl.source.Open(fl.path)
24 if err != nil {
25 return err
26 }
27 defer f.Close()
28
29 fi, err := f.Stat()
30 if err != nil {
31 return err
32 }
33
34 readSize := max(3*head.BlockLength, 256*1024)
35 ms := mapFile(f, fi.Size(), readSize, head.BlockLength)
36
37 if err := st.Conn.WriteInt32(fileIndex); err != nil {
38 return err
39 }
40
41 if err := head.WriteTo(st.Conn); err != nil {
42 return err
43 }
44
45 if !st.Opts.Server() &&
46 st.Opts.InfoGTE(rsyncopts.INFO_NAME, 1) &&
47 st.Opts.InfoGTE(rsyncopts.INFO_PROGRESS, 1) {
48 fmt.Fprintln(st.Env.Stdout, fl.path)
49 }
50
51 // sum_init()
52 h := md4.New()
53 binary.Write(h, binary.LittleEndian, st.Seed)
54
55 // The following quotes are citations from
56 // https://www.samba.org/~tridge/phd_thesis.pdf, section 3.2.6 The
57 // signature search algorithm (PDF page 64).
58
59 // “Once the sorted signature table and the index table have been formed the
60 // signature search process can begin. For each byte offset in a_i the fast
61 // signature is computed, along with the 16 bit hash of the fast
62 // signature. The 16 bit hash is then used to lookup the signature index,
63 // giving the index in the signature table of the first fast signature with
64 // that hash.”
65
66 var k int
67 var sum uint32
68 var s1, s2 uint32
69 var offset int64
70 end := fi.Size() + 1 - head.Sums[len(head.Sums)-1].Len
71 st.Logger.Printf("last block len=%d, end=%d", head.Sums[len(head.Sums)-1].Len, end)
72
73 readChunk := func() error {
74 k = int(head.BlockLength)
75 if remaining := int(fi.Size() - offset); remaining < k {
76 k = remaining
77 }
78
79 chunk, err := ms.ptr(offset, int32(k))
80 if err != nil {
81 return err
82 }
83 sum = rsyncchecksum.Checksum1(chunk)
84 s1 = uint32(sum & 0xFFFF)
85 s2 = uint32(sum >> 16)
86 return nil
87 }
88 if err := readChunk(); err != nil {
89 return err
90 }
91
92 tagHits := 0
93 Outer:
94 for {
95 tag := rsyncchecksum.Tag2(uint16(s1), uint16(s2))
96 var sum2 []byte
97 doneCsum2 := false
98 j, ok := tagTable[tag]
99 if ok {
100 // “A linear search is then performed through the signature table, stopping
101 // when an entry is found with a 16 bit hash which doesn’t match. For each
102 // entry the current 32 bit fast signature is compared to the entry in the
103 // signature table, and if that matches then the full 128 bit strong
104 // signature is computed at the current byte offset and compared to the
105 // strong signature in the signature table”
106 sum = (uint32(s1) & 0xFFFF) | (uint32(s2) << 16)
107 tagHits++
108 for ; j < int(head.ChecksumCount) && targets[j].tag == tag; j++ {
109 i := targets[j].index
110 if sum != head.Sums[i].Sum1 {
111 continue
112 }
113
114 l := int64(head.BlockLength)
115 if v := fi.Size() - offset; v < l {
116 l = v
117 }
118 if l != head.Sums[i].Len {
119 continue
120 }
121
122 // st.logger.Printf("potential match at %d target=%d %d sum=%08x", offset, j, i, sum)
123
124 if !doneCsum2 {
125 buf, err := ms.ptr(offset, int32(l))
126 if err != nil {
127 return err
128 }
129 sum2 = rsyncchecksum.Checksum2(st.Seed, buf[:])
130 doneCsum2 = true
131 }
132
133 if local, remote := sum2[:head.ChecksumLength], head.Sums[i].Sum2[:head.ChecksumLength]; !bytes.Equal(local, remote) {
134 st.Logger.Printf("false alarm: local %x, remote %x", local, remote)
135 //falseAlarms++
136 continue
137 }
138
139 // TODO(optimization): tridge rsync locates adjacent matches
140 // here for better run-length encoding, but I’m not sure where
141 // (if at all) we currently use run-length encoding:
142 // https://github.com/WayneD/rsync/commit/923fa978088f4c044eec528d9472962d9c9d13c3
143
144 // “If the strong signature is found to match then A emits a
145 // token telling B that a match was found and which block in bi
146 // was matched12. The search then continues at the byte after
147 // the matching block.”
148
149 if err := st.matched(h, ms, head, offset, i); err != nil {
150 return err
151 }
152
153 // rsync doesn’t read the next chunk (offset+sums[i].len),
154 // rsync starts reading one byte before the next chunk
155 // (offset+sums[i].len-1), because the code path starting at
156 // “null_tag” removes the chunk’s first byte and adds the
157 // next byte after the chunk.
158 offset += head.Sums[i].Len - 1
159 if err := readChunk(); err != nil {
160 return fmt.Errorf("readChunk: %v", err)
161 }
162
163 if offset >= end {
164 break Outer
165 }
166
167 break
168 }
169 }
170
171 // Update the rolling checksum by removing the oldest byte (update[0])
172 // and adding the newest byte (update[k]).
173 backup := max(offset-st.lastMatch, 0)
174
175 more := offset+int64(k) < fi.Size()
176 mmore := int64(0)
177 if more {
178 mmore = 1
179 }
180 update, err := ms.ptr(offset-backup, int32(int64(k)+mmore+backup))
181 if err != nil {
182 return err
183 }
184 update = update[backup:]
185
186 s1 -= rsyncchecksum.SignExtend(update[0])
187 s2 -= uint32(k) * rsyncchecksum.SignExtend(update[0])
188
189 if more {
190 s1 += rsyncchecksum.SignExtend(update[k])
191 s2 += s1
192 } else {
193 k--
194 }
195 s1 = uint32(uint16(s1))
196 s2 = uint32(uint16(s2))
197
198 if backup >= int64(head.BlockLength)+chunkSize && end-offset > chunkSize {
199 // Prevent offset-st.lastMatch from growing too large by flushing
200 // intermediate chunks.
201 if err := st.matched(h, ms, head, offset-int64(head.BlockLength), -2); err != nil {
202 return err
203 }
204 }
205
206 offset++
207 if offset >= end {
208 break
209 }
210 }
211
212 if err := st.matched(h, ms, head, fi.Size(), -1); err != nil {
213 return err
214 }
215
216 if st.Opts.InfoGTE(rsyncopts.INFO_PROGRESS, 1) {
217 st.Progress.Show(uint64(offset), true)
218 }
219
220 {
221 sum := h.Sum(nil)
222 st.Logger.Printf("sum: %x (len = %d)", sum, len(sum))
223 if _, err := st.Conn.Writer.Write(sum); err != nil {
224 return err
225 }
226 }
227
228 return nil
229
230 }
231
232 // rsync/match.c:matched
233 func (st *Transfer) matched(h hash.Hash, ms *mapStruct, head rsync.SumHead, offset int64, i int32) error {
234 n := offset - st.lastMatch
235
236 transmitAccumulated := i < 0
237
238 // if !transmitAccumulated {
239 // st.logger.Printf("match at offset=%d last_match=%d i=%d len=%d n=%d",
240 // offset, st.lastMatch, i, head.Sums[i].Len, n)
241 // } else {
242 // st.logger.Printf("transmit accumulated at offset=%d", offset)
243 // }
244
245 /* FIXME: this is not used
246 l := int64(0)
247 if !transmitAccumulated {
248 l = head.Sums[i].Len
249 }
250 */
251
252 if err := st.sendToken(ms, i, st.lastMatch, n); err != nil {
253 return fmt.Errorf("sendToken: %v", err)
254 }
255 // TODO: data_transfer += n;
256
257 if !transmitAccumulated {
258 // stats.matched_data += s->sums[i].len;
259 n += head.Sums[i].Len
260 }
261
262 for j := int64(0); j < n; j += chunkSize {
263 n1 := min(int64(chunkSize), n-j)
264 chunk, err := ms.ptr(st.lastMatch+j, int32(n1))
265 if err != nil {
266 return err
267 }
268 h.Write(chunk)
269 }
270
271 if !transmitAccumulated {
272 st.lastMatch = offset + head.Sums[i].Len
273 } else {
274 st.lastMatch = offset
275 }
276
277 if st.Opts.InfoGTE(rsyncopts.INFO_PROGRESS, 1) {
278 st.Progress.MaybeShow(uint64(offset), false)
279 }
280
281 return nil
282 }