Skip to content

Implement std.Io dirCreateFile and dirOpenFile - #156

Merged
lalinsky merged 1 commit into
zig-0.16from
stdio-dir-file-ops
Dec 8, 2025
Merged

lalinsky merged 1 commit into
zig-0.16from
stdio-dir-file-ops

Conversation

@lalinsky

@lalinsky lalinsky commented Dec 8, 2025

Copy link
Copy Markdown
Owner

Summary

  • Implement dirCreateFileImpl and dirOpenFileImpl for the std.Io interface
  • Add proper error mapping from aio errors to std.Io errors
  • Return error.Unexpected for unsupported options (lock, lock_nonblocking, allow_ctty, follow_symlinks=false)
  • Add test for the new functionality

Test plan

  • All existing Io tests pass (21/21)
  • New test Io: Dir createFile/openFile passes

@coderabbitai

coderabbitai Bot commented Dec 8, 2025 •

Copy link
Copy Markdown
Contributor

Walkthrough

The PR replaces TODO implementations in src/stdio.zig with functional directory file operations. It adds flag validation, converts between std I/O and zio dir handles, maps file operation flags, and invokes corresponding zio operations while translating errors. A test validates basic file creation and read/write workflows.

Changes

Cohort / File(s) Summary
Module imports
src/stdio.zig
Adds imports for aio and fs/dir.zig to support directory file operations
Directory file operations
src/stdio.zig
Implements dirCreateFileImpl and dirOpenFileImpl with flag validation, handle conversion between std I/O and zio dir types, flag mapping to aio.system.fs constants, zio operation invocation, and error translation
Test coverage
src/stdio.zig
Adds "Io: Dir createFile/openFile" test exercising file creation, writing, and reading through directory operations

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Flag validation logic: ensure all unsupported cases (lock flags, ctty, follow_symlinks) are correctly rejected
  • Handle conversion accuracy between std I/O Dir and zio_dir_io.Dir types
  • Flag mapping completeness from std I/O flags to aio.system.fs equivalents
  • Error translation mappings and whether all zio errors have corresponding std I/O translations
  • File descriptor management in File construction (fromFd usage)

Possibly related PRs

Poem

📂 Directories now open files with flair,
Flags validated, handles mapped with care,
Errors translated across the divide,
Tests confirm what was hidden inside. 🔧

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Implement std.Io dirCreateFile and dirOpenFile' directly and specifically describes the main change—implementing two functions for directory file operations in the std.Io interface.
Description check ✅ Passed The description accurately outlines the implementation work, error handling strategy, and test coverage, directly relating to the changeset content.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch stdio-dir-file-ops

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@lalinsky

lalinsky commented Dec 8, 2025

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Dec 8, 2025

Copy link
Copy Markdown
Contributor
✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/stdio.zig (1)

1530-1537: Missing errdefer for file close on error path.

If writePositional fails, created_file never gets closed. On Windows, the subsequent deleteFile in the defer would fail since you can't delete an open file. Add an errdefer to handle this:

             // Create a new file
             const created_file = try cwd.createFile(io, file_path, .{});
             defer std.fs.cwd().deleteFile(file_path) catch {};
+            errdefer created_file.close(io);

             // Write some data
             var write_buf = [_][]const u8{"hello world"};
             _ = try created_file.writePositional(io, &write_buf, 0);
             created_file.close(io);
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f5d8842 and af63f8c.

📒 Files selected for processing (1)
  • src/stdio.zig (4 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.zig

📄 CodeRabbit inference engine (CLAUDE.md)

Use zig env to get the path to the Zig standard library when needed

Files:

  • src/stdio.zig
🧠 Learnings (5)
📓 Common learnings
Learnt from: CR
Repo: lalinsky/zio PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-11-30T10:31:31.222Z
Learning: Applies to **/*.zig : Use `zig env` to get the path to the Zig standard library when needed
📚 Learning: 2025-11-30T10:31:31.222Z
Learnt from: CR
Repo: lalinsky/zio PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-11-30T10:31:31.222Z
Learning: Applies to **/*.zig : Use `zig env` to get the path to the Zig standard library when needed

Applied to files:

  • src/stdio.zig
📚 Learning: 2025-11-04T10:29:56.949Z
Learnt from: lalinsky
Repo: lalinsky/zio PR: 118
File: src/core/task.zig:33-115
Timestamp: 2025-11-04T10:29:56.949Z
Learning: In zio's task implementation (src/core/task.zig), the Closure struct intentionally caps result_len and context_len at 4096 bytes (using u12 fields) and alignment at 16 bytes (using u4 padding fields). This is a deliberate design decision to avoid bloating task structures. Users needing larger result or context storage should heap-allocate and pass pointers instead. These limits are more generous than std lib's fixed storage (64 bytes for results, 1024 bytes for args).
<!-- [add_learning]
The task storage limits in zio (4 KiB for result/context, 16-byte alignment) are intentional design constraints to prevent task bloat, not correctness issues. The maintainer's position is that larger data should be heap-allocated.

Applied to files:

  • src/stdio.zig
📚 Learning: 2025-10-23T16:17:53.575Z
Learnt from: lalinsky
Repo: lalinsky/zio PR: 70
File: src/select.zig:145-153
Timestamp: 2025-10-23T16:17:53.575Z
Learning: In src/select.zig, the select() function is designed to accept futures either as copyable wrappers (e.g., JoinHandle(T)) by value, or as pointers (e.g., &reset_event) for value-owned types. This is intentional, and users are expected to pass the appropriate form based on the future type.

Applied to files:

  • src/stdio.zig
📚 Learning: 2025-11-30T10:31:31.222Z
Learnt from: CR
Repo: lalinsky/zio PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-11-30T10:31:31.222Z
Learning: Applies to build.zig : To test Windows builds on Linux, use `zig build build-tests -Dtarget=x86_64-windows && wine zig-out/bin/test.exe`

Applied to files:

  • src/stdio.zig
🔇 Additional comments (3)
src/stdio.zig (3)

2-2: LGTM!

New imports are required for the directory file operations - aio for flag types and zio_dir_io for the Dir wrapper.

Also applies to: 15-15


322-363: LGTM!

Clean implementation. The error mapping covers the expected filesystem errors, and the catch-all handles any future additions from the underlying layer gracefully. Flag validation correctly rejects lock options that aren't supported.


365-409: LGTM!

Proper validation of unsupported flags per the PR description. The mode mapping is direct, and the error handling mirrors the create implementation which keeps things consistent.

@lalinsky
lalinsky merged commit e201f84 into zig-0.16 Dec 8, 2025
@lalinsky
lalinsky deleted the stdio-dir-file-ops branch December 8, 2025 09:57
@coderabbitai coderabbitai Bot mentioned this pull request Jan 9, 2026
@coderabbitai coderabbitai Bot mentioned this pull request May 11, 2026
1 task done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant