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)
fdst := cmd.NewFsDst(args)
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
}
// 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)
}

View File

@ -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")
}

View File

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