Skip to content

Commit 809d1ad

Browse files
Xu Jihuiimeoer
authored andcommitted
feat(fusedev): add try_with_writer on FuseSession; update
`with_writer` docs - Introduce `try_with_writer<F, R, E>` that builds a `FuseDevWriter` and returns the closure’s `Result`, converting `super::Error` via `From`. - Return `SessionFailure("invalid fuse session")` when `self.file` is absent. - Tweak `with_writer` doc to clarify it passes a writer to the given closure. example(passthrough): update example to show how to use try_with_writer
1 parent 2f6ff6f commit 809d1ad

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

src/transport/fusedev/linux_session.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ impl FuseSession {
238238
}
239239
}
240240

241-
/// Create a new fuse message channel with a specific buffer size.
241+
/// Create a new fuse message writer and pass it to the given closure.
242242
pub fn with_writer<F>(&mut self, f: F)
243243
where
244244
F: FnOnce(FuseDevWriter),
@@ -251,6 +251,22 @@ impl FuseSession {
251251
}
252252
}
253253

254+
/// Create a new fuse message writer and pass it to the given closure. and return the result from the closure.
255+
pub fn try_with_writer<F, R, E>(&mut self, f: F) -> std::result::Result<R, E>
256+
where
257+
F: FnOnce(FuseDevWriter) -> std::result::Result<R, E>,
258+
E: From<super::Error>,
259+
{
260+
if let Some(file) = &self.file {
261+
let fd = file.as_raw_fd();
262+
let mut buf = vec![0x0u8; self.bufsize];
263+
let writer = FuseDevWriter::new(fd, &mut buf)?;
264+
f(writer)
265+
} else {
266+
Err(SessionFailure("invalid fuse session".into()).into())
267+
}
268+
}
269+
254270
/// Wake channel loop and exit
255271
pub fn wake(&self) -> Result<()> {
256272
let wakers = self

tests/passthrough/src/main.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ pub struct Daemon {
2323
session: Option<FuseSession>,
2424
}
2525

26+
pub enum PassthroughFsError {
27+
FuseError(fuse_backend_rs::Error),
28+
TransportError(fuse_backend_rs::transport::Error),
29+
}
30+
31+
impl From<fuse_backend_rs::transport::Error> for PassthroughFsError {
32+
fn from(e: fuse_backend_rs::transport::Error) -> Self {
33+
PassthroughFsError::TransportError(e)
34+
}
35+
}
36+
2637
#[allow(dead_code)]
2738
impl Daemon {
2839
/// Creates a fusedev daemon instance
@@ -59,11 +70,10 @@ impl Daemon {
5970
FuseSession::new(Path::new(&self.mountpoint), "testpassthrough", "", false).unwrap();
6071
se.mount().unwrap();
6172

62-
se.with_writer(|writer| {
73+
se.try_with_writer(|writer| {
6374
self.server
64-
.notify_resend(writer)
65-
.unwrap_or_else(|e| println!("failed to send resend notification {}", e));
66-
});
75+
.notify_resend(writer).map_err(PassthroughFsError::FuseError)
76+
}).map_err(|_| Error::from_raw_os_error(libc::EINVAL))?;
6777

6878
for _ in 0..self.thread_cnt {
6979
let mut server = FuseServer {

0 commit comments

Comments
 (0)