Files
addr2line
adler
aho_corasick
ansi_term
arraydeque
as_slice
atty
backtrace
base64
bincode_core
bitflags
byteorder
bytes
capnp
capnp_futures
capnp_rpc
cfg_if
chrono
clap
ctrlc
derivative
dlib
downcast_rs
enumflags2
enumflags2_derive
evdev_rs
evdev_sys
failure
failure_derive
flexi_logger
futures
futures_channel
futures_core
futures_executor
futures_io
futures_macro
futures_sink
futures_task
futures_util
async_await
future
io
lock
sink
stream
task
generic_array
getrandom
gimli
glob
hash32
heapless
hid_io_core
hid_io_protocol
hidapi
install_service
lazy_static
libc
libloading
libudev_sys
log
memchr
memmap
miniz_oxide
mio
nanoid
nix
num_cpus
num_enum
num_enum_derive
num_integer
num_traits
object
once_cell
open
pem
pin_project_lite
pin_utils
ppv_lite86
proc_macro2
proc_macro_hack
proc_macro_nested
quote
rand
rand_chacha
rand_core
rcgen
regex
regex_syntax
remove_dir_all
ring
rustc_demangle
rustls
scoped_tls
sct
serde
serde_derive
slab
smallvec
spin
stable_deref_trait
strsim
syn
synstructure
sys_info
tempfile
textwrap
thiserror
thiserror_impl
time
tokio
future
io
loom
macros
net
park
runtime
stream
sync
task
time
util
tokio_macros
tokio_rustls
tokio_util
typenum
udev
uhid_virt
uhidrs_sys
unicode_width
unicode_xid
untrusted
vec_map
wayland_client
wayland_commons
wayland_sys
webpki
which
x11
xcb
xkbcommon
yansi
yasna
zwp_virtual_keyboard
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//! Get filesystem statistics
//!
//! See [the man pages](http://pubs.opengroup.org/onlinepubs/9699919799/functions/fstatvfs.html)
//! for more details.
use std::mem;
use std::os::unix::io::AsRawFd;

use libc::{self, c_ulong};

use crate::{Result, NixPath, errno::Errno};

#[cfg(not(target_os = "redox"))]
libc_bitflags!(
    /// File system mount Flags
    #[repr(C)]
    #[derive(Default)]
    pub struct FsFlags: c_ulong {
        /// Read Only
        ST_RDONLY;
        /// Do not allow the set-uid bits to have an effect
        ST_NOSUID;
        /// Do not interpret character or block-special devices
        #[cfg(any(target_os = "android", target_os = "linux"))]
        ST_NODEV;
        /// Do not allow execution of binaries on the filesystem
        #[cfg(any(target_os = "android", target_os = "linux"))]
        ST_NOEXEC;
        /// All IO should be done synchronously
        #[cfg(any(target_os = "android", target_os = "linux"))]
        ST_SYNCHRONOUS;
        /// Allow mandatory locks on the filesystem
        #[cfg(any(target_os = "android", target_os = "linux"))]
        ST_MANDLOCK;
        /// Write on file/directory/symlink
        #[cfg(target_os = "linux")]
        ST_WRITE;
        /// Append-only file
        #[cfg(target_os = "linux")]
        ST_APPEND;
        /// Immutable file
        #[cfg(target_os = "linux")]
        ST_IMMUTABLE;
        /// Do not update access times on files
        #[cfg(any(target_os = "android", target_os = "linux"))]
        ST_NOATIME;
        /// Do not update access times on files
        #[cfg(any(target_os = "android", target_os = "linux"))]
        ST_NODIRATIME;
        /// Update access time relative to modify/change time
        #[cfg(any(target_os = "android", all(target_os = "linux", not(target_env = "musl"))))]
        ST_RELATIME;
    }
);

/// Wrapper around the POSIX `statvfs` struct
///
/// For more information see the [`statvfs(3)` man pages](http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_statvfs.h.html).
#[repr(transparent)]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct Statvfs(libc::statvfs);

impl Statvfs {
    /// get the file system block size
    pub fn block_size(&self) -> c_ulong {
        self.0.f_bsize
    }

    /// Get the fundamental file system block size
    pub fn fragment_size(&self) -> c_ulong {
        self.0.f_frsize
    }

    /// Get the number of blocks.
    ///
    /// Units are in units of `fragment_size()`
    pub fn blocks(&self) -> libc::fsblkcnt_t {
        self.0.f_blocks
    }

    /// Get the number of free blocks in the file system
    pub fn blocks_free(&self) -> libc::fsblkcnt_t {
        self.0.f_bfree
    }

    /// Get the number of free blocks for unprivileged users
    pub fn blocks_available(&self) -> libc::fsblkcnt_t {
        self.0.f_bavail
    }

    /// Get the total number of file inodes
    pub fn files(&self) -> libc::fsfilcnt_t {
        self.0.f_files
    }

    /// Get the number of free file inodes
    pub fn files_free(&self) -> libc::fsfilcnt_t {
        self.0.f_ffree
    }

    /// Get the number of free file inodes for unprivileged users
    pub fn files_available(&self) -> libc::fsfilcnt_t {
        self.0.f_favail
    }

    /// Get the file system id
    pub fn filesystem_id(&self) -> c_ulong {
        self.0.f_fsid
    }

    /// Get the mount flags
    #[cfg(not(target_os = "redox"))]
    pub fn flags(&self) -> FsFlags {
        FsFlags::from_bits_truncate(self.0.f_flag)
    }

    /// Get the maximum filename length
    pub fn name_max(&self) -> c_ulong {
        self.0.f_namemax
    }

}

/// Return a `Statvfs` object with information about the `path`
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()))
    }
}

/// Return a `Statvfs` object with information about `fd`
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 std::fs::File;
    use crate::sys::statvfs::*;

    #[test]
    fn statvfs_call() {
        statvfs("/".as_bytes()).unwrap();
    }

    #[test]
    fn fstatvfs_call() {
        let root = File::open("/").unwrap();
        fstatvfs(&root).unwrap();
    }
}