Skip to content

Commit 04d315d

Browse files
quodlibetorcarllerche
authored andcommitted
Statvfs improvements
* Implement `Default` * Add documentation * Add some convenience wrappers
1 parent 073cc85 commit 04d315d

File tree

2 files changed

+111
-1
lines changed

2 files changed

+111
-1
lines changed

src/sys/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ pub mod quota;
6767
pub mod statfs;
6868

6969

70-
#[cfg(all(target_os = "linux",
70+
#[cfg(all(any(target_os = "linux",
71+
target_os = "macos"),
7172
any(target_arch = "x86",
7273
target_arch = "x86_64",
7374
target_arch = "arm")),

src/sys/statvfs.rs

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,133 @@
1+
//! FFI for statvfs functions
2+
//!
3+
//! See the `vfs::Statvfs` struct for some rusty wrappers
4+
15
use {Result, NixPath, from_ffi};
26
use errno::Errno;
37
use std::os::unix::io::AsRawFd;
48

59
pub mod vfs {
10+
//! Structs related to the `statvfs` and `fstatvfs` functions
11+
//!
12+
//! The `Statvfs` struct has some wrappers methods around the `statvfs` and
13+
//! `fstatvfs` calls.
14+
615
use libc::{c_ulong,c_int};
16+
use std::os::unix::io::AsRawFd;
17+
use {Result, NixPath};
18+
19+
use super::{statvfs, fstatvfs};
720

821
bitflags!(
22+
/// Mount Flags
923
#[repr(C)]
1024
#[derive(Default)]
1125
flags FsFlags: c_ulong {
26+
/// Read Only
1227
const RDONLY = 1,
28+
/// Do not allow the set-uid bits to have an effect
1329
const NOSUID = 2,
30+
/// Do not interpret character or block-special devices
1431
const NODEV = 4,
32+
/// Do not allow execution of binaries on the filesystem
1533
const NOEXEC = 8,
34+
/// All IO should be done synchronously
1635
const SYNCHRONOUS = 16,
36+
/// Allow mandatory locks on the filesystem
1737
const MANDLOCK = 64,
1838
const WRITE = 128,
1939
const APPEND = 256,
2040
const IMMUTABLE = 512,
41+
/// Do not update access times on files
2142
const NOATIME = 1024,
43+
/// Do not update access times on files
2244
const NODIRATIME = 2048,
45+
/// Update access time relative to modify/change time
2346
const RELATIME = 4096,
2447
}
2548
);
2649

50+
/// The posix statvfs struct
51+
///
52+
/// http://linux.die.net/man/2/statvfs
2753
#[repr(C)]
2854
#[derive(Debug,Copy,Clone)]
2955
pub struct Statvfs {
56+
/// Filesystem block size. This is the value that will lead to
57+
/// most efficient use of the filesystem
3058
pub f_bsize: c_ulong,
59+
/// Fragment Size -- actual minimum unit of allocation on this
60+
/// filesystem
3161
pub f_frsize: c_ulong,
62+
/// Total number of blocks on the filesystem
3263
pub f_blocks: u64,
64+
/// Number of unused blocks on the filesystem, including those
65+
/// reserved for root
3366
pub f_bfree: u64,
67+
/// Number of blocks available to non-root users
3468
pub f_bavail: u64,
69+
/// Total number of inodes available on the filesystem
3570
pub f_files: u64,
71+
/// Number of inodes available on the filesystem
3672
pub f_ffree: u64,
73+
/// Number of inodes available to non-root users
3774
pub f_favail: u64,
75+
/// File System ID
3876
pub f_fsid: c_ulong,
77+
/// Mount Flags
3978
pub f_flag: FsFlags,
79+
/// Maximum filename length
4080
pub f_namemax: c_ulong,
81+
/// Reserved extra space, OS-dependent
4182
f_spare: [c_int; 6],
4283
}
84+
85+
impl Statvfs {
86+
/// Create a new `Statvfs` object and fill it with information about
87+
/// the mount that contains `path`
88+
pub fn for_path<P: ?Sized + NixPath>(path: &P) -> Result<Statvfs> {
89+
let mut stat = Statvfs::default();
90+
let res = statvfs(path, &mut stat);
91+
res.map(|_| stat)
92+
}
93+
94+
/// Replace information in this struct with information about `path`
95+
pub fn update_with_path<P: ?Sized + NixPath>(&mut self, path: &P) -> Result<()> {
96+
statvfs(path, self)
97+
}
98+
99+
/// Create a new `Statvfs` object and fill it with information from fd
100+
pub fn for_fd<T: AsRawFd>(fd: &T) -> Result<Statvfs> {
101+
let mut stat = Statvfs::default();
102+
let res = fstatvfs(fd, &mut stat);
103+
res.map(|_| stat)
104+
}
105+
106+
/// Replace information in this struct with information about `fd`
107+
pub fn update_with_fd<T: AsRawFd>(&mut self, fd: &T) -> Result<()> {
108+
fstatvfs(fd, self)
109+
}
110+
}
111+
112+
impl Default for Statvfs {
113+
/// Create a statvfs object initialized to all zeros
114+
fn default() -> Self {
115+
Statvfs {
116+
f_bsize: 0,
117+
f_frsize: 0,
118+
f_blocks: 0,
119+
f_bfree: 0,
120+
f_bavail: 0,
121+
f_files: 0,
122+
f_ffree: 0,
123+
f_favail: 0,
124+
f_fsid: 0,
125+
f_flag: FsFlags::default(),
126+
f_namemax: 0,
127+
f_spare: [0, 0, 0, 0, 0, 0],
128+
}
129+
}
130+
}
43131
}
44132

45133
mod ffi {
@@ -52,6 +140,7 @@ mod ffi {
52140
}
53141
}
54142

143+
/// Fill an existing `Statvfs` object with information about the `path`
55144
pub fn statvfs<P: ?Sized + NixPath>(path: &P, stat: &mut vfs::Statvfs) -> Result<()> {
56145
unsafe {
57146
Errno::clear();
@@ -62,9 +151,29 @@ pub fn statvfs<P: ?Sized + NixPath>(path: &P, stat: &mut vfs::Statvfs) -> Result
62151
}
63152
}
64153

154+
/// Fill an existing `Statvfs` object with information about `fd`
65155
pub fn fstatvfs<T: AsRawFd>(fd: &T, stat: &mut vfs::Statvfs) -> Result<()> {
66156
unsafe {
67157
Errno::clear();
68158
from_ffi(ffi::fstatvfs(fd.as_raw_fd(), stat))
69159
}
70160
}
161+
162+
#[cfg(test)]
163+
mod test {
164+
use std::fs::File;
165+
use sys::statvfs::*;
166+
167+
#[test]
168+
fn statvfs_call() {
169+
let mut stat = vfs::Statvfs::default();
170+
statvfs("/".as_bytes(), &mut stat).unwrap()
171+
}
172+
173+
#[test]
174+
fn fstatvfs_call() {
175+
let mut stat = vfs::Statvfs::default();
176+
let root = File::open("/").unwrap();
177+
fstatvfs(&root, &mut stat).unwrap()
178+
}
179+
}

0 commit comments

Comments
 (0)