From 948a5d25c226829e896a6fdea96d191ac9aa5717 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Fri, 26 Oct 2018 23:47:23 +0100 Subject: [PATCH] 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. --- fs/operations/operations.go | 4 +- fs/operations/operations_test.go | 94 ++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 2 deletions(-) diff --git a/fs/operations/operations.go b/fs/operations/operations.go index 8b2c05fc4..a22aa4d69 100644 --- a/fs/operations/operations.go +++ b/fs/operations/operations.go @@ -975,7 +975,7 @@ func Purge(f fs.Fs, dir string) error { if err != nil { return err } - err = Rmdirs(f, "", false) + err = Rmdirs(f, dir, false) } if err != nil { fs.CountError(err) @@ -1206,7 +1206,7 @@ func PublicLink(f fs.Fs, remote string) (string, error) { // containing empty directories) under f, including f. func Rmdirs(f fs.Fs, dir string, leaveRoot bool) error { 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 { if err != nil { fs.CountError(err) diff --git a/fs/operations/operations_test.go b/fs/operations/operations_test.go index bb5cb7221..b95ed1baf 100644 --- a/fs/operations/operations_test.go +++ b/fs/operations/operations_test.go @@ -401,6 +401,78 @@ func TestRcat(t *testing.T) { 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) { r := fstest.NewRun(t) defer r.Finalise() @@ -441,6 +513,28 @@ func TestRmdirsNoLeaveRoot(t *testing.T) { 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)) fstest.CheckListingWithPrecision(