1 //! Linux-specific extensions to primitives in the `std::fs` module.
3 #![stable(feature = "metadata_ext", since = "1.1.0")]
5 use crate::fs
::Metadata
;
6 use crate::sys_common
::AsInner
;
9 use crate::os
::linux
::raw
;
11 /// OS-specific extensions to [`fs::Metadata`].
13 /// [`fs::Metadata`]: crate::fs::Metadata
14 #[stable(feature = "metadata_ext", since = "1.1.0")]
15 pub trait MetadataExt
{
16 /// Gain a reference to the underlying `stat` structure which contains
17 /// the raw information returned by the OS.
19 /// The contents of the returned [`stat`] are **not** consistent across
20 /// Unix platforms. The `os::unix::fs::MetadataExt` trait contains the
21 /// cross-Unix abstractions contained within the raw stat.
23 /// [`stat`]: struct@crate::os::linux::raw::stat
30 /// use std::os::linux::fs::MetadataExt;
32 /// fn main() -> io::Result<()> {
33 /// let meta = fs::metadata("some_file")?;
34 /// let stat = meta.as_raw_stat();
38 #[stable(feature = "metadata_ext", since = "1.1.0")]
39 #[rustc_deprecated(since = "1.8.0", reason = "other methods of this trait are now preferred")]
41 fn as_raw_stat(&self) -> &raw
::stat
;
43 /// Returns the device ID on which this file resides.
50 /// use std::os::linux::fs::MetadataExt;
52 /// fn main() -> io::Result<()> {
53 /// let meta = fs::metadata("some_file")?;
54 /// println!("{}", meta.st_dev());
58 #[stable(feature = "metadata_ext2", since = "1.8.0")]
59 fn st_dev(&self) -> u64;
60 /// Returns the inode number.
67 /// use std::os::linux::fs::MetadataExt;
69 /// fn main() -> io::Result<()> {
70 /// let meta = fs::metadata("some_file")?;
71 /// println!("{}", meta.st_ino());
75 #[stable(feature = "metadata_ext2", since = "1.8.0")]
76 fn st_ino(&self) -> u64;
77 /// Returns the file type and mode.
84 /// use std::os::linux::fs::MetadataExt;
86 /// fn main() -> io::Result<()> {
87 /// let meta = fs::metadata("some_file")?;
88 /// println!("{}", meta.st_mode());
92 #[stable(feature = "metadata_ext2", since = "1.8.0")]
93 fn st_mode(&self) -> u32;
94 /// Returns the number of hard links to file.
101 /// use std::os::linux::fs::MetadataExt;
103 /// fn main() -> io::Result<()> {
104 /// let meta = fs::metadata("some_file")?;
105 /// println!("{}", meta.st_nlink());
109 #[stable(feature = "metadata_ext2", since = "1.8.0")]
110 fn st_nlink(&self) -> u64;
111 /// Returns the user ID of the file owner.
118 /// use std::os::linux::fs::MetadataExt;
120 /// fn main() -> io::Result<()> {
121 /// let meta = fs::metadata("some_file")?;
122 /// println!("{}", meta.st_uid());
126 #[stable(feature = "metadata_ext2", since = "1.8.0")]
127 fn st_uid(&self) -> u32;
128 /// Returns the group ID of the file owner.
135 /// use std::os::linux::fs::MetadataExt;
137 /// fn main() -> io::Result<()> {
138 /// let meta = fs::metadata("some_file")?;
139 /// println!("{}", meta.st_gid());
143 #[stable(feature = "metadata_ext2", since = "1.8.0")]
144 fn st_gid(&self) -> u32;
145 /// Returns the device ID that this file represents. Only relevant for special file.
152 /// use std::os::linux::fs::MetadataExt;
154 /// fn main() -> io::Result<()> {
155 /// let meta = fs::metadata("some_file")?;
156 /// println!("{}", meta.st_rdev());
160 #[stable(feature = "metadata_ext2", since = "1.8.0")]
161 fn st_rdev(&self) -> u64;
162 /// Returns the size of the file (if it is a regular file or a symbolic link) in bytes.
164 /// The size of a symbolic link is the length of the pathname it contains,
165 /// without a terminating null byte.
172 /// use std::os::linux::fs::MetadataExt;
174 /// fn main() -> io::Result<()> {
175 /// let meta = fs::metadata("some_file")?;
176 /// println!("{}", meta.st_size());
180 #[stable(feature = "metadata_ext2", since = "1.8.0")]
181 fn st_size(&self) -> u64;
182 /// Returns the last access time of the file, in seconds since Unix Epoch.
189 /// use std::os::linux::fs::MetadataExt;
191 /// fn main() -> io::Result<()> {
192 /// let meta = fs::metadata("some_file")?;
193 /// println!("{}", meta.st_atime());
197 #[stable(feature = "metadata_ext2", since = "1.8.0")]
198 fn st_atime(&self) -> i64;
199 /// Returns the last access time of the file, in nanoseconds since [`st_atime`].
201 /// [`st_atime`]: Self::st_atime
208 /// use std::os::linux::fs::MetadataExt;
210 /// fn main() -> io::Result<()> {
211 /// let meta = fs::metadata("some_file")?;
212 /// println!("{}", meta.st_atime_nsec());
216 #[stable(feature = "metadata_ext2", since = "1.8.0")]
217 fn st_atime_nsec(&self) -> i64;
218 /// Returns the last modification time of the file, in seconds since Unix Epoch.
225 /// use std::os::linux::fs::MetadataExt;
227 /// fn main() -> io::Result<()> {
228 /// let meta = fs::metadata("some_file")?;
229 /// println!("{}", meta.st_mtime());
233 #[stable(feature = "metadata_ext2", since = "1.8.0")]
234 fn st_mtime(&self) -> i64;
235 /// Returns the last modification time of the file, in nanoseconds since [`st_mtime`].
237 /// [`st_mtime`]: Self::st_mtime
244 /// use std::os::linux::fs::MetadataExt;
246 /// fn main() -> io::Result<()> {
247 /// let meta = fs::metadata("some_file")?;
248 /// println!("{}", meta.st_mtime_nsec());
252 #[stable(feature = "metadata_ext2", since = "1.8.0")]
253 fn st_mtime_nsec(&self) -> i64;
254 /// Returns the last status change time of the file, in seconds since Unix Epoch.
261 /// use std::os::linux::fs::MetadataExt;
263 /// fn main() -> io::Result<()> {
264 /// let meta = fs::metadata("some_file")?;
265 /// println!("{}", meta.st_ctime());
269 #[stable(feature = "metadata_ext2", since = "1.8.0")]
270 fn st_ctime(&self) -> i64;
271 /// Returns the last status change time of the file, in nanoseconds since [`st_ctime`].
273 /// [`st_ctime`]: Self::st_ctime
280 /// use std::os::linux::fs::MetadataExt;
282 /// fn main() -> io::Result<()> {
283 /// let meta = fs::metadata("some_file")?;
284 /// println!("{}", meta.st_ctime_nsec());
288 #[stable(feature = "metadata_ext2", since = "1.8.0")]
289 fn st_ctime_nsec(&self) -> i64;
290 /// Returns the "preferred" block size for efficient filesystem I/O.
297 /// use std::os::linux::fs::MetadataExt;
299 /// fn main() -> io::Result<()> {
300 /// let meta = fs::metadata("some_file")?;
301 /// println!("{}", meta.st_blksize());
305 #[stable(feature = "metadata_ext2", since = "1.8.0")]
306 fn st_blksize(&self) -> u64;
307 /// Returns the number of blocks allocated to the file, 512-byte units.
314 /// use std::os::linux::fs::MetadataExt;
316 /// fn main() -> io::Result<()> {
317 /// let meta = fs::metadata("some_file")?;
318 /// println!("{}", meta.st_blocks());
322 #[stable(feature = "metadata_ext2", since = "1.8.0")]
323 fn st_blocks(&self) -> u64;
326 #[stable(feature = "metadata_ext", since = "1.1.0")]
327 impl MetadataExt
for Metadata
{
329 fn as_raw_stat(&self) -> &raw
::stat
{
330 unsafe { &*(self.as_inner().as_inner() as *const libc::stat64 as *const raw::stat) }
332 fn st_dev(&self) -> u64 {
333 self.as_inner().as_inner().st_dev
as u64
335 fn st_ino(&self) -> u64 {
336 self.as_inner().as_inner().st_ino
as u64
338 fn st_mode(&self) -> u32 {
339 self.as_inner().as_inner().st_mode
as u32
341 fn st_nlink(&self) -> u64 {
342 self.as_inner().as_inner().st_nlink
as u64
344 fn st_uid(&self) -> u32 {
345 self.as_inner().as_inner().st_uid
as u32
347 fn st_gid(&self) -> u32 {
348 self.as_inner().as_inner().st_gid
as u32
350 fn st_rdev(&self) -> u64 {
351 self.as_inner().as_inner().st_rdev
as u64
353 fn st_size(&self) -> u64 {
354 self.as_inner().as_inner().st_size
as u64
356 fn st_atime(&self) -> i64 {
357 self.as_inner().as_inner().st_atime
as i64
359 fn st_atime_nsec(&self) -> i64 {
360 self.as_inner().as_inner().st_atime_nsec
as i64
362 fn st_mtime(&self) -> i64 {
363 self.as_inner().as_inner().st_mtime
as i64
365 fn st_mtime_nsec(&self) -> i64 {
366 self.as_inner().as_inner().st_mtime_nsec
as i64
368 fn st_ctime(&self) -> i64 {
369 self.as_inner().as_inner().st_ctime
as i64
371 fn st_ctime_nsec(&self) -> i64 {
372 self.as_inner().as_inner().st_ctime_nsec
as i64
374 fn st_blksize(&self) -> u64 {
375 self.as_inner().as_inner().st_blksize
as u64
377 fn st_blocks(&self) -> u64 {
378 self.as_inner().as_inner().st_blocks
as u64