Files
OpenVulkano/openVulkanoCpp/IO/FsUtils.cpp
2024-08-09 14:52:48 +02:00

92 lines
1.5 KiB
C++

/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "FsUtils.hpp"
namespace fs = std::filesystem;
namespace OpenVulkano
{
bool FsUtils::DeleteEmptyDirs(const std::filesystem::path& dir, const bool recursive)
{
bool deleted = false;
for (auto& p: fs::directory_iterator(dir))
{
if (fs::is_directory(p))
{
if (recursive)
{
if (DeleteEmptyDirs(p), recursive)
{
deleted = true;
}
}
if (fs::is_empty(p))
{
fs::remove(p);
deleted = true;
}
}
}
return deleted;
}
bool FsUtils::DeleteEmptySubTrees(const std::filesystem::path& dir)
{
bool deleted = false;
for (auto& p: fs::directory_iterator(dir))
{
if (fs::is_directory(p))
{
if (IsTreeEmpty(p))
{
fs::remove_all(p);
deleted = true;
}
}
}
return deleted;
}
bool FsUtils::IsTreeEmpty(const std::filesystem::path& dir)
{
for (auto& p: fs::directory_iterator(dir))
{
if (fs::is_directory(p))
{
if (!IsTreeEmpty(p))
{
return false;
}
}
else
{
return false;
}
}
return true;
}
ByteSize FsUtils::GetDirSize(const std::filesystem::path& dir)
{
ByteSize size = 0;
for (auto& p: fs::directory_iterator(dir))
{
if (fs::is_directory(p))
{
size += GetDirSize(p);
}
else
{
size += p.file_size();
}
}
return size;
}
}