From 5bf3f850887c8cae426eeba97381c87d2ed49a35 Mon Sep 17 00:00:00 2001 From: wuyuanchao Date: Thu, 21 Dec 2023 19:21:21 +0800 Subject: [PATCH 01/20] bugfix for removing long path directory Signed-off-by: wuyuanchao --- base/src/directory_ex.cpp | 124 ++++++++++++++++++++------------------ 1 file changed, 66 insertions(+), 58 deletions(-) diff --git a/base/src/directory_ex.cpp b/base/src/directory_ex.cpp index 2ef101b..bb579d2 100644 --- a/base/src/directory_ex.cpp +++ b/base/src/directory_ex.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include "securec.h" #include "unistd.h" #include "utils_log.h" @@ -195,84 +196,91 @@ bool ForceCreateDirectory(const string& path) return access(path.c_str(), F_OK) == 0; } -bool ForceRemoveDirectoryInternal(DIR *dir) +struct DirectoryNode { + DIR *dir; + int dirFd; + const char *name; +}; + +bool ForceRemoveDirectory(const string& path) { bool ret = true; - int rootFd = dirfd(dir); - if (rootFd < 0) { - UTILS_LOGD("Failed to get dirfd, fd: %{public}d: %{public}s ", rootFd, strerror(errno)); + DIR *dir = opendir(path.c_str()); + if (dir == nullptr) { + UTILS_LOGD("Failed to open root dir: %{public}s: %{public}s ", path.c_str(), strerror(errno)); return false; } - - while (true) { - struct dirent *ptr = readdir(dir); - if (ptr == nullptr) { - break; - } - const char *name = ptr->d_name; - - // current dir or parent dir - if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) { - continue; - } - - if (ptr->d_type == DT_DIR) { - int subFd = openat(rootFd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC); - if (subFd < 0) { - UTILS_LOGD("Failed in subFd openat: %{public}s ", name); - ret = false; - continue; + stack dirStack1; + stack dirStack2; + dirStack1.push(dir); + while (!dirStack1.empty()) { + DIR *currentDir = dirStack1.top(); + dirStack1.pop(); + DirectoryNode node; + int currentFd = dirfd(currentDir); + while (true) { + struct dirent *ptr = readdir(currentDir); + if (ptr == nullptr) { + break; } - DIR *subDir = fdopendir(subFd); - if (subDir == nullptr) { - close(subFd); - UTILS_LOGD("Failed in fdopendir: %{public}s", strerror(errno)); - ret = false; + const char *name = ptr->d_name; + if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0 ) { continue; } - ret = ForceRemoveDirectoryInternal(subDir); - closedir(subDir); - if (unlinkat(rootFd, name, AT_REMOVEDIR) < 0) { - UTILS_LOGD("Couldn't unlinkat subDir %{public}s: %{public}s", name, strerror(errno)); - ret = false; - continue; - } - } else { - if (faccessat(rootFd, name, F_OK, AT_SYMLINK_NOFOLLOW) == 0) { - if (unlinkat(rootFd, name, 0) < 0) { - UTILS_LOGD("Couldn't unlinkat subFile %{public}s: %{public}s", name, strerror(errno)); - return false; + if (ptr->d_type == DT_DIR) { + node.dir = currentDir; + node.dirFd = currentFd; + node.name = name; + dirStack2.push(node); + int subFd = openat(currentFd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC); + if (subFd < 0) { + UTILS_LOGD("Failed in subFd openat: %{public}s ", name); + ret = false; + break; + } + DIR *subDir = fdopendir(subFd); + if (subDir == nullptr) { + close(subFd); + UTILS_LOGD("Failed in fdopendir: %{public}s", strerror(errno)); + ret = false; + break; } + dirStack1.push(subDir); } else { - UTILS_LOGD("Access to file: %{public}s is failed", name); - return false; + if (faccessat(currentFd, name, F_OK, AT_SYMLINK_NOFOLLOW) == 0) { + if (unlinkat(currentFd, name, 0) < 0) { + UTILS_LOGD("Couldn't unlinkat subFile %{public}s: %{public}s", name, strerror(errno)); + ret = false; + break; + } + } else { + UTILS_LOGD("Access to file: %{public}s is failed", name); + ret = false; + break; + } } } } - - return ret; -} - -bool ForceRemoveDirectory(const string& path) -{ - bool ret = true; - DIR *dir = opendir(path.c_str()); - if (dir == nullptr) { - UTILS_LOGD("Failed to open root dir: %{public}s: %{public}s ", path.c_str(), strerror(errno)); - return false; - } - ret = ForceRemoveDirectoryInternal(dir); if (!ret) { UTILS_LOGD("Failed to remove some subfile under path: %{public}s", path.c_str()); + return ret; + } + while (!dirStack2.empty()) { + DirectoryNode node = dirStack2.top(); + dirStack2.pop(); + if (unlinkat(node.dirFd, node.name, AT_REMOVEDIR) < 0) { + UTILS_LOGD("Couldn't unlinkat subDir %{public}s: %{public}s", name, strerror(errno)); + return false; + } + close(node.dirFd); + closedir(node.dir); } - closedir(dir); if (faccessat(AT_FDCWD, path.c_str(), F_OK, AT_SYMLINK_NOFOLLOW) == 0) { - if (remove(path.c_str()) != 0) { + if (unlinkat(AT_FDCWD, path.c_str(), 0) != 0) { UTILS_LOGD("Failed to remove root dir: %{public}s: %{public}s ", path.c_str(), strerror(errno)); return false; } } - return faccessat(AT_FDCWD, path.c_str(), F_OK, AT_SYMLINK_NOFOLLOW) != 0; } -- Gitee From 8ce4096eccfbcdbb007c1b372b5db22278b0f6e9 Mon Sep 17 00:00:00 2001 From: wuyuanchao Date: Mon, 25 Dec 2023 18:19:07 +0800 Subject: [PATCH 02/20] bugfix for removing long path directory Signed-off-by: wuyuanchao --- base/src/directory_ex.cpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/base/src/directory_ex.cpp b/base/src/directory_ex.cpp index bb579d2..d89274f 100644 --- a/base/src/directory_ex.cpp +++ b/base/src/directory_ex.cpp @@ -198,7 +198,8 @@ bool ForceCreateDirectory(const string& path) struct DirectoryNode { DIR *dir; - int dirFd; + int currentFd; + int subFd; const char *name; }; @@ -228,10 +229,6 @@ bool ForceRemoveDirectory(const string& path) continue; } if (ptr->d_type == DT_DIR) { - node.dir = currentDir; - node.dirFd = currentFd; - node.name = name; - dirStack2.push(node); int subFd = openat(currentFd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC); if (subFd < 0) { UTILS_LOGD("Failed in subFd openat: %{public}s ", name); @@ -245,6 +242,11 @@ bool ForceRemoveDirectory(const string& path) ret = false; break; } + node.dir = subDir; + node.subFd = subFd; + node.currentFd = currentFd; + node.name = name; + dirStack2.push(node); dirStack1.push(subDir); } else { if (faccessat(currentFd, name, F_OK, AT_SYMLINK_NOFOLLOW) == 0) { @@ -268,15 +270,15 @@ bool ForceRemoveDirectory(const string& path) while (!dirStack2.empty()) { DirectoryNode node = dirStack2.top(); dirStack2.pop(); - if (unlinkat(node.dirFd, node.name, AT_REMOVEDIR) < 0) { + if (unlinkat(node.currentFd, node.name, AT_REMOVEDIR) < 0) { UTILS_LOGD("Couldn't unlinkat subDir %{public}s: %{public}s", name, strerror(errno)); return false; } - close(node.dirFd); closedir(node.dir); } + closedir(dir); if (faccessat(AT_FDCWD, path.c_str(), F_OK, AT_SYMLINK_NOFOLLOW) == 0) { - if (unlinkat(AT_FDCWD, path.c_str(), 0) != 0) { + if (unlinkat(AT_FDCWD, path.c_str(), AT_REMOVEDIR) != 0) { UTILS_LOGD("Failed to remove root dir: %{public}s: %{public}s ", path.c_str(), strerror(errno)); return false; } -- Gitee From c4be16a609ce73647ab8d3f77d27fc9c31fcff7b Mon Sep 17 00:00:00 2001 From: wuyuanchao Date: Tue, 26 Dec 2023 10:42:35 +0800 Subject: [PATCH 03/20] bugfix for removing long path directory Signed-off-by: wuyuanchao --- base/src/directory_ex.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/base/src/directory_ex.cpp b/base/src/directory_ex.cpp index d89274f..86b5826 100644 --- a/base/src/directory_ex.cpp +++ b/base/src/directory_ex.cpp @@ -219,6 +219,11 @@ bool ForceRemoveDirectory(const string& path) dirStack1.pop(); DirectoryNode node; int currentFd = dirfd(currentDir); + if (currentFd < 0) { + UTILS_LOGD("Failed in currentFd openat: %{public}s ", name); + ret = false; + break; + } while (true) { struct dirent *ptr = readdir(currentDir); if (ptr == nullptr) { -- Gitee From 662efebb4bce6e44f0e0a7a36bae918897160771 Mon Sep 17 00:00:00 2001 From: wuyuanchao Date: Tue, 26 Dec 2023 14:46:26 +0800 Subject: [PATCH 04/20] bugfix for removing long path directory Signed-off-by: wuyuanchao --- base/src/directory_ex.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/base/src/directory_ex.cpp b/base/src/directory_ex.cpp index 86b5826..ef16e3c 100644 --- a/base/src/directory_ex.cpp +++ b/base/src/directory_ex.cpp @@ -199,7 +199,6 @@ bool ForceCreateDirectory(const string& path) struct DirectoryNode { DIR *dir; int currentFd; - int subFd; const char *name; }; @@ -248,7 +247,6 @@ bool ForceRemoveDirectory(const string& path) break; } node.dir = subDir; - node.subFd = subFd; node.currentFd = currentFd; node.name = name; dirStack2.push(node); @@ -283,7 +281,7 @@ bool ForceRemoveDirectory(const string& path) } closedir(dir); if (faccessat(AT_FDCWD, path.c_str(), F_OK, AT_SYMLINK_NOFOLLOW) == 0) { - if (unlinkat(AT_FDCWD, path.c_str(), AT_REMOVEDIR) != 0) { + if (remove(path.c_str()) != 0) { UTILS_LOGD("Failed to remove root dir: %{public}s: %{public}s ", path.c_str(), strerror(errno)); return false; } -- Gitee From d5a2522cf4277b1bed15a098cd2145d3ab42f9c4 Mon Sep 17 00:00:00 2001 From: wuyuanchao Date: Tue, 26 Dec 2023 14:49:07 +0800 Subject: [PATCH 05/20] bugfix for removing long path directory Signed-off-by: wuyuanchao --- base/src/directory_ex.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/base/src/directory_ex.cpp b/base/src/directory_ex.cpp index ef16e3c..c4533e3 100644 --- a/base/src/directory_ex.cpp +++ b/base/src/directory_ex.cpp @@ -237,14 +237,14 @@ bool ForceRemoveDirectory(const string& path) if (subFd < 0) { UTILS_LOGD("Failed in subFd openat: %{public}s ", name); ret = false; - break; + continue; } DIR *subDir = fdopendir(subFd); if (subDir == nullptr) { close(subFd); UTILS_LOGD("Failed in fdopendir: %{public}s", strerror(errno)); ret = false; - break; + continue; } node.dir = subDir; node.currentFd = currentFd; -- Gitee From 88fa468bc9e116d9039358a4091b13340e9ded42 Mon Sep 17 00:00:00 2001 From: wuyuanchao Date: Tue, 26 Dec 2023 15:25:07 +0800 Subject: [PATCH 06/20] bugfix for removing long path directory Signed-off-by: wuyuanchao --- base/src/directory_ex.cpp | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/base/src/directory_ex.cpp b/base/src/directory_ex.cpp index c4533e3..86dc1db 100644 --- a/base/src/directory_ex.cpp +++ b/base/src/directory_ex.cpp @@ -210,12 +210,12 @@ bool ForceRemoveDirectory(const string& path) UTILS_LOGD("Failed to open root dir: %{public}s: %{public}s ", path.c_str(), strerror(errno)); return false; } - stack dirStack1; - stack dirStack2; - dirStack1.push(dir); - while (!dirStack1.empty()) { - DIR *currentDir = dirStack1.top(); - dirStack1.pop(); + stack traversStack; + stack removeStack; + traversStack.push(dir); + while (!traversStack.empty()) { + DIR *currentDir = traversStack.top(); + traversStack.pop(); DirectoryNode node; int currentFd = dirfd(currentDir); if (currentFd < 0) { @@ -229,9 +229,12 @@ bool ForceRemoveDirectory(const string& path) break; } const char *name = ptr->d_name; - if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0 ) { + + // current dir or parent dir + if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) { continue; } + if (ptr->d_type == DT_DIR) { int subFd = openat(currentFd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC); if (subFd < 0) { @@ -249,8 +252,8 @@ bool ForceRemoveDirectory(const string& path) node.dir = subDir; node.currentFd = currentFd; node.name = name; - dirStack2.push(node); - dirStack1.push(subDir); + removeStack.push(node); + traversStack.push(subDir); } else { if (faccessat(currentFd, name, F_OK, AT_SYMLINK_NOFOLLOW) == 0) { if (unlinkat(currentFd, name, 0) < 0) { @@ -270,12 +273,13 @@ bool ForceRemoveDirectory(const string& path) UTILS_LOGD("Failed to remove some subfile under path: %{public}s", path.c_str()); return ret; } - while (!dirStack2.empty()) { - DirectoryNode node = dirStack2.top(); - dirStack2.pop(); + while (!removeStack.empty()) { + DirectoryNode node = removeStack.top(); + removeStack.pop(); if (unlinkat(node.currentFd, node.name, AT_REMOVEDIR) < 0) { - UTILS_LOGD("Couldn't unlinkat subDir %{public}s: %{public}s", name, strerror(errno)); - return false; + closedir(node.dir); + UTILS_LOGD("Couldn't unlinkat subDir %{public}s: %{public}s", node.name, strerror(errno)); + continue; } closedir(node.dir); } -- Gitee From 71845fbe7d7a475ce8698979cdd943b92d0854f2 Mon Sep 17 00:00:00 2001 From: wuyuanchao Date: Wed, 27 Dec 2023 09:17:06 +0800 Subject: [PATCH 07/20] bugfix for removing long path directory Signed-off-by: wuyuanchao --- base/src/directory_ex.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/base/src/directory_ex.cpp b/base/src/directory_ex.cpp index 86dc1db..a2b7362 100644 --- a/base/src/directory_ex.cpp +++ b/base/src/directory_ex.cpp @@ -219,9 +219,9 @@ bool ForceRemoveDirectory(const string& path) DirectoryNode node; int currentFd = dirfd(currentDir); if (currentFd < 0) { - UTILS_LOGD("Failed in currentFd openat: %{public}s ", name); + UTILS_LOGD("Failed to get dirfd, fd: %{public}d: %{public}s ", currentFd, strerror(errno)); ret = false; - break; + continue; } while (true) { struct dirent *ptr = readdir(currentDir); -- Gitee From fcd9e266f36ac6f06663c352d228afa248b49002 Mon Sep 17 00:00:00 2001 From: wuyuanchao Date: Wed, 27 Dec 2023 11:14:40 +0800 Subject: [PATCH 08/20] bugfix for removing long path directory Signed-off-by: wuyuanchao --- base/src/directory_ex.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/src/directory_ex.cpp b/base/src/directory_ex.cpp index a2b7362..57e8e3b 100644 --- a/base/src/directory_ex.cpp +++ b/base/src/directory_ex.cpp @@ -221,7 +221,7 @@ bool ForceRemoveDirectory(const string& path) if (currentFd < 0) { UTILS_LOGD("Failed to get dirfd, fd: %{public}d: %{public}s ", currentFd, strerror(errno)); ret = false; - continue; + break; } while (true) { struct dirent *ptr = readdir(currentDir); -- Gitee From 8c8d2f7cd632c202593f748501aff30ebfd91c05 Mon Sep 17 00:00:00 2001 From: wuyuanchao Date: Wed, 27 Dec 2023 14:42:02 +0800 Subject: [PATCH 09/20] bugfix for removing long path directory Signed-off-by: wuyuanchao --- base/src/directory_ex.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/src/directory_ex.cpp b/base/src/directory_ex.cpp index 57e8e3b..a2b7362 100644 --- a/base/src/directory_ex.cpp +++ b/base/src/directory_ex.cpp @@ -221,7 +221,7 @@ bool ForceRemoveDirectory(const string& path) if (currentFd < 0) { UTILS_LOGD("Failed to get dirfd, fd: %{public}d: %{public}s ", currentFd, strerror(errno)); ret = false; - break; + continue; } while (true) { struct dirent *ptr = readdir(currentDir); -- Gitee From 53a97ff7f57f8624d298d6f1d1527f578e9acc73 Mon Sep 17 00:00:00 2001 From: wuyuanchao Date: Wed, 27 Dec 2023 16:18:14 +0800 Subject: [PATCH 10/20] bugfix for removing long path directory Signed-off-by: wuyuanchao --- base/src/directory_ex.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/base/src/directory_ex.cpp b/base/src/directory_ex.cpp index a2b7362..e072411 100644 --- a/base/src/directory_ex.cpp +++ b/base/src/directory_ex.cpp @@ -271,7 +271,6 @@ bool ForceRemoveDirectory(const string& path) } if (!ret) { UTILS_LOGD("Failed to remove some subfile under path: %{public}s", path.c_str()); - return ret; } while (!removeStack.empty()) { DirectoryNode node = removeStack.top(); -- Gitee From a4a021ecfc78b9c0068bc60fe861c304043888d7 Mon Sep 17 00:00:00 2001 From: wuyuanchao Date: Tue, 2 Jan 2024 10:52:03 +0800 Subject: [PATCH 11/20] bugfix for removing long path directory Signed-off-by: wuyuanchao --- base/src/directory_ex.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/base/src/directory_ex.cpp b/base/src/directory_ex.cpp index e072411..00978b1 100644 --- a/base/src/directory_ex.cpp +++ b/base/src/directory_ex.cpp @@ -275,12 +275,11 @@ bool ForceRemoveDirectory(const string& path) while (!removeStack.empty()) { DirectoryNode node = removeStack.top(); removeStack.pop(); + closedir(node.dir); if (unlinkat(node.currentFd, node.name, AT_REMOVEDIR) < 0) { - closedir(node.dir); UTILS_LOGD("Couldn't unlinkat subDir %{public}s: %{public}s", node.name, strerror(errno)); continue; } - closedir(node.dir); } closedir(dir); if (faccessat(AT_FDCWD, path.c_str(), F_OK, AT_SYMLINK_NOFOLLOW) == 0) { -- Gitee From 2122a8c14decf27c0a6cd08198716a724bb398b7 Mon Sep 17 00:00:00 2001 From: wuyuanchao Date: Tue, 2 Jan 2024 15:18:27 +0800 Subject: [PATCH 12/20] bugfix for removing long path directory Signed-off-by: wuyuanchao --- base/src/directory_ex.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/base/src/directory_ex.cpp b/base/src/directory_ex.cpp index 00978b1..32ad05f 100644 --- a/base/src/directory_ex.cpp +++ b/base/src/directory_ex.cpp @@ -223,11 +223,11 @@ bool ForceRemoveDirectory(const string& path) ret = false; continue; } - while (true) { - struct dirent *ptr = readdir(currentDir); - if (ptr == nullptr) { - break; - } + struct dirent *ptr = readdir(currentDir); + if (ptr == nullptr) { + continue; + } + do { const char *name = ptr->d_name; // current dir or parent dir @@ -267,7 +267,8 @@ bool ForceRemoveDirectory(const string& path) break; } } - } + ptr = readdir(currentDir); + } while (ptr != nullptr); } if (!ret) { UTILS_LOGD("Failed to remove some subfile under path: %{public}s", path.c_str()); -- Gitee From 18ee61f637f1452b65c0393cc4be898e1d9ca575 Mon Sep 17 00:00:00 2001 From: wuyuanchao Date: Tue, 2 Jan 2024 15:43:52 +0800 Subject: [PATCH 13/20] bugfix for removing long path directory Signed-off-by: wuyuanchao --- base/src/directory_ex.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/src/directory_ex.cpp b/base/src/directory_ex.cpp index 32ad05f..eb98cb6 100644 --- a/base/src/directory_ex.cpp +++ b/base/src/directory_ex.cpp @@ -227,7 +227,7 @@ bool ForceRemoveDirectory(const string& path) if (ptr == nullptr) { continue; } - do { + do { const char *name = ptr->d_name; // current dir or parent dir -- Gitee From 50c1da6f0aefe36ff3675729e8370360bc7dcf03 Mon Sep 17 00:00:00 2001 From: wuyuanchao Date: Tue, 2 Jan 2024 20:09:54 +0800 Subject: [PATCH 14/20] bugfix for removing long path directory Signed-off-by: wuyuanchao --- base/src/directory_ex.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/base/src/directory_ex.cpp b/base/src/directory_ex.cpp index eb98cb6..e37deec 100644 --- a/base/src/directory_ex.cpp +++ b/base/src/directory_ex.cpp @@ -225,6 +225,7 @@ bool ForceRemoveDirectory(const string& path) } struct dirent *ptr = readdir(currentDir); if (ptr == nullptr) { + ptr = readdir(currentDir); continue; } do { -- Gitee From 0a42b268b29ad80df5c282068f0757ba3dcd0baf Mon Sep 17 00:00:00 2001 From: wuyuanchao Date: Tue, 2 Jan 2024 20:40:00 +0800 Subject: [PATCH 15/20] bugfix for removing long path directory Signed-off-by: wuyuanchao --- base/src/directory_ex.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/base/src/directory_ex.cpp b/base/src/directory_ex.cpp index e37deec..cf3776b 100644 --- a/base/src/directory_ex.cpp +++ b/base/src/directory_ex.cpp @@ -225,7 +225,6 @@ bool ForceRemoveDirectory(const string& path) } struct dirent *ptr = readdir(currentDir); if (ptr == nullptr) { - ptr = readdir(currentDir); continue; } do { @@ -233,6 +232,7 @@ bool ForceRemoveDirectory(const string& path) // current dir or parent dir if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) { + ptr = readdir(currentDir); continue; } @@ -241,6 +241,7 @@ bool ForceRemoveDirectory(const string& path) if (subFd < 0) { UTILS_LOGD("Failed in subFd openat: %{public}s ", name); ret = false; + ptr = readdir(currentDir); continue; } DIR *subDir = fdopendir(subFd); @@ -248,6 +249,7 @@ bool ForceRemoveDirectory(const string& path) close(subFd); UTILS_LOGD("Failed in fdopendir: %{public}s", strerror(errno)); ret = false; + ptr = readdir(currentDir); continue; } node.dir = subDir; -- Gitee From a2111e2fc9d3c438ba7cc7caf7b133b852d0c99e Mon Sep 17 00:00:00 2001 From: wuyuanchao Date: Wed, 3 Jan 2024 09:23:17 +0800 Subject: [PATCH 16/20] bugfix for removing long path directory Signed-off-by: wuyuanchao --- base/src/directory_ex.cpp | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/base/src/directory_ex.cpp b/base/src/directory_ex.cpp index cf3776b..69fefb5 100644 --- a/base/src/directory_ex.cpp +++ b/base/src/directory_ex.cpp @@ -224,24 +224,20 @@ bool ForceRemoveDirectory(const string& path) continue; } struct dirent *ptr = readdir(currentDir); - if (ptr == nullptr) { - continue; - } - do { - const char *name = ptr->d_name; - + while (ptr != nullptr) { + struct dirent *currentPtr = ptr; + ptr = readdir(currentDir); + const char *name = currentPtr->d_name; // current dir or parent dir if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) { - ptr = readdir(currentDir); continue; } - if (ptr->d_type == DT_DIR) { + if (currentPtr->d_type == DT_DIR) { int subFd = openat(currentFd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC); if (subFd < 0) { UTILS_LOGD("Failed in subFd openat: %{public}s ", name); ret = false; - ptr = readdir(currentDir); continue; } DIR *subDir = fdopendir(subFd); @@ -249,7 +245,6 @@ bool ForceRemoveDirectory(const string& path) close(subFd); UTILS_LOGD("Failed in fdopendir: %{public}s", strerror(errno)); ret = false; - ptr = readdir(currentDir); continue; } node.dir = subDir; @@ -269,9 +264,8 @@ bool ForceRemoveDirectory(const string& path) ret = false; break; } - } - ptr = readdir(currentDir); - } while (ptr != nullptr); + } + } } if (!ret) { UTILS_LOGD("Failed to remove some subfile under path: %{public}s", path.c_str()); -- Gitee From 814affa9cf67fcc3205d54ce286e4db41cfec520 Mon Sep 17 00:00:00 2001 From: wuyuanchao Date: Wed, 3 Jan 2024 10:01:51 +0800 Subject: [PATCH 17/20] bugfix for removing long path directory Signed-off-by: wuyuanchao --- base/src/directory_ex.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/src/directory_ex.cpp b/base/src/directory_ex.cpp index 69fefb5..31d7aa1 100644 --- a/base/src/directory_ex.cpp +++ b/base/src/directory_ex.cpp @@ -264,7 +264,7 @@ bool ForceRemoveDirectory(const string& path) ret = false; break; } - } + } } } if (!ret) { -- Gitee From f4a2fdb53098e89b232c1960e2f124a49fdc5828 Mon Sep 17 00:00:00 2001 From: wuyuanchao Date: Wed, 3 Jan 2024 11:18:54 +0800 Subject: [PATCH 18/20] bugfix for removing long path directory Signed-off-by: wuyuanchao --- base/src/directory_ex.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/src/directory_ex.cpp b/base/src/directory_ex.cpp index 31d7aa1..4512b46 100644 --- a/base/src/directory_ex.cpp +++ b/base/src/directory_ex.cpp @@ -265,7 +265,7 @@ bool ForceRemoveDirectory(const string& path) break; } } - } + } } if (!ret) { UTILS_LOGD("Failed to remove some subfile under path: %{public}s", path.c_str()); -- Gitee From cedab951f19b6055242e9d32d796e753cc51c97f Mon Sep 17 00:00:00 2001 From: wuyuanchao Date: Wed, 3 Jan 2024 11:30:28 +0800 Subject: [PATCH 19/20] bugfix for removing long path directory Signed-off-by: wuyuanchao --- base/src/directory_ex.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/base/src/directory_ex.cpp b/base/src/directory_ex.cpp index 4512b46..724883d 100644 --- a/base/src/directory_ex.cpp +++ b/base/src/directory_ex.cpp @@ -260,9 +260,9 @@ bool ForceRemoveDirectory(const string& path) break; } } else { - UTILS_LOGD("Access to file: %{public}s is failed", name); - ret = false; - break; + UTILS_LOGD("Access to file: %{public}s is failed", name); + ret = false; + break; } } } -- Gitee From 9f417019d3c2c901186ec2d964c58f95cfebdbd5 Mon Sep 17 00:00:00 2001 From: wuyuanchao Date: Wed, 3 Jan 2024 11:41:50 +0800 Subject: [PATCH 20/20] bugfix for removing long path directory Signed-off-by: wuyuanchao --- base/src/directory_ex.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/base/src/directory_ex.cpp b/base/src/directory_ex.cpp index 724883d..310f601 100644 --- a/base/src/directory_ex.cpp +++ b/base/src/directory_ex.cpp @@ -232,7 +232,6 @@ bool ForceRemoveDirectory(const string& path) if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) { continue; } - if (currentPtr->d_type == DT_DIR) { int subFd = openat(currentFd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC); if (subFd < 0) { -- Gitee