MCPcopy
hub / github.com/syncthing/syncthing / blockDiff

Function blockDiff

lib/model/folder_sendrecv.go:1215–1241  ·  view source on GitHub ↗

blockDiff returns lists of common and missing (to transform src into tgt) blocks. Both block lists must have been created with the same block size.

(src, tgt []protocol.BlockInfo)

Source from the content-addressed store, hash-verified

1213// blockDiff returns lists of common and missing (to transform src into tgt)
1214// blocks. Both block lists must have been created with the same block size.
1215func blockDiff(src, tgt []protocol.BlockInfo) ([]protocol.BlockInfo, []protocol.BlockInfo) {
1216 if len(tgt) == 0 {
1217 return nil, nil
1218 }
1219
1220 if len(src) == 0 {
1221 // Copy the entire file
1222 return nil, tgt
1223 }
1224
1225 have := make([]protocol.BlockInfo, 0, len(src))
1226 need := make([]protocol.BlockInfo, 0, len(tgt))
1227
1228 for i := range tgt {
1229 if i >= len(src) {
1230 return have, append(need, tgt[i:]...)
1231 }
1232 if !bytes.Equal(tgt[i].Hash, src[i].Hash) {
1233 // Copy differing block
1234 need = append(need, tgt[i])
1235 } else {
1236 have = append(have, tgt[i])
1237 }
1238 }
1239
1240 return have, need
1241}
1242
1243// populateOffsets sets the Offset field on each block
1244func populateOffsets(blocks []protocol.BlockInfo) {

Callers 5

TestDiffFunction · 0.85
BenchmarkDiffFunction · 0.85
TestDiffEmptyFunction · 0.85
handleFileMethod · 0.85
reuseBlocksMethod · 0.85

Calls 1

EqualMethod · 0.45

Tested by 3

TestDiffFunction · 0.68
BenchmarkDiffFunction · 0.68
TestDiffEmptyFunction · 0.68