IO: implement positional-file syscalls on Windows

pread/pwrite use serialized seek+read/write on the CRT, sync uses
_commit, close uses _close, and open_file_native opens files in binary
mode with _wopen. The offset-boundary test uses the backend's int64
limit instead of assuming 64-bit off_t.

Windows cross build under Wine 11 (x86_64-w64-mingw32 GCC 16.2):
Storage;Testing 674 tests (667 passed, 7 skipped, 0 failed).
Linux Storage;Testing: 674 tests (671 passed, 3 skipped, 0 failed).
This commit is contained in:
faumaray committed 2026-12-06 02:33:12 +03:00
1 parent 88cecb72c9
commit ea285d1c65
2 files changed
+85

No files matched your search

+79
View File
@@ -15,6 +15,13 @@
#include <sys/types.h>
#include <unistd.h>
#define VOID_IO_POSIX_FILES 1
#elif defined(_WIN32)
#include <io.h>
#include <fcntl.h>
#include <process.h>
#include <sys/stat.h>
#include <mutex>
#define VOID_IO_WINDOWS_FILES 1
#endif
namespace Void::IO
@@ -23,6 +30,16 @@ namespace Void::IO
namespace
{
thread_local const detail::FileWorkerShared* tl_io_worker_owner = nullptr;
#ifdef VOID_IO_WINDOWS_FILES
// The CRT has no pread/pwrite: serialize seek+read/write. The worker pool
// already allows concurrent reads on one fd, so the lock is required.
std::mutex& file_io_mutex()
{
static std::mutex mutex;
return mutex;
}
#endif
} // namespace
bool is_io_worker_thread() noexcept
@@ -40,6 +57,16 @@ FileSyscallResult FileSyscalls::pread(int fd, std::span<std::byte> buf, std::uin
return FileSyscallResult{-1, errno};
}
return FileSyscallResult{static_cast<std::int64_t>(n), 0};
#elif defined(VOID_IO_WINDOWS_FILES)
std::lock_guard lock{file_io_mutex()};
if (::_lseeki64(fd, static_cast<__int64>(offset), SEEK_SET) < 0) {
return FileSyscallResult{-1, errno};
}
const int n = ::_read(fd, buf.data(), static_cast<unsigned int>(buf.size()));
if (n < 0) {
return FileSyscallResult{-1, errno};
}
return FileSyscallResult{static_cast<std::int64_t>(n), 0};
#else
(void)fd;
(void)buf;
@@ -58,6 +85,16 @@ FileSyscallResult FileSyscalls::pwrite(int fd,
return FileSyscallResult{-1, errno};
}
return FileSyscallResult{static_cast<std::int64_t>(n), 0};
#elif defined(VOID_IO_WINDOWS_FILES)
std::lock_guard lock{file_io_mutex()};
if (::_lseeki64(fd, static_cast<__int64>(offset), SEEK_SET) < 0) {
return FileSyscallResult{-1, errno};
}
const int n = ::_write(fd, buf.data(), static_cast<unsigned int>(buf.size()));
if (n < 0) {
return FileSyscallResult{-1, errno};
}
return FileSyscallResult{static_cast<std::int64_t>(n), 0};
#else
(void)fd;
(void)buf;
@@ -80,6 +117,12 @@ FileSyscallResult FileSyscalls::sync(int fd, SyncMode mode) noexcept
return FileSyscallResult{-1, errno};
}
return FileSyscallResult{0, 0};
#elif defined(VOID_IO_WINDOWS_FILES)
(void)mode; // No fdatasync distinction on Windows.
if (::_commit(fd) != 0) {
return FileSyscallResult{-1, errno};
}
return FileSyscallResult{0, 0};
#else
(void)fd;
(void)mode;
@@ -94,6 +137,11 @@ FileSyscallResult FileSyscalls::close(int fd) noexcept
return FileSyscallResult{-1, errno};
}
return FileSyscallResult{0, 0};
#elif defined(VOID_IO_WINDOWS_FILES)
if (::_close(fd) != 0) {
return FileSyscallResult{-1, errno};
}
return FileSyscallResult{0, 0};
#else
(void)fd;
return FileSyscallResult{-1, ENOSYS};
@@ -483,6 +531,37 @@ Void::Result::Result<int> open_file_native(const std::filesystem::path& path,
IoErrorCode::os_error, "open failed for " + path.string(), errno_code(errno))};
}
return fd;
#elif defined(VOID_IO_WINDOWS_FILES)
int flags = _O_BINARY | _O_NOINHERIT;
if (options.read && options.write) {
flags |= _O_RDWR;
} else if (options.write) {
flags |= _O_WRONLY;
} else {
flags |= _O_RDONLY;
}
if (options.create) {
flags |= _O_CREAT;
}
if (options.truncate) {
flags |= _O_TRUNC;
}
if (options.exclusive) {
flags |= _O_EXCL;
}
int fd = -1;
for (;;) {
fd = ::_wopen(path.c_str(), flags, static_cast<int>(options.mode));
if (fd < 0 && errno == EINTR) {
continue;
}
break;
}
if (fd < 0) {
return std::unexpected{make_io_error(
IoErrorCode::os_error, "open failed for " + path.string(), errno_code(errno))};
}
return fd;
#else
(void)path;
(void)options;
+6
View File
@@ -1774,7 +1774,13 @@ Void::Testing::Test io_file_zero_length_overflow_and_zero_queue_limit()
TaskScope scope{exec};
auto drv = make_driver(exec, mock_options({}, &sys));
file = open_rw(*drv, dir.file("edges.bin"));
#ifdef _WIN32
// The backend's limit is int64 max; off_t is 32-bit in MinGW.
const auto off_max = static_cast<std::uint64_t>(
std::numeric_limits<std::int64_t>::max());
#else
const auto off_max = static_cast<std::uint64_t>(std::numeric_limits<off_t>::max());
#endif
spawn(scope, do_read(&file, 0, {}, {}, &zr));
spawn(scope, do_write(&file, 0, {}, {}, &zw));
spawn(scope, do_read(&file, std::numeric_limits<std::uint64_t>::max(), one, {}, &ov1));