backend: Add Delete() to restic.Backend interface

This commit is contained in:
Alexander Neumann 2017-10-14 13:38:17 +02:00
parent 3736f33ebf
commit 0e722efb09
4 changed files with 40 additions and 1 deletions

View File

@ -275,7 +275,7 @@ func (b *Local) List(ctx context.Context, t restic.FileType) <-chan string {
}
// Delete removes the repository and all files.
func (b *Local) Delete() error {
func (b *Local) Delete(ctx context.Context) error {
debug.Log("Delete()")
return fs.RemoveAll(b.Path)
}

View File

@ -349,3 +349,34 @@ func (b *restBackend) Close() error {
// same function.
return nil
}
// Remove keys for a specified backend type.
func (b *restBackend) removeKeys(ctx context.Context, t restic.FileType) error {
for key := range b.List(ctx, restic.DataFile) {
err := b.Remove(ctx, restic.Handle{Type: restic.DataFile, Name: key})
if err != nil {
return err
}
}
return nil
}
// Delete removes all data in the backend.
func (b *restBackend) Delete(ctx context.Context) error {
alltypes := []restic.FileType{
restic.DataFile,
restic.KeyFile,
restic.LockFile,
restic.SnapshotFile,
restic.IndexFile}
for _, t := range alltypes {
err := b.removeKeys(ctx, t)
if err != nil {
return nil
}
}
return b.Remove(ctx, restic.Handle{Type: restic.ConfigFile})
}

View File

@ -499,3 +499,8 @@ func (r *SFTP) Close() error {
<-r.result
return nil
}
// Delete removes all data in the backend.
func (r *SFTP) Delete(context.Context) error {
return r.c.RemoveDirectory(r.p)
}

View File

@ -40,6 +40,9 @@ type Backend interface {
// IsNotExist returns true if the error was caused by a non-existing file
// in the backend.
IsNotExist(err error) bool
// Delete removes all data in the backend.
Delete(ctx context.Context) error
}
// FileInfo is returned by Stat() and contains information about a file in the