Skip to main content

c/
statvfs.rs

1// Copyright (c) 2026 Meng Zhuo
2// SPDX-License-Identifier: BSD-3-Clause
3
4#[repr(C)]
5struct kernel_statfs {
6    f_type: c_ulong,
7    f_bsize: c_ulong,
8    f_blocks: u64,
9    f_bfree: u64,
10    f_bavail: u64,
11    f_files: u64,
12    f_ffree: u64,
13    f_fsid: [c_int; 2],
14    f_namelen: c_ulong,
15    f_frsize: c_ulong,
16    f_flags: c_ulong,
17    f_spare: [c_ulong; 4],
18}
19
20#[repr(C)]
21pub struct statvfs {
22    pub f_bsize: c_ulong,
23    pub f_frsize: c_ulong,
24    pub f_blocks: u64,
25    pub f_bfree: u64,
26    pub f_bavail: u64,
27    pub f_files: u64,
28    pub f_ffree: u64,
29    pub f_favail: u64,
30    pub f_fsid: c_ulong,
31    pub f_flag: c_ulong,
32    pub f_namemax: c_ulong,
33    pub f_type: c_uint,
34    __reserved: [c_int; 5],
35}
36
37pub const ST_RDONLY: c_ulong = 1;
38pub const ST_NOSUID: c_ulong = 2;
39pub const ST_NODEV: c_ulong = 4;
40pub const ST_NOEXEC: c_ulong = 8;
41pub const ST_SYNCHRONOUS: c_ulong = 16;
42pub const ST_MANDLOCK: c_ulong = 64;
43pub const ST_WRITE: c_ulong = 128;
44pub const ST_APPEND: c_ulong = 256;
45pub const ST_IMMUTABLE: c_ulong = 512;
46pub const ST_NOATIME: c_ulong = 1024;
47pub const ST_NODIRATIME: c_ulong = 2048;
48pub const ST_RELATIME: c_ulong = 4096;
49
50// The kernel's statfs structure is already 64-bit on all supported
51// architectures, so the separate statfs64 syscall is not needed.
52#[inline]
53unsafe fn sys_statfs(path: *const c_char, buf: *mut kernel_statfs) -> i64 {
54    <Arch as Syscalls>::syscall2(SYS_STATFS, path as i64, buf as i64)
55}
56
57#[inline]
58unsafe fn sys_fstatfs(fd: c_int, buf: *mut kernel_statfs) -> i64 {
59    <Arch as Syscalls>::syscall2(SYS_FSTATFS, fd as i64, buf as i64)
60}
61
62unsafe fn fixup(out: *mut statvfs, kbuf: *const kernel_statfs) {
63    core::ptr::write_bytes(out, 0, 1);
64    (*out).f_bsize = (*kbuf).f_bsize;
65    (*out).f_frsize = if (*kbuf).f_frsize != 0 { (*kbuf).f_frsize } else { (*kbuf).f_bsize };
66    (*out).f_blocks = (*kbuf).f_blocks;
67    (*out).f_bfree = (*kbuf).f_bfree;
68    (*out).f_bavail = (*kbuf).f_bavail;
69    (*out).f_files = (*kbuf).f_files;
70    (*out).f_ffree = (*kbuf).f_ffree;
71    (*out).f_favail = (*kbuf).f_ffree;
72    (*out).f_fsid = (*kbuf).f_fsid[0] as c_ulong;
73    (*out).f_flag = (*kbuf).f_flags;
74    (*out).f_namemax = (*kbuf).f_namelen;
75    (*out).f_type = (*kbuf).f_type as c_uint;
76}
77
78#[no_mangle]
79pub unsafe extern "C" fn statvfs(path: *const c_char, buf: *mut statvfs) -> c_int {
80    let mut kbuf: kernel_statfs = core::mem::zeroed();
81    let r = sys_statfs(path, &mut kbuf);
82    if r < 0 {
83        ERRNO!() = (-r) as c_int;
84        return -1;
85    }
86    fixup(buf, &kbuf);
87    0
88}
89
90#[no_mangle]
91pub unsafe extern "C" fn fstatvfs(fd: c_int, buf: *mut statvfs) -> c_int {
92    let mut kbuf: kernel_statfs = core::mem::zeroed();
93    let r = sys_fstatfs(fd, &mut kbuf);
94    if r < 0 {
95        ERRNO!() = (-r) as c_int;
96        return -1;
97    }
98    fixup(buf, &kbuf);
99    0
100}