yash_env/system/real/
file_system.rs1use super::super::{FileType, Gid, Mode, RawMode, Uid};
20use std::mem::MaybeUninit;
21
22impl FileType {
23 #[must_use]
24 pub(super) const fn from_raw(mode: RawMode) -> Self {
25 match mode & libc::S_IFMT {
26 libc::S_IFREG => Self::Regular,
27 libc::S_IFDIR => Self::Directory,
28 libc::S_IFLNK => Self::Symlink,
29 libc::S_IFIFO => Self::Fifo,
30 libc::S_IFBLK => Self::BlockDevice,
31 libc::S_IFCHR => Self::CharacterDevice,
32 libc::S_IFSOCK => Self::Socket,
33 _ => Self::Other,
34 }
35 }
36}
37
38#[derive(Clone, Debug)]
43#[repr(transparent)]
44pub struct Stat(MaybeUninit<libc::stat>);
45impl Stat {
49 #[must_use]
61 pub(super) const unsafe fn from_raw(stat: MaybeUninit<libc::stat>) -> Self {
62 Self(stat)
63 }
64}
65
66#[allow(clippy::unnecessary_cast)]
69impl super::super::Stat for Stat {
70 #[inline(always)]
71 fn dev(&self) -> u64 {
72 (unsafe { (*self.0.as_ptr()).st_dev }) as u64
73 }
74 #[inline(always)]
75 fn ino(&self) -> u64 {
76 (unsafe { (*self.0.as_ptr()).st_ino }) as u64
77 }
78 #[inline(always)]
79 fn mode(&self) -> Mode {
80 let raw_mode = unsafe { (*self.0.as_ptr()).st_mode };
81 Mode::from_bits_truncate(raw_mode)
82 }
83 #[inline(always)]
84 fn r#type(&self) -> FileType {
85 let raw_mode = unsafe { (*self.0.as_ptr()).st_mode };
86 FileType::from_raw(raw_mode)
87 }
88 #[inline(always)]
89 fn nlink(&self) -> u64 {
90 (unsafe { (*self.0.as_ptr()).st_nlink }) as u64
91 }
92 #[inline(always)]
93 fn uid(&self) -> Uid {
94 Uid(unsafe { (*self.0.as_ptr()).st_uid })
95 }
96 #[inline(always)]
97 fn gid(&self) -> Gid {
98 Gid(unsafe { (*self.0.as_ptr()).st_gid })
99 }
100 #[inline(always)]
101 fn size(&self) -> u64 {
102 (unsafe { (*self.0.as_ptr()).st_size }) as u64
103 }
104
105 fn is_regular_file(&self) -> bool {
106 let raw_mode = unsafe { (*self.0.as_ptr()).st_mode };
107 raw_mode & libc::S_IFMT == libc::S_IFREG
108 }
109 fn is_directory(&self) -> bool {
110 let raw_mode = unsafe { (*self.0.as_ptr()).st_mode };
111 raw_mode & libc::S_IFMT == libc::S_IFDIR
112 }
113 fn is_symlink(&self) -> bool {
114 let raw_mode = unsafe { (*self.0.as_ptr()).st_mode };
115 raw_mode & libc::S_IFMT == libc::S_IFLNK
116 }
117 fn is_fifo(&self) -> bool {
118 let raw_mode = unsafe { (*self.0.as_ptr()).st_mode };
119 raw_mode & libc::S_IFMT == libc::S_IFIFO
120 }
121 fn is_block_device(&self) -> bool {
122 let raw_mode = unsafe { (*self.0.as_ptr()).st_mode };
123 raw_mode & libc::S_IFMT == libc::S_IFBLK
124 }
125 fn is_character_device(&self) -> bool {
126 let raw_mode = unsafe { (*self.0.as_ptr()).st_mode };
127 raw_mode & libc::S_IFMT == libc::S_IFCHR
128 }
129 fn is_socket(&self) -> bool {
130 let raw_mode = unsafe { (*self.0.as_ptr()).st_mode };
131 raw_mode & libc::S_IFMT == libc::S_IFSOCK
132 }
133}