Skip to content

Commit 250fbb3

Browse files
Xu Jihuiimeoer
authored andcommitted
refactor: Extract writer methods to FuseSessionExt trait
To reduce code duplication between the Linux and macOS FuseSession implementations, this change extracts the with_writer and try_with_writer helper methods into a new extension trait, FuseSessionExt. The trait provides default implementations for these methods, which rely on file() and bufsize() accessor methods that the concrete session types must provide. Both linux_session::FuseSession and macos_session::FuseSession now implement this trait, allowing them to share the common logic.
1 parent 809d1ad commit 250fbb3

File tree

5 files changed

+64
-30
lines changed

5 files changed

+64
-30
lines changed

src/transport/fusedev/linux_session.rs

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ use nix::poll::{poll, PollFd, PollFlags};
2424
use nix::sys::epoll::{epoll_ctl, EpollEvent, EpollFlags, EpollOp};
2525
use nix::unistd::{getgid, getuid, read};
2626

27+
use crate::transport::fusedev::FuseSessionExt;
28+
2729
use super::{
2830
super::pagesize,
2931
Error::{IoError, SessionFailure},
@@ -238,35 +240,6 @@ impl FuseSession {
238240
}
239241
}
240242

241-
/// Create a new fuse message writer and pass it to the given closure.
242-
pub fn with_writer<F>(&mut self, f: F)
243-
where
244-
F: FnOnce(FuseDevWriter),
245-
{
246-
if let Some(file) = &self.file {
247-
let fd = file.as_raw_fd();
248-
let mut buf = vec![0x0u8; self.bufsize];
249-
let writer = FuseDevWriter::new(fd, &mut buf).unwrap();
250-
f(writer);
251-
}
252-
}
253-
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-
270243
/// Wake channel loop and exit
271244
pub fn wake(&self) -> Result<()> {
272245
let wakers = self
@@ -297,6 +270,16 @@ impl Drop for FuseSession {
297270
}
298271
}
299272

273+
impl FuseSessionExt for FuseSession {
274+
fn file(&self) -> Option<&File> {
275+
self.file.as_ref()
276+
}
277+
278+
fn bufsize(&self) -> usize {
279+
self.bufsize
280+
}
281+
}
282+
300283
/// A fuse channel abstraction.
301284
///
302285
/// Each session can hold multiple channels.

src/transport/fusedev/macos_session.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,16 @@ impl Drop for FuseSession {
203203
}
204204
}
205205

206+
impl FuseSessionExt for FuseSession {
207+
fn file(&self) -> Option<&File> {
208+
self.file.as_ref()
209+
}
210+
211+
fn bufsize(&self) -> usize {
212+
self.bufsize
213+
}
214+
}
215+
206216
/// A fuse channel abstruction. Each session can hold multiple channels.
207217
pub struct FuseChannel {
208218
file: File,

src/transport/fusedev/mod.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use std::collections::VecDeque;
1111
use std::io::{self, IoSlice, Write};
1212
use std::marker::PhantomData;
1313
use std::mem::ManuallyDrop;
14+
use std::os::fd::AsRawFd;
1415
use std::os::unix::io::RawFd;
1516

1617
use nix::sys::uio::writev;
@@ -334,6 +335,45 @@ impl<S: BitmapSlice> Write for FuseDevWriter<'_, S> {
334335
}
335336
}
336337

338+
/// Extension trait for FuseSession to provide helper methods.
339+
pub trait FuseSessionExt {
340+
341+
/// Get the underlying file of the fuse session.
342+
fn file(&self) -> Option<&std::fs::File>;
343+
344+
/// Get the buffer size of the fuse session.
345+
fn bufsize(&self) -> usize;
346+
347+
/// Create a new fuse message writer and pass it to the given closure.
348+
fn with_writer<F>(&mut self, f: F)
349+
where
350+
F: FnOnce(FuseDevWriter),
351+
{
352+
if let Some(file) = self.file() {
353+
let fd = file.as_raw_fd();
354+
let mut buf = vec![0x0u8; self.bufsize()];
355+
let writer = FuseDevWriter::new(fd, &mut buf).unwrap();
356+
f(writer);
357+
}
358+
}
359+
360+
/// Create a new fuse message writer and pass it to the given closure. and return the result from the closure.
361+
fn try_with_writer<F, R, E>(&mut self, f: F) -> std::result::Result<R, E>
362+
where
363+
F: FnOnce(FuseDevWriter) -> std::result::Result<R, E>,
364+
E: From<Error>,
365+
{
366+
if let Some(file) = self.file() {
367+
let fd = file.as_raw_fd();
368+
let mut buf = vec![0x0u8; self.bufsize()];
369+
let writer = FuseDevWriter::new(fd, &mut buf)?;
370+
f(writer)
371+
} else {
372+
Err(Error::SessionFailure("invalid fuse session".into()).into())
373+
}
374+
}
375+
}
376+
337377
#[cfg(feature = "async-io")]
338378
mod async_io {
339379
use super::*;

src/transport/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ mod virtiofs;
4242

4343
pub use self::fs_cache_req_handler::FsCacheReqHandler;
4444
#[cfg(feature = "fusedev")]
45-
pub use self::fusedev::{FuseBuf, FuseChannel, FuseDevWriter, FuseSession};
45+
pub use self::fusedev::{FuseBuf, FuseChannel, FuseDevWriter, FuseSession, FuseSessionExt};
4646
#[cfg(feature = "virtiofs")]
4747
pub use self::virtiofs::VirtioFsWriter;
4848

tests/passthrough/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use fuse_backend_rs::transport::FuseSessionExt as _;
12
use log::{error, info, warn, LevelFilter};
23
use std::env;
34
use std::fs;

0 commit comments

Comments
 (0)