-
-
Notifications
You must be signed in to change notification settings - Fork 15k
Implement Metadata:from_statx for Linux MetadataExt trait
#156269
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,24 @@ | |
| use crate::fs::Metadata; | ||
| #[allow(deprecated)] | ||
| use crate::os::linux::raw; | ||
| use crate::os::raw::{c_uint, c_void}; | ||
| use crate::sys::AsInner; | ||
| use crate::sys::fs::cfg_has_statx; | ||
| cfg_has_statx! {{ | ||
| use crate::sys::fs::FileAttr; | ||
| use crate::sys::FromInner; | ||
| } else { | ||
| use crate::sys::unsupported; | ||
| }} | ||
|
|
||
| /// This is the [`statx`] mask expected by [`Metadata::from_statx`], which sets both | ||
| /// `STATX_BASIC_STATS` and `STATX_BTIME`. See the [Linux man page] for statx for more | ||
| /// details. | ||
| /// | ||
| /// [`statx`]: https://docs.rs/libc/latest/libc/struct.statx.html | ||
| /// [Linux man page]: https://man7.org/linux/man-pages/man2/statx.2.html | ||
| #[unstable(feature = "metadata_statx", issue = "156268")] | ||
| pub const STATX_MASK: c_uint = libc::STATX_BASIC_STATS | libc::STATX_BTIME; | ||
|
asder8215 marked this conversation as resolved.
|
||
|
|
||
| /// OS-specific extensions to [`fs::Metadata`]. | ||
| /// | ||
|
|
@@ -41,6 +58,53 @@ pub trait MetadataExt { | |
| #[allow(deprecated)] | ||
| fn as_raw_stat(&self) -> &raw::stat; | ||
|
|
||
| /// Creates a [`Metadata`] from a const void pointer populated by the [`statx`] syscall. | ||
| /// | ||
| /// Currently [`Metadata::from_statx`] is only supported on Linux platforms with a target | ||
| /// environment of GNU. | ||
|
Comment on lines
+63
to
+64
Member
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. I was planning to wait for #154981 before implementing this to avoid this clause.
Contributor
Author
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. Got you. I think the order doesn't matter right now since this function would automatically have |
||
| /// | ||
| /// # Safety | ||
| /// | ||
| /// The caller must take care to provide a valid const void pointer containing information | ||
| /// populated by the [`statx`] syscall. In particular, the provided pointer should contain | ||
| /// statx information pertaining to the mask [`STATX_MASK`], so that there will be no | ||
| /// uninitialized data encountered in constructing [`Metadata`]. | ||
|
Mark-Simulacrum marked this conversation as resolved.
|
||
| /// | ||
| /// Note that the relevant information is copied out of the structure and the pointer is | ||
| /// not retained past the call. | ||
| /// | ||
| /// [`Metadata`]: crate::fs::Metadata | ||
| /// [`statx`]: https://docs.rs/libc/latest/libc/struct.statx.html | ||
| /// | ||
| /// ```no_run | ||
| /// #![feature(metadata_statx)] | ||
| /// use libc::statx; | ||
| /// use std::ffi::c_void; | ||
| /// use std::fs::{write, Metadata}; | ||
| /// use std::io; | ||
| /// use std::os::linux::fs::{MetadataExt, STATX_MASK}; | ||
| /// | ||
| /// fn main() -> io::Result<()> { | ||
| /// write("hello.txt", "Hello World!")?; | ||
| /// let mut buf = Box::<statx>::new_uninit(); | ||
| /// unsafe { | ||
| /// libc::statx( | ||
| /// libc::AT_FDCWD, | ||
| /// "hello.txt".as_ptr().cast(), | ||
| /// libc::AT_STATX_SYNC_AS_STAT, | ||
| /// STATX_MASK, | ||
| /// buf.as_mut_ptr().cast() | ||
| /// ); | ||
| /// } | ||
| /// let statxbuf: Box<statx> = unsafe { buf.assume_init() }; | ||
| /// let metadata = unsafe { Metadata::from_statx(&*statxbuf as *const statx as *const c_void) }; | ||
| /// assert_eq!(metadata.len(), 12); // "Hello World!" is 12 bytes | ||
| /// Ok(()) | ||
| /// } | ||
| /// ``` | ||
| #[unstable(feature = "metadata_statx", issue = "156268")] | ||
| unsafe fn from_statx(statxbuf: *const c_void) -> Self; | ||
|
|
||
| /// Returns the device ID on which this file resides. | ||
| /// | ||
| /// # Examples | ||
|
|
@@ -337,6 +401,14 @@ impl MetadataExt for Metadata { | |
| &*(self.as_inner().as_inner() as *const libc::stat64 as *const raw::stat) | ||
| } | ||
| } | ||
| cfg_has_statx! {{ | ||
| unsafe fn from_statx(statxbuf: *const c_void) -> Metadata { | ||
| Metadata::from_inner(FileAttr::from_statx(*(statxbuf as *const libc::statx))) | ||
| }} else { | ||
| unsafe fn from_statx(statxbuf: *const c_void) -> Self { | ||
| unsupported(); | ||
| } | ||
| }} | ||
| fn st_dev(&self) -> u64 { | ||
| self.as_inner().as_inner().st_dev as u64 | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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.
Hm, so it seems like we can't really extend this mask in the future if we want more information unless it's available on all Linux versions we support. E.g., if we for some reason wanted to pass
STATX_MNT_ID, we couldn't add it here because the caller may not have allocated enough space for it in theirstatxthey pass to the kernel.I see that the man page on my system at least says that:
I'm wondering:
from_statxif we didn't in fact get what we asked for?View changes since the review
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.
On the ACP I suggested providing an opaque statx type in the stdlib so that the stdlib can tell the library what size (and alignment) to use for the pointer.
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.
Also from what Josh said in the ACP:
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.
I think I'm OK landing this as-is if we add unresolved questions to the ACP, but I think we shouldn't stabilize this without a plan for extending this constant. AFAICT, right now we can't really do so (per my previous comment).
I'll nominate for libs-api for now.