Debian Codegraph @

chunk.go

raw
1 package desync
2
3 import (
4 "errors"
5 )
6
7 // Chunk holds chunk data plain, storage format, or both. If a chunk is created
8 // from storage data, such as read from a compressed chunk store, and later the
9 // application requires the plain data, it'll be converted on demand by applying
10 // the given storage converters in reverse order. The converters can only be used
11 // to read the plain data, not to convert back to storage format.
12 type Chunk struct {
13 data []byte // Plain data if available
14 storage []byte // Storage format (compressed, encrypted, etc)
15 converters Converters // Modifiers to convert from storage format to plain
16 id ChunkID
17 idCalculated bool
18 }
19
20 // NewChunk creates a new chunk from plain data. The data is trusted and the ID is
21 // calculated on demand.
22 func NewChunk(b []byte) *Chunk {
23 return &Chunk{data: b}
24 }
25
26 // clone returns a shallow copy of the chunk. The copy shares the underlying
27 // data and storage slices, which are never modified after construction, but
28 // gets its own lazily-calculated fields (plain data, ID). Data() and ID()
29 // mutate the chunk without synchronization, so a chunk handed to multiple
30 // goroutines must be cloned for each of them.
31 func (c *Chunk) clone() *Chunk {
32 cp := *c
33 return &cp
34 }
35
36 // NewChunkWithID creates a new chunk from plain data. It also expects an ID
37 // and validates that it matches the data unless skipVerify is true.
38 func NewChunkWithID(id ChunkID, b []byte, skipVerify bool) (*Chunk, error) {
39 return NewChunkFromStorage(id, b, nil, skipVerify)
40 }
41
42 // NewChunkFromStorage builds a new chunk from data that is not in plain format.
43 // It uses raw storage format from its source and the modifiers are used to convert
44 // into plain data as needed.
45 func NewChunkFromStorage(id ChunkID, b []byte, modifiers Converters, skipVerify bool) (*Chunk, error) {
46 c := &Chunk{id: id, storage: b, converters: modifiers}
47 if skipVerify {
48 c.idCalculated = true // Pretend this was calculated. No need to re-calc later
49 return c, nil
50 }
51 if err := c.verify(); err != nil {
52 return nil, err
53 }
54 return c, nil
55 }
56
57 // verify confirms the chunk's plain data matches its expected ID. Errors
58 // converting the storage data to plain form, such as decryption failures,
59 // are reported as ChunkInvalid as well, with the cause preserved, since
60 // they equally mean the chunk in the store is unusable.
61 func (c *Chunk) verify() error {
62 b, err := c.Data()
63 if err != nil {
64 return ChunkInvalid{ID: c.id, Err: err}
65 }
66 sum := Digest.Sum(b)
67 if sum != c.id {
68 return ChunkInvalid{ID: c.id, Sum: sum}
69 }
70 c.idCalculated = true
71 return nil
72 }
73
74 // Data returns the chunk data in uncompressed form. If the chunk was created
75 // with compressed data only, it'll be decompressed, stored and returned. The
76 // caller must not modify the data in the returned slice.
77 func (c *Chunk) Data() ([]byte, error) {
78 if len(c.data) > 0 {
79 return c.data, nil
80 }
81 if len(c.storage) > 0 {
82 var err error
83 c.data, err = c.converters.fromStorage(c.storage)
84 return c.data, err
85 }
86 return nil, errors.New("no data in chunk")
87 }
88
89 // ID returns the checksum/ID of the uncompressed chunk data. The ID is stored
90 // after the first call and doesn't need to be re-calculated. Note that calculating
91 // the ID may mean decompressing the data first.
92 func (c *Chunk) ID() ChunkID {
93 if c.idCalculated {
94 return c.id
95 }
96 b, err := c.Data()
97 if err != nil {
98 return ChunkID{}
99 }
100 c.id = Digest.Sum(b)
101 c.idCalculated = true
102 return c.id
103 }
104
105 // Storage returns the chunk data in storage format according to the given
106 // modifiers. If the chunk was created with storage data and the same modifiers,
107 // this data will be returned as is. If the modifiers share leading layers with
108 // the chunk's own, only the difference is applied, avoiding expensive
109 // conversions of the shared layers, such as recompression when just an
110 // encryption layer is added, removed, or uses a different key. The caller
111 // must not modify the data in the returned slice.
112 func (c *Chunk) Storage(modifiers Converters) ([]byte, error) {
113 if len(c.storage) > 0 {
114 // If the stacks share leading layers, unwind the chunk's own extra
115 // layers and apply the requested ones on top. When nothing is shared,
116 // unwinding is the same work as Data() below, which also caches the
117 // plain data on the chunk for other consumers, so prefer that path.
118 if n := modifiers.commonPrefix(c.converters); n > 0 {
119 b := c.storage
120 if n < len(c.converters) {
121 var err error
122 b, err = c.converters[n:].fromStorage(b)
123 if err != nil {
124 return nil, err
125 }
126 }
127 return modifiers[n:].toStorage(b)
128 }
129 }
130 b, err := c.Data()
131 if err != nil {
132 return nil, err
133 }
134 return modifiers.toStorage(b)
135 }