rusl/platform/compat/
stat.rs

1use crate::platform::numbers::NonNegativeI32;
2use crate::platform::TimeSpec;
3
4pub type Stat = linux_rust_bindings::stat::stat;
5
6#[repr(transparent)]
7#[derive(Debug)]
8pub struct Statx(pub(crate) linux_rust_bindings::stat::statx);
9
10impl Statx {
11    #[inline]
12    #[must_use]
13    pub const fn mask(&self) -> StatxMask {
14        StatxMask(self.0.stx_mask)
15    }
16
17    #[inline]
18    #[must_use]
19    pub const fn size(&self) -> u64 {
20        self.0.stx_size
21    }
22
23    /// Last access
24    #[inline]
25    #[must_use]
26    pub const fn access_time(&self) -> TimeSpec {
27        let ts = self.0.stx_atime;
28        TimeSpec::new(ts.tv_sec, ts.tv_nsec as i64)
29    }
30
31    /// Creation
32    #[inline]
33    #[must_use]
34    pub const fn birth_time(&self) -> TimeSpec {
35        let ts = self.0.stx_btime;
36        TimeSpec::new(ts.tv_sec, ts.tv_nsec as i64)
37    }
38
39    /// Content modification
40    #[inline]
41    #[must_use]
42    pub const fn modified_time(&self) -> TimeSpec {
43        let ts = self.0.stx_mtime;
44        TimeSpec::new(ts.tv_sec, ts.tv_nsec as i64)
45    }
46
47    /// Metadata modification
48    #[inline]
49    #[must_use]
50    pub const fn changed_time(&self) -> TimeSpec {
51        let ts = self.0.stx_ctime;
52        TimeSpec::new(ts.tv_sec, ts.tv_nsec as i64)
53    }
54}
55
56transparent_bitflags! {
57    pub struct StatxMask: u32 {
58        const DEFAULT = 0;
59        const STATX_TYPE = linux_rust_bindings::stat::STATX_TYPE as u32;
60        const STATX_MODE = linux_rust_bindings::stat::STATX_MODE as u32;
61        const STATX_NLINK = linux_rust_bindings::stat::STATX_NLINK as u32;
62        const STATX_UID = linux_rust_bindings::stat::STATX_UID as u32;
63        const STATX_GID = linux_rust_bindings::stat::STATX_GID as u32;
64        const STATX_ATIME = linux_rust_bindings::stat::STATX_ATIME as u32;
65        const STATX_MTIME = linux_rust_bindings::stat::STATX_MTIME as u32;
66        const STATX_CTIME = linux_rust_bindings::stat::STATX_CTIME as u32;
67        const STATX_INO = linux_rust_bindings::stat::STATX_INO as u32;
68        const STATX_SIZE = linux_rust_bindings::stat::STATX_SIZE as u32;
69        const STATX_BLOCKS = linux_rust_bindings::stat::STATX_BLOCKS as u32;
70        const STATX_BASIC_STATS = linux_rust_bindings::stat::STATX_BASIC_STATS as u32;
71        const STATX_BTIME = linux_rust_bindings::stat::STATX_BTIME as u32;
72        const STATX_MNT_ID = linux_rust_bindings::stat::STATX_MNT_ID as u32;
73        const STATX_DIOALIGN = linux_rust_bindings::stat::STATX_DIOALIGN as u32;
74    }
75}
76
77transparent_bitflags! {
78    pub struct StatxFlags: NonNegativeI32 {
79        const DEFAULT = NonNegativeI32::comptime_checked_new(0);
80        const AT_SYMLINK_FOLLOW = NonNegativeI32::comptime_checked_new(linux_rust_bindings::fcntl::AT_SYMLINK_FOLLOW);
81        const AT_NO_AUTOMOUNT = NonNegativeI32::comptime_checked_new(linux_rust_bindings::fcntl::AT_NO_AUTOMOUNT);
82        const AT_EMPTY_PATH = NonNegativeI32::comptime_checked_new(linux_rust_bindings::fcntl::AT_EMPTY_PATH);
83        const AT_STATX_SYNC_TYPE = NonNegativeI32::comptime_checked_new(linux_rust_bindings::fcntl::AT_STATX_SYNC_TYPE);
84        const AT_STATX_SYNC_AS_STAT = NonNegativeI32::comptime_checked_new(linux_rust_bindings::fcntl::AT_STATX_SYNC_AS_STAT);
85        const AT_STATX_FORCE_SYNC = NonNegativeI32::comptime_checked_new(linux_rust_bindings::fcntl::AT_STATX_FORCE_SYNC);
86        const AT_STATX_DONT_SYNC = NonNegativeI32::comptime_checked_new(linux_rust_bindings::fcntl::AT_STATX_DONT_SYNC);
87    }
88}
89
90/// Mode accepted by the [open syscall](https://man7.org/linux/man-pages/man2/open.2.html)
91transparent_bitflags! {
92    pub struct Mode: u32 {
93        const DEFAULT = 0;
94        const S_IRWXU = linux_rust_bindings::stat::S_IRWXU as u32; // 00700 user read write exec
95        const S_IRUSR = linux_rust_bindings::stat::S_IRUSR as u32; // 00400 user Read
96        const S_IWUSR = linux_rust_bindings::stat::S_IWUSR as u32; // 00200 user write
97        const S_IXUSR = linux_rust_bindings::stat::S_IXUSR as u32;  // 00100 user execute
98        const S_IRWXG = linux_rust_bindings::stat::S_IRWXG as u32;  // 00070 group read write exec
99        const S_IRGRP = linux_rust_bindings::stat::S_IRGRP as u32;  // 00040 group read
100        const S_IWGRP = linux_rust_bindings::stat::S_IWGRP as u32;  // 00020 group write
101        const S_IXGRP = linux_rust_bindings::stat::S_IXGRP as u32;   // 00010 group exec
102        const S_IRWXO = linux_rust_bindings::stat::S_IRWXO as u32;   // 00007 other read write exec
103        const S_IROTH = linux_rust_bindings::stat::S_IROTH as u32;   // 00004 other read
104        const S_IWOTH = linux_rust_bindings::stat::S_IWOTH as u32;   // 00002 other write
105        const S_IXOTH = linux_rust_bindings::stat::S_IXOTH as u32;   // 00001 other execute
106
107        // Linux specific bits
108        const S_ISUID = linux_rust_bindings::stat::S_ISUID as u32; // 0004000 set-user-ID bit
109        const S_ISGID = linux_rust_bindings::stat::S_ISGID as u32; // 0002000 set-group-ID bit
110        const S_ISVTX = linux_rust_bindings::stat::S_ISVTX as u32; // 0001000 set-sticky bit
111
112        // File specific bits
113        const S_IFIFO  = linux_rust_bindings::stat::S_IFIFO as u32;
114        const S_IFCHR  = linux_rust_bindings::stat::S_IFCHR as u32;
115        const S_IFDIR  = linux_rust_bindings::stat::S_IFDIR as u32;
116        const S_IFBLK  = linux_rust_bindings::stat::S_IFBLK as u32;
117        const S_IFREG  = linux_rust_bindings::stat::S_IFREG as u32;
118        const S_IFLNK  = linux_rust_bindings::stat::S_IFLNK as u32;
119        const S_IFSOCK = linux_rust_bindings::stat::S_IFSOCK as u32;
120        const S_IFMT   = linux_rust_bindings::stat::S_IFMT as u32;
121    }
122}
123
124impl Mode {
125    pub const MODE_755: Mode = Mode(
126        Mode::S_IRWXU.0 | Mode::S_IRGRP.0 | Mode::S_IXGRP.0 | Mode::S_IXOTH.0 | Mode::S_IROTH.0,
127    );
128}
129
130impl From<u32> for Mode {
131    #[inline]
132    fn from(value: u32) -> Self {
133        Mode(value)
134    }
135}