1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
use super::to_cstring;
use fstype::FilesystemType;
use libc::*;
use std::{
ffi::{CString, OsStr},
io,
os::unix::ffi::OsStrExt,
path::Path,
ptr,
};
use umount::{unmount_, Unmount, UnmountDrop, UnmountFlags};
bitflags! {
pub struct MountFlags: c_ulong {
const BIND = MS_BIND;
const DIRSYNC = MS_DIRSYNC;
const MANDLOCK = MS_MANDLOCK;
const MOVE = MS_MOVE;
const NOATIME = MS_NOATIME;
const NODEV = MS_NODEV;
const NODIRATIME = MS_NODIRATIME;
const NOEXEC = MS_NOEXEC;
const NOSUID = MS_NOSUID;
const RDONLY = MS_RDONLY;
const RELATIME = MS_RELATIME;
const REMOUNT = MS_REMOUNT;
const SILENT = MS_SILENT;
const STRICTATIME = MS_STRICTATIME;
const SYNCHRONOUS = MS_SYNCHRONOUS;
}
}
#[derive(Debug)]
pub struct Mount {
pub(crate) target: CString,
pub(crate) fstype: String,
#[cfg(feature = "loop")]
loopback: Option<loopdev::LoopDevice>,
#[cfg(feature = "loop")]
loop_path: Option<std::path::PathBuf>,
}
impl Unmount for Mount {
fn unmount(&self, flags: UnmountFlags) -> io::Result<()> {
unsafe {
unmount_(self.target.as_ptr(), flags)?;
}
#[cfg(feature = "loop")]
if let Some(ref loopback) = self.loopback {
loopback.detach()?;
}
Ok(())
}
}
impl Mount {
#[cfg_attr(not(feature = "loop"), allow(unused_mut))]
pub fn new<'a, S, T, F>(
source: S,
target: T,
fstype: F,
mut flags: MountFlags,
data: Option<&str>,
) -> io::Result<Self>
where
S: AsRef<Path>,
T: AsRef<Path>,
F: Into<FilesystemType<'a>>,
{
let mut fstype = fstype.into();
let source = source.as_ref();
let mut c_source = None;
#[cfg(feature = "loop")]
let mut loopback = None;
#[cfg(feature = "loop")]
let mut loop_path = None;
if !source.as_os_str().is_empty() {
#[cfg(feature = "loop")]
if let Some(ext) = source.extension() {
let extf = if ext == "iso" { 1 } else { 0 } | if ext == "squashfs" { 2 } else { 0 };
if extf != 0 {
fstype = if extf == 1 {
flags |= MountFlags::RDONLY;
FilesystemType::Manual("iso9660")
} else {
flags |= MountFlags::RDONLY;
FilesystemType::Manual("squashfs")
};
}
let new_loopback = loopdev::LoopControl::open()?.next_free()?;
new_loopback.attach_file(source)?;
let path = new_loopback.path().expect("loopback does not have path");
c_source = Some(to_cstring(path.as_os_str().as_bytes())?);
loop_path = Some(path);
loopback = Some(new_loopback);
}
if c_source.is_none() {
c_source = Some(to_cstring(source.as_os_str().as_bytes())?);
}
};
let c_target = to_cstring(target.as_ref().as_os_str().as_bytes())?;
let c_data = match data.map(|o| to_cstring(o.as_bytes())) {
Some(Ok(string)) => Some(string),
Some(Err(why)) => return Err(why),
None => None,
};
let data = c_data.as_ref().map_or(ptr::null(), |d| d.as_ptr()) as *const c_void;
let mut mount_data = MountData { c_source, c_target, flags, data };
let mut res = match fstype {
FilesystemType::Auto(supported) => mount_data.automount(supported.dev_file_systems()),
FilesystemType::Set(set) => mount_data.automount(set.iter().cloned()),
FilesystemType::Manual(fstype) => mount_data.mount(fstype),
};
#[cfg(feature = "loop")]
match res {
Ok(ref mut mount) => {
mount.loopback = loopback;
mount.loop_path = loop_path;
}
Err(_) => {
if let Some(loopback) = loopback {
let _ = loopback.detach();
}
}
}
res
}
#[cfg(feature = "loop")]
pub fn backing_loop_device(&self) -> Option<&Path> {
self.loop_path.as_ref().map(|p| p.as_path())
}
pub fn get_fstype(&self) -> &str { &self.fstype }
pub fn target_path(&self) -> &Path {
Path::new(OsStr::from_bytes(self.target.as_bytes()))
}
#[cfg(feature = "loop")]
fn from_target_and_fstype(target: CString, fstype: String) -> Self {
Mount {
target: target,
fstype: fstype,
loopback: None,
loop_path: None,
}
}
#[cfg(not(feature = "loop"))]
fn from_target_and_fstype(target: CString, fstype: String) -> Self {
Mount { target: target, fstype: fstype }
}
}
struct MountData {
c_source: Option<CString>,
c_target: CString,
flags: MountFlags,
data: *const c_void,
}
impl MountData {
fn mount(&mut self, fstype: &str) -> io::Result<Mount> {
let c_fstype = to_cstring(fstype.as_bytes())?;
match mount_(self.c_source.as_ref(), &self.c_target, &c_fstype, self.flags, self.data) {
Ok(()) => Ok(Mount::from_target_and_fstype(self.c_target.clone(), fstype.to_owned())),
Err(why) => Err(why),
}
}
fn automount<'a, I: Iterator<Item = &'a str> + 'a>(mut self, iter: I) -> io::Result<Mount> {
let mut res = Ok(());
for fstype in iter {
match self.mount(fstype) {
mount @ Ok(_) => return mount,
Err(why) => res = Err(why),
}
}
match res {
Ok(()) => {
Err(io::Error::new(io::ErrorKind::NotFound, "no supported file systems found"))
}
Err(why) => Err(why),
}
}
}
fn mount_(
c_source: Option<&CString>,
c_target: &CString,
c_fstype: &CString,
flags: MountFlags,
c_data: *const c_void,
) -> io::Result<()> {
let result = unsafe {
mount(
c_source.map_or_else(ptr::null, |s| s.as_ptr()),
c_target.as_ptr(),
c_fstype.as_ptr(),
flags.bits(),
c_data,
)
};
match result {
0 => Ok(()),
_err => Err(io::Error::last_os_error()),
}
}
pub struct Mounts(pub Vec<UnmountDrop<Mount>>);
impl Mounts {
#[cfg_attr(rustfmt, rustfmt_skip)]
pub fn unmount(&mut self, lazy: bool) -> io::Result<()> {
let flags = if lazy { UnmountFlags::DETACH } else { UnmountFlags::empty() };
self.0.iter_mut().rev().map(|mount| mount.unmount(flags)).collect()
}
}
impl Drop for Mounts {
fn drop(&mut self) {
for mount in self.0.drain(..).rev() {
drop(mount);
}
}
}