From 4fa053ce97585dbfeee07539e7f94fdaf3178c98 Mon Sep 17 00:00:00 2001 From: J Reece Wilson Date: Wed, 20 Sep 2023 20:17:27 +0100 Subject: [PATCH] [*] Harden POSIX FS Remove --- Source/IO/FS/FS.Unix.cpp | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/Source/IO/FS/FS.Unix.cpp b/Source/IO/FS/FS.Unix.cpp index bbfdfa0f..c6b04610 100755 --- a/Source/IO/FS/FS.Unix.cpp +++ b/Source/IO/FS/FS.Unix.cpp @@ -346,6 +346,8 @@ namespace Aurora::IO::FS AUKN_SYM bool Remove(const AuString &path) { + struct stat s; + if (path.empty()) { SysPushErrorArg("Cannot open an IO handle to the provided empty path"); @@ -359,7 +361,38 @@ namespace Aurora::IO::FS return false; } - return remove(pathExpanded.c_str()) != -1; + if (::stat(pathExpanded.c_str(), &s) == -1) + { + if (errno == ENOENT) + { + return true; + } + } + else + { + if (S_ISDIR(s.st_mode)) + { + if (::rmdir(pathExpanded.c_str()) == 0) + { + return true; + } + } + + if (S_ISREG(s.st_mode)) + { + if (::unlink(pathExpanded.c_str()) == 0) + { + return true; + } + } + } + + if (::remove(pathExpanded.c_str()) == 0) + { + return true; + } + + return false; } AUKN_SYM bool Relink(const AuString &src, const AuString &dest)