1 //! Filesystem operations.
2 
3 mod abs;
4 #[cfg(not(target_os = "redox"))]
5 mod at;
6 mod constants;
7 #[cfg(linux_kernel)]
8 mod copy_file_range;
9 #[cfg(not(any(target_os = "espidf", target_os = "redox")))]
10 #[cfg(not(target_os = "haiku"))] // Haiku needs <https://github.com/rust-lang/rust/pull/112371>
11 mod cwd;
12 #[cfg(all(feature = "alloc", not(any(target_os = "espidf", target_os = "redox"))))]
13 mod dir;
14 #[cfg(not(any(
15     apple,
16     netbsdlike,
17     solarish,
18     target_os = "dragonfly",
19     target_os = "espidf",
20     target_os = "haiku",
21     target_os = "redox",
22     target_os = "vita",
23 )))]
24 mod fadvise;
25 pub(crate) mod fcntl;
26 #[cfg(apple)]
27 mod fcntl_apple;
28 #[cfg(apple)]
29 mod fcopyfile;
30 pub(crate) mod fd;
31 #[cfg(all(apple, feature = "alloc"))]
32 mod getpath;
33 #[cfg(not(target_os = "wasi"))] // WASI doesn't have get[gpu]id.
34 mod id;
35 #[cfg(linux_kernel)]
36 mod ioctl;
37 #[cfg(not(any(
38     target_os = "espidf",
39     target_os = "haiku",
40     target_os = "redox",
41     target_os = "vita",
42     target_os = "wasi"
43 )))]
44 mod makedev;
45 #[cfg(any(linux_kernel, target_os = "freebsd"))]
46 mod memfd_create;
47 #[cfg(linux_kernel)]
48 #[cfg(feature = "fs")]
49 mod mount;
50 #[cfg(feature = "linux-raw-sys")]
51 mod openat2;
52 #[cfg(feature = "linux-raw-sys")]
53 mod raw_dir;
54 mod seek_from;
55 #[cfg(target_os = "linux")]
56 mod sendfile;
57 #[cfg(linux_kernel)]
58 mod statx;
59 #[cfg(not(any(
60     target_os = "espidf",
61     target_os = "redox",
62     target_os = "vita",
63     target_os = "wasi"
64 )))]
65 mod sync;
66 #[cfg(any(apple, linux_kernel))]
67 mod xattr;
68 
69 #[cfg(linux_kernel)]
70 pub use crate::backend::fs::inotify;
71 pub use abs::*;
72 #[cfg(not(target_os = "redox"))]
73 pub use at::*;
74 pub use constants::*;
75 #[cfg(linux_kernel)]
76 pub use copy_file_range::copy_file_range;
77 #[cfg(not(any(target_os = "espidf", target_os = "redox")))]
78 #[cfg(not(target_os = "haiku"))] // Haiku needs <https://github.com/rust-lang/rust/pull/112371>
79 pub use cwd::*;
80 #[cfg(all(feature = "alloc", not(any(target_os = "espidf", target_os = "redox"))))]
81 pub use dir::{Dir, DirEntry};
82 #[cfg(not(any(
83     apple,
84     netbsdlike,
85     solarish,
86     target_os = "dragonfly",
87     target_os = "espidf",
88     target_os = "haiku",
89     target_os = "redox",
90     target_os = "vita",
91 )))]
92 pub use fadvise::fadvise;
93 pub use fcntl::*;
94 #[cfg(apple)]
95 pub use fcntl_apple::*;
96 #[cfg(apple)]
97 pub use fcopyfile::*;
98 pub use fd::*;
99 #[cfg(all(apple, feature = "alloc"))]
100 pub use getpath::getpath;
101 #[cfg(not(target_os = "wasi"))]
102 pub use id::*;
103 #[cfg(linux_kernel)]
104 pub use ioctl::*;
105 #[cfg(not(any(
106     target_os = "espidf",
107     target_os = "haiku",
108     target_os = "redox",
109     target_os = "vita",
110     target_os = "wasi"
111 )))]
112 pub use makedev::*;
113 #[cfg(any(linux_kernel, target_os = "freebsd"))]
114 pub use memfd_create::memfd_create;
115 #[cfg(linux_kernel)]
116 #[cfg(feature = "fs")]
117 pub use mount::*;
118 #[cfg(feature = "linux-raw-sys")]
119 pub use openat2::openat2;
120 #[cfg(feature = "linux-raw-sys")]
121 pub use raw_dir::{RawDir, RawDirEntry};
122 pub use seek_from::SeekFrom;
123 #[cfg(target_os = "linux")]
124 pub use sendfile::sendfile;
125 #[cfg(linux_kernel)]
126 pub use statx::statx;
127 #[cfg(not(any(
128     target_os = "espidf",
129     target_os = "redox",
130     target_os = "vita",
131     target_os = "wasi"
132 )))]
133 pub use sync::sync;
134 #[cfg(any(apple, linux_kernel))]
135 pub use xattr::*;
136 
137 /// Re-export types common to POSIX-ish platforms.
138 #[cfg(feature = "std")]
139 #[cfg(unix)]
140 pub use std::os::unix::fs::{DirEntryExt, FileExt, FileTypeExt, MetadataExt, OpenOptionsExt};
141 #[cfg(feature = "std")]
142 #[cfg(all(wasi_ext, target_os = "wasi"))]
143 pub use std::os::wasi::fs::{DirEntryExt, FileExt, FileTypeExt, MetadataExt, OpenOptionsExt};
144 
145 /// Extension trait for accessing timestamp fields of `Stat`.
146 ///
147 /// Rustix's `Stat` type on some platforms has unsigned `st_mtime`,
148 /// `st_atime`, and `st_ctime` fields. This is incorrect, as Unix defines
149 /// these fields to be signed, with negative values representing dates before
150 /// the Unix epoch. Until the next semver bump, these unsigned fields are
151 /// deprecated, and this trait provides accessors which return their values
152 /// as signed integers.
153 #[cfg(all(unix, not(any(target_os = "aix", target_os = "nto"))))]
154 pub trait StatExt {
155     /// Return the value of the `st_atime` field, casted to the correct type.
atime(&self) -> i64156     fn atime(&self) -> i64;
157     /// Return the value of the `st_mtime` field, casted to the correct type.
mtime(&self) -> i64158     fn mtime(&self) -> i64;
159     /// Return the value of the `st_ctime` field, casted to the correct type.
ctime(&self) -> i64160     fn ctime(&self) -> i64;
161 }
162 
163 #[cfg(all(unix, not(any(target_os = "aix", target_os = "nto"))))]
164 #[allow(deprecated)]
165 impl StatExt for Stat {
166     #[inline]
atime(&self) -> i64167     fn atime(&self) -> i64 {
168         self.st_atime as i64
169     }
170     #[inline]
mtime(&self) -> i64171     fn mtime(&self) -> i64 {
172         self.st_mtime as i64
173     }
174     #[inline]
ctime(&self) -> i64175     fn ctime(&self) -> i64 {
176         self.st_ctime as i64
177     }
178 }
179