diff --git a/src/backend/linux_raw/fs/types.rs b/src/backend/linux_raw/fs/types.rs index c46ed875c..aa4937ff9 100644 --- a/src/backend/linux_raw/fs/types.rs +++ b/src/backend/linux_raw/fs/types.rs @@ -1,6 +1,6 @@ -use crate::ffi; #[allow(unused_imports)] use crate::types::Padding; +use crate::{ffi, fs::Timespec}; use bitflags::bitflags; bitflags! { @@ -856,3 +856,44 @@ pub type FsWord = i64; /// `__fsword_t` #[cfg(target_arch = "s390x")] pub type FsWord = u32; + +impl Stat { + /// Returns the [`FileType`] in the [`Stat`] value. + #[inline] + pub const fn filetype(&self) -> FileType { + FileType::from_raw_mode(self.st_mode) + } + + /// Returns the [`Mode`] in the [`Stat`] value. + #[inline] + pub const fn mode(&self) -> Mode { + Mode::from_raw_mode(self.st_mode) + } + + /// Returns the access time in the [`Stat`] value. + #[inline] + pub const fn atime(&self) -> Timespec { + Timespec { + tv_sec: self.st_atime, + tv_nsec: self.st_atime_nsec as i64, + } + } + + /// Returns the modified time in the [`Stat`] value. + #[inline] + pub const fn mtime(&self) -> Timespec { + Timespec { + tv_sec: self.st_mtime, + tv_nsec: self.st_mtime_nsec as i64, + } + } + + /// Returns the change time in the [`Stat`] value. + #[inline] + pub const fn ctime(&self) -> Timespec { + Timespec { + tv_sec: self.st_ctime, + tv_nsec: self.st_ctime_nsec as i64, + } + } +} diff --git a/src/fs/statx.rs b/src/fs/statx.rs index b03cfac39..f8bdeee42 100644 --- a/src/fs/statx.rs +++ b/src/fs/statx.rs @@ -1,7 +1,7 @@ //! Linux `statx`. use crate::fd::AsFd; -use crate::fs::AtFlags; +use crate::fs::{AtFlags, FileType, Mode}; use crate::types::Padding; use crate::{backend, io, path}; use backend::c; @@ -212,6 +212,32 @@ pub fn statx( path.into_with_c_str(|path| _statx(dirfd.as_fd(), path, flags, mask)) } +impl Statx { + /// Returns the [`FileType`] if it is available in the [`Statx`] value. + /// + /// Returns `None` if [`StatxFlags::TYPE`] is not set in `stx_mask`. + #[inline] + pub const fn filetype(&self) -> Option { + if self.stx_mask & StatxFlags::TYPE.bits() != 0 { + Some(FileType::from_raw_mode(self.stx_mode as u32)) + } else { + None + } + } + + /// Returns the [`Mode`] if it is available in the [`Statx`] value. + /// + /// Returns `None` if [`StatxFlags::MODE`] is not set in `stx_mask`. + #[inline] + pub const fn mode(&self) -> Option { + if self.stx_mask & StatxFlags::MODE.bits() != 0 { + Some(Mode::from_raw_mode(self.stx_mode as u32)) + } else { + None + } + } +} + #[cfg(not(feature = "linux_4_11"))] mod compat { use crate::fd::BorrowedFd;