use std::ffi::OsStr;
use std::os::fd::{AsFd, BorrowedFd, OwnedFd};
use std::os::unix::ffi::OsStrExt;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use rustix::fs::{AtFlags, Dir, Mode, OFlags, ResolveFlags};
use rustix::io::Errno;
use super::MediaSkip;
use super::mountinfo::{self, Mount};
use crate::error::Result;
use crate::repo::Repo;
use crate::summary::{
OSTREE_METADATA_REF, SUMMARY_FILE, Summary, collection_map_has_ref, parse_ref_list,
read_root_file_with, summary_collection_id,
};
const REPOS_D: &str = ".ostree/repos.d";
const CANDIDATES: [&str; 3] = [".ostree/repo", "ostree/repo", "var/lib/flatpak/repo"];
const PSEUDO_TYPES: [&str; 18] = [
"proc",
"sysfs",
"devtmpfs",
"devpts",
"cgroup",
"cgroup2",
"securityfs",
"debugfs",
"tracefs",
"bpf",
"pstore",
"mqueue",
"hugetlbfs",
"configfs",
"fusectl",
"autofs",
"binfmt_misc",
"efivarfs",
];
const NETWORK_TYPES: [&str; 16] = [
"nfs",
"nfs4",
"cifs",
"smb3",
"smbfs",
"ceph",
"9p",
"virtiofs",
"davfs",
"afs",
"glusterfs",
"fuse.glusterfs",
"fuse.sshfs",
"fuse.rclone",
"fuse.s3fs",
"fuse.gvfsd-fuse",
];
type Identity = (u64, u64);
pub(super) struct FoundMedia {
pub(super) path: PathBuf,
pub(super) repo: Repo,
pub(super) summary: Vec<u8>,
}
pub(super) enum MediaChoice {
None(Vec<(PathBuf, MediaSkip)>),
One(FoundMedia),
Many(Vec<PathBuf>),
}
pub(super) async fn find_media(client: &Repo, table: Vec<u8>) -> Result<MediaChoice> {
let mut scan = Scan::new(client).await?;
let points = ostrya_rt::unblock(move || probe_points(&table)).await;
for point in points {
let _ = scan.probe(&point, true).await;
}
Ok(scan.choice())
}
pub(super) async fn find_media_in(client: &Repo, dir: &Path) -> Result<MediaChoice> {
let mut scan = Scan::new(client).await?;
scan.probe(dir, false).await?;
Ok(scan.choice())
}
fn probe_points(table: &[u8]) -> Vec<PathBuf> {
mountinfo::parse(table)
.iter()
.filter(|mount| !is_excluded(mount))
.map(Mount::point)
.collect()
}
fn is_excluded(mount: &Mount<'_>) -> bool {
PSEUDO_TYPES
.iter()
.chain(&NETWORK_TYPES)
.any(|ty| ty.as_bytes() == mount.fs_type)
|| mount.escaped_point == b"/"
}
struct Scan {
client: Identity,
skipped: Vec<(PathBuf, MediaSkip)>,
found: Vec<(Identity, PathBuf)>,
first: Option<(Repo, Vec<u8>)>,
}
struct MountDir {
fd: OwnedFd,
repos_d: std::result::Result<Vec<Vec<u8>>, MediaSkip>,
}
enum Probe {
Missing,
Skipped(MediaSkip),
Found,
Media(Identity, Repo, Vec<u8>),
}
impl Scan {
async fn new(client: &Repo) -> Result<Scan> {
let repo = client.clone();
let client = ostrya_rt::unblock(move || identity(repo.repo_fd())).await?;
Ok(Scan {
client,
skipped: Vec::new(),
found: Vec::new(),
first: None,
})
}
async fn probe(&mut self, dir: &Path, skip_writable: bool) -> Result<()> {
let path = dir.to_owned();
let Some(mount) = ostrya_rt::unblock(move || open_mount(&path, skip_writable)).await?
else {
return Ok(());
};
let names = mount.repos_d.unwrap_or_else(|skip| {
self.skipped.push((dir.join(REPOS_D), skip));
Vec::new()
});
let fd = Arc::new(mount.fd);
let candidates = names
.iter()
.map(|name| Path::new(REPOS_D).join(OsStr::from_bytes(name)))
.chain(CANDIDATES.iter().map(PathBuf::from));
for relative in candidates {
let path = dir.join(&relative);
let fd = Arc::clone(&fd);
let client = self.client;
let found: Vec<Identity> = self.found.iter().map(|(id, _)| *id).collect();
let repo_path = path.clone();
let probe = ostrya_rt::unblock(move || {
probe_candidate(fd.as_fd(), &relative, client, &found, repo_path)
.unwrap_or_else(Probe::Skipped)
})
.await;
match probe {
Probe::Missing => {}
Probe::Skipped(skip) => self.skipped.push((path, skip)),
Probe::Found => return Ok(()),
Probe::Media(id, repo, summary) => {
if self.first.is_none() {
self.first = Some((repo, summary));
}
self.found.push((id, path));
return Ok(());
}
}
}
Ok(())
}
fn choice(self) -> MediaChoice {
let mut paths: Vec<PathBuf> = self.found.into_iter().map(|(_, path)| path).collect();
match self.first {
None => MediaChoice::None(self.skipped),
Some((repo, summary)) if paths.len() == 1 => MediaChoice::One(FoundMedia {
path: paths.remove(0),
repo,
summary,
}),
Some(_) => MediaChoice::Many(paths),
}
}
}
fn open_mount(path: &Path, skip_writable: bool) -> Result<Option<MountDir>> {
let fd = rustix::fs::open(
path,
OFlags::PATH | OFlags::DIRECTORY | OFlags::CLOEXEC,
Mode::empty(),
)?;
if skip_writable && Mode::from_raw_mode(rustix::fs::fstat(&fd)?.st_mode).contains(Mode::WOTH) {
return Ok(None);
}
let repos_d = repos_d_names(fd.as_fd());
Ok(Some(MountDir { fd, repos_d }))
}
fn repos_d_names(dir: BorrowedFd<'_>) -> std::result::Result<Vec<Vec<u8>>, MediaSkip> {
let Some(fd) = open_in_mount(dir, Path::new(REPOS_D))? else {
return Ok(Vec::new());
};
let unreadable = |e: Errno| MediaSkip::Unreadable(e.into());
let mut names = Vec::new();
for entry in Dir::new(fd).map_err(unreadable)? {
let name = entry.map_err(unreadable)?.file_name().to_bytes().to_vec();
if name != b"." && name != b".." {
names.push(name);
}
}
names.sort();
Ok(names)
}
fn probe_candidate(
dir: BorrowedFd<'_>,
relative: &Path,
client: Identity,
found: &[Identity],
path: PathBuf,
) -> std::result::Result<Probe, MediaSkip> {
let Some(candidate) = open_in_mount(dir, relative)? else {
return Ok(Probe::Missing);
};
let id = identity(candidate.as_fd()).map_err(|e| MediaSkip::Unreadable(e.into()))?;
if id == client {
return Err(MediaSkip::Client);
}
if found.contains(&id) {
return Ok(Probe::Found);
}
let summary = read_root_file_with(candidate.as_fd(), SUMMARY_FILE, true)
.map_err(MediaSkip::Unreadable)?
.ok_or(MediaSkip::NoCollectionRef)?;
if !lists_collection_ref(&summary).map_err(MediaSkip::Unreadable)? {
return Err(MediaSkip::NoCollectionRef);
}
let repo = Repo::open_strict_blocking(candidate, path).map_err(MediaSkip::Unreadable)?;
Ok(Probe::Media(id, repo, summary))
}
fn open_in_mount(
dir: BorrowedFd<'_>,
relative: &Path,
) -> std::result::Result<Option<OwnedFd>, MediaSkip> {
match rustix::fs::openat2(
dir,
relative,
OFlags::RDONLY | OFlags::DIRECTORY | OFlags::CLOEXEC,
Mode::empty(),
ResolveFlags::NO_XDEV,
) {
Ok(fd) => Ok(Some(fd)),
Err(Errno::NOENT) => Ok(None),
Err(Errno::NOTDIR) if rustix::fs::statat(dir, relative, AtFlags::empty()).is_err() => {
Ok(None)
}
Err(Errno::XDEV) => Err(MediaSkip::OtherFileSystem),
Err(e) => Err(MediaSkip::Unreadable(e.into())),
}
}
pub(super) fn identity(fd: impl AsFd) -> std::result::Result<Identity, Errno> {
let stat = rustix::fs::fstat(fd)?;
Ok((stat.st_dev, stat.st_ino))
}
fn lists_collection_ref(bytes: &[u8]) -> Result<bool> {
let metadata = Summary::parse_metadata(bytes)?;
if collection_map_has_ref(&metadata)? {
return Ok(true);
}
let own = summary_collection_id(&metadata).is_some();
Ok(own
&& parse_ref_list(bytes)?
.iter()
.any(|entry| entry.name != OSTREE_METADATA_REF))
}
#[cfg(test)]
mod tests {
use std::os::unix::fs::PermissionsExt;
use std::process::{Command, Stdio};
use ostrya_core::{Type, Value, to_bytes};
use super::*;
use crate::summary::COLLECTION_ID_KEY;
use crate::{CreateOptions, Error, RepoMode};
const ID: &str = "org.example.Os";
const NAMESPACE_CHILD: &str = "OSTRYA_MEDIA_FIND_CHILD";
struct Scratch {
root: PathBuf,
}
impl Scratch {
fn new(dir: &Path) -> Scratch {
let root = dir.join(format!(
"ostrya-media-find-{}-{}",
std::process::id(),
crate::write::unique()
));
let _ = std::fs::remove_dir_all(&root);
std::fs::create_dir_all(&root).unwrap();
Scratch { root }
}
fn dir(&self, name: &str) -> PathBuf {
let path = self.root.join(name);
std::fs::create_dir_all(&path).unwrap();
path
}
}
impl Drop for Scratch {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.root);
}
}
fn summary(own: Option<&str>, refs: &[&str], map: &[(&str, &[&str])]) -> Vec<u8> {
let entries = |names: &[&str]| {
let entry = |name: &&str| {
Value::Tuple(vec![
Value::Str((*name).to_owned()),
Value::Tuple(vec![
Value::U64(0),
Value::Bytes(vec![1; 32]),
Value::Array(Vec::new()),
]),
])
};
Value::Array(names.iter().map(entry).collect())
};
let item = |key: &str, ty: &str, value: Value| {
Value::Tuple(vec![
Value::Str(key.to_owned()),
Value::Variant(Box::new((Type::parse(ty).unwrap(), value))),
])
};
let mut metadata = Vec::new();
if let Some(id) = own {
metadata.push(item(COLLECTION_ID_KEY, "s", Value::Str(id.to_owned())));
}
if !map.is_empty() {
let map = map
.iter()
.map(|(id, names)| Value::Tuple(vec![Value::Str((*id).to_owned()), entries(names)]))
.collect();
metadata.push(item(
"ostree.summary.collection-map",
"a{sa(s(taya{sv}))}",
Value::Array(map),
));
}
let value = Value::Tuple(vec![entries(refs), Value::Array(metadata)]);
to_bytes(&Type::parse("(a(s(taya{sv}))a{sv})").unwrap(), &value).unwrap()
}
fn media_summary() -> Vec<u8> {
summary(None, &[], &[(ID, &["os/stable"])])
}
async fn repo_at(path: &Path, summary: Option<&[u8]>) -> Repo {
std::fs::create_dir_all(path).unwrap();
let repo = Repo::create(path, CreateOptions::new(RepoMode::Archive))
.await
.unwrap();
if let Some(bytes) = summary {
std::fs::write(path.join("summary"), bytes).unwrap();
}
repo
}
fn table(mounts: &[(&Path, &str)]) -> Vec<u8> {
let mut out = String::new();
for (i, (point, ty)) in mounts.iter().enumerate() {
out.push_str(&format!(
"{} 1 0:{i} / {} rw,relatime shared:{i} - {ty} src rw\n",
i + 100,
point.display()
));
}
out.into_bytes()
}
fn one(choice: MediaChoice) -> FoundMedia {
match choice {
MediaChoice::One(found) => found,
MediaChoice::None(skipped) => panic!("no media: {skipped:?}"),
MediaChoice::Many(paths) => panic!("more than one media: {paths:?}"),
}
}
fn skipped(choice: MediaChoice) -> Vec<(PathBuf, String)> {
match choice {
MediaChoice::None(skipped) => skipped
.into_iter()
.map(|(path, skip)| {
let reason = match skip {
MediaSkip::Unreadable(Error::Io(_)) => "unreadable".to_owned(),
other => format!("{other:?}"),
};
(path, reason)
})
.collect(),
MediaChoice::One(found) => panic!("media at {}", found.path.display()),
MediaChoice::Many(paths) => panic!("more than one media: {paths:?}"),
}
}
fn other_file_system(scratch: &Scratch) -> Option<Scratch> {
let shm = Path::new("/dev/shm");
let dev =
|path: &Path| std::fs::metadata(path).map(|m| std::os::unix::fs::MetadataExt::dev(&m));
match (dev(shm), dev(&scratch.root)) {
(Ok(a), Ok(b)) if a != b => Some(Scratch::new(shm)),
_ => {
eprintln!(
"skipping the other file system: /dev/shm is missing or on the scratch file system"
);
None
}
}
}
#[test]
fn the_finder_takes_one_media_and_reports_two() {
ostrya_rt::block_on(async {
let scratch = Scratch::new(&std::env::temp_dir());
let client = repo_at(&scratch.root.join("client"), None).await;
let empty = scratch.dir("empty");
let a = scratch.dir("a");
let b = scratch.dir("b");
repo_at(&a.join(".ostree/repo"), Some(&media_summary())).await;
repo_at(&b.join("ostree/repo"), Some(&media_summary())).await;
let choice = find_media(&client, table(&[(&empty, "ext4")])).await;
assert!(skipped(choice.unwrap()).is_empty());
std::fs::set_permissions(&a, std::fs::Permissions::from_mode(0o311)).unwrap();
let choice = find_media(&client, table(&[(&empty, "ext4"), (&a, "vfat")])).await;
let _ = std::fs::set_permissions(&a, std::fs::Permissions::from_mode(0o755));
let found = one(choice.unwrap());
assert_eq!(found.path, a.join(".ostree/repo"));
assert_eq!(found.summary, media_summary());
assert!(found.repo.is_strict());
assert_eq!(found.repo.path(), a.join(".ostree/repo"));
let choice = find_media(&client, table(&[(&b, "exfat"), (&a, "vfat")])).await;
let MediaChoice::Many(paths) = choice.unwrap() else {
panic!("two mounts give two media");
};
assert_eq!(paths, [b.join("ostree/repo"), a.join(".ostree/repo")]);
});
}
#[test]
fn the_first_candidate_with_a_collection_ref_is_the_media() {
ostrya_rt::block_on(async {
let scratch = Scratch::new(&std::env::temp_dir());
let client = repo_at(&scratch.root.join("client"), None).await;
let mount = scratch.dir("mount");
let repos_d = mount.join(".ostree/repos.d");
let local = summary(None, &["os/stable"], &[]);
let own = summary(Some(ID), &["os/stable"], &[]);
repo_at(&repos_d.join("b"), Some(&own)).await;
repo_at(&repos_d.join("a"), Some(&media_summary())).await;
repo_at(&repos_d.join("d"), None).await;
repo_at(&repos_d.join("c"), Some(&local)).await;
repo_at(&mount.join(".ostree/repo"), Some(&media_summary())).await;
let choice = find_media(&client, table(&[(&mount, "ext4")])).await;
assert_eq!(one(choice.unwrap()).path, repos_d.join("a"));
std::fs::remove_dir_all(repos_d.join("a")).unwrap();
let choice = find_media(&client, table(&[(&mount, "ext4")])).await;
assert_eq!(one(choice.unwrap()).path, repos_d.join("b"));
std::fs::remove_dir_all(repos_d.join("b")).unwrap();
std::fs::remove_dir_all(mount.join(".ostree/repo")).unwrap();
let choice = find_media(&client, table(&[(&mount, "ext4")])).await;
assert_eq!(
skipped(choice.unwrap()),
[
(repos_d.join("c"), "NoCollectionRef".to_owned()),
(repos_d.join("d"), "NoCollectionRef".to_owned()),
]
);
});
}
#[test]
fn a_repository_with_only_its_anchor_ref_is_no_media() {
ostrya_rt::block_on(async {
let scratch = Scratch::new(&std::env::temp_dir());
let client = repo_at(&scratch.root.join("client"), None).await;
let mount = scratch.dir("mount");
let path = mount.join("ostree/repo");
let anchor = summary(Some(ID), &["ostree-metadata"], &[]);
repo_at(&path, Some(&anchor)).await;
let choice = find_media_in(&client, &mount).await;
assert_eq!(
skipped(choice.unwrap()),
[(path.clone(), "NoCollectionRef".to_owned())]
);
let both = summary(Some(ID), &["ostree-metadata", "os/stable"], &[]);
std::fs::write(path.join("summary"), both).unwrap();
let choice = find_media_in(&client, &mount).await;
assert_eq!(one(choice.unwrap()).path, path);
let map = summary(None, &[], &[(ID, &["ostree-metadata"])]);
std::fs::write(path.join("summary"), map).unwrap();
let choice = find_media_in(&client, &mount).await;
assert_eq!(one(choice.unwrap()).path, path);
let own_map = summary(
Some(ID),
&["ostree-metadata"],
&[(ID, &["ostree-metadata"])],
);
std::fs::write(path.join("summary"), own_map).unwrap();
let choice = find_media_in(&client, &mount).await;
assert_eq!(one(choice.unwrap()).path, path);
});
}
#[test]
fn the_finder_skips_the_client_and_candidates_it_cannot_take() {
ostrya_rt::block_on(async {
let scratch = Scratch::new(&std::env::temp_dir());
let mount = scratch.dir("mount");
let client = repo_at(&mount.join(".ostree/repo"), Some(&media_summary())).await;
let mut expected = vec![(mount.join(".ostree/repo"), "Client".to_owned())];
let unreadable = scratch.dir("mount/ostree/repo");
std::fs::set_permissions(&unreadable, std::fs::Permissions::from_mode(0o000)).unwrap();
if rustix::process::geteuid().is_root() {
eprintln!("skipping the unreadable candidate: root reads each directory");
std::fs::remove_dir(&unreadable).unwrap();
} else {
expected.push((unreadable.clone(), "unreadable".to_owned()));
}
let other = other_file_system(&scratch);
if let Some(other) = &other {
repo_at(&other.root.join("repo"), Some(&media_summary())).await;
std::fs::create_dir_all(mount.join("var/lib/flatpak")).unwrap();
std::os::unix::fs::symlink(
other.root.join("repo"),
mount.join("var/lib/flatpak/repo"),
)
.unwrap();
expected.push((
mount.join("var/lib/flatpak/repo"),
"OtherFileSystem".to_owned(),
));
}
let found = skipped(
find_media(&client, table(&[(&mount, "ext4")]))
.await
.unwrap(),
);
let given = skipped(find_media_in(&client, &mount).await.unwrap());
let _ = std::fs::set_permissions(&unreadable, std::fs::Permissions::from_mode(0o755));
assert_eq!(found, expected);
assert_eq!(given, expected);
assert!(matches!(
find_media_in(&client, &scratch.root.join("absent")).await,
Err(Error::Io(_))
));
});
}
#[test]
fn a_path_through_a_file_is_no_candidate() {
ostrya_rt::block_on(async {
let scratch = Scratch::new(&std::env::temp_dir());
let client = repo_at(&scratch.root.join("client"), None).await;
let mount = scratch.dir("mount");
std::fs::write(mount.join(".ostree"), b"").unwrap();
std::fs::write(mount.join("ostree"), b"").unwrap();
std::fs::create_dir_all(mount.join("var/lib/flatpak")).unwrap();
std::fs::write(mount.join("var/lib/flatpak/repo"), b"").unwrap();
let choice = find_media(&client, table(&[(&mount, "ext4")])).await;
assert_eq!(
skipped(choice.unwrap()),
[(mount.join("var/lib/flatpak/repo"), "unreadable".to_owned())]
);
});
}
#[test]
fn one_repository_at_two_mount_points_is_one_media() {
ostrya_rt::block_on(async {
let scratch = Scratch::new(&std::env::temp_dir());
let client = repo_at(&scratch.root.join("client"), None).await;
let a = scratch.dir("a");
repo_at(&a.join(".ostree/repo"), Some(&media_summary())).await;
let b = scratch.root.join("b");
std::os::unix::fs::symlink("a", &b).unwrap();
let choice = find_media(&client, table(&[(&b, "ext4"), (&a, "ext4")])).await;
assert_eq!(one(choice.unwrap()).path, b.join(".ostree/repo"));
});
}
#[test]
fn the_finder_probes_no_excluded_mount() {
let points: Vec<PathBuf> = PSEUDO_TYPES
.iter()
.chain(&NETWORK_TYPES)
.map(|ty| PathBuf::from(format!("/mnt/{ty}")))
.collect();
let mut mounts = vec![(Path::new("/"), "ext4")];
for (point, ty) in points.iter().zip(PSEUDO_TYPES.iter().chain(&NETWORK_TYPES)) {
mounts.push((point, ty));
}
mounts.push((Path::new("/mnt/media"), "ext4"));
assert_eq!(probe_points(&table(&mounts)), [Path::new("/mnt/media")]);
}
#[test]
fn the_scan_skips_a_mount_that_any_user_can_write() {
ostrya_rt::block_on(async {
let scratch = Scratch::new(&std::env::temp_dir());
let client = repo_at(&scratch.root.join("client"), None).await;
for mode in [0o1777, 0o777] {
let mount = scratch.dir(&format!("{mode:o}"));
repo_at(&mount.join(".ostree/repo"), Some(&media_summary())).await;
std::fs::set_permissions(&mount, std::fs::Permissions::from_mode(mode)).unwrap();
let choice = find_media(&client, table(&[(&mount, "tmpfs")])).await;
assert!(skipped(choice.unwrap()).is_empty(), "mode {mode:o}");
let choice = find_media_in(&client, &mount).await;
assert_eq!(one(choice.unwrap()).path, mount.join(".ostree/repo"));
}
});
}
#[test]
fn the_last_mount_at_a_mount_point_counts() {
ostrya_rt::block_on(async {
let scratch = Scratch::new(&std::env::temp_dir());
let client = repo_at(&scratch.root.join("client"), None).await;
let mount = scratch.dir("mount");
repo_at(&mount.join(".ostree/repo"), Some(&media_summary())).await;
let choice = find_media(&client, table(&[(&mount, "ext4"), (&mount, "nfs")])).await;
assert!(skipped(choice.unwrap()).is_empty());
let choice = find_media(&client, table(&[(&mount, "nfs"), (&mount, "ext4")])).await;
assert_eq!(one(choice.unwrap()).path, mount.join(".ostree/repo"));
});
}
#[test]
fn a_given_directory_skips_a_candidate_on_another_file_system() {
ostrya_rt::block_on(async {
let scratch = Scratch::new(&std::env::temp_dir());
let Some(other) = other_file_system(&scratch) else {
return;
};
let client = repo_at(&scratch.root.join("client"), None).await;
let dir = scratch.dir("dir/.ostree");
repo_at(&other.root.join("repo"), Some(&media_summary())).await;
std::os::unix::fs::symlink(other.root.join("repo"), dir.join("repo")).unwrap();
let dir = scratch.root.join("dir");
repo_at(&dir.join("ostree/repo"), Some(&media_summary())).await;
let choice = find_media_in(&client, &dir).await;
assert_eq!(one(choice.unwrap()).path, dir.join("ostree/repo"));
});
}
fn btrfs_subvolume(path: &Path) -> bool {
Command::new("btrfs")
.args(["subvolume", "create"])
.arg(path)
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.is_ok_and(|s| s.success())
}
#[test]
fn a_repository_in_a_btrfs_subvolume_is_media() {
ostrya_rt::block_on(async {
let scratch = Scratch::new(&std::env::temp_dir());
let client = repo_at(&scratch.root.join("client"), None).await;
let cases = [("a", ".ostree"), ("b", ".ostree/repo")].map(|(name, subvolume)| {
let mount = scratch.dir(name);
let subvolume = mount.join(subvolume);
std::fs::create_dir_all(subvolume.parent().unwrap()).unwrap();
(btrfs_subvolume(&subvolume), mount, subvolume)
});
if !cases.iter().all(|(created, ..)| *created) {
eprintln!("skipping: the host cannot create a btrfs subvolume");
return;
}
let dev = |path: &Path| {
std::os::unix::fs::MetadataExt::dev(&std::fs::metadata(path).unwrap())
};
for (_, mount, subvolume) in cases {
assert_ne!(dev(&subvolume), dev(&mount));
repo_at(&mount.join(".ostree/repo"), Some(&media_summary())).await;
let choice = find_media(&client, table(&[(&mount, "btrfs")])).await;
assert_eq!(one(choice.unwrap()).path, mount.join(".ostree/repo"));
let choice = find_media_in(&client, &mount).await;
assert_eq!(one(choice.unwrap()).path, mount.join(".ostree/repo"));
}
});
}
#[test]
fn the_finder_finds_media_on_a_tmpfs_in_a_user_namespace() {
let scratch = Scratch::new(&std::env::temp_dir());
let unshare = |args: &[&str]| {
Command::new("unshare")
.args(args)
.arg("true")
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
};
if unshare(&[]).is_err() {
eprintln!("skipping: unshare is not available");
return;
}
let root = rustix::process::geteuid().is_root();
if !root && !unshare(&["--map-root-user"]).is_ok_and(|s| s.success()) {
eprintln!("skipping: the host grants no user namespace");
return;
}
let namespace: &[&str] = if root {
&["-m", "--propagation", "private"]
} else {
&["--map-root-user", "-m", "--propagation", "private"]
};
if !unshare(namespace).is_ok_and(|s| s.success()) {
eprintln!("skipping: the host grants no mount namespace");
return;
}
let out = Command::new("unshare")
.args(namespace)
.arg(std::env::current_exe().unwrap())
.args([
"media::find::tests::tmpfs_media_subprocess",
"--exact",
"--ignored",
"--nocapture",
])
.env(NAMESPACE_CHILD, &scratch.root)
.stdin(Stdio::null())
.output()
.unwrap();
let stdout = String::from_utf8_lossy(&out.stdout);
if let Some(line) = stdout
.lines()
.find(|l| l.starts_with("tmpfs mount refused"))
{
eprintln!("skipping: {line}");
return;
}
assert!(
out.status.success() && stdout.contains("1 passed"),
"the child reported {}:\n{stdout}{}",
out.status,
String::from_utf8_lossy(&out.stderr),
);
}
#[test]
#[ignore = "helper process for the_finder_finds_media_on_a_tmpfs_in_a_user_namespace"]
fn tmpfs_media_subprocess() {
let Some(dir) = std::env::var_os(NAMESPACE_CHILD).map(PathBuf::from) else {
return;
};
let dir = std::fs::canonicalize(dir).unwrap();
let mount = dir.join("mnt");
std::fs::create_dir(&mount).unwrap();
match Command::new("mount")
.args(["-t", "tmpfs", "-o", "mode=0755", "tmpfs"])
.arg(&mount)
.output()
{
Ok(out) if out.status.success() => {}
Ok(out) => {
let stderr = String::from_utf8_lossy(&out.stderr);
println!("tmpfs mount refused: {}", stderr.trim());
return;
}
Err(e) => {
println!("tmpfs mount refused: mount: {e}");
return;
}
}
let nested = mount.join("ostree");
std::fs::create_dir(&nested).unwrap();
let out = Command::new("mount")
.args(["-t", "tmpfs", "tmpfs"])
.arg(&nested)
.output()
.unwrap();
assert!(out.status.success(), "{out:?}");
ostrya_rt::block_on(async {
let client = repo_at(&dir.join("client"), None).await;
repo_at(&mount.join(".ostree/repo"), Some(&media_summary())).await;
repo_at(&nested.join("repo"), Some(&media_summary())).await;
let table = mountinfo::read_table().await.unwrap();
let paths = match find_media(&client, table).await.unwrap() {
MediaChoice::One(found) => vec![found.path],
MediaChoice::Many(paths) => paths,
MediaChoice::None(skipped) => panic!("no media: {skipped:?}"),
};
assert!(paths.contains(&mount.join(".ostree/repo")), "{paths:?}");
std::fs::remove_dir_all(mount.join(".ostree")).unwrap();
let choice = find_media_in(&client, &mount).await.unwrap();
assert_eq!(
skipped(choice),
[(nested.join("repo"), "OtherFileSystem".to_owned())]
);
});
}
}