Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement AsRawFd and FromRawFd for ReadDir #56777

Open
hvenev opened this issue Dec 13, 2018 · 4 comments
Open

Implement AsRawFd and FromRawFd for ReadDir #56777

hvenev opened this issue Dec 13, 2018 · 4 comments
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@hvenev
Copy link
Contributor

hvenev commented Dec 13, 2018

Most unixes provide dirfd(), which can be used to get a file descriptor from struct DIR *.

The two errors are EINVAL (dirfd called on invalid pointer), which can't occur in correct code, and ENOTSUP (not supported by implementation), which doesn't occur on most implementations.

Most unixes also provide fdopendir(), which opens a file descriptor as struct DIR *. It fails if the file descriptor is invalid or if it fails to allocate memory.

@csmoe csmoe added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. C-feature-request Category: A feature request, i.e: not implemented / a PR. labels Dec 13, 2018
@zopsicle
Copy link
Contributor

zopsicle commented Jun 6, 2021

ReadDir internally stores the path that was passed to read_dir, so that it can be prepended to the file name when calling DirEntry::path. This means a potential FromRawFd impl would not work, as the path is not available.

So we would need to find a workaround for this, such as defining a method that takes the path instead of implementing FromRawFd, or we could document some caveat on DirEntry::path that it won’t work if the ReadDir was created using FromRawFd (e.g. have it panic).

(Note that DirEntry::metadata should work fine without the directory’s path, as it already uses dirfd and fstatat.)

@the8472
Copy link
Member

the8472 commented Oct 8, 2021

We could try recovering the path by doing a readlink on /proc/self/fd/<fd>, but that's not 100% reliable since procfs may not be available or the path can be too long for readlink. A second fallback would be just putting inthe proc path, as long as it exists. And as last resort we could panic.
But it might be a bit unexpected though if FromRawFd does syscalls or panics.

In addition to FromRawFd we could also provide a constructor method that takes a Path and a file descriptor.

@zopsicle
Copy link
Contributor

With I/O safety being stabilized this issue should also include implementing AsFd, From<OwnedFd>, and From<ReadDir> for OwnedFd.

@the8472
Copy link
Member

the8472 commented Jan 27, 2024

I don't think we can provide AsFd due to the API requirements of fdopendir(). After #120426 we may be able to provide it on some platforms, but it won't be fully portable so we either have to hide it behind cfgs or make the API fallible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants