fix(pack): remove a new store directory whose mode cannot be set

A directory left private after a failed mode change would stay
unreadable to other users on every later install, since existing
directories are not touched. It is now removed again. The test's
relative store is cleaned up even when an assertion fails.
This commit is contained in:
Marco Allegretti 2026-10-09 14:26:43 +00:00
parent 025388f84e
commit fb13a99e17
No known key found for this signature in database

View file

@ -308,8 +308,9 @@ fn install_into(
/// Creates the store and any missing parents at 0755 whatever the umask, so /// Creates the store and any missing parents at 0755 whatever the umask, so
/// a store created by a root install is readable by every user. Each new /// a store created by a root install is readable by every user. Each new
/// directory is created private and its mode set through a descriptor that /// directory is created private and its mode set through a descriptor opened
/// does not follow links, so only the directory just created is changed. /// without following a link in its last component; a directory whose mode
/// cannot be set is removed again rather than left private.
fn create_store(store_root: &Path) -> anyhow::Result<()> { fn create_store(store_root: &Path) -> anyhow::Result<()> {
let mut missing: Vec<&Path> = store_root let mut missing: Vec<&Path> = store_root
.ancestors() .ancestors()
@ -328,15 +329,18 @@ fn create_store(store_root: &Path) -> anyhow::Result<()> {
#[cfg(unix)] #[cfg(unix)]
{ {
use rustix::fs::{CWD, Mode, OFlags}; use rustix::fs::{CWD, Mode, OFlags};
let created = rustix::fs::openat( let opened = rustix::fs::openat(
CWD, CWD,
dir, dir,
OFlags::RDONLY | OFlags::DIRECTORY | OFlags::NOFOLLOW | OFlags::CLOEXEC, OFlags::RDONLY | OFlags::DIRECTORY | OFlags::NOFOLLOW | OFlags::CLOEXEC,
Mode::empty(), Mode::empty(),
) );
.with_context(|| format!("open {}", dir.display()))?; if let Err(e) =
rustix::fs::fchmod(&created, Mode::from_raw_mode(0o755)) opened.and_then(|created| rustix::fs::fchmod(&created, Mode::from_raw_mode(0o755)))
.with_context(|| format!("set the mode of {}", dir.display()))?; {
let _ = std::fs::remove_dir(dir);
return Err(e).with_context(|| format!("set the mode of {}", dir.display()));
}
} }
} }
anyhow::ensure!( anyhow::ensure!(
@ -1371,12 +1375,23 @@ mod tests {
for dir in [home.join("a"), home.join("a/b"), absolute.clone()] { for dir in [home.join("a"), home.join("a/b"), absolute.clone()] {
assert_eq!(mode(&dir), 0o755, "{}", dir.display()); assert_eq!(mode(&dir), 0o755, "{}", dir.display());
} }
// A relative store none of whose components exist yet. // A relative store none of whose components exist yet, removed even
let relative = PathBuf::from(format!("weft-pack-store-{}/sub", std::process::id())); // if an assertion fails.
let _ = std::fs::remove_dir_all(relative.parent().unwrap()); struct Removed(PathBuf);
impl Drop for Removed {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}
let top = Removed(PathBuf::from(format!(
"weft-pack-store-{}",
std::process::id()
)));
let _ = std::fs::remove_dir_all(&top.0);
let relative = top.0.join("sub");
create_store(&relative).unwrap(); create_store(&relative).unwrap();
assert_eq!(mode(&relative), 0o755); assert_eq!(mode(&relative), 0o755);
std::fs::remove_dir_all(relative.parent().unwrap()).unwrap(); drop(top);
// An existing store is left as it is. // An existing store is left as it is.
std::fs::set_permissions(&absolute, std::fs::Permissions::from_mode(0o750)).unwrap(); std::fs::set_permissions(&absolute, std::fs::Permissions::from_mode(0o750)).unwrap();
create_store(&absolute).unwrap(); create_store(&absolute).unwrap();