Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fusedev: add clone_fuse_file method to FuseSession #177

Merged
merged 2 commits into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ jobs:
- name: smoke
if: ${{ matrix.arch == 'amd64' }}
run: |
make test
echo user_allow_other | sudo tee --append /etc/fuse.conf
make test
make smoke-all

Macos-CI:
Expand Down
43 changes: 43 additions & 0 deletions src/transport/fusedev/linux_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,35 @@ impl FuseSession {
self.file = Some(file);
}

/// Clone fuse file using ioctl FUSE_DEV_IOC_CLONE.
pub fn clone_fuse_file(&self) -> Result<File> {
let mut old_fd = self
.file
.as_ref()
.ok_or(SessionFailure(
"fuse session file doesn't exist".to_string(),
))?
.as_raw_fd();

let cloned_file = OpenOptions::new()
.create(false)
.read(true)
.write(true)
.open(FUSE_DEVICE)
.map_err(|e| SessionFailure(format!("open {FUSE_DEVICE}: {e}")))?;

// define the function which invokes "ioctl FUSE_DEV_IOC_CLONE"
// refer: https://github.com/torvalds/linux/blob/c42d9eeef8e5ba9292eda36fd8e3c11f35ee065c/include/uapi/linux/fuse.h#L1051-L1052
// #define FUSE_DEV_IOC_MAGIC 229
// #define FUSE_DEV_IOC_CLONE _IOR(FUSE_DEV_IOC_MAGIC, 0, uint32_t)
nix::ioctl_read!(clone_fuse_fd, 229, 0, i32);

unsafe { clone_fuse_fd(cloned_file.as_raw_fd(), (&mut old_fd) as *mut i32) }
.map_err(|e| SessionFailure(format!("failed to clone fuse file: {:?}", e)))?;

Ok(cloned_file)
}

/// Get the mountpoint of the session.
pub fn mountpoint(&self) -> &Path {
&self.mountpoint
Expand Down Expand Up @@ -643,6 +672,20 @@ mod tests {
se.set_fusermount("fusermount");
assert_eq!(se.get_fusermount(), "fusermount");
}

#[test]
fn test_clone_fuse_file() {
let dir = TempDir::new().unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let mut se = FuseSession::new(dir.as_path(), "foo", "bar", true).unwrap();?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

let mut se = FuseSession::new(dir.as_path(), "foo", "bar", true).unwrap();
se.mount().unwrap();

let cloned_file = se.clone_fuse_file().unwrap();
assert!(cloned_file.as_raw_fd() > 0);

se.umount().unwrap();
se.set_fuse_file(cloned_file);
se.mount().unwrap();
}
}

#[cfg(feature = "async_io")]
Expand Down