Skip to content

Commit

Permalink
fix: make mounting deterministic
Browse files Browse the repository at this point in the history
  • Loading branch information
lleyton committed Sep 2, 2024
1 parent 19865dd commit 8495ba3
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{
collections::HashMap,
fs::File,
os::fd::AsRawFd,
path::{Path, PathBuf},
path::{Component, Path, PathBuf},
};
use sys_mount::{FilesystemType, Mount, MountFlags, Unmount, UnmountDrop, UnmountFlags};
/// Mount object struct
Expand Down Expand Up @@ -115,10 +115,14 @@ impl MountTable {
/// Closer to root, and root is first
/// everything else is either sorted by depth, or alphabetically
fn sort_mounts(&self) -> impl Iterator<Item = (&PathBuf, &MountTarget)> {
self.inner.iter().sorted_unstable_by(|(_, a), (_, b)| {
self.inner.iter().sorted_by(|(_, a), (_, b)| {
match (a.target.components().count(), b.target.components().count()) {
(1, _) => std::cmp::Ordering::Less, // root dir
(_, 1) => std::cmp::Ordering::Greater, // root dir
(1, _) if a.target.components().next() == Some(Component::RootDir) => {
std::cmp::Ordering::Less
} // root dir
(_, 1) if b.target.components().next() == Some(Component::RootDir) => {
std::cmp::Ordering::Greater
} // root dir
(x, y) if x == y => a.target.cmp(&b.target),
(x, y) => x.cmp(&y),
}
Expand All @@ -137,7 +141,6 @@ impl MountTable {
.sort_mounts()
.map(|(source, mount)| {
tracing::trace!(?mount, ?source, "Mounting");
std::fs::create_dir_all(root.join(source))?;
mount.mount(source, root)
})
.collect::<std::io::Result<_>>()?;
Expand Down

0 comments on commit 8495ba3

Please sign in to comment.