You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opendal.apache.org by ju...@apache.org on 2024/03/25 14:25:24 UTC

(opendal) 01/01: feat: add ofs ctrl-c exit unmount hook

This is an automated email from the ASF dual-hosted git repository.

junouyang pushed a commit to branch feat/ofs-unmount-ctrl-c
in repository https://gitbox.apache.org/repos/asf/opendal.git

commit 5a85ae82e78d457445b69e6dbff87a8a00bfcc76
Author: Jun Ouyang <ou...@gmail.com>
AuthorDate: Mon Mar 25 22:25:10 2024 +0800

    feat: add ofs ctrl-c exit unmount hook
---
 bin/ofs/Cargo.lock  | 108 ++++++++++++++++++++++++++++++++++++++++------------
 bin/ofs/Cargo.toml  |   3 +-
 bin/ofs/src/fuse.rs |   3 +-
 bin/ofs/src/lib.rs  |  12 +++++-
 4 files changed, 97 insertions(+), 29 deletions(-)

diff --git a/bin/ofs/Cargo.lock b/bin/ofs/Cargo.lock
index fe9aa95626..60c92a067c 100644
--- a/bin/ofs/Cargo.lock
+++ b/bin/ofs/Cargo.lock
@@ -95,11 +95,22 @@ version = "1.0.80"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "5ad32ce52e4161730f7098c077cd2ed6229b5804ccf99e5366be1ab72a98b4e1"
 
+[[package]]
+name = "async-notify"
+version = "0.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "db96c36382f56ea91e5d012c4c7170046c7731449daa46655cbcabc594c8bee6"
+dependencies = [
+ "event-listener",
+ "futures-core",
+ "pin-project-lite",
+]
+
 [[package]]
 name = "async-trait"
