| 1 |
package desync |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"os" |
| 8 |
"path/filepath" |
| 9 |
"strings" |
| 10 |
"sync" |
| 11 |
|
| 12 |
"github.com/folbricht/tempfile" |
| 13 |
) |
| 14 |
|
| 15 |
var _ WriteStore = LocalStore{} |
| 16 |
|
| 17 |
const ( |
| 18 |
tmpChunkPrefix = ".tmp-cacnk" |
| 19 |
) |
| 20 |
|
| 21 |
// LocalStore casync store |
| 22 |
type LocalStore struct { |
| 23 |
Base string |
| 24 |
|
| 25 |
// When accessing chunks, should mtime be updated? Useful when this is |
| 26 |
// a cache. Old chunks can be identified and removed from the store that way |
| 27 |
UpdateTimes bool |
| 28 |
|
| 29 |
Opt StoreOptions |
| 30 |
|
| 31 |
converters Converters |
| 32 |
|
| 33 |
// Chunk file extension, derived from the converters at construction |
| 34 |
extension string |
| 35 |
} |
| 36 |
|
| 37 |
// NewLocalStore creates an instance of a local castore, it only checks presence |
| 38 |
// of the store |
| 39 |
func NewLocalStore(dir string, opt StoreOptions) (LocalStore, error) { |
| 40 |
info, err := os.Stat(dir) |
| 41 |
if err != nil { |
| 42 |
return LocalStore{}, err |
| 43 |
} |
| 44 |
if !info.IsDir() { |
| 45 |
return LocalStore{}, fmt.Errorf("%s is not a directory", dir) |
| 46 |
} |
| 47 |
converters, err := opt.StorageConverters() |
| 48 |
if err != nil { |
| 49 |
return LocalStore{}, err |
| 50 |
} |
| 51 |
return LocalStore{Base: dir, Opt: opt, converters: converters, extension: converters.storageExtension()}, nil |
| 52 |
} |
| 53 |
|
| 54 |
// GetChunk reads and returns one (compressed!) chunk from the store |
| 55 |
func (s LocalStore) GetChunk(id ChunkID) (*Chunk, error) { |
| 56 |
_, p := s.nameFromID(id) |
| 57 |
b, err := os.ReadFile(p) |
| 58 |
if err != nil { |
| 59 |
if os.IsNotExist(err) { |
| 60 |
return nil, ChunkMissing{id} |
| 61 |
} |
| 62 |
return nil, err |
| 63 |
} |
| 64 |
return NewChunkFromStorage(id, b, s.converters, s.Opt.SkipVerify) |
| 65 |
} |
| 66 |
|
| 67 |
// RemoveChunk deletes a chunk, typically an invalid one, from the filesystem. |
| 68 |
// Used when verifying and repairing caches. |
| 69 |
func (s LocalStore) RemoveChunk(id ChunkID) error { |
| 70 |
_, p := s.nameFromID(id) |
| 71 |
if _, err := os.Stat(p); err != nil { |
| 72 |
return ChunkMissing{id} |
| 73 |
} |
| 74 |
return os.Remove(p) |
| 75 |
} |
| 76 |
|
| 77 |
// StoreChunk adds a new chunk to the store |
| 78 |
func (s LocalStore) StoreChunk(chunk *Chunk) error { |
| 79 |
d, p := s.nameFromID(chunk.ID()) |
| 80 |
b, err := chunk.Storage(s.converters) |
| 81 |
if err != nil { |
| 82 |
return err |
| 83 |
} |
| 84 |
if err := os.MkdirAll(d, 0755); err != nil { |
| 85 |
return err |
| 86 |
} |
| 87 |
tmp, err := tempfile.NewMode(d, tmpChunkPrefix, 0644) |
| 88 |
if err != nil { |
| 89 |
return err |
| 90 |
} |
| 91 |
if _, err = tmp.Write(b); err != nil { |
| 92 |
tmp.Close() |
| 93 |
os.Remove(tmp.Name()) // clean up |
| 94 |
return err |
| 95 |
} |
| 96 |
tmp.Close() // Windows can't rename open files, close explicitly |
| 97 |
return os.Rename(tmp.Name(), p) |
| 98 |
} |
| 99 |
|
| 100 |
// Verify all chunks in the store. If repair is set true, bad chunks are deleted. |
| 101 |
// n determines the number of concurrent operations. w is used to write any messages |
| 102 |
// intended for the user, typically os.Stderr. |
| 103 |
func (s LocalStore) Verify(ctx context.Context, n int, repair bool, w io.Writer) error { |
| 104 |
var wg sync.WaitGroup |
| 105 |
ids := make(chan ChunkID) |
| 106 |
|
| 107 |
// Start the workers |
| 108 |
for range n { |
| 109 |
wg.Add(1) |
| 110 |
go func() { |
| 111 |
for id := range ids { |
| 112 |
_, err := s.GetChunk(id) |
| 113 |
switch err.(type) { |
| 114 |
case ChunkInvalid: // bad chunk, report and delete (if repair=true) |
| 115 |
msg := err.Error() |
| 116 |
if repair { |
| 117 |
if err = s.RemoveChunk(id); err != nil { |
| 118 |
msg = msg + ":" + err.Error() |
| 119 |
} else { |
| 120 |
msg = msg + ": removed" |
| 121 |
} |
| 122 |
} |
| 123 |
fmt.Fprintln(w, msg) |
| 124 |
case nil: |
| 125 |
default: // unexpected, print the error and carry on |
| 126 |
fmt.Fprintln(w, err) |
| 127 |
} |
| 128 |
} |
| 129 |
wg.Done() |
| 130 |
}() |
| 131 |
} |
| 132 |
|
| 133 |
// Go through all chunks underneath Base, filtering out other files, then feed |
| 134 |
// the IDs to the workers |
| 135 |
err := filepath.Walk(s.Base, func(path string, info os.FileInfo, err error) error { |
| 136 |
// See if we're meant to stop |
| 137 |
select { |
| 138 |
case <-ctx.Done(): |
| 139 |
return Interrupted{} |
| 140 |
default: |
| 141 |
} |
| 142 |
if err != nil { // failed to walk? => fail |
| 143 |
return err |
| 144 |
} |
| 145 |
if info.IsDir() { // Skip dirs |
| 146 |
return nil |
| 147 |
} |
| 148 |
// Skip files that aren't chunks matching the store's extension, they |
| 149 |
// may use different compression or encryption settings |
| 150 |
id, ok := chunkIDFromFilename(filepath.Base(path), s.extension) |
| 151 |
if !ok { |
| 152 |
return nil |
| 153 |
} |
| 154 |
// Feed the workers |
| 155 |
ids <- id |
| 156 |
return nil |
| 157 |
}) |
| 158 |
close(ids) |
| 159 |
wg.Wait() |
| 160 |
return err |
| 161 |
} |
| 162 |
|
| 163 |
// Prune removes any chunks from the store that are not contained in a list |
| 164 |
// of chunks |
| 165 |
func (s LocalStore) Prune(ctx context.Context, ids map[ChunkID]struct{}) error { |
| 166 |
// Go through all chunks underneath Base, filtering out other directories and files |
| 167 |
err := filepath.Walk(s.Base, func(path string, info os.FileInfo, err error) error { |
| 168 |
// See if we're meant to stop |
| 169 |
select { |
| 170 |
case <-ctx.Done(): |
| 171 |
return Interrupted{} |
| 172 |
default: |
| 173 |
} |
| 174 |
if err != nil { // failed to walk? => fail |
| 175 |
return err |
| 176 |
} |
| 177 |
if info.IsDir() { // Skip dirs |
| 178 |
return nil |
| 179 |
} |
| 180 |
|
| 181 |
// If the chunk is only partially downloaded remove it |
| 182 |
if strings.HasPrefix(filepath.Base(path), tmpChunkPrefix) { |
| 183 |
_ = os.Remove(path) |
| 184 |
return nil |
| 185 |
} |
| 186 |
// Skip files that aren't chunks matching the store's extension, they |
| 187 |
// may use different compression or encryption settings |
| 188 |
id, ok := chunkIDFromFilename(filepath.Base(path), s.extension) |
| 189 |
if !ok { |
| 190 |
return nil |
| 191 |
} |
| 192 |
// See if the chunk we're looking at is in the list we want to keep, if not |
| 193 |
// remove it. |
| 194 |
if _, ok := ids[id]; !ok { |
| 195 |
if err = s.RemoveChunk(id); err != nil { |
| 196 |
return err |
| 197 |
} |
| 198 |
} |
| 199 |
return nil |
| 200 |
}) |
| 201 |
return err |
| 202 |
} |
| 203 |
|
| 204 |
// HasChunk returns true if the chunk is in the store |
| 205 |
func (s LocalStore) HasChunk(id ChunkID) (bool, error) { |
| 206 |
_, p := s.nameFromID(id) |
| 207 |
_, err := os.Stat(p) |
| 208 |
if err == nil { |
| 209 |
return true, nil |
| 210 |
} |
| 211 |
if os.IsNotExist(err) { |
| 212 |
return false, nil |
| 213 |
} |
| 214 |
return false, err |
| 215 |
} |
| 216 |
|
| 217 |
func (s LocalStore) String() string { |
| 218 |
return s.Base |
| 219 |
} |
| 220 |
|
| 221 |
// Close the store. NOP operation, needed to implement Store interface. |
| 222 |
func (s LocalStore) Close() error { return nil } |
| 223 |
|
| 224 |
// GetChunkSize returns the bytes size of the raw, possibly compressed chunk in this store. |
| 225 |
func (s LocalStore) GetChunkSize(id ChunkID) (int64, error) { |
| 226 |
_, p := s.nameFromID(id) |
| 227 |
i, err := os.Stat(p) |
| 228 |
if err != nil { |
| 229 |
return 0, err |
| 230 |
} |
| 231 |
return i.Size(), nil |
| 232 |
} |
| 233 |
|
| 234 |
func (s LocalStore) nameFromID(id ChunkID) (dir, name string) { |
| 235 |
sID := id.String() |
| 236 |
dir = filepath.Join(s.Base, sID[0:4]) |
| 237 |
name = filepath.Join(dir, sID) + s.extension |
| 238 |
return |
| 239 |
} |