summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlan Somers <asomers@gmail.com>2022-10-17 19:00:48 -0600
committerAlan Somers <asomers@gmail.com>2022-10-23 22:42:00 -0600
commitb2c036164744bc06f2f194f4ea7c9b8acc19234c (patch)
tree8f118faa9b41860e5125135c1b92867ffc414744
parent3f66d1f4ce06403f646876864260c59ca3627ee4 (diff)
downloadnix-b2c036164744bc06f2f194f4ea7c9b8acc19234c.zip
Add a Statfs::flags method
It returns the mount flags on the BSDs. On Linux, it returns a slightly different set of flags.
-rw-r--r--CHANGELOG.md2
-rw-r--r--src/sys/statfs.rs62
2 files changed, 54 insertions, 10 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5c3a3d1d..43d122ab 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
- Add `MntFlags` and `unmount` on all of the BSDs.
([#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.
diff --git a/src/sys/statfs.rs b/src/sys/statfs.rs
index acbc75cd..9c28bc21 100644
--- a/src/sys/statfs.rs
+++ b/src/sys/statfs.rs
@@ -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")]
@@ -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())))]
@@ -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()
+
}
}