Compare commits

...

3 commits

Author SHA1 Message Date
Vladislav Belov
4259c78150
Removes unused Get/SetDepthTextureBits methods
Some checks are pending
checkrefs / lfscheck (push) Waiting to run
checkrefs / checkrefs (push) Waiting to run
lint / cppcheck (push) Waiting to run
lint / copyright (push) Waiting to run
lint / jenkinsfiles (push) Waiting to run
pre-commit / build (push) Waiting to run
Methods were added in f903b83674.
Methods became unused in 12e2428495.
2026-06-15 00:35:04 +02:00
Ralph Sennhauser
71400e8045
Drop unused lowlevel functions
and associated types. Aka posix dirent.h abstraction.

Signed-off-by: Ralph Sennhauser <ralph.sennhauser@gmail.com>
2026-06-14 21:58:59 +02:00
Ralph Sennhauser
43e7dbc6da
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>
2026-06-14 21:58:59 +02:00
6 changed files with 121 additions and 383 deletions

View file

@ -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
* a copy of this software and associated documentation files (the
@ -26,28 +26,23 @@
#include "precompiled.h"
#include "file_system.h"
#include "lib/debug.h"
#include "lib/file/file_system.h"
#include "lib/posix/posix_filesystem.h"
#include "lib/sysdep/filesystem.h"
#include "lib/sysdep/os.h"
#include <boost/version.hpp>
#include <cerrno>
#include <cstring>
#include <chrono>
#include <filesystem>
#include <memory>
#include <string>
struct WDIR;
bool DirectoryExists(const OsPath& path)
{
WDIR* dir = wopendir(path);
if(dir)
try
{
wclosedir(dir);
return true;
return std::filesystem::is_directory(path.string());
}
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;
}
@ -55,158 +50,152 @@ bool DirectoryExists(const OsPath& path)
bool FileExists(const OsPath& pathname)
{
struct stat s;
const bool exists = wstat(pathname, &s) == 0;
return exists;
try
{
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)
{
struct stat s;
ENSURE(wstat(pathname, &s) == 0);
return s.st_size;
try
{
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)
{
errno = 0;
struct stat s;
memset(&s, 0, sizeof(s));
if(wstat(pathname, &s) != 0)
WARN_RETURN(StatusFromErrno());
*pPtrInfo = CFileInfo(pathname.Filename(), s.st_size, s.st_mtime);
try
{
const std::filesystem::path path{pathname.string()};
*pPtrInfo = CFileInfo(path.filename().wstring(), static_cast<u64>(std::filesystem::file_size(path)),
static_cast<time_t>(std::chrono::duration_cast<std::chrono::seconds>(std::filesystem::last_write_time(path).time_since_epoch()).count()));
}
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;
}
struct DirDeleter
{
void operator()(WDIR* osDir) const
{
const int ret = wclosedir(osDir);
ENSURE(ret == 0);
}
};
Status GetDirectoryEntries(const OsPath& path, CFileInfos* files, DirectoryNames* subdirectoryNames)
{
// open directory
errno = 0;
WDIR* pDir = wopendir(path);
if(!pDir)
return StatusFromErrno(); // NOWARN
std::shared_ptr<WDIR> osDir(pDir, DirDeleter());
for(;;)
try
{
errno = 0;
struct wdirent* osEnt = wreaddir(osDir.get());
if(!osEnt)
for (const std::filesystem::directory_entry& entry : std::filesystem::directory_iterator(path.string()))
{
// no error, just no more entries to return
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)
if (entry.is_directory() && entry.path().filename() != "." && entry.path().filename() != ".." && subdirectoryNames)
{
// TODO: This should be displayed to the user as a LOGWARNING when this code is
// moved to ps/
debug_printf("The path \"%s\" cannot be found. It is probably a dangling link "
"pointing to a non-existent path.\n", pathname.string8().c_str());
continue;
subdirectoryNames->emplace_back(entry.path().filename());
}
else if (entry.is_regular_file() && files)
{
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)
{
if(path.empty())
return INFO::OK;
struct stat s;
if(wstat(path, &s) == 0)
try
{
if(!S_ISDIR(s.st_mode)) // encountered a file
WARN_RETURN(ERR::FAIL);
return INFO::OK;
return CreateDirectoriesImpl(std::filesystem::path(path.string()), ModeTToPerms(mode));
}
// 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)
catch (std::filesystem::filesystem_error& err)
{
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)
WARN_RETURN(StatusFromErrno());
else
return StatusFromErrno();
WARN_RETURN(ERR::EXCEPTION);
else
return ERR::EXCEPTION;
}
return INFO::OK;
}
Status DeleteDirectory(const OsPath& path)
{
// note: we have to recursively empty the directory before it can
// 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++)
try
{
const OsPath pathname = path / files[i].Name();
errno = 0;
if(wunlink(pathname) != 0)
WARN_RETURN(StatusFromErrno());
std::filesystem::remove_all(path.string());
}
catch (std::filesystem::filesystem_error& err)
{
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;
}

View file

@ -1,4 +1,4 @@
/* Copyright (C) 2022 Wildfire Games.
/* Copyright (C) 2026 Wildfire Games.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
@ -31,32 +31,6 @@
#include "lib/posix/posix_filesystem.h" // mode_t
//
// dirent.h
//
struct WDIR;
struct wdirent
{
// note: SUSv3 describes this as a "char array" but of unspecified size.
// we declare as a pointer to avoid having to copy the string.
wchar_t* d_name;
};
extern WDIR* wopendir(const OsPath& path);
extern wdirent* wreaddir(WDIR*);
// return status for the file returned by the last successful
// wreaddir call from the given directory stream.
// currently sets st_size, st_mode, and st_mtime; the rest are zeroed.
// non-portable, but considerably faster than stat(). used by dir_ForEachSortedEntry.
extern int wreaddir_stat_np(WDIR*, struct stat*);
extern int wclosedir(WDIR*);
//
// fcntl.h
//

View file

@ -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
* a copy of this software and associated documentation files (the
@ -42,13 +42,6 @@
#include <fcntl.h>
#include <string>
struct WDIR
{
DIR* d;
wchar_t name[PATH_MAX];
wdirent ent;
};
#if OS_ANDROID
// The Crystax NDK seems to do weird things with opendir etc.
@ -84,36 +77,6 @@ void init_libc() { }
#endif
WDIR* wopendir(const OsPath& path)
{
init_libc();
DIR* d = opendir(OsString(path).c_str());
if(!d)
return 0;
WDIR* wd = new WDIR;
wd->d = d;
wd->name[0] = '\0';
wd->ent.d_name = wd->name;
return wd;
}
struct wdirent* wreaddir(WDIR* wd)
{
dirent* ent = readdir(wd->d);
if(!ent)
return 0;
wcscpy_s(wd->name, ARRAY_SIZE(wd->name), OsPath(ent->d_name).string().c_str());
return &wd->ent;
}
int wclosedir(WDIR* wd)
{
int ret = closedir(wd->d);
delete wd;
return ret;
}
int wopen(const OsPath& pathname, int oflag)
{
ENSURE(!(oflag & O_CREAT));

View file

@ -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
* a copy of this software and associated documentation files (the
@ -30,55 +30,6 @@
#include <atomic>
//-----------------------------------------------------------------------------
// WDIR suballocator
//-----------------------------------------------------------------------------
// most applications only need a single WDIR at a time. we avoid expensive
// heap allocations by reusing a single static instance. if it is already
// in use, we allocate further instances dynamically.
// NB: this is thread-safe due to CAS.
struct WDIR // POD
{
HANDLE hFind;
WIN32_FIND_DATAW findData; // indeterminate if hFind == INVALID_HANDLE_VALUE
// wreaddir will return the address of this member.
// (must be stored in WDIR to allow multiple independent
// wopendir/wreaddir sequences).
struct wdirent ent;
// used by wreaddir to skip the first FindNextFileW. (a counter is
// easy to test/update and also provides useful information.)
size_t numCalls;
};
static WDIR wdir_storage;
static std::atomic<bool> wdir_in_use{ false };
static inline WDIR* wdir_alloc()
{
if(!wdir_in_use.exchange(true)) // gained ownership
return &wdir_storage;
// already in use (rare) - allocate from heap
return new WDIR;
}
static inline void wdir_free(WDIR* d)
{
if(d == &wdir_storage)
{
const bool ok = wdir_in_use.exchange(false); // relinquish ownership
ENSURE(ok); // ensure it wasn't double-freed
}
else // allocated from heap
delete d;
}
//-----------------------------------------------------------------------------
// dirent.h
//-----------------------------------------------------------------------------
@ -127,112 +78,6 @@ static bool IsValidDirectory(const OsPath& path)
return true;
}
// Return owning pointer or nullptr on error.
[[nodiscard]] WDIR* wopendir(const OsPath& path)
{
WinScopedPreserveLastError s;
if(!IsValidDirectory(path))
{
errno = ENOENT;
return 0;
}
WDIR* d = wdir_alloc();
d->numCalls = 0;
// NB: "c:\\path" only returns information about that directory;
// trailing slashes aren't allowed. append "\\*" to retrieve its entries.
OsPath searchPath = path/"*";
// (we don't defer FindFirstFileW until wreaddir because callers
// expect us to return 0 if directory reading will/did fail.)
d->hFind = FindFirstFileW(OsString(searchPath).c_str(), &d->findData);
if(d->hFind != INVALID_HANDLE_VALUE)
return d; // success
const DWORD nativeError{GetLastError()};
if(nativeError == ERROR_NO_MORE_FILES)
return d; // success, but directory is empty
Status status = StatusFromWin();
// release the WDIR allocated above (this is preferable to
// always copying the large WDIR or findData from a temporary)
wdir_free(d);
if(nativeError == ERROR_PATH_NOT_FOUND)
// TODO: This should be displayed to the user as a LOGWARNING when this code is moved to ps/
debug_printf("The path \"%s\" cannot be found. It is probably a dangling link "
"pointing to a non-existent path.\n", path.string8().c_str());
else
WARN_IF_ERR(status);
errno = ErrnoFromStatus(status);
return 0;
}
struct wdirent* wreaddir(WDIR* d)
{
// directory is empty and d->findData is indeterminate
if(d->hFind == INVALID_HANDLE_VALUE)
return 0;
WinScopedPreserveLastError s;
// until end of directory or a valid entry was found:
for(;;)
{
if(d->numCalls++ != 0) // (skip first call to FindNextFileW - see wopendir)
{
if(!FindNextFileW(d->hFind, &d->findData))
{
if(GetLastError() == ERROR_NO_MORE_FILES)
SetLastError(0);
else // unexpected error
DEBUG_WARN_ERR(StatusFromWin());
return 0; // end of directory or error
}
}
// only accept non-hidden and non-system entries - otherwise,
// callers might encounter errors when attempting to open them.
if((d->findData.dwFileAttributes & (FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM)) == 0)
{
d->ent.d_name = d->findData.cFileName; // (NB: d_name is a pointer)
return &d->ent;
}
}
}
int wreaddir_stat_np(WDIR* d, struct stat* s)
{
// NTFS stores UTC but FAT stores local times, which are incorrectly
// translated to UTC based on the _current_ DST settings. we no longer
// bother checking the filesystem, since that's either unreliable or
// expensive. timestamps may therefore be off after a DST transition,
// which means our cached files would be regenerated.
FILETIME* filetime = &d->findData.ftLastWriteTime;
memset(s, 0, sizeof(*s));
s->st_size = (off_t)u64_from_u32(d->findData.nFileSizeHigh, d->findData.nFileSizeLow);
s->st_mode = (unsigned short)((d->findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)? S_IFDIR : S_IFREG);
s->st_mtime = wtime_utc_filetime_to_time_t(filetime);
return 0;
}
int wclosedir(WDIR* d)
{
FindClose(d->hFind);
wdir_free(d);
return 0;
}
//-----------------------------------------------------------------------------
// fcntl.h

View file

@ -703,23 +703,6 @@ void ShadowMap::BindTo(
}
}
// Depth texture bits
int ShadowMap::GetDepthTextureBits() const
{
return m->DepthTextureBits;
}
void ShadowMap::SetDepthTextureBits(int bits)
{
if (bits != m->DepthTextureBits)
{
m->Texture.reset();
m->Width = m->Height = 0;
m->DepthTextureBits = bits;
}
}
void ShadowMap::RenderDebugBounds(Renderer::Backend::IDeviceCommandContext& deviceCommandContext)
{
// Render various shadow bounds:

View file

@ -47,22 +47,6 @@ public:
*/
void RecreateTexture();
/**
* GetDepthTextureBits: Return the number of bits to use for depth textures when
* enabled.
*
* @return depth texture bit depth
*/
int GetDepthTextureBits() const;
/**
* SetDepthTextureBits: Sets the number of bits to use for depth textures when enabled.
* Possible values are 16, 24, 32 and 0 (= use default)
*
* @param bits number of bits
*/
void SetDepthTextureBits(int bits);
/**
* SetupFrame: Configure light space for the given camera and light direction,
* create the shadow texture if necessary, etc.