Add exception handling of RemoveAll 86/315986/1 tizen_6.5
authorIlho Kim <ilho159.kim@samsung.com>
Mon, 12 Aug 2024 06:28:12 +0000 (15:28 +0900)
committerIlho Kim <ilho159.kim@samsung.com>
Mon, 12 Aug 2024 07:08:30 +0000 (16:08 +0900)
If a file is create or removed in the directory by another process while
calling RemoveAll on the direcotry, an ENOENT or ENOTEMPTY error
may occur. In that case the remove_all to be attempted again

Change-Id: I857d1a768df59fc8f45ef67526818cad1fb4f4f1
Signed-off-by: Ilho Kim <ilho159.kim@samsung.com>
(cherry picked from commit 80ae58e3d079e57c82ef070458b8ce0a4e899a59)

src/common/utils/file_util.cc

index b9652499e9058b438b4591127fe5ec841cb17a23..06dfff2e82f493ed56c82ed9c3f8c78734762dab 100644 (file)
@@ -316,18 +316,30 @@ bool RemoveBackup(const bf::path& path) {
 }
 
 bool RemoveAll(const bf::path& path) {
-  if (!exists(path) && !bf::is_symlink(bf::symlink_status(path)))
-    return true;
+  int retry_cnt = 5;
+  do {
+    if (!bf::exists(path) && !bf::is_symlink(bf::symlink_status(path)))
+      return true;
 
-  bs::error_code error;
-  bf::remove_all(path, error);
+    bs::error_code error;
+    bf::remove_all(path, error);
 
-  if (error) {
-    LOG(ERROR) << "Cannot remove: " << path << ", " << error.message();
-    return false;
-  }
+    if (error) {
+      if (error.value() == ENOENT || error.value() == ENOTEMPTY) {
+        LOG(WARNING) << "Cannot remove " << path << ", " << error.message() <<
+            ", because other process access some file in this path, retry[" <<
+            retry_cnt << "]";
+        usleep(500 * 1000);
+        continue;
+      }
+      LOG(ERROR) << "Cannot remove: " << path << ", " << error.message();
+      return false;
+    }
 
-  return true;
+    return true;
+  } while(--retry_cnt);
+
+  return false;
 }
 
 bool Remove(const bf::path& path) {