Skip to main content

relibc/header/sys_statvfs/
mod.rs

1//! `sys_statvfs.h` implementation.
2//!
3//! See <https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/sys_statvfs.h.html>.
4
5use crate::{
6    c_str::CStr,
7    error::ResultExt,
8    header::fcntl::O_PATH,
9    out::Out,
10    platform::{
11        Pal, Sys,
12        types::{c_char, c_int, c_ulong, fsblkcnt_t, fsfilcnt_t},
13    },
14};
15
16//pub const ST_RDONLY
17//pub const ST_NOSUID
18
19#[repr(C)]
20#[derive(Default)]
21pub struct statvfs {
22    pub f_bsize: c_ulong,
23    pub f_frsize: c_ulong,
24    pub f_blocks: fsblkcnt_t,
25    pub f_bfree: fsblkcnt_t,
26    pub f_bavail: fsblkcnt_t,
27    pub f_files: fsfilcnt_t,
28    pub f_ffree: fsfilcnt_t,
29    pub f_favail: fsfilcnt_t,
30    pub f_fsid: c_ulong,
31    pub f_flag: c_ulong,
32    pub f_namemax: c_ulong,
33}
34
35/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/fstatvfs.html>.
36#[unsafe(no_mangle)]
37pub unsafe extern "C" fn fstatvfs(fildes: c_int, buf: *mut statvfs) -> c_int {
38    let buf = unsafe { Out::nonnull(buf) };
39    Sys::fstatvfs(fildes, buf).map(|()| 0).or_minus_one_errno()
40}
41
42/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/fstatvfs.html>.
43#[unsafe(no_mangle)]
44pub unsafe extern "C" fn statvfs(file: *const c_char, buf: *mut statvfs) -> c_int {
45    let file = unsafe { CStr::from_ptr(file) };
46    let buf = unsafe { Out::nonnull(buf) };
47    // TODO: Rustify
48    let fd = Sys::open(file, O_PATH, 0).or_minus_one_errno();
49    if fd < 0 {
50        return -1;
51    }
52
53    let res = Sys::fstatvfs(fd, buf).map(|()| 0).or_minus_one_errno();
54
55    if let Ok(()) = Sys::close(fd) {}; // TODO handle error
56
57    res
58}