nix/sys/
statvfs.rs

1//! Get filesystem statistics
2//!
3//! See [the man pages](https://pubs.opengroup.org/onlinepubs/9699919799/functions/fstatvfs.html)
4//! for more details.
5use std::mem;
6use std::os::unix::io::AsRawFd;
7
8use libc::{self, c_ulong};
9
10use crate::{Result, NixPath, errno::Errno};
11
12#[cfg(not(target_os = "redox"))]
13libc_bitflags!(
14    /// File system mount Flags
15    #[repr(C)]
16    #[derive(Default)]
17    pub struct FsFlags: c_ulong {
18        /// Read Only
19        ST_RDONLY;
20        /// Do not allow the set-uid bits to have an effect
21        ST_NOSUID;
22        /// Do not interpret character or block-special devices
23        #[cfg(any(target_os = "android", target_os = "linux"))]
24        #[cfg_attr(docsrs, doc(cfg(all())))]
25        ST_NODEV;
26        /// Do not allow execution of binaries on the filesystem
27        #[cfg(any(target_os = "android", target_os = "linux"))]
28        #[cfg_attr(docsrs, doc(cfg(all())))]
29        ST_NOEXEC;
30        /// All IO should be done synchronously
31        #[cfg(any(target_os = "android", target_os = "linux"))]
32        #[cfg_attr(docsrs, doc(cfg(all())))]
33        ST_SYNCHRONOUS;
34        /// Allow mandatory locks on the filesystem
35        #[cfg(any(target_os = "android", target_os = "linux"))]
36        #[cfg_attr(docsrs, doc(cfg(all())))]
37        ST_MANDLOCK;
38        /// Write on file/directory/symlink
39        #[cfg(target_os = "linux")]
40        #[cfg_attr(docsrs, doc(cfg(all())))]
41        ST_WRITE;
42        /// Append-only file
43        #[cfg(target_os = "linux")]
44        #[cfg_attr(docsrs, doc(cfg(all())))]
45        ST_APPEND;
46        /// Immutable file
47        #[cfg(target_os = "linux")]
48        #[cfg_attr(docsrs, doc(cfg(all())))]
49        ST_IMMUTABLE;
50        /// Do not update access times on files
51        #[cfg(any(target_os = "android", target_os = "linux"))]
52        #[cfg_attr(docsrs, doc(cfg(all())))]
53        ST_NOATIME;
54        /// Do not update access times on files
55        #[cfg(any(target_os = "android", target_os = "linux"))]
56        #[cfg_attr(docsrs, doc(cfg(all())))]
57        ST_NODIRATIME;
58        /// Update access time relative to modify/change time
59        #[cfg(any(target_os = "android", all(target_os = "linux", not(target_env = "musl"))))]
60        #[cfg_attr(docsrs, doc(cfg(all())))]
61        ST_RELATIME;
62    }
63);
64
65/// Wrapper around the POSIX `statvfs` struct
66///
67/// For more information see the [`statvfs(3)` man pages](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_statvfs.h.html).
68#[repr(transparent)]
69#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
70pub struct Statvfs(libc::statvfs);
71
72impl Statvfs {
73    /// get the file system block size
74    pub fn block_size(&self) -> c_ulong {
75        self.0.f_bsize
76    }
77
78    /// Get the fundamental file system block size
79    pub fn fragment_size(&self) -> c_ulong {
80        self.0.f_frsize
81    }
82
83    /// Get the number of blocks.
84    ///
85    /// Units are in units of `fragment_size()`
86    pub fn blocks(&self) -> libc::fsblkcnt_t {
87        self.0.f_blocks
88    }
89
90    /// Get the number of free blocks in the file system
91    pub fn blocks_free(&self) -> libc::fsblkcnt_t {
92        self.0.f_bfree
93    }
94
95    /// Get the number of free blocks for unprivileged users
96    pub fn blocks_available(&self) -> libc::fsblkcnt_t {
97        self.0.f_bavail
98    }
99
100    /// Get the total number of file inodes
101    pub fn files(&self) -> libc::fsfilcnt_t {
102        self.0.f_files
103    }
104
105    /// Get the number of free file inodes
106    pub fn files_free(&self) -> libc::fsfilcnt_t {
107        self.0.f_ffree
108    }
109
110    /// Get the number of free file inodes for unprivileged users
111    pub fn files_available(&self) -> libc::fsfilcnt_t {
112        self.0.f_favail
113    }
114
115    /// Get the file system id
116    pub fn filesystem_id(&self) -> c_ulong {
117        self.0.f_fsid
118    }
119
120    /// Get the mount flags
121    #[cfg(not(target_os = "redox"))]
122    #[cfg_attr(docsrs, doc(cfg(all())))]
123    pub fn flags(&self) -> FsFlags {
124        FsFlags::from_bits_truncate(self.0.f_flag)
125    }
126
127    /// Get the maximum filename length
128    pub fn name_max(&self) -> c_ulong {
129        self.0.f_namemax
130    }
131
132}
133
134/// Return a `Statvfs` object with information about the `path`
135pub fn statvfs<P: ?Sized + NixPath>(path: &P) -> Result<Statvfs> {
136    unsafe {
137        Errno::clear();
138        let mut stat = mem::MaybeUninit::<libc::statvfs>::uninit();
139        let res = path.with_nix_path(|path|
140            libc::statvfs(path.as_ptr(), stat.as_mut_ptr())
141        )?;
142
143        Errno::result(res).map(|_| Statvfs(stat.assume_init()))
144    }
145}
146
147/// Return a `Statvfs` object with information about `fd`
148pub fn fstatvfs<T: AsRawFd>(fd: &T) -> Result<Statvfs> {
149    unsafe {
150        Errno::clear();
151        let mut stat = mem::MaybeUninit::<libc::statvfs>::uninit();
152        Errno::result(libc::fstatvfs(fd.as_raw_fd(), stat.as_mut_ptr()))
153            .map(|_| Statvfs(stat.assume_init()))
154    }
155}
156
157#[cfg(test)]
158mod test {
159    use std::fs::File;
160    use crate::sys::statvfs::*;
161
162    #[test]
163    fn statvfs_call() {
164        statvfs(&b"/"[..]).unwrap();
165    }
166
167    #[test]
168    fn fstatvfs_call() {
169        let root = File::open("/").unwrap();
170        fstatvfs(&root).unwrap();
171    }
172}