Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

operations: Fix Purge and Rmdirs when dir is not empty

Nick Craig-Wood948a5d25c226
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(