Skip to content

Commit b2c0361

Browse files
committed
Add a Statfs::flags method
It returns the mount flags on the BSDs. On Linux, it returns a slightly different set of flags.
1 parent 3f66d1f commit b2c0361

File tree

2 files changed

+54
-10
lines changed

2 files changed

+54
-10
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
88

99
- Add `MntFlags` and `unmount` on all of the BSDs.
1010
([#1849](https://github.com/nix-rust/nix/pull/1849))
11+
- Added a 'Statfs::flags' method.
12+
([#1849](https://github.com/nix-rust/nix/pull/1849))
1113
- Added `NSFS_MAGIC` FsType on Linux and Android.
1214
([#1829](https://github.com/nix-rust/nix/pull/1829))
1315
- Added `sched_getcpu` on platforms that support it.

src/sys/statfs.rs

+52-10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ use std::ffi::CStr;
1010
use cfg_if::cfg_if;
1111

1212
use crate::{NixPath, Result, errno::Errno};
13+
#[cfg(all(feature = "mount",
14+
any(target_os = "dragonfly",
15+
target_os = "freebsd",
16+
target_os = "macos",
17+
target_os = "netbsd",
18+
target_os = "openbsd")
19+
))]
20+
use crate::mount::MntFlags;
21+
#[cfg(target_os = "linux")]
22+
use crate::sys::statvfs::FsFlags;
1323

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

387+
/// Get the mount flags
388+
#[cfg(all(feature = "mount",
389+
any(target_os = "dragonfly",
390+
target_os = "freebsd",
391+
target_os = "macos",
392+
target_os = "netbsd",
393+
target_os = "openbsd")
394+
))]
395+
#[cfg_attr(docsrs, doc(cfg(all())))]
396+
#[allow(clippy::unnecessary_cast)] // Not unnecessary on all arches
397+
pub fn flags(&self) -> MntFlags {
398+
MntFlags::from_bits_truncate(self.0.f_flags as i32)
399+
}
400+
401+
/// Get the mount flags
402+
// The f_flags field exists on Android and Fuchsia too, but without man
403+
// pages I can't tell if it can be cast to FsFlags.
404+
#[cfg(target_os = "linux")]
405+
#[cfg_attr(docsrs, doc(cfg(all())))]
406+
pub fn flags(&self) -> FsFlags {
407+
FsFlags::from_bits_truncate(self.0.f_flags as libc::c_ulong)
408+
}
409+
377410
/// Maximum length of filenames
378411
#[cfg(any(target_os = "freebsd", target_os = "openbsd"))]
379412
#[cfg_attr(docsrs, doc(cfg(all())))]
@@ -580,16 +613,25 @@ impl Statfs {
580613

581614
impl Debug for Statfs {
582615
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
583-
f.debug_struct("Statfs")
584-
.field("optimal_transfer_size", &self.optimal_transfer_size())
585-
.field("block_size", &self.block_size())
586-
.field("blocks", &self.blocks())
587-
.field("blocks_free", &self.blocks_free())
588-
.field("blocks_available", &self.blocks_available())
589-
.field("files", &self.files())
590-
.field("files_free", &self.files_free())
591-
.field("filesystem_id", &self.filesystem_id())
592-
.finish()
616+
let mut ds = f.debug_struct("Statfs");
617+
ds.field("optimal_transfer_size", &self.optimal_transfer_size());
618+
ds.field("block_size", &self.block_size());
619+
ds.field("blocks", &self.blocks());
620+
ds.field("blocks_free", &self.blocks_free());
621+
ds.field("blocks_available", &self.blocks_available());
622+
ds.field("files", &self.files());
623+
ds.field("files_free", &self.files_free());
624+
ds.field("filesystem_id", &self.filesystem_id());
625+
#[cfg(all(feature = "mount",
626+
any(target_os = "dragonfly",
627+
target_os = "freebsd",
628+
target_os = "macos",
629+
target_os = "netbsd",
630+
target_os = "openbsd")
631+
))]
632+
ds.field("flags", &self.flags());
633+
ds.finish()
634+
593635
}
594636
}
595637

0 commit comments

Comments
 (0)