Skip to content

Commit

Permalink
Merge pull request restic#4802 from MichaelEischer/backend-cleanups
Browse files Browse the repository at this point in the history
Repository: Remove Backend() method
  • Loading branch information
MichaelEischer committed May 18, 2024
2 parents eb6c653 + 223aa22 commit 1dfe1b8
Show file tree
Hide file tree
Showing 52 changed files with 614 additions and 648 deletions.
3 changes: 2 additions & 1 deletion cmd/restic/cmd_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/restic/restic/internal/checker"
"github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/fs"
"github.com/restic/restic/internal/repository"
"github.com/restic/restic/internal/restic"
"github.com/restic/restic/internal/ui"
)
Expand Down Expand Up @@ -347,7 +348,7 @@ func runCheck(ctx context.Context, opts CheckOptions, gopts GlobalOptions, args
for err := range errChan {
errorsFound = true
Warnf("%v\n", err)
if err, ok := err.(*checker.ErrPackData); ok {
if err, ok := err.(*repository.ErrPackData); ok {
salvagePacks = append(salvagePacks, err.PackID)
}
}
Expand Down
20 changes: 5 additions & 15 deletions cmd/restic/cmd_debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"

"github.com/restic/restic/internal/backend"
"github.com/restic/restic/internal/crypto"
"github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/index"
Expand Down Expand Up @@ -475,21 +474,12 @@ func runDebugExamine(ctx context.Context, gopts GlobalOptions, opts DebugExamine
func examinePack(ctx context.Context, opts DebugExamineOptions, repo restic.Repository, id restic.ID) error {
Printf("examine %v\n", id)

h := backend.Handle{
Type: restic.PackFile,
Name: id.String(),
}
fi, err := repo.Backend().Stat(ctx, h)
if err != nil {
return err
}
Printf(" file size is %v\n", fi.Size)

buf, err := repo.LoadRaw(ctx, restic.PackFile, id)
// also process damaged pack files
if buf == nil {
return err
}
Printf(" file size is %v\n", len(buf))
gotID := restic.Hash(buf)
if !id.Equal(gotID) {
Printf(" wanted hash %v, got %v\n", id, gotID)
Expand All @@ -508,7 +498,7 @@ func examinePack(ctx context.Context, opts DebugExamineOptions, repo restic.Repo
continue
}

checkPackSize(blobs, fi.Size)
checkPackSize(blobs, len(buf))

err = loadBlobs(ctx, opts, repo, id, blobs)
if err != nil {
Expand All @@ -521,19 +511,19 @@ func examinePack(ctx context.Context, opts DebugExamineOptions, repo restic.Repo
Printf(" ========================================\n")
Printf(" inspect the pack itself\n")

blobs, _, err := repo.ListPack(ctx, id, fi.Size)
blobs, _, err := repo.ListPack(ctx, id, int64(len(buf)))
if err != nil {
return fmt.Errorf("pack %v: %v", id.Str(), err)
}
checkPackSize(blobs, fi.Size)
checkPackSize(blobs, len(buf))

if !blobsLoaded {
return loadBlobs(ctx, opts, repo, id, blobs)
}
return nil
}

func checkPackSize(blobs []restic.Blob, fileSize int64) {
func checkPackSize(blobs []restic.Blob, fileSize int) {
// track current size and offset
var size, offset uint64

Expand Down
4 changes: 0 additions & 4 deletions cmd/restic/cmd_prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,6 @@ func getUsedBlobs(ctx context.Context, repo restic.Repository, ignoreSnapshots r

err = restic.FindUsedBlobs(ctx, repo, snapshotTrees, usedBlobs, bar)
if err != nil {
if repo.Backend().IsNotExist(err) {
return nil, errors.Fatal("unable to load a tree from the repository: " + err.Error())
}

return nil, err
}
return usedBlobs, nil
Expand Down
7 changes: 2 additions & 5 deletions cmd/restic/cmd_rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"

"github.com/restic/restic/internal/backend"
"github.com/restic/restic/internal/debug"
"github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/repository"
Expand Down Expand Up @@ -181,8 +180,7 @@ func filterAndReplaceSnapshot(ctx context.Context, repo restic.Repository, sn *r
if dryRun {
Verbosef("would delete empty snapshot\n")
} else {
h := backend.Handle{Type: restic.SnapshotFile, Name: sn.ID().String()}
if err = repo.Backend().Remove(ctx, h); err != nil {
if err = repo.RemoveUnpacked(ctx, restic.SnapshotFile, *sn.ID()); err != nil {
return false, err
}
debug.Log("removed empty snapshot %v", sn.ID())
Expand Down Expand Up @@ -241,8 +239,7 @@ func filterAndReplaceSnapshot(ctx context.Context, repo restic.Repository, sn *r
Verbosef("saved new snapshot %v\n", id.Str())

if forget {
h := backend.Handle{Type: restic.SnapshotFile, Name: sn.ID().String()}
if err = repo.Backend().Remove(ctx, h); err != nil {
if err = repo.RemoveUnpacked(ctx, restic.SnapshotFile, *sn.ID()); err != nil {
return false, err
}
debug.Log("removed old snapshot %v", sn.ID())
Expand Down
4 changes: 1 addition & 3 deletions cmd/restic/cmd_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/spf13/cobra"

"github.com/restic/restic/internal/backend"
"github.com/restic/restic/internal/debug"
"github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/repository"
Expand Down Expand Up @@ -86,8 +85,7 @@ func changeTags(ctx context.Context, repo *repository.Repository, sn *restic.Sna
debug.Log("new snapshot saved as %v", id)

// Remove the old snapshot.
h := backend.Handle{Type: restic.SnapshotFile, Name: sn.ID().String()}
if err = repo.Backend().Remove(ctx, h); err != nil {
if err = repo.RemoveUnpacked(ctx, restic.SnapshotFile, *sn.ID()); err != nil {
return false, err
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/restic/integration_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ func removePacks(gopts GlobalOptions, t testing.TB, remove restic.IDSet) {
defer unlock()

for id := range remove {
rtest.OK(t, r.Backend().Remove(ctx, backend.Handle{Type: restic.PackFile, Name: id.String()}))
rtest.OK(t, r.RemoveUnpacked(ctx, restic.PackFile, id))
}
}

Expand All @@ -291,7 +291,7 @@ func removePacksExcept(gopts GlobalOptions, t testing.TB, keep restic.IDSet, rem
if treePacks.Has(id) != removeTreePacks || keep.Has(id) {
return nil
}
return r.Backend().Remove(ctx, backend.Handle{Type: restic.PackFile, Name: id.String()})
return r.RemoveUnpacked(ctx, restic.PackFile, id)
}))
}

Expand Down
2 changes: 1 addition & 1 deletion internal/archiver/archiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1970,7 +1970,7 @@ func TestArchiverContextCanceled(t *testing.T) {
})

// Ensure that the archiver itself reports the canceled context and not just the backend
repo := repository.TestRepositoryWithBackend(t, &noCancelBackend{mem.New()}, 0, repository.Options{})
repo, _ := repository.TestRepositoryWithBackend(t, &noCancelBackend{mem.New()}, 0, repository.Options{})

back := rtest.Chdir(t, tempdir)
defer back()
Expand Down
5 changes: 0 additions & 5 deletions internal/backend/azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,6 @@ func (be *Backend) Connections() uint {
return be.connections
}

// Location returns this backend's location (the container name).
func (be *Backend) Location() string {
return be.Join(be.cfg.AccountName, be.cfg.Prefix)
}

// Hasher may return a hash function for calculating a content hash for the backend
func (be *Backend) Hasher() hash.Hash {
return md5.New()
Expand Down
5 changes: 0 additions & 5 deletions internal/backend/b2/b2.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,6 @@ func (be *b2Backend) Connections() uint {
return be.cfg.Connections
}

// Location returns the location for the backend.
func (be *b2Backend) Location() string {
return be.cfg.Bucket
}

// Hasher may return a hash function for calculating a content hash for the backend
func (be *b2Backend) Hasher() hash.Hash {
return nil
Expand Down
4 changes: 0 additions & 4 deletions internal/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ import (
// the context package need not be wrapped, as context cancellation is checked
// separately by the retrying logic.
type Backend interface {
// Location returns a string that describes the type and location of the
// repository.
Location() string

// Connections returns the maximum number of concurrent backend operations.
Connections() uint

Expand Down
5 changes: 0 additions & 5 deletions internal/backend/dryrun/dry_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ func (be *Backend) Connections() uint {
return be.b.Connections()
}

// Location returns the location of the backend.
func (be *Backend) Location() string {
return "DRY:" + be.b.Location()
}

// Delete removes all data in the backend.
func (be *Backend) Delete(_ context.Context) error {
return nil
Expand Down
6 changes: 0 additions & 6 deletions internal/backend/dryrun/dry_backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ func TestDry(t *testing.T) {
content string
wantErr string
}{
{d, "loc", "", "DRY:RAM", ""},
{d, "delete", "", "", ""},
{d, "stat", "a", "", "not found"},
{d, "list", "", "", ""},
Expand Down Expand Up @@ -76,11 +75,6 @@ func TestDry(t *testing.T) {
if files != step.content {
t.Errorf("%d. List = %q, want %q", i, files, step.content)
}
case "loc":
loc := step.be.Location()
if loc != step.content {
t.Errorf("%d. Location = %q, want %q", i, loc, step.content)
}
case "delete":
err = step.be.Delete(ctx)
case "remove":
Expand Down
5 changes: 0 additions & 5 deletions internal/backend/gs/gs.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,6 @@ func (be *Backend) Connections() uint {
return be.connections
}

// Location returns this backend's location (the bucket name).
func (be *Backend) Location() string {
return be.Join(be.bucketName, be.prefix)
}

// Hasher may return a hash function for calculating a content hash for the backend
func (be *Backend) Hasher() hash.Hash {
return md5.New()
Expand Down
5 changes: 0 additions & 5 deletions internal/backend/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,6 @@ func (b *Local) Connections() uint {
return b.Config.Connections
}

// Location returns this backend's location (the directory name).
func (b *Local) Location() string {
return b.Path
}

// Hasher may return a hash function for calculating a content hash for the backend
func (b *Local) Hasher() hash.Hash {
return nil
Expand Down
5 changes: 0 additions & 5 deletions internal/backend/mem/mem_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,6 @@ func (be *MemoryBackend) Connections() uint {
return connectionCount
}

// Location returns the location of the backend (RAM).
func (be *MemoryBackend) Location() string {
return "RAM"
}

// Hasher may return a hash function for calculating a content hash for the backend
func (be *MemoryBackend) Hasher() hash.Hash {
return xxhash.New()
Expand Down
10 changes: 0 additions & 10 deletions internal/backend/mock/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ type Backend struct {
RemoveFn func(ctx context.Context, h backend.Handle) error
DeleteFn func(ctx context.Context) error
ConnectionsFn func() uint
LocationFn func() string
HasherFn func() hash.Hash
HasAtomicReplaceFn func() bool
}
Expand Down Expand Up @@ -49,15 +48,6 @@ func (m *Backend) Connections() uint {
return m.ConnectionsFn()
}

// Location returns a location string.
func (m *Backend) Location() string {
if m.LocationFn == nil {
return ""
}

return m.LocationFn()
}

// Hasher may return a hash function for calculating a content hash for the backend
func (m *Backend) Hasher() hash.Hash {
if m.HasherFn == nil {
Expand Down
5 changes: 0 additions & 5 deletions internal/backend/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,6 @@ func (b *Backend) Connections() uint {
return b.connections
}

// Location returns this backend's location (the server's URL).
func (b *Backend) Location() string {
return b.url.String()
}

// Hasher may return a hash function for calculating a content hash for the backend
func (b *Backend) Hasher() hash.Hash {
return nil
Expand Down
5 changes: 0 additions & 5 deletions internal/backend/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,6 @@ func (be *Backend) Connections() uint {
return be.cfg.Connections
}

// Location returns this backend's location (the bucket name).
func (be *Backend) Location() string {
return be.Join(be.cfg.Bucket, be.cfg.Prefix)
}

// Hasher may return a hash function for calculating a content hash for the backend
func (be *Backend) Hasher() hash.Hash {
return nil
Expand Down
5 changes: 0 additions & 5 deletions internal/backend/sftp/sftp.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,6 @@ func (r *SFTP) Connections() uint {
return r.Config.Connections
}

// Location returns this backend's location (the directory name).
func (r *SFTP) Location() string {
return r.p
}

// Hasher may return a hash function for calculating a content hash for the backend
func (r *SFTP) Hasher() hash.Hash {
return nil
Expand Down
5 changes: 0 additions & 5 deletions internal/backend/swift/swift.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,6 @@ func (be *beSwift) Connections() uint {
return be.connections
}

// Location returns this backend's location (the container name).
func (be *beSwift) Location() string {
return be.container
}

// Hasher may return a hash function for calculating a content hash for the backend
func (be *beSwift) Hasher() hash.Hash {
return md5.New()
Expand Down
11 changes: 0 additions & 11 deletions internal/backend/test/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,6 @@ func (s *Suite[C]) TestCreateWithConfig(t *testing.T) {
}
}

// TestLocation tests that a location string is returned.
func (s *Suite[C]) TestLocation(t *testing.T) {
b := s.open(t)
defer s.close(t, b)

l := b.Location()
if l == "" {
t.Fatalf("invalid location string %q", l)
}
}

// TestConfig saves and loads a config from the backend.
func (s *Suite[C]) TestConfig(t *testing.T) {
b := s.open(t)
Expand Down

0 comments on commit 1dfe1b8

Please sign in to comment.