2016-08-31 20:39:36 +00:00
|
|
|
package restic
|
2015-07-02 20:36:31 +00:00
|
|
|
|
|
|
|
import (
|
2017-06-04 09:16:55 +00:00
|
|
|
"context"
|
2016-07-31 15:45:22 +00:00
|
|
|
"testing"
|
2015-07-02 20:36:31 +00:00
|
|
|
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/errors"
|
2016-08-21 15:46:23 +00:00
|
|
|
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/debug"
|
2016-07-31 14:27:36 +00:00
|
|
|
|
|
|
|
"github.com/restic/chunker"
|
2015-07-02 20:36:31 +00: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"`
|
|
|
|
}
|
|
|
|
|
2022-02-12 23:52:03 +00:00
|
|
|
const MinRepoVersion = 1
|
|
|
|
const MaxRepoVersion = 2
|
|
|
|
|
|
|
|
// StableRepoVersion is the version that is written to the config when a repository
|
2015-07-02 20:36:31 +00:00
|
|
|
// is newly created with Init().
|
2022-04-11 19:06:37 +00:00
|
|
|
const StableRepoVersion = 2
|
2015-07-02 20:36:31 +00:00
|
|
|
|
|
|
|
// JSONUnpackedLoader loads unpacked JSON.
|
|
|
|
type JSONUnpackedLoader interface {
|
2017-06-04 09:16:55 +00:00
|
|
|
LoadJSONUnpacked(context.Context, FileType, ID, interface{}) error
|
2015-07-02 20:36:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateConfig creates a config file with a randomly selected polynomial and
|
2016-07-31 14:27:36 +00:00
|
|
|
// ID.
|
2022-02-12 23:52:03 +00:00
|
|
|
func CreateConfig(version uint) (Config, error) {
|
2015-07-02 20:36:31 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
cfg Config
|
|
|
|
)
|
|
|
|
|
|
|
|
cfg.ChunkerPolynomial, err = chunker.RandomPolynomial()
|
|
|
|
if err != nil {
|
2016-08-29 20:16:58 +00:00
|
|
|
return Config{}, errors.Wrap(err, "chunker.RandomPolynomial")
|
2015-07-02 20:36:31 +00:00
|
|
|
}
|
|
|
|
|
2016-09-01 19:37:59 +00:00
|
|
|
cfg.ID = NewRandomID().String()
|
2022-02-12 23:52:03 +00:00
|
|
|
cfg.Version = version
|
2015-07-02 20:36:31 +00:00
|
|
|
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("New config: %#v", cfg)
|
2016-07-31 14:27:36 +00:00
|
|
|
return cfg, nil
|
2015-07-02 20:36:31 +00:00
|
|
|
}
|
|
|
|
|
2016-07-31 15:45:22 +00:00
|
|
|
// TestCreateConfig creates a config for use within tests.
|
2022-04-29 21:16:16 +00:00
|
|
|
func TestCreateConfig(t testing.TB, pol chunker.Pol, version uint) (cfg Config) {
|
2016-07-31 15:45:22 +00:00
|
|
|
cfg.ChunkerPolynomial = pol
|
|
|
|
|
2016-09-01 19:37:59 +00:00
|
|
|
cfg.ID = NewRandomID().String()
|
2022-04-29 21:16:16 +00:00
|
|
|
if version == 0 {
|
|
|
|
version = StableRepoVersion
|
|
|
|
}
|
|
|
|
if version < MinRepoVersion || version > MaxRepoVersion {
|
|
|
|
t.Fatalf("version %d is out of range", version)
|
|
|
|
}
|
|
|
|
cfg.Version = version
|
2016-07-31 15:45:22 +00:00
|
|
|
|
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
|
2018-03-11 19:59:40 +00:00
|
|
|
var checkPolynomial = true
|
|
|
|
|
|
|
|
// TestDisableCheckPolynomial disables the check that the polynomial used for
|
|
|
|
// the chunker.
|
|
|
|
func TestDisableCheckPolynomial(t testing.TB) {
|
|
|
|
t.Logf("disabling check of the chunker polynomial")
|
|
|
|
checkPolynomial = false
|
|
|
|
}
|
|
|
|
|
2015-07-02 20:36:31 +00:00
|
|
|
// LoadConfig returns loads, checks and returns the config for a repository.
|
2022-06-12 12:38:19 +00:00
|
|
|
func LoadConfig(ctx context.Context, r LoaderUnpacked) (Config, error) {
|
2015-07-02 20:36:31 +00:00
|
|
|
var (
|
|
|
|
cfg Config
|
|
|
|
)
|
|
|
|
|
2022-06-12 12:38:19 +00:00
|
|
|
err := LoadJSONUnpacked(ctx, r, ConfigFile, ID{}, &cfg)
|
2015-07-02 20:36:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return Config{}, err
|
|
|
|
}
|
|
|
|
|
2022-02-12 23:52:03 +00:00
|
|
|
if cfg.Version < MinRepoVersion || cfg.Version > MaxRepoVersion {
|
2022-02-12 22:31:31 +00:00
|
|
|
return Config{}, errors.Errorf("unsupported repository version %v", cfg.Version)
|
2015-07-02 20:36:31 +00:00
|
|
|
}
|
|
|
|
|
2018-03-11 19:59:40 +00:00
|
|
|
if checkPolynomial {
|
|
|
|
if !cfg.ChunkerPolynomial.Irreducible() {
|
|
|
|
return Config{}, errors.New("invalid chunker polynomial")
|
|
|
|
}
|
2015-07-02 20:36:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return cfg, nil
|
|
|
|
}
|
2022-06-12 12:38:19 +00:00
|
|
|
|
|
|
|
func SaveConfig(ctx context.Context, r SaverUnpacked, cfg Config) error {
|
|
|
|
_, err := SaveJSONUnpacked(ctx, r, ConfigFile, cfg)
|
|
|
|
return err
|
|
|
|
}
|