Repository navigation
io: implement dirCreateFileAtomic - #382
Conversation
Creates temporary files with random hex names for atomic file operations. Supports link() for atomic create and replace() for atomic overwrite.
|
ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughThis PR implements atomic file creation ( ChangesAtomic File Creation with Windows Path Handling
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
Three issues: 1. Implement renameatPreserve for Windows using MoveFileExW without MOVEFILE_REPLACE_EXISTING, which fails if the destination exists. 2. Remove panic for allow_directory=false in dirOpenFileImpl and pass the flag through to openat. On Windows, CreateFileW without FILE_FLAG_BACKUP_SEMANTICS already blocks opening directories. Add allow_directory field to FileOpenFlags. 3. Normalize forward slashes to backslashes in utf8ToWide. Wine's CreateFileW with FILE_FLAG_BACKUP_SEMANTICS returns INVALID_NAME for paths that use forward slashes as separators. Co-authored-by: Lukáš Lalinský <[email protected]>
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/os/fs.zig (1)
574-599:⚠️ Potential issue | 🟠 Major | ⚡ Quick win
allow_directory=falsecurrently returns the wrong error on Windows.Line 108 documents
error.IsDir, but in Line 597ACCESS_DENIEDis returned aserror.AccessDenied. For directory targets this breaks the contract and caller behavior.💡 Suggested fix
if (handle == w.INVALID_HANDLE_VALUE) { - return switch (w.GetLastError()) { + return switch (w.GetLastError()) { .FILE_NOT_FOUND => error.FileNotFound, .PATH_NOT_FOUND => error.FileNotFound, - .ACCESS_DENIED => error.AccessDenied, + .ACCESS_DENIED => blk: { + if (!flags.allow_directory) { + const attrs = w.GetFileAttributesW(path_w.ptr); + if (attrs != w.INVALID_FILE_ATTRIBUTES and (attrs & w.FILE_ATTRIBUTE_DIRECTORY) != 0) { + break :blk error.IsDir; + } + } + break :blk error.AccessDenied; + }, else => |err| return unexpectedError(err), }; }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/os/fs.zig` around lines 574 - 599, The code maps w.ACCESS_DENIED to error.AccessDenied after CreateFileW, but when flags.allow_directory is false Windows returns ACCESS_DENIED for directory targets and the API contract expects error.IsDir; update the GetLastError switch in the CreateFileW failure handling so that when the error is .ACCESS_DENIED and flags.allow_directory is false (i.e., when FILE_FLAG_BACKUP_SEMANTICS was not set) you return error.IsDir instead of error.AccessDenied; keep other mappings (.FILE_NOT_FOUND/.PATH_NOT_FOUND => error.FileNotFound) unchanged and reference the symbols flags.allow_directory, file_flags/FILE_FLAG_BACKUP_SEMANTICS, CreateFileW and the GetLastError switch to locate where to change the mapping.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/io.zig`:
- Around line 625-643: The new directory handle created by
dirCreateDirPathOpenImpl or dirOpenDirImpl (assigned to new_dir) is not closed
if the subsequent atomicFileInit call fails, leaking handles; modify the block
around dirCreateDirPathOpenImpl/dirOpenDirImpl and atomicFileInit so that when
atomicFileInit returns an error you close new_dir (call its close method or use
a defer/cleanup pattern) before returning the error, and apply the same fix to
the analogous block that covers the other branch (the code paths referenced by
dirCreateDirPathOpenImpl, dirOpenDirImpl, atomicFileInit and the new_dir
variable).
---
Outside diff comments:
In `@src/os/fs.zig`:
- Around line 574-599: The code maps w.ACCESS_DENIED to error.AccessDenied after
CreateFileW, but when flags.allow_directory is false Windows returns
ACCESS_DENIED for directory targets and the API contract expects error.IsDir;
update the GetLastError switch in the CreateFileW failure handling so that when
the error is .ACCESS_DENIED and flags.allow_directory is false (i.e., when
FILE_FLAG_BACKUP_SEMANTICS was not set) you return error.IsDir instead of
error.AccessDenied; keep other mappings (.FILE_NOT_FOUND/.PATH_NOT_FOUND =>
error.FileNotFound) unchanged and reference the symbols flags.allow_directory,
file_flags/FILE_FLAG_BACKUP_SEMANTICS, CreateFileW and the GetLastError switch
to locate where to change the mapping.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 0e3653ce-2054-4273-9081-d54f8ed69c18
📒 Files selected for processing (3)
src/io.zigsrc/os/fs.zigsrc/os/windows.zig
Add errdefer to close the parent directory handle if atomicFileInit returns an error, preventing handle leaks under repeated failures. Co-authored-by: Lukáš Lalinský <[email protected]>
Summary
dirCreateFileAtomicImplfor atomic file creationlink()for atomic create andreplace()for atomic overwritemake_pathoption to create parent directoriesTest plan
createFileAtomiclink, replace, and make_path scenarios