operations: Fix Purge and Rmdirs when dir is not empty

Before this change, Purge on the fallback path would try to delete
directories starting from the root rather than the dir passed in.
Rmdirs would also attempt to delete the root.
This commit is contained in:
Nick Craig-Wood 2018-10-26 23:47:23 +01:00
parent f7c31cd210
commit 948a5d25c2
2 changed files with 96 additions and 2 deletions

View file

@ -975,7 +975,7 @@ func Purge(f fs.Fs, dir string) error {
if err != nil { if err != nil {
return err return err
} }
err = Rmdirs(f, "", false) err = Rmdirs(f, dir, false)
} }
if err != nil { if err != nil {
fs.CountError(err) fs.CountError(err)
@ -1206,7 +1206,7 @@ func PublicLink(f fs.Fs, remote string) (string, error) {
// containing empty directories) under f, including f. // containing empty directories) under f, including f.
func Rmdirs(f fs.Fs, dir string, leaveRoot bool) error { func Rmdirs(f fs.Fs, dir string, leaveRoot bool) error {
dirEmpty := make(map[string]bool) dirEmpty := make(map[string]bool)
dirEmpty[""] = !leaveRoot dirEmpty[dir] = !leaveRoot
err := walk.Walk(f, dir, true, fs.Config.MaxDepth, func(dirPath string, entries fs.DirEntries, err error) error { err := walk.Walk(f, dir, true, fs.Config.MaxDepth, func(dirPath string, entries fs.DirEntries, err error) error {
if err != nil { if err != nil {
fs.CountError(err) fs.CountError(err)

View file

@ -401,6 +401,78 @@ func TestRcat(t *testing.T) {
check(false) check(false)
} }
func TestPurge(t *testing.T) {
r := fstest.NewRun(t)
defer r.Finalise()
r.Mkdir(r.Fremote)
// Make some files and dirs
r.ForceMkdir(r.Fremote)
file1 := r.WriteObject("A1/B1/C1/one", "aaa", t1)
//..and dirs we expect to delete
require.NoError(t, operations.Mkdir(r.Fremote, "A2"))
require.NoError(t, operations.Mkdir(r.Fremote, "A1/B2"))
require.NoError(t, operations.Mkdir(r.Fremote, "A1/B2/C2"))
require.NoError(t, operations.Mkdir(r.Fremote, "A1/B1/C3"))
require.NoError(t, operations.Mkdir(r.Fremote, "A3"))
require.NoError(t, operations.Mkdir(r.Fremote, "A3/B3"))
require.NoError(t, operations.Mkdir(r.Fremote, "A3/B3/C4"))
//..and one more file at the end
file2 := r.WriteObject("A1/two", "bbb", t2)
fstest.CheckListingWithPrecision(
t,
r.Fremote,
[]fstest.Item{
file1, file2,
},
[]string{
"A1",
"A1/B1",
"A1/B1/C1",
"A2",
"A1/B2",
"A1/B2/C2",
"A1/B1/C3",
"A3",
"A3/B3",
"A3/B3/C4",
},
fs.GetModifyWindow(r.Fremote),
)
require.NoError(t, operations.Purge(r.Fremote, "A1/B1"))
fstest.CheckListingWithPrecision(
t,
r.Fremote,
[]fstest.Item{
file2,
},
[]string{
"A1",
"A2",
"A1/B2",
"A1/B2/C2",
"A3",
"A3/B3",
"A3/B3/C4",
},
fs.GetModifyWindow(r.Fremote),
)
require.NoError(t, operations.Purge(r.Fremote, ""))
fstest.CheckListingWithPrecision(
t,
r.Fremote,
[]fstest.Item{},
[]string{},
fs.GetModifyWindow(r.Fremote),
)
}
func TestRmdirsNoLeaveRoot(t *testing.T) { func TestRmdirsNoLeaveRoot(t *testing.T) {
r := fstest.NewRun(t) r := fstest.NewRun(t)
defer r.Finalise() defer r.Finalise()
@ -441,6 +513,28 @@ func TestRmdirsNoLeaveRoot(t *testing.T) {
fs.GetModifyWindow(r.Fremote), fs.GetModifyWindow(r.Fremote),
) )
require.NoError(t, operations.Rmdirs(r.Fremote, "A3/B3/C4", false))
fstest.CheckListingWithPrecision(
t,
r.Fremote,
[]fstest.Item{
file1, file2,
},
[]string{
"A1",
"A1/B1",
"A1/B1/C1",
"A2",
"A1/B2",
"A1/B2/C2",
"A1/B1/C3",
"A3",
"A3/B3",
},
fs.GetModifyWindow(r.Fremote),
)
require.NoError(t, operations.Rmdirs(r.Fremote, "", false)) require.NoError(t, operations.Rmdirs(r.Fremote, "", false))
fstest.CheckListingWithPrecision( fstest.CheckListingWithPrecision(