Debian Codegraph @

seed.go

raw
1 package desync
2
3 import (
4 "context"
5 "os"
6 )
7
8 // DefaultBlockSize is used when the actual filesystem block size cannot be determined automatically
9 const DefaultBlockSize = 4096
10
11 // cloneRange is an indirection over CloneRange that allows tests to simulate
12 // filesystems where CanClone succeeds but actual cloning fails (e.g. ZFS).
13 var cloneRange = CloneRange
14
15 // Seed represent a source of chunks other than the store. Typically a seed is
16 // another index+blob that present on disk already and is used to copy or clone
17 // existing chunks or blocks into the target from.
18 type Seed interface {
19 LongestMatchWith(chunks []IndexChunk) (int, SeedSegment)
20 RegenerateIndex(ctx context.Context, n int, attempt int, seedNumber int) error
21 SetInvalid(value bool)
22 IsInvalid() bool
23 }
24
25 // SeedSegment represents a matching range between a Seed and a file being
26 // assembled from an Index. It's used to copy or reflink data from seeds into
27 // a target file during an extract operation.
28 type SeedSegment interface {
29 FileName() string
30 Size() uint64
31 Validate(file *os.File) error
32 WriteInto(dst *os.File, offset, end, blocksize uint64, isBlank bool) (copied uint64, cloned uint64, err error)
33 }
34
35 // IndexSegment represents a contiguous section of an index which is used when
36 // assembling a file from seeds. first/last are positions in the index.
37 type IndexSegment struct {
38 index Index
39 first, last int
40 }
41
42 func (s IndexSegment) lengthChunks() int {
43 return s.last - s.first + 1
44 }
45
46 func (s IndexSegment) lengthBytes() uint64 {
47 return s.end() - s.start()
48 }
49
50 func (s IndexSegment) start() uint64 {
51 return s.index.Chunks[s.first].Start
52 }
53
54 func (s IndexSegment) end() uint64 {
55 return s.index.Chunks[s.last].Start + s.index.Chunks[s.last].Size
56 }
57
58 func (s IndexSegment) chunks() []IndexChunk {
59 return s.index.Chunks[s.first : s.last+1]
60 }