91 lines
3.0 KiB
Rust
91 lines
3.0 KiB
Rust
//! Version preconditions for known mutable roles. These are actual observations
|
|
//! under the job's cooperative lock, not an atomic filesystem snapshot or lease.
|
|
use std::{
|
|
collections::BTreeMap,
|
|
path::{Component, Path, PathBuf},
|
|
};
|
|
use texrra_tex_core::resources::ResourceVersion;
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
|
pub struct GeneratedInitialState {
|
|
schema: u32,
|
|
cwd: PathBuf,
|
|
files: BTreeMap<PathBuf, Option<ResourceVersion>>,
|
|
}
|
|
impl GeneratedInitialState {
|
|
pub fn new(
|
|
cwd: PathBuf,
|
|
files: BTreeMap<PathBuf, Option<ResourceVersion>>,
|
|
) -> Result<Self, String> {
|
|
let state = Self {
|
|
schema: 1,
|
|
cwd,
|
|
files,
|
|
};
|
|
state.validate()?;
|
|
Ok(state)
|
|
}
|
|
pub fn cwd(&self) -> &Path {
|
|
&self.cwd
|
|
}
|
|
pub fn files(&self) -> &BTreeMap<PathBuf, Option<ResourceVersion>> {
|
|
&self.files
|
|
}
|
|
pub fn validate(&self) -> Result<(), String> {
|
|
if self.schema != 1 || !normalized(&self.cwd) {
|
|
return Err("incompatible generated initial state".into());
|
|
}
|
|
for (path, version) in &self.files {
|
|
if !normalized(path)
|
|
|| version.as_ref().is_some_and(|v| {
|
|
v.path != *path
|
|
|| v.sha256.len() != 64
|
|
|| !v
|
|
.sha256
|
|
.bytes()
|
|
.all(|c| c.is_ascii_digit() || (b'a'..=b'f').contains(&c))
|
|
})
|
|
{
|
|
return Err(format!(
|
|
"invalid generated initial entry: {}",
|
|
path.display()
|
|
));
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
pub(crate) fn check(&self, actual: &Self) -> Result<(), String> {
|
|
if self.cwd != actual.cwd {
|
|
return Err("generated initial workdir differs from the actual host".into());
|
|
}
|
|
for path in self.files.keys().chain(actual.files.keys()) {
|
|
if self.files.get(path) != actual.files.get(path) {
|
|
return Err(format!(
|
|
"generated initial role or version mismatch: {}",
|
|
path.display()
|
|
));
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|
|
fn normalized(path: &Path) -> bool {
|
|
path.is_absolute()
|
|
&& path.components().collect::<PathBuf>().as_os_str() == path.as_os_str()
|
|
&& !path
|
|
.components()
|
|
.any(|c| matches!(c, Component::CurDir | Component::ParentDir))
|
|
}
|
|
|
|
#[derive(Clone, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
|
pub enum GeneratedInputPolicy {
|
|
/// Existing live I/O policy; no extra initial-state reads.
|
|
#[default]
|
|
Live,
|
|
/// Observe every known mutable role before the transcript is opened.
|
|
Observe,
|
|
/// Require exactly this role set and these versions. None means absence.
|
|
/// This neither adopts files nor authorizes skipping actual I/O.
|
|
Expect(GeneratedInitialState),
|
|
}
|