1use crate::backend;
4
5pub use crate::timespec::{Nsecs, Secs, Timespec};
6pub use backend::fs::types::*;
7
8impl FileType {
9 pub fn is_file(self) -> bool {
11 self == Self::RegularFile
12 }
13
14 pub fn is_dir(self) -> bool {
16 self == Self::Directory
17 }
18
19 pub fn is_symlink(self) -> bool {
21 self == Self::Symlink
22 }
23
24 #[cfg(not(target_os = "wasi"))]
26 pub fn is_fifo(self) -> bool {
27 self == Self::Fifo
28 }
29
30 #[cfg(not(target_os = "wasi"))]
32 pub fn is_socket(self) -> bool {
33 self == Self::Socket
34 }
35
36 pub fn is_char_device(self) -> bool {
38 self == Self::CharacterDevice
39 }
40
41 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)]
50mod tests {
51 use super::*;
52 use crate::backend::c;
53 #[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 #[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() };
84 assert_eq!(some_stat.st_atime, 0_i64);
85 assert_eq!(some_stat.st_mtime, 0_i64);
86 assert_eq!(some_stat.st_ctime, 0_i64);
87
88 assert_eq!(some_stat.st_size, 0_i64);
90
91 assert_eq!(some_stat.st_mode, 0 as RawMode);
93 assert_eq!(some_stat.st_dev, 0 as Dev);
94 assert_eq!(some_stat.st_rdev, 0 as Dev);
95 assert_eq!(some_stat.st_uid, 0 as crate::ugid::RawUid);
96 assert_eq!(some_stat.st_gid, 0 as crate::ugid::RawGid);
97
98 #[cfg(not(any(
101 all(libc, linux_kernel, target_pointer_width = "32"),
102 all(
103 linux_raw,
104 any(
105 target_pointer_width = "32",
106 target_arch = "mips64",
107 target_arch = "mips64r6"
108 )
109 )
110 )))]
111 {
112 #[cfg(not(all(
114 libc,
115 any(
116 all(linux_kernel, target_pointer_width = "64"),
117 target_os = "hurd",
118 target_os = "emscripten",
119 target_os = "l4re",
120 )
121 )))]
122 {
123 check_renamed_type!(Stat, stat);
124 check_renamed_struct_field!(Stat, stat, st_dev);
125 check_renamed_struct_field!(Stat, stat, st_ino);
126 check_renamed_struct_field!(Stat, stat, st_nlink);
127 check_renamed_struct_field!(Stat, stat, st_mode);
128 check_renamed_struct_field!(Stat, stat, st_uid);
129 check_renamed_struct_field!(Stat, stat, st_gid);
130 #[cfg(all(
131 linux_raw,
132 not(any(
133 target_arch = "aarch64",
134 target_arch = "powerpc64",
135 target_arch = "riscv64",
136 target_arch = "s390x"
137 ))
138 ))]
139 check_renamed_struct_field!(Stat, stat, __pad0);
140 check_renamed_struct_field!(Stat, stat, st_rdev);
141 #[cfg(all(linux_raw, not(any(target_arch = "powerpc64", target_arch = "x86_64"))))]
142 check_renamed_struct_field!(Stat, stat, __pad1);
143 check_renamed_struct_field!(Stat, stat, st_size);
144 check_renamed_struct_field!(Stat, stat, st_blksize);
145 #[cfg(all(
146 linux_raw,
147 not(any(
148 target_arch = "powerpc64",
149 target_arch = "s390x",
150 target_arch = "x86_64"
151 ))
152 ))]
153 check_renamed_struct_field!(Stat, stat, __pad2);
154 check_renamed_struct_field!(Stat, stat, st_blocks);
155 check_renamed_struct_field!(Stat, stat, st_atime);
156 #[cfg(not(target_os = "netbsd"))]
157 check_renamed_struct_field!(Stat, stat, st_atime_nsec);
158 #[cfg(target_os = "netbsd")]
159 check_renamed_struct_renamed_field!(Stat, stat, st_atime_nsec, st_atimensec);
160 check_renamed_struct_field!(Stat, stat, st_mtime);
161 #[cfg(not(target_os = "netbsd"))]
162 check_renamed_struct_field!(Stat, stat, st_mtime_nsec);
163 #[cfg(target_os = "netbsd")]
164 check_renamed_struct_renamed_field!(Stat, stat, st_mtime_nsec, st_mtimensec);
165 check_renamed_struct_field!(Stat, stat, st_ctime);
166 #[cfg(not(target_os = "netbsd"))]
167 check_renamed_struct_field!(Stat, stat, st_ctime_nsec);
168 #[cfg(target_os = "netbsd")]
169 check_renamed_struct_renamed_field!(Stat, stat, st_ctime_nsec, st_ctimensec);
170 #[cfg(all(
171 linux_raw,
172 not(any(
173 target_arch = "aarch64",
174 target_arch = "powerpc64",
175 target_arch = "riscv64"
176 ))
177 ))]
178 check_renamed_struct_field!(Stat, stat, __unused);
179 #[cfg(all(linux_raw, not(any(target_arch = "s390x", target_arch = "x86_64"))))]
180 check_renamed_struct_field!(Stat, stat, __unused4);
181 #[cfg(all(linux_raw, not(any(target_arch = "s390x", target_arch = "x86_64"))))]
182 check_renamed_struct_field!(Stat, stat, __unused5);
183 #[cfg(all(
184 linux_raw,
185 not(any(
186 target_arch = "aarch64",
187 target_arch = "riscv64",
188 target_arch = "s390x",
189 target_arch = "x86_64"
190 ))
191 ))]
192 check_renamed_struct_field!(Stat, stat, __unused6);
193 }
194
195 #[cfg(all(
197 libc,
198 any(
199 all(linux_kernel, target_pointer_width = "64"),
200 target_os = "hurd",
201 target_os = "emscripten",
202 target_os = "l4re",
203 )
204 ))]
205 {
206 check_renamed_type!(Stat, stat64);
207 check_renamed_struct_field!(Stat, stat64, st_dev);
208 check_renamed_struct_field!(Stat, stat64, st_ino);
209 check_renamed_struct_field!(Stat, stat64, st_nlink);
210 check_renamed_struct_field!(Stat, stat64, st_mode);
211 check_renamed_struct_field!(Stat, stat64, st_uid);
212 check_renamed_struct_field!(Stat, stat64, st_gid);
213 #[cfg(all(
214 linux_raw,
215 not(any(
216 target_arch = "aarch64",
217 target_arch = "powerpc64",
218 target_arch = "riscv64",
219 target_arch = "s390x"
220 ))
221 ))]
222 check_renamed_struct_field!(Stat, stat64, __pad0);
223 check_renamed_struct_field!(Stat, stat64, st_rdev);
224 #[cfg(all(linux_raw, not(any(target_arch = "powerpc64", target_arch = "x86_64"))))]
225 check_renamed_struct_field!(Stat, stat64, __pad1);
226 check_renamed_struct_field!(Stat, stat64, st_size);
227 check_renamed_struct_field!(Stat, stat64, st_blksize);
228 #[cfg(all(
229 linux_raw,
230 not(any(
231 target_arch = "powerpc64",
232 target_arch = "s390x",
233 target_arch = "x86_64"
234 ))
235 ))]
236 check_renamed_struct_field!(Stat, stat64, __pad2);
237 check_renamed_struct_field!(Stat, stat64, st_blocks);
238 check_renamed_struct_field!(Stat, stat64, st_atime);
239 check_renamed_struct_field!(Stat, stat64, st_atime_nsec);
240 check_renamed_struct_field!(Stat, stat64, st_mtime);
241 check_renamed_struct_field!(Stat, stat64, st_mtime_nsec);
242 check_renamed_struct_field!(Stat, stat64, st_ctime);
243 check_renamed_struct_field!(Stat, stat64, st_ctime_nsec);
244 #[cfg(all(
245 linux_raw,
246 not(any(
247 target_arch = "aarch64",
248 target_arch = "powerpc64",
249 target_arch = "riscv64"
250 ))
251 ))]
252 check_renamed_struct_field!(Stat, stat64, __unused);
253 #[cfg(all(linux_raw, not(any(target_arch = "s390x", target_arch = "x86_64"))))]
254 check_renamed_struct_field!(Stat, stat64, __unused4);
255 #[cfg(all(linux_raw, not(any(target_arch = "s390x", target_arch = "x86_64"))))]
256 check_renamed_struct_field!(Stat, stat64, __unused5);
257 #[cfg(all(
258 linux_raw,
259 not(any(
260 target_arch = "aarch64",
261 target_arch = "riscv64",
262 target_arch = "s390x",
263 target_arch = "x86_64"
264 ))
265 ))]
266 check_renamed_struct_field!(Stat, stat64, __unused6);
267 }
268 }
269
270 #[cfg(not(any(
271 solarish,
272 target_os = "cygwin",
273 target_os = "haiku",
274 target_os = "nto",
275 target_os = "redox",
276 target_os = "wasi",
277 )))]
278 {
279 check_renamed_type!(Fsid, fsid_t);
280 #[cfg(not(libc))] check_renamed_struct_field!(Fsid, fsid_t, val);
282 }
283
284 #[cfg(linux_like)]
285 {
286 check_renamed_type!(StatFs, statfs64);
287 check_renamed_struct_field!(StatFs, statfs64, f_type);
288 check_renamed_struct_field!(StatFs, statfs64, f_bsize);
289 check_renamed_struct_field!(StatFs, statfs64, f_blocks);
290 check_renamed_struct_field!(StatFs, statfs64, f_bfree);
291 check_renamed_struct_field!(StatFs, statfs64, f_bavail);
292 check_renamed_struct_field!(StatFs, statfs64, f_files);
293 check_renamed_struct_field!(StatFs, statfs64, f_ffree);
294 check_renamed_struct_field!(StatFs, statfs64, f_fsid);
295 check_renamed_struct_field!(StatFs, statfs64, f_namelen);
296 check_renamed_struct_field!(StatFs, statfs64, f_frsize);
297 check_renamed_struct_field!(StatFs, statfs64, f_flags);
298 #[cfg(linux_raw)]
299 check_renamed_struct_field!(StatFs, statfs64, f_spare);
300 }
301
302 #[cfg(all(target_os = "linux", target_env = "gnu"))]
304 {
305 check_renamed_type!(StatxTimestamp, statx_timestamp);
306 check_renamed_struct_field!(StatxTimestamp, statx_timestamp, tv_sec);
307 check_renamed_struct_field!(StatxTimestamp, statx_timestamp, tv_nsec);
308 #[cfg(linux_raw)]
309 check_renamed_struct_field!(StatxTimestamp, statx_timestamp, __reserved);
310
311 check_renamed_type!(Statx, statx);
312 check_renamed_struct_field!(Statx, statx, stx_mask);
313 check_renamed_struct_field!(Statx, statx, stx_blksize);
314 check_renamed_struct_field!(Statx, statx, stx_attributes);
315 check_renamed_struct_field!(Statx, statx, stx_nlink);
316 check_renamed_struct_field!(Statx, statx, stx_uid);
317 check_renamed_struct_field!(Statx, statx, stx_gid);
318 check_renamed_struct_field!(Statx, statx, stx_mode);
319 #[cfg(linux_raw)]
320 check_renamed_struct_field!(Statx, statx, __spare0);
321 check_renamed_struct_field!(Statx, statx, stx_ino);
322 check_renamed_struct_field!(Statx, statx, stx_size);
323 check_renamed_struct_field!(Statx, statx, stx_blocks);
324 check_renamed_struct_field!(Statx, statx, stx_attributes_mask);
325 check_renamed_struct_field!(Statx, statx, stx_atime);
326 check_renamed_struct_field!(Statx, statx, stx_btime);
327 check_renamed_struct_field!(Statx, statx, stx_ctime);
328 check_renamed_struct_field!(Statx, statx, stx_mtime);
329 check_renamed_struct_field!(Statx, statx, stx_rdev_major);
330 check_renamed_struct_field!(Statx, statx, stx_rdev_minor);
331 check_renamed_struct_field!(Statx, statx, stx_dev_major);
332 check_renamed_struct_field!(Statx, statx, stx_dev_minor);
333 check_renamed_struct_field!(Statx, statx, stx_mnt_id);
334 check_renamed_struct_field!(Statx, statx, stx_dio_mem_align);
335 check_renamed_struct_field!(Statx, statx, stx_dio_offset_align);
336 #[cfg(not(libc))] check_renamed_struct_field!(Statx, statx, stx_subvol);
338 #[cfg(not(libc))] check_renamed_struct_field!(Statx, statx, stx_atomic_write_unit_min);
340 #[cfg(not(libc))] check_renamed_struct_field!(Statx, statx, stx_atomic_write_unit_max);
342 #[cfg(not(libc))] check_renamed_struct_field!(Statx, statx, stx_atomic_write_segments_max);
344 #[cfg(linux_raw)]
345 check_renamed_struct_field!(Statx, statx, __spare1);
346 #[cfg(linux_raw)]
347 check_renamed_struct_field!(Statx, statx, __spare3);
348 }
349 }
350}