Skip to content

Commit

Permalink
fix: freebsd ci fail
Browse files Browse the repository at this point in the history
  • Loading branch information
Sherlock-Holo committed Feb 20, 2024
1 parent bb67259 commit 1c3d57d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 34 deletions.
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@

#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]

use std::io;
use std::io::ErrorKind;
#[cfg(target_os = "linux")]
use std::io::{self, ErrorKind};
#[cfg(target_os = "linux")]
use std::path::PathBuf;
use std::time::{Duration, SystemTime, UNIX_EPOCH};

Expand Down Expand Up @@ -196,6 +197,7 @@ impl From<SystemTime> for Timestamp {
}
}

#[cfg(target_os = "linux")]
fn find_fusermount3() -> io::Result<PathBuf> {
which::which("fusermount3").map_err(|err| {
io::Error::new(
Expand Down
2 changes: 2 additions & 0 deletions src/raw/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ mod tokio_connection {
use tracing::debug;
use tracing::warn;

#[cfg(target_os = "linux")]
use crate::find_fusermount3;
#[cfg(all(target_os = "linux", feature = "unprivileged"))]
use crate::MountOptions;
Expand Down Expand Up @@ -228,6 +229,7 @@ mod async_std_connection {
#[cfg(all(target_os = "linux", feature = "unprivileged"))]
use tracing::debug;

#[cfg(target_os = "linux")]
use crate::find_fusermount3;
#[cfg(all(target_os = "linux", feature = "unprivileged"))]
use crate::MountOptions;
Expand Down
89 changes: 57 additions & 32 deletions src/raw/session.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::ffi::{OsStr, OsString};
#[cfg(target_os = "linux")]
use std::ffi::OsStr;
use std::ffi::OsString;
use std::future::Future;
use std::io::Error as IoError;
use std::io::ErrorKind;
Expand All @@ -12,7 +14,11 @@ use std::sync::Arc;
use std::task::Context;
use std::task::Poll;

#[cfg(all(not(feature = "tokio-runtime"), feature = "async-std-runtime"))]
#[cfg(all(
target_os = "linux",
not(feature = "tokio-runtime"),
feature = "async-std-runtime"
))]
use async_std::process::Command;
#[cfg(all(not(feature = "tokio-runtime"), feature = "async-std-runtime"))]
use async_std::{fs::read_dir, task};
Expand All @@ -22,14 +28,21 @@ use futures_util::future::FutureExt;
use futures_util::sink::{Sink, SinkExt};
use futures_util::stream::StreamExt;
use futures_util::{pin_mut, select};
#[cfg(target_os = "linux")]
use nix::mount;
#[cfg(all(not(feature = "async-std-runtime"), feature = "tokio-runtime"))]
#[cfg(target_os = "freebsd")]
use nix::mount::MntFlags;
#[cfg(all(
target_os = "linux",
not(feature = "async-std-runtime"),
feature = "tokio-runtime"
))]
use tokio::process::Command;
#[cfg(all(not(feature = "async-std-runtime"), feature = "tokio-runtime"))]
use tokio::{fs::read_dir, task};
use tracing::{debug, debug_span, error, instrument, warn, Instrument, Span};

#[cfg(target_os = "linux")]
use crate::find_fusermount3;
use crate::helper::*;
use crate::notify::Notify;
use crate::raw::abi::*;
Expand All @@ -38,7 +51,7 @@ use crate::raw::connection::FuseConnection;
use crate::raw::filesystem::Filesystem;
use crate::raw::reply::ReplyXAttr;
use crate::raw::request::Request;
use crate::{find_fusermount3, MountOptions};
use crate::MountOptions;
use crate::{Errno, SetAttr};

/// A Future which returns when a file system is unmounted
Expand Down Expand Up @@ -73,29 +86,35 @@ impl Drop for MountHandle {
struct MountHandleInner {
task: task::JoinHandle<IoResult<()>>,
mount_path: PathBuf,
#[cfg(target_os = "linux")]
unprivileged: bool,
}

impl MountHandleInner {
#[cfg(target_os = "linux")]
fn inner_unmount(self) -> impl Future<Output = IoResult<()>> + 'static + Send {
async fn inner_unmount(self) -> IoResult<()> {
#[cfg(all(not(feature = "tokio-runtime"), feature = "async-std-runtime"))]
{
let task = self.task;
let unprivileged = self.unprivileged;
let mount_path = self.mount_path.clone();
if let Some(res) = self.task.cancel().await {
res?;
}

async move {
if let Some(res) = task.cancel().await {
res?;
}
// TODO: freebsd mount is unprivileged, then unmount is unprivileged too?
#[cfg(target_os = "freebsd")]
{
task::spawn_blocking(move || {
mount::unmount(&self.mount_path, MntFlags::MNT_SYNCHRONOUS)
})
.await?;
}

if !unprivileged {
task::spawn_blocking(move || mount::umount(&mount_path)).await?;
#[cfg(target_os = "linux")]
{
if !self.unprivileged {
task::spawn_blocking(move || mount::umount(&self.mount_path)).await?;
} else {
let binary_path = find_fusermount3()?;
let mut child = Command::new(binary_path)
.args([OsStr::new("-u"), mount_path.as_os_str()])
.args([OsStr::new("-u"), self.mount_path.as_os_str()])
.spawn()?;
if !child.status().await?.success() {
return Err(IoError::new(
Expand All @@ -104,28 +123,33 @@ impl MountHandleInner {
));
}
}

Ok(())
}
}

#[cfg(all(not(feature = "async-std-runtime"), feature = "tokio-runtime"))]
{
let task = self.task;
let unprivileged = self.unprivileged;
let mount_path = self.mount_path;
self.task.abort();

async move {
task.abort();
// TODO: freebsd mount is unprivileged, then unmount is unprivileged too?
#[cfg(target_os = "freebsd")]
{
task::spawn_blocking(move || {
mount::unmount(&self.mount_path, MntFlags::MNT_SYNCHRONOUS)
})
.await
.unwrap()?;
}

if !unprivileged {
task::spawn_blocking(move || mount::umount(&mount_path))
#[cfg(target_os = "linux")]
{
if !self.unprivileged {
task::spawn_blocking(move || mount::umount(&self.mount_path))
.await
.unwrap()?;
} else {
let binary_path = find_fusermount3()?;
let mut child = Command::new(binary_path)
.args([OsStr::new("-u"), mount_path.as_os_str()])
.args([OsStr::new("-u"), self.mount_path.as_os_str()])
.spawn()?;
if !child.wait().await?.success() {
return Err(IoError::new(
Expand All @@ -134,10 +158,10 @@ impl MountHandleInner {
));
}
}

Ok(())
}
}

Ok(())
}
}

Expand Down Expand Up @@ -344,9 +368,10 @@ impl<FS: Filesystem + Send + Sync + 'static> Session<FS> {
debug!("mount {:?} success", mount_path);

Ok(MountHandle {
task: task::spawn(self.inner_mount()),
mount_path: mount_path.to_path_buf(),
unprivileged: true,
inner: Some(MountHandleInner {
task: task::spawn(self.inner_mount()),
mount_path: mount_path.to_path_buf(),
}),
})
}

Expand Down

0 comments on commit 1c3d57d

Please sign in to comment.