1use std::mem;
6use std::os::unix::io::{AsFd, AsRawFd};
7
8use libc::{self, c_ulong};
9
10use crate::{errno::Errno, NixPath, Result};
11
12#[cfg(not(target_os = "redox"))]
13libc_bitflags!(
14 #[derive(Default)]
16 pub struct FsFlags: c_ulong {
17 #[cfg(not(target_os = "haiku"))]
19 ST_RDONLY;
20 #[cfg(not(target_os = "haiku"))]
22 ST_NOSUID;
23 #[cfg(linux_android)]
25 ST_NODEV;
26 #[cfg(linux_android)]
28 ST_NOEXEC;
29 #[cfg(linux_android)]
31 ST_SYNCHRONOUS;
32 #[cfg(linux_android)]
34 ST_MANDLOCK;
35 #[cfg(target_os = "linux")]
37 ST_WRITE;
38 #[cfg(target_os = "linux")]
40 ST_APPEND;
41 #[cfg(target_os = "linux")]
43 ST_IMMUTABLE;
44 #[cfg(linux_android)]
46 ST_NOATIME;
47 #[cfg(linux_android)]
49 ST_NODIRATIME;
50 #[cfg(any(target_os = "android", all(target_os = "linux", not(target_env = "musl"), not(target_env = "ohos"))))]
52 ST_RELATIME;
53 }
54);
55
56#[repr(transparent)]
60#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
61pub struct Statvfs(libc::statvfs);
62
63impl Statvfs {
64 pub fn block_size(&self) -> c_ulong {
66 self.0.f_bsize
67 }
68
69 pub fn fragment_size(&self) -> c_ulong {
71 self.0.f_frsize
72 }
73
74 pub fn blocks(&self) -> libc::fsblkcnt_t {
78 self.0.f_blocks
79 }
80
81 pub fn blocks_free(&self) -> libc::fsblkcnt_t {
83 self.0.f_bfree
84 }
85
86 pub fn blocks_available(&self) -> libc::fsblkcnt_t {
88 self.0.f_bavail
89 }
90
91 pub fn files(&self) -> libc::fsfilcnt_t {
93 self.0.f_files
94 }
95
96 pub fn files_free(&self) -> libc::fsfilcnt_t {
98 self.0.f_ffree
99 }
100
101 pub fn files_available(&self) -> libc::fsfilcnt_t {
103 self.0.f_favail
104 }
105
106 #[cfg(not(target_os = "hurd"))]
108 pub fn filesystem_id(&self) -> c_ulong {
109 self.0.f_fsid
110 }
111 #[cfg(target_os = "hurd")]
113 pub fn filesystem_id(&self) -> u64 {
114 self.0.f_fsid
115 }
116
117 #[cfg(not(target_os = "redox"))]
119 pub fn flags(&self) -> FsFlags {
120 FsFlags::from_bits_truncate(self.0.f_flag)
121 }
122
123 pub fn name_max(&self) -> c_ulong {
125 self.0.f_namemax
126 }
127}
128
129pub fn statvfs<P: ?Sized + NixPath>(path: &P) -> Result<Statvfs> {
131 unsafe {
132 Errno::clear();
133 let mut stat = mem::MaybeUninit::<libc::statvfs>::uninit();
134 let res = path.with_nix_path(|path| {
135 libc::statvfs(path.as_ptr(), stat.as_mut_ptr())
136 })?;
137
138 Errno::result(res).map(|_| Statvfs(stat.assume_init()))
139 }
140}
141
142pub fn fstatvfs<Fd: AsFd>(fd: Fd) -> Result<Statvfs> {
144 unsafe {
145 Errno::clear();
146 let mut stat = mem::MaybeUninit::<libc::statvfs>::uninit();
147 Errno::result(libc::fstatvfs(fd.as_fd().as_raw_fd(), stat.as_mut_ptr()))
148 .map(|_| Statvfs(stat.assume_init()))
149 }
150}