Debian Codegraph @

cmd/desync/make.go

raw
1 package main
2
3 import (
4 "context"
5 "fmt"
6 "strconv"
7 "strings"
8
9 "github.com/folbricht/desync"
10 "github.com/pkg/errors"
11 "github.com/spf13/cobra"
12 )
13
14 type makeOptions struct {
15 cmdStoreOptions
16 store string
17 chunkSize string
18 printStats bool
19 }
20
21 func newMakeCommand(ctx context.Context) *cobra.Command {
22 var opt makeOptions
23
24 cmd := &cobra.Command{
25 Use: "make <index> <file>",
26 Short: "Chunk input file and create index",
27 Long: `Creates chunks from the input file and builds an index. If a chunk store is
28 provided with -s, such as a local directory or S3 store, it splits the input
29 file according to the index and stores the chunks. Use '-' to write the index
30 to STDOUT.`,
31 Example: ` desync make -s /path/to/local file.caibx largefile.bin
32 desync make -m 8:32:128 - largefile.bin > file.caibx`,
33 Args: cobra.ExactArgs(2),
34 RunE: func(cmd *cobra.Command, args []string) error {
35 return runMake(ctx, opt, args)
36 },
37 SilenceUsage: true,
38 }
39 flags := cmd.Flags()
40 flags.StringVarP(&opt.store, "store", "s", "", "target store")
41 flags.StringVarP(&opt.chunkSize, "chunk-size", "m", "16:64:256", "min:avg:max chunk size in kb")
42 flags.BoolVarP(&opt.printStats, "print-stats", "", false, "print chunking statistics to stderr when done")
43 addStoreOptions(&opt.cmdStoreOptions, flags)
44 return cmd
45 }
46
47 func runMake(ctx context.Context, opt makeOptions, args []string) error {
48 if err := opt.cmdStoreOptions.validate(); err != nil {
49 return err
50 }
51
52 min, avg, max, err := parseChunkSizeParam(opt.chunkSize)
53 if err != nil {
54 return err
55 }
56
57 indexFile := args[0]
58 dataFile := args[1]
59
60 // Open the target store if one was given
61 var s desync.WriteStore
62 if opt.store != "" {
63 s, err = WritableStore(opt.store, opt.cmdStoreOptions)
64 if err != nil {
65 return err
66 }
67 defer s.Close()
68 }
69
70 // Split up the file and create and index from it
71 pb := desync.NewProgressBar("Chunking ")
72 index, stats, err := desync.IndexFromFile(ctx, dataFile, opt.n, min, avg, max, pb)
73 if err != nil {
74 return err
75 }
76
77 // Chop up the file into chunks and store them in the target store if a store was given
78 if s != nil {
79 pb := desync.NewProgressBar("Storing ")
80 if err := desync.ChopFile(ctx, dataFile, index.Chunks, s, opt.n, pb); err != nil {
81 return err
82 }
83 }
84 if opt.printStats {
85 printJSON(stderr, stats) // write to stderr since stdout could be used for index data
86 }
87 return storeCaibxFile(index, indexFile, opt.cmdStoreOptions)
88 }
89
90 func parseChunkSizeParam(s string) (min, avg, max uint64, err error) {
91 sizes := strings.Split(s, ":")
92 if len(sizes) != 3 {
93 return 0, 0, 0, fmt.Errorf("invalid chunk size '%s'", s)
94 }
95 num, err := strconv.Atoi(sizes[0])
96 if err != nil {
97 return 0, 0, 0, errors.Wrap(err, "min chunk size")
98 }
99 min = uint64(num) * 1024
100 num, err = strconv.Atoi(sizes[1])
101 if err != nil {
102 return 0, 0, 0, errors.Wrap(err, "avg chunk size")
103 }
104 avg = uint64(num) * 1024
105 num, err = strconv.Atoi(sizes[2])
106 if err != nil {
107 return 0, 0, 0, errors.Wrap(err, "max chunk size")
108 }
109 max = uint64(num) * 1024
110 return
111 }