diff --git a/src/IO/FileWorkers.cpp b/src/IO/FileWorkers.cpp index 5a67738..b02509e 100644 --- a/src/IO/FileWorkers.cpp +++ b/src/IO/FileWorkers.cpp @@ -15,6 +15,13 @@ #include #include #define VOID_IO_POSIX_FILES 1 +#elif defined(_WIN32) +#include +#include +#include +#include +#include +#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 buf, std::uin return FileSyscallResult{-1, errno}; } return FileSyscallResult{static_cast(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(buf.size())); + if (n < 0) { + return FileSyscallResult{-1, errno}; + } + return FileSyscallResult{static_cast(n), 0}; #else (void)fd; (void)buf; @@ -58,6 +85,16 @@ FileSyscallResult FileSyscalls::pwrite(int fd, return FileSyscallResult{-1, errno}; } return FileSyscallResult{static_cast(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(buf.size())); + if (n < 0) { + return FileSyscallResult{-1, errno}; + } + return FileSyscallResult{static_cast(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 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(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; diff --git a/tests/standalone/IO/IoFile.cpp b/tests/standalone/IO/IoFile.cpp index 20b3ca6..16c94c9 100644 --- a/tests/standalone/IO/IoFile.cpp +++ b/tests/standalone/IO/IoFile.cpp @@ -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::numeric_limits::max()); +#else const auto off_max = static_cast(std::numeric_limits::max()); +#endif spawn(scope, do_read(&file, 0, {}, {}, &zr)); spawn(scope, do_write(&file, 0, {}, {}, &zw)); spawn(scope, do_read(&file, std::numeric_limits::max(), one, {}, &ov1));