-
Notifications
You must be signed in to change notification settings - Fork 689
feat: introduce macos mount API support #2347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
7de4c75
feat: introduce macos mount API support
oowl 0da612d
feat: add doc comment
oowl 298111b
feat: fix code
oowl 6bc2472
feat: fix code
oowl 86520a5
feat: fix code
oowl ba11a92
feat: fix code
oowl 3a6fa9b
feat: fix code
oowl fd4cc36
feat: fix code
oowl 9d4043b
feat: fix code
oowl 47e45ee
feat: fix code
oowl 51b5910
feat: fix code
oowl a2f3797
feat: fix code
oowl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add `mount` and `unmount` API for apple targets. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
use crate::{Errno, NixPath, Result}; | ||
use libc::c_int; | ||
|
||
libc_bitflags!( | ||
/// Used with [`mount()`] and [`unmount()`]. | ||
pub struct MntFlags: c_int { | ||
/// Do not interpret special files on the filesystem. | ||
MNT_NODEV; | ||
/// Enable data protection on the filesystem if the filesystem is configured for it. | ||
MNT_CPROTECT; | ||
/// file system is quarantined | ||
MNT_QUARANTINE; | ||
/// filesystem is stored locally | ||
MNT_LOCAL; | ||
/// quotas are enabled on filesystem | ||
MNT_QUOTA; | ||
/// identifies the root filesystem | ||
MNT_ROOTFS; | ||
/// file system is not appropriate path to user data | ||
MNT_DONTBROWSE; | ||
/// VFS will ignore ownership information on filesystem objects | ||
MNT_IGNORE_OWNERSHIP; | ||
/// filesystem was mounted by automounter | ||
MNT_AUTOMOUNTED; | ||
/// filesystem is journaled | ||
MNT_JOURNALED; | ||
/// Don't allow user extended attributes | ||
MNT_NOUSERXATTR; | ||
/// filesystem should defer writes | ||
MNT_DEFWRITE; | ||
/// don't block unmount if not responding | ||
MNT_NOBLOCK; | ||
/// file system is exported | ||
MNT_EXPORTED; | ||
/// file system written asynchronously | ||
MNT_ASYNC; | ||
/// Force a read-write mount even if the file system appears to be | ||
/// unclean. | ||
MNT_FORCE; | ||
/// MAC support for objects. | ||
MNT_MULTILABEL; | ||
/// Do not update access times. | ||
MNT_NOATIME; | ||
/// Disallow program execution. | ||
MNT_NOEXEC; | ||
/// Do not honor setuid or setgid bits on files when executing them. | ||
MNT_NOSUID; | ||
/// Mount read-only. | ||
MNT_RDONLY; | ||
/// Causes the vfs subsystem to update its data structures pertaining to | ||
/// the specified already mounted file system. | ||
MNT_RELOAD; | ||
/// Create a snapshot of the file system. | ||
MNT_SNAPSHOT; | ||
/// All I/O to the file system should be done synchronously. | ||
MNT_SYNCHRONOUS; | ||
/// Union with underlying fs. | ||
MNT_UNION; | ||
/// Indicates that the mount command is being applied to an already | ||
/// mounted file system. | ||
MNT_UPDATE; | ||
} | ||
); | ||
|
||
/// Mount a file system. | ||
/// | ||
/// # Arguments | ||
/// - `source` - Specifies the file system. e.g. `/dev/sd0`. | ||
/// - `target` - Specifies the destination. e.g. `/mnt`. | ||
/// - `flags` - Optional flags controlling the mount. | ||
/// - `data` - Optional file system specific data. | ||
/// | ||
/// # see also | ||
/// [`mount`](https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/mount.2.html) | ||
pub fn mount< | ||
P1: ?Sized + NixPath, | ||
P2: ?Sized + NixPath, | ||
P3: ?Sized + NixPath, | ||
>( | ||
source: &P1, | ||
target: &P2, | ||
flags: MntFlags, | ||
data: Option<&P3>, | ||
) -> Result<()> { | ||
fn with_opt_nix_path<P, T, F>(p: Option<&P>, f: F) -> Result<T> | ||
where | ||
P: ?Sized + NixPath, | ||
F: FnOnce(*const libc::c_char) -> T, | ||
{ | ||
match p { | ||
Some(path) => path.with_nix_path(|p_str| f(p_str.as_ptr())), | ||
None => Ok(f(std::ptr::null())), | ||
} | ||
} | ||
|
||
let res = source.with_nix_path(|s| { | ||
target.with_nix_path(|t| { | ||
with_opt_nix_path(data, |d| unsafe { | ||
libc::mount( | ||
s.as_ptr(), | ||
t.as_ptr(), | ||
flags.bits(), | ||
d.cast_mut().cast(), | ||
) | ||
}) | ||
}) | ||
})???; | ||
|
||
Errno::result(res).map(drop) | ||
} | ||
|
||
/// Umount the file system mounted at `target`. | ||
pub fn unmount<P>(target: &P, flags: MntFlags) -> Result<()> | ||
where | ||
P: ?Sized + NixPath, | ||
{ | ||
let res = target.with_nix_path(|cstr| unsafe { | ||
libc::unmount(cstr.as_ptr(), flags.bits()) | ||
})?; | ||
|
||
Errno::result(res).map(drop) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#[cfg(target_os = "linux")] | ||
mod test_mount; | ||
#[cfg(apple_targets)] | ||
mod test_mount_apple; | ||
#[cfg(target_os = "freebsd")] | ||
mod test_nmount; |
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
use nix::errno::Errno; | ||
use nix::mount::{mount, MntFlags}; | ||
|
||
#[test] | ||
fn test_mount() { | ||
let res = mount::<str, str, str>("", "", MntFlags::empty(), None); | ||
assert_eq!(res, Err(Errno::ENOENT)); | ||
} |
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.