Debian Codegraph @

src/collection/disks/unix/other/bindings.rs

raw symbols SCIP
1 //! Based on [heim's](https://github.com/heim-rs/heim/blob/master/heim-disk/src/sys/unix/bindings/mod.rs)
2 //! implementation.
3
4 use std::io::Error;
5
6 const MNT_NOWAIT: libc::c_int = 2;
7
8 // SAFETY: Bindings like this are inherently unsafe.
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 /// Returns all the mounts on the system at the moment.
15 pub(crate) fn mounts() -> anyhow::Result<Vec<libc::statfs>> {
16 // SAFETY: System API FFI call, arguments should be correct.
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 // SAFETY: System API FFI call, arguments should be correct.
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 // SAFETY: We have a debug assert check, and if `result` is not correct (-1), we
39 // check against it. Otherwise, getfsstat64 should return the number of
40 // statfs structures if it succeeded.
41 //
42 // Source: https://man.freebsd.org/cgi/man.cgi?query=getfsstat&sektion=2&format=html
43 unsafe {
44 mounts.set_len(result as usize);
45 }
46 Ok(mounts)
47 }
48 }