| 1 |
package desync |
| 2 |
|
| 3 |
import "strings" |
| 4 |
|
| 5 |
// Converters are modifiers for chunk data, such as compression or encryption. |
| 6 |
// They are used to prepare chunk data for storage, or to read it from storage. |
| 7 |
// The order of the conversion layers matters. When plain data is prepared for |
| 8 |
// storage, the toStorage method is used in the order the layers are defined. |
| 9 |
// To read from storage, the fromStorage method is called for each layer in |
| 10 |
// reverse order. |
| 11 |
type Converters []converter |
| 12 |
|
| 13 |
// Apply every data converter in the forward direction. |
| 14 |
func (s Converters) toStorage(in []byte) ([]byte, error) { |
| 15 |
var ( |
| 16 |
b = in |
| 17 |
err error |
| 18 |
) |
| 19 |
for _, layer := range s { |
| 20 |
b, err = layer.toStorage(b) |
| 21 |
if err != nil { |
| 22 |
return nil, err |
| 23 |
} |
| 24 |
} |
| 25 |
return b, nil |
| 26 |
} |
| 27 |
|
| 28 |
// Apply the layers backwards. |
| 29 |
func (s Converters) fromStorage(in []byte) ([]byte, error) { |
| 30 |
var ( |
| 31 |
b = in |
| 32 |
err error |
| 33 |
) |
| 34 |
for i := len(s) - 1; i >= 0; i-- { |
| 35 |
b, err = s[i].fromStorage(b) |
| 36 |
if err != nil { |
| 37 |
return nil, err |
| 38 |
} |
| 39 |
} |
| 40 |
return b, nil |
| 41 |
} |
| 42 |
|
| 43 |
// commonPrefix returns the number of leading layers shared between the |
| 44 |
// two conversion stacks. Used to determine the difference between them, |
| 45 |
// for example a compressed store being served encrypted, where only the |
| 46 |
// differing layers need to be applied. |
| 47 |
func (s Converters) commonPrefix(c Converters) int { |
| 48 |
var n int |
| 49 |
for n < len(s) && n < len(c) && s[n].equal(c[n]) { |
| 50 |
n++ |
| 51 |
} |
| 52 |
return n |
| 53 |
} |
| 54 |
|
| 55 |
// Extension to be used in storage. Concatenation of converter |
| 56 |
// extensions in order (towards storage). |
| 57 |
func (s Converters) storageExtension() string { |
| 58 |
var ext strings.Builder |
| 59 |
for _, layer := range s { |
| 60 |
ext.WriteString(layer.storageExtension()) |
| 61 |
} |
| 62 |
return ext.String() |
| 63 |
} |
| 64 |
|
| 65 |
// converter is a storage data modifier layer. |
| 66 |
type converter interface { |
| 67 |
// Convert data from its original form to storage format. |
| 68 |
// The input could be plain data, or the output of a prior |
| 69 |
// converter. |
| 70 |
toStorage([]byte) ([]byte, error) |
| 71 |
|
| 72 |
// Convert data from its storage format towards its plain |
| 73 |
// form. The input could be encrypted or compressed, while |
| 74 |
// the output may be used for the next conversion layer. |
| 75 |
fromStorage([]byte) ([]byte, error) |
| 76 |
|
| 77 |
// Returns the file extension that should be used for a |
| 78 |
// chunk when stored. Usually a concatenation of layers. |
| 79 |
storageExtension() string |
| 80 |
|
| 81 |
// True is one converter matches another exactly. |
| 82 |
equal(converter) bool |
| 83 |
} |
| 84 |
|
| 85 |
// Compression layer converter. Compresses/decompresses chunk data |
| 86 |
// to and from storage. Implements the converter interface. Lives in |
| 87 |
// this file rather than compress.go so it is part of both compression |
| 88 |
// build variants. |
| 89 |
type Compressor struct{} |
| 90 |
|
| 91 |
var _ converter = Compressor{} |
| 92 |
|
| 93 |
func (d Compressor) toStorage(in []byte) ([]byte, error) { |
| 94 |
return Compress(in) |
| 95 |
} |
| 96 |
|
| 97 |
func (d Compressor) fromStorage(in []byte) ([]byte, error) { |
| 98 |
return Decompress(nil, in) |
| 99 |
} |
| 100 |
|
| 101 |
func (d Compressor) equal(c converter) bool { |
| 102 |
_, ok := c.(Compressor) |
| 103 |
return ok |
| 104 |
} |
| 105 |
|
| 106 |
func (d Compressor) storageExtension() string { |
| 107 |
return ".cacnk" |
| 108 |
} |