From 07f1987d028b4cf35f3a287085bb8b3e79cb917b Mon Sep 17 00:00:00 2001 From: Alessandro Passaro Date: Wed, 26 Jul 2023 11:21:32 +0100 Subject: [PATCH] Add callbacks to a fuse session run Introduce `Session::run_with_callbacks()` to notify callers before and after kernel messages are dispatched. Signed-off-by: Alessandro Passaro --- src/session.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/session.rs b/src/session.rs index 8adef584c..b91497b77 100644 --- a/src/session.rs +++ b/src/session.rs @@ -116,6 +116,18 @@ impl Session { /// 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(&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]; @@ -129,7 +141,11 @@ impl Session { 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, },