Skip to content

Implement fileStat, dirStat, and dirStatPath operations - #162

Merged
lalinsky merged 3 commits into
zig-0.16from
implement-stat-operations
Dec 16, 2025
Merged

lalinsky merged 3 commits into
zig-0.16from
implement-stat-operations

Conversation

@lalinsky

Copy link
Copy Markdown
Owner

Summary

  • Implements File.stat() to get metadata of an open file
  • Implements Dir.stat() to get metadata of an open directory
  • Implements Dir.statPath() to get metadata at a relative path

These operations were previously unimplemented (marked as @panic("TODO")).

Implementation Details

  • All three operations use the aio FileStat async operation
  • Added helper function aioFileStatToStdIo() to convert between aio's FileStatInfo and std.Io's File.Stat
  • Main conversion: aio uses i64 nanoseconds for timestamps, std.Io uses Io.Timestamp { nanoseconds: i96 }
  • fileStat and dirStat call fstat() on file descriptor
  • dirStatPath calls fstatat() with directory fd and relative path

Test plan

  • Added test for File.stat() - verifies file size, kind, and timestamps
  • Added test for Dir.stat() - verifies directory kind and timestamps
  • Added test for Dir.statPath() - verifies stat via relative path
  • All 194 tests pass

Files Changed

  • src/fs/file.zig - Added stat() method to File struct
  • src/fs/dir.zig - Added stat() and statPath() methods to Dir struct
  • src/stdio.zig - Implemented vtable functions and added tests

Add support for file and directory stat operations in the std.Io interface:
- Implement File.stat() to get metadata of an open file
- Implement Dir.stat() to get metadata of an open directory
- Implement Dir.statPath() to get metadata at a relative path

These operations use the aio FileStat async operation and convert
between aio's FileStatInfo and std.Io's File.Stat format.

All three operations include comprehensive tests verifying file size,
kind, and timestamps.
@coderabbitai

coderabbitai Bot commented Dec 15, 2025 •

Copy link
Copy Markdown
Contributor

Walkthrough

Added stat operation methods to Dir and File structures enabling async metadata queries via FileStat. Integrated stdio support with aioFileStatToStdIo helper for converting aio stat results to standard Io.File.Stat format, including error translation and new test coverage.

Changes

Cohort / File(s) Summary
Dir and File stat API
src/fs/dir.zig, src/fs/file.zig
Added StatError type aliases and public stat/statPath methods to query file metadata asynchronously with runtime support.
Stdio integration
src/stdio.zig
Implemented dirStatImpl, dirStatPathImpl, fileStatImpl for error translation; added aioFileStatToStdIo helper converting aio stat results to std.Io.File.Stat with kind/timestamp mapping; introduced File stat, Dir stat, and Dir statPath test cases.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Error translation coverage: Verify aioFileStatToStdIo handles all relevant error cases consistently across dirStatImpl, dirStatPathImpl, and fileStatImpl.
  • Async execution flow: Confirm FileStat operations in Dir.stat/statPath and File.stat properly schedule, wait, and propagate results through the Runtime.
  • Test case completeness: Check that new Io test cases exercise the critical paths for file and directory stat operations.

Poem

📊 Stats bloom where queries dare to fly,
Async whispers gather metadata on high,
Dir and File now speak their truth with grace,
Errors mapped and tamed to their proper place.
✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title directly matches the main changeset: implementing fileStat, dirStat, and dirStatPath operations across File and Dir structs.
Description check ✅ Passed The description comprehensively covers the implementation, explaining what was added, how it works, the conversion logic, and test coverage.
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 implement-stat-operations

📜 Recent 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 b7347ec and 2192010.

