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 18, 2022
1 parent e31fead commit 37bfda2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
## [Unreleased] - ReleaseDate
### Added

- 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
56 changes: 46 additions & 10 deletions src/sys/statfs.rs
Expand Up @@ -10,6 +10,14 @@ use std::ffi::CStr;
use cfg_if::cfg_if;

use crate::{NixPath, Result, errno::Errno};
#[cfg(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 +382,26 @@ impl Statfs {
self.0.f_bsize
}

/// Get the mount flags
#[cfg(any(target_os = "dragonfly",
target_os = "freebsd",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd"))]
#[cfg_attr(docsrs, doc(cfg(all())))]
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 @@ -641,16 +669,24 @@ 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(any(target_os = "dragonfly",
target_os = "freebsd",
target_os = "macos",
target_os = "linux",
target_os = "netbsd",
target_os = "openbsd"))]
ds.field("flags", &self.flags());
ds.finish()

}
}

Expand Down

0 comments on commit 37bfda2

Please sign in to comment.