github.com/advanderveer/restic@v0.8.1-0.20171209104529-42a8c19aaea6/internal/restic/config.go (about) 1 package restic 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/restic/restic/internal/errors" 8 9 "github.com/restic/restic/internal/debug" 10 11 "github.com/restic/chunker" 12 ) 13 14 // Config contains the configuration for a repository. 15 type Config struct { 16 Version uint `json:"version"` 17 ID string `json:"id"` 18 ChunkerPolynomial chunker.Pol `json:"chunker_polynomial"` 19 } 20 21 // RepoVersion is the version that is written to the config when a repository 22 // is newly created with Init(). 23 const RepoVersion = 1 24 25 // JSONUnpackedLoader loads unpacked JSON. 26 type JSONUnpackedLoader interface { 27 LoadJSONUnpacked(context.Context, FileType, ID, interface{}) error 28 } 29 30 // CreateConfig creates a config file with a randomly selected polynomial and 31 // ID. 32 func CreateConfig() (Config, error) { 33 var ( 34 err error 35 cfg Config 36 ) 37 38 cfg.ChunkerPolynomial, err = chunker.RandomPolynomial() 39 if err != nil { 40 return Config{}, errors.Wrap(err, "chunker.RandomPolynomial") 41 } 42 43 cfg.ID = NewRandomID().String() 44 cfg.Version = RepoVersion 45 46 debug.Log("New config: %#v", cfg) 47 return cfg, nil 48 } 49 50 // TestCreateConfig creates a config for use within tests. 51 func TestCreateConfig(t testing.TB, pol chunker.Pol) (cfg Config) { 52 cfg.ChunkerPolynomial = pol 53 54 cfg.ID = NewRandomID().String() 55 cfg.Version = RepoVersion 56 57 return cfg 58 } 59 60 // LoadConfig returns loads, checks and returns the config for a repository. 61 func LoadConfig(ctx context.Context, r JSONUnpackedLoader) (Config, error) { 62 var ( 63 cfg Config 64 ) 65 66 err := r.LoadJSONUnpacked(ctx, ConfigFile, ID{}, &cfg) 67 if err != nil { 68 return Config{}, err 69 } 70 71 if cfg.Version != RepoVersion { 72 return Config{}, errors.New("unsupported repository version") 73 } 74 75 if !cfg.ChunkerPolynomial.Irreducible() { 76 return Config{}, errors.New("invalid chunker polynomial") 77 } 78 79 return cfg, nil 80 }