rustix/backend/libc/fs/
types.rs

1use crate::backend::c;
2use crate::ffi;
3use bitflags::bitflags;
4
5#[cfg(not(any(target_os = "espidf", target_os = "horizon", target_os = "vita")))]
6bitflags! {
7    /// `*_OK` constants for use with [`accessat`].
8    ///
9    /// [`accessat`]: fn.accessat.html
10    #[repr(transparent)]
11    #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
12    pub struct Access: ffi::c_int {
13        /// `R_OK`
14        const READ_OK = c::R_OK;
15
16        /// `W_OK`
17        const WRITE_OK = c::W_OK;
18
19        /// `X_OK`
20        const EXEC_OK = c::X_OK;
21
22        /// `F_OK`
23        const EXISTS = c::F_OK;
24
25        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
26        const _ = !0;
27    }
28}
29
30#[cfg(not(any(target_os = "espidf", target_os = "horizon", target_os = "redox")))]
31bitflags! {
32    /// `AT_*` constants for use with [`openat`], [`statat`], and other `*at`
33    /// functions.
34    ///
35    /// [`openat`]: crate::fs::openat
36    /// [`statat`]: crate::fs::statat
37    #[repr(transparent)]
38    #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
39    pub struct AtFlags: u32 {
40        /// `AT_SYMLINK_NOFOLLOW`
41        const SYMLINK_NOFOLLOW = bitcast!(c::AT_SYMLINK_NOFOLLOW);
42
43        /// `AT_EACCESS`
44        #[cfg(not(target_os = "android"))]
45        const EACCESS = bitcast!(c::AT_EACCESS);
46
47        /// `AT_REMOVEDIR`
48        const REMOVEDIR = bitcast!(c::AT_REMOVEDIR);
49
50        /// `AT_SYMLINK_FOLLOW`
51        const SYMLINK_FOLLOW = bitcast!(c::AT_SYMLINK_FOLLOW);
52
53        /// `AT_NO_AUTOMOUNT`
54        #[cfg(any(linux_like, target_os = "fuchsia"))]
55        const NO_AUTOMOUNT = bitcast!(c::AT_NO_AUTOMOUNT);
56
57        /// `AT_EMPTY_PATH`
58        #[cfg(any(
59            linux_kernel,
60            target_os = "freebsd",
61            target_os = "fuchsia",
62        ))]
63        const EMPTY_PATH = bitcast!(c::AT_EMPTY_PATH);
64
65        /// `AT_RESOLVE_BENEATH`
66        #[cfg(target_os = "freebsd")]
67        const RESOLVE_BENEATH = bitcast!(c::AT_RESOLVE_BENEATH);
68
69        /// `AT_STATX_SYNC_AS_STAT`
70        #[cfg(all(target_os = "linux", target_env = "gnu"))]
71        const STATX_SYNC_AS_STAT = bitcast!(c::AT_STATX_SYNC_AS_STAT);
72
73        /// `AT_STATX_FORCE_SYNC`
74        #[cfg(all(target_os = "linux", target_env = "gnu"))]
75        const STATX_FORCE_SYNC = bitcast!(c::AT_STATX_FORCE_SYNC);
76
77        /// `AT_STATX_DONT_SYNC`
78        #[cfg(all(target_os = "linux", target_env = "gnu"))]
79        const STATX_DONT_SYNC = bitcast!(c::AT_STATX_DONT_SYNC);
80
81        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
82        const _ = !0;
83    }
84}
85
86#[cfg(target_os = "horizon")]
87bitflags! {
88    /// `AT_*` constants for use with [`openat`], [`statat`], and other `*at`
89    /// functions.
90    ///
91    /// [`openat`]: crate::fs::openat
92    /// [`statat`]: crate::fs::statat
93    #[repr(transparent)]
94    #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
95    pub struct AtFlags: u32 {
96        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
97        const _ = !0;
98    }
99}
100
101#[cfg(not(target_os = "horizon"))]
102bitflags! {
103    /// `S_I*` constants for use with [`openat`], [`chmodat`], and [`fchmod`].
104    ///
105    /// [`openat`]: crate::fs::openat
106    /// [`chmodat`]: crate::fs::chmodat
107    /// [`fchmod`]: crate::fs::fchmod
108    #[repr(transparent)]
109    #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
110    pub struct Mode: RawMode {
111        /// `S_IRWXU`
112        #[cfg(not(any(target_os = "espidf", target_os = "vita")))]
113        const RWXU = c::S_IRWXU as RawMode;
114
115        /// `S_IRUSR`
116        #[cfg(not(any(target_os = "espidf", target_os = "vita")))]
117        const RUSR = c::S_IRUSR as RawMode;
118
119        /// `S_IWUSR`
120        #[cfg(not(any(target_os = "espidf", target_os = "vita")))]
121        const WUSR = c::S_IWUSR as RawMode;
122
123        /// `S_IXUSR`
124        #[cfg(not(any(target_os = "espidf", target_os = "vita")))]
125        const XUSR = c::S_IXUSR as RawMode;
126
127        /// `S_IRWXG`
128        #[cfg(not(any(target_os = "espidf", target_os = "vita")))]
129        const RWXG = c::S_IRWXG as RawMode;
130
131        /// `S_IRGRP`
132        #[cfg(not(any(target_os = "espidf", target_os = "vita")))]
133        const RGRP = c::S_IRGRP as RawMode;
134
135        /// `S_IWGRP`
136        #[cfg(not(any(target_os = "espidf", target_os = "vita")))]
137        const WGRP = c::S_IWGRP as RawMode;
138
139        /// `S_IXGRP`
140        #[cfg(not(any(target_os = "espidf", target_os = "vita")))]
141        const XGRP = c::S_IXGRP as RawMode;
142
143        /// `S_IRWXO`
144        #[cfg(not(any(target_os = "espidf", target_os = "vita")))]
145        const RWXO = c::S_IRWXO as RawMode;
146
147        /// `S_IROTH`
148        #[cfg(not(any(target_os = "espidf", target_os = "vita")))]
149        const ROTH = c::S_IROTH as RawMode;
150
151        /// `S_IWOTH`
152        #[cfg(not(any(target_os = "espidf", target_os = "vita")))]
153        const WOTH = c::S_IWOTH as RawMode;
154
155        /// `S_IXOTH`
156        #[cfg(not(any(target_os = "espidf", target_os = "vita")))]
157        const XOTH = c::S_IXOTH as RawMode;
158
159        /// `S_ISUID`
160        #[cfg(not(any(target_os = "espidf", target_os = "vita")))]
161        const SUID = c::S_ISUID as RawMode;
162
163        /// `S_ISGID`
164        #[cfg(not(any(target_os = "espidf", target_os = "vita")))]
165        const SGID = c::S_ISGID as RawMode;
166
167        /// `S_ISVTX`
168        #[cfg(not(any(target_os = "espidf", target_os = "vita")))]
169        const SVTX = c::S_ISVTX as RawMode;
170
171        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
172        const _ = !0;
173    }
174}
175
176#[cfg(target_os = "horizon")]
177bitflags! {
178    /// `S_I*` constants for use with [`openat`], [`chmodat`], and [`fchmod`].
179    ///
180    /// [`openat`]: crate::fs::openat
181    /// [`chmodat`]: crate::fs::chmodat
182    /// [`fchmod`]: crate::fs::fchmod
183    #[repr(transparent)]
184    #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
185    pub struct Mode: RawMode {
186        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
187        const _ = !0;
188    }
189}
190
191#[cfg(not(target_os = "espidf"))]
192impl Mode {
193    /// Construct a `Mode` from the mode bits of the `st_mode` field of a
194    /// `Mode`.
195    #[inline]
196    pub const fn from_raw_mode(st_mode: RawMode) -> Self {
197        Self::from_bits_truncate(st_mode & !c::S_IFMT as RawMode)
198    }
199
200    /// Construct an `st_mode` value from a `Mode`.
201    #[inline]
202    pub const fn as_raw_mode(self) -> RawMode {
203        self.bits()
204    }
205}
206
207#[cfg(not(target_os = "espidf"))]
208impl From<RawMode> for Mode {
209    /// Support conversions from raw mode values to `Mode`.
210    ///
211    /// ```
212    /// use rustix::fs::{Mode, RawMode};
213    /// assert_eq!(Mode::from(0o700), Mode::RWXU);
214    /// ```
215    #[inline]
216    fn from(st_mode: RawMode) -> Self {
217        Self::from_raw_mode(st_mode)
218    }
219}
220
221#[cfg(not(target_os = "espidf"))]
222impl From<Mode> for RawMode {
223    /// Support conversions from `Mode` to raw mode values.
224    ///
225    /// ```
226    /// use rustix::fs::{Mode, RawMode};
227    /// assert_eq!(RawMode::from(Mode::RWXU), 0o700);
228    /// ```
229    #[inline]
230    fn from(mode: Mode) -> Self {
231        mode.as_raw_mode()
232    }
233}
234
235bitflags! {
236    /// `O_*` constants for use with [`openat`].
237    ///
238    /// [`openat`]: crate::fs::openat
239    #[repr(transparent)]
240    #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
241    pub struct OFlags: u32 {
242        /// `O_ACCMODE`
243        const ACCMODE = bitcast!(c::O_ACCMODE);
244
245        /// Similar to `ACCMODE`, but just includes the read/write flags, and
246        /// no other flags.
247        ///
248        /// On some platforms, `PATH` may be included in `ACCMODE`, when
249        /// sometimes we really just want the read/write bits. Caution is
250        /// indicated, as the presence of `PATH` may mean that the read/write
251        /// bits don't have their usual meaning.
252        const RWMODE = bitcast!(c::O_RDONLY | c::O_WRONLY | c::O_RDWR);
253
254        /// `O_APPEND`
255        const APPEND = bitcast!(c::O_APPEND);
256
257        /// `O_CREAT`
258        #[doc(alias = "CREAT")]
259        const CREATE = bitcast!(c::O_CREAT);
260
261        /// `O_DIRECTORY`
262        #[cfg(not(any(target_os = "espidf", target_os = "horizon")))]
263        const DIRECTORY = bitcast!(c::O_DIRECTORY);
264
265        /// `O_DSYNC`
266        #[cfg(not(any(target_os = "dragonfly", target_os = "espidf", target_os = "horizon", target_os = "l4re", target_os = "redox", target_os = "vita")))]
267        const DSYNC = bitcast!(c::O_DSYNC);
268
269        /// `O_EXCL`
270        const EXCL = bitcast!(c::O_EXCL);
271
272        /// `O_FSYNC`
273        #[cfg(any(
274            bsd,
275            all(target_os = "linux", not(target_env = "musl")),
276        ))]
277        const FSYNC = bitcast!(c::O_FSYNC);
278
279        /// `O_NOFOLLOW`
280        #[cfg(not(any(target_os = "espidf", target_os = "horizon")))]
281        const NOFOLLOW = bitcast!(c::O_NOFOLLOW);
282
283        /// `O_NONBLOCK`
284        const NONBLOCK = bitcast!(c::O_NONBLOCK);
285
286        /// `O_RDONLY`
287        const RDONLY = bitcast!(c::O_RDONLY);
288
289        /// `O_WRONLY`
290        const WRONLY = bitcast!(c::O_WRONLY);
291
292        /// `O_RDWR`
293        ///
294        /// This is not equal to `RDONLY | WRONLY`. It's a distinct flag.
295        const RDWR = bitcast!(c::O_RDWR);
296
297        /// `O_NOCTTY`
298        #[cfg(not(any(target_os = "espidf", target_os = "horizon", target_os = "l4re", target_os = "redox", target_os = "vita")))]
299        const NOCTTY = bitcast!(c::O_NOCTTY);
300
301        /// `O_RSYNC`
302        #[cfg(any(
303            linux_kernel,
304            netbsdlike,
305            solarish,
306            target_os = "emscripten",
307            target_os = "wasi",
308        ))]
309        const RSYNC = bitcast!(c::O_RSYNC);
310
311        /// `O_SYNC`
312        #[cfg(not(any(target_os = "l4re", target_os = "redox")))]
313        const SYNC = bitcast!(c::O_SYNC);
314
315        /// `O_TRUNC`
316        const TRUNC = bitcast!(c::O_TRUNC);
317
318        /// `O_PATH`
319        #[cfg(any(
320            linux_kernel,
321            target_os = "emscripten",
322            target_os = "freebsd",
323            target_os = "fuchsia",
324            target_os = "redox",
325        ))]
326        const PATH = bitcast!(c::O_PATH);
327
328        /// `O_CLOEXEC`
329        const CLOEXEC = bitcast!(c::O_CLOEXEC);
330
331        /// `O_TMPFILE`
332        #[cfg(any(
333            linux_kernel,
334            target_os = "emscripten",
335            target_os = "fuchsia",
336        ))]
337        const TMPFILE = bitcast!(c::O_TMPFILE);
338
339        /// `O_NOATIME`
340        #[cfg(any(
341            linux_kernel,
342            target_os = "fuchsia",
343        ))]
344        const NOATIME = bitcast!(c::O_NOATIME);
345
346        /// `O_DIRECT`
347        #[cfg(any(
348            linux_kernel,
349            target_os = "emscripten",
350            target_os = "freebsd",
351            target_os = "fuchsia",
352            target_os = "netbsd",
353        ))]
354        const DIRECT = bitcast!(c::O_DIRECT);
355
356        /// `O_RESOLVE_BENEATH`
357        #[cfg(target_os = "freebsd")]
358        const RESOLVE_BENEATH = bitcast!(c::O_RESOLVE_BENEATH);
359
360        /// `O_EMPTY_PATH`
361        #[cfg(target_os = "freebsd")]
362        const EMPTY_PATH = bitcast!(c::O_EMPTY_PATH);
363
364        /// `O_LARGEFILE`
365        ///
366        /// Rustix and/or libc will automatically set this flag when
367        /// appropriate in the [`rustix::fs::open`] family of functions, so
368        /// typical users do not need to care about it. It may be reported in
369        /// the return of `fcntl_getfl`, though.
370        #[cfg(any(linux_kernel, solarish))]
371        const LARGEFILE = bitcast!(c::O_LARGEFILE);
372
373        /// `O_ASYNC`, `FASYNC`
374        ///
375        /// This flag can't be used with the [`rustix::fs::open`] family of
376        /// functions, use [`rustix::fs::fcntl_setfl`] instead.
377        #[cfg(not(any(
378            target_os = "aix",
379            target_os = "cygwin",
380            target_os = "espidf",
381            target_os = "haiku",
382            target_os = "horizon",
383            target_os = "wasi",
384            target_os = "vita",
385            solarish
386        )))]
387        const ASYNC = bitcast!(c::O_ASYNC);
388
389        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
390        const _ = !0;
391    }
392}
393
394#[cfg(apple)]
395bitflags! {
396    /// `CLONE_*` constants for use with [`fclonefileat`].
397    ///
398    /// [`fclonefileat`]: crate::fs::fclonefileat
399    #[repr(transparent)]
400    #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
401    pub struct CloneFlags: u32 {
402        /// `CLONE_NOFOLLOW`
403        const NOFOLLOW = 1;
404
405        /// `CLONE_NOOWNERCOPY`
406        const NOOWNERCOPY = 2;
407
408        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
409        const _ = !0;
410    }
411}
412
413#[cfg(apple)]
414mod copyfile {
415    pub(super) const ACL: u32 = 1 << 0;
416    pub(super) const STAT: u32 = 1 << 1;
417    pub(super) const XATTR: u32 = 1 << 2;
418    pub(super) const DATA: u32 = 1 << 3;
419    pub(super) const SECURITY: u32 = STAT | ACL;
420    pub(super) const METADATA: u32 = SECURITY | XATTR;
421    pub(super) const ALL: u32 = METADATA | DATA;
422}
423
424#[cfg(apple)]
425bitflags! {
426    /// `COPYFILE_*` constants for use with [`fcopyfile`].
427    ///
428    /// [`fcopyfile`]: crate::fs::fcopyfile
429    #[repr(transparent)]
430    #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
431    pub struct CopyfileFlags: ffi::c_uint {
432        /// `COPYFILE_ACL`
433        const ACL = copyfile::ACL;
434
435        /// `COPYFILE_STAT`
436        const STAT = copyfile::STAT;
437
438        /// `COPYFILE_XATTR`
439        const XATTR = copyfile::XATTR;
440
441        /// `COPYFILE_DATA`
442        const DATA = copyfile::DATA;
443
444        /// `COPYFILE_SECURITY`
445        const SECURITY = copyfile::SECURITY;
446
447        /// `COPYFILE_METADATA`
448        const METADATA = copyfile::METADATA;
449
450        /// `COPYFILE_ALL`
451        const ALL = copyfile::ALL;
452
453        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
454        const _ = !0;
455    }
456}
457
458#[cfg(linux_kernel)]
459bitflags! {
460    /// `RESOLVE_*` constants for use with [`openat2`].
461    ///
462    /// [`openat2`]: crate::fs::openat2
463    #[repr(transparent)]
464    #[derive(Default, Copy, Clone, Eq, PartialEq, Hash, Debug)]
465    pub struct ResolveFlags: u64 {
466        /// `RESOLVE_NO_XDEV`
467        const NO_XDEV = 0x01;
468
469        /// `RESOLVE_NO_MAGICLINKS`
470        const NO_MAGICLINKS = 0x02;
471
472        /// `RESOLVE_NO_SYMLINKS`
473        const NO_SYMLINKS = 0x04;
474
475        /// `RESOLVE_BENEATH`
476        const BENEATH = 0x08;
477
478        /// `RESOLVE_IN_ROOT`
479        const IN_ROOT = 0x10;
480
481        /// `RESOLVE_CACHED` (since Linux 5.12)
482        const CACHED = 0x20;
483
484        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
485        const _ = !0;
486    }
487}
488
489#[cfg(linux_kernel)]
490bitflags! {
491    /// `RENAME_*` constants for use with [`renameat_with`].
492    ///
493    /// [`renameat_with`]: crate::fs::renameat_with
494    #[repr(transparent)]
495    #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
496    pub struct RenameFlags: ffi::c_uint {
497        /// `RENAME_EXCHANGE`
498        const EXCHANGE = bitcast!(c::RENAME_EXCHANGE);
499
500        /// `RENAME_NOREPLACE`
501        const NOREPLACE = bitcast!(c::RENAME_NOREPLACE);
502
503        /// `RENAME_WHITEOUT`
504        const WHITEOUT = bitcast!(c::RENAME_WHITEOUT);
505
506        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
507        const _ = !0;
508    }
509}
510
511#[cfg(apple)]
512bitflags! {
513    /// `RENAME_*` constants for use with [`renameat_with`].
514    ///
515    /// [`renameat_with`]: crate::fs::renameat_with
516    #[repr(transparent)]
517    #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
518    pub struct RenameFlags: ffi::c_uint {
519        /// `RENAME_SWAP`
520        const EXCHANGE = bitcast!(c::RENAME_SWAP);
521
522        /// `RENAME_EXCL`
523        const NOREPLACE = bitcast!(c::RENAME_EXCL);
524
525        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
526        const _ = !0;
527    }
528}
529
530/// `S_IF*` constants for use with [`mknodat`] and [`Stat`]'s `st_mode` field.
531///
532/// [`mknodat`]: crate::fs::mknodat
533/// [`Stat`]: crate::fs::Stat
534#[derive(Clone, Copy, Debug, PartialEq, Eq)]
535pub enum FileType {
536    /// `S_IFREG`
537    RegularFile = c::S_IFREG as isize,
538
539    /// `S_IFDIR`
540    Directory = c::S_IFDIR as isize,
541
542    /// `S_IFLNK`
543    Symlink = c::S_IFLNK as isize,
544
545    /// `S_IFIFO`
546    #[cfg(not(target_os = "wasi"))] // TODO: Use WASI's `S_IFIFO`.
547    #[doc(alias = "IFO")]
548    Fifo = c::S_IFIFO as isize,
549
550    /// `S_IFSOCK`
551    #[cfg(not(target_os = "wasi"))] // TODO: Use WASI's `S_IFSOCK`.
552    Socket = c::S_IFSOCK as isize,
553
554    /// `S_IFCHR`
555    CharacterDevice = c::S_IFCHR as isize,
556
557    /// `S_IFBLK`
558    BlockDevice = c::S_IFBLK as isize,
559
560    /// An unknown filesystem object.
561    Unknown,
562}
563
564impl FileType {
565    /// Construct a `FileType` from the `S_IFMT` bits of the `st_mode` field of
566    /// a `Stat`.
567    #[inline]
568    pub const fn from_raw_mode(st_mode: RawMode) -> Self {
569        match (st_mode as c::mode_t) & c::S_IFMT {
570            c::S_IFREG => Self::RegularFile,
571            c::S_IFDIR => Self::Directory,
572            c::S_IFLNK => Self::Symlink,
573            #[cfg(not(target_os = "wasi"))] // TODO: Use WASI's `S_IFIFO`.
574            c::S_IFIFO => Self::Fifo,
575            #[cfg(not(target_os = "wasi"))] // TODO: Use WASI's `S_IFSOCK`.
576            c::S_IFSOCK => Self::Socket,
577            c::S_IFCHR => Self::CharacterDevice,
578            c::S_IFBLK => Self::BlockDevice,
579            _ => Self::Unknown,
580        }
581    }
582
583    /// Construct an `st_mode` value from a `FileType`.
584    #[inline]
585    pub const fn as_raw_mode(self) -> RawMode {
586        match self {
587            Self::RegularFile => c::S_IFREG as RawMode,
588            Self::Directory => c::S_IFDIR as RawMode,
589            Self::Symlink => c::S_IFLNK as RawMode,
590            #[cfg(not(target_os = "wasi"))] // TODO: Use WASI's `S_IFIFO`.
591            Self::Fifo => c::S_IFIFO as RawMode,
592            #[cfg(not(target_os = "wasi"))] // TODO: Use WASI's `S_IFSOCK`.
593            Self::Socket => c::S_IFSOCK as RawMode,
594            Self::CharacterDevice => c::S_IFCHR as RawMode,
595            Self::BlockDevice => c::S_IFBLK as RawMode,
596            Self::Unknown => c::S_IFMT as RawMode,
597        }
598    }
599
600    /// Construct a `FileType` from the `d_type` field of a `c::dirent`.
601    #[cfg(not(any(
602        solarish,
603        target_os = "aix",
604        target_os = "espidf",
605        target_os = "haiku",
606        target_os = "nto",
607        target_os = "redox",
608        target_os = "vita"
609    )))]
610    #[inline]
611    pub(crate) const fn from_dirent_d_type(d_type: u8) -> Self {
612        match d_type {
613            c::DT_REG => Self::RegularFile,
614            c::DT_DIR => Self::Directory,
615            c::DT_LNK => Self::Symlink,
616            #[cfg(not(target_os = "wasi"))] // TODO: Use WASI's `DT_SOCK`.
617            c::DT_SOCK => Self::Socket,
618            #[cfg(not(target_os = "wasi"))] // TODO: Use WASI's `DT_FIFO`.
619            c::DT_FIFO => Self::Fifo,
620            c::DT_CHR => Self::CharacterDevice,
621            c::DT_BLK => Self::BlockDevice,
622            // c::DT_UNKNOWN |
623            _ => Self::Unknown,
624        }
625    }
626}
627
628/// `POSIX_FADV_*` constants for use with [`fadvise`].
629///
630/// [`fadvise`]: crate::fs::fadvise
631#[cfg(not(any(
632    apple,
633    netbsdlike,
634    target_os = "dragonfly",
635    target_os = "espidf",
636    target_os = "horizon",
637    target_os = "haiku",
638    target_os = "redox",
639    target_os = "solaris",
640    target_os = "vita",
641)))]
642#[derive(Debug, Copy, Clone, Eq, PartialEq)]
643#[repr(u32)]
644pub enum Advice {
645    /// `POSIX_FADV_NORMAL`
646    Normal = c::POSIX_FADV_NORMAL as c::c_uint,
647
648    /// `POSIX_FADV_SEQUENTIAL`
649    Sequential = c::POSIX_FADV_SEQUENTIAL as c::c_uint,
650
651    /// `POSIX_FADV_RANDOM`
652    Random = c::POSIX_FADV_RANDOM as c::c_uint,
653
654    /// `POSIX_FADV_NOREUSE`
655    NoReuse = c::POSIX_FADV_NOREUSE as c::c_uint,
656
657    /// `POSIX_FADV_WILLNEED`
658    WillNeed = c::POSIX_FADV_WILLNEED as c::c_uint,
659
660    /// `POSIX_FADV_DONTNEED`
661    DontNeed = c::POSIX_FADV_DONTNEED as c::c_uint,
662}
663
664#[cfg(any(linux_kernel, target_os = "freebsd"))]
665bitflags! {
666    /// `MFD_*` constants for use with [`memfd_create`].
667    ///
668    /// [`memfd_create`]: crate::fs::memfd_create
669    #[repr(transparent)]
670    #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
671    pub struct MemfdFlags: ffi::c_uint {
672        /// `MFD_CLOEXEC`
673        const CLOEXEC = c::MFD_CLOEXEC;
674
675        /// `MFD_ALLOW_SEALING`
676        const ALLOW_SEALING = c::MFD_ALLOW_SEALING;
677
678        /// `MFD_HUGETLB` (since Linux 4.14)
679        const HUGETLB = c::MFD_HUGETLB;
680
681        /// `MFD_NOEXEC_SEAL` (since Linux 6.3)
682        #[cfg(linux_kernel)]
683        const NOEXEC_SEAL = c::MFD_NOEXEC_SEAL;
684        /// `MFD_EXEC` (since Linux 6.3)
685        #[cfg(linux_kernel)]
686        const EXEC = c::MFD_EXEC;
687
688        /// `MFD_HUGE_64KB`
689        const HUGE_64KB = c::MFD_HUGE_64KB;
690        /// `MFD_HUGE_512JB`
691        const HUGE_512KB = c::MFD_HUGE_512KB;
692        /// `MFD_HUGE_1MB`
693        const HUGE_1MB = c::MFD_HUGE_1MB;
694        /// `MFD_HUGE_2MB`
695        const HUGE_2MB = c::MFD_HUGE_2MB;
696        /// `MFD_HUGE_8MB`
697        const HUGE_8MB = c::MFD_HUGE_8MB;
698        /// `MFD_HUGE_16MB`
699        const HUGE_16MB = c::MFD_HUGE_16MB;
700        /// `MFD_HUGE_32MB`
701        const HUGE_32MB = c::MFD_HUGE_32MB;
702        /// `MFD_HUGE_256MB`
703        const HUGE_256MB = c::MFD_HUGE_256MB;
704        /// `MFD_HUGE_512MB`
705        const HUGE_512MB = c::MFD_HUGE_512MB;
706        /// `MFD_HUGE_1GB`
707        const HUGE_1GB = c::MFD_HUGE_1GB;
708        /// `MFD_HUGE_2GB`
709        const HUGE_2GB = c::MFD_HUGE_2GB;
710        /// `MFD_HUGE_16GB`
711        const HUGE_16GB = c::MFD_HUGE_16GB;
712
713        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
714        const _ = !0;
715    }
716}
717
718#[cfg(any(linux_kernel, target_os = "freebsd", target_os = "fuchsia"))]
719bitflags! {
720    /// `F_SEAL_*` constants for use with [`fcntl_add_seals`] and
721    /// [`fcntl_get_seals`].
722    ///
723    /// [`fcntl_add_seals`]: crate::fs::fcntl_add_seals
724    /// [`fcntl_get_seals`]: crate::fs::fcntl_get_seals
725    #[repr(transparent)]
726    #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
727    pub struct SealFlags: u32 {
728        /// `F_SEAL_SEAL`
729        const SEAL = bitcast!(c::F_SEAL_SEAL);
730        /// `F_SEAL_SHRINK`
731        const SHRINK = bitcast!(c::F_SEAL_SHRINK);
732        /// `F_SEAL_GROW`
733        const GROW = bitcast!(c::F_SEAL_GROW);
734        /// `F_SEAL_WRITE`
735        const WRITE = bitcast!(c::F_SEAL_WRITE);
736        /// `F_SEAL_FUTURE_WRITE` (since Linux 5.1)
737        #[cfg(linux_kernel)]
738        const FUTURE_WRITE = bitcast!(c::F_SEAL_FUTURE_WRITE);
739
740        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
741        const _ = !0;
742    }
743}
744
745#[cfg(not(any(
746    netbsdlike,
747    target_os = "espidf",
748    target_os = "horizon",
749    target_os = "nto",
750    target_os = "redox",
751    target_os = "vita"
752)))]
753bitflags! {
754    /// `FALLOC_FL_*` constants for use with [`fallocate`].
755    ///
756    /// [`fallocate`]: crate::fs::fallocate
757    #[repr(transparent)]
758    #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
759    pub struct FallocateFlags: u32 {
760        /// `FALLOC_FL_KEEP_SIZE`
761        #[cfg(not(any(
762            bsd,
763            solarish,
764            target_os = "aix",
765            target_os = "haiku",
766            target_os = "hurd",
767            target_os = "wasi",
768        )))]
769        const KEEP_SIZE = bitcast!(c::FALLOC_FL_KEEP_SIZE);
770        /// `FALLOC_FL_PUNCH_HOLE`
771        #[cfg(not(any(
772            bsd,
773            solarish,
774            target_os = "aix",
775            target_os = "haiku",
776            target_os = "hurd",
777            target_os = "wasi",
778        )))]
779        const PUNCH_HOLE = bitcast!(c::FALLOC_FL_PUNCH_HOLE);
780        /// `FALLOC_FL_NO_HIDE_STALE`
781        #[cfg(not(any(
782            bsd,
783            solarish,
784            target_os = "aix",
785            target_os = "cygwin",
786            target_os = "emscripten",
787            target_os = "fuchsia",
788            target_os = "haiku",
789            target_os = "hurd",
790            target_os = "l4re",
791            target_os = "linux",
792            target_os = "wasi",
793        )))]
794        const NO_HIDE_STALE = bitcast!(c::FALLOC_FL_NO_HIDE_STALE);
795        /// `FALLOC_FL_COLLAPSE_RANGE`
796        #[cfg(not(any(
797            bsd,
798            solarish,
799            target_os = "aix",
800            target_os = "haiku",
801            target_os = "hurd",
802            target_os = "emscripten",
803            target_os = "wasi",
804        )))]
805        const COLLAPSE_RANGE = bitcast!(c::FALLOC_FL_COLLAPSE_RANGE);
806        /// `FALLOC_FL_ZERO_RANGE`
807        #[cfg(not(any(
808            bsd,
809            solarish,
810            target_os = "aix",
811            target_os = "haiku",
812            target_os = "hurd",
813            target_os = "emscripten",
814            target_os = "wasi",
815        )))]
816        const ZERO_RANGE = bitcast!(c::FALLOC_FL_ZERO_RANGE);
817        /// `FALLOC_FL_INSERT_RANGE`
818        #[cfg(not(any(
819            bsd,
820            solarish,
821            target_os = "aix",
822            target_os = "haiku",
823            target_os = "hurd",
824            target_os = "emscripten",
825            target_os = "wasi",
826        )))]
827        const INSERT_RANGE = bitcast!(c::FALLOC_FL_INSERT_RANGE);
828        /// `FALLOC_FL_UNSHARE_RANGE`
829        #[cfg(not(any(
830            bsd,
831            solarish,
832            target_os = "aix",
833            target_os = "haiku",
834            target_os = "hurd",
835            target_os = "emscripten",
836            target_os = "wasi",
837        )))]
838        const UNSHARE_RANGE = bitcast!(c::FALLOC_FL_UNSHARE_RANGE);
839
840        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
841        const _ = !0;
842    }
843}
844
845#[cfg(not(any(target_os = "haiku", target_os = "redox", target_os = "wasi")))]
846bitflags! {
847    /// `ST_*` constants for use with [`StatVfs`].
848    #[repr(transparent)]
849    #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
850    pub struct StatVfsMountFlags: u64 {
851        /// `ST_MANDLOCK`
852        #[cfg(any(linux_kernel, target_os = "emscripten", target_os = "fuchsia"))]
853        const MANDLOCK = c::ST_MANDLOCK as u64;
854
855        /// `ST_NOATIME`
856        #[cfg(any(linux_kernel, target_os = "emscripten", target_os = "fuchsia"))]
857        const NOATIME = c::ST_NOATIME as u64;
858
859        /// `ST_NODEV`
860        #[cfg(any(
861            linux_kernel,
862            target_os = "aix",
863            target_os = "emscripten",
864            target_os = "fuchsia"
865        ))]
866        const NODEV = c::ST_NODEV as u64;
867
868        /// `ST_NODIRATIME`
869        #[cfg(any(linux_kernel, target_os = "emscripten", target_os = "fuchsia"))]
870        const NODIRATIME = c::ST_NODIRATIME as u64;
871
872        /// `ST_NOEXEC`
873        #[cfg(any(linux_kernel, target_os = "emscripten", target_os = "fuchsia"))]
874        const NOEXEC = c::ST_NOEXEC as u64;
875
876        /// `ST_NOSUID`
877        #[cfg(not(any(target_os = "espidf", target_os = "horizon", target_os = "vita")))]
878        const NOSUID = c::ST_NOSUID as u64;
879
880        /// `ST_RDONLY`
881        #[cfg(not(any(target_os = "espidf", target_os = "horizon", target_os = "vita")))]
882        const RDONLY = c::ST_RDONLY as u64;
883
884        /// `ST_RELATIME`
885        #[cfg(any(target_os = "android", all(target_os = "linux", target_env = "gnu")))]
886        const RELATIME = c::ST_RELATIME as u64;
887
888        /// `ST_SYNCHRONOUS`
889        #[cfg(any(linux_kernel, target_os = "emscripten", target_os = "fuchsia"))]
890        const SYNCHRONOUS = c::ST_SYNCHRONOUS as u64;
891
892        /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
893        const _ = !0;
894    }
895}
896
897/// `LOCK_*` constants for use with [`flock`] and [`fcntl_lock`].
898///
899/// [`flock`]: crate::fs::flock
900/// [`fcntl_lock`]: crate::fs::fcntl_lock
901// Solaris doesn't support `flock` and doesn't define `LOCK_SH` etc., but we
902// reuse this `FlockOperation` enum for `fcntl_lock`, so on Solaris we use
903// our own made-up integer values.
904#[cfg(not(any(
905    target_os = "espidf",
906    target_os = "horizon",
907    target_os = "vita",
908    target_os = "wasi"
909)))]
910#[derive(Clone, Copy, Debug, PartialEq, Eq)]
911#[repr(u32)]
912pub enum FlockOperation {
913    /// `LOCK_SH`
914    #[cfg(not(target_os = "solaris"))]
915    LockShared = bitcast!(c::LOCK_SH),
916    /// `LOCK_SH`
917    #[cfg(target_os = "solaris")]
918    LockShared = bitcast!(1),
919    /// `LOCK_EX`
920    #[cfg(not(target_os = "solaris"))]
921    LockExclusive = bitcast!(c::LOCK_EX),
922    /// `LOCK_EX`
923    #[cfg(target_os = "solaris")]
924    LockExclusive = bitcast!(2),
925    /// `LOCK_UN`
926    #[cfg(not(target_os = "solaris"))]
927    Unlock = bitcast!(c::LOCK_UN),
928    /// `LOCK_UN`
929    #[cfg(target_os = "solaris")]
930    Unlock = bitcast!(8),
931    /// `LOCK_SH | LOCK_NB`
932    #[cfg(not(target_os = "solaris"))]
933    NonBlockingLockShared = bitcast!(c::LOCK_SH | c::LOCK_NB),
934    /// `LOCK_SH | LOCK_NB`
935    #[cfg(target_os = "solaris")]
936    NonBlockingLockShared = bitcast!(1 | 4),
937    /// `LOCK_EX | LOCK_NB`
938    #[cfg(not(target_os = "solaris"))]
939    NonBlockingLockExclusive = bitcast!(c::LOCK_EX | c::LOCK_NB),
940    /// `LOCK_EX | LOCK_NB`
941    #[cfg(target_os = "solaris")]
942    NonBlockingLockExclusive = bitcast!(2 | 4),
943    /// `LOCK_UN | LOCK_NB`
944    #[cfg(not(target_os = "solaris"))]
945    NonBlockingUnlock = bitcast!(c::LOCK_UN | c::LOCK_NB),
946    /// `LOCK_UN | LOCK_NB`
947    #[cfg(target_os = "solaris")]
948    NonBlockingUnlock = bitcast!(8 | 4),
949}
950
951/// `struct stat` for use with [`statat`] and [`fstat`].
952///
953/// [`statat`]: crate::fs::statat
954/// [`fstat`]: crate::fs::fstat
955#[cfg(not(any(linux_like, target_os = "hurd", target_os = "netbsd")))]
956pub type Stat = c::stat;
957
958/// `struct stat` for use with [`statat`] and [`fstat`].
959///
960/// [`statat`]: crate::fs::statat
961/// [`fstat`]: crate::fs::fstat
962#[cfg(any(
963    all(linux_kernel, target_pointer_width = "64"),
964    target_os = "hurd",
965    target_os = "emscripten",
966    target_os = "l4re",
967))]
968pub type Stat = c::stat64;
969
970/// `struct stat` for use with [`statat`] and [`fstat`].
971///
972/// [`statat`]: crate::fs::statat
973/// [`fstat`]: crate::fs::fstat
974// On 32-bit, Linux's `struct stat64` has a 32-bit `st_mtime` and friends, so
975// we use our own struct, populated from `statx` where possible, to avoid the
976// y2038 bug.
977#[cfg(all(linux_kernel, target_pointer_width = "32"))]
978#[derive(Debug, Copy, Clone)]
979#[allow(missing_docs)]
980pub struct Stat {
981    pub st_dev: u64,
982    pub st_mode: u32,
983    pub st_nlink: u64,
984    pub st_uid: u32,
985    pub st_gid: u32,
986    pub st_rdev: u64,
987    pub st_size: i64,
988    pub st_blksize: u32,
989    pub st_blocks: u64,
990    pub st_atime: i64,
991    pub st_atime_nsec: u32,
992    pub st_mtime: i64,
993    pub st_mtime_nsec: u32,
994    pub st_ctime: i64,
995    pub st_ctime_nsec: u32,
996    pub st_ino: u64,
997}
998
999/// `struct stat` for use with [`statat`] and [`fstat`].
1000///
1001/// [`statat`]: crate::fs::statat
1002/// [`fstat`]: crate::fs::fstat
1003// NetBSD's `st_mtime_nsec` is named `st_mtimensec` so we declare our own
1004// `Stat` so that we can be consistent with other platforms.
1005#[cfg(target_os = "netbsd")]
1006#[derive(Debug, Copy, Clone)]
1007#[allow(missing_docs)]
1008#[repr(C)]
1009pub struct Stat {
1010    pub st_dev: c::dev_t,
1011    pub st_mode: c::mode_t,
1012    pub st_ino: c::ino_t,
1013    pub st_nlink: c::nlink_t,
1014    pub st_uid: c::uid_t,
1015    pub st_gid: c::gid_t,
1016    pub st_rdev: c::dev_t,
1017    pub st_atime: c::time_t,
1018    pub st_atime_nsec: c::c_long,
1019    pub st_mtime: c::time_t,
1020    pub st_mtime_nsec: c::c_long,
1021    pub st_ctime: c::time_t,
1022    pub st_ctime_nsec: c::c_long,
1023    pub st_birthtime: c::time_t,
1024    pub st_birthtime_nsec: c::c_long,
1025    pub st_size: c::off_t,
1026    pub st_blocks: c::blkcnt_t,
1027    pub st_blksize: c::blksize_t,
1028    pub st_flags: u32,
1029    pub st_gen: u32,
1030    pub st_spare: [u32; 2],
1031}
1032
1033/// `struct statfs` for use with [`statfs`] and [`fstatfs`].
1034///
1035/// [`statfs`]: crate::fs::statfs
1036/// [`fstatfs`]: crate::fs::fstatfs
1037#[cfg(not(any(
1038    linux_like,
1039    solarish,
1040    target_os = "espidf",
1041    target_os = "haiku",
1042    target_os = "horizon",
1043    target_os = "netbsd",
1044    target_os = "nto",
1045    target_os = "redox",
1046    target_os = "vita",
1047    target_os = "wasi",
1048)))]
1049#[allow(clippy::module_name_repetitions)]
1050pub type StatFs = c::statfs;
1051
1052/// `struct statfs` for use with [`statfs`] and [`fstatfs`].
1053///
1054/// [`statfs`]: crate::fs::statfs
1055/// [`fstatfs`]: crate::fs::fstatfs
1056#[cfg(linux_like)]
1057pub type StatFs = c::statfs64;
1058
1059/// `fsid_t` for use with [`StatFs`].
1060#[cfg(not(any(
1061    solarish,
1062    target_os = "cygwin",
1063    target_os = "espidf",
1064    target_os = "haiku",
1065    target_os = "horizon",
1066    target_os = "nto",
1067    target_os = "redox",
1068    target_os = "vita",
1069    target_os = "wasi",
1070)))]
1071pub type Fsid = c::fsid_t;
1072
1073/// `struct statvfs` for use with [`statvfs`] and [`fstatvfs`].
1074///
1075/// [`statvfs`]: crate::fs::statvfs
1076/// [`fstatvfs`]: crate::fs::fstatvfs
1077#[cfg(not(any(target_os = "haiku", target_os = "redox", target_os = "wasi")))]
1078#[allow(missing_docs)]
1079pub struct StatVfs {
1080    pub f_bsize: u64,
1081    pub f_frsize: u64,
1082    pub f_blocks: u64,
1083    pub f_bfree: u64,
1084    pub f_bavail: u64,
1085    pub f_files: u64,
1086    pub f_ffree: u64,
1087    pub f_favail: u64,
1088    pub f_fsid: u64,
1089    pub f_flag: StatVfsMountFlags,
1090    pub f_namemax: u64,
1091}
1092
1093/// `mode_t`
1094#[cfg(not(all(target_os = "android", target_pointer_width = "32")))]
1095pub type RawMode = c::mode_t;
1096
1097/// `mode_t`
1098#[cfg(all(target_os = "android", target_pointer_width = "32"))]
1099pub type RawMode = ffi::c_uint;
1100
1101/// `dev_t`
1102#[cfg(not(all(target_os = "android", target_pointer_width = "32")))]
1103pub type Dev = c::dev_t;
1104
1105/// `dev_t`
1106#[cfg(all(target_os = "android", target_pointer_width = "32"))]
1107pub type Dev = ffi::c_ulonglong;
1108
1109/// `__fsword_t`
1110#[cfg(all(
1111    target_os = "linux",
1112    not(target_env = "musl"),
1113    not(target_arch = "s390x"),
1114))]
1115pub type FsWord = c::__fsword_t;
1116
1117/// `__fsword_t`
1118#[cfg(all(
1119    any(target_os = "android", all(target_os = "linux", target_env = "musl")),
1120    target_pointer_width = "32",
1121))]
1122pub type FsWord = u32;
1123
1124/// `__fsword_t`
1125#[cfg(all(
1126    any(target_os = "android", all(target_os = "linux", target_env = "musl")),
1127    not(target_arch = "s390x"),
1128    target_pointer_width = "64",
1129))]
1130pub type FsWord = u64;
1131
1132/// `__fsword_t`
1133// s390x uses `u32` for `statfs` entries on glibc, even though `__fsword_t` is
1134// `u64`.
1135#[cfg(all(target_os = "linux", target_arch = "s390x", target_env = "gnu"))]
1136pub type FsWord = u32;
1137
1138/// `__fsword_t`
1139// s390x uses `u64` for `statfs` entries on musl.
1140#[cfg(all(target_os = "linux", target_arch = "s390x", target_env = "musl"))]
1141pub type FsWord = u64;
1142
1143/// `copyfile_state_t`—State for use with [`fcopyfile`].
1144///
1145/// [`fcopyfile`]: crate::fs::fcopyfile
1146#[cfg(apple)]
1147#[allow(non_camel_case_types)]
1148#[repr(transparent)]
1149#[derive(Copy, Clone)]
1150pub struct copyfile_state_t(pub(crate) *mut c::c_void);