1use crate::fd::AsFd;
4use crate::fs::AtFlags;
5use crate::{backend, io, path};
6use backend::c;
7use bitflags::bitflags;
8
9#[cfg(feature = "linux_4_11")]
10use backend::fs::syscalls::statx as _statx;
11#[cfg(not(feature = "linux_4_11"))]
12use compat::statx as _statx;
13
14#[repr(C)]
16#[derive(Debug, Copy, Clone)]
17#[allow(missing_docs)]
18#[non_exhaustive]
19pub struct Statx {
20 pub stx_mask: u32,
21 pub stx_blksize: u32,
22 pub stx_attributes: StatxAttributes,
23 pub stx_nlink: u32,
24 pub stx_uid: u32,
25 pub stx_gid: u32,
26 pub stx_mode: u16,
27 pub(crate) __spare0: [u16; 1],
28 pub stx_ino: u64,
29 pub stx_size: u64,
30 pub stx_blocks: u64,
31 pub stx_attributes_mask: StatxAttributes,
32 pub stx_atime: StatxTimestamp,
33 pub stx_btime: StatxTimestamp,
34 pub stx_ctime: StatxTimestamp,
35 pub stx_mtime: StatxTimestamp,
36 pub stx_rdev_major: u32,
37 pub stx_rdev_minor: u32,
38 pub stx_dev_major: u32,
39 pub stx_dev_minor: u32,
40 pub stx_mnt_id: u64,
41 pub stx_dio_mem_align: u32,
42 pub stx_dio_offset_align: u32,
43 pub stx_subvol: u64,
44 pub stx_atomic_write_unit_min: u32,
45 pub stx_atomic_write_unit_max: u32,
46 pub stx_atomic_write_segments_max: u32,
47 pub stx_dio_read_offset_align: u32,
48 pub stx_atomic_write_unit_max_opt: u32,
49 pub __spare2: [u32; 1usize],
50 pub __spare3: [u64; 8usize],
51}
52
53#[repr(C)]
55#[derive(Debug, Copy, Clone)]
56#[non_exhaustive]
57pub struct StatxTimestamp {
58 pub tv_sec: i64,
60
61 pub tv_nsec: u32,
63
64 pub(crate) __reserved: i32,
65}
66
67bitflags! {
68 #[repr(transparent)]
72 #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
73 pub struct StatxFlags: u32 {
74 const TYPE = c::STATX_TYPE;
76
77 const MODE = c::STATX_MODE;
79
80 const NLINK = c::STATX_NLINK;
82
83 const UID = c::STATX_UID;
85
86 const GID = c::STATX_GID;
88
89 const ATIME = c::STATX_ATIME;
91
92 const MTIME = c::STATX_MTIME;
94
95 const CTIME = c::STATX_CTIME;
97
98 const INO = c::STATX_INO;
100
101 const SIZE = c::STATX_SIZE;
103
104 const BLOCKS = c::STATX_BLOCKS;
106
107 const BASIC_STATS = c::STATX_BASIC_STATS;
109
110 const BTIME = c::STATX_BTIME;
112
113 const MNT_ID = c::STATX_MNT_ID;
115
116 const DIOALIGN = c::STATX_DIOALIGN;
118
119 const ALL = c::STATX_ALL;
121
122 const _ = !0;
124 }
125}
126
127bitflags! {
128 #[repr(transparent)]
130 #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
131 pub struct StatxAttributes: u64 {
132 const COMPRESSED = c::STATX_ATTR_COMPRESSED as u64;
134
135 const IMMUTABLE = c::STATX_ATTR_IMMUTABLE as u64;
137
138 const APPEND = c::STATX_ATTR_APPEND as u64;
140
141 const NODUMP = c::STATX_ATTR_NODUMP as u64;
143
144 const ENCRYPTED = c::STATX_ATTR_ENCRYPTED as u64;
146
147 const AUTOMOUNT = c::STATX_ATTR_AUTOMOUNT as u64;
149
150 const MOUNT_ROOT = c::STATX_ATTR_MOUNT_ROOT as u64;
152
153 const VERITY = c::STATX_ATTR_VERITY as u64;
155
156 const DAX = c::STATX_ATTR_DAX as u64;
158
159 const _ = !0;
161 }
162}
163
164#[inline]
205pub fn statx<P: path::Arg, Fd: AsFd>(
206 dirfd: Fd,
207 path: P,
208 flags: AtFlags,
209 mask: StatxFlags,
210) -> io::Result<Statx> {
211 path.into_with_c_str(|path| _statx(dirfd.as_fd(), path, flags, mask))
212}
213
214#[cfg(not(feature = "linux_4_11"))]
215mod compat {
216 use crate::fd::BorrowedFd;
217 use crate::ffi::CStr;
218 use crate::fs::{AtFlags, Statx, StatxFlags};
219 use crate::{backend, io};
220 use core::sync::atomic::{AtomicU8, Ordering};
221
222 static STATX_STATE: AtomicU8 = AtomicU8::new(0);
230
231 #[inline]
232 pub fn statx(
233 dirfd: BorrowedFd<'_>,
234 path: &CStr,
235 flags: AtFlags,
236 mask: StatxFlags,
237 ) -> io::Result<Statx> {
238 match STATX_STATE.load(Ordering::Relaxed) {
239 0 => statx_init(dirfd, path, flags, mask),
240 1 => Err(io::Errno::NOSYS),
241 _ => backend::fs::syscalls::statx(dirfd, path, flags, mask),
242 }
243 }
244
245 fn statx_init(
247 dirfd: BorrowedFd<'_>,
248 path: &CStr,
249 flags: AtFlags,
250 mask: StatxFlags,
251 ) -> io::Result<Statx> {
252 match backend::fs::syscalls::statx(dirfd, path, flags, mask) {
253 Err(err) => statx_error(err),
254 result => {
255 STATX_STATE.store(2, Ordering::Relaxed);
256 result
257 }
258 }
259 }
260
261 #[cold]
265 fn statx_error(err: io::Errno) -> io::Result<Statx> {
266 if backend::fs::syscalls::is_statx_available() {
267 STATX_STATE.store(2, Ordering::Relaxed);
270 Err(err)
271 } else {
272 STATX_STATE.store(1, Ordering::Relaxed);
274 Err(io::Errno::NOSYS)
275 }
276 }
277}