| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"errors" |
| 6 |
|
| 7 |
"github.com/folbricht/desync" |
| 8 |
"github.com/spf13/cobra" |
| 9 |
) |
| 10 |
|
| 11 |
type cacheOptions struct { |
| 12 |
cmdStoreOptions |
| 13 |
stores []string |
| 14 |
cache string |
| 15 |
ignoreIndexes []string |
| 16 |
ignoreChunks []string |
| 17 |
} |
| 18 |
|
| 19 |
func newCacheCommand(ctx context.Context) *cobra.Command { |
| 20 |
var opt cacheOptions |
| 21 |
|
| 22 |
cmd := &cobra.Command{ |
| 23 |
Use: "cache <index> [<index>...]", |
| 24 |
Short: "Read indexes and copy the referenced chunks", |
| 25 |
Long: `Read chunk IDs from one or more index files (caibx or caidx) and copy the |
| 26 |
referenced chunks from the source store(s) into the target store given with |
| 27 |
-c, without assembling any blob on disk. This can be used to pre-populate a |
| 28 |
cache, or to replicate the chunks referenced by indexes into another store. |
| 29 |
Use '-' to read (a single) index from STDIN. |
| 30 |
|
| 31 |
To exclude chunks that are known to exist in the target store already, use |
| 32 |
--ignore <index> which will skip any chunks from the given index. The same can |
| 33 |
be achieved by providing the chunks in their ASCII representation in a text |
| 34 |
file with --ignore-chunks <file>.`, |
| 35 |
Example: ` desync cache -s http://192.168.1.1/ -c /path/to/local file.caibx`, |
| 36 |
Args: cobra.MinimumNArgs(1), |
| 37 |
RunE: func(cmd *cobra.Command, args []string) error { |
| 38 |
return runCache(ctx, opt, args) |
| 39 |
}, |
| 40 |
SilenceUsage: true, |
| 41 |
} |
| 42 |
flags := cmd.Flags() |
| 43 |
flags.StringSliceVarP(&opt.stores, "store", "s", nil, "source store(s)") |
| 44 |
flags.StringVarP(&opt.cache, "cache", "c", "", "target store the chunks are copied to") |
| 45 |
flags.StringSliceVarP(&opt.ignoreIndexes, "ignore", "", nil, "index(es) with chunks to be excluded") |
| 46 |
flags.StringSliceVarP(&opt.ignoreChunks, "ignore-chunks", "", nil, "text file with chunk IDs to be excluded") |
| 47 |
addStoreOptions(&opt.cmdStoreOptions, flags) |
| 48 |
return cmd |
| 49 |
} |
| 50 |
|
| 51 |
func runCache(ctx context.Context, opt cacheOptions, args []string) error { |
| 52 |
if err := opt.cmdStoreOptions.validate(); err != nil { |
| 53 |
return err |
| 54 |
} |
| 55 |
if len(opt.stores) == 0 { |
| 56 |
return errors.New("no source store provided") |
| 57 |
} |
| 58 |
if opt.cache == "" { |
| 59 |
return errors.New("no target cache store provided") |
| 60 |
} |
| 61 |
|
| 62 |
// Read the input files and merge all chunk IDs in a map to de-dup them |
| 63 |
idm := make(map[desync.ChunkID]struct{}) |
| 64 |
for _, name := range args { |
| 65 |
c, err := readCaibxFile(name, opt.cmdStoreOptions) |
| 66 |
if err != nil { |
| 67 |
return err |
| 68 |
} |
| 69 |
for _, c := range c.Chunks { |
| 70 |
idm[c.ID] = struct{}{} |
| 71 |
} |
| 72 |
} |
| 73 |
// If requested, skip/ignore all chunks that are referenced in other indexes or text files |
| 74 |
if len(opt.ignoreIndexes) > 0 || len(opt.ignoreChunks) > 0 { |
| 75 |
// Remove chunks referenced in indexes |
| 76 |
for _, f := range opt.ignoreIndexes { |
| 77 |
i, err := readCaibxFile(f, opt.cmdStoreOptions) |
| 78 |
if err != nil { |
| 79 |
return err |
| 80 |
} |
| 81 |
for _, c := range i.Chunks { |
| 82 |
delete(idm, c.ID) |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
// Remove chunks referenced in ASCII text files |
| 87 |
for _, f := range opt.ignoreChunks { |
| 88 |
ids, err := readChunkIDFile(f) |
| 89 |
if err != nil { |
| 90 |
return err |
| 91 |
} |
| 92 |
for _, id := range ids { |
| 93 |
delete(idm, id) |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
// Now put the IDs into an array for further processing |
| 99 |
ids := make([]desync.ChunkID, 0, len(idm)) |
| 100 |
for id := range idm { |
| 101 |
ids = append(ids, id) |
| 102 |
} |
| 103 |
|
| 104 |
s, err := multiStoreWithRouter(opt.cmdStoreOptions, opt.stores...) |
| 105 |
if err != nil { |
| 106 |
return err |
| 107 |
} |
| 108 |
defer s.Close() |
| 109 |
|
| 110 |
dst, err := WritableStore(opt.cache, opt.cmdStoreOptions) |
| 111 |
if err != nil { |
| 112 |
return err |
| 113 |
} |
| 114 |
defer dst.Close() |
| 115 |
|
| 116 |
// If this is a terminal, we want a progress bar |
| 117 |
pb := desync.NewProgressBar("") |
| 118 |
|
| 119 |
// Pull all the chunks, and load them into the cache in the process |
| 120 |
return desync.Copy(ctx, ids, s, dst, opt.n, pb) |
| 121 |
} |