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 ST_NODEV;
25 #[cfg(any(target_os = "android", target_os = "linux"))]
27 ST_NOEXEC;
28 #[cfg(any(target_os = "android", target_os = "linux"))]
30 ST_SYNCHRONOUS;
31 #[cfg(any(target_os = "android", target_os = "linux"))]
33 ST_MANDLOCK;
34 #[cfg(target_os = "linux")]
36 ST_WRITE;
37 #[cfg(target_os = "linux")]
39 ST_APPEND;
40 #[cfg(target_os = "linux")]
42 ST_IMMUTABLE;
43 #[cfg(any(target_os = "android", target_os = "linux"))]
45 ST_NOATIME;
46 #[cfg(any(target_os = "android", target_os = "linux"))]
48 ST_NODIRATIME;
49 #[cfg(any(target_os = "android", all(target_os = "linux", not(target_env = "musl"))))]
51 ST_RELATIME;
52 }
53);
54
55#[repr(transparent)]
59#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
60pub struct Statvfs(libc::statvfs);
61
62impl Statvfs {
63 pub fn block_size(&self) -> c_ulong {
65 self.0.f_bsize
66 }
67
68 pub fn fragment_size(&self) -> c_ulong {
70 self.0.f_frsize
71 }
72
73 pub fn blocks(&self) -> libc::fsblkcnt_t {
77 self.0.f_blocks
78 }
79
80 pub fn blocks_free(&self) -> libc::fsblkcnt_t {
82 self.0.f_bfree
83 }
84
85 pub fn blocks_available(&self) -> libc::fsblkcnt_t {
87 self.0.f_bavail
88 }
89
90 pub fn files(&self) -> libc::fsfilcnt_t {
92 self.0.f_files
93 }
94
95 pub fn files_free(&self) -> libc::fsfilcnt_t {
97 self.0.f_ffree
98 }
99
100 pub fn files_available(&self) -> libc::fsfilcnt_t {
102 self.0.f_favail
103 }
104
105 pub fn filesystem_id(&self) -> c_ulong {
107 self.0.f_fsid
108 }
109
110 #[cfg(not(target_os = "redox"))]
112 pub fn flags(&self) -> FsFlags {
113 FsFlags::from_bits_truncate(self.0.f_flag)
114 }
115
116 pub fn name_max(&self) -> c_ulong {
118 self.0.f_namemax
119 }
120
121}
122
123pub fn statvfs<P: ?Sized + NixPath>(path: &P) -> Result<Statvfs> {
125 unsafe {
126 Errno::clear();
127 let mut stat = mem::MaybeUninit::<libc::statvfs>::uninit();
128 let res = path.with_nix_path(|path|
129 libc::statvfs(path.as_ptr(), stat.as_mut_ptr())
130 )?;
131
132 Errno::result(res).map(|_| Statvfs(stat.assume_init()))
133 }
134}
135
136pub fn fstatvfs<T: AsRawFd>(fd: &T) -> Result<Statvfs> {
138 unsafe {
139 Errno::clear();
140 let mut stat = mem::MaybeUninit::<libc::statvfs>::uninit();
141 Errno::result(libc::fstatvfs(fd.as_raw_fd(), stat.as_mut_ptr()))
142 .map(|_| Statvfs(stat.assume_init()))
143 }
144}
145
146#[cfg(test)]
147mod test {
148 use std::fs::File;
149 use crate::sys::statvfs::*;
150
151 #[test]
152 fn statvfs_call() {
153 statvfs(&b"/"[..]).unwrap();
154 }
155
156 #[test]
157 fn fstatvfs_call() {
158 let root = File::open("/").unwrap();
159 fstatvfs(&root).unwrap();
160 }
161}