Skip to content

Commit 890e5b1

Browse files
jiangliubergwolf
authored andcommitted
transport: refine code syntax, no functional change
Refine code syntax, no functional change. Signed-off-by: Jiang Liu <[email protected]>
1 parent 7cb2f01 commit 890e5b1

File tree

4 files changed

+112
-119
lines changed

4 files changed

+112
-119
lines changed

src/transport/fusedev/fuse_t_session.rs

+25-25
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use vm_memory::ByteValued;
2727

2828
use super::{
2929
Error::IoError, Error::SessionFailure, FuseBuf, FuseDevWriter, Reader, Result,
30-
FUSE_HEADER_SIZE, FUSE_KERN_BUF_SIZE,
30+
FUSE_HEADER_SIZE, FUSE_KERN_BUF_PAGES,
3131
};
3232
use crate::transport::pagesize;
3333

@@ -112,23 +112,13 @@ impl FuseSession {
112112
subtype: subtype.to_owned(),
113113
file: None,
114114
file_lock: Arc::new(Mutex::new(())),
115-
bufsize: FUSE_KERN_BUF_SIZE * pagesize() + FUSE_HEADER_SIZE,
115+
bufsize: FUSE_KERN_BUF_PAGES * pagesize() + FUSE_HEADER_SIZE,
116116
monitor_file: None,
117117
wait_handle: None,
118118
readonly,
119119
})
120120
}
121121

