1
0
mirror of https://github.com/luanti-org/luanti.git synced 2025-12-13 18:55:20 +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)
{
infostream << "Recursively deleting \"" << path << "\"" << std::endl;
assert(IsPathAbsolute(path));
if (!IsDir(path)) {
infostream << "RecursiveDelete: Deleting file " << path << std::endl;
if (!PathExists(path))
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())) {
errorstream << "RecursiveDelete: Failed to delete file "
<< path << std::endl;
errorstream << "RecursiveDelete: Failed to delete file \""
<< path << "\": " << LAST_OS_ERROR() << std::endl;
return false;
}
return true;
}
infostream << "RecursiveDelete: Deleting content of directory "
<< path << std::endl;
std::vector<DirListNode> content = GetDirListing(path);
for (const DirListNode &n: content) {
for (const auto &n : content) {
std::string fullpath = path + DIR_DELIM + n.name;
if (!RecursiveDelete(fullpath)) {
errorstream << "RecursiveDelete: Failed to recurse to "
<< fullpath << std::endl;
errorstream << "RecursiveDelete: Failed to recurse to \""
<< fullpath << "\"" << std::endl;
return false;
}
}
infostream << "RecursiveDelete: Deleting directory " << path << std::endl;
if (!RemoveDirectory(path.c_str())) {
errorstream << "Failed to recursively delete directory "
<< path << std::endl;
errorstream << "RecursiveDelete: Failed to delete directory \""
<< path << "\": " << LAST_OS_ERROR() << std::endl;
return false;
}
return true;

View File

@@ -599,8 +599,7 @@ bool setSystemPaths()
// Delete tmp folder if it exists (it only ever contained
// 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
if (path_cache == local_cache_path || !fs::PathExists(local_cache_path)

View File

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