Expanded ostd::FileSystem functionality

This commit is contained in:
OmniaX-Dev 2026-03-20 21:20:18 +01:00
parent c1011f1233
commit 5e89ce586c
3 changed files with 192 additions and 56 deletions

View file

@ -1 +1 @@
2021 2022

View file

@ -1,5 +1,6 @@
#include "FileSystem.hpp" #include "FileSystem.hpp"
#include "Logger.hpp" #include "Logger.hpp"
#include <fstream>
namespace ostd namespace ostd
{ {
@ -83,48 +84,118 @@ namespace ostd
} }
bool FileSystem::ensureDirectory(const String& path) bool FileSystem::ensureDirectory(const String& directoryPath)
{ {
namespace fs = std::filesystem; namespace fs = std::filesystem;
try { try {
fs::path dir(path); fs::path dir(directoryPath);
if (!fs::exists(dir)) if (!fs::exists(dir))
{ {
if (!fs::create_directories(dir)) if (!fs::create_directories(dir))
{ {
OX_ERROR("Failed to create directory: %s", path.c_str()); OX_ERROR("Failed to create directory: %s", directoryPath.c_str());
return false; return false;
} }
} }
else if (!fs::is_directory(dir)) else if (!fs::is_directory(dir))
{ {
OX_ERROR("Path exists but is not a directory: %s", path.c_str()); OX_ERROR("Path exists but is not a directory: %s", directoryPath.c_str());
return false; return false;
} }
} }
catch (const fs::filesystem_error& e) catch (const fs::filesystem_error& e)
{ {
OX_ERROR("Filesystem error for path '%s': %s", path.c_str(), e.what()); OX_ERROR("Filesystem error for path '%s': %s", directoryPath.c_str(), e.what());
return false; return false;
} }
return true; return true;
} }
bool FileSystem::deleteDirectory(const String& path) bool FileSystem::deleteDirectory(const String& directoryPath)
{ {
namespace fs = std::filesystem; namespace fs = std::filesystem;
try try
{ {
if (fs::exists(path) && fs::is_directory(path)) if (fs::exists(directoryPath) && fs::is_directory(directoryPath))
{ {
fs::remove_all(path); fs::remove_all(directoryPath);
return true; return true;
} }
} catch (const fs::filesystem_error& e) } catch (const fs::filesystem_error& e)
{ {
OX_ERROR("Failed to delete tmp folder '%s': %s", path.c_str(), e.what()); OX_ERROR("Failed to delete folder '%s': %s", directoryPath.c_str(), e.what());
return false;
}
return false;
}
bool FileSystem::ensureFile(const String& filePath, bool truncate)
{
namespace fs = std::filesystem;
try
{
fs::path p(filePath);
if (p.empty())
{
OX_ERROR("Invalid file path: '%s'", filePath.c_str());
return false;
}
fs::path parent = p.parent_path();
if (parent.empty() || !fs::exists(parent) || !fs::is_directory(parent))
{
OX_ERROR("Parent directory does not exist for file: '%s'", filePath.c_str());
return false;
}
if (fs::exists(p))
{
if (fs::is_directory(p))
{
OX_ERROR("Path exists but is a directory: '%s'", filePath.c_str());
return false;
}
if (!truncate)
return true;
std::ofstream ofs(p, std::ios::trunc);
if (!ofs)
{
OX_ERROR("Failed to truncate file: '%s'", filePath.c_str());
return false;
}
return true;
}
else
{
std::ofstream ofs(p);
if (!ofs)
{
OX_ERROR("Failed to create file: '%s'", filePath.c_str());
return false;
}
return true;
}
}
catch (const fs::filesystem_error& e)
{
OX_ERROR("Filesystem error for file '%s': %s", filePath.c_str(), e.what());
return false;
}
}
bool FileSystem::deleteFile(const String& filePath)
{
namespace fs = std::filesystem;
try
{
if (fs::exists(filePath) && !fs::is_directory(filePath))
{
fs::remove(filePath);
return true;
}
} catch (const fs::filesystem_error& e)
{
OX_ERROR("Failed to delete file '%s': %s", filePath.c_str(), e.what());
return false; return false;
} }
return false; return false;
@ -172,4 +243,62 @@ namespace ostd
return false; return false;
} }
} }
bool FileSystem::isValidFileCreationPath(const ostd::String& filePath)
{
namespace fs = std::filesystem;
try
{
fs::path p(filePath);
if (p.empty())
return false;
fs::path parent = p.parent_path();
if (parent.empty())
return false;
if (!fs::exists(parent) || !fs::is_directory(parent))
return false;
if (fs::exists(p) && !fs::is_regular_file(p))
return false;
return true;
}
catch (const fs::filesystem_error&)
{
return false;
}
}
bool FileSystem::isValidDirectoryCreationPath(const ostd::String& directoryPath)
{
namespace fs = std::filesystem;
try {
fs::path p(directoryPath);
if (p.empty())
return false;
fs::path parent = p.parent_path();
if (parent.empty())
return false;
if (!fs::exists(parent) || !fs::is_directory(parent))
return false;
if (fs::exists(p))
return false;
return true;
}
catch (const fs::filesystem_error&)
{
return false;
}
}
FileSystem::ePathStatus FileSystem::getPathStatus(const ostd::String& path)
{
if (directoryExists(path))
return ePathStatus::ExistingDirectory;
if (fileExists(path))
return ePathStatus::ExistingFile;
if (isValidDirectoryCreationPath(path))
return ePathStatus::ValidNewDirectory;
if (isValidFileCreationPath(path))
return ePathStatus::ValidNewFile;
return ePathStatus::Invalid;
}
} }

View file

@ -28,6 +28,8 @@ namespace ostd
{ {
class FileSystem class FileSystem
{ {
public: enum class ePathStatus { Invalid = 0, ExistingDirectory, ValidNewDirectory, ExistingFile, ValidNewFile };
public: public:
static std::vector<std::filesystem::path> listFilesInDirectory(const String& directoryPath); static std::vector<std::filesystem::path> listFilesInDirectory(const String& directoryPath);
static std::vector<std::filesystem::path> listDirectoriesInDirectory(const String& directoryPath); static std::vector<std::filesystem::path> listDirectoriesInDirectory(const String& directoryPath);
@ -39,11 +41,16 @@ namespace ostd
static std::filesystem::path getHomeDirPath(void); static std::filesystem::path getHomeDirPath(void);
static std::filesystem::path getWorkingDirPath(void); static std::filesystem::path getWorkingDirPath(void);
static bool ensureDirectory(const String& path); static bool ensureDirectory(const String& directoryPath);
static bool deleteDirectory(const String& path); static bool deleteDirectory(const String& directoryPath);
static bool ensureFile(const String& filePath, bool truncate = true);
static bool deleteFile(const String& filePath);
static bool directoryExists(const String& directoryPath); static bool directoryExists(const String& directoryPath);
static bool fileExists(const String& filePath); static bool fileExists(const String& filePath);
static bool pathExists(const String& path); static bool pathExists(const String& path);
static bool isValidFileCreationPath(const ostd::String& filePath);
static bool isValidDirectoryCreationPath(const ostd::String& directoryPath);
static ePathStatus getPathStatus(const ostd::String& path);
}; };
} }