1
0
mirror of https://github.com/restic/restic.git synced 2024-07-02 08:40:55 +02:00
restic/internal/config.go

81 lines
1.7 KiB
Go
Raw Normal View History

2016-08-31 22:39:36 +02:00
package restic
2015-07-02 22:36:31 +02:00
import (
2017-06-04 11:16:55 +02:00
"context"
"testing"
2015-07-02 22:36:31 +02:00
2016-09-01 22:17:37 +02:00
"restic/errors"
"restic/debug"
"github.com/restic/chunker"
2015-07-02 22:36:31 +02:00
)
// Config contains the configuration for a repository.
type Config struct {
Version uint `json:"version"`
ID string `json:"id"`
ChunkerPolynomial chunker.Pol `json:"chunker_polynomial"`
}
// RepoVersion is the version that is written to the config when a repository
// is newly created with Init().
const RepoVersion = 1
// JSONUnpackedLoader loads unpacked JSON.
type JSONUnpackedLoader interface {
2017-06-04 11:16:55 +02:00
LoadJSONUnpacked(context.Context, FileType, ID, interface{}) error
2015-07-02 22:36:31 +02:00
}
// CreateConfig creates a config file with a randomly selected polynomial and
// ID.
func CreateConfig() (Config, error) {
2015-07-02 22:36:31 +02:00
var (
err error
cfg Config
)
cfg.ChunkerPolynomial, err = chunker.RandomPolynomial()
if err != nil {
2016-08-29 22:16:58 +02:00
return Config{}, errors.Wrap(err, "chunker.RandomPolynomial")
2015-07-02 22:36:31 +02:00
}
cfg.ID = NewRandomID().String()
2015-07-02 22:36:31 +02:00
cfg.Version = RepoVersion
2016-09-27 22:35:08 +02:00
debug.Log("New config: %#v", cfg)
return cfg, nil
2015-07-02 22:36:31 +02:00
}
// TestCreateConfig creates a config for use within tests.
func TestCreateConfig(t testing.TB, pol chunker.Pol) (cfg Config) {
cfg.ChunkerPolynomial = pol
cfg.ID = NewRandomID().String()
cfg.Version = RepoVersion
return cfg
}
2015-07-02 22:36:31 +02:00
// LoadConfig returns loads, checks and returns the config for a repository.
2017-06-04 11:16:55 +02:00
func LoadConfig(ctx context.Context, r JSONUnpackedLoader) (Config, error) {
2015-07-02 22:36:31 +02:00
var (
cfg Config
)
2017-06-04 11:16:55 +02:00
err := r.LoadJSONUnpacked(ctx, ConfigFile, ID{}, &cfg)
2015-07-02 22:36:31 +02:00
if err != nil {
return Config{}, err
}
if cfg.Version != RepoVersion {
return Config{}, errors.New("unsupported repository version")
}
if !cfg.ChunkerPolynomial.Irreducible() {
return Config{}, errors.New("invalid chunker polynomial")
}
return cfg, nil
}