aboutsummaryrefslogtreecommitdiffstats
path: root/src/store.rs
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-30 18:21:16 +0000
committertv <tv@krebsco.de>2026-11-30 19:51:09 +0000
commitb74359e97643be0f43034c3aeab533a2389a939f (patch)
tree4f13dc922acb8f7bef53ca3e787d0a296692bb52 /src/store.rs
parent62f857e35f94e98b3205502e410f0d727fa5ecc5 (diff)
enstate: submissions waiting for the next switch
--migrate first takes the submissions in STATE/incoming, in the order of their names: planned against the schema, stored if accepted (their views follow with the rest), removed, and moved to STATE/refused if refused, which refuses the migration. A dry run (the check before a switch) does so on a copy of the snapshots, changing nothing. --stage leaves a marker, STATE/migrate-due, for the next switch to migrate even if the schema doesn't change; --migrate removes it. Scratch directories are cleaned up without following the symbolic links in them.
Diffstat (limited to 'src/store.rs')
-rw-r--r--src/store.rs77
1 files changed, 77 insertions, 0 deletions
diff --git a/src/store.rs b/src/store.rs
index 4ac5ef2..7e6531e 100644
--- a/src/store.rs
+++ b/src/store.rs
@@ -7,6 +7,9 @@
//! STATE/channels/CHANNEL/snapshots/ID/schema.json the schema it was accepted under
//! STATE/channels/CHANNEL/snapshots/ID/entries/SLOT
//! STATE/channels/CHANNEL/snapshots/ID/kept/SLOT what no slot wanted, types in snapshot.json
+//! STATE/incoming/NAME/ submissions for the next --migrate
+//! STATE/refused/NAME/ those it refused
+//! STATE/migrate-due something was staged for the next switch
//! RUN/views/VIEW -> .generations/VIEW.ID
//! RUN/tmp/ scratch
//! ```
@@ -202,6 +205,55 @@ impl Store {
Ok(id)
}
+ /// Where submissions wait for the next `--migrate`, each in a directory of its own,
+ /// taken in the order of their names.
+ pub fn incoming_dir(&self) -> PathBuf {
+ self.state.join("incoming")
+ }
+
+ /// Where `--migrate` moves incoming submissions it refused.
+ pub fn refused_dir(&self) -> PathBuf {
+ self.state.join("refused")
+ }
+
+ /// A marker that a submission was staged, so the next switch serves the views
+ /// anew (`--migrate`), even if the schema doesn't change. `--migrate` removes it.
+ pub fn migrate_due(&self) -> PathBuf {
+ self.state.join("migrate-due")
+ }
+
+ /// The submissions waiting in the incoming directory, in order.
+ pub fn incoming(&self) -> Result<Vec<PathBuf>> {
+ let dir = self.incoming_dir();
+ let mut submissions = Vec::new();
+ match std::fs::read_dir(&dir) {
+ Ok(entries) => {
+ for entry in entries {
+ let entry = entry?;
+ if entry.file_type()?.is_dir()
+ && !entry.file_name().to_string_lossy().starts_with('.')
+ {
+ submissions.push(entry.path());
+ }
+ }
+ }
+ Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
+ Err(error) => return Err(error).with_context(|| format!("reading {}", dir.display())),
+ }
+ submissions.sort();
+ Ok(submissions)
+ }
+
+ /// A copy of the snapshots in `dir`, as a store with the same views: for a dry run
+ /// that applies submissions without changing anything.
+ pub fn copy_to(&self, dir: &Path) -> Result<Store> {
+ copy_tree(&self.state.join("channels"), &dir.join("channels"))?;
+ Ok(Store {
+ state: dir.to_owned(),
+ run: self.run.clone(),
+ })
+ }
+
/// The views in place.
pub fn installed_views(&self) -> Result<Vec<String>> {
let views = self.run.join("views");
@@ -279,3 +331,28 @@ fn replace_link(link: &Path, target: &Path) -> Result<()> {
std::os::unix::fs::symlink(target, &new)?;
std::fs::rename(&new, link).with_context(|| format!("replacing {}", link.display()))
}
+
+/// Copies the tree `from` to `to`: directories, regular files and symbolic links. A
+/// missing `from` copies nothing.
+fn copy_tree(from: &Path, to: &Path) -> Result<()> {
+ let entries = match std::fs::read_dir(from) {
+ Ok(entries) => entries,
+ Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(()),
+ Err(error) => return Err(error).with_context(|| format!("reading {}", from.display())),
+ };
+ create_dir(to, 0o700)?;
+ for entry in entries {
+ let entry = entry?;
+ let (source, target) = (entry.path(), to.join(entry.file_name()));
+ let kind = entry.file_type()?;
+ if kind.is_symlink() {
+ std::os::unix::fs::symlink(std::fs::read_link(&source)?, &target)?;
+ } else if kind.is_dir() {
+ copy_tree(&source, &target)?;
+ } else {
+ std::fs::copy(&source, &target)
+ .with_context(|| format!("copying {}", source.display()))?;
+ }
+ }
+ Ok(())
+}