2016-08-31 20:39:36 +00:00
|
|
|
package restic
|
2015-07-02 20:36:31 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
|
|
|
"io"
|
2016-07-31 15:45:22 +00:00
|
|
|
"testing"
|
2015-07-02 20:36:31 +00:00
|
|
|
|
2016-08-21 15:46:23 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2016-02-14 14:29:28 +00:00
|
|
|
"restic/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"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// repositoryIDSize is the length of the ID chosen at random for a new repository.
|
|
|
|
const repositoryIDSize = sha256.Size
|
|
|
|
|
|
|
|
// RepoVersion is the version that is written to the config when a repository
|
|
|
|
// is newly created with Init().
|
|
|
|
const RepoVersion = 1
|
|
|
|
|
|
|
|
// JSONUnpackedSaver saves unpacked JSON.
|
|
|
|
type JSONUnpackedSaver interface {
|
2016-08-31 20:39:36 +00:00
|
|
|
SaveJSONUnpacked(FileType, interface{}) (ID, error)
|
2015-07-02 20:36:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// JSONUnpackedLoader loads unpacked JSON.
|
|
|
|
type JSONUnpackedLoader interface {
|
2016-08-31 20:39:36 +00:00
|
|
|
LoadJSONUnpacked(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.
|
|
|
|
func CreateConfig() (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
|
|
|
}
|
|
|
|
|
|
|
|
newID := make([]byte, repositoryIDSize)
|
|
|
|
_, err = io.ReadFull(rand.Reader, newID)
|
|
|
|
if err != nil {
|
2016-08-29 20:16:58 +00:00
|
|
|
return Config{}, errors.Wrap(err, "io.ReadFull")
|
2015-07-02 20:36:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cfg.ID = hex.EncodeToString(newID)
|
|
|
|
cfg.Version = RepoVersion
|
|
|
|
|
|
|
|
debug.Log("Repo.CreateConfig", "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.
|
|
|
|
func TestCreateConfig(t testing.TB, pol chunker.Pol) (cfg Config) {
|
|
|
|
cfg.ChunkerPolynomial = pol
|
|
|
|
|
|
|
|
newID := make([]byte, repositoryIDSize)
|
|
|
|
_, err := io.ReadFull(rand.Reader, newID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create random ID: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg.ID = hex.EncodeToString(newID)
|
|
|
|
cfg.Version = RepoVersion
|
|
|
|
|
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
|
2015-07-02 20:36:31 +00:00
|
|
|
// LoadConfig returns loads, checks and returns the config for a repository.
|
|
|
|
func LoadConfig(r JSONUnpackedLoader) (Config, error) {
|
|
|
|
var (
|
|
|
|
cfg Config
|
|
|
|
)
|
|
|
|
|
2016-08-31 20:39:36 +00:00
|
|
|
err := r.LoadJSONUnpacked(ConfigFile, ID{}, &cfg)
|
2015-07-02 20:36:31 +00: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
|
|
|
|
}
|