Skip to content

Commit

Permalink
Ignore ENOENT errors when checking if paths are mounted
Browse files Browse the repository at this point in the history
It's safe to say that paths that do not exist are not mounted for all of
the purposes in the spfs setup logic

Signed-off-by: Ryan Bottriell <ryan@bottriell.ca>
  • Loading branch information
rydrman committed May 16, 2024
1 parent 018f18a commit 179c9ef
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions crates/spfs/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ where
}

/// Check if the identified directory is an active mount point.
///
/// Returns false in the case where the path or its parent do not exist.
async fn is_mounted<P: Into<PathBuf>>(&self, target: P) -> Result<bool> {
let target = target.into();
let parent = match target.parent() {
Expand All @@ -292,11 +294,9 @@ where

// A new thread created while holding _guard will be inside the same
// mount namespace...
let stat_parent_thread =
std::thread::spawn(move || nix::sys::stat::stat(&parent).map_err(Error::from));
let stat_parent_thread = std::thread::spawn(move || nix::sys::stat::stat(&parent));

let stat_target_thread =
std::thread::spawn(move || nix::sys::stat::stat(&target).map_err(Error::from));
let stat_target_thread = std::thread::spawn(move || nix::sys::stat::stat(&target));

let (st_parent, st_target) = tokio::task::spawn_blocking(move || {
let st_parent = stat_parent_thread.join();
Expand All @@ -306,9 +306,22 @@ where
.await?;

let st_parent =
st_parent.map_err(|_| Error::String("Failed to stat parent".to_owned()))??;
match st_parent.map_err(|_| Error::String("Failed to stat parent".to_owned()))? {
// the parent not existing means the child also doesn't exist
// and so cannot be considered as mounted
Err(nix::errno::Errno::ENOENT) => {
return Ok(false);
}
r => r?,
};
let st_target =
st_target.map_err(|_| Error::String("Failed to stat target".to_owned()))??;
match st_target.map_err(|_| Error::String("Failed to stat target".to_owned()))? {
// a non-existent directory is considered not mounted
Err(nix::errno::Errno::ENOENT) => {
return Ok(false);
}
r => r?,
};

Ok(st_target.st_dev != st_parent.st_dev)
}
Expand Down Expand Up @@ -346,8 +359,8 @@ where
));
}

if self.is_mounted("/spfs").await? {
res = mount(NONE, "/spfs", NONE, MsFlags::MS_PRIVATE, NONE);
if self.is_mounted(SPFS_DIR).await? {
res = mount(NONE, SPFS_DIR, NONE, MsFlags::MS_PRIVATE, NONE);
if let Err(err) = res {
return Err(Error::wrap_nix(
err,
Expand Down

0 comments on commit 179c9ef

Please sign in to comment.