-version = "0.1.77"
+version = "0.1.79"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "c980ee35e870bd1a4d2c8294d4c04d0499e67bca1e4b5cefcc693c2fa00caea9"
+checksum = "a507401cad91ec6a857ed5513a2073c82a9b9048762b885bb98655b306964681"
 dependencies = [
  "proc-macro2",
  "quote",
@@ -262,6 +273,15 @@ version = "1.0.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7"
 
+[[package]]
+name = "concurrent-queue"
+version = "2.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d16048cd947b08fa32c24458a22f5dc5e835264f689f4f5653210c69fd107363"
+dependencies = [
+ "crossbeam-utils",
+]
+
 [[package]]
 name = "const-oid"
 version = "0.9.6"
@@ -313,6 +333,12 @@ dependencies = [
  "libc",
 ]
 
+[[package]]
+name = "crossbeam-utils"
+version = "0.8.19"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "248e3bacc7dc6baa3b21e405ee045c3047101a49145e7e9eca583ab4c2ca5345"
+
 [[package]]
 name = "crunchy"
 version = "0.2.2"
@@ -414,6 +440,17 @@ dependencies = [
  "windows-sys 0.52.0",
 ]
 
+[[package]]
+name = "event-listener"
+version = "4.0.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "67b215c49b2b248c855fb73579eb1f4f26c38ffdc12973e20e07b91d78d5646e"
+dependencies = [
+ "concurrent-queue",
+ "parking",
+ "pin-project-lite",
+]
+
 [[package]]
 name = "fastrand"
 version = "2.0.1"
@@ -443,22 +480,23 @@ dependencies = [
 
 [[package]]
 name = "fuse3"
-version = "0.6.1"
+version = "0.7.0-alpha.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "8aaac75c1369ee81ed34a8e26216258126a3c555bfc85e49eb33b5e239fd182e"
+checksum = "5f2c31ac1f50145d6df478922d47ac1f4dca17dedeb7ac82640c84737170dd1c"
 dependencies = [
- "async-trait",
+ "async-notify",
  "bincode",
  "bytes",
  "cstr",
  "futures-channel",
  "futures-util",
  "libc",
- "nix 0.26.4",
+ "nix",
  "serde",
  "slab",
  "tokio",
  "tracing",
+ "trait-make",
  "which",
 ]
 
@@ -813,9 +851,9 @@ checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149"
 
 [[package]]
 name = "memoffset"
-version = "0.7.1"
+version = "0.9.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "5de893c32cde5f383baa4c04c5d6dbdd735cfd4a794b0debdb2bb1b421da5ff4"
+checksum = "5a634b1c61a95585bd15607c6ab0c4e5b226e695ff2800ba0cdccddf208c406c"
 dependencies = [
  "autocfg",
 ]
@@ -846,18 +884,6 @@ dependencies = [
  "windows-sys 0.48.0",
 ]
 
-[[package]]
-name = "nix"
-version = "0.26.4"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "598beaf3cc6fdd9a5dfb1630c2800c7acd31df7aaf0f565796fba2b53ca1af1b"
-dependencies = [
- "bitflags 1.3.2",
- "cfg-if",
- "libc",
- "memoffset",
-]
-
 [[package]]
 name = "nix"
 version = "0.27.1"
@@ -867,6 +893,7 @@ dependencies = [
  "bitflags 2.4.2",
  "cfg-if",
  "libc",
+ "memoffset",
 ]
 
 [[package]]
@@ -910,7 +937,7 @@ dependencies = [
  "futures-util",
  "libc",
  "log",
- "nix 0.27.1",
+ "nix",
  "opendal",
  "sharded-slab",
  "tokio",
@@ -960,6 +987,12 @@ dependencies = [
  "hashbrown",
 ]
 
+[[package]]
+name = "parking"
+version = "2.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bb813b8af86854136c6922af0598d719255ecb2179515e6e7730d468f05c9cae"
+
 [[package]]
 name = "percent-encoding"
 version = "2.3.1"
@@ -1326,6 +1359,15 @@ dependencies = [
  "lazy_static",
 ]
 
+[[package]]
+name = "signal-hook-registry"
+version = "1.4.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d8229b473baa5980ac72ef434c4415e70c4b5e71b423043adb4ba059f89c99a1"
+dependencies = [
+ "libc",
+]
+
 [[package]]
 name = "slab"
 version = "0.4.9"
@@ -1437,6 +1479,7 @@ dependencies = [
  "mio",
  "num_cpus",
  "pin-project-lite",
+ "signal-hook-registry",
  "socket2",
  "tokio-macros",
  "windows-sys 0.48.0",
@@ -1514,6 +1557,17 @@ dependencies = [
  "once_cell",
 ]
 
+[[package]]
+name = "trait-make"
+version = "0.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "96cbd06a7b648f1603e60d75d9ed295d096b340d30e9f9324f4b512b5d40cd92"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
 [[package]]
 name = "try-lock"
 version = "0.2.5"
@@ -1698,14 +1752,14 @@ checksum = "5f20c57d8d7db6d3b86154206ae5d8fba62dd39573114de97c2cb0578251f8e1"
 
 [[package]]
 name = "which"
-version = "4.4.2"
+version = "6.0.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "87ba24419a2078cd2b0f2ede2691b6c66d8e47836da3b6db8265ebad47afbfc7"
+checksum = "8211e4f58a2b2805adfbefbc07bab82958fc91e3836339b1ab7ae32465dce0d7"
 dependencies = [
  "either",
  "home",
- "once_cell",
  "rustix",
+ "winsafe",
 ]
 
 [[package]]
@@ -1858,3 +1912,9 @@ dependencies = [
  "cfg-if",
  "windows-sys 0.48.0",
 ]
+
+[[package]]
+name = "winsafe"
+version = "0.0.19"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d135d17ab770252ad95e9a872d365cf3090e3be864a34ab46f48555993efc904"
diff --git a/bin/ofs/Cargo.toml b/bin/ofs/Cargo.toml
index b02d20fefe..ee66757c9e 100644
--- a/bin/ofs/Cargo.toml
+++ b/bin/ofs/Cargo.toml
@@ -41,6 +41,7 @@ tokio = { version = "1.34", features = [
   "macros",
   "rt-multi-thread",
   "io-std",
+  "signal",
 ] }
 url = "2.5.0"
 chrono = "0.4.34"
@@ -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.0-alpha.2", "features" = ["tokio-runtime", "unprivileged"] }
 nix = { version = "0.27.1", features = ["user"] }
 
 [features]
diff --git a/bin/ofs/src/fuse.rs b/bin/ofs/src/fuse.rs
index f671cad58d..2b83a8fb0a 100644
--- a/bin/ofs/src/fuse.rs
+++ b/bin/ofs/src/fuse.rs
@@ -23,7 +23,6 @@ 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;
@@ -127,7 +126,6 @@ impl Fuse {
     }
 }
 
-#[async_trait]
 impl PathFilesystem for Fuse {
     type DirEntryStream = BoxStream<'static, Result<DirectoryEntry>>;
     type DirEntryPlusStream = BoxStream<'static, Result<DirectoryEntryPlus>>;
@@ -474,6 +472,7 @@ impl PathFilesystem for Fuse {
         fh: u64,
         offset: u64,
         data: &[u8],
+        _write_flags: u32,
         flags: u32,
     ) -> Result<ReplyWrite> {
         log::debug!(
diff --git a/bin/ofs/src/lib.rs b/bin/ofs/src/lib.rs
index 02417adbb4..6ce6203540 100644
--- a/bin/ofs/src/lib.rs
+++ b/bin/ofs/src/lib.rs
@@ -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;
@@ -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.unwrap(),
+        _ = signal::ctrl_c() => {
+            mount_handle.unmount().await.unwrap()
+        }
+    }
 
     Ok(())
 }