Skip to content

Commit

Permalink
Add callbacks to a fuse session run
Browse files Browse the repository at this point in the history
Introduce `Session::run_with_callbacks()` to notify callers before and after kernel
messages are dispatched.

Signed-off-by: Alessandro Passaro <alexpax@amazon.co.uk>
  • Loading branch information
passaro committed Jul 28, 2023
1 parent b1fe66c commit 07f1987
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,18 @@ impl<FS: Filesystem> Session<FS> {
/// Run the session loop that receives kernel requests and dispatches them to method
/// calls into the filesystem.
pub fn run(&self) -> io::Result<()> {
self.run_with_callbacks(|_| {}, |_| {})
}

/// Run the session loop that receives kernel requests and dispatches them to method
/// calls into the filesystem.
/// This version also notifies callers of kernel requests before and after they
/// are dispatched to the filesystem.
pub fn run_with_callbacks<FA, FB>(&self, mut before_dispatch: FB, mut after_dispatch: FA) -> io::Result<()>
where
FB: FnMut(&Request<'_>),
FA: FnMut(&Request<'_>),
{
// Buffer for receiving requests from the kernel. Only one is allocated and
// it is reused immediately after dispatching to conserve memory and allocations.
let mut buffer = vec![0; BUFFER_SIZE];
Expand All @@ -129,7 +141,11 @@ impl<FS: Filesystem> Session<FS> {
match self.ch.receive(buf) {
Ok(size) => match Request::new(self.ch.sender(), &buf[..size]) {
// Dispatch request
Some(req) => req.dispatch(self),
Some(req) => {
before_dispatch(&req);
req.dispatch(self);
after_dispatch(&req);
},
// Quit loop on illegal request
None => break,
},
Expand Down

0 comments on commit 07f1987

Please sign in to comment.