1 | //! Filesystem API constants, translated into `bitflags` constants. |
2 | |
3 | use crate::backend; |
4 | |
5 | pub use crate::timespec::{Nsecs, Secs, Timespec}; |
6 | pub use backend::fs::types::*; |
7 | |
8 | impl FileType { |
9 | /// Returns `true` if this `FileType` is a regular file. |
10 | pub fn is_file(self) -> bool { |
11 | self == Self::RegularFile |
12 | } |
13 | |
14 | /// Returns `true` if this `FileType` is a directory. |
15 | pub fn is_dir(self) -> bool { |
16 | self == Self::Directory |
17 | } |
18 | |
19 | /// Returns `true` if this `FileType` is a symlink. |
20 | pub fn is_symlink(self) -> bool { |
21 | self == Self::Symlink |
22 | } |
23 | |
24 | /// Returns `true` if this `FileType` is a fifo. |
25 | #[cfg (not(target_os = "wasi" ))] |
26 | pub fn is_fifo(self) -> bool { |
27 | self == Self::Fifo |
28 | } |
29 | |
30 | /// Returns `true` if this `FileType` is a socket. |
31 | #[cfg (not(target_os = "wasi" ))] |
32 | pub fn is_socket(self) -> bool { |
33 | self == Self::Socket |
34 | } |
35 | |
36 | /// Returns `true` if this `FileType` is a character device. |
37 | pub fn is_char_device(self) -> bool { |
38 | self == Self::CharacterDevice |
39 | } |
40 | |
41 | /// Returns `true` if this `FileType` is a block device. |
42 | pub fn is_block_device(self) -> bool { |
43 | self == Self::BlockDevice |
44 | } |
45 | } |
46 | |
47 | #[cfg (test)] |
48 | #[allow (unused_imports)] |
49 | #[allow (unsafe_code)] |
50 | mod tests { |
51 | use super::*; |
52 | use crate::backend::c; |
53 | // Rust's libc crate lacks statx for Non-glibc targets. |
54 | #[cfg (all(target_os = "linux" , target_env = "gnu" ))] |
55 | use crate::fs::{Statx, StatxTimestamp}; |
56 | |
57 | #[test ] |
58 | fn test_layouts() { |
59 | #[cfg (linux_kernel)] |
60 | assert_eq_size!(FsWord, linux_raw_sys::general::__fsword_t); |
61 | |
62 | // Don't test against `__kernel_mode_t` on platforms where it's a |
63 | // `u16`. |
64 | #[cfg (linux_kernel)] |
65 | #[cfg (not(any( |
66 | target_arch = "x86" , |
67 | target_arch = "sparc" , |
68 | target_arch = "avr" , |
69 | target_arch = "arm" , |
70 | )))] |
71 | assert_eq_size!(RawMode, linux_raw_sys::general::__kernel_mode_t); |
72 | |
73 | #[cfg (linux_kernel)] |
74 | #[cfg (any( |
75 | target_arch = "x86" , |
76 | target_arch = "sparc" , |
77 | target_arch = "avr" , |
78 | target_arch = "arm" , |
79 | ))] |
80 | assert_eq_size!(u16, linux_raw_sys::general::__kernel_mode_t); |
81 | |
82 | let some_stat: Stat = unsafe { core::mem::zeroed() }; |
83 | |
84 | // Ensure that seconds fields are 64-bit on non-y2038-bug platforms, and |
85 | // on Linux where we use statx. |
86 | #[cfg (any(linux_kernel, not(fix_y2038)))] |
87 | { |
88 | assert_eq!(some_stat.st_atime, 0_i64); |
89 | assert_eq!(some_stat.st_mtime, 0_i64); |
90 | assert_eq!(some_stat.st_ctime, 0_i64); |
91 | } |
92 | |
93 | // Ensure that file offsets are 64-bit. |
94 | assert_eq!(some_stat.st_size, 0_i64); |
95 | |
96 | // Check that various fields match expected types. |
97 | assert_eq!(some_stat.st_mode, 0 as RawMode); |
98 | assert_eq!(some_stat.st_dev, 0 as Dev); |
99 | assert_eq!(some_stat.st_rdev, 0 as Dev); |
100 | assert_eq!(some_stat.st_uid, 0 as crate::ugid::RawUid); |
101 | assert_eq!(some_stat.st_gid, 0 as crate::ugid::RawGid); |
102 | |
103 | // `Stat` should match `c::stat` or `c::stat64` unless we need y2038 |
104 | // fixes and are using a different layout. |
105 | #[cfg (not(any( |
106 | all(libc, linux_kernel, target_pointer_width = "32" ), |
107 | all( |
108 | linux_raw, |
109 | any( |
110 | target_pointer_width = "32" , |
111 | target_arch = "mips64" , |
112 | target_arch = "mips64r6" |
113 | ) |
114 | ) |
115 | )))] |
116 | { |
117 | // Check that `Stat` matches `c::stat`. |
118 | #[cfg (not(all( |
119 | libc, |
120 | any( |
121 | all(linux_kernel, target_pointer_width = "64" ), |
122 | target_os = "hurd" , |
123 | target_os = "emscripten" , |
124 | target_os = "l4re" , |
125 | ) |
126 | )))] |
127 | { |
128 | check_renamed_type!(Stat, stat); |
129 | check_renamed_struct_field!(Stat, stat, st_dev); |
130 | check_renamed_struct_field!(Stat, stat, st_ino); |
131 | check_renamed_struct_field!(Stat, stat, st_nlink); |
132 | check_renamed_struct_field!(Stat, stat, st_mode); |
133 | check_renamed_struct_field!(Stat, stat, st_uid); |
134 | check_renamed_struct_field!(Stat, stat, st_gid); |
135 | #[cfg (all( |
136 | linux_raw, |
137 | not(any( |
138 | target_arch = "aarch64" , |
139 | target_arch = "powerpc64" , |
140 | target_arch = "riscv64" , |
141 | target_arch = "s390x" |
142 | )) |
143 | ))] |
144 | check_renamed_struct_field!(Stat, stat, __pad0); |
145 | check_renamed_struct_field!(Stat, stat, st_rdev); |
146 | #[cfg (all(linux_raw, not(any(target_arch = "powerpc64" , target_arch = "x86_64" ))))] |
147 | check_renamed_struct_field!(Stat, stat, __pad1); |
148 | check_renamed_struct_field!(Stat, stat, st_size); |
149 | check_renamed_struct_field!(Stat, stat, st_blksize); |
150 | #[cfg (all( |
151 | linux_raw, |
152 | not(any( |
153 | target_arch = "powerpc64" , |
154 | target_arch = "s390x" , |
155 | target_arch = "x86_64" |
156 | )) |
157 | ))] |
158 | check_renamed_struct_field!(Stat, stat, __pad2); |
159 | check_renamed_struct_field!(Stat, stat, st_blocks); |
160 | check_renamed_struct_field!(Stat, stat, st_atime); |
161 | #[cfg (not(target_os = "netbsd" ))] |
162 | check_renamed_struct_field!(Stat, stat, st_atime_nsec); |
163 | #[cfg (target_os = "netbsd" )] |
164 | check_renamed_struct_renamed_field!(Stat, stat, st_atime_nsec, st_atimensec); |
165 | check_renamed_struct_field!(Stat, stat, st_mtime); |
166 | #[cfg (not(target_os = "netbsd" ))] |
167 | check_renamed_struct_field!(Stat, stat, st_mtime_nsec); |
168 | #[cfg (target_os = "netbsd" )] |
169 | check_renamed_struct_renamed_field!(Stat, stat, st_mtime_nsec, st_mtimensec); |
170 | check_renamed_struct_field!(Stat, stat, st_ctime); |
171 | #[cfg (not(target_os = "netbsd" ))] |
172 | check_renamed_struct_field!(Stat, stat, st_ctime_nsec); |
173 | #[cfg (target_os = "netbsd" )] |
174 | check_renamed_struct_renamed_field!(Stat, stat, st_ctime_nsec, st_ctimensec); |
175 | #[cfg (all( |
176 | linux_raw, |
177 | not(any( |
178 | target_arch = "aarch64" , |
179 | target_arch = "powerpc64" , |
180 | target_arch = "riscv64" |
181 | )) |
182 | ))] |
183 | check_renamed_struct_field!(Stat, stat, __unused); |
184 | #[cfg (all(linux_raw, not(any(target_arch = "s390x" , target_arch = "x86_64" ))))] |
185 | check_renamed_struct_field!(Stat, stat, __unused4); |
186 | #[cfg (all(linux_raw, not(any(target_arch = "s390x" , target_arch = "x86_64" ))))] |
187 | check_renamed_struct_field!(Stat, stat, __unused5); |
188 | #[cfg (all( |
189 | linux_raw, |
190 | not(any( |
191 | target_arch = "aarch64" , |
192 | target_arch = "riscv64" , |
193 | target_arch = "s390x" , |
194 | target_arch = "x86_64" |
195 | )) |
196 | ))] |
197 | check_renamed_struct_field!(Stat, stat, __unused6); |
198 | } |
199 | |
200 | // Check that `Stat` matches `c::stat64`. |
201 | #[cfg (all( |
202 | libc, |
203 | any( |
204 | all(linux_kernel, target_pointer_width = "64" ), |
205 | target_os = "hurd" , |
206 | target_os = "emscripten" , |
207 | target_os = "l4re" , |
208 | ) |
209 | ))] |
210 | { |
211 | check_renamed_type!(Stat, stat64); |
212 | check_renamed_struct_field!(Stat, stat64, st_dev); |
213 | check_renamed_struct_field!(Stat, stat64, st_ino); |
214 | check_renamed_struct_field!(Stat, stat64, st_nlink); |
215 | check_renamed_struct_field!(Stat, stat64, st_mode); |
216 | check_renamed_struct_field!(Stat, stat64, st_uid); |
217 | check_renamed_struct_field!(Stat, stat64, st_gid); |
218 | #[cfg (all( |
219 | linux_raw, |
220 | not(any( |
221 | target_arch = "aarch64" , |
222 | target_arch = "powerpc64" , |
223 | target_arch = "riscv64" , |
224 | target_arch = "s390x" |
225 | )) |
226 | ))] |
227 | check_renamed_struct_field!(Stat, stat64, __pad0); |
228 | check_renamed_struct_field!(Stat, stat64, st_rdev); |
229 | #[cfg (all(linux_raw, not(any(target_arch = "powerpc64" , target_arch = "x86_64" ))))] |
230 | check_renamed_struct_field!(Stat, stat64, __pad1); |
231 | check_renamed_struct_field!(Stat, stat64, st_size); |
232 | check_renamed_struct_field!(Stat, stat64, st_blksize); |
233 | #[cfg (all( |
234 | linux_raw, |
235 | not(any( |
236 | target_arch = "powerpc64" , |
237 | target_arch = "s390x" , |
238 | target_arch = "x86_64" |
239 | )) |
240 | ))] |
241 | check_renamed_struct_field!(Stat, stat64, __pad2); |
242 | check_renamed_struct_field!(Stat, stat64, st_blocks); |
243 | check_renamed_struct_field!(Stat, stat64, st_atime); |
244 | check_renamed_struct_field!(Stat, stat64, st_atime_nsec); |
245 | check_renamed_struct_field!(Stat, stat64, st_mtime); |
246 | check_renamed_struct_field!(Stat, stat64, st_mtime_nsec); |
247 | check_renamed_struct_field!(Stat, stat64, st_ctime); |
248 | check_renamed_struct_field!(Stat, stat64, st_ctime_nsec); |
249 | #[cfg (all( |
250 | linux_raw, |
251 | not(any( |
252 | target_arch = "aarch64" , |
253 | target_arch = "powerpc64" , |
254 | target_arch = "riscv64" |
255 | )) |
256 | ))] |
257 | check_renamed_struct_field!(Stat, stat64, __unused); |
258 | #[cfg (all(linux_raw, not(any(target_arch = "s390x" , target_arch = "x86_64" ))))] |
259 | check_renamed_struct_field!(Stat, stat64, __unused4); |
260 | #[cfg (all(linux_raw, not(any(target_arch = "s390x" , target_arch = "x86_64" ))))] |
261 | check_renamed_struct_field!(Stat, stat64, __unused5); |
262 | #[cfg (all( |
263 | linux_raw, |
264 | not(any( |
265 | target_arch = "aarch64" , |
266 | target_arch = "riscv64" , |
267 | target_arch = "s390x" , |
268 | target_arch = "x86_64" |
269 | )) |
270 | ))] |
271 | check_renamed_struct_field!(Stat, stat64, __unused6); |
272 | } |
273 | } |
274 | |
275 | #[cfg (not(any( |
276 | solarish, |
277 | target_os = "cygwin" , |
278 | target_os = "haiku" , |
279 | target_os = "nto" , |
280 | target_os = "redox" , |
281 | target_os = "wasi" , |
282 | )))] |
283 | { |
284 | check_renamed_type!(Fsid, fsid_t); |
285 | #[cfg (not(libc))] // libc hides the `val` field |
286 | check_renamed_struct_field!(Fsid, fsid_t, val); |
287 | } |
288 | |
289 | #[cfg (linux_like)] |
290 | { |
291 | check_renamed_type!(StatFs, statfs64); |
292 | check_renamed_struct_field!(StatFs, statfs64, f_type); |
293 | check_renamed_struct_field!(StatFs, statfs64, f_bsize); |
294 | check_renamed_struct_field!(StatFs, statfs64, f_blocks); |
295 | check_renamed_struct_field!(StatFs, statfs64, f_bfree); |
296 | check_renamed_struct_field!(StatFs, statfs64, f_bavail); |
297 | check_renamed_struct_field!(StatFs, statfs64, f_files); |
298 | check_renamed_struct_field!(StatFs, statfs64, f_ffree); |
299 | check_renamed_struct_field!(StatFs, statfs64, f_fsid); |
300 | check_renamed_struct_field!(StatFs, statfs64, f_namelen); |
301 | check_renamed_struct_field!(StatFs, statfs64, f_frsize); |
302 | check_renamed_struct_field!(StatFs, statfs64, f_flags); |
303 | #[cfg (linux_raw)] |
304 | check_renamed_struct_field!(StatFs, statfs64, f_spare); |
305 | } |
306 | |
307 | // Rust's libc crate lacks statx for Non-glibc targets. |
308 | #[cfg (all(target_os = "linux" , target_env = "gnu" ))] |
309 | { |
310 | check_renamed_type!(StatxTimestamp, statx_timestamp); |
311 | check_renamed_struct_field!(StatxTimestamp, statx_timestamp, tv_sec); |
312 | check_renamed_struct_field!(StatxTimestamp, statx_timestamp, tv_nsec); |
313 | #[cfg (linux_raw)] |
314 | check_renamed_struct_field!(StatxTimestamp, statx_timestamp, __reserved); |
315 | |
316 | check_renamed_type!(Statx, statx); |
317 | check_renamed_struct_field!(Statx, statx, stx_mask); |
318 | check_renamed_struct_field!(Statx, statx, stx_blksize); |
319 | check_renamed_struct_field!(Statx, statx, stx_attributes); |
320 | check_renamed_struct_field!(Statx, statx, stx_nlink); |
321 | check_renamed_struct_field!(Statx, statx, stx_uid); |
322 | check_renamed_struct_field!(Statx, statx, stx_gid); |
323 | check_renamed_struct_field!(Statx, statx, stx_mode); |
324 | #[cfg (linux_raw)] |
325 | check_renamed_struct_field!(Statx, statx, __spare0); |
326 | check_renamed_struct_field!(Statx, statx, stx_ino); |
327 | check_renamed_struct_field!(Statx, statx, stx_size); |
328 | check_renamed_struct_field!(Statx, statx, stx_blocks); |
329 | check_renamed_struct_field!(Statx, statx, stx_attributes_mask); |
330 | check_renamed_struct_field!(Statx, statx, stx_atime); |
331 | check_renamed_struct_field!(Statx, statx, stx_btime); |
332 | check_renamed_struct_field!(Statx, statx, stx_ctime); |
333 | check_renamed_struct_field!(Statx, statx, stx_mtime); |
334 | check_renamed_struct_field!(Statx, statx, stx_rdev_major); |
335 | check_renamed_struct_field!(Statx, statx, stx_rdev_minor); |
336 | check_renamed_struct_field!(Statx, statx, stx_dev_major); |
337 | check_renamed_struct_field!(Statx, statx, stx_dev_minor); |
338 | check_renamed_struct_field!(Statx, statx, stx_mnt_id); |
339 | check_renamed_struct_field!(Statx, statx, stx_dio_mem_align); |
340 | check_renamed_struct_field!(Statx, statx, stx_dio_offset_align); |
341 | #[cfg (not(libc))] // not in libc yet |
342 | check_renamed_struct_field!(Statx, statx, stx_subvol); |
343 | #[cfg (not(libc))] // not in libc yet |
344 | check_renamed_struct_field!(Statx, statx, stx_atomic_write_unit_min); |
345 | #[cfg (not(libc))] // not in libc yet |
346 | check_renamed_struct_field!(Statx, statx, stx_atomic_write_unit_max); |
347 | #[cfg (not(libc))] // not in libc yet |
348 | check_renamed_struct_field!(Statx, statx, stx_atomic_write_segments_max); |
349 | #[cfg (linux_raw)] |
350 | check_renamed_struct_field!(Statx, statx, __spare1); |
351 | #[cfg (linux_raw)] |
352 | check_renamed_struct_field!(Statx, statx, __spare3); |
353 | } |
354 | } |
355 | } |
356 | |