mirror of
https://github.com/restic/restic.git
synced 2025-12-04 02:41:52 +00:00
repository: extract Load/StoreJSONUnpacked
A Load/Store method for each data type is much clearer. As a result the repository no longer needs a method to load / store json.
This commit is contained in:
@@ -76,12 +76,12 @@ func TestDisableCheckPolynomial(t testing.TB) {
|
||||
}
|
||||
|
||||
// LoadConfig returns loads, checks and returns the config for a repository.
|
||||
func LoadConfig(ctx context.Context, r JSONUnpackedLoader) (Config, error) {
|
||||
func LoadConfig(ctx context.Context, r LoaderUnpacked) (Config, error) {
|
||||
var (
|
||||
cfg Config
|
||||
)
|
||||
|
||||
err := r.LoadJSONUnpacked(ctx, ConfigFile, ID{}, &cfg)
|
||||
err := LoadJSONUnpacked(ctx, r, ConfigFile, ID{}, &cfg)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
@@ -98,3 +98,8 @@ func LoadConfig(ctx context.Context, r JSONUnpackedLoader) (Config, error) {
|
||||
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func SaveConfig(ctx context.Context, r SaverUnpacked, cfg Config) error {
|
||||
_, err := SaveJSONUnpacked(ctx, r, ConfigFile, cfg)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -8,47 +8,56 @@ import (
|
||||
rtest "github.com/restic/restic/internal/test"
|
||||
)
|
||||
|
||||
type saver func(restic.FileType, interface{}) (restic.ID, error)
|
||||
|
||||
func (s saver) SaveJSONUnpacked(t restic.FileType, arg interface{}) (restic.ID, error) {
|
||||
return s(t, arg)
|
||||
type saver struct {
|
||||
fn func(restic.FileType, []byte) (restic.ID, error)
|
||||
}
|
||||
|
||||
type loader func(context.Context, restic.FileType, restic.ID, interface{}) error
|
||||
func (s saver) SaveUnpacked(ctx context.Context, t restic.FileType, buf []byte) (restic.ID, error) {
|
||||
return s.fn(t, buf)
|
||||
}
|
||||
|
||||
func (l loader) LoadJSONUnpacked(ctx context.Context, t restic.FileType, id restic.ID, arg interface{}) error {
|
||||
return l(ctx, t, id, arg)
|
||||
func (s saver) Connections() uint {
|
||||
return 2
|
||||
}
|
||||
|
||||
type loader struct {
|
||||
fn func(restic.FileType, restic.ID, []byte) ([]byte, error)
|
||||
}
|
||||
|
||||
func (l loader) LoadUnpacked(ctx context.Context, t restic.FileType, id restic.ID, buf []byte) (data []byte, err error) {
|
||||
return l.fn(t, id, buf)
|
||||
}
|
||||
|
||||
func (l loader) Connections() uint {
|
||||
return 2
|
||||
}
|
||||
|
||||
func TestConfig(t *testing.T) {
|
||||
resultConfig := restic.Config{}
|
||||
save := func(tpe restic.FileType, arg interface{}) (restic.ID, error) {
|
||||
var resultBuf []byte
|
||||
save := func(tpe restic.FileType, buf []byte) (restic.ID, error) {
|
||||
rtest.Assert(t, tpe == restic.ConfigFile,
|
||||
"wrong backend type: got %v, wanted %v",
|
||||
tpe, restic.ConfigFile)
|
||||
|
||||
cfg := arg.(restic.Config)
|
||||
resultConfig = cfg
|
||||
resultBuf = buf
|
||||
return restic.ID{}, nil
|
||||
}
|
||||
|
||||
cfg1, err := restic.CreateConfig(restic.MaxRepoVersion)
|
||||
rtest.OK(t, err)
|
||||
|
||||
_, err = saver(save).SaveJSONUnpacked(restic.ConfigFile, cfg1)
|
||||
err = restic.SaveConfig(context.TODO(), saver{save}, cfg1)
|
||||
rtest.OK(t, err)
|
||||
|
||||
load := func(ctx context.Context, tpe restic.FileType, id restic.ID, arg interface{}) error {
|
||||
load := func(tpe restic.FileType, id restic.ID, in []byte) ([]byte, error) {
|
||||
rtest.Assert(t, tpe == restic.ConfigFile,
|
||||
"wrong backend type: got %v, wanted %v",
|
||||
tpe, restic.ConfigFile)
|
||||
|
||||
cfg := arg.(*restic.Config)
|
||||
*cfg = resultConfig
|
||||
return nil
|
||||
return resultBuf, nil
|
||||
}
|
||||
|
||||
cfg2, err := restic.LoadConfig(context.TODO(), loader(load))
|
||||
cfg2, err := restic.LoadConfig(context.TODO(), loader{load})
|
||||
rtest.OK(t, err)
|
||||
|
||||
rtest.Assert(t, cfg1 == cfg2,
|
||||
|
||||
32
internal/restic/json.go
Normal file
32
internal/restic/json.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package restic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/restic/restic/internal/debug"
|
||||
"github.com/restic/restic/internal/errors"
|
||||
)
|
||||
|
||||
// LoadJSONUnpacked decrypts the data and afterwards calls json.Unmarshal on
|
||||
// the item.
|
||||
func LoadJSONUnpacked(ctx context.Context, repo LoaderUnpacked, t FileType, id ID, item interface{}) (err error) {
|
||||
buf, err := repo.LoadUnpacked(ctx, t, id, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return json.Unmarshal(buf, item)
|
||||
}
|
||||
|
||||
// SaveJSONUnpacked serialises item as JSON and encrypts and saves it in the
|
||||
// backend as type t, without a pack. It returns the storage hash.
|
||||
func SaveJSONUnpacked(ctx context.Context, repo SaverUnpacked, t FileType, item interface{}) (ID, error) {
|
||||
debug.Log("save new blob %v", t)
|
||||
plaintext, err := json.Marshal(item)
|
||||
if err != nil {
|
||||
return ID{}, errors.Wrap(err, "json.Marshal")
|
||||
}
|
||||
|
||||
return repo.SaveUnpacked(ctx, t, plaintext)
|
||||
}
|
||||
@@ -158,7 +158,7 @@ func (l *Lock) checkForOtherLocks(ctx context.Context) error {
|
||||
|
||||
// createLock acquires the lock by creating a file in the repository.
|
||||
func (l *Lock) createLock(ctx context.Context) (ID, error) {
|
||||
id, err := l.repo.SaveJSONUnpacked(ctx, LockFile, l)
|
||||
id, err := SaveJSONUnpacked(ctx, l.repo, LockFile, l)
|
||||
if err != nil {
|
||||
return ID{}, err
|
||||
}
|
||||
@@ -255,7 +255,7 @@ func init() {
|
||||
// LoadLock loads and unserializes a lock from a repository.
|
||||
func LoadLock(ctx context.Context, repo Repository, id ID) (*Lock, error) {
|
||||
lock := &Lock{}
|
||||
if err := repo.LoadJSONUnpacked(ctx, LockFile, id, lock); err != nil {
|
||||
if err := LoadJSONUnpacked(ctx, repo, LockFile, id, lock); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
lock.lockID = &id
|
||||
|
||||
@@ -99,7 +99,7 @@ func createFakeLock(repo restic.Repository, t time.Time, pid int) (restic.ID, er
|
||||
}
|
||||
|
||||
newLock := &restic.Lock{Time: t, PID: pid, Hostname: hostname}
|
||||
return repo.SaveJSONUnpacked(context.TODO(), restic.LockFile, &newLock)
|
||||
return restic.SaveJSONUnpacked(context.TODO(), repo, restic.LockFile, &newLock)
|
||||
}
|
||||
|
||||
func removeLock(repo restic.Repository, id restic.ID) error {
|
||||
|
||||
@@ -37,24 +37,20 @@ type Repository interface {
|
||||
// the the pack header.
|
||||
ListPack(context.Context, ID, int64) ([]Blob, uint32, error)
|
||||
|
||||
LoadBlob(context.Context, BlobType, ID, []byte) ([]byte, error)
|
||||
SaveBlob(context.Context, BlobType, []byte, ID, bool) (ID, bool, int, error)
|
||||
|
||||
// StartPackUploader start goroutines to upload new pack files. The errgroup
|
||||
// is used to immediately notify about an upload error. Flush() will also return
|
||||
// that error.
|
||||
StartPackUploader(ctx context.Context, wg *errgroup.Group)
|
||||
Flush(context.Context) error
|
||||
|
||||
SaveJSONUnpacked(context.Context, FileType, interface{}) (ID, error)
|
||||
|
||||
LoadJSONUnpacked(ctx context.Context, t FileType, id ID, dest interface{}) error
|
||||
|
||||
// LoadUnpacked loads and decrypts the file with the given type and ID,
|
||||
// using the supplied buffer (which must be empty). If the buffer is nil, a
|
||||
// new buffer will be allocated and returned.
|
||||
LoadUnpacked(ctx context.Context, buf []byte, t FileType, id ID) (data []byte, err error)
|
||||
LoadUnpacked(ctx context.Context, t FileType, id ID, buf []byte) (data []byte, err error)
|
||||
SaveUnpacked(context.Context, FileType, []byte) (ID, error)
|
||||
|
||||
LoadBlob(context.Context, BlobType, ID, []byte) ([]byte, error)
|
||||
SaveBlob(context.Context, BlobType, []byte, ID, bool) (ID, bool, int, error)
|
||||
}
|
||||
|
||||
// Lister allows listing files in a backend.
|
||||
@@ -62,16 +58,11 @@ type Lister interface {
|
||||
List(context.Context, FileType, func(FileInfo) error) error
|
||||
}
|
||||
|
||||
// LoadJSONUnpackeder allows loading a JSON file not stored in a pack file
|
||||
type LoadJSONUnpackeder interface {
|
||||
// Connections returns the maximum number of concurrent backend operations
|
||||
Connections() uint
|
||||
LoadJSONUnpacked(ctx context.Context, t FileType, id ID, dest interface{}) error
|
||||
}
|
||||
|
||||
// LoaderUnpacked allows loading a blob not stored in a pack file
|
||||
type LoaderUnpacked interface {
|
||||
LoadUnpacked(ctx context.Context, buf []byte, t FileType, id ID) (data []byte, err error)
|
||||
// Connections returns the maximum number of concurrent backend operations
|
||||
Connections() uint
|
||||
LoadUnpacked(ctx context.Context, t FileType, id ID, buf []byte) (data []byte, err error)
|
||||
}
|
||||
|
||||
// SaverUnpacked allows saving a blob not stored in a pack file
|
||||
|
||||
@@ -59,9 +59,9 @@ func NewSnapshot(paths []string, tags []string, hostname string, time time.Time)
|
||||
}
|
||||
|
||||
// LoadSnapshot loads the snapshot with the id and returns it.
|
||||
func LoadSnapshot(ctx context.Context, loader LoadJSONUnpackeder, id ID) (*Snapshot, error) {
|
||||
func LoadSnapshot(ctx context.Context, loader LoaderUnpacked, id ID) (*Snapshot, error) {
|
||||
sn := &Snapshot{id: &id}
|
||||
err := loader.LoadJSONUnpacked(ctx, SnapshotFile, id, sn)
|
||||
err := LoadJSONUnpacked(ctx, loader, SnapshotFile, id, sn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -69,12 +69,17 @@ func LoadSnapshot(ctx context.Context, loader LoadJSONUnpackeder, id ID) (*Snaps
|
||||
return sn, nil
|
||||
}
|
||||
|
||||
// SaveSnapshot saves the snapshot sn and returns its ID.
|
||||
func SaveSnapshot(ctx context.Context, repo SaverUnpacked, sn *Snapshot) (ID, error) {
|
||||
return SaveJSONUnpacked(ctx, repo, SnapshotFile, sn)
|
||||
}
|
||||
|
||||
// ForAllSnapshots reads all snapshots in parallel and calls the
|
||||
// given function. It is guaranteed that the function is not run concurrently.
|
||||
// If the called function returns an error, this function is cancelled and
|
||||
// also returns this error.
|
||||
// If a snapshot ID is in excludeIDs, it will be ignored.
|
||||
func ForAllSnapshots(ctx context.Context, be Lister, loader LoadJSONUnpackeder, excludeIDs IDSet, fn func(ID, *Snapshot, error) error) error {
|
||||
func ForAllSnapshots(ctx context.Context, be Lister, loader LoaderUnpacked, excludeIDs IDSet, fn func(ID, *Snapshot, error) error) error {
|
||||
var m sync.Mutex
|
||||
|
||||
// track spawned goroutines using wg, create a new context which is
|
||||
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
var ErrNoSnapshotFound = errors.New("no snapshot found")
|
||||
|
||||
// FindLatestSnapshot finds latest snapshot with optional target/directory, tags, hostname, and timestamp filters.
|
||||
func FindLatestSnapshot(ctx context.Context, be Lister, loader LoadJSONUnpackeder, targets []string,
|
||||
func FindLatestSnapshot(ctx context.Context, be Lister, loader LoaderUnpacked, targets []string,
|
||||
tagLists []TagList, hostnames []string, timeStampLimit *time.Time) (ID, error) {
|
||||
|
||||
var err error
|
||||
@@ -92,7 +92,7 @@ func FindSnapshot(ctx context.Context, be Lister, s string) (ID, error) {
|
||||
|
||||
// FindFilteredSnapshots yields Snapshots filtered from the list of all
|
||||
// snapshots.
|
||||
func FindFilteredSnapshots(ctx context.Context, be Lister, loader LoadJSONUnpackeder, hosts []string, tags []TagList, paths []string) (Snapshots, error) {
|
||||
func FindFilteredSnapshots(ctx context.Context, be Lister, loader LoaderUnpacked, hosts []string, tags []TagList, paths []string) (Snapshots, error) {
|
||||
results := make(Snapshots, 0, 20)
|
||||
|
||||
err := ForAllSnapshots(ctx, be, loader, nil, func(id ID, sn *Snapshot, err error) error {
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
package restic_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/restic/restic/internal/repository"
|
||||
"github.com/restic/restic/internal/restic"
|
||||
rtest "github.com/restic/restic/internal/test"
|
||||
)
|
||||
@@ -24,3 +26,27 @@ func TestTagList(t *testing.T) {
|
||||
r := sn.HasTags(tags)
|
||||
rtest.Assert(t, r, "Failed to match untagged snapshot")
|
||||
}
|
||||
|
||||
func TestLoadJSONUnpacked(t *testing.T) {
|
||||
repository.TestAllVersions(t, testLoadJSONUnpacked)
|
||||
}
|
||||
|
||||
func testLoadJSONUnpacked(t *testing.T, version uint) {
|
||||
repo, cleanup := repository.TestRepositoryWithVersion(t, version)
|
||||
defer cleanup()
|
||||
|
||||
// archive a snapshot
|
||||
sn := restic.Snapshot{}
|
||||
sn.Hostname = "foobar"
|
||||
sn.Username = "test!"
|
||||
|
||||
id, err := restic.SaveSnapshot(context.TODO(), repo, &sn)
|
||||
rtest.OK(t, err)
|
||||
|
||||
// restore
|
||||
sn2, err := restic.LoadSnapshot(context.TODO(), repo, id)
|
||||
rtest.OK(t, err)
|
||||
|
||||
rtest.Equals(t, sn.Hostname, sn2.Hostname)
|
||||
rtest.Equals(t, sn.Username, sn2.Username)
|
||||
}
|
||||
|
||||
@@ -181,7 +181,7 @@ func TestCreateSnapshot(t testing.TB, repo Repository, at time.Time, depth int,
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
id, err := repo.SaveJSONUnpacked(context.TODO(), SnapshotFile, snapshot)
|
||||
id, err := SaveSnapshot(context.TODO(), repo, snapshot)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user