smbprovider: Work around bug in smbc_rmdir()

smbc_rmdir() is meant to return ENOTEMPTY if the directory is not empty,
but instead returns success (https://bugzilla.samba.org/show_bug.cgi?id=13204).
Work around by stat()ing the directory on success, and if that also succeeds,
assume the directory was not deleted.

BUG=chromium:1013901
TEST=Deploy to chell, directory deletion on SMB shares succeeds

Change-Id: I4316a18ebc7018b19f79a252c6bd013d80820e6a
Reviewed-on: https://chromium-review.googlesource.com/1864589
Tested-by: Anand Mistry <amistry@chromium.org>
Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com>
Legacy-Commit-Queue: Commit Bot <commit-bot@chromium.org>
Reviewed-by: Sergei Datsenko <dats@chromium.org>
(cherry picked from commit a10fb49380b2a517c7ea4cf04616107150e509c1)
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform2/+/1880729
Reviewed-by: Anand Mistry <amistry@chromium.org>
Commit-Queue: Anand Mistry <amistry@chromium.org>
diff --git a/smbprovider/samba_interface_impl.cc b/smbprovider/samba_interface_impl.cc
index 7f083f3..4cd6d16 100644
--- a/smbprovider/samba_interface_impl.cc
+++ b/smbprovider/samba_interface_impl.cc
@@ -230,7 +230,24 @@
 }
 
 int32_t SambaInterfaceImpl::RemoveDirectory(const std::string& dir_path) {
-  return smbc_rmdir_ctx_(context_, dir_path.c_str()) < 0 ? errno : 0;
+  int result = smbc_rmdir_ctx_(context_, dir_path.c_str());
+  if (result < 0) {
+    return errno;
+  }
+
+  // smbc_rmdir() is meant to return ENOTEMPTY if the directory is not empty.
+  // However, due to a samba bug
+  // (https://bugzilla.samba.org/show_bug.cgi?id=13204), it returns success. As
+  // a workaround, stat() the directory and if it exists, assume the removal
+  // failed. This is racy, because the directory could be re-created immediately
+  // after it is deleted, but a good enough heuristic.
+  // TODO(crbug.com/892289): Remove when Samba is upreved to 4.10 or later.
+  struct stat stat = {0};
+  result = smbc_stat_ctx_(context_, dir_path.c_str(), &stat);
+  if (result == 0) {
+    return ENOTEMPTY;
+  }
+  return 0;
 }
 
 int32_t SambaInterfaceImpl::CreateFile(const std::string& file_path,