From 05abc6d6f5766d73c079a596db0d04ab8e777942 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Fri, 7 Apr 2023 23:56:16 +0200 Subject: [PATCH] backend: deduplicate implementation of Delete() method --- internal/backend/azure/azure.go | 23 +---------------------- internal/backend/b2/b2.go | 27 +-------------------------- internal/backend/gs/gs.go | 23 +---------------------- internal/backend/rest/rest.go | 28 ++-------------------------- internal/backend/s3/s3.go | 23 +---------------------- internal/backend/swift/swift.go | 28 +--------------------------- internal/backend/utils.go | 25 +++++++++++++++++++++++++ 7 files changed, 32 insertions(+), 145 deletions(-) diff --git a/internal/backend/azure/azure.go b/internal/backend/azure/azure.go index 82d55960f..9a3695f0f 100644 --- a/internal/backend/azure/azure.go +++ b/internal/backend/azure/azure.go @@ -384,30 +384,9 @@ func (be *Backend) List(ctx context.Context, t restic.FileType, fn func(restic.F return ctx.Err() } -// Remove keys for a specified backend type. -func (be *Backend) removeKeys(ctx context.Context, t restic.FileType) error { - return be.List(ctx, t, func(fi restic.FileInfo) error { - return be.Remove(ctx, restic.Handle{Type: t, Name: fi.Name}) - }) -} - // Delete removes all restic keys in the bucket. It will not remove the bucket itself. func (be *Backend) Delete(ctx context.Context) error { - alltypes := []restic.FileType{ - restic.PackFile, - restic.KeyFile, - restic.LockFile, - restic.SnapshotFile, - restic.IndexFile} - - for _, t := range alltypes { - err := be.removeKeys(ctx, t) - if err != nil { - return nil - } - } - - return be.Remove(ctx, restic.Handle{Type: restic.ConfigFile}) + return backend.DefaultDelete(ctx, be) } // Close does nothing diff --git a/internal/backend/b2/b2.go b/internal/backend/b2/b2.go index 0827f727b..738df198d 100644 --- a/internal/backend/b2/b2.go +++ b/internal/backend/b2/b2.go @@ -312,34 +312,9 @@ func (be *b2Backend) List(ctx context.Context, t restic.FileType, fn func(restic return nil } -// Remove keys for a specified backend type. -func (be *b2Backend) removeKeys(ctx context.Context, t restic.FileType) error { - return be.List(ctx, t, func(fi restic.FileInfo) error { - return be.Remove(ctx, restic.Handle{Type: t, Name: fi.Name}) - }) -} - // Delete removes all restic keys in the bucket. It will not remove the bucket itself. func (be *b2Backend) Delete(ctx context.Context) error { - alltypes := []restic.FileType{ - restic.PackFile, - restic.KeyFile, - restic.LockFile, - restic.SnapshotFile, - restic.IndexFile} - - for _, t := range alltypes { - err := be.removeKeys(ctx, t) - if err != nil { - return nil - } - } - err := be.Remove(ctx, restic.Handle{Type: restic.ConfigFile}) - if err != nil && be.IsNotExist(err) { - err = nil - } - - return err + return backend.DefaultDelete(ctx, be) } // Close does nothing diff --git a/internal/backend/gs/gs.go b/internal/backend/gs/gs.go index 12458a79c..de798ac92 100644 --- a/internal/backend/gs/gs.go +++ b/internal/backend/gs/gs.go @@ -339,30 +339,9 @@ func (be *Backend) List(ctx context.Context, t restic.FileType, fn func(restic.F return ctx.Err() } -// Remove keys for a specified backend type. -func (be *Backend) removeKeys(ctx context.Context, t restic.FileType) error { - return be.List(ctx, t, func(fi restic.FileInfo) error { - return be.Remove(ctx, restic.Handle{Type: t, Name: fi.Name}) - }) -} - // Delete removes all restic keys in the bucket. It will not remove the bucket itself. func (be *Backend) Delete(ctx context.Context) error { - alltypes := []restic.FileType{ - restic.PackFile, - restic.KeyFile, - restic.LockFile, - restic.SnapshotFile, - restic.IndexFile} - - for _, t := range alltypes { - err := be.removeKeys(ctx, t) - if err != nil { - return nil - } - } - - return be.Remove(ctx, restic.Handle{Type: restic.ConfigFile}) + return backend.DefaultDelete(ctx, be) } // Close does nothing. diff --git a/internal/backend/rest/rest.go b/internal/backend/rest/rest.go index f9030d076..7be5a07c7 100644 --- a/internal/backend/rest/rest.go +++ b/internal/backend/rest/rest.go @@ -11,6 +11,7 @@ import ( "path" "strings" + "github.com/restic/restic/internal/backend" "github.com/restic/restic/internal/backend/layout" "github.com/restic/restic/internal/debug" "github.com/restic/restic/internal/errors" @@ -411,32 +412,7 @@ func (b *Backend) Close() error { return nil } -// Remove keys for a specified backend type. -func (b *Backend) removeKeys(ctx context.Context, t restic.FileType) error { - return b.List(ctx, t, func(fi restic.FileInfo) error { - return b.Remove(ctx, restic.Handle{Type: t, Name: fi.Name}) - }) -} - // Delete removes all data in the backend. func (b *Backend) Delete(ctx context.Context) error { - alltypes := []restic.FileType{ - restic.PackFile, - restic.KeyFile, - restic.LockFile, - restic.SnapshotFile, - restic.IndexFile} - - for _, t := range alltypes { - err := b.removeKeys(ctx, t) - if err != nil { - return nil - } - } - - err := b.Remove(ctx, restic.Handle{Type: restic.ConfigFile}) - if err != nil && b.IsNotExist(err) { - return nil - } - return err + return backend.DefaultDelete(ctx, b) } diff --git a/internal/backend/s3/s3.go b/internal/backend/s3/s3.go index 591ad2185..7b7a761ce 100644 --- a/internal/backend/s3/s3.go +++ b/internal/backend/s3/s3.go @@ -411,30 +411,9 @@ func (be *Backend) List(ctx context.Context, t restic.FileType, fn func(restic.F return ctx.Err() } -// Remove keys for a specified backend type. -func (be *Backend) removeKeys(ctx context.Context, t restic.FileType) error { - return be.List(ctx, restic.PackFile, func(fi restic.FileInfo) error { - return be.Remove(ctx, restic.Handle{Type: t, Name: fi.Name}) - }) -} - // Delete removes all restic keys in the bucket. It will not remove the bucket itself. func (be *Backend) Delete(ctx context.Context) error { - alltypes := []restic.FileType{ - restic.PackFile, - restic.KeyFile, - restic.LockFile, - restic.SnapshotFile, - restic.IndexFile} - - for _, t := range alltypes { - err := be.removeKeys(ctx, t) - if err != nil { - return nil - } - } - - return be.Remove(ctx, restic.Handle{Type: restic.ConfigFile}) + return backend.DefaultDelete(ctx, be) } // Close does nothing diff --git a/internal/backend/swift/swift.go b/internal/backend/swift/swift.go index fcdbe6634..cfa9ed665 100644 --- a/internal/backend/swift/swift.go +++ b/internal/backend/swift/swift.go @@ -231,13 +231,6 @@ func (be *beSwift) List(ctx context.Context, t restic.FileType, fn func(restic.F return ctx.Err() } -// Remove keys for a specified backend type. -func (be *beSwift) removeKeys(ctx context.Context, t restic.FileType) error { - return be.List(ctx, t, func(fi restic.FileInfo) error { - return be.Remove(ctx, restic.Handle{Type: t, Name: fi.Name}) - }) -} - // IsNotExist returns true if the error is caused by a not existing file. func (be *beSwift) IsNotExist(err error) bool { var e *swift.Error @@ -247,26 +240,7 @@ func (be *beSwift) IsNotExist(err error) bool { // Delete removes all restic objects in the container. // It will not remove the container itself. func (be *beSwift) Delete(ctx context.Context) error { - alltypes := []restic.FileType{ - restic.PackFile, - restic.KeyFile, - restic.LockFile, - restic.SnapshotFile, - restic.IndexFile} - - for _, t := range alltypes { - err := be.removeKeys(ctx, t) - if err != nil { - return nil - } - } - - err := be.Remove(ctx, restic.Handle{Type: restic.ConfigFile}) - if err != nil && !be.IsNotExist(err) { - return err - } - - return nil + return backend.DefaultDelete(ctx, be) } // Close does nothing diff --git a/internal/backend/utils.go b/internal/backend/utils.go index bf8a7ad6d..cd6614f34 100644 --- a/internal/backend/utils.go +++ b/internal/backend/utils.go @@ -75,6 +75,31 @@ func DefaultLoad(ctx context.Context, h restic.Handle, length int, offset int64, return rd.Close() } +// DefaultDelete removes all restic keys in the bucket. It will not remove the bucket itself. +func DefaultDelete(ctx context.Context, be restic.Backend) error { + alltypes := []restic.FileType{ + restic.PackFile, + restic.KeyFile, + restic.LockFile, + restic.SnapshotFile, + restic.IndexFile} + + for _, t := range alltypes { + err := be.List(ctx, t, func(fi restic.FileInfo) error { + return be.Remove(ctx, restic.Handle{Type: t, Name: fi.Name}) + }) + if err != nil { + return nil + } + } + err := be.Remove(ctx, restic.Handle{Type: restic.ConfigFile}) + if err != nil && be.IsNotExist(err) { + err = nil + } + + return err +} + type memorizedLister struct { fileInfos []restic.FileInfo tpe restic.FileType