From 5ad226ab54bdd04fe6d5d8d3d58a25871bdd729d Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Thu, 7 Dec 2017 12:25:56 +0000 Subject: [PATCH] fs: Add dir option to fs.Purge #1891 Purge optional interface needs fixing too. --- cmd/purge/purge.go | 2 +- fs/operations/operations.go | 31 +++++++++++++++++-------------- fstest/fstests/fstests.go | 4 ++-- fstest/test_all/test_all.go | 2 +- 4 files changed, 21 insertions(+), 18 deletions(-) diff --git a/cmd/purge/purge.go b/cmd/purge/purge.go index 68dce5107..804cb2812 100644 --- a/cmd/purge/purge.go +++ b/cmd/purge/purge.go @@ -22,7 +22,7 @@ you want to selectively delete files. cmd.CheckArgs(1, 1, command, args) fdst := cmd.NewFsDst(args) cmd.Run(true, false, command, func() error { - return operations.Purge(fdst) + return operations.Purge(fdst, "") }) }, } diff --git a/fs/operations/operations.go b/fs/operations/operations.go index ea91b3d52..a115fbbe6 100644 --- a/fs/operations/operations.go +++ b/fs/operations/operations.go @@ -932,24 +932,27 @@ func Rmdir(f fs.Fs, dir string) error { return err } -// Purge removes a container and all of its contents -func Purge(f fs.Fs) error { +// Purge removes a directory and all of its contents +func Purge(f fs.Fs, dir string) error { doFallbackPurge := true var err error - if doPurge := f.Features().Purge; doPurge != nil { - doFallbackPurge = false - if fs.Config.DryRun { - fs.Logf(f, "Not purging as --dry-run set") - } else { - err = doPurge() - if err == fs.ErrorCantPurge { - doFallbackPurge = true + if dir == "" { + // FIXME change the Purge interface so it takes a dir - see #1891 + if doPurge := f.Features().Purge; doPurge != nil { + doFallbackPurge = false + if fs.Config.DryRun { + fs.Logf(f, "Not purging as --dry-run set") + } else { + err = doPurge() + if err == fs.ErrorCantPurge { + doFallbackPurge = true + } } } } if doFallbackPurge { // DeleteFiles and Rmdir observe --dry-run - err = DeleteFiles(listToChan(f)) + err = DeleteFiles(listToChan(f, dir)) if err != nil { return err } @@ -1257,11 +1260,11 @@ func Deduplicate(f fs.Fs, mode DeduplicateMode) error { // channel. // // If the error was ErrorDirNotFound then it will be ignored -func listToChan(f fs.Fs) fs.ObjectsChan { +func listToChan(f fs.Fs, dir string) fs.ObjectsChan { o := make(fs.ObjectsChan, fs.Config.Checkers) go func() { defer close(o) - _ = walk.Walk(f, "", true, fs.Config.MaxDepth, func(dirPath string, entries fs.DirEntries, err error) error { + _ = walk.Walk(f, dir, true, fs.Config.MaxDepth, func(dirPath string, entries fs.DirEntries, err error) error { if err != nil { if err == fs.ErrorDirNotFound { return nil @@ -1411,7 +1414,7 @@ func Rcat(fdst fs.Fs, dstFileName string, in io.ReadCloser, modTime time.Time) ( return nil, errors.Wrap(err, "Failed to create temporary local FS to spool file") } defer func() { - err := Purge(tmpLocalFs) + err := Purge(tmpLocalFs, "") if err != nil { fs.Infof(tmpLocalFs, "Failed to cleanup temporary FS: %v", err) } diff --git a/fstest/fstests/fstests.go b/fstest/fstests/fstests.go index b1bfc963f..92206f82d 100644 --- a/fstest/fstests/fstests.go +++ b/fstest/fstests/fstests.go @@ -939,11 +939,11 @@ again: func TestObjectPurge(t *testing.T) { skipIfNotOk(t) - err := operations.Purge(remote) + err := operations.Purge(remote, "") require.NoError(t, err) fstest.CheckListing(t, remote, []fstest.Item{}) - err = operations.Purge(remote) + err = operations.Purge(remote, "") assert.Error(t, err, "Expecting error after on second purge") } diff --git a/fstest/test_all/test_all.go b/fstest/test_all/test_all.go index fa3b29f6b..5c90c0a42 100644 --- a/fstest/test_all/test_all.go +++ b/fstest/test_all/test_all.go @@ -276,7 +276,7 @@ func (t *test) cleanFs() error { if err != nil { return err } - return operations.Purge(dir) + return operations.Purge(dir, "") } return nil })