Skip to content

Make (most?) std::os::wasi::fs::FileExt functions available on all Unix platforms #102776

@alyssais

Description

@alyssais
Contributor

The methods in std::os::wasi::fs::FileExt look a lot like the POSIX *at methods. Would it be possible to make this extension available for all Unix platforms, rather than limiting it to wasi?

There are a lot of reasons to want to use these methods from other Unix operating systems:

  • They make it possible to work with paths longer than MAX_PATH.
  • They are the only way to interact with files in a Capsicum sandbox on FreeBSD.
  • They can prevent TOCTOU vulnerabilities where a directory is moved between accesses to files within that directory.

Tracking issue for wasi_ext: #71213

Activity

ChrisDenton

ChrisDenton commented on Oct 7, 2022

@ChrisDenton
Member

There is definitely a lot of interest in bringing *at style functions to more that just os::wasi. The ideal would be to have cross-platform APIs where possible rather than something Unix specific.

the8472

the8472 commented on Oct 8, 2022

@the8472
Member

I don't think the wasi API is ideal. Usually you need to hold onto a directory and be able to iterate it at the same time. So the *at methods should primarily be available on a ReadDir and optionally on File.
If we do this in std then this can be done via a trait or a Fd wrapper type.

But if we want to let other crates handle this, e.g. to support async support then they'd need a way to get access to the file descriptor in ReadDir. But that's problematic due to the posix spec:

Upon successful return from fdopendir(), the file descriptor is under the control of the system, and if any attempt is made to close the file descriptor, or to modify the state of the associated description, other than by means of closedir(), readdir(), readdir_r(), rewinddir(), or seekdir(), the behavior is undefined. Upon calling closedir() the file descriptor shall be closed.

There are ways around this by making the function unsafe, by attempting to reopen the file descriptor (which can fail) or by using getdents-style syscalls on each platform. Each of those approaches has downsides.
Or perhaps we could ask the standards body to clarify why this is UB. If it were merely implementation-defined behavior we could shrug and let people get the file descriptor at the risk of getting garbage when using the descriptor for multiple concurrent directory iterations.

Anyway, some design work is needed.

Some prior discussion
https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/Path.20reform.202022
https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/ReadDir's.20fd

added
C-enhancementCategory: An issue proposing an enhancement or a PR with one.
T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.
A-ioArea: `std::io`, `std::fs`, `std::net` and `std::path`
on Apr 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ioArea: `std::io`, `std::fs`, `std::net` and `std::path`C-enhancementCategory: An issue proposing an enhancement or a PR with one.F-wasi_ext`#[feature(wasi_ext)]`T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @the8472@alyssais@ChrisDenton@lolbinarycat@jyn514

        Issue actions

          Make (most?) `std::os::wasi::fs::FileExt` functions available on all Unix platforms · Issue #102776 · rust-lang/rust