Skip to content

Commit

Permalink
Add a Statfs::flags method
Browse files Browse the repository at this point in the history
It returns the mount flags on the BSDs.  On Linux, it returns a slightly
different set of flags.
  • Loading branch information
asomers committed Oct 23, 2022
1 parent 1e8c667 commit d91962f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
([#1849](https://github.com/nix-rust/nix/pull/1849))
- Added a 'Statfs::flags' method.
([#1849](https://github.com/nix-rust/nix/pull/1849))
- Added a 'Statfs::flags' method.
([#1849](https://github.com/nix-rust/nix/pull/1849))
- Added `NSFS_MAGIC` FsType on Linux and Android.
([#1829](https://github.com/nix-rust/nix/pull/1829))
- Added `sched_getcpu` on platforms that support it.
Expand Down
62 changes: 52 additions & 10 deletions src/sys/statfs.rs
Expand Up @@ -10,6 +10,16 @@ use std::ffi::CStr;
use cfg_if::cfg_if;

use crate::{NixPath, Result, errno::Errno};
#[cfg(all(feature = "mount",
any(target_os = "dragonfly",
target_os = "freebsd",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd")
))]
use crate::mount::MntFlags;
#[cfg(target_os = "linux")]
use crate::sys::statvfs::FsFlags;

/// Identifies a mounted file system
#[cfg(target_os = "android")]
Expand Down Expand Up @@ -374,6 +384,29 @@ impl Statfs {
self.0.f_bsize
}

/// Get the mount flags
#[cfg(all(feature = "mount",
any(target_os = "dragonfly",
target_os = "freebsd",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd")
))]
#[cfg_attr(docsrs, doc(cfg(all())))]
#[allow(clippy::unnecessary_cast)] // Not unnecessary on all arches
pub fn flags(&self) -> MntFlags {
MntFlags::from_bits_truncate(self.0.f_flags as i32)
}

/// Get the mount flags
// The f_flags field exists on Android and Fuchsia too, but without man
// pages I can't tell if it can be cast to FsFlags.
#[cfg(target_os = "linux")]
#[cfg_attr(docsrs, doc(cfg(all())))]
pub fn flags(&self) -> FsFlags {
FsFlags::from_bits_truncate(self.0.f_flags as libc::c_ulong)
}

/// Maximum length of filenames
#[cfg(any(target_os = "freebsd", target_os = "openbsd"))]
#[cfg_attr(docsrs, doc(cfg(all())))]
Expand Down Expand Up @@ -580,16 +613,25 @@ impl Statfs {

impl Debug for Statfs {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Statfs")
.field("optimal_transfer_size", &self.optimal_transfer_size())
.field("block_size", &self.block_size())
.field("blocks", &self.blocks())
.field("blocks_free", &self.blocks_free())
.field("blocks_available", &self.blocks_available())
.field("files", &self.files())
.field("files_free", &self.files_free())
.field("filesystem_id", &self.filesystem_id())
.finish()
let mut ds = f.debug_struct("Statfs");
ds.field("optimal_transfer_size", &self.optimal_transfer_size());
ds.field("block_size", &self.block_size());
ds.field("blocks", &self.blocks());
ds.field("blocks_free", &self.blocks_free());
ds.field("blocks_available", &self.blocks_available());
ds.field("files", &self.files());
ds.field("files_free", &self.files_free());
ds.field("filesystem_id", &self.filesystem_id());
#[cfg(all(feature = "mount",
any(target_os = "dragonfly",
target_os = "freebsd",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd")
))]
ds.field("flags", &self.flags());
ds.finish()

}
}

Expand Down

0 comments on commit d91962f

Please sign in to comment.