From 4a995105a99a000d28fd4b22ed2f62fa67d39094 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sat, 14 Oct 2017 16:08:15 +0200 Subject: [PATCH] sftp: Fix Delete() --- internal/backend/sftp/sftp.go | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/internal/backend/sftp/sftp.go b/internal/backend/sftp/sftp.go index ef006ce6f..116a1c20e 100644 --- a/internal/backend/sftp/sftp.go +++ b/internal/backend/sftp/sftp.go @@ -500,7 +500,38 @@ func (r *SFTP) Close() error { return nil } +func (r *SFTP) deleteRecursive(name string) error { + entries, err := r.c.ReadDir(name) + if err != nil { + return errors.Wrap(err, "ReadDir") + } + + for _, fi := range entries { + itemName := r.Join(name, fi.Name()) + if fi.IsDir() { + err := r.deleteRecursive(itemName) + if err != nil { + return errors.Wrap(err, "ReadDir") + } + + err = r.c.RemoveDirectory(itemName) + if err != nil { + return errors.Wrap(err, "RemoveDirectory") + } + + continue + } + + err := r.c.Remove(itemName) + if err != nil { + return errors.Wrap(err, "ReadDir") + } + } + + return nil +} + // Delete removes all data in the backend. func (r *SFTP) Delete(context.Context) error { - return r.c.RemoveDirectory(r.p) + return r.deleteRecursive(r.p) }