From b4d86d5450db281c0bc8a402b597dc92a2f961a1 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Fri, 31 Aug 2018 23:07:12 +0100 Subject: [PATCH] onedrive: fix rmdir sometimes deleting directories with contents Before this change we were using the ChildCount in the Folder facet to determine if a directory was empty or not. However this seems to be unreliable, or updated asynchronously which meant that `rclone rmdir` sometimes deleted directories that had files in. This problem was spotted by the integration tests. Listing the directory instead of relying on the ChildCount fixes the problem and the integration tests, without changing the cost (one http transaction). --- backend/onedrive/onedrive.go | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/backend/onedrive/onedrive.go b/backend/onedrive/onedrive.go index d7172bf61..9cfe81cc4 100644 --- a/backend/onedrive/onedrive.go +++ b/backend/onedrive/onedrive.go @@ -718,15 +718,17 @@ func (f *Fs) purgeCheck(dir string, check bool) error { if err != nil { return err } - item, _, err := f.readMetaDataForPath(root) - if err != nil { - return err - } - if item.Folder == nil { - return errors.New("not a folder") - } - if check && item.Folder.ChildCount != 0 { - return errors.New("folder not empty") + if check { + // check to see if there are any items + found, err := f.listAll(rootID, false, false, func(item *api.Item) bool { + return true + }) + if err != nil { + return err + } + if found { + return fs.ErrorDirectoryNotEmpty + } } err = f.deleteObject(rootID) if err != nil {