From 9fd07127fb77e9ecf19af1d0dcb776a8fe60aa56 Mon Sep 17 00:00:00 2001 From: "Aleksei (Faumaray) Selikhov" Date: Sun, 6 Dec 2026 02:14:11 +0300 Subject: [PATCH] Storage: Windows compatibility layer for the file port (WIP) POSIX shims over the Windows CRT for the storage file port: statvfs -> GetDiskFreeSpaceExW, flock -> LockFileEx, pread/pwrite -> serialized seek+read/write, rename -> MoveFileExW replace, link -> CreateHardLinkW, fsync -> FlushFileBuffers, directory opens via handle-backed descriptors, and O_NOFOLLOW enforced through reparse-point checks. Void_Storage and its tests build for Windows; the remaining failures are being worked through before Storage is allowlisted. Refs #65. --- src/Storage/FilePort.cpp | 266 ++++++++++++++++++++++++++- tests/standalone/Storage/Storage.cpp | 8 + 2 files changed, 272 insertions(+), 2 deletions(-) diff --git a/src/Storage/FilePort.cpp b/src/Storage/FilePort.cpp index 520c2c2..bbb7a38 100644 --- a/src/Storage/FilePort.cpp +++ b/src/Storage/FilePort.cpp @@ -5,12 +5,272 @@ #include #include #include -#include #include +#include + +#ifndef _WIN32 +#include #include #include #include -#include +#else +#include +#include +#include +#include +#include +#endif + +#ifdef _WIN32 +// POSIX shims over the Windows CRT so the storage file port keeps its logic. +// Offsets are handled by a serialized seek+read/write (the CRT has no pread). +#ifndef O_CLOEXEC +#define O_CLOEXEC 0 +#endif +#ifndef O_NOFOLLOW +// Custom bit: the CRT does not know it; the open() shim enforces it. +#define O_NOFOLLOW 0x40000000 +#endif +#ifndef O_DIRECTORY +#define O_DIRECTORY 0 +#endif +#ifndef LOCK_EX +#define LOCK_EX 2 +#endif +#ifndef LOCK_NB +#define LOCK_NB 4 +#endif +#ifndef LOCK_UN +#define LOCK_UN 8 +#endif +#ifndef S_IFLNK +#define S_IFLNK 0xA000 +#endif + +namespace void_win_storage +{ +inline std::mutex& io_mutex() +{ + static std::mutex mutex; + return mutex; +} + +inline std::filesystem::path from_utf8(const char* text) +{ + return std::filesystem::path{std::u8string{reinterpret_cast(text)}}; +} + +inline std::string to_utf8(const wchar_t* text) +{ + const std::u8string value = std::filesystem::path{text}.u8string(); + return std::string{value.begin(), value.end()}; +} +} // namespace void_win_storage + +inline int fstat(const int fd, struct stat* status) +{ + struct _stat64 info {}; + if (::_fstat64(fd, &info) != 0) { + return -1; + } + std::memset(status, 0, sizeof(*status)); + status->st_mode = static_cast(info.st_mode); + status->st_nlink = static_cast(info.st_nlink); + status->st_size = static_cast(info.st_size); + return 0; +} + +inline int lstat(const char* path, struct stat* status) +{ + std::error_code ec; + const auto link = std::filesystem::symlink_status( + void_win_storage::from_utf8(path), ec); + if (ec) { + errno = ENOENT; + return -1; + } + std::memset(status, 0, sizeof(*status)); + if (std::filesystem::is_symlink(link)) { + status->st_mode = S_IFLNK | 0777; + } else if (std::filesystem::is_directory(link)) { + status->st_mode = S_IFDIR | 0755; + } else { + status->st_mode = S_IFREG | 0644; + status->st_size = static_cast( + std::filesystem::file_size(void_win_storage::from_utf8(path), ec)); + } + return 0; +} + +inline ssize_t pread(const int fd, void* buffer, const std::size_t count, const off_t offset) +{ + std::lock_guard lock{void_win_storage::io_mutex()}; + if (::_lseeki64(fd, offset, SEEK_SET) < 0) { + return -1; + } + return ::_read(fd, buffer, static_cast(count)); +} + +inline ssize_t pwrite(const int fd, const void* buffer, const std::size_t count, const off_t offset) +{ + std::lock_guard lock{void_win_storage::io_mutex()}; + if (::_lseeki64(fd, offset, SEEK_SET) < 0) { + return -1; + } + return ::_write(fd, buffer, static_cast(count)); +} + +inline int fsync(const int fd) +{ + const intptr_t raw = ::_get_osfhandle(fd); + if (raw == -1) { + errno = EBADF; + return -1; + } + if (::FlushFileBuffers(reinterpret_cast(raw)) != 0) { + return 0; + } + // Read-only file handles cannot be flushed; there is nothing pending. + if (::GetLastError() == ERROR_ACCESS_DENIED) { + return 0; + } + errno = EIO; + return -1; +} + +inline int ftruncate(const int fd, const off_t size) +{ + return ::_chsize_s(fd, static_cast<__int64>(size)) == 0 ? 0 : -1; +} + +inline int flock(const int fd, const int operation) +{ + const HANDLE handle = reinterpret_cast(::_get_osfhandle(fd)); + if (handle == INVALID_HANDLE_VALUE) { + errno = EBADF; + return -1; + } + OVERLAPPED overlapped{}; + if ((operation & LOCK_UN) != 0) { + return ::UnlockFileEx(handle, 0, MAXDWORD, MAXDWORD, &overlapped) != 0 ? 0 : -1; + } + DWORD flags = 0; + if ((operation & LOCK_EX) != 0) { + flags |= LOCKFILE_EXCLUSIVE_LOCK; + } + if ((operation & LOCK_NB) != 0) { + flags |= LOCKFILE_FAIL_IMMEDIATELY; + } + if (::LockFileEx(handle, flags, 0, MAXDWORD, MAXDWORD, &overlapped) == 0) { + errno = EACCES; + return -1; + } + return 0; +} + +inline int link(const char* from, const char* to) +{ + if (::CreateHardLinkW(void_win_storage::from_utf8(to).c_str(), + void_win_storage::from_utf8(from).c_str(), + nullptr) == 0) { + errno = EEXIST; + return -1; + } + return 0; +} + +inline int getpid() +{ + return ::_getpid(); +} + +struct statvfs +{ + unsigned long f_bavail{0}; + unsigned long f_frsize{1}; +}; + +inline int statvfs(const char* path, struct statvfs* out) +{ + ULARGE_INTEGER free_for_user{}; + ULARGE_INTEGER total{}; + ULARGE_INTEGER total_free{}; + const std::filesystem::path wide = void_win_storage::from_utf8(path); + if (::GetDiskFreeSpaceExW(wide.c_str(), &free_for_user, &total, + &total_free) == 0) { + errno = ENOENT; + return -1; + } + out->f_frsize = 1; + out->f_bavail = static_cast(free_for_user.QuadPart); + return 0; +} + +inline int lstat(const wchar_t* path, struct stat* status) +{ + return lstat(void_win_storage::to_utf8(path).c_str(), status); +} + +inline int statvfs(const wchar_t* path, struct statvfs* out) +{ + return statvfs(void_win_storage::to_utf8(path).c_str(), out); +} + +inline int link(const wchar_t* from, const wchar_t* to) +{ + return link(void_win_storage::to_utf8(from).c_str(), + void_win_storage::to_utf8(to).c_str()); +} + +inline int unlink(const wchar_t* path) +{ + return ::_wunlink(path); +} + +// POSIX rename replaces the destination; the CRT rename does not. +inline int rename(const wchar_t* from, const wchar_t* to) +{ + if (::MoveFileExW(from, to, + MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH) == 0) { + errno = EIO; + return -1; + } + return 0; +} + +// The mode is ignored by _wopen unless O_CREAT is set. +inline int open(const wchar_t* path, const int flags, const int mode = 0) +{ + const DWORD attributes = ::GetFileAttributesW(path); + if ((flags & O_NOFOLLOW) != 0 && attributes != INVALID_FILE_ATTRIBUTES && + (attributes & FILE_ATTRIBUTE_REPARSE_POINT) != 0) { + errno = ELOOP; + return -1; + } + // The CRT cannot open directories; give sync_dir() a real handle-backed + // descriptor so FlushFileBuffers works. + if ((flags & (O_CREAT | O_EXCL)) == 0 && attributes != INVALID_FILE_ATTRIBUTES && + (attributes & FILE_ATTRIBUTE_DIRECTORY) != 0) { + const HANDLE handle = ::CreateFileW( + path, GENERIC_READ | GENERIC_WRITE, + FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr, + OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr); + if (handle == INVALID_HANDLE_VALUE) { + errno = EACCES; + return -1; + } + const int fd = ::_open_osfhandle(reinterpret_cast(handle), + _O_RDWR | _O_BINARY); + if (fd < 0) { + (void)::CloseHandle(handle); + errno = EMFILE; + return -1; + } + return fd; + } + return ::_wopen(path, flags & ~O_NOFOLLOW, mode); +} +#endif // _WIN32 namespace Void::Storage { @@ -36,7 +296,9 @@ namespace Void::Storage #endif return StorageErrorCode::unsupported_capability; case ENOSPC: +#ifdef EDQUOT case EDQUOT: +#endif return StorageErrorCode::quota_exceeded; case EINVAL: return StorageErrorCode::invalid_argument; diff --git a/tests/standalone/Storage/Storage.cpp b/tests/standalone/Storage/Storage.cpp index 3d2ec78..8232065 100644 --- a/tests/standalone/Storage/Storage.cpp +++ b/tests/standalone/Storage/Storage.cpp @@ -911,7 +911,15 @@ Void::Testing::Test storage_file_port_space_link_noreplace_and_no_follow() Void::Testing::require((*file)->close().has_value()); } const auto symlink_path = dir.path / "symlink"; +#ifdef _WIN32 + std::error_code symlink_ec; + std::filesystem::create_symlink(target, symlink_path, symlink_ec); + if (symlink_ec) { + Void::Testing::skip("symlink creation is not permitted"); + } +#else Void::Testing::require(::symlink(target.c_str(), symlink_path.c_str()) == 0); +#endif Void::Testing::expect( !port->open_file(symlink_path, FileOpenMode::ReadOnly).has_value()); }