archiver: replace most uses of restic.Repository

This commit is contained in:
Michael Eischer 2024-05-19 15:11:32 +02:00
parent 864995271e
commit 6ca12c1b4a
4 changed files with 28 additions and 18 deletions

View file

@ -64,9 +64,19 @@ func (s *ItemStats) Add(other ItemStats) {
s.TreeSizeInRepo += other.TreeSizeInRepo s.TreeSizeInRepo += other.TreeSizeInRepo
} }
type archiverRepo interface {
restic.Loader
restic.BlobSaver
restic.SaverUnpacked
Config() restic.Config
StartPackUploader(ctx context.Context, wg *errgroup.Group)
Flush(ctx context.Context) error
}
// Archiver saves a directory structure to the repo. // Archiver saves a directory structure to the repo.
type Archiver struct { type Archiver struct {
Repo restic.Repository Repo archiverRepo
SelectByName SelectByNameFunc SelectByName SelectByNameFunc
Select SelectFunc Select SelectFunc
FS fs.FS FS fs.FS
@ -160,7 +170,7 @@ func (o Options) ApplyDefaults() Options {
} }
// New initializes a new archiver. // New initializes a new archiver.
func New(repo restic.Repository, fs fs.FS, opts Options) *Archiver { func New(repo archiverRepo, fs fs.FS, opts Options) *Archiver {
arch := &Archiver{ arch := &Archiver{
Repo: repo, Repo: repo,
SelectByName: func(_ string) bool { return true }, SelectByName: func(_ string) bool { return true },

View file

@ -36,7 +36,7 @@ func prepareTempdirRepoSrc(t testing.TB, src TestDir) (string, restic.Repository
return tempdir, repo return tempdir, repo
} }
func saveFile(t testing.TB, repo restic.Repository, filename string, filesystem fs.FS) (*restic.Node, ItemStats) { func saveFile(t testing.TB, repo archiverRepo, filename string, filesystem fs.FS) (*restic.Node, ItemStats) {
wg, ctx := errgroup.WithContext(context.TODO()) wg, ctx := errgroup.WithContext(context.TODO())
repo.StartPackUploader(ctx, wg) repo.StartPackUploader(ctx, wg)
@ -416,14 +416,14 @@ func BenchmarkArchiverSaveFileLarge(b *testing.B) {
} }
type blobCountingRepo struct { type blobCountingRepo struct {
restic.Repository archiverRepo
m sync.Mutex m sync.Mutex
saved map[restic.BlobHandle]uint saved map[restic.BlobHandle]uint
} }
func (repo *blobCountingRepo) SaveBlob(ctx context.Context, t restic.BlobType, buf []byte, id restic.ID, storeDuplicate bool) (restic.ID, bool, int, error) { func (repo *blobCountingRepo) SaveBlob(ctx context.Context, t restic.BlobType, buf []byte, id restic.ID, storeDuplicate bool) (restic.ID, bool, int, error) {
id, exists, size, err := repo.Repository.SaveBlob(ctx, t, buf, id, storeDuplicate) id, exists, size, err := repo.archiverRepo.SaveBlob(ctx, t, buf, id, storeDuplicate)
if exists { if exists {
return id, exists, size, err return id, exists, size, err
} }
@ -435,7 +435,7 @@ func (repo *blobCountingRepo) SaveBlob(ctx context.Context, t restic.BlobType, b
} }
func (repo *blobCountingRepo) SaveTree(ctx context.Context, t *restic.Tree) (restic.ID, error) { func (repo *blobCountingRepo) SaveTree(ctx context.Context, t *restic.Tree) (restic.ID, error) {
id, err := restic.SaveTree(ctx, repo.Repository, t) id, err := restic.SaveTree(ctx, repo.archiverRepo, t)
h := restic.BlobHandle{ID: id, Type: restic.TreeBlob} h := restic.BlobHandle{ID: id, Type: restic.TreeBlob}
repo.m.Lock() repo.m.Lock()
repo.saved[h]++ repo.saved[h]++
@ -465,8 +465,8 @@ func TestArchiverSaveFileIncremental(t *testing.T) {
tempdir := rtest.TempDir(t) tempdir := rtest.TempDir(t)
repo := &blobCountingRepo{ repo := &blobCountingRepo{
Repository: repository.TestRepository(t), archiverRepo: repository.TestRepository(t),
saved: make(map[restic.BlobHandle]uint), saved: make(map[restic.BlobHandle]uint),
} }
data := rtest.Random(23, 512*1024+887898) data := rtest.Random(23, 512*1024+887898)
@ -902,8 +902,8 @@ func TestArchiverSaveDirIncremental(t *testing.T) {
tempdir := rtest.TempDir(t) tempdir := rtest.TempDir(t)
repo := &blobCountingRepo{ repo := &blobCountingRepo{
Repository: repository.TestRepository(t), archiverRepo: repository.TestRepository(t),
saved: make(map[restic.BlobHandle]uint), saved: make(map[restic.BlobHandle]uint),
} }
appendToFile(t, filepath.Join(tempdir, "testfile"), []byte("foobar")) appendToFile(t, filepath.Join(tempdir, "testfile"), []byte("foobar"))
@ -2017,7 +2017,7 @@ func (m *TrackFS) OpenFile(name string, flag int, perm os.FileMode) (fs.File, er
} }
type failSaveRepo struct { type failSaveRepo struct {
restic.Repository archiverRepo
failAfter int32 failAfter int32
cnt int32 cnt int32
err error err error
@ -2029,7 +2029,7 @@ func (f *failSaveRepo) SaveBlob(ctx context.Context, t restic.BlobType, buf []by
return restic.Hash(buf), false, 0, f.err return restic.Hash(buf), false, 0, f.err
} }
return f.Repository.SaveBlob(ctx, t, buf, id, storeDuplicate) return f.archiverRepo.SaveBlob(ctx, t, buf, id, storeDuplicate)
} }
func TestArchiverAbortEarlyOnError(t *testing.T) { func TestArchiverAbortEarlyOnError(t *testing.T) {
@ -2105,9 +2105,9 @@ func TestArchiverAbortEarlyOnError(t *testing.T) {
} }
testRepo := &failSaveRepo{ testRepo := &failSaveRepo{
Repository: repo, archiverRepo: repo,
failAfter: int32(test.failAfter), failAfter: int32(test.failAfter),
err: test.err, err: test.err,
} }
// at most two files may be queued // at most two files may be queued
@ -2134,7 +2134,7 @@ func TestArchiverAbortEarlyOnError(t *testing.T) {
} }
} }
func snapshot(t testing.TB, repo restic.Repository, fs fs.FS, parent *restic.Snapshot, filename string) (*restic.Snapshot, *restic.Node) { func snapshot(t testing.TB, repo archiverRepo, fs fs.FS, parent *restic.Snapshot, filename string) (*restic.Snapshot, *restic.Node) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()

View file

@ -46,7 +46,7 @@ func wrapFileInfo(fi os.FileInfo) os.FileInfo {
return res return res
} }
func statAndSnapshot(t *testing.T, repo restic.Repository, name string) (*restic.Node, *restic.Node) { func statAndSnapshot(t *testing.T, repo archiverRepo, name string) (*restic.Node, *restic.Node) {
fi := lstat(t, name) fi := lstat(t, name)
want, err := restic.NodeFromFileInfo(name, fi, false) want, err := restic.NodeFromFileInfo(name, fi, false)
rtest.OK(t, err) rtest.OK(t, err)

View file

@ -25,7 +25,7 @@ func TestSnapshot(t testing.TB, repo restic.Repository, path string, parent *res
Tags: []string{"test"}, Tags: []string{"test"},
} }
if parent != nil { if parent != nil {
sn, err := restic.LoadSnapshot(context.TODO(), arch.Repo, *parent) sn, err := restic.LoadSnapshot(context.TODO(), repo, *parent)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }