Replace all uses of POSIX stat

Use `<filesystem>` instead of `stat` and remove wrapper.

Signed-off-by: Ralph Sennhauser <ralph.sennhauser@gmail.com>
This commit is contained in:
Ralph Sennhauser
2026-06-16 19:11:34 +02:00
parent fab4a132e1
commit e3c65841de
5 changed files with 15 additions and 42 deletions
+13 -27
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
@@ -38,40 +38,26 @@
#include "lib/os_path.h"
#include "lib/path.h"
#include "lib/posix/posix_filesystem.h"
#include "lib/sysdep/filesystem.h"
#include <cerrno>
#include <filesystem>
#include <string>
#include <system_error>
static Status CreateDirectory(const OsPath& path)
static Status CreateDirectory(const OsPath& dirpath)
{
{
const mode_t mode = S_IRWXU; // 0700 as prescribed by XDG basedir
const int ret = wmkdir(path, mode);
if(ret == 0) // success
return INFO::OK;
}
const std::filesystem::path path{dirpath.string()};
std::error_code ec{};
// Failed because the directory already exists.
// Return 'success' to attach the existing directory.
if(errno == EEXIST)
{
// But first ensure it's really a directory
// (otherwise, a file is "in the way" and needs to be deleted).
struct stat s;
const int ret = wstat(path, &s);
ENSURE(ret == 0); // (wmkdir said it existed)
ENSURE(S_ISDIR(s.st_mode));
if (std::filesystem::is_directory(path))
return INFO::OK;
}
if (errno == EACCES)
return ERR::FILE_ACCESS;
std::filesystem::create_directory(path, ec);
if (ec)
return StatusFromSystemError(ec);
// unexpected failure
debug_printf("wmkdir failed with errno=%d\n", errno);
DEBUG_WARN_ERR(ERR::LOGIC);
WARN_RETURN(StatusFromErrno());
// 0700 as prescribed by XDG basedir
std::filesystem::permissions(path, std::filesystem::perms::owner_all, ec);
return StatusFromSystemError(ec);
}
-2
View File
@@ -60,8 +60,6 @@ extern int wclose(int fd);
// sys/stat.h
//
int wstat(const OsPath& pathname, struct stat* buf);
int wmkdir(const OsPath& path, mode_t mode);
#endif // #ifndef INCLUDED_SYSDEP_FILESYSTEM
@@ -98,11 +98,6 @@ int wrename(const OsPath& pathnameOld, const OsPath& pathnameNew)
return rename(OsString(pathnameOld).c_str(), OsString(pathnameNew).c_str());
}
int wstat(const OsPath& pathname, struct stat* buf)
{
return stat(OsString(pathname).c_str(), buf);
}
int wmkdir(const OsPath& path, mode_t mode)
{
return mkdir(OsString(path).c_str(), mode);
@@ -160,9 +160,3 @@ int wmkdir(const OsPath& path, mode_t)
return 0;
}
int wstat(const OsPath& pathname, struct stat* buf)
{
return _wstat64(OsString(pathname).c_str(), buf);
}
+2 -2
View File
@@ -37,6 +37,7 @@
#include <cstdio>
#include <cstdlib>
#include <cwchar>
#include <filesystem>
#include <string>
class TestSysdep : public CxxTest::TestSuite
@@ -59,8 +60,7 @@ public:
// Check it's absolute
TSM_ASSERT(L"Path: "+path.string(), path_is_absolute(path.string().c_str()));
// Check the file exists
struct stat s;
TSM_ASSERT_EQUALS(L"Path: "+path.string(), wstat(path, &s), 0);
TS_ASSERT(std::filesystem::is_regular_file(path.string()));
}
private: