fs: Add dir option to fs.Purge #1891

Purge optional interface needs fixing too.
This commit is contained in:
Nick Craig-Wood 2017-12-07 12:25:56 +00:00
parent a375992186
commit 5ad226ab54
4 changed files with 21 additions and 18 deletions

View File

@ -22,7 +22,7 @@ you want to selectively delete files.
cmd.CheckArgs(1, 1, command, args) cmd.CheckArgs(1, 1, command, args)
fdst := cmd.NewFsDst(args) fdst := cmd.NewFsDst(args)
cmd.Run(true, false, command, func() error { cmd.Run(true, false, command, func() error {
return operations.Purge(fdst) return operations.Purge(fdst, "")
}) })
}, },
} }

View File

@ -932,24 +932,27 @@ func Rmdir(f fs.Fs, dir string) error {
return err return err
} }
// Purge removes a container and all of its contents // Purge removes a directory and all of its contents
func Purge(f fs.Fs) error { func Purge(f fs.Fs, dir string) error {
doFallbackPurge := true doFallbackPurge := true
var err error var err error
if doPurge := f.Features().Purge; doPurge != nil { if dir == "" {
doFallbackPurge = false // FIXME change the Purge interface so it takes a dir - see #1891
if fs.Config.DryRun { if doPurge := f.Features().Purge; doPurge != nil {
fs.Logf(f, "Not purging as --dry-run set") doFallbackPurge = false
} else { if fs.Config.DryRun {
err = doPurge() fs.Logf(f, "Not purging as --dry-run set")
if err == fs.ErrorCantPurge { } else {
doFallbackPurge = true err = doPurge()
if err == fs.ErrorCantPurge {
doFallbackPurge = true
}
} }
} }
} }
if doFallbackPurge { if doFallbackPurge {
// DeleteFiles and Rmdir observe --dry-run // DeleteFiles and Rmdir observe --dry-run
err = DeleteFiles(listToChan(f)) err = DeleteFiles(listToChan(f, dir))
if err != nil { if err != nil {
return err return err
} }
@ -1257,11 +1260,11 @@ func Deduplicate(f fs.Fs, mode DeduplicateMode) error {
// channel. // channel.
// //
// If the error was ErrorDirNotFound then it will be ignored // 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) o := make(fs.ObjectsChan, fs.Config.Checkers)
go func() { go func() {
defer close(o) 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 != nil {
if err == fs.ErrorDirNotFound { if err == fs.ErrorDirNotFound {
return nil 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") return nil, errors.Wrap(err, "Failed to create temporary local FS to spool file")
} }
defer func() { defer func() {
err := Purge(tmpLocalFs) err := Purge(tmpLocalFs, "")
if err != nil { if err != nil {
fs.Infof(tmpLocalFs, "Failed to cleanup temporary FS: %v", err) fs.Infof(tmpLocalFs, "Failed to cleanup temporary FS: %v", err)
} }

View File

@ -939,11 +939,11 @@ again:
func TestObjectPurge(t *testing.T) { func TestObjectPurge(t *testing.T) {
skipIfNotOk(t) skipIfNotOk(t)
err := operations.Purge(remote) err := operations.Purge(remote, "")
require.NoError(t, err) require.NoError(t, err)
fstest.CheckListing(t, remote, []fstest.Item{}) fstest.CheckListing(t, remote, []fstest.Item{})
err = operations.Purge(remote) err = operations.Purge(remote, "")
assert.Error(t, err, "Expecting error after on second purge") assert.Error(t, err, "Expecting error after on second purge")
} }

View File

@ -276,7 +276,7 @@ func (t *test) cleanFs() error {
if err != nil { if err != nil {
return err return err
} }
return operations.Purge(dir) return operations.Purge(dir, "")
} }
return nil return nil
}) })