aboutsummaryrefslogtreecommitdiffstats
path: root/src/store.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/store.rs')
-rw-r--r--src/store.rs60
1 files changed, 58 insertions, 2 deletions
diff --git a/src/store.rs b/src/store.rs
index b25495e..4ac5ef2 100644
--- a/src/store.rs
+++ b/src/store.rs
@@ -6,6 +6,7 @@
//! STATE/channels/CHANNEL/snapshots/ID/snapshot.json id, and the id it was built on
//! 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
//! RUN/views/VIEW -> .generations/VIEW.ID
//! RUN/tmp/ scratch
//! ```
@@ -24,6 +25,9 @@ use serde::{Deserialize, Serialize};
use crate::content::{self, Content};
use crate::scratch::Scratch;
+/// Entries kept: by slot name, the type each had, and the entry.
+pub type Kept = std::collections::BTreeMap<String, (String, Vec<u8>)>;
+
pub struct Store {
pub state: PathBuf,
pub run: PathBuf,
@@ -34,6 +38,9 @@ pub struct Snapshot {
pub id: String,
pub base: Option<String>,
pub content: Content,
+ /// Entries no slot of the schema it was accepted under wanted, by slot name, with
+ /// the types they had (docs/design.md, section 6).
+ pub kept: Kept,
/// The schema it was accepted under, as it was.
pub schema: Vec<u8>,
}
@@ -44,6 +51,9 @@ struct SnapshotFile {
id: String,
channel: String,
base: Option<String>,
+ /// The types of the entries in `kept/`.
+ #[serde(default, skip_serializing_if = "std::collections::BTreeMap::is_empty")]
+ kept: std::collections::BTreeMap<String, String>,
}
/// Held while the store is used; released when dropped.
@@ -122,20 +132,33 @@ impl Store {
}
let schema = std::fs::read(dir.join("schema.json"))
.with_context(|| format!("reading {}", dir.join("schema.json").display()))?;
+ let mut kept_entries = content::read_dir(&dir.join("kept"))?;
+ let mut kept = Kept::new();
+ for (slot, media_type) in file.kept {
+ let bytes = kept_entries
+ .remove(&slot)
+ .with_context(|| format!("{}: kept {slot} is missing", dir.display()))?;
+ kept.insert(slot, (media_type, bytes));
+ }
+ if let Some(slot) = kept_entries.keys().next() {
+ bail!("{}: kept {slot} has no type", dir.display());
+ }
Ok(Some(Snapshot {
id: file.id,
base: file.base,
content: content::read_dir(&dir.join("entries"))?,
+ kept,
schema,
}))
}
- /// Makes `content` the current snapshot of `channel`, accepted under `schema`,
- /// built on `base`; returns its id. The snapshot it replaces is removed.
+ /// Makes `content`, and `kept`, the current snapshot of `channel`, accepted under
+ /// `schema`, built on `base`; returns its id. The snapshot it replaces is removed.
pub fn commit(
&self,
channel: &str,
content: &Content,
+ kept: &Kept,
base: Option<&str>,
schema: &[u8],
) -> Result<String> {
@@ -148,6 +171,10 @@ impl Store {
id: id.clone(),
channel: channel.to_owned(),
base: base.map(str::to_owned),
+ kept: kept
+ .iter()
+ .map(|(slot, (media_type, _))| (slot.clone(), media_type.clone()))
+ .collect(),
};
content::write_file(
&new.join("snapshot.json"),
@@ -156,6 +183,13 @@ impl Store {
)?;
content::write_file(&new.join("schema.json"), schema, 0o600)?;
content::write_dir(content, &new.join("entries"), 0o600)?;
+ if !kept.is_empty() {
+ let kept: Content = kept
+ .iter()
+ .map(|(slot, (_, bytes))| (slot.clone(), bytes.clone()))
+ .collect();
+ content::write_dir(&kept, &new.join("kept"), 0o600)?;
+ }
std::fs::rename(&new, snapshots.join(&id))?;
let old = std::fs::read_link(self.channel_dir(channel).join("current")).ok();
replace_link(
@@ -168,6 +202,28 @@ impl Store {
Ok(id)
}
+ /// The views in place.
+ pub fn installed_views(&self) -> Result<Vec<String>> {
+ let views = self.run.join("views");
+ let mut names = Vec::new();
+ let entries = match std::fs::read_dir(&views) {
+ Ok(entries) => entries,
+ Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(names),
+ Err(error) => {
+ return Err(error).with_context(|| format!("reading {}", views.display()));
+ }
+ };
+ for entry in entries {
+ let entry = entry?;
+ let name = entry.file_name().to_string_lossy().into_owned();
+ if entry.file_type()?.is_symlink() && !name.ends_with(".new-link") {
+ names.push(name);
+ }
+ }
+ names.sort();
+ Ok(names)
+ }
+
/// The directory the view `name` is in place at, if it is.
pub fn installed_view(&self, name: &str) -> Option<PathBuf> {
let link = self.run.join("views").join(name);