use std::mem;
use std::os::unix::io::AsRawFd;
use libc::{self, c_ulong};
use crate::{errno::Errno, NixPath, Result};
#[cfg(not(target_os = "redox"))]
libc_bitflags!(
#[repr(C)]
#[derive(Default)]
pub struct FsFlags: c_ulong {
#[cfg(not(target_os = "haiku"))]
ST_RDONLY;
#[cfg(not(target_os = "haiku"))]
ST_NOSUID;
#[cfg(any(target_os = "android", target_os = "linux"))]
#[cfg_attr(docsrs, doc(cfg(all())))]
ST_NODEV;
#[cfg(any(target_os = "android", target_os = "linux"))]
#[cfg_attr(docsrs, doc(cfg(all())))]
ST_NOEXEC;
#[cfg(any(target_os = "android", target_os = "linux"))]
#[cfg_attr(docsrs, doc(cfg(all())))]
ST_SYNCHRONOUS;
#[cfg(any(target_os = "android", target_os = "linux"))]
#[cfg_attr(docsrs, doc(cfg(all())))]
ST_MANDLOCK;
#[cfg(target_os = "linux")]
#[cfg_attr(docsrs, doc(cfg(all())))]
ST_WRITE;
#[cfg(target_os = "linux")]
#[cfg_attr(docsrs, doc(cfg(all())))]
ST_APPEND;
#[cfg(target_os = "linux")]
#[cfg_attr(docsrs, doc(cfg(all())))]
ST_IMMUTABLE;
#[cfg(any(target_os = "android", target_os = "linux"))]
#[cfg_attr(docsrs, doc(cfg(all())))]
ST_NOATIME;
#[cfg(any(target_os = "android", target_os = "linux"))]
#[cfg_attr(docsrs, doc(cfg(all())))]
ST_NODIRATIME;
#[cfg(any(target_os = "android", all(target_os = "linux", not(target_env = "musl"))))]
#[cfg_attr(docsrs, doc(cfg(all())))]
ST_RELATIME;
}
);
#[repr(transparent)]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct Statvfs(libc::statvfs);
impl Statvfs {
pub fn block_size(&self) -> c_ulong {
self.0.f_bsize
}
pub fn fragment_size(&self) -> c_ulong {
self.0.f_frsize
}
pub fn blocks(&self) -> libc::fsblkcnt_t {
self.0.f_blocks
}
pub fn blocks_free(&self) -> libc::fsblkcnt_t {
self.0.f_bfree
}
pub fn blocks_available(&self) -> libc::fsblkcnt_t {
self.0.f_bavail
}
pub fn files(&self) -> libc::fsfilcnt_t {
self.0.f_files
}
pub fn files_free(&self) -> libc::fsfilcnt_t {
self.0.f_ffree
}
pub fn files_available(&self) -> libc::fsfilcnt_t {
self.0.f_favail
}
pub fn filesystem_id(&self) -> c_ulong {
self.0.f_fsid
}
#[cfg(not(target_os = "redox"))]
#[cfg_attr(docsrs, doc(cfg(all())))]
pub fn flags(&self) -> FsFlags {
FsFlags::from_bits_truncate(self.0.f_flag)
}
pub fn name_max(&self) -> c_ulong {
self.0.f_namemax
}
}
pub fn statvfs<P: ?Sized + NixPath>(path: &P) -> Result<Statvfs> {
unsafe {
Errno::clear();
let mut stat = mem::MaybeUninit::<libc::statvfs>::uninit();
let res = path.with_nix_path(|path| {
libc::statvfs(path.as_ptr(), stat.as_mut_ptr())
})?;
Errno::result(res).map(|_| Statvfs(stat.assume_init()))
}
}
pub fn fstatvfs<T: AsRawFd>(fd: &T) -> Result<Statvfs> {
unsafe {
Errno::clear();
let mut stat = mem::MaybeUninit::<libc::statvfs>::uninit();
Errno::result(libc::fstatvfs(fd.as_raw_fd(), stat.as_mut_ptr()))
.map(|_| Statvfs(stat.assume_init()))
}
}
#[cfg(test)]
mod test {
use crate::sys::statvfs::*;
use std::fs::File;
#[test]
fn statvfs_call() {
statvfs(&b"/"[..]).unwrap();
}
#[test]
fn fstatvfs_call() {
let root = File::open("/").unwrap();
fstatvfs(&root).unwrap();
}
}