Something went wrong. Try again.
Plaintext Records, Organized and Verifiable. CLI + Library for managing plaintext information
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289//! Shadowed payloads — the files prov can read but must not.//!//! `attach --opaque` makes a promise: prov will link, move and fixity-check this//! file, but never read *it* as a document. When the payload happens to be//! something prov can parse — a `.md`, a `.yaml` — keeping that promise means//! every scan has to *notice* the sidecar beside it and skip the file. That//! check lives here rather than beside the `attach` verb because the check is a//! read: the census, the title scan and the id scan all owe it, and none of them//! is attaching anything.//!//! The convention is the fast path and the `content` pointer is authoritative. A//! sidecar under a non-conventional name still claims its payload; it just is//! not found by probing, which is why `attach`'s own sweep confirms the pointer//! rather than trusting the name.
use std::collections::HashMap;use std::path::{Path, PathBuf};
use super::{Graph, Target};use crate::document::whole_file_format;use crate::fs::ReadStorage;use crate::index::IdIndex;use crate::link::{self, Link};
pub(crate) const SIDECAR_EXTENSIONS: &[&str] = &["yaml", "yml", "json", "toml", "fig", "figl"];
/// Every path that could be `payload`'s sidecar under the `<payload>.<ext>`/// convention, in reverse-lookup preference order. The probe half of the lookup;/// the `content` pointer confirms a hit ([`Graph::sidecar_claims`]).////// Note the convention cannot collide with a *separated* document's metadata/// half, which replaces the extension (`note.md` → `note.yaml`) rather than/// appending to it (`note.md.yaml`).pub fn sidecar_candidates(payload: &Path) -> impl Iterator<Item = PathBuf> + '_ { let name = payload .file_name() .and_then(|n| n.to_str()) .unwrap_or_default(); SIDECAR_EXTENSIONS .iter() .map(move |ext| payload.with_file_name(format!("{name}.{ext}")))}
/// A directory listing turned around: which of its files are conventionally/// named sidecars, and for what.////// The lookup used to run the other way — build all six `<payload>.<ext>`/// candidates for a file and ask an ordered set of the listing whether it holds/// any of them. That is six `PathBuf` allocations and six ordered-set probes/// *per file scanned*, to answer "no" in every workspace with no attachments in/// it; over twenty thousand documents it was 16% of a `check`, most of it/// comparing paths component by component inside the set.////// Inverting it costs one pass over the listing — the same/// `<payload>.<ext>` convention read backwards, so the two are exact inverses —/// and leaves the per-file question a single hash lookup that usually misses./// A workspace with no attachments builds an empty map and every probe is that/// miss.////// The `content` pointer is still what confirms a hit/// ([`Graph::sidecar_claims`]); this only says which files are worth asking/// about.#[derive(Debug, Default, Clone)]pub struct ShadowProbe { /// Payload path → the conventionally named sidecars actually present, in /// [`sidecar_candidates`]' preference order. sidecars: HashMap<PathBuf, Vec<PathBuf>>,}
impl ShadowProbe { /// Index the workspace-relative files a scan enumerated. pub fn over<'a>(listing: impl IntoIterator<Item = &'a PathBuf>) -> Self { let mut ranked: HashMap<PathBuf, Vec<(usize, PathBuf)>> = HashMap::new(); for entry in listing { let Some(rank) = entry .extension() .and_then(|e| e.to_str()) .and_then(|ext| SIDECAR_EXTENSIONS.iter().position(|known| *known == ext)) else { continue; }; // `photo.jpg.yaml` names `photo.jpg` — `sidecar_candidates` run // backwards, which is what makes this the same question. ranked .entry(entry.with_extension("")) .or_default() .push((rank, entry.clone())); } let sidecars = ranked .into_iter() .map(|(payload, mut found)| { found.sort_by_key(|(rank, _)| *rank); (payload, found.into_iter().map(|(_, path)| path).collect()) }) .collect(); Self { sidecars } }
/// The sidecars the listing holds for `payload`, in preference order — /// empty for a file nothing beside it could be claiming. fn sidecars_for(&self, payload: &Path) -> &[PathBuf] { self.sidecars.get(payload).map_or(&[], Vec::as_slice) }}
impl<FS: ReadStorage, Ix: IdIndex> Graph<FS, Ix> { /// Whether the document at `candidate` is an attachment sidecar whose /// `content` resolves to `payload` — the authoritative half of the reverse /// lookup, the `<payload>.<ext>` convention above being only the probe. /// /// Requires [`is_attachment`](crate::Document::is_attachment), so a separated /// *prose* node never reads as one: its body is a document in its own right, /// and prov must keep scanning it. Unreadable or unparsable candidates simply /// do not claim (this runs inside best-effort scans). pub async fn sidecar_claims(&self, candidate: &Path, payload: &Path) -> bool { let Ok((_, doc)) = self.load(candidate).await else { return false; }; let Some(content) = doc.content_attr() else { return false; }; let dir = candidate.parent().unwrap_or(Path::new("")); doc.is_attachment() && link::normalize(dir.join(content)) == payload }
/// Whether `path` — a file prov *can* read — has been deliberately shadowed: /// claimed as an opaque payload by an attachment sidecar beside it. The /// promise `attach --opaque` makes, enforced: prov links, moves and fixity- /// checks the file (through its sidecar's own `content_hash`) but never /// reads *it* as a document, so its title stays out of the title index, any /// `id` it shows stays out of the registry, any `fields` value it carries is /// never checked against a vocabulary, and any `content_hash` it shows is /// never treated as its own. /// /// `probe` is the [`ShadowProbe`] over the workspace-relative files the /// calling scan already enumerated (its directory read), so a shadow check /// costs one hash lookup rather than a stat per metadata extension — this /// runs per file in the flat title and id scans, and per reachable path in /// the vocabulary and fixity passes /// (`validate::Workspace::reachable_documents`). A sidecar outside the /// listing the probe was built over therefore does not shadow, which is the /// same bound the scans themselves observe. pub async fn is_shadowed_payload(&self, path: &Path, probe: &ShadowProbe) -> bool { for candidate in probe.sidecars_for(path) { if self.sidecar_claims(candidate, path).await { return true; } } false }
/// The payload the attachment sidecar at `path` stands for — its `content` /// target, normalized and workspace-relative — or `None` when `path` is not /// an attachment sidecar: a document, a separated prose node, a payload, a /// manifest node, or nothing at all. /// /// Only a whole-file metadata document can be a sidecar, so any other path /// answers `None` without being read: asking this of every embed in a page /// costs a load only for the references that name a card. The payload is /// not checked for existence; a missing one is `check`'s to report, and a /// caller drawing it learns the same thing by failing to open it. pub async fn attachment_payload(&self, path: &Path) -> Option<PathBuf> { let path = link::normalize(path); whole_file_format(&path)?; let (_, doc) = self.load(&path).await.ok()?; if !doc.is_attachment() { return None; } doc.content_path(&path) }
/// The sidecar that claims `payload` under the `<payload>.<ext>` /// convention ([`sidecar_candidates`], confirmed by /// [`sidecar_claims`](Self::sidecar_claims)), or `None` when it has none — /// the reverse of [`attachment_payload`](Self::attachment_payload). pub async fn attachment_sidecar(&self, payload: &Path) -> Option<PathBuf> { let payload = link::normalize(payload); for candidate in sidecar_candidates(&payload) { if self.sidecar_claims(&candidate, &payload).await { return Some(candidate); } } None }
/// Both handles of the node at `path`: `(sidecar, Some(payload))` when /// `path` is either half of an attachment, `(path, None)` for anything /// else. What [`backlinks_to_node`](Self::backlinks_to_node) inverts the /// census at. pub async fn attachment_handles(&self, path: &Path) -> (PathBuf, Option<PathBuf>) { let path = link::normalize(path); if let Some(payload) = self.attachment_payload(&path).await { return (path, Some(payload)); } match self.attachment_sidecar(&path).await { Some(sidecar) => (sidecar, Some(path)), None => (path, None), } }
/// Resolve `link` (written in the document at `doc`) to the **file it /// stands for**: [`resolve_link`](Self::resolve_link), and then, when the /// target is an attachment sidecar, its payload /// ([`attachment_payload`](Self::attachment_payload)). /// /// A card is the node, so a reference naming one — `[photo](id:x)`, /// `photo.jpg.yaml` — resolves to the card everywhere structure is /// concerned, which is what [`resolve_link`](Self::resolve_link) answers. /// But a host *drawing* the reference wants the bytes: an embed /// `` of a card shows its image, and a download link to a card /// fetches its file. This is that reading. A reference straight to a /// payload resolves to the payload, and one to anything that is not an /// attachment sidecar — a document, an unresolved id, an external URL — /// comes back exactly as `resolve_link` gives it. /// /// Path and `id:` references only, as `resolve_link`; for a nominal /// (`[[alias]]`) reference resolve with /// [`resolve_link_with`](Self::resolve_link_with) and pass the path to /// [`attachment_payload`](Self::attachment_payload). pub async fn resolve_payload(&self, doc: &Path, link: &Link) -> Target { let target = self.resolve_link(doc, link); if let Target::Path(path) = &target && let Some(payload) = self.attachment_payload(path).await { return Target::Path(payload); } target }}
#[cfg(test)]mod tests { use super::*;
fn paths(names: &[&str]) -> Vec<PathBuf> { names.iter().map(PathBuf::from).collect() }
/// The probe is `sidecar_candidates` read backwards, so the two must agree /// about every file in a listing — that equivalence is the whole argument /// for inverting the lookup. #[test] fn the_probe_answers_what_probing_every_candidate_answered() { let listing = paths(&[ "photo.jpg", "photo.jpg.yaml", "notes/scan.pdf", "notes/scan.pdf.json", "notes/a.md", "loose.toml", ]); let probe = ShadowProbe::over(listing.iter()); for path in &listing { let by_candidate: Vec<PathBuf> = sidecar_candidates(path) .filter(|c| listing.contains(c)) .collect(); assert_eq!( probe.sidecars_for(path), by_candidate.as_slice(), "{}", path.display() ); } }
/// A payload with more than one conventional sidecar is reported in /// `SIDECAR_EXTENSIONS` order however the directory listed them, because the /// caller confirms them in that order and stops at the first that claims. #[test] fn several_sidecars_come_back_in_preference_order() { let listing = paths(&["photo.jpg.figl", "photo.jpg.yaml", "photo.jpg.json"]); let probe = ShadowProbe::over(listing.iter()); assert_eq!( probe.sidecars_for(Path::new("photo.jpg")), paths(&["photo.jpg.yaml", "photo.jpg.json", "photo.jpg.figl"]).as_slice() ); }
/// The common case: nothing in the listing could be a sidecar, so every /// file's probe is a miss and no candidate path is ever built. #[test] fn a_listing_with_no_sidecars_claims_nothing() { let listing = paths(&["index.md", "a.md", "b.md"]); let probe = ShadowProbe::over(listing.iter()); assert!(probe.sidecars.is_empty()); assert!(probe.sidecars_for(Path::new("a.md")).is_empty()); }}