122-
/// Mount the fuse mountpoint, building connection with the in kernel fuse driver.
123-
pub fn mount(&mut self) -> Result<()> {
124-
let files = fuse_kern_mount(&self.mountpoint, &self.fsname, &self.subtype, self.readonly)?;
125-
self.file = Some(files.0);
126-
self.monitor_file = Some(files.1);
127-
self.wait_handle = Some(self.send_mount_command()?);
128-
129-
Ok(())
130-
}
131-
132122
/// Expose the associated FUSE session file.
133123
pub fn get_fuse_file(&self) -> Option<&File> {
134124
self.file.as_ref()
@@ -139,19 +129,6 @@ impl FuseSession {
139129
self.file = Some(file);
140130
}
141131

142-
/// Destroy a fuse session.
143-
pub fn umount(&mut self) -> Result<()> {
144-
if let Some(file) = self.monitor_file.take() {
145-
if self.mountpoint.to_str().is_some() {
146-
fuse_kern_umount(file)
147-
} else {
148-
Err(SessionFailure("invalid mountpoint".to_string()))
149-
}
150-
} else {
151-
Ok(())
152-
}
153-
}
154-
155132
/// Get the mountpoint of the session.
156133
pub fn mountpoint(&self) -> &Path {
157134
&self.mountpoint
@@ -172,6 +149,29 @@ impl FuseSession {
172149
self.bufsize
173150
}
174151

152+
/// Mount the fuse mountpoint, building connection with the in kernel fuse driver.
153+
pub fn mount(&mut self) -> Result<()> {
154+
let files = fuse_kern_mount(&self.mountpoint, &self.fsname, &self.subtype, self.readonly)?;
155+
self.file = Some(files.0);
156+
self.monitor_file = Some(files.1);
157+
self.wait_handle = Some(self.send_mount_command()?);
158+
159+
Ok(())
160+
}
161+
162+
/// Destroy a fuse session.
163+
pub fn umount(&mut self) -> Result<()> {
164+
if let Some(file) = self.monitor_file.take() {
165+
if self.mountpoint.to_str().is_some() {
166+
fuse_kern_umount(file)
167+
} else {
168+
Err(SessionFailure("invalid mountpoint".to_string()))
169+
}
170+
} else {
171+
Ok(())
172+
}
173+
}
174+
175175
/// Create a new fuse message channel.
176176
pub fn new_channel(&self) -> Result<FuseChannel> {
177177
if let Some(file) = &self.file {

src/transport/fusedev/linux_session.rs

+46-46
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
//! sequentially. A FUSE session is a connection from a FUSE mountpoint to a FUSE server daemon.
99
//! A FUSE session can have multiple FUSE channels so that FUSE requests are handled in parallel.
1010
11-
use mio::{Events, Poll, Token, Waker};
1211
use std::fs::{File, OpenOptions};
1312
use std::ops::Deref;
1413
use std::os::unix::fs::PermissionsExt;
@@ -17,6 +16,7 @@ use std::os::unix::net::UnixStream;
1716
use std::path::{Path, PathBuf};
1817
use std::sync::{Arc, Mutex};
1918

19+
use mio::{Events, Poll, Token, Waker};
2020
use nix::errno::Errno;
2121
use nix::fcntl::{fcntl, FcntlArg, FdFlag, OFlag};
2222
use nix::mount::{mount, umount2, MntFlags, MsFlags};
@@ -27,7 +27,7 @@ use nix::unistd::{getgid, getuid, read};
2727
use super::{
2828
super::pagesize,
2929
Error::{IoError, SessionFailure},
30-
FuseBuf, FuseDevWriter, Reader, Result, FUSE_HEADER_SIZE, FUSE_KERN_BUF_SIZE,
30+
FuseBuf, FuseDevWriter, Reader, Result, FUSE_HEADER_SIZE, FUSE_KERN_BUF_PAGES,
3131
};
3232

3333
// These follows definition from libfuse.
@@ -90,7 +90,7 @@ impl FuseSession {
9090
subtype: subtype.to_owned(),
9191
file: None,
9292
keep_alive: None,
93-
bufsize: FUSE_KERN_BUF_SIZE * pagesize() + FUSE_HEADER_SIZE,
93+
bufsize: FUSE_KERN_BUF_PAGES * pagesize() + FUSE_HEADER_SIZE,
9494
readonly,
9595
wakers: Mutex::new(Vec::new()),
9696
auto_unmount,
@@ -123,6 +123,36 @@ impl FuseSession {
123123
self.fusermount.as_str()
124124
}
125125

126+
/// Expose the associated FUSE session file.
127+
pub fn get_fuse_file(&self) -> Option<&File> {
128+
self.file.as_ref()
129+
}
130+
131+
/// Force setting the associated FUSE session file.
132+
pub fn set_fuse_file(&mut self, file: File) {
133+
self.file = Some(file);
134+
}
135+
136+
/// Get the mountpoint of the session.
137+
pub fn mountpoint(&self) -> &Path {
138+
&self.mountpoint
139+
}
140+
141+
/// Get the file system name of the session.
142+
pub fn fsname(&self) -> &str {
143+
&self.fsname
144+
}
145+
146+
/// Get the subtype of the session.
147+
pub fn subtype(&self) -> &str {
148+
&self.subtype
149+
}
150+
151+
/// Get the default buffer size of the session.
152+
pub fn bufsize(&self) -> usize {
153+
self.bufsize
154+
}
155+
126156
/// Mount the fuse mountpoint, building connection with the in kernel fuse driver.
127157
pub fn mount(&mut self) -> Result<()> {
128158
let mut flags = MsFlags::MS_NOSUID | MsFlags::MS_NODEV | MsFlags::MS_NOATIME;
@@ -148,16 +178,6 @@ impl FuseSession {
148178
Ok(())
149179
}
150180

151-
/// Expose the associated FUSE session file.
152-
pub fn get_fuse_file(&self) -> Option<&File> {
153-
self.file.as_ref()
154-
}
155-
156-
/// Force setting the associated FUSE session file.
157-
pub fn set_fuse_file(&mut self, file: File) {
158-
self.file = Some(file);
159-
}
160-
161181
/// Destroy a fuse session.
162182
pub fn umount(&mut self) -> Result<()> {
163183
// If we have a keep_alive socket, just drop it,
@@ -173,26 +193,6 @@ impl FuseSession {
173193
}
174194
}
175195

176-
/// Get the mountpoint of the session.
177-
pub fn mountpoint(&self) -> &Path {
178-
&self.mountpoint
179-
}
180-
181-
/// Get the file system name of the session.
182-
pub fn fsname(&self) -> &str {
183-
&self.fsname
184-
}
185-
186-
/// Get the subtype of the session.
187-
pub fn subtype(&self) -> &str {
188-
&self.subtype
189-
}
190-
191-
/// Get the default buffer size of the session.
192-
pub fn bufsize(&self) -> usize {
193-
self.bufsize
194-
}
195-
196196
/// Create a new fuse message channel.
197197
pub fn new_channel(&self) -> Result<FuseChannel> {
198198
if let Some(file) = &self.file {
@@ -209,15 +209,6 @@ impl FuseSession {
209209
}
210210
}
211211

212-
fn add_waker(&self, waker: Arc<Waker>) -> Result<()> {
213-
let mut wakers = self
214-
.wakers
215-
.lock()
216-
.map_err(|e| SessionFailure(format!("lock wakers: {e}")))?;
217-
wakers.push(waker);
218-
Ok(())
219-
}
220-
221212
/// Wake channel loop and exit
222213
pub fn wake(&self) -> Result<()> {
223214
let wakers = self
@@ -231,6 +222,15 @@ impl FuseSession {
231222
}
232223
Ok(())
233224
}
225+
226+
fn add_waker(&self, waker: Arc<Waker>) -> Result<()> {
227+
let mut wakers = self
228+
.wakers
229+
.lock()
230+
.map_err(|e| SessionFailure(format!("lock wakers: {e}")))?;
231+
wakers.push(waker);
232+
Ok(())
233+
}
234234
}
235235

236236
impl Drop for FuseSession {
@@ -442,7 +442,7 @@ fn fuse_kern_mount(
442442
Some(opts.deref()),
443443
) {
444444
Ok(()) => Ok((file, None)),
445-
Err(nix::errno::Errno::EPERM) => fuse_fusermount_mount(
445+
Err(Errno::EPERM) => fuse_fusermount_mount(
446446
mountpoint,
447447
fsname,
448448
subtype,
@@ -507,7 +507,7 @@ fn fuse_fusermount_mount(
507507
// When its partner recv closes, fusermount will unmount.
508508
// Remove the close-on-exec flag from the socket, so we can pass it to
509509
// fusermount.
510-
nix::fcntl::fcntl(send.as_raw_fd(), FcntlArg::F_SETFD(FdFlag::empty()))
510+
fcntl(send.as_raw_fd(), FcntlArg::F_SETFD(FdFlag::empty()))
511511
.map_err(|e| SessionFailure(format!("Failed to remove close-on-exec flag: {e}")))?;
512512

513513
let mut cmd = match target_mntns {
@@ -581,14 +581,14 @@ fn fuse_kern_umount(mountpoint: &str, file: File, fusermount: &str) -> Result<()
581581
drop(file);
582582
match umount2(mountpoint, MntFlags::MNT_DETACH) {
583583
Ok(()) => Ok(()),
584-
Err(nix::errno::Errno::EPERM) => fuse_fusermount_umount(mountpoint, fusermount),
584+
Err(Errno::EPERM) => fuse_fusermount_umount(mountpoint, fusermount),
585585
Err(e) => Err(SessionFailure(format!(
586586
"failed to umount {mountpoint}: {e}"
587587
))),
588588
}
589589
}
590590

591-
/// Umount a fuse file system
591+
/// Umount a fuse file system by fusermount helper
592592
fn fuse_fusermount_umount(mountpoint: &str, fusermount: &str) -> Result<()> {
593593
match std::process::Command::new(fusermount)
594594
.arg("--unmount")

src/transport/fusedev/macos_session.rs

+33-34
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
//! sequentially. A FUSE session is a connection from a FUSE mountpoint to a FUSE server daemon.
99
//! A FUSE session can have multiple FUSE channels so that FUSE requests are handled in parallel.
1010
11-
use core_foundation_sys::base::{CFAllocatorRef, CFIndex, CFRelease};
12-
use core_foundation_sys::string::{kCFStringEncodingUTF8, CFStringCreateWithBytes};
13-
use core_foundation_sys::url::{kCFURLPOSIXPathStyle, CFURLCreateWithFileSystemPath, CFURLRef};
1411
use std::ffi::CString;
1512
use std::fs::File;
1613
use std::io::IoSliceMut;
@@ -20,6 +17,9 @@ use std::path::{Path, PathBuf};
2017
use std::sync::atomic::{AtomicPtr, Ordering};
2118
use std::sync::{Arc, Mutex};
2219

20+
use core_foundation_sys::base::{CFAllocatorRef, CFIndex, CFRelease};
21+
use core_foundation_sys::string::{kCFStringEncodingUTF8, CFStringCreateWithBytes};
22+
use core_foundation_sys::url::{kCFURLPOSIXPathStyle, CFURLCreateWithFileSystemPath, CFURLRef};
2323
use libc::{c_void, proc_pidpath, PROC_PIDPATHINFO_MAXSIZE};
2424
use nix::errno::Errno;
2525
use nix::fcntl::{fcntl, FdFlag, F_SETFD};
@@ -33,7 +33,7 @@ use nix::{cmsg_space, NixPath};
3333

3434
use super::{
3535
Error::IoError, Error::SessionFailure, FuseBuf, FuseDevWriter, Reader, Result,
36-
FUSE_HEADER_SIZE, FUSE_KERN_BUF_SIZE,
36+
FUSE_HEADER_SIZE, FUSE_KERN_BUF_PAGES,
3737
};
3838
use crate::transport::pagesize;
3939

@@ -51,9 +51,8 @@ type DADissenterRef = *const __DADissenter;
5151
struct __DASession(c_void);
5252
type DASessionRef = *const __DASession;
5353

54-
type DADiskUnmountCallback = ::std::option::Option<
55-
unsafe extern "C" fn(disk: DADiskRef, dissenter: DADissenterRef, context: *mut c_void),
56-
>;
54+
type DADiskUnmountCallback =
55+
Option<unsafe extern "C" fn(disk: DADiskRef, dissenter: DADissenterRef, context: *mut c_void)>;
5756

5857
extern "C" {
5958
fn DADiskUnmount(
@@ -113,7 +112,7 @@ impl FuseSession {
113112
fsname: fsname.to_owned(),
114113
subtype: subtype.to_owned(),
115114
file: None,
116-
bufsize: FUSE_KERN_BUF_SIZE * pagesize() + FUSE_HEADER_SIZE,
115+
bufsize: FUSE_KERN_BUF_PAGES * pagesize() + FUSE_HEADER_SIZE,
117116
disk: Mutex::new(None),
118117
dasession: Arc::new(AtomicPtr::new(unsafe {
119118
DASessionCreate(std::ptr::null()) as *mut c_void
@@ -122,18 +121,6 @@ impl FuseSession {
122121
})
123122
}
124123

125-
/// Mount the fuse mountpoint, building connection with the in kernel fuse driver.
126-
pub fn mount(&mut self) -> Result<()> {
127-
let mut disk = self.disk.lock().expect("lock disk failed");
128-
let file = fuse_kern_mount(&self.mountpoint, &self.fsname, &self.subtype, self.readonly)?;
129-
let session = self.dasession.load(Ordering::SeqCst);
130-
let mount_disk = create_disk(&self.mountpoint, session as DASessionRef);
131-
self.file = Some(file);
132-
*disk = Some(mount_disk);
133-
134-
Ok(())
135-
}
136-
137124
/// Expose the associated FUSE session file.
138125
pub fn get_fuse_file(&self) -> Option<&File> {
139126
self.file.as_ref()
@@ -144,20 +131,6 @@ impl FuseSession {
144131
self.file = Some(file);
145132
}
146133

147-
/// Destroy a fuse session.
148-
pub fn umount(&mut self) -> Result<()> {
149-
if let Some(file) = self.file.take() {
150-
if self.mountpoint.to_str().is_some() {
151-
let mut disk = self.disk.lock().expect("lock disk failed");
152-
fuse_kern_umount(file, disk.take())
153-
} else {
154-
Err(SessionFailure("invalid mountpoint".to_string()))
155-
}
156-
} else {
157-
Ok(())
158-
}
159-
}
160-
161134
/// Get the mountpoint of the session.
162135
pub fn mountpoint(&self) -> &Path {
163136
&self.mountpoint
@@ -178,6 +151,32 @@ impl FuseSession {
178151
self.bufsize
179152
}
180153

154+
/// Mount the fuse mountpoint, building connection with the in kernel fuse driver.
155+
pub fn mount(&mut self) -> Result<()> {
156+
let mut disk = self.disk.lock().expect("lock disk failed");
157+
let file = fuse_kern_mount(&self.mountpoint, &self.fsname, &self.subtype, self.readonly)?;
158+
let session = self.dasession.load(Ordering::SeqCst);
159+
let mount_disk = create_disk(&self.mountpoint, session as DASessionRef);
160+
self.file = Some(file);
161+
*disk = Some(mount_disk);
162+
163+
Ok(())
164+
}
165+
166+
/// Destroy a fuse session.
167+
pub fn umount(&mut self) -> Result<()> {
168+
if let Some(file) = self.file.take() {
169+
if self.mountpoint.to_str().is_some() {
170+
let mut disk = self.disk.lock().expect("lock disk failed");
171+
fuse_kern_umount(file, disk.take())
172+
} else {
173+
Err(SessionFailure("invalid mountpoint".to_string()))
174+
}
175+
} else {
176+
Ok(())
177+
}
178+
}
179+
181180
/// Create a new fuse message channel.
182181
pub fn new_channel(&self) -> Result<FuseChannel> {
183182
if let Some(file) = &self.file {

0 commit comments

Comments
 (0)