Define MntFlags and unmount on all of the BSDs.

This commit is contained in:
Alan Somers 2022-10-18 08:38:16 -06:00
parent d6fdd5952d
commit 3f66d1f4ce
5 changed files with 55 additions and 101 deletions

View File

@ -6,6 +6,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
## [Unreleased] - ReleaseDate
### Added
- Add `MntFlags` and `unmount` on all of the BSDs.
([#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.

View File

@ -27,7 +27,7 @@ targets = [
]
[dependencies]
libc = { version = "0.2.135", features = [ "extra_traits" ] }
libc = { git = "https://github.com/rust-lang/libc", rev = "cc19b6f0801", features = [ "extra_traits" ] }
bitflags = "1.1"
cfg-if = "1.0"
pin-utils = { version = "0.1.0", optional = true }

View File

@ -106,7 +106,6 @@ feature! {
#[allow(missing_docs)]
pub mod kmod;
}
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "linux"))]
feature! {
#![feature = "mount"]
pub mod mount;

View File

@ -1,16 +1,22 @@
#[cfg(target_os = "freebsd")]
use crate::{
Error,
};
use crate::{
Errno,
NixPath,
Result,
};
use libc::{c_char, c_int, c_uint, c_void};
#[cfg(target_os = "freebsd")]
use libc::{c_char, c_uint, c_void};
use libc::c_int;
#[cfg(target_os = "freebsd")]
use std::{
borrow::Cow,
ffi::{CString, CStr},
marker::PhantomData,
fmt,
io,
marker::PhantomData,
};
@ -110,12 +116,14 @@ libc_bitflags!(
///
/// It wraps an [`Errno`], but also may contain an additional message returned
/// by `nmount(2)`.
#[cfg(target_os = "freebsd")]
#[derive(Debug)]
pub struct NmountError {
errno: Error,
errmsg: Option<String>
}
#[cfg(target_os = "freebsd")]
impl NmountError {
/// Returns the additional error string sometimes generated by `nmount(2)`.
pub fn errmsg(&self) -> Option<&str> {
@ -135,8 +143,10 @@ impl NmountError {
}
}
#[cfg(target_os = "freebsd")]
impl std::error::Error for NmountError {}
#[cfg(target_os = "freebsd")]
impl fmt::Display for NmountError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let Some(errmsg) = &self.errmsg {
@ -147,6 +157,7 @@ impl fmt::Display for NmountError {
}
}
#[cfg(target_os = "freebsd")]
impl From<NmountError> for io::Error {
fn from(err: NmountError) -> Self {
err.errno.into()
@ -154,6 +165,7 @@ impl From<NmountError> for io::Error {
}
/// Result type of [`Nmount::nmount`].
#[cfg(target_os = "freebsd")]
pub type NmountResult = std::result::Result<(), NmountError>;
/// Mount a FreeBSD file system.
@ -425,13 +437,15 @@ impl<'a> Drop for Nmount<'a> {
///
/// Useful flags include
/// * `MNT_FORCE` - Unmount even if still in use.
/// * `MNT_BYFSID` - `mountpoint` is not a path, but a file system ID
/// encoded as `FSID:val0:val1`, where `val0` and `val1`
/// are the contents of the `fsid_t val[]` array in decimal.
/// The file system that has the specified file system ID
/// will be unmounted. See
/// [`statfs`](crate::sys::statfs::statfs) to determine the
/// `fsid`.
#[cfg_attr(target_os = "freebsd", doc = "
* `MNT_BYFSID` - `mountpoint` is not a path, but a file system ID
encoded as `FSID:val0:val1`, where `val0` and `val1`
are the contents of the `fsid_t val[]` array in decimal.
The file system that has the specified file system ID
will be unmounted. See
[`statfs`](crate::sys::statfs::statfs) to determine the
`fsid`.
")]
pub fn unmount<P>(mountpoint: &P, flags: MntFlags) -> Result<()>
where P: ?Sized + NixPath
{

View File

@ -422,7 +422,9 @@ impl Statfs {
target_os = "macos",
target_os = "android",
target_os = "freebsd",
target_os = "fuchsia",
target_os = "openbsd",
target_os = "linux",
))]
#[cfg_attr(docsrs, doc(cfg(all())))]
pub fn blocks(&self) -> u64 {
@ -437,24 +439,9 @@ impl Statfs {
}
/// Total data blocks in filesystem
#[cfg(all(target_os = "linux", any(target_env = "musl", target_arch = "riscv32", all(target_arch = "x86_64", target_pointer_width = "32"))))]
#[cfg(target_os = "emscripten")]
#[cfg_attr(docsrs, doc(cfg(all())))]
pub fn blocks(&self) -> u64 {
self.0.f_blocks
}
/// Total data blocks in filesystem
#[cfg(not(any(
target_os = "ios",
target_os = "macos",
target_os = "android",
target_os = "freebsd",
target_os = "openbsd",
target_os = "dragonfly",
all(target_os = "linux", any(target_env = "musl", target_arch = "riscv32", all(target_arch = "x86_64", target_pointer_width = "32")))
)))]
#[cfg_attr(docsrs, doc(cfg(all())))]
pub fn blocks(&self) -> libc::c_ulong {
pub fn blocks(&self) -> u32 {
self.0.f_blocks
}
@ -464,7 +451,9 @@ impl Statfs {
target_os = "macos",
target_os = "android",
target_os = "freebsd",
target_os = "fuchsia",
target_os = "openbsd",
target_os = "linux",
))]
#[cfg_attr(docsrs, doc(cfg(all())))]
pub fn blocks_free(&self) -> u64 {
@ -479,29 +468,20 @@ impl Statfs {
}
/// Free blocks in filesystem
#[cfg(all(target_os = "linux", any(target_env = "musl", target_arch = "riscv32", all(target_arch = "x86_64", target_pointer_width = "32"))))]
#[cfg(target_os = "emscripten")]
#[cfg_attr(docsrs, doc(cfg(all())))]
pub fn blocks_free(&self) -> u64 {
self.0.f_bfree
}
/// Free blocks in filesystem
#[cfg(not(any(
target_os = "ios",
target_os = "macos",
target_os = "android",
target_os = "freebsd",
target_os = "openbsd",
target_os = "dragonfly",
all(target_os = "linux", any(target_env = "musl", target_arch = "riscv32", all(target_arch = "x86_64", target_pointer_width = "32")))
)))]
#[cfg_attr(docsrs, doc(cfg(all())))]
pub fn blocks_free(&self) -> libc::c_ulong {
pub fn blocks_free(&self) -> u32 {
self.0.f_bfree
}
/// Free blocks available to unprivileged user
#[cfg(any(target_os = "ios", target_os = "macos", target_os = "android"))]
#[cfg(any(
target_os = "ios",
target_os = "macos",
target_os = "android",
target_os = "fuchsia",
target_os = "linux",
))]
#[cfg_attr(docsrs, doc(cfg(all())))]
pub fn blocks_available(&self) -> u64 {
self.0.f_bavail
@ -522,24 +502,9 @@ impl Statfs {
}
/// Free blocks available to unprivileged user
#[cfg(all(target_os = "linux", any(target_env = "musl", target_arch = "riscv32", all(target_arch = "x86_64", target_pointer_width = "32"))))]
#[cfg(target_os = "emscripten")]
#[cfg_attr(docsrs, doc(cfg(all())))]
pub fn blocks_available(&self) -> u64 {
self.0.f_bavail
}
/// Free blocks available to unprivileged user
#[cfg(not(any(
target_os = "ios",
target_os = "macos",
target_os = "android",
target_os = "freebsd",
target_os = "openbsd",
target_os = "dragonfly",
all(target_os = "linux", any(target_env = "musl", target_arch = "riscv32", all(target_arch = "x86_64", target_pointer_width = "32")))
)))]
#[cfg_attr(docsrs, doc(cfg(all())))]
pub fn blocks_available(&self) -> libc::c_ulong {
pub fn blocks_available(&self) -> u32 {
self.0.f_bavail
}
@ -549,7 +514,9 @@ impl Statfs {
target_os = "macos",
target_os = "android",
target_os = "freebsd",
target_os = "fuchsia",
target_os = "openbsd",
target_os = "linux",
))]
#[cfg_attr(docsrs, doc(cfg(all())))]
pub fn files(&self) -> u64 {
@ -564,33 +531,20 @@ impl Statfs {
}
/// Total file nodes in filesystem
#[cfg(all(target_os = "linux", any(target_env = "musl", target_arch = "riscv32", all(target_arch = "x86_64", target_pointer_width = "32"))))]
#[cfg(target_os = "emscripten")]
#[cfg_attr(docsrs, doc(cfg(all())))]
pub fn files(&self) -> libc::fsfilcnt_t {
self.0.f_files
}
/// Total file nodes in filesystem
#[cfg(not(any(
target_os = "ios",
target_os = "macos",
target_os = "android",
target_os = "freebsd",
target_os = "openbsd",
target_os = "dragonfly",
all(target_os = "linux", any(target_env = "musl", target_arch = "riscv32", all(target_arch = "x86_64", target_pointer_width = "32")))
)))]
#[cfg_attr(docsrs, doc(cfg(all())))]
pub fn files(&self) -> libc::c_ulong {
pub fn files(&self) -> u32 {
self.0.f_files
}
/// Free file nodes in filesystem
#[cfg(any(
target_os = "android",
target_os = "ios",
target_os = "macos",
target_os = "openbsd"
target_os = "android",
target_os = "fuchsia",
target_os = "openbsd",
target_os = "linux",
))]
#[cfg_attr(docsrs, doc(cfg(all())))]
pub fn files_free(&self) -> u64 {
@ -612,24 +566,9 @@ impl Statfs {
}
/// Free file nodes in filesystem
#[cfg(all(target_os = "linux", any(target_env = "musl", target_arch = "riscv32", all(target_arch = "x86_64", target_pointer_width = "32"))))]
#[cfg(target_os = "emscripten")]
#[cfg_attr(docsrs, doc(cfg(all())))]
pub fn files_free(&self) -> libc::fsfilcnt_t {
self.0.f_ffree
}
/// Free file nodes in filesystem
#[cfg(not(any(
target_os = "ios",
target_os = "macos",
target_os = "android",
target_os = "freebsd",
target_os = "openbsd",
target_os = "dragonfly",
all(target_os = "linux", any(target_env = "musl", target_arch = "riscv32", all(target_arch = "x86_64", target_pointer_width = "32")))
)))]
#[cfg_attr(docsrs, doc(cfg(all())))]
pub fn files_free(&self) -> libc::c_ulong {
pub fn files_free(&self) -> u32 {
self.0.f_ffree
}