Skip to content

Hermit: fix readdir() #158649

Open
jounathaen wants to merge 6 commits into
rust-lang:mainfrom
hermit-os:hermit-readdir
Open

Hermit: fix readdir() #158649
jounathaen wants to merge 6 commits into
rust-lang:mainfrom
hermit-os:hermit-readdir

Conversation

@jounathaen

Copy link
Copy Markdown

On Hermit, the sys_getdents implementation was flawed, as it wasn't stateful. So the caller had to provide a buffer large enough to hold all dirents of the directory in question. Else it returns EINVAL. The current workaround used in Rust is to grow the buffer until it is sufficiently large.

hermit-os/kernel#1738 fixes the behavior of sys_getdents. Hermit now keeps track of the directory position, and each new call to the sys_getdents provides the next entries.

But this results in the current implementation in Rust Std being flawed: Because Hermit now returns a proper readcount, read_dir will only ever iterate over the first 512 bytes of directory entries, even if not all dirent64s are read.

This PR fixes this with a proper implementation of the getdents64 algorithm.

Remark:
This breaks compatibility with Hermit versions 0.11 and previous. We have discussed this in the Hermit team and have come to the conclusion that this is ok. We have released two newer "major" versions since, and Hermit is still in the development phase where we don't guarantee any stability and backwards compatibility in the kernel itself.

mkroening and others added 6 commits July 1, 2026 11:18
This is also how it is done on other platforms.
This optimization was already partially in place, but not used.
Other platforms already do this.
Previously, we read all entries into a huge initialized buffer up front,
which does not work correctly since on Hermit 0.12, getdents64 behaves more
reasonable and does no longer fail on buffers which cannot hold all
entries. Additionally, for each new entry, we started searching for the
current position from the start instead of just saving the position
directly.

The new design uses a fixed-size uninitialized buffer that is read into
as necessary.

Co-authored-by: Martin Kröning <martin.kroening@eonerc.rwth-aachen.de>
While Hermit does not return these yet, it will do so in the future.
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jul 1, 2026
@rustbot

rustbot commented Jul 1, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request, and welcome! The Rust Project is excited to review your changes, and you should hear from @joboet (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue
Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: @ChrisDenton, libs
  • @ChrisDenton, libs expanded to 13 candidates
  • Random selection from 7 candidates
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.

4 participants