Add image ls and image rm commands
This commit is contained in:
1 parent
37f537184c
commit
8b544ab688
3 files changed
+61
-23
No files matched your search
+18
-23
@@ -1,8 +1,8 @@
|
||||
//! Registry: local storage of built VM artifacts under
|
||||
//! `$XDG_DATA_HOME/slim-rs/registry/<image>/`.
|
||||
|
||||
use anyhow::{Context, Result, bail};
|
||||
use std::path::PathBuf;
|
||||
use anyhow::{Context, Result, anyhow, bail};
|
||||
use std::{fs, io, path::PathBuf};
|
||||
|
||||
/// Validate a user-supplied image name before it is used in a registry path.
|
||||
///
|
||||
@@ -26,30 +26,25 @@ pub(crate) fn validate_image_name(name: &str) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn registry_base_dir() -> Result<PathBuf> {
|
||||
let base = xdg::BaseDirectories::with_prefix("slim-rs");
|
||||
base.create_data_directory("registry")
|
||||
.context("Failed to create XDG_DATA_HOME subdirectory")
|
||||
}
|
||||
|
||||
pub(crate) fn registry_dir(image: &str) -> Result<PathBuf> {
|
||||
validate_image_name(image)?;
|
||||
let base = xdg::BaseDirectories::with_prefix("slim-rs");
|
||||
let initrd_path = base
|
||||
.place_data_file(format!("registry/{image}/initrd"))
|
||||
.context("Failed to write to XDG_DATA_HOME")?;
|
||||
let reg_dir = initrd_path
|
||||
.parent()
|
||||
.expect("data file has a parent")
|
||||
.to_owned();
|
||||
let base = registry_base_dir()?;
|
||||
|
||||
let reg_dir = base.join(image);
|
||||
fs::create_dir(®_dir)
|
||||
.or_else(|e| {
|
||||
(e.kind() == io::ErrorKind::AlreadyExists)
|
||||
.then_some(())
|
||||
.ok_or(e)
|
||||
})
|
||||
.with_context(|| anyhow!("Failed to create {reg_dir:?}"))?;
|
||||
|
||||
// Defense in depth: the constructed path must stay inside the registry.
|
||||
let registry_root = base
|
||||
.place_data_file("registry/.root-check")
|
||||
.context("Failed to write to XDG_DATA_HOME")?
|
||||
.parent()
|
||||
.expect("data file has a parent")
|
||||
.to_owned();
|
||||
if !reg_dir.starts_with(®istry_root) {
|
||||
bail!(
|
||||
"invalid image name '{image}': registry path escapes {}",
|
||||
registry_root.display()
|
||||
);
|
||||
}
|
||||
Ok(reg_dir)
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user