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:
@@ -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;
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user