| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
use std::io::Error; |
| 5 |
|
| 6 |
const MNT_NOWAIT: libc::c_int = 2; |
| 7 |
|
| 8 |
|
| 9 |
unsafe extern "C" { |
| 10 |
fn getfsstat64(buf: *mut libc::statfs, bufsize: libc::c_int, flags: libc::c_int) |
| 11 |
-> libc::c_int; |
| 12 |
} |
| 13 |
|
| 14 |
|
| 15 |
pub(crate) fn mounts() -> anyhow::Result<Vec<libc::statfs>> { |
| 16 |
|
| 17 |
let expected_len = unsafe { getfsstat64(std::ptr::null_mut(), 0, MNT_NOWAIT) }; |
| 18 |
|
| 19 |
let mut mounts: Vec<libc::statfs> = Vec::with_capacity(expected_len as usize); |
| 20 |
|
| 21 |
|
| 22 |
let result = unsafe { |
| 23 |
getfsstat64( |
| 24 |
mounts.as_mut_ptr(), |
| 25 |
std::mem::size_of::<libc::statfs>() as libc::c_int * expected_len, |
| 26 |
MNT_NOWAIT, |
| 27 |
) |
| 28 |
}; |
| 29 |
|
| 30 |
if result == -1 { |
| 31 |
Err(anyhow::Error::from(Error::last_os_error()).context("getfsstat64")) |
| 32 |
} else { |
| 33 |
debug_assert_eq!( |
| 34 |
expected_len, result, |
| 35 |
"Expected {expected_len} statfs entries, but instead got {result} entries", |
| 36 |
); |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
unsafe { |
| 44 |
mounts.set_len(result as usize); |
| 45 |
} |
| 46 |
Ok(mounts) |
| 47 |
} |
| 48 |
} |