1
0
mirror of https://github.com/luanti-org/luanti.git synced 2025-12-19 21:35:46 +01:00

Fix errors with fs::RecursiveDelete() on paths that don't exist

This commit is contained in:
sfan5
2025-12-10 21:32:05 +01:00
committed by GitHub
parent f6c472fd2c
commit 2ecd127f96
3 changed files with 18 additions and 15 deletions

View File

@@ -142,32 +142,33 @@ bool IsExecutable(const std::string &path)
bool RecursiveDelete(const std::string &path) bool RecursiveDelete(const std::string &path)
{ {
infostream << "Recursively deleting \"" << path << "\"" << std::endl;
assert(IsPathAbsolute(path)); assert(IsPathAbsolute(path));
if (!IsDir(path)) { if (!PathExists(path))
infostream << "RecursiveDelete: Deleting file " << path << std::endl; return true;
bool is_file = !IsDir(path);
infostream << "Recursively deleting " << (is_file ? "file" : "directory")
<< " \"" << path << "\"" << std::endl;
if (is_file) {
if (!DeleteFile(path.c_str())) { if (!DeleteFile(path.c_str())) {
errorstream << "RecursiveDelete: Failed to delete file " errorstream << "RecursiveDelete: Failed to delete file \""
<< path << std::endl; << path << "\": " << LAST_OS_ERROR() << std::endl;
return false; return false;
} }
return true; return true;
} }
infostream << "RecursiveDelete: Deleting content of directory "
<< path << std::endl;
std::vector<DirListNode> content = GetDirListing(path); std::vector<DirListNode> content = GetDirListing(path);
for (const DirListNode &n: content) { for (const auto &n : content) {
std::string fullpath = path + DIR_DELIM + n.name; std::string fullpath = path + DIR_DELIM + n.name;
if (!RecursiveDelete(fullpath)) { if (!RecursiveDelete(fullpath)) {
errorstream << "RecursiveDelete: Failed to recurse to " errorstream << "RecursiveDelete: Failed to recurse to \""
<< fullpath << std::endl; << fullpath << "\"" << std::endl;
return false; return false;
} }
} }
infostream << "RecursiveDelete: Deleting directory " << path << std::endl;
if (!RemoveDirectory(path.c_str())) { if (!RemoveDirectory(path.c_str())) {
errorstream << "Failed to recursively delete directory " errorstream << "RecursiveDelete: Failed to delete directory \""
<< path << std::endl; << path << "\": " << LAST_OS_ERROR() << std::endl;
return false; return false;
} }
return true; return true;

View File

@@ -599,8 +599,7 @@ bool setSystemPaths()
// Delete tmp folder if it exists (it only ever contained // Delete tmp folder if it exists (it only ever contained
// a temporary ogg file, which is no longer used). // a temporary ogg file, which is no longer used).
if (fs::PathExists(local_cache_path + DIR_DELIM + "tmp")) fs::RecursiveDelete(local_cache_path + DIR_DELIM + "tmp");
fs::RecursiveDelete(local_cache_path + DIR_DELIM + "tmp");
// Bail if migration impossible // Bail if migration impossible
if (path_cache == local_cache_path || !fs::PathExists(local_cache_path) if (path_cache == local_cache_path || !fs::PathExists(local_cache_path)

View File

@@ -415,6 +415,9 @@ void TestFileSys::testRecursiveDelete()
UASSERT(!fs::IsDir(it)); UASSERT(!fs::IsDir(it));
for (auto &it : files) for (auto &it : files)
UASSERT(!fs::IsFile(it)); UASSERT(!fs::IsFile(it));
// Deleting something that doesn't exist is *not* an error
UASSERT(fs::RecursiveDelete(dirs[0]));
} }
void TestFileSys::testGetRecursiveSubPaths() void TestFileSys::testGetRecursiveSubPaths()