📒 Files selected for processing (3)
  • src/fs/dir.zig (2 hunks)
  • src/fs/file.zig (1 hunks)
  • src/stdio.zig (3 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
  • src/fs/file.zig
  • src/fs/dir.zig
🧠 Learnings (2)
📚 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
  • src/fs/dir.zig
📚 Learning: 2025-10-10T05:59:17.087Z
Learnt from: lalinsky
Repo: lalinsky/zio PR: 41
File: src/udp.zig:79-82
Timestamp: 2025-10-10T05:59:17.087Z
Learning: In UDP socket operations (src/udp.zig), error.Canceled from result_data.result represents OS/IO-level cancellation (e.g., io_uring), not user-requested cancellation. User cancellation is handled by waitForXevCompletion() via try. OS-level error.Canceled should be mapped to error.Unexpected as it represents an unexpected system event.

Applied to files:

  • src/fs/dir.zig
🔇 Additional comments (8)
src/fs/file.zig (1)

136-150: LGTM - Clean stat implementation.

The File.stat() implementation follows the exact pattern established by other I/O methods in this file. Using aio.FileStat.init(self.fd, null) correctly performs fstat on the file descriptor without a path.

src/fs/dir.zig (2)

5-5: LGTM - Required import for stat operations.


75-103: LGTM - Consistent stat implementations.

Both stat() and statPath() follow the established pattern. The distinction is clear:

  • stat() uses null path to stat the directory itself (fstat)
  • statPath() uses the provided path for relative stat operations (fstatat)
src/stdio.zig (5)

300-312: LGTM - Directory stat implementation.

The error mapping is appropriate for statting a directory handle. The reduced error set compared to dirStatPathImpl makes sense since there's no path traversal involved.


449-473: LGTM - Clean conversion helper.

The kind mapping is exhaustive and the timestamp conversion from i64 to i96 nanoseconds is a safe widening cast. All relevant fields are mapped.


475-487: LGTM - File stat implementation.

Error handling matches dirStatImpl as both operate on file descriptors without path traversal.


1631-1714: LGTM - Good test coverage for stat operations.

The three tests verify the core functionality:

  1. File.stat() on open file handles
  2. Dir.statPath() on directories
  3. Dir.statPath() on files

All tests properly check size, kind, and timestamps. The tests align with the PR's intent and the commit messages explain the platform-specific adjustments (e.g., using statPath(".") instead of stat() on cwd).


314-336: The fstatat comment is correct—remove the incorrect verification note.

By default, fstatat() dereferences symbolic links, like stat(2). The code comment accurately describes POSIX behavior. The logic of rejecting follow_symlinks=false and passing through to aio.FileStat when true aligns with this default, assuming aio matches fstatat's behavior. If aio has a different default, that would need documenting, but the fstatat statement itself is sound.


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.

Use statPath with "." instead of calling stat() directly on the cwd()
handle, since cwd() returns a special handle (AT_FDCWD on POSIX, PEB
handle on Windows) that doesn't support fstat on all platforms.
Windows doesn't handle statting "." correctly through the path conversion.
Changed the test to create an actual temporary directory using std.fs.cwd().makeDir()
and then stat that directory via statPath().
@lalinsky

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Dec 16, 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.

@lalinsky
lalinsky merged commit 6b4eabd into zig-0.16 Dec 16, 2025
@lalinsky
lalinsky deleted the implement-stat-operations branch December 16, 2025 18:48
lalinsky added a commit that referenced this pull request Dec 16, 2025
* Implement fileStat, dirStat, and dirStatPath operations

Add support for file and directory stat operations in the std.Io interface:
- Implement File.stat() to get metadata of an open file
- Implement Dir.stat() to get metadata of an open directory
- Implement Dir.statPath() to get metadata at a relative path

These operations use the aio FileStat async operation and convert
between aio's FileStatInfo and std.Io's File.Stat format.

All three operations include comprehensive tests verifying file size,
kind, and timestamps.

* Fix Dir stat test for macOS and Windows

Use statPath with "." instead of calling stat() directly on the cwd()
handle, since cwd() returns a special handle (AT_FDCWD on POSIX, PEB
handle on Windows) that doesn't support fstat on all platforms.

* Fix Windows CI: create real directory for Dir stat test

Windows doesn't handle statting "." correctly through the path conversion.
Changed the test to create an actual temporary directory using std.fs.cwd().makeDir()
and then stat that directory via statPath().
@coderabbitai coderabbitai Bot mentioned this pull request Jan 1, 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