From 4f82239aee6dfb30b437fd4b70cc3878a6e19d93 Mon Sep 17 00:00:00 2001 From: Ilho Kim Date: Mon, 12 Aug 2024 15:28:12 +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: I857d1a768df59fc8f45ef67526818cad1fb4f4f1 Signed-off-by: Ilho Kim (cherry picked from commit 80ae58e3d079e57c82ef070458b8ce0a4e899a59) --- 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 8df33f45..27c9b05c 100644 --- a/src/common/utils/file_util.cc +++ b/src/common/utils/file_util.cc @@ -316,18 +316,30 @@ bool RemoveBackup(const bf::path& path) { } bool RemoveAll(const bf::path& path) { - if (!bf::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) { -- cgit v1.2.3