backend: deduplicate implementation of Delete() method

This commit is contained in:
Michael Eischer 2023-04-07 23:56:16 +02:00
parent 45244fdf68
commit 05abc6d6f5
7 changed files with 32 additions and 145 deletions

View file

@ -384,30 +384,9 @@ func (be *Backend) List(ctx context.Context, t restic.FileType, fn func(restic.F
return ctx.Err() 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. // Delete removes all restic keys in the bucket. It will not remove the bucket itself.
func (be *Backend) Delete(ctx context.Context) error { func (be *Backend) Delete(ctx context.Context) error {
alltypes := []restic.FileType{ return backend.DefaultDelete(ctx, be)
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})
} }
// Close does nothing // Close does nothing

View file

@ -312,34 +312,9 @@ func (be *b2Backend) List(ctx context.Context, t restic.FileType, fn func(restic
return nil 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. // Delete removes all restic keys in the bucket. It will not remove the bucket itself.
func (be *b2Backend) Delete(ctx context.Context) error { func (be *b2Backend) Delete(ctx context.Context) error {
alltypes := []restic.FileType{ return backend.DefaultDelete(ctx, be)
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
} }
// Close does nothing // Close does nothing

View file

@ -339,30 +339,9 @@ func (be *Backend) List(ctx context.Context, t restic.FileType, fn func(restic.F
return ctx.Err() 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. // Delete removes all restic keys in the bucket. It will not remove the bucket itself.
func (be *Backend) Delete(ctx context.Context) error { func (be *Backend) Delete(ctx context.Context) error {
alltypes := []restic.FileType{ return backend.DefaultDelete(ctx, be)
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})
} }
// Close does nothing. // Close does nothing.

View file

@ -11,6 +11,7 @@ import (
"path" "path"
"strings" "strings"
"github.com/restic/restic/internal/backend"
"github.com/restic/restic/internal/backend/layout" "github.com/restic/restic/internal/backend/layout"
"github.com/restic/restic/internal/debug" "github.com/restic/restic/internal/debug"
"github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/errors"
@ -411,32 +412,7 @@ func (b *Backend) Close() error {
return nil 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. // Delete removes all data in the backend.
func (b *Backend) Delete(ctx context.Context) error { func (b *Backend) Delete(ctx context.Context) error {
alltypes := []restic.FileType{ return backend.DefaultDelete(ctx, b)
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
} }

View file

@ -411,30 +411,9 @@ func (be *Backend) List(ctx context.Context, t restic.FileType, fn func(restic.F
return ctx.Err() 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. // Delete removes all restic keys in the bucket. It will not remove the bucket itself.
func (be *Backend) Delete(ctx context.Context) error { func (be *Backend) Delete(ctx context.Context) error {
alltypes := []restic.FileType{ return backend.DefaultDelete(ctx, be)
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})
} }
// Close does nothing // Close does nothing

View file

@ -231,13 +231,6 @@ func (be *beSwift) List(ctx context.Context, t restic.FileType, fn func(restic.F
return ctx.Err() 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. // IsNotExist returns true if the error is caused by a not existing file.
func (be *beSwift) IsNotExist(err error) bool { func (be *beSwift) IsNotExist(err error) bool {
var e *swift.Error var e *swift.Error
@ -247,26 +240,7 @@ func (be *beSwift) IsNotExist(err error) bool {
// Delete removes all restic objects in the container. // Delete removes all restic objects in the container.
// It will not remove the container itself. // It will not remove the container itself.
func (be *beSwift) Delete(ctx context.Context) error { func (be *beSwift) Delete(ctx context.Context) error {
alltypes := []restic.FileType{ return backend.DefaultDelete(ctx, be)
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
} }
// Close does nothing // Close does nothing

View file

@ -75,6 +75,31 @@ func DefaultLoad(ctx context.Context, h restic.Handle, length int, offset int64,
return rd.Close() 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 { type memorizedLister struct {
fileInfos []restic.FileInfo fileInfos []restic.FileInfo
tpe restic.FileType tpe restic.FileType