Skip to content

Commit

Permalink
Refactor Session to allow callers to mount it themselves
Browse files Browse the repository at this point in the history
This is a minor refactor of `Session`, with the aim of allowing callers
to create an unmounted session and mount it themselves (using the AsFd
implementation to get the FD).

One use case for this is when mounting inside containers, when you need
to call setns(2) before mounting.

Fixes #300
  • Loading branch information
colinmarc committed Oct 7, 2024
1 parent 12f05bb commit 9724cd3
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 105 deletions.
4 changes: 2 additions & 2 deletions examples/notify_inval_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,9 @@ fn main() {
timeout: Duration::from_secs_f32(opts.timeout),
};

let session = fuser::Session::new(fs, opts.mount_point, &options).unwrap();
let (session, mount) = fuser::Session::new(fs, opts.mount_point, &options).unwrap();
let notifier = session.notifier();
let _bg = session.spawn().unwrap();
let _bg = fuser::BackgroundSession::new(session, mount);

loop {
let mut fname = fname.lock().unwrap();
Expand Down
4 changes: 2 additions & 2 deletions examples/notify_inval_inode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,9 @@ fn main() {
lookup_cnt,
};

let session = fuser::Session::new(fs, opts.mount_point, &options).unwrap();
let (session, mount) = fuser::Session::new(fs, opts.mount_point, &options).unwrap();
let notifier = session.notifier();
let _bg = session.spawn().unwrap();
let _bg = fuser::BackgroundSession::new(session, mount);

loop {
let mut s = fdata.lock().unwrap();
Expand Down
4 changes: 2 additions & 2 deletions examples/poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,8 @@ fn main() {
let fs = FSelFS { data: data.clone() };

let mntpt = std::env::args().nth(1).unwrap();
let session = fuser::Session::new(fs, mntpt, &options).unwrap();
let bg = session.spawn().unwrap();
let (session, mount) = fuser::Session::new(fs, mntpt, &options).unwrap();
let bg = fuser::BackgroundSession::new(session, mount).unwrap();

producer(&data, &bg.notifier());
}
21 changes: 15 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub use reply::{
ReplyStatfs, ReplyWrite,
};
pub use request::Request;
pub use session::{BackgroundSession, Session, SessionUnmounter};
pub use session::{BackgroundSession, Session, Unmounter};
#[cfg(feature = "abi-7-28")]
use std::cmp::max;
#[cfg(feature = "abi-7-13")]
Expand Down Expand Up @@ -1008,7 +1008,8 @@ pub fn mount<FS: Filesystem, P: AsRef<Path>>(
}

/// Mount the given filesystem to the given mountpoint. This function will
/// not return until the filesystem is unmounted.
/// not return until the filesystem is unmounted. This function requires
/// CAP_SYS_ADMIN to run.
///
/// NOTE: This will eventually replace mount(), once the API is stable
pub fn mount2<FS: Filesystem, P: AsRef<Path>>(
Expand All @@ -1017,7 +1018,9 @@ pub fn mount2<FS: Filesystem, P: AsRef<Path>>(
options: &[MountOption],
) -> io::Result<()> {
check_option_conflicts(options, false)?;
Session::new(filesystem, mountpoint.as_ref(), options).and_then(|mut se| se.run())
let (mut session, _mount) = Session::new(filesystem, mountpoint, options)?;

session.run()
}

/// Mount the given filesystem using fusermount(1). The binary must exist on
Expand All @@ -1028,7 +1031,9 @@ pub fn fusermount(
options: &[MountOption],
) -> io::Result<()> {
check_option_conflicts(options, true)?;
Session::new_fusermount(filesystem, mountpoint, options).and_then(|mut se| se.run())
let (mut session, _mount) = Session::new_fusermount(filesystem, mountpoint, options)?;

session.run()
}

/// Mount the given filesystem to the given mountpoint. This function spawns
Expand Down Expand Up @@ -1066,7 +1071,9 @@ pub fn spawn_mount2<'a, FS: Filesystem + Send + 'static + 'a, P: AsRef<Path>>(
options: &[MountOption],
) -> io::Result<BackgroundSession> {
check_option_conflicts(options, false)?;
Session::new(filesystem, mountpoint.as_ref(), options).and_then(|se| se.spawn())

let (session, mount) = Session::new(filesystem, mountpoint, options)?;
BackgroundSession::new(session, mount)
}

/// Mount the given filesystem to the given mountpoint. This function spawns
Expand All @@ -1084,5 +1091,7 @@ pub fn spawn_fusermount<'a, FS: Filesystem + Send + 'static + 'a>(
options: &[MountOption],
) -> io::Result<BackgroundSession> {
check_option_conflicts(options, true)?;
Session::new_fusermount(filesystem, mountpoint, options).and_then(|se| se.spawn())

let (session, mount) = Session::new(filesystem, mountpoint, options)?;
BackgroundSession::new(session, mount)
}
Loading

0 comments on commit 9724cd3

Please sign in to comment.