Repository navigation
Add Windows file I/O support with IOCP compatibility - #13
Conversation
* Add new zio.fs module with openFile and createFile functions * Implement Windows-specific file creation with FILE_FLAG_OVERLAPPED * Add configurable file sharing permissions for Windows * Enable cross-platform async file operations * Remove Windows test skip conditions from existing file tests * Add comprehensive documentation for all public APIs This resolves the Windows file I/O blocking issue where files created with std.fs.File couldn't be used with IOCP due to missing FILE_FLAG_OVERLAPPED flag. Key features: - Windows: Uses CreateFileW with proper IOCP flags - Unix: Uses standard std.fs operations - Configurable sharing modes (none, read, write, read_write) - Full path support (absolute and relative paths) - Comprehensive error handling and documentation Fixes #IOCP_FILE_HANDLE_ISSUE
WalkthroughAdds a cross-platform async filesystem module with Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant Test as Test
participant FS as fs
participant OS as OS
Note over Test,FS: Create → Write → Close → Reopen → Read → Delete
Test->>FS: createFile(runtime, path, CreateFlags)
FS->>OS: platform-specific create/open
OS-->>FS: handle
FS-->>Test: File handle
Test->>Test: write via in-memory zio_file wrapper
Test->>FS: close(zio_file)
FS->>OS: close handle
OS-->>FS: ok
Test->>FS: openFile(runtime, path, OpenFlags{Read})
FS->>OS: platform-specific open
OS-->>FS: handle
FS-->>Test: read handle
Test->>Test: read via reopened read handle
Test->>FS: close(read_handle)
FS->>OS: close
OS-->>FS: ok
Test->>OS: deleteFile(path)
OS-->>Test: ok
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🧪 Early access (Sonnet 4.5): enabledWe are currently testing the Sonnet 4.5 model, which is expected to improve code review quality. However, this model may lead to increased noise levels in the review comments. Please disable the early access features if the noise level causes any inconvenience. Note:
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
src/fs.zig (1)
185-199: Consider simplifying the Unix file creation logic.The nested switch with error handling could be simplified for better readability.
Consider extracting the read flag logic:
+ const allow_read = options.mode != .write_only; const std_file = switch (options.create) { .create_new => blk: { - const file = std.fs.cwd().createFile(path, .{ .read = options.mode != .write_only, .exclusive = true }) catch |err| switch (err) { + const file = std.fs.cwd().createFile(path, .{ .read = allow_read, .exclusive = true }) catch |err| switch (err) { error.PathAlreadyExists => return error.PathAlreadyExists, else => return err, }; break :blk file; }, - .create_always => try std.fs.cwd().createFile(path, .{ .read = options.mode != .write_only }), - .open_always => std.fs.cwd().createFile(path, .{ .read = options.mode != .write_only }) catch |err| switch (err) { + .create_always => try std.fs.cwd().createFile(path, .{ .read = allow_read }), + .open_always => std.fs.cwd().createFile(path, .{ .read = allow_read }) catch |err| switch (err) { error.PathAlreadyExists => try std.fs.cwd().openFile(path, flags), else => return err, }, .open_existing => try std.fs.cwd().openFile(path, flags), };
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
src/file.zig(5 hunks)src/fs.zig(1 hunks)src/zio.zig(1 hunks)
🧰 Additional context used
🪛 GitHub Actions: CI
src/fs.zig
[error] 116-116: Type mismatch: expected type '?*anyopaque', found 'mem.Allocator' when calling windows.sliceToPrefixedFileW. Build attempted with command 'zig test Debug native', which failed with exit code 1.
🔇 Additional comments (6)
src/zio.zig (1)
17-17: LGTM! Clean module re-export following established patterns.The fs module re-export is properly integrated alongside existing I/O functionality exports, maintaining consistency with the codebase's module organization.
src/file.zig (3)
259-312: Test implementation looks good with proper file lifecycle management.The migration to use the new fs module is well-executed. The test properly:
- Creates files using fs.createFile
- Closes the file after writing before reopening for reading
- Handles cleanup with deleteFile in defer blocks
- Maintains error handling for close operations
314-356: Positional I/O test correctly adapted to new fs module.The test maintains its original functionality while properly using the new fs API for file creation and ensuring cleanup through defer statements.
358-390: Close operation test properly migrated.The test correctly uses fs.createFile and maintains proper cleanup semantics.
src/fs.zig (2)
142-151: Windows implementation correctly uses FILE_FLAG_OVERLAPPED for IOCP compatibility.Good implementation addressing the IOCP compatibility requirement. The use of FILE_FLAG_OVERLAPPED ensures async operations won't block the thread pool as described in the PR objectives.
214-254: Well-structured test covering basic fs module operations.The test properly exercises file creation, writing, closing, reopening for reading, and cleanup. Good coverage of the core API surface.
|
@claude address CI build failure and coderabbits comments |
|
Claude finished @lalinsky's task —— View job ✅ Fixed CI build failure and addressed CodeRabbit comments
SummaryFixed the Windows compilation error in
The fix has been tested locally with all 27 tests passing and pushed to the branch. |
- Pass null instead of allocator to sliceToPrefixedFileW - Remove unnecessary PathSpace memory management (stack-allocated) - Fixes CI build failure on Windows Co-authored-by: Lukáš Lalinský <[email protected]>
- Replace custom OpenOptions/CreateOptions with std.fs.File.OpenFlags/CreateFlags - Add file locking checks that return error.Unsupported for async I/O context - Use FILE_SHARE_DELETE in Windows sharing mode to match std.fs behavior - Fix positional I/O test to create file with read permissions on Windows All tests now pass on both Linux and Windows.
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (2)
src/fs.zig (2)
91-98: Consider mapping PATH_NOT_FOUND to FileNotFound.The error mapping handles
FILE_NOT_FOUND, but Windows can also returnPATH_NOT_FOUND(error code 3) when a directory component in the path doesn't exist. Consider adding this case for consistency:if (handle == windows.INVALID_HANDLE_VALUE) { const err = windows.kernel32.GetLastError(); return switch (err) { windows.Win32Error.FILE_NOT_FOUND => error.FileNotFound, + windows.Win32Error.PATH_NOT_FOUND => error.FileNotFound, windows.Win32Error.ACCESS_DENIED => error.AccessDenied, else => windows.unexpectedError(err), }; }
142-150: Consider mapping PATH_NOT_FOUND to FileNotFound.Same as in
openFileWindows, consider addingPATH_NOT_FOUNDerror mapping for completeness:if (handle == windows.INVALID_HANDLE_VALUE) { const err = windows.kernel32.GetLastError(); return switch (err) { windows.Win32Error.FILE_NOT_FOUND => error.FileNotFound, + windows.Win32Error.PATH_NOT_FOUND => error.FileNotFound, windows.Win32Error.ACCESS_DENIED => error.AccessDenied, windows.Win32Error.FILE_EXISTS => error.PathAlreadyExists, else => windows.unexpectedError(err), }; }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/file.zig(5 hunks)src/fs.zig(1 hunks)
🔇 Additional comments (7)
src/fs.zig (4)
27-33: LGTM! Clean platform dispatch.The public API is well-documented and correctly dispatches to platform-specific implementations.
50-56: LGTM! Consistent with openFile.The API design and documentation are clear and consistent with
openFile.
156-174: LGTM! Clean delegation to std.fs.The Unix implementations correctly delegate to the standard library while maintaining the same file locking constraint as Windows. The distinction between
File.init(forstd.fs.File) andFile.initFd(for raw handles) is appropriate.
176-216: LGTM! Comprehensive integration test.The test covers the complete file lifecycle: create, write, close, reopen for reading, read, and cleanup. The close-before-reopen pattern is correct for async file I/O.
src/file.zig (3)
262-302: LGTM! Correct close/reopen pattern for cross-platform file I/O.The test correctly migrates to the new
fsmodule API and implements the close-before-reopen pattern necessary for Windows IOCP compatibility. The cleanup sequence (close, deinit, delete) is properly ordered.
317-347: LGTM! Positional I/O test correctly updated.The test correctly uses
fs.createFilewith.read = trueto enable both read and write operations. Sincepreadandpwriteuse explicit offsets, no close/reopen is needed between operations.
361-390: LGTM! Close test correctly migrated to fs API.The test correctly uses
fs.createFileand maintains the original purpose of verifying the close operation.
* Refactor cancelation * Refactor how cancelations work
Summary
This PR implements Windows file I/O support for the zio async I/O library by creating a new
zio.fsmodule with cross-platformopenFileandcreateFilefunctions.Problem
Windows files need
FILE_FLAG_OVERLAPPEDto work with IOCP (I/O Completion Ports), whichstd.fs.Filedoesn't provide. This caused file I/O operations to hang indefinitely on Windows as described inIOCP_FILE_HANDLE_ISSUE.md.Solution
zio.fsmodule withopenFileandcreateFilefunctionsCreateFileWwithFILE_FLAG_OVERLAPPEDstd.fsoperationsSharingModeenum (Windows-specific)Key Features
✅ Windows IOCP Integration - Files properly created with
FILE_FLAG_OVERLAPPED✅ Configurable File Sharing - Support for exclusive, read-only, write-only, or read-write access
✅ Full Path Support - Handles both absolute and relative paths
✅ Comprehensive Documentation - All public APIs documented with examples
✅ Test Coverage - All existing file tests now run on Windows
API Example
Breaking Changes
None - this is purely additive. Existing
zio.FileAPI remains unchanged.Testing
Test Results
Resolves the Windows file I/O blocking issue and enables full async file operations across all platforms.
Summary by CodeRabbit
New Features
Refactor
Tests
Chores