1use 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 #[repr(C)]
16 #[derive(Default)]
17 pub struct FsFlags: c_ulong {
18 ST_RDONLY;
20 ST_NOSUID;
22 #[cfg(any(target_os = "android", target_os = "linux"))]
24 #[cfg_attr(docsrs, doc(cfg(all())))]
25 ST_NODEV;
26 #[cfg(any(target_os = "android", target_os = "linux"))]
28 #[cfg_attr(docsrs, doc(cfg(all())))]
29 ST_NOEXEC;
30 #[cfg(any(target_os = "android", target_os = "linux"))]
32 #[cfg_attr(docsrs, doc(cfg(all())))]
33 ST_SYNCHRONOUS;
34 #[cfg(any(target_os = "android", target_os = "linux"))]
36 #[cfg_attr(docsrs, doc(cfg(all())))]
37 ST_MANDLOCK;
38 #[cfg(target_os = "linux")]
40 #[cfg_attr(docsrs, doc(cfg(all())))]
41 ST_WRITE;
42 #[cfg(target_os = "linux")]
44 #[cfg_attr(docsrs, doc(cfg(all())))]
45 ST_APPEND;
46 #[cfg(target_os = "linux")]
48 #[cfg_attr(docsrs, doc(cfg(all())))]
49 ST_IMMUTABLE;
50 #[cfg(any(target_os = "android", target_os = "linux"))]
52 #[cfg_attr(docsrs, doc(cfg(all())))]
53 ST_NOATIME;
54 #[cfg(any(target_os = "android", target_os = "linux"))]
56 #[cfg_attr(docsrs, doc(cfg(all())))]
57 ST_NODIRATIME;
58 #[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#[repr(transparent)]
69#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
70pub struct Statvfs(libc::statvfs);
71
72impl Statvfs {
73 pub fn block_size(&self) -> c_ulong {
75 self.0.f_bsize
76 }
77
78 pub fn fragment_size(&self) -> c_ulong {
80 self.0.f_frsize
81 }
82
83 pub fn blocks(&self) -> libc::fsblkcnt_t {
87 self.0.f_blocks
88 }
89
90 pub fn blocks_free(&self) -> libc::fsblkcnt_t {
92 self.0.f_bfree
93 }
94
95 pub fn blocks_available(&self) -> libc::fsblkcnt_t {
97 self.0.f_bavail
98 }
99
100 pub fn files(&self) -> libc::fsfilcnt_t {
102 self.0.f_files
103 }
104
105 pub fn files_free(&self) -> libc::fsfilcnt_t {
107 self.0.f_ffree
108 }
109
110 pub fn files_available(&self) -> libc::fsfilcnt_t {
112 self.0.f_favail
113 }
114
115 pub fn filesystem_id(&self) -> c_ulong {
117 self.0.f_fsid
118 }
119
120 #[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 pub fn name_max(&self) -> c_ulong {
129 self.0.f_namemax
130 }
131
132}
133
134pub 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
147pub 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}