Debian Codegraph @

debian/missing-sources/rustix/src/backend/libc/mount/syscalls.rs

raw
1 use crate::backend::c;
2 use crate::backend::conv::{borrowed_fd, c_str, ret, ret_owned_fd};
3 use crate::fd::{BorrowedFd, OwnedFd};
4 use crate::ffi::CStr;
5 use crate::io;
6 use core::ptr::null;
7
8 #[cfg(linux_kernel)]
9 pub(crate) fn mount(
10 source: Option<&CStr>,
11 target: &CStr,
12 file_system_type: Option<&CStr>,
13 flags: super::types::MountFlagsArg,
14 data: Option<&CStr>,
15 ) -> io::Result<()> {
16 unsafe {
17 ret(c::mount(
18 source.map_or_else(null, CStr::as_ptr),
19 target.as_ptr(),
20 file_system_type.map_or_else(null, CStr::as_ptr),
21 flags.0,
22 data.map_or_else(null, CStr::as_ptr).cast(),
23 ))
24 }
25 }
26
27 #[cfg(linux_kernel)]
28 pub(crate) fn unmount(target: &CStr, flags: super::types::UnmountFlags) -> io::Result<()> {
29 unsafe { ret(c::umount2(target.as_ptr(), bitflags_bits!(flags))) }
30 }
31
32 #[cfg(linux_kernel)]
33 pub(crate) fn fsopen(fs_name: &CStr, flags: super::types::FsOpenFlags) -> io::Result<OwnedFd> {
34 syscall! {
35 fn fsopen(
36 fs_name: *const c::c_char,
37 flags: c::c_uint
38 ) via SYS_fsopen -> c::c_int
39 }
40 unsafe { ret_owned_fd(fsopen(c_str(fs_name), flags.bits())) }
41 }
42
43 #[cfg(linux_kernel)]
44 pub(crate) fn fsmount(
45 fs_fd: BorrowedFd<'_>,
46 flags: super::types::FsMountFlags,
47 attr_flags: super::types::MountAttrFlags,
48 ) -> io::Result<OwnedFd> {
49 syscall! {
50 fn fsmount(
51 fs_fd: c::c_int,
52 flags: c::c_uint,
53 attr_flags: c::c_uint
54 ) via SYS_fsmount -> c::c_int
55 }
56 unsafe { ret_owned_fd(fsmount(borrowed_fd(fs_fd), flags.bits(), attr_flags.bits())) }
57 }
58
59 #[cfg(linux_kernel)]
60 pub(crate) fn move_mount(
61 from_dfd: BorrowedFd<'_>,
62 from_pathname: &CStr,
63 to_dfd: BorrowedFd<'_>,
64 to_pathname: &CStr,
65 flags: super::types::MoveMountFlags,
66 ) -> io::Result<()> {
67 syscall! {
68 fn move_mount(
69 from_dfd: c::c_int,
70 from_pathname: *const c::c_char,
71 to_dfd: c::c_int,
72 to_pathname: *const c::c_char,
73 flags: c::c_uint
74 ) via SYS_move_mount -> c::c_int
75 }
76 unsafe {
77 ret(move_mount(
78 borrowed_fd(from_dfd),
79 c_str(from_pathname),
80 borrowed_fd(to_dfd),
81 c_str(to_pathname),
82 flags.bits(),
83 ))
84 }
85 }
86
87 #[cfg(linux_kernel)]
88 pub(crate) fn open_tree(
89 dfd: BorrowedFd<'_>,
90 filename: &CStr,
91 flags: super::types::OpenTreeFlags,
92 ) -> io::Result<OwnedFd> {
93 syscall! {
94 fn open_tree(
95 dfd: c::c_int,
96 filename: *const c::c_char,
97 flags: c::c_uint
98 ) via SYS_open_tree -> c::c_int
99 }
100
101 unsafe { ret_owned_fd(open_tree(borrowed_fd(dfd), c_str(filename), flags.bits())) }
102 }
103
104 #[cfg(linux_kernel)]
105 pub(crate) fn fspick(
106 dfd: BorrowedFd<'_>,
107 path: &CStr,
108 flags: super::types::FsPickFlags,
109 ) -> io::Result<OwnedFd> {
110 syscall! {
111 fn fspick(
112 dfd: c::c_int,
113 path: *const c::c_char,
114 flags: c::c_uint
115 ) via SYS_fspick -> c::c_int
116 }
117
118 unsafe { ret_owned_fd(fspick(borrowed_fd(dfd), c_str(path), flags.bits())) }
119 }
120
121 #[cfg(linux_kernel)]
122 syscall! {
123 fn fsconfig(
124 fs_fd: c::c_int,
125 cmd: c::c_uint,
126 key: *const c::c_char,
127 val: *const c::c_char,
128 aux: c::c_int
129 ) via SYS_fsconfig -> c::c_int
130 }
131
132 #[cfg(linux_kernel)]
133 pub(crate) fn fsconfig_set_flag(fs_fd: BorrowedFd<'_>, key: &CStr) -> io::Result<()> {
134 unsafe {
135 ret(fsconfig(
136 borrowed_fd(fs_fd),
137 super::types::FsConfigCmd::SetFlag as _,
138 c_str(key),
139 null(),
140 0,
141 ))
142 }
143 }
144
145 #[cfg(linux_kernel)]
146 pub(crate) fn fsconfig_set_string(
147 fs_fd: BorrowedFd<'_>,
148 key: &CStr,
149 value: &CStr,
150 ) -> io::Result<()> {
151 unsafe {
152 ret(fsconfig(
153 borrowed_fd(fs_fd),
154 super::types::FsConfigCmd::SetString as _,
155 c_str(key),
156 c_str(value),
157 0,
158 ))
159 }
160 }
161
162 #[cfg(linux_kernel)]
163 pub(crate) fn fsconfig_set_binary(
164 fs_fd: BorrowedFd<'_>,
165 key: &CStr,
166 value: &[u8],
167 ) -> io::Result<()> {
168 unsafe {
169 ret(fsconfig(
170 borrowed_fd(fs_fd),
171 super::types::FsConfigCmd::SetBinary as _,
172 c_str(key),
173 value.as_ptr().cast(),
174 value.len().try_into().map_err(|_| io::Errno::OVERFLOW)?,
175 ))
176 }
177 }
178
179 #[cfg(linux_kernel)]
180 pub(crate) fn fsconfig_set_fd(
181 fs_fd: BorrowedFd<'_>,
182 key: &CStr,
183 fd: BorrowedFd<'_>,
184 ) -> io::Result<()> {
185 unsafe {
186 ret(fsconfig(
187 borrowed_fd(fs_fd),
188 super::types::FsConfigCmd::SetFd as _,
189 c_str(key),
190 null(),
191 borrowed_fd(fd),
192 ))
193 }
194 }
195
196 #[cfg(linux_kernel)]
197 pub(crate) fn fsconfig_set_path(
198 fs_fd: BorrowedFd<'_>,
199 key: &CStr,
200 path: &CStr,
201 fd: BorrowedFd<'_>,
202 ) -> io::Result<()> {
203 unsafe {
204 ret(fsconfig(
205 borrowed_fd(fs_fd),
206 super::types::FsConfigCmd::SetPath as _,
207 c_str(key),
208 c_str(path),
209 borrowed_fd(fd),
210 ))
211 }
212 }
213
214 #[cfg(linux_kernel)]
215 pub(crate) fn fsconfig_set_path_empty(
216 fs_fd: BorrowedFd<'_>,
217 key: &CStr,
218 fd: BorrowedFd<'_>,
219 ) -> io::Result<()> {
220 unsafe {
221 ret(fsconfig(
222 borrowed_fd(fs_fd),
223 super::types::FsConfigCmd::SetPathEmpty as _,
224 c_str(key),
225 c_str(cstr!("")),
226 borrowed_fd(fd),
227 ))
228 }
229 }
230
231 #[cfg(linux_kernel)]
232 pub(crate) fn fsconfig_create(fs_fd: BorrowedFd<'_>) -> io::Result<()> {
233 unsafe {
234 ret(fsconfig(
235 borrowed_fd(fs_fd),
236 super::types::FsConfigCmd::Create as _,
237 null(),
238 null(),
239 0,
240 ))
241 }
242 }
243
244 #[cfg(linux_kernel)]
245 pub(crate) fn fsconfig_reconfigure(fs_fd: BorrowedFd<'_>) -> io::Result<()> {
246 unsafe {
247 ret(fsconfig(
248 borrowed_fd(fs_fd),
249 super::types::FsConfigCmd::Reconfigure as _,
250 null(),
251 null(),
252 0,
253 ))
254 }
255 }
256
257 #[cfg(linux_kernel)]
258 pub(crate) fn fsconfig_create_excl(fs_fd: BorrowedFd<'_>) -> io::Result<()> {
259 unsafe {
260 ret(fsconfig(
261 borrowed_fd(fs_fd),
262 super::types::FsConfigCmd::CreateExclusive as _,
263 null(),
264 null(),
265 0,
266 ))
267 }
268 }