From 63f6f7d45885f653422cd64da6a81c8a0c72e8a6 Mon Sep 17 00:00:00 2001 From: Ilho Kim Date: Mon, 12 Aug 2024 15:46:57 +0900 Subject: Add exception handling of RemoveAll 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: Ic9600fb9f1ca097d9a4fae6f53cf94336c452e53 Signed-off-by: Ilho Kim --- src/common/utils/file_util.cc | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/common/utils/file_util.cc b/src/common/utils/file_util.cc index aa87c788..248db577 100644 --- a/src/common/utils/file_util.cc +++ b/src/common/utils/file_util.cc @@ -307,18 +307,30 @@ bool RemoveBackup(const fs::path& path) { } bool RemoveAll(const fs::path& path) { - if (!fs::exists(path) && !fs::is_symlink(fs::symlink_status(path))) - return true; + int retry_cnt = 5; + do { + if (!fs::exists(path) && !fs::is_symlink(fs::symlink_status(path))) + return true; - std::error_code error; - fs::remove_all(path, error); + std::error_code error; + fs::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 fs::path& path) { -- cgit v1.2.3