aboutsummaryrefslogtreecommitdiffstats
path: root/src/store.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/store.rs')
-rw-r--r--src/store.rs117
1 files changed, 117 insertions, 0 deletions
diff --git a/src/store.rs b/src/store.rs
index 0c8f50d..35b35aa 100644
--- a/src/store.rs
+++ b/src/store.rs
@@ -272,6 +272,90 @@ impl Store {
Ok(submissions)
}
+ /// Moves the incoming submission `dir`, called `name`, to the refused directory,
+ /// with why, in `NAME.json` next to it: for `--status` to show.
+ pub fn refuse_incoming(&self, dir: &Path, name: &str, reasons: &[String]) -> Result<()> {
+ let refused = self.refused_dir();
+ create_dir(&refused, 0o700)?;
+ let target = refused.join(name);
+ let _ = std::fs::remove_dir_all(&target);
+ let _ = std::fs::remove_file(refused.join(format!("{name}.json")));
+ std::fs::rename(dir, &target)
+ .with_context(|| format!("moving {} to {}", dir.display(), target.display()))?;
+ let refusal = Refusal {
+ name: name.to_owned(),
+ at: now(),
+ reasons: reasons.to_vec(),
+ };
+ content::write_file(
+ &refused.join(format!("{name}.json")),
+ &serde_json::to_vec_pretty(&refusal)?,
+ 0o600,
+ )
+ }
+
+ /// The submissions the switch refused, oldest name first, each with why.
+ pub fn refused(&self) -> Result<Vec<Refusal>> {
+ let dir = self.refused_dir();
+ let mut refusals = Vec::new();
+ let entries = match std::fs::read_dir(&dir) {
+ Ok(entries) => entries,
+ Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(refusals),
+ Err(error) => return Err(error).with_context(|| format!("reading {}", dir.display())),
+ };
+ for entry in entries {
+ let entry = entry?;
+ if !entry.file_type()?.is_dir() {
+ continue;
+ }
+ let name = entry.file_name().to_string_lossy().into_owned();
+ let record = dir.join(format!("{name}.json"));
+ let refusal = std::fs::read(&record)
+ .ok()
+ .and_then(|bytes| serde_json::from_slice(&bytes).ok())
+ .unwrap_or(Refusal {
+ name: name.clone(),
+ at: String::new(),
+ reasons: Vec::new(),
+ });
+ refusals.push(refusal);
+ }
+ refusals.sort_by(|one, other| one.name.cmp(&other.name));
+ Ok(refusals)
+ }
+
+ /// Removes the waiting or refused submission `name` (from the incoming or the
+ /// refused directory, with its record of refusal); what was removed. An error if
+ /// there is none of that name.
+ pub fn forget(&self, name: &str) -> Result<Vec<PathBuf>> {
+ if name.is_empty() || name.contains('/') || name == "." || name == ".." {
+ bail!("{name:?} is not the name of a submission");
+ }
+ let mut removed = Vec::new();
+ for path in [
+ self.incoming_dir().join(name),
+ self.refused_dir().join(name),
+ self.refused_dir().join(format!("{name}.json")),
+ ] {
+ let result = if path.is_dir() {
+ std::fs::remove_dir_all(&path)
+ } else {
+ std::fs::remove_file(&path)
+ };
+ match result {
+ Ok(()) => removed.push(path),
+ Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
+ Err(error) => {
+ return Err(error).with_context(|| format!("removing {}", path.display()));
+ }
+ }
+ }
+ if removed.is_empty() {
+ bail!("no submission {name} waits or was refused");
+ }
+ Ok(removed)
+ }
+
/// 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> {
@@ -408,3 +492,36 @@ fn sync_dir(dir: &Path) -> Result<()> {
.and_then(|file| file.sync_all())
.with_context(|| format!("flushing {}", dir.display()))
}
+
+/// A submission the switch refused: its name in the incoming directory, when, and why.
+#[derive(Debug, Clone, Serialize, Deserialize)]
+pub struct Refusal {
+ pub name: String,
+ /// UTC, as `YYYY-MM-DDTHH:MM:SSZ`.
+ pub at: String,
+ pub reasons: Vec<String>,
+}
+
+/// Now, in UTC, as `YYYY-MM-DDTHH:MM:SSZ`.
+fn now() -> String {
+ let seconds = std::time::SystemTime::now()
+ .duration_since(std::time::UNIX_EPOCH)
+ .map_or(0, |since| since.as_secs());
+ let (days, rest) = (seconds / 86_400, seconds % 86_400);
+ // Days since 1970-01-01 as a civil date (Howard Hinnant's algorithm).
+ let z = days as i64 + 719_468;
+ let era = z.div_euclid(146_097);
+ let doe = z.rem_euclid(146_097);
+ let yoe = (doe - doe / 1_460 + doe / 36_524 - doe / 146_096) / 365;
+ let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
+ let mp = (5 * doy + 2) / 153;
+ let day = doy - (153 * mp + 2) / 5 + 1;
+ let month = if mp < 10 { mp + 3 } else { mp - 9 };
+ let year = yoe + era * 400 + i64::from(month <= 2);
+ format!(
+ "{year:04}-{month:02}-{day:02}T{:02}:{:02}:{:02}Z",
+ rest / 3_600,
+ rest % 3_600 / 60,
+ rest % 60
+ )
+}