-
Notifications
You must be signed in to change notification settings - Fork 13.3k
dirfd: initial quick and dirty implementation for unix #139514
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
Open
Qelxiros
wants to merge
2
commits into
rust-lang:master
Choose a base branch
from
Qelxiros:120426-dirfd
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+233
−78
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,7 +54,7 @@ use libc::{c_int, mode_t}; | |
#[cfg(target_os = "android")] | ||
use libc::{ | ||
dirent as dirent64, fstat as fstat64, fstatat as fstatat64, ftruncate64, lseek64, | ||
lstat as lstat64, off64_t, open as open64, stat as stat64, | ||
lstat as lstat64, off64_t, open as open64, openat as openat64, stat as stat64, | ||
}; | ||
#[cfg(not(any( | ||
all(target_os = "linux", not(target_env = "musl")), | ||
|
@@ -64,14 +64,14 @@ use libc::{ | |
)))] | ||
use libc::{ | ||
dirent as dirent64, fstat as fstat64, ftruncate as ftruncate64, lseek as lseek64, | ||
lstat as lstat64, off_t as off64_t, open as open64, stat as stat64, | ||
lstat as lstat64, off_t as off64_t, open as open64, openat as openat64, stat as stat64, | ||
}; | ||
#[cfg(any( | ||
all(target_os = "linux", not(target_env = "musl")), | ||
target_os = "l4re", | ||
target_os = "hurd" | ||
))] | ||
use libc::{dirent64, fstat64, ftruncate64, lseek64, lstat64, off64_t, open64, stat64}; | ||
use libc::{dirent64, fstat64, ftruncate64, lseek64, lstat64, off64_t, open64, openat64, stat64}; | ||
|
||
use crate::ffi::{CStr, OsStr, OsString}; | ||
use crate::fmt::{self, Write as _}; | ||
|
@@ -264,7 +264,183 @@ impl ReadDir { | |
} | ||
} | ||
|
||
struct Dir(*mut libc::DIR); | ||
pub struct Dir(*mut libc::DIR); | ||
|
||
// dirfd isn't supported everywhere | ||
#[cfg(not(any( | ||
miri, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we handle such missing platforms by returning errors instead of not having the methods exist at all |
||
target_os = "redox", | ||
target_os = "nto", | ||
target_os = "vita", | ||
target_os = "hurd", | ||
target_os = "espidf", | ||
target_os = "horizon", | ||
target_os = "vxworks", | ||
target_os = "rtems", | ||
target_os = "nuttx", | ||
)))] | ||
impl Dir { | ||
pub fn open<P: AsRef<Path>>(&self, path: P) -> io::Result<File> { | ||
let mut opts = OpenOptions::new(); | ||
opts.read(true); | ||
run_path_with_cstr(path.as_ref(), &|path| self.open_c(path, &opts)) | ||
} | ||
|
||
pub fn open_with<P: AsRef<Path>>(&self, path: P, opts: &OpenOptions) -> io::Result<File> { | ||
run_path_with_cstr(path.as_ref(), &|path| self.open_c(path, opts)) | ||
} | ||
|
||
pub fn open_c(&self, path: &CStr, opts: &OpenOptions) -> io::Result<File> { | ||
let flags = libc::O_CLOEXEC | ||
| opts.get_access_mode()? | ||
| opts.get_creation_mode()? | ||
| (opts.custom_flags as c_int & !libc::O_ACCMODE); | ||
let fd = cvt_r(|| unsafe { | ||
openat64(libc::dirfd(self.0), path.as_ptr(), flags, opts.mode as c_int) | ||
})?; | ||
Ok(File(unsafe { FileDesc::from_raw_fd(fd) })) | ||
} | ||
} | ||
|
||
#[cfg(any( | ||
miri, | ||
target_os = "redox", | ||
target_os = "nto", | ||
target_os = "vita", | ||
target_os = "hurd", | ||
target_os = "espidf", | ||
target_os = "horizon", | ||
target_os = "vxworks", | ||
target_os = "rtems", | ||
target_os = "nuttx", | ||
))] | ||
impl Dir { | ||
pub fn open<P: AsRef<Path>>(&self, path: P) -> io::Result<File> { | ||
Err(io::const_error!( | ||
io::ErrorKind::Unsupported, | ||
"directory handles are not available for this platform", | ||
)) | ||
} | ||
|
||
pub fn open_with<P: AsRef<Path>>(&self, path: P, opts: &OpenOptions) -> io::Result<File> { | ||
Err(io::const_error!( | ||
io::ErrorKind::Unsupported, | ||
"directory handles are not available for this platform", | ||
)) | ||
} | ||
|
||
pub fn open_c(&self, path: &CStr, opts: &OpenOptions) -> io::Result<File> { | ||
Err(io::const_error!( | ||
io::ErrorKind::Unsupported, | ||
"directory handles are not available for this platform", | ||
)) | ||
} | ||
} | ||
|
||
fn get_path_from_fd(fd: c_int) -> Option<PathBuf> { | ||
#[cfg(any(target_os = "linux", target_os = "illumos", target_os = "solaris"))] | ||
fn get_path(fd: c_int) -> Option<PathBuf> { | ||
let mut p = PathBuf::from("/proc/self/fd"); | ||
p.push(&fd.to_string()); | ||
run_path_with_cstr(&p, &readlink).ok() | ||
} | ||
|
||
#[cfg(any(target_vendor = "apple", target_os = "netbsd"))] | ||
fn get_path(fd: c_int) -> Option<PathBuf> { | ||
// FIXME: The use of PATH_MAX is generally not encouraged, but it | ||
// is inevitable in this case because Apple targets and NetBSD define `fcntl` | ||
// with `F_GETPATH` in terms of `MAXPATHLEN`, and there are no | ||
// alternatives. If a better method is invented, it should be used | ||
// instead. | ||
let mut buf = vec![0; libc::PATH_MAX as usize]; | ||
let n = unsafe { libc::fcntl(fd, libc::F_GETPATH, buf.as_ptr()) }; | ||
if n == -1 { | ||
cfg_if::cfg_if! { | ||
if #[cfg(target_os = "netbsd")] { | ||
// fallback to procfs as last resort | ||
let mut p = PathBuf::from("/proc/self/fd"); | ||
p.push(&fd.to_string()); | ||
return run_path_with_cstr(&p, &readlink).ok() | ||
} else { | ||
return None; | ||
} | ||
} | ||
} | ||
let l = buf.iter().position(|&c| c == 0).unwrap(); | ||
buf.truncate(l as usize); | ||
buf.shrink_to_fit(); | ||
Some(PathBuf::from(OsString::from_vec(buf))) | ||
} | ||
|
||
#[cfg(target_os = "freebsd")] | ||
fn get_path(fd: c_int) -> Option<PathBuf> { | ||
let info = Box::<libc::kinfo_file>::new_zeroed(); | ||
let mut info = unsafe { info.assume_init() }; | ||
info.kf_structsize = size_of::<libc::kinfo_file>() as libc::c_int; | ||
let n = unsafe { libc::fcntl(fd, libc::F_KINFO, &mut *info) }; | ||
if n == -1 { | ||
return None; | ||
} | ||
let buf = unsafe { CStr::from_ptr(info.kf_path.as_mut_ptr()).to_bytes().to_vec() }; | ||
Some(PathBuf::from(OsString::from_vec(buf))) | ||
} | ||
|
||
#[cfg(target_os = "vxworks")] | ||
fn get_path(fd: c_int) -> Option<PathBuf> { | ||
let mut buf = vec![0; libc::PATH_MAX as usize]; | ||
let n = unsafe { libc::ioctl(fd, libc::FIOGETNAME, buf.as_ptr()) }; | ||
if n == -1 { | ||
return None; | ||
} | ||
let l = buf.iter().position(|&c| c == 0).unwrap(); | ||
buf.truncate(l as usize); | ||
Some(PathBuf::from(OsString::from_vec(buf))) | ||
} | ||
|
||
#[cfg(not(any( | ||
target_os = "linux", | ||
target_os = "vxworks", | ||
target_os = "freebsd", | ||
target_os = "netbsd", | ||
target_os = "illumos", | ||
target_os = "solaris", | ||
target_vendor = "apple", | ||
)))] | ||
fn get_path(_fd: c_int) -> Option<PathBuf> { | ||
// FIXME(#24570): implement this for other Unix platforms | ||
None | ||
} | ||
|
||
get_path(fd) | ||
} | ||
|
||
impl fmt::Debug for Dir { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
fn get_mode(fd: c_int) -> Option<(bool, bool)> { | ||
let mode = unsafe { libc::fcntl(fd, libc::F_GETFL) }; | ||
if mode == -1 { | ||
return None; | ||
} | ||
match mode & libc::O_ACCMODE { | ||
libc::O_RDONLY => Some((true, false)), | ||
libc::O_RDWR => Some((true, true)), | ||
libc::O_WRONLY => Some((false, true)), | ||
_ => None, | ||
} | ||
} | ||
|
||
let fd = unsafe { dirfd(self.0) }; | ||
let mut b = f.debug_struct("Dir"); | ||
b.field("fd", &fd); | ||
if let Some(path) = get_path_from_fd(fd) { | ||
b.field("path", &path); | ||
} | ||
if let Some((read, write)) = get_mode(fd) { | ||
b.field("read", &read).field("write", &write); | ||
} | ||
b.finish() | ||
} | ||
} | ||
|
||
unsafe impl Send for Dir {} | ||
unsafe impl Sync for Dir {} | ||
|
@@ -1653,79 +1829,6 @@ impl FromRawFd for File { | |
|
||
impl fmt::Debug for File { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
#[cfg(any(target_os = "linux", target_os = "illumos", target_os = "solaris"))] | ||
fn get_path(fd: c_int) -> Option<PathBuf> { | ||
let mut p = PathBuf::from("/proc/self/fd"); | ||
p.push(&fd.to_string()); | ||
run_path_with_cstr(&p, &readlink).ok() | ||
} | ||
|
||
#[cfg(any(target_vendor = "apple", target_os = "netbsd"))] | ||
fn get_path(fd: c_int) -> Option<PathBuf> { | ||
// FIXME: The use of PATH_MAX is generally not encouraged, but it | ||
// is inevitable in this case because Apple targets and NetBSD define `fcntl` | ||
// with `F_GETPATH` in terms of `MAXPATHLEN`, and there are no | ||
// alternatives. If a better method is invented, it should be used | ||
// instead. | ||
let mut buf = vec![0; libc::PATH_MAX as usize]; | ||
let n = unsafe { libc::fcntl(fd, libc::F_GETPATH, buf.as_ptr()) }; | ||
if n == -1 { | ||
cfg_if::cfg_if! { | ||
if #[cfg(target_os = "netbsd")] { | ||
// fallback to procfs as last resort | ||
let mut p = PathBuf::from("/proc/self/fd"); | ||
p.push(&fd.to_string()); | ||
return run_path_with_cstr(&p, &readlink).ok() | ||
} else { | ||
return None; | ||
} | ||
} | ||
} | ||
let l = buf.iter().position(|&c| c == 0).unwrap(); | ||
buf.truncate(l as usize); | ||
buf.shrink_to_fit(); | ||
Some(PathBuf::from(OsString::from_vec(buf))) | ||
} | ||
|
||
#[cfg(target_os = "freebsd")] | ||
fn get_path(fd: c_int) -> Option<PathBuf> { | ||
let info = Box::<libc::kinfo_file>::new_zeroed(); | ||
let mut info = unsafe { info.assume_init() }; | ||
info.kf_structsize = size_of::<libc::kinfo_file>() as libc::c_int; | ||
let n = unsafe { libc::fcntl(fd, libc::F_KINFO, &mut *info) }; | ||
if n == -1 { | ||
return None; | ||
} | ||
let buf = unsafe { CStr::from_ptr(info.kf_path.as_mut_ptr()).to_bytes().to_vec() }; | ||
Some(PathBuf::from(OsString::from_vec(buf))) | ||
} | ||
|
||
#[cfg(target_os = "vxworks")] | ||
fn get_path(fd: c_int) -> Option<PathBuf> { | ||
let mut buf = vec![0; libc::PATH_MAX as usize]; | ||
let n = unsafe { libc::ioctl(fd, libc::FIOGETNAME, buf.as_ptr()) }; | ||
if n == -1 { | ||
return None; | ||
} | ||
let l = buf.iter().position(|&c| c == 0).unwrap(); | ||
buf.truncate(l as usize); | ||
Some(PathBuf::from(OsString::from_vec(buf))) | ||
} | ||
|
||
#[cfg(not(any( | ||
target_os = "linux", | ||
target_os = "vxworks", | ||
target_os = "freebsd", | ||
target_os = "netbsd", | ||
target_os = "illumos", | ||
target_os = "solaris", | ||
target_vendor = "apple", | ||
)))] | ||
fn get_path(_fd: c_int) -> Option<PathBuf> { | ||
// FIXME(#24570): implement this for other Unix platforms | ||
None | ||
} | ||
|
||
fn get_mode(fd: c_int) -> Option<(bool, bool)> { | ||
let mode = unsafe { libc::fcntl(fd, libc::F_GETFL) }; | ||
if mode == -1 { | ||
|
@@ -1742,7 +1845,7 @@ impl fmt::Debug for File { | |
let fd = self.as_raw_fd(); | ||
let mut b = f.debug_struct("File"); | ||
b.field("fd", &fd); | ||
if let Some(path) = get_path(fd) { | ||
if let Some(path) = get_path_from_fd(fd) { | ||
b.field("path", &path); | ||
} | ||
if let Some((read, write)) = get_mode(fd) { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you elaborate the documentation here a bit? At least make it clear that this is a handle, and what happens on drop (similar to
File
).