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

feat: add ofs ctrl-c exit unmount hook #4393

Merged
merged 4 commits into from
Mar 26, 2024
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
112 changes: 95 additions & 17 deletions bin/ofs/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion bin/ofs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ tokio = { version = "1.34", features = [
"macros",
"rt-multi-thread",
"io-std",
"signal",
] }
url = "2.5.0"
chrono = "0.4.34"
Expand All @@ -49,7 +50,7 @@ bytes = "1.5.0"

[target.'cfg(target_os = "linux")'.dependencies]
libc = "0.2.151"
fuse3 = { "version" = "0.6.1", "features" = ["tokio-runtime", "unprivileged"] }
fuse3 = { "version" = "0.7.1", "features" = ["tokio-runtime", "unprivileged"] }
nix = { version = "0.27.1", features = ["user"] }

[features]
Expand Down
30 changes: 16 additions & 14 deletions bin/ofs/src/fuse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@

use std::ffi::OsStr;
use std::ffi::OsString;
use std::num::NonZeroU32;
use std::ops::Deref;
use std::path::PathBuf;
use std::time::Duration;
use std::time::SystemTime;

use bytes::Bytes;
use fuse3::async_trait;
use fuse3::path::prelude::*;
use fuse3::Errno;
use fuse3::Result;
Expand Down Expand Up @@ -127,14 +127,15 @@ impl Fuse {
}
}

#[async_trait]
impl PathFilesystem for Fuse {
type DirEntryStream = BoxStream<'static, Result<DirectoryEntry>>;
type DirEntryPlusStream = BoxStream<'static, Result<DirectoryEntryPlus>>;
type DirEntryStream<'a> = BoxStream<'a, Result<DirectoryEntry>>;
type DirEntryPlusStream<'a> = BoxStream<'a, Result<DirectoryEntryPlus>>;

// Init a fuse filesystem
async fn init(&self, _req: Request) -> Result<()> {
Ok(())
async fn init(&self, _req: Request) -> Result<ReplyInit> {
Ok(ReplyInit {
max_write: NonZeroU32::new(16 * 1024).unwrap(),
})
}

// Callback when fs is being destroyed
Expand Down Expand Up @@ -474,6 +475,7 @@ impl PathFilesystem for Fuse {
fh: u64,
offset: u64,
data: &[u8],
_write_flags: u32,
flags: u32,
) -> Result<ReplyWrite> {
log::debug!(
Expand Down Expand Up @@ -508,13 +510,13 @@ impl PathFilesystem for Fuse {
})
}

async fn readdir(
&self,
async fn readdir<'a>(
&'a self,
_req: Request,
path: &OsStr,
path: &'a OsStr,
fh: u64,
offset: i64,
) -> Result<ReplyDirectory<Self::DirEntryStream>> {
) -> Result<ReplyDirectory<Self::DirEntryStream<'a>>> {
log::debug!("readdir(path={:?}, fh={}, offset={})", path, fh, offset);

let mut current_dir = PathBuf::from(path);
Expand Down Expand Up @@ -565,14 +567,14 @@ impl PathFilesystem for Fuse {
Ok(())
}

async fn readdirplus(
&self,
async fn readdirplus<'a>(
&'a self,
_req: Request,
parent: &OsStr,
parent: &'a OsStr,
fh: u64,
offset: u64,
_lock_owner: u64,
) -> Result<ReplyDirectoryPlus<Self::DirEntryPlusStream>> {
) -> Result<ReplyDirectoryPlus<Self::DirEntryPlusStream<'a>>> {
log::debug!(
"readdirplus(parent={:?}, fh={}, offset={})",
parent,
Expand Down
12 changes: 10 additions & 2 deletions bin/ofs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use anyhow::anyhow;
use anyhow::Result;
use opendal::Operator;
use opendal::Scheme;
use tokio::signal;

pub mod config;
pub use config::Config;
Expand Down Expand Up @@ -83,11 +84,18 @@ async fn execute_inner(args: Args) -> Result<()> {

let adapter = fuse::Fuse::new(args.backend, uid.into(), gid.into());

let mount_handle = Session::new(mount_option)
let mut mount_handle = Session::new(mount_option)
.mount_with_unprivileged(adapter, args.mount_path)
.await?;

mount_handle.await?;
let handle = &mut mount_handle;

tokio::select! {
res = handle => res?,
_ = signal::ctrl_c() => {
mount_handle.unmount().await?
}
}

Ok(())
}
Loading