Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion src/backend/linux_raw/fs/types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::ffi;
#[allow(unused_imports)]
use crate::types::Padding;
use crate::{ffi, fs::Timespec};
use bitflags::bitflags;

bitflags! {
Expand Down Expand Up @@ -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,
}
}
}
28 changes: 27 additions & 1 deletion src/fs/statx.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -212,6 +212,32 @@ pub fn statx<P: path::Arg, Fd: AsFd>(
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<FileType> {
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<Mode> {
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;
Expand Down
Loading