package desync import ( "errors" ) // Chunk holds chunk data plain, storage format, or both. If a chunk is created // from storage data, such as read from a compressed chunk store, and later the // application requires the plain data, it'll be converted on demand by applying // the given storage converters in reverse order. The converters can only be used // to read the plain data, not to convert back to storage format. type Chunk struct { data []byte // Plain data if available storage []byte // Storage format (compressed, encrypted, etc) converters Converters // Modifiers to convert from storage format to plain id ChunkID idCalculated bool } // NewChunk creates a new chunk from plain data. The data is trusted and the ID is // calculated on demand. func NewChunk(b []byte) *Chunk { return &Chunk{data: b} } // clone returns a shallow copy of the chunk. The copy shares the underlying // data and storage slices, which are never modified after construction, but // gets its own lazily-calculated fields (plain data, ID). Data() and ID() // mutate the chunk without synchronization, so a chunk handed to multiple // goroutines must be cloned for each of them. func (c *Chunk) clone() *Chunk { cp := *c return &cp } // NewChunkWithID creates a new chunk from plain data. It also expects an ID // and validates that it matches the data unless skipVerify is true. func NewChunkWithID(id ChunkID, b []byte, skipVerify bool) (*Chunk, error) { return NewChunkFromStorage(id, b, nil, skipVerify) } // NewChunkFromStorage builds a new chunk from data that is not in plain format. // It uses raw storage format from its source and the modifiers are used to convert // into plain data as needed. func NewChunkFromStorage(id ChunkID, b []byte, modifiers Converters, skipVerify bool) (*Chunk, error) { c := &Chunk{id: id, storage: b, converters: modifiers} if skipVerify { c.idCalculated = true // Pretend this was calculated. No need to re-calc later return c, nil } if err := c.verify(); err != nil { return nil, err } return c, nil } // verify confirms the chunk's plain data matches its expected ID. Errors // converting the storage data to plain form, such as decryption failures, // are reported as ChunkInvalid as well, with the cause preserved, since // they equally mean the chunk in the store is unusable. func (c *Chunk) verify() error { b, err := c.Data() if err != nil { return ChunkInvalid{ID: c.id, Err: err} } sum := Digest.Sum(b) if sum != c.id { return ChunkInvalid{ID: c.id, Sum: sum} } c.idCalculated = true return nil } // Data returns the chunk data in uncompressed form. If the chunk was created // with compressed data only, it'll be decompressed, stored and returned. The // caller must not modify the data in the returned slice. func (c *Chunk) Data() ([]byte, error) { if len(c.data) > 0 { return c.data, nil } if len(c.storage) > 0 { var err error c.data, err = c.converters.fromStorage(c.storage) return c.data, err } return nil, errors.New("no data in chunk") } // ID returns the checksum/ID of the uncompressed chunk data. The ID is stored // after the first call and doesn't need to be re-calculated. Note that calculating // the ID may mean decompressing the data first. func (c *Chunk) ID() ChunkID { if c.idCalculated { return c.id } b, err := c.Data() if err != nil { return ChunkID{} } c.id = Digest.Sum(b) c.idCalculated = true return c.id } // Storage returns the chunk data in storage format according to the given // modifiers. If the chunk was created with storage data and the same modifiers, // this data will be returned as is. If the modifiers share leading layers with // the chunk's own, only the difference is applied, avoiding expensive // conversions of the shared layers, such as recompression when just an // encryption layer is added, removed, or uses a different key. The caller // must not modify the data in the returned slice. func (c *Chunk) Storage(modifiers Converters) ([]byte, error) { if len(c.storage) > 0 { // If the stacks share leading layers, unwind the chunk's own extra // layers and apply the requested ones on top. When nothing is shared, // unwinding is the same work as Data() below, which also caches the // plain data on the chunk for other consumers, so prefer that path. if n := modifiers.commonPrefix(c.converters); n > 0 { b := c.storage if n < len(c.converters) { var err error b, err = c.converters[n:].fromStorage(b) if err != nil { return nil, err } } return modifiers[n:].toStorage(b) } } b, err := c.Data() if err != nil { return nil, err } return modifiers.toStorage(b) }