use std::path::{Path, PathBuf};
use crate::logging::{info, warn};
pub const LEGACY_ENV_DIR: &str = "chunks.mdb";
const RETIRED_SUFFIX: &str = ".retired";
const RETIRED_MARKER: &str = "RETIRED";
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Leftover {
Holding,
Harmless,
Unreadable,
}
fn legacy_directories(root_dir: &Path) -> Result<Vec<PathBuf>, Unreadable> {
let mut found = Vec::new();
let live = root_dir.join(LEGACY_ENV_DIR);
if !matches!(std::fs::symlink_metadata(&live), Err(ref e) if e.kind() == std::io::ErrorKind::NotFound)
{
found.push(live);
}
let entries = match std::fs::read_dir(root_dir) {
Ok(entries) => entries,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(found),
Err(_) => return Err(Unreadable),
};
for entry in entries {
let Ok(entry) = entry else {
return Err(Unreadable);
};
if entry.file_name().to_str().is_some_and(is_tombstone_name) {
found.push(entry.path());
}
}
Ok(found)
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct Unreadable;
const MAX_TOMBSTONES: u32 = 64;
fn is_tombstone_name(name: &str) -> bool {
let base = format!("{LEGACY_ENV_DIR}{RETIRED_SUFFIX}");
if name == base {
return true;
}
let Some(suffix) = name.strip_prefix(&format!("{base}.")) else {
return false;
};
suffix
.parse::<u32>()
.is_ok_and(|n| (1..=MAX_TOMBSTONES).contains(&n) && suffix == n.to_string())
}
fn classify(dir: &Path) -> Leftover {
match std::fs::symlink_metadata(dir) {
Ok(meta) if meta.file_type().is_symlink() => return Leftover::Holding,
Ok(_) => {}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Leftover::Harmless,
Err(_) => return Leftover::Unreadable,
}
match std::fs::symlink_metadata(dir.join(RETIRED_MARKER)) {
Ok(meta) if meta.is_file() => return Leftover::Harmless,
Ok(_) => {}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
Err(_) => return Leftover::Unreadable,
}
std::fs::read_dir(dir).map_or(Leftover::Unreadable, |mut entries| {
if entries.next().is_none() {
Leftover::Harmless
} else {
Leftover::Holding
}
})
}
pub fn clean_up(root_dir: &Path) {
let Ok(leftovers) = legacy_directories(root_dir) else {
warn!(
migration_event = "legacy_store_left",
"{} could not be listed, so nothing left over from the storage migration was \
removed from it. Any leftover is still costing disk.",
root_dir.display()
);
return;
};
let mut finished_with = Vec::new();
for dir in leftovers {
match classify(&dir) {
Leftover::Harmless => finished_with.push(dir),
Leftover::Holding | Leftover::Unreadable => warn!(
migration_event = "legacy_store_left",
"{} is left over from the storage migration and this build will not remove \
it: {}. It is costing disk until it is dealt with by hand.",
dir.display(),
why_it_is_kept(&dir)
),
}
}
if !finished_with.is_empty() {
remove_all(finished_with);
}
}
#[cfg_attr(not(feature = "logging"), allow(dead_code))]
fn why_it_is_kept(dir: &Path) -> &'static str {
let Ok(meta) = std::fs::symlink_metadata(dir) else {
return "it cannot be examined";
};
if meta.file_type().is_symlink() {
return "it is a link to storage this node does not own";
}
if !meta.is_dir() {
return "it is not a directory";
}
match std::fs::symlink_metadata(dir.join(RETIRED_MARKER)) {
Ok(meta) if meta.is_file() => "it carries the mark that says it was finished with",
Ok(_) => {
"something that is not the storage migration's mark is using the name the mark \
would have, so this node cannot tell whether it was finished with"
}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
"it has chunks in it that may not have been copied into the file store, and this \
build cannot read them. Nothing here will delete them, so they are not lost. Whether \
this node's close group still needs them is the question to answer before \
removing it by hand"
}
Err(_) => "whether it was finished with cannot be established",
}
}
fn remove_all(dirs: Vec<PathBuf>) {
let spawned = std::thread::Builder::new()
.name("legacy-store-cleanup".into())
.spawn(move || {
for dir in dirs {
match delete_mark_last(&dir) {
Ok(()) => info!(
migration_event = "space_returned",
"Removed {}, which the storage migration had finished with, and \
returned its space.",
dir.display()
),
Err(e) => warn!(
migration_event = "legacy_store_left",
"{} was finished with by the storage migration but could not be \
removed ({e}). It is costing disk. The next start tries again.",
dir.display()
),
}
}
});
if let Err(e) = spawned {
warn!(
migration_event = "legacy_store_left",
"Could not start the storage migration's cleanup thread ({e}), so nothing it had \
finished with was removed. Those directories are still costing disk and the next \
start tries again."
);
}
}
const MAX_ENTRIES: usize = 1024;
fn delete_mark_last(dir: &Path) -> std::io::Result<()> {
empty_and_unmark(dir)?;
std::fs::remove_dir(dir)
}
#[cfg(unix)]
fn empty_and_unmark(dir: &Path) -> std::io::Result<()> {
use rustix::fs::{AtFlags, FileType};
let dirfd = open_dir_nofollow(dir)?;
let marked = matches!(
rustix::fs::statat(&dirfd, RETIRED_MARKER, AtFlags::SYMLINK_NOFOLLOW),
Ok(stat) if FileType::from_raw_mode(stat.st_mode) == FileType::RegularFile
);
let names = read_names(&dirfd)?;
if !marked
&& names
.iter()
.any(|name| name.as_bytes() != RETIRED_MARKER.as_bytes())
{
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"it no longer carries the mark that said it was finished with, and it is not \
empty either",
));
}
for name in &names {
if name.as_bytes() == RETIRED_MARKER.as_bytes() {
continue;
}
match rustix::fs::unlinkat(&dirfd, name.as_c_str(), AtFlags::empty()) {
Ok(()) | Err(rustix::io::Errno::NOENT) => {}
Err(rustix::io::Errno::ISDIR | rustix::io::Errno::PERM) => {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"it has a directory inside it, which a retired chunk environment never \
has. Whatever it is, this build will not walk into it",
))
}
Err(e) => return Err(e.into()),
}
}
if !is_empty_but_for_the_mark(&dirfd)? {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"something was created in it while it was being emptied, so the mark stays and \
the next start looks again",
));
}
match rustix::fs::unlinkat(&dirfd, RETIRED_MARKER, AtFlags::empty()) {
Ok(()) | Err(rustix::io::Errno::NOENT) => Ok(()),
Err(e) => Err(e.into()),
}
}
#[cfg(unix)]
fn open_dir_nofollow(dir: &Path) -> std::io::Result<std::os::fd::OwnedFd> {
use rustix::fs::{Mode, OFlags};
rustix::fs::open(
dir,
OFlags::RDONLY | OFlags::DIRECTORY | OFlags::NOFOLLOW | OFlags::CLOEXEC,
Mode::empty(),
)
.map_err(|e| {
std::io::Error::new(
std::io::ErrorKind::InvalidInput,
format!(
"{} is no longer the directory that was found to be finished with ({e})",
dir.display()
),
)
})
}
#[cfg(unix)]
fn is_empty_but_for_the_mark(dirfd: &std::os::fd::OwnedFd) -> std::io::Result<bool> {
Ok(read_names(dirfd)?
.iter()
.all(|name| name.as_bytes() == RETIRED_MARKER.as_bytes()))
}
#[cfg(unix)]
fn read_names(dirfd: &std::os::fd::OwnedFd) -> std::io::Result<Vec<std::ffi::CString>> {
let mut names = Vec::new();
let dir = rustix::fs::Dir::read_from(dirfd)?;
for entry in dir {
let entry = entry?;
let name = entry.file_name();
if name.to_bytes() == b"." || name.to_bytes() == b".." {
continue;
}
if names.len() >= MAX_ENTRIES {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
format!(
"it holds more than {MAX_ENTRIES} entries, which a retired chunk \
environment never does"
),
));
}
names.push(name.to_owned());
}
Ok(names)
}
#[cfg(not(unix))]
fn empty_and_unmark(dir: &Path) -> std::io::Result<()> {
if !matches!(std::fs::symlink_metadata(dir), Ok(meta) if meta.is_dir()) {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"it is no longer the directory that was found to be finished with",
));
}
let marked = matches!(
std::fs::symlink_metadata(dir.join(RETIRED_MARKER)),
Ok(meta) if meta.is_file()
);
let names = read_names_by_path(dir)?;
if !marked && names.iter().any(|name| name != RETIRED_MARKER) {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"it no longer carries the mark that said it was finished with, and it is not \
empty either",
));
}
for name in &names {
if name == RETIRED_MARKER {
continue;
}
let path = dir.join(name);
if matches!(std::fs::symlink_metadata(&path), Ok(meta) if meta.is_dir()) {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"it has a directory inside it, which a retired chunk environment never has. \
Whatever it is, this build will not walk into it",
));
}
match std::fs::remove_file(&path) {
Ok(()) => {}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
Err(e) => return Err(e),
}
}
if read_names_by_path(dir)?
.iter()
.any(|name| name != RETIRED_MARKER)
{
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"something was created in it while it was being emptied, so the mark stays and \
the next start looks again",
));
}
match std::fs::remove_file(dir.join(RETIRED_MARKER)) {
Ok(()) => Ok(()),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
Err(e) => Err(e),
}
}
#[cfg(not(unix))]
fn read_names_by_path(dir: &Path) -> std::io::Result<Vec<std::ffi::OsString>> {
let mut names = Vec::new();
for entry in std::fs::read_dir(dir)? {
if names.len() >= MAX_ENTRIES {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
format!(
"it holds more than {MAX_ENTRIES} entries, which a retired chunk \
environment never does"
),
));
}
names.push(entry?.file_name());
}
Ok(names)
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
mod tests {
use super::*;
use tempfile::TempDir;
fn wait_gone(path: &Path) -> bool {
for _ in 0..200 {
if !path.exists() {
return true;
}
std::thread::sleep(std::time::Duration::from_millis(10));
}
false
}
fn settle() {
std::thread::sleep(std::time::Duration::from_millis(200));
}
fn env_with_chunks(root: &Path, name: &str) -> PathBuf {
let dir = root.join(name);
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(dir.join("data.mdb"), b"chunks").unwrap();
dir
}
#[cfg(unix)]
fn bind_socket(path: &Path) {
std::os::unix::net::UnixListener::bind(path).expect("bind");
}
fn mark_retired(dir: &Path) {
std::fs::write(dir.join(RETIRED_MARKER), b"retired").unwrap();
}
#[test]
fn nothing_here_can_stop_a_node_starting() {
let root = TempDir::new().unwrap();
env_with_chunks(root.path(), LEGACY_ENV_DIR);
env_with_chunks(root.path(), "chunks.mdb.retired.2");
std::fs::create_dir_all(root.path().join("chunks.mdb.retired")).unwrap();
clean_up(root.path());
settle();
}
#[test]
fn a_root_that_does_not_exist_yet_is_fine() {
let root = TempDir::new().unwrap();
clean_up(&root.path().join("not").join("created"));
}
#[test]
fn a_store_the_migration_finished_with_is_removed() {
let root = TempDir::new().unwrap();
let env = env_with_chunks(root.path(), LEGACY_ENV_DIR);
mark_retired(&env);
clean_up(root.path());
assert!(wait_gone(&env));
}
#[test]
fn a_marked_tombstone_is_removed_too() {
let root = TempDir::new().unwrap();
let tomb = env_with_chunks(root.path(), "chunks.mdb.retired.7");
mark_retired(&tomb);
clean_up(root.path());
assert!(wait_gone(&tomb));
}
#[test]
fn an_empty_leftover_is_removed() {
let root = TempDir::new().unwrap();
let tomb = root.path().join("chunks.mdb.retired");
std::fs::create_dir_all(&tomb).unwrap();
clean_up(root.path());
assert!(wait_gone(&tomb));
}
#[test]
fn a_marked_store_goes_even_when_nothing_was_copied_into_this_node() {
let root = TempDir::new().unwrap();
let env = env_with_chunks(root.path(), LEGACY_ENV_DIR);
mark_retired(&env);
assert!(!root.path().join("chunks").exists());
clean_up(root.path());
assert!(
wait_gone(&env),
"a marked directory must go whether or not this node kept a copy: the close \
group's possession proofs are what cleared it, not a local file"
);
}
#[test]
fn a_store_that_was_never_migrated_is_left_exactly_where_it_is() {
let root = TempDir::new().unwrap();
let env = env_with_chunks(root.path(), LEGACY_ENV_DIR);
clean_up(root.path());
settle();
assert!(
env.join("data.mdb").exists(),
"an unmarked store passed no retirement gate, and was deleted anyway"
);
}
#[test]
fn an_unmarked_tombstone_with_chunks_in_it_is_left_too() {
let root = TempDir::new().unwrap();
let tomb = env_with_chunks(root.path(), "chunks.mdb.retired.3");
clean_up(root.path());
settle();
assert!(tomb.join("data.mdb").exists());
}
#[test]
fn a_directory_that_only_looks_like_a_tombstone_is_left_alone() {
let root = TempDir::new().unwrap();
let mine = env_with_chunks(root.path(), "chunks.mdb.retired-keep-this");
mark_retired(&mine);
let also = env_with_chunks(root.path(), "chunks.mdb.backup");
mark_retired(&also);
let high = env_with_chunks(root.path(), "chunks.mdb.retired.65");
mark_retired(&high);
let padded = env_with_chunks(root.path(), "chunks.mdb.retired.007");
mark_retired(&padded);
clean_up(root.path());
settle();
for kept in [&mine, &also, &high, &padded] {
assert!(
kept.exists(),
"{} was deleted and nothing here created it",
kept.display()
);
}
assert!(is_tombstone_name("chunks.mdb.retired"));
assert!(is_tombstone_name("chunks.mdb.retired.1"));
assert!(is_tombstone_name("chunks.mdb.retired.64"));
assert!(!is_tombstone_name("chunks.mdb.retired.65"));
assert!(!is_tombstone_name("chunks.mdb.retired.0"));
assert!(!is_tombstone_name("chunks.mdb.retired.007"));
assert!(!is_tombstone_name("chunks.mdb.retired-keep-this"));
assert!(!is_tombstone_name("chunks.mdb.retired."));
assert!(!is_tombstone_name("chunks.mdb.retired.x"));
}
#[test]
fn something_else_wearing_the_marks_name_is_not_the_mark() {
let root = TempDir::new().unwrap();
let env = env_with_chunks(root.path(), LEGACY_ENV_DIR);
std::fs::create_dir_all(env.join(RETIRED_MARKER)).unwrap();
clean_up(root.path());
settle();
assert!(
env.join("data.mdb").exists(),
"a directory called RETIRED authorised deleting an unmigrated store"
);
}
#[test]
fn the_mark_outlives_everything_it_was_vouching_for() {
let root = TempDir::new().unwrap();
let env = env_with_chunks(root.path(), LEGACY_ENV_DIR);
std::fs::write(env.join("lock.mdb"), b"lock").unwrap();
mark_retired(&env);
delete_mark_last(&env).unwrap();
assert!(!env.exists());
let other = TempDir::new().unwrap();
let env = env_with_chunks(other.path(), LEGACY_ENV_DIR);
mark_retired(&env);
let nested = env.join("sub");
std::fs::create_dir_all(&nested).unwrap();
std::fs::write(nested.join("data.mdb"), b"chunks").unwrap();
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
std::fs::set_permissions(&nested, std::fs::Permissions::from_mode(0o500)).unwrap();
let failed = delete_mark_last(&env);
std::fs::set_permissions(&nested, std::fs::Permissions::from_mode(0o755)).unwrap();
assert!(failed.is_err(), "the deletion was supposed to fail here");
assert!(
env.join(RETIRED_MARKER).exists(),
"the mark went before the chunks did, so the next start cannot tell this \
directory was already finished with"
);
}
}
#[cfg(unix)]
#[test]
fn the_deleting_thread_refuses_a_path_that_is_no_longer_what_was_classified() {
let root = TempDir::new().unwrap();
let elsewhere = env_with_chunks(root.path(), "elsewhere");
mark_retired(&elsewhere);
let swapped = root.path().join(LEGACY_ENV_DIR);
std::os::unix::fs::symlink(&elsewhere, &swapped).unwrap();
assert!(
delete_mark_last(&swapped).is_err(),
"the deletion followed a link that appeared after the classification"
);
assert!(
elsewhere.join("data.mdb").exists(),
"the link was followed and the contents of somewhere else were deleted"
);
assert!(elsewhere.join(RETIRED_MARKER).exists());
let unmarked = env_with_chunks(root.path(), "chunks.mdb.retired.9");
assert!(delete_mark_last(&unmarked).is_err());
assert!(unmarked.join("data.mdb").exists());
}
#[cfg(unix)]
#[test]
fn a_link_never_yields_a_directory_handle() {
let root = TempDir::new().unwrap();
let real = env_with_chunks(root.path(), "real");
assert!(
open_dir_nofollow(&real).is_ok(),
"a directory that is what it says it is has to open, or nothing is ever cleaned up"
);
let link = root.path().join("link");
std::os::unix::fs::symlink(&real, &link).unwrap();
assert!(
open_dir_nofollow(&link).is_err(),
"a link produced a handle, so every unlink made against it lands on the target"
);
let not_a_dir = root.path().join("file");
std::fs::write(¬_a_dir, b"x").unwrap();
assert!(open_dir_nofollow(¬_a_dir).is_err());
}
#[cfg(unix)]
#[test]
fn only_a_regular_file_is_the_mark_when_the_deleting_thread_asks() {
let root = TempDir::new().unwrap();
let linked = env_with_chunks(root.path(), "chunks.mdb.retired.11");
let real_file = root.path().join("somewhere-else");
std::fs::write(&real_file, b"not the mark").unwrap();
std::os::unix::fs::symlink(&real_file, linked.join(RETIRED_MARKER)).unwrap();
assert!(
delete_mark_last(&linked).is_err(),
"a symlink was accepted as the mark, which authorises deleting an unmigrated store"
);
assert!(
linked.join("data.mdb").exists(),
"and the store it was guarding is gone"
);
let socketed = env_with_chunks(root.path(), "chunks.mdb.retired.12");
bind_socket(&socketed.join(RETIRED_MARKER));
assert!(
delete_mark_last(&socketed).is_err(),
"a socket was accepted as the mark"
);
assert!(socketed.join("data.mdb").exists());
let genuine = env_with_chunks(root.path(), "chunks.mdb.retired.13");
mark_retired(&genuine);
assert!(delete_mark_last(&genuine).is_ok());
assert!(!genuine.exists());
}
#[test]
fn a_directory_inside_a_leftover_is_refused_rather_than_entered() {
let root = TempDir::new().unwrap();
let env = env_with_chunks(root.path(), LEGACY_ENV_DIR);
mark_retired(&env);
let inside = env.join("somewhere");
std::fs::create_dir_all(&inside).unwrap();
std::fs::write(inside.join("not-ours"), b"another filesystem's problem").unwrap();
assert!(
delete_mark_last(&env).is_err(),
"a directory inside a leftover was walked into"
);
assert!(
inside.join("not-ours").exists(),
"and its contents were deleted"
);
assert!(
env.join(RETIRED_MARKER).exists(),
"the mark has to survive a refusal, or the next start cannot tell what this is"
);
}
#[test]
fn the_mark_is_not_removed_over_an_entry_that_arrived_late() {
let root = TempDir::new().unwrap();
let env = env_with_chunks(root.path(), LEGACY_ENV_DIR);
mark_retired(&env);
let stubborn = env.join("arrived-late");
std::fs::create_dir_all(&stubborn).unwrap();
assert!(delete_mark_last(&env).is_err());
assert!(
env.join(RETIRED_MARKER).exists(),
"the mark went while the directory still had something in it, so the next start \
sees an unmigrated store and keeps it for ever"
);
}
#[test]
fn an_implausibly_wide_leftover_is_refused() {
let root = TempDir::new().unwrap();
let env = root.path().join(LEGACY_ENV_DIR);
std::fs::create_dir_all(&env).unwrap();
mark_retired(&env);
for n in 0..(MAX_ENTRIES + 8) {
std::fs::write(env.join(format!("e{n}")), b"x").unwrap();
}
assert!(
delete_mark_last(&env).is_err(),
"a directory with more entries than any leftover has was read in full"
);
assert!(env.join(RETIRED_MARKER).exists());
}
#[test]
fn the_cleanup_removes_exactly_what_classify_calls_harmless() {
let root = TempDir::new().unwrap();
let base = root.path();
let marked_live = env_with_chunks(base, LEGACY_ENV_DIR);
mark_retired(&marked_live);
let marked_tomb = env_with_chunks(base, "chunks.mdb.retired.5");
mark_retired(&marked_tomb);
let empty_tomb = base.join("chunks.mdb.retired");
std::fs::create_dir_all(&empty_tomb).unwrap();
let unmarked_tomb = env_with_chunks(base, "chunks.mdb.retired.6");
let fake_mark = env_with_chunks(base, "chunks.mdb.retired.7");
std::fs::create_dir_all(fake_mark.join(RETIRED_MARKER)).unwrap();
let near_miss = env_with_chunks(base, "chunks.mdb.retired.65");
mark_retired(&near_miss);
let operators = env_with_chunks(base, "chunks.mdb.retired-keep-this");
mark_retired(&operators);
let not_a_dir = base.join("chunks.mdb.retired.8");
std::fs::write(¬_a_dir, b"not a store").unwrap();
#[cfg(unix)]
let linked = {
let elsewhere = env_with_chunks(base, "elsewhere-for-the-link");
mark_retired(&elsewhere);
let link = base.join("chunks.mdb.retired.9");
std::os::unix::fs::symlink(&elsewhere, &link).unwrap();
(link, elsewhere)
};
#[cfg_attr(not(unix), allow(unused_mut))]
let mut considered = vec![
&marked_live,
&marked_tomb,
&empty_tomb,
&unmarked_tomb,
&fake_mark,
¬_a_dir,
];
#[cfg(unix)]
considered.push(&linked.0);
let verdicts: Vec<_> = considered.iter().map(|dir| (*dir, classify(dir))).collect();
clean_up(base);
settle();
for (dir, verdict) in verdicts {
let gone = !dir.exists();
assert_eq!(
gone,
verdict == Leftover::Harmless,
"{} was classified {verdict:?} and {} removed",
dir.display(),
if gone { "was" } else { "was not" }
);
}
for untouched in [&near_miss, &operators] {
assert!(
untouched.join("data.mdb").exists(),
"{} is not a name retirement can have created",
untouched.display()
);
}
#[cfg(unix)]
{
let (link, elsewhere) = &linked;
assert!(
std::fs::symlink_metadata(link).is_ok(),
"the link was removed"
);
assert!(
elsewhere.join("data.mdb").exists(),
"the link was followed and somebody else's data was deleted"
);
}
}
#[test]
fn every_leftover_a_root_can_hold_is_removed_without_a_thread_each() {
let root = TempDir::new().unwrap();
let base = root.path();
let mut staged = vec![env_with_chunks(base, LEGACY_ENV_DIR)];
staged.push(env_with_chunks(base, "chunks.mdb.retired"));
for n in 1..=MAX_TOMBSTONES {
staged.push(env_with_chunks(base, &format!("chunks.mdb.retired.{n}")));
}
assert_eq!(staged.len(), 66, "the accepted namespace is this big");
for dir in &staged {
mark_retired(dir);
}
let before = std::thread::available_parallelism().is_ok();
clean_up(base);
assert!(before, "sanity: the platform reports its parallelism");
for dir in &staged {
assert!(
wait_gone(dir),
"{} was finished with and is still there",
dir.display()
);
}
}
#[cfg(unix)]
#[test]
fn a_root_that_cannot_be_listed_removes_nothing() {
use std::os::unix::fs::PermissionsExt;
let root = TempDir::new().unwrap();
let base = root.path().join("locked");
std::fs::create_dir_all(&base).unwrap();
let env = env_with_chunks(&base, LEGACY_ENV_DIR);
mark_retired(&env);
std::fs::set_permissions(&base, std::fs::Permissions::from_mode(0o000)).unwrap();
clean_up(&base);
settle();
std::fs::set_permissions(&base, std::fs::Permissions::from_mode(0o755)).unwrap();
assert!(
env.join("data.mdb").exists(),
"a root that could not be listed had something removed from it anyway"
);
}
#[cfg(unix)]
#[test]
fn a_linked_environment_is_left_exactly_as_it_is() {
let root = TempDir::new().unwrap();
let elsewhere = env_with_chunks(root.path(), "elsewhere");
mark_retired(&elsewhere);
let link = root.path().join(LEGACY_ENV_DIR);
std::os::unix::fs::symlink(&elsewhere, &link).unwrap();
clean_up(root.path());
settle();
assert!(
std::fs::symlink_metadata(&link).is_ok(),
"the link itself was removed, which throws away the only record of where the \
data went"
);
assert!(
elsewhere.join("data.mdb").exists(),
"the link was followed and somebody else's data was deleted"
);
}
}