Skip to content

Commit 9724cd3

Browse files
committed
Refactor Session to allow callers to mount it themselves
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
1 parent 12f05bb commit 9724cd3

File tree

7 files changed

+179
-105
lines changed

7 files changed

+179
-105
lines changed

examples/notify_inval_entry.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,9 @@ fn main() {
162162
timeout: Duration::from_secs_f32(opts.timeout),
163163
};
164164

165-
let session = fuser::Session::new(fs, opts.mount_point, &options).unwrap();
165+
let (session, mount) = fuser::Session::new(fs, opts.mount_point, &options).unwrap();
166166
let notifier = session.notifier();
167-
let _bg = session.spawn().unwrap();
167+
let _bg = fuser::BackgroundSession::new(session, mount);
168168

169169
loop {
170170
let mut fname = fname.lock().unwrap();

examples/notify_inval_inode.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,9 @@ fn main() {
200200
lookup_cnt,
201201
};
202202

203-
let session = fuser::Session::new(fs, opts.mount_point, &options).unwrap();
203+
let (session, mount) = fuser::Session::new(fs, opts.mount_point, &options).unwrap();
204204
let notifier = session.notifier();
205-
let _bg = session.spawn().unwrap();
205+
let _bg = fuser::BackgroundSession::new(session, mount);
206206

207207
loop {
208208
let mut s = fdata.lock().unwrap();

examples/poll.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,8 @@ fn main() {
337337
let fs = FSelFS { data: data.clone() };
338338

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

343343
producer(&data, &bg.notifier());
344344
}

src/lib.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub use reply::{
3939
ReplyStatfs, ReplyWrite,
4040
};
4141
pub use request::Request;
42-
pub use session::{BackgroundSession, Session, SessionUnmounter};
42+
pub use session::{BackgroundSession, Session, Unmounter};
4343
#[cfg(feature = "abi-7-28")]
4444
use std::cmp::max;
4545
#[cfg(feature = "abi-7-13")]
@@ -1008,7 +1008,8 @@ pub fn mount<FS: Filesystem, P: AsRef<Path>>(
10081008
}
10091009

10101010
/// Mount the given filesystem to the given mountpoint. This function will
1011-
/// not return until the filesystem is unmounted.
1011+
/// not return until the filesystem is unmounted. This function requires
1012+
/// CAP_SYS_ADMIN to run.
10121013
///
10131014
/// NOTE: This will eventually replace mount(), once the API is stable
10141015
pub fn mount2<FS: Filesystem, P: AsRef<Path>>(
@@ -1017,7 +1018,9 @@ pub fn mount2<FS: Filesystem, P: AsRef<Path>>(
10171018
options: &[MountOption],
10181019
) -> io::Result<()> {
10191020
check_option_conflicts(options, false)?;
1020-
Session::new(filesystem, mountpoint.as_ref(), options).and_then(|mut se| se.run())
1021+
let (mut session, _mount) = Session::new(filesystem, mountpoint, options)?;
1022+
1023+
session.run()
10211024
}
10221025

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

10341039
/// Mount the given filesystem to the given mountpoint. This function spawns
@@ -1066,7 +1071,9 @@ pub fn spawn_mount2<'a, FS: Filesystem + Send + 'static + 'a, P: AsRef<Path>>(
10661071
options: &[MountOption],
10671072
) -> io::Result<BackgroundSession> {
10681073
check_option_conflicts(options, false)?;
1069-
Session::new(filesystem, mountpoint.as_ref(), options).and_then(|se| se.spawn())
1074+
1075+
let (session, mount) = Session::new(filesystem, mountpoint, options)?;
1076+
BackgroundSession::new(session, mount)
10701077
}
10711078

10721079
/// Mount the given filesystem to the given mountpoint. This function spawns
@@ -1084,5 +1091,7 @@ pub fn spawn_fusermount<'a, FS: Filesystem + Send + 'static + 'a>(
10841091
options: &[MountOption],
10851092
) -> io::Result<BackgroundSession> {
10861093
check_option_conflicts(options, true)?;
1087-
Session::new_fusermount(filesystem, mountpoint, options).and_then(|se| se.spawn())
1094+
1095+
let (session, mount) = Session::new(filesystem, mountpoint, options)?;
1096+
BackgroundSession::new(session, mount)
10881097
}

0 commit comments

Comments
 (0)