mirror of
https://gitea.wildfiregames.com/0ad/0ad
synced 2026-06-16 05:13:58 -07:00
Use std::filesystem for filesystem abstraction
Drop the use of lowlevel interfaces and consitently use the cpp interface decupling it from systm dependent code. Signed-off-by: Ralph Sennhauser <ralph.sennhauser@gmail.com>
This commit is contained in:
parent
2f2cbb96bf
commit
43e7dbc6da
1 changed files with 118 additions and 129 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
/* Copyright (C) 2025 Wildfire Games.
|
/* Copyright (C) 2026 Wildfire Games.
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining
|
* Permission is hereby granted, free of charge, to any person obtaining
|
||||||
* a copy of this software and associated documentation files (the
|
* a copy of this software and associated documentation files (the
|
||||||
|
|
@ -26,28 +26,23 @@
|
||||||
|
|
||||||
#include "precompiled.h"
|
#include "precompiled.h"
|
||||||
|
|
||||||
|
#include "file_system.h"
|
||||||
|
|
||||||
#include "lib/debug.h"
|
#include "lib/debug.h"
|
||||||
#include "lib/file/file_system.h"
|
|
||||||
#include "lib/posix/posix_filesystem.h"
|
#include "lib/posix/posix_filesystem.h"
|
||||||
#include "lib/sysdep/filesystem.h"
|
|
||||||
#include "lib/sysdep/os.h"
|
|
||||||
|
|
||||||
#include <boost/version.hpp>
|
#include <chrono>
|
||||||
#include <cerrno>
|
|
||||||
#include <cstring>
|
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <memory>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
struct WDIR;
|
|
||||||
|
|
||||||
bool DirectoryExists(const OsPath& path)
|
bool DirectoryExists(const OsPath& path)
|
||||||
{
|
{
|
||||||
WDIR* dir = wopendir(path);
|
try
|
||||||
if(dir)
|
|
||||||
{
|
{
|
||||||
wclosedir(dir);
|
return std::filesystem::is_directory(path.string());
|
||||||
return true;
|
}
|
||||||
|
catch (std::filesystem::filesystem_error& err)
|
||||||
|
{
|
||||||
|
debug_printf("DirectoryExists: failed to check if directory '%s' exists, reason: %s\n", path.string8().c_str(), err.what());
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
@ -55,158 +50,152 @@ bool DirectoryExists(const OsPath& path)
|
||||||
|
|
||||||
bool FileExists(const OsPath& pathname)
|
bool FileExists(const OsPath& pathname)
|
||||||
{
|
{
|
||||||
struct stat s;
|
try
|
||||||
const bool exists = wstat(pathname, &s) == 0;
|
{
|
||||||
return exists;
|
return std::filesystem::is_regular_file(pathname.string());
|
||||||
|
}
|
||||||
|
catch (std::filesystem::filesystem_error& err)
|
||||||
|
{
|
||||||
|
debug_printf("FileExists: failed to check if file '%s' exists, reason: %s\n", pathname.string8().c_str(), err.what());
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
u64 FileSize(const OsPath& pathname)
|
u64 FileSize(const OsPath& pathname)
|
||||||
{
|
{
|
||||||
struct stat s;
|
try
|
||||||
ENSURE(wstat(pathname, &s) == 0);
|
{
|
||||||
return s.st_size;
|
return static_cast<u64>(std::filesystem::file_size(pathname.string()));
|
||||||
|
}
|
||||||
|
catch (std::filesystem::filesystem_error& err)
|
||||||
|
{
|
||||||
|
debug_printf("FileSize: failed to get filesize for '%s', reason: %s\n", pathname.string8().c_str(), err.what());
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Status GetFileInfo(const OsPath& pathname, CFileInfo* pPtrInfo)
|
Status GetFileInfo(const OsPath& pathname, CFileInfo* pPtrInfo)
|
||||||
{
|
{
|
||||||
errno = 0;
|
try
|
||||||
struct stat s;
|
{
|
||||||
memset(&s, 0, sizeof(s));
|
const std::filesystem::path path{pathname.string()};
|
||||||
if(wstat(pathname, &s) != 0)
|
*pPtrInfo = CFileInfo(path.filename().wstring(), static_cast<u64>(std::filesystem::file_size(path)),
|
||||||
WARN_RETURN(StatusFromErrno());
|
static_cast<time_t>(std::chrono::duration_cast<std::chrono::seconds>(std::filesystem::last_write_time(path).time_since_epoch()).count()));
|
||||||
|
}
|
||||||
*pPtrInfo = CFileInfo(pathname.Filename(), s.st_size, s.st_mtime);
|
catch (std::filesystem::filesystem_error& err)
|
||||||
|
{
|
||||||
|
debug_printf("GetFileInfo: failed to get file info for '%s', reason: %s\n", pathname.string8().c_str(), err.what());
|
||||||
|
return ERR::EXCEPTION;
|
||||||
|
}
|
||||||
return INFO::OK;
|
return INFO::OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
struct DirDeleter
|
|
||||||
{
|
|
||||||
void operator()(WDIR* osDir) const
|
|
||||||
{
|
|
||||||
const int ret = wclosedir(osDir);
|
|
||||||
ENSURE(ret == 0);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
Status GetDirectoryEntries(const OsPath& path, CFileInfos* files, DirectoryNames* subdirectoryNames)
|
Status GetDirectoryEntries(const OsPath& path, CFileInfos* files, DirectoryNames* subdirectoryNames)
|
||||||
{
|
{
|
||||||
// open directory
|
try
|
||||||
errno = 0;
|
|
||||||
WDIR* pDir = wopendir(path);
|
|
||||||
if(!pDir)
|
|
||||||
return StatusFromErrno(); // NOWARN
|
|
||||||
std::shared_ptr<WDIR> osDir(pDir, DirDeleter());
|
|
||||||
|
|
||||||
for(;;)
|
|
||||||
{
|
{
|
||||||
errno = 0;
|
for (const std::filesystem::directory_entry& entry : std::filesystem::directory_iterator(path.string()))
|
||||||
struct wdirent* osEnt = wreaddir(osDir.get());
|
|
||||||
if(!osEnt)
|
|
||||||
{
|
{
|
||||||
// no error, just no more entries to return
|
if (entry.is_directory() && entry.path().filename() != "." && entry.path().filename() != ".." && subdirectoryNames)
|
||||||
if(!errno)
|
|
||||||
return INFO::OK;
|
|
||||||
WARN_RETURN(StatusFromErrno());
|
|
||||||
}
|
|
||||||
|
|
||||||
for(size_t i = 0; osEnt->d_name[i] != '\0'; i++)
|
|
||||||
RETURN_STATUS_IF_ERR(Path::Validate(osEnt->d_name[i]));
|
|
||||||
|
|
||||||
const std::wstring_view name{osEnt->d_name};
|
|
||||||
|
|
||||||
// get file information (mode, size, mtime)
|
|
||||||
struct stat s;
|
|
||||||
#if OS_WIN
|
|
||||||
// .. return wdirent directly (much faster than calling stat).
|
|
||||||
RETURN_STATUS_IF_ERR(wreaddir_stat_np(osDir.get(), &s));
|
|
||||||
#else
|
|
||||||
// .. call regular stat().
|
|
||||||
errno = 0;
|
|
||||||
const OsPath pathname = path / OsPath(osEnt->d_name);
|
|
||||||
if(wstat(pathname, &s) != 0)
|
|
||||||
{
|
|
||||||
if(errno == ENOENT)
|
|
||||||
{
|
{
|
||||||
// TODO: This should be displayed to the user as a LOGWARNING when this code is
|
subdirectoryNames->emplace_back(entry.path().filename());
|
||||||
// moved to ps/
|
}
|
||||||
debug_printf("The path \"%s\" cannot be found. It is probably a dangling link "
|
else if (entry.is_regular_file() && files)
|
||||||
"pointing to a non-existent path.\n", pathname.string8().c_str());
|
{
|
||||||
continue;
|
files->emplace_back(entry.path().filename().wstring(), static_cast<u64>(entry.file_size()),
|
||||||
|
static_cast<time_t>(std::chrono::duration_cast<std::chrono::seconds>(entry.last_write_time().time_since_epoch()).count()));
|
||||||
}
|
}
|
||||||
WARN_RETURN(StatusFromErrno());
|
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
if(files && S_ISREG(s.st_mode))
|
|
||||||
files->emplace_back(osEnt->d_name, s.st_size, s.st_mtime);
|
|
||||||
else if(subdirectoryNames && S_ISDIR(s.st_mode) && name != L"." && name != L"..")
|
|
||||||
subdirectoryNames->emplace_back(osEnt->d_name);
|
|
||||||
}
|
}
|
||||||
|
catch (std::filesystem::filesystem_error& err)
|
||||||
|
{
|
||||||
|
debug_printf("GetDirectoryEntries: failed to get directory entries for'%s', reason: %s\n", path.string8().c_str(), err.what());
|
||||||
|
return ERR::EXCEPTION;
|
||||||
|
}
|
||||||
|
return INFO::OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
|
||||||
|
std::filesystem::perms ModeTToPerms(mode_t mode)
|
||||||
|
{
|
||||||
|
using std::filesystem::perms;
|
||||||
|
perms perm{perms::none};
|
||||||
|
|
||||||
|
if (mode | S_IRUSR)
|
||||||
|
perm |= perms::owner_read;
|
||||||
|
if (mode | S_IWUSR)
|
||||||
|
perm |= perms::owner_write;
|
||||||
|
if (mode | S_IXUSR)
|
||||||
|
perm |= perms::owner_exec;
|
||||||
|
|
||||||
|
if (mode | S_IRGRP)
|
||||||
|
perm |= perms::group_read;
|
||||||
|
if (mode | S_IWGRP)
|
||||||
|
perm |= perms::group_write;
|
||||||
|
if (mode | S_IXGRP)
|
||||||
|
perm |= perms::group_exec;
|
||||||
|
|
||||||
|
if (mode | S_IROTH)
|
||||||
|
perm |= perms::others_read;
|
||||||
|
if (mode | S_IWOTH)
|
||||||
|
perm |= perms::others_write;
|
||||||
|
if (mode | S_IXOTH)
|
||||||
|
perm |= perms::others_exec;
|
||||||
|
|
||||||
|
return perm;
|
||||||
|
}
|
||||||
|
|
||||||
|
Status CreateDirectoriesImpl(const std::filesystem::path& path, const std::filesystem::perms& perms)
|
||||||
|
{
|
||||||
|
if (std::filesystem::exists(path))
|
||||||
|
return ERR::FAIL;
|
||||||
|
|
||||||
|
if (!std::filesystem::is_directory(path.parent_path()))
|
||||||
|
{
|
||||||
|
const Status status = CreateDirectoriesImpl(path.parent_path(), perms);
|
||||||
|
if (status != INFO::OK)
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::filesystem::create_directory(path);
|
||||||
|
std::filesystem::permissions(path, perms);
|
||||||
|
return INFO::OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
Status CreateDirectories(const OsPath& path, mode_t mode, bool breakpoint)
|
Status CreateDirectories(const OsPath& path, mode_t mode, bool breakpoint)
|
||||||
{
|
{
|
||||||
if(path.empty())
|
try
|
||||||
return INFO::OK;
|
|
||||||
|
|
||||||
struct stat s;
|
|
||||||
if(wstat(path, &s) == 0)
|
|
||||||
{
|
{
|
||||||
if(!S_ISDIR(s.st_mode)) // encountered a file
|
return CreateDirectoriesImpl(std::filesystem::path(path.string()), ModeTToPerms(mode));
|
||||||
WARN_RETURN(ERR::FAIL);
|
|
||||||
return INFO::OK;
|
|
||||||
}
|
}
|
||||||
|
catch (std::filesystem::filesystem_error& err)
|
||||||
// If we were passed a path ending with '/', strip the '/' now so that
|
|
||||||
// we can consistently use Parent to find parent directory names
|
|
||||||
if(path.IsDirectory())
|
|
||||||
return CreateDirectories(path.Parent(), mode, breakpoint);
|
|
||||||
|
|
||||||
RETURN_STATUS_IF_ERR(CreateDirectories(path.Parent(), mode));
|
|
||||||
|
|
||||||
errno = 0;
|
|
||||||
if(wmkdir(path, mode) != 0)
|
|
||||||
{
|
{
|
||||||
debug_printf("CreateDirectories: failed to mkdir %s (mode %d)\n", path.string8().c_str(), mode);
|
debug_printf("CreateDirectories: failed to create directories '%s', reason: %s\n", path.string8().c_str(), err.what());
|
||||||
if (breakpoint)
|
if (breakpoint)
|
||||||
WARN_RETURN(StatusFromErrno());
|
WARN_RETURN(ERR::EXCEPTION);
|
||||||
else
|
else
|
||||||
return StatusFromErrno();
|
return ERR::EXCEPTION;
|
||||||
}
|
}
|
||||||
|
|
||||||
return INFO::OK;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Status DeleteDirectory(const OsPath& path)
|
Status DeleteDirectory(const OsPath& path)
|
||||||
{
|
{
|
||||||
// note: we have to recursively empty the directory before it can
|
try
|
||||||
// be deleted (required by Windows and POSIX rmdir()).
|
|
||||||
|
|
||||||
CFileInfos files; DirectoryNames subdirectoryNames;
|
|
||||||
RETURN_STATUS_IF_ERR(GetDirectoryEntries(path, &files, &subdirectoryNames));
|
|
||||||
|
|
||||||
// delete files
|
|
||||||
for(size_t i = 0; i < files.size(); i++)
|
|
||||||
{
|
{
|
||||||
const OsPath pathname = path / files[i].Name();
|
std::filesystem::remove_all(path.string());
|
||||||
errno = 0;
|
}
|
||||||
if(wunlink(pathname) != 0)
|
catch (std::filesystem::filesystem_error& err)
|
||||||
WARN_RETURN(StatusFromErrno());
|
{
|
||||||
|
debug_printf("DeleteDirectory: failed to delete directory '%s', reason: %s\n", path.string8().c_str(), err.what());
|
||||||
|
return ERR::EXCEPTION;
|
||||||
}
|
}
|
||||||
|
|
||||||
// recurse over subdirectoryNames
|
|
||||||
for(size_t i = 0; i < subdirectoryNames.size(); i++)
|
|
||||||
RETURN_STATUS_IF_ERR(DeleteDirectory(path / subdirectoryNames[i]));
|
|
||||||
|
|
||||||
errno = 0;
|
|
||||||
if(wrmdir(path) != 0)
|
|
||||||
WARN_RETURN(StatusFromErrno());
|
|
||||||
|
|
||||||
return INFO::OK;
|
return INFO::OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue