38a2fea043
Road files in Pensum's strict header subset (unknown keys are errors, random ids), the directory and device snapshots, deny-by-default compilation to one plan per device, explain from the compiler's own grants, CBOR envelope v1 with version-first refusals. Tests: the six mandatory ones (validation fixtures for every rule, revocation, the least-privilege property against an independent oracle, determinism with golden plans, explain, custom round-trip), QA's attack cases, the protocol and the CLI.
162 lines
5.7 KiB
Rust
162 lines
5.7 KiB
Rust
//! The store: `directory.json`, `devices.json` and `roads/` (docs/FORMATS.md, "The store").
|
|
|
|
use crate::id::RoadId;
|
|
use crate::problem::Problem;
|
|
use crate::road::Road;
|
|
use crate::rules as r;
|
|
use crate::snapshot::{DEVICES_FILE, DIRECTORY_FILE, Devices, Directory};
|
|
use std::collections::BTreeMap;
|
|
use std::path::Path;
|
|
|
|
pub const ROADS_DIR: &str = "roads";
|
|
pub const MAX_ROADS: usize = 10_000;
|
|
|
|
/// Everything the compiler reads, already parsed. Roads with errors are not in `roads`; their problems are.
|
|
#[derive(Debug, Clone, Default)]
|
|
pub struct Store {
|
|
pub roads: Vec<Road>,
|
|
pub directory: Option<Directory>,
|
|
pub devices: Option<Devices>,
|
|
pub problems: Vec<Problem>,
|
|
}
|
|
|
|
impl Store {
|
|
/// Builds a store from bytes: `(file name inside roads/, contents)`, and the two snapshots (`None`: missing).
|
|
pub fn from_parts(
|
|
road_files: &[(String, Vec<u8>)],
|
|
directory: Option<&[u8]>,
|
|
devices: Option<&[u8]>,
|
|
) -> Store {
|
|
let mut store = Store::default();
|
|
match directory {
|
|
None => store.problems.push(Problem::error(
|
|
DIRECTORY_FILE,
|
|
None,
|
|
r::SNAPSHOT_JSON,
|
|
"missing",
|
|
)),
|
|
Some(b) => match Directory::parse(b) {
|
|
Ok(d) => store.directory = Some(d),
|
|
Err(p) => store.problems.extend(p),
|
|
},
|
|
}
|
|
match devices {
|
|
None => store.problems.push(Problem::error(
|
|
DEVICES_FILE,
|
|
None,
|
|
r::SNAPSHOT_JSON,
|
|
"missing",
|
|
)),
|
|
Some(b) => match Devices::parse(b, store.directory.as_ref()) {
|
|
Ok(d) => store.devices = Some(d),
|
|
Err(p) => store.problems.extend(p),
|
|
},
|
|
}
|
|
|
|
let mut files: Vec<&(String, Vec<u8>)> = road_files.iter().collect();
|
|
files.sort_by(|a, b| a.0.cmp(&b.0));
|
|
if files.len() > MAX_ROADS {
|
|
store.problems.push(Problem::error(
|
|
ROADS_DIR,
|
|
None,
|
|
r::FILE_COUNT,
|
|
format!("more than {MAX_ROADS} road files"),
|
|
));
|
|
return store;
|
|
}
|
|
let mut by_id: BTreeMap<RoadId, Vec<Road>> = BTreeMap::new();
|
|
for (name, bytes) in files {
|
|
let path = format!("{ROADS_DIR}/{name}");
|
|
match Road::parse(&path, bytes) {
|
|
Ok(road) => by_id.entry(road.id.clone()).or_default().push(road),
|
|
Err(p) => store.problems.extend(p),
|
|
}
|
|
}
|
|
for (id, roads) in by_id {
|
|
if roads.len() == 1 {
|
|
store.roads.extend(roads);
|
|
continue;
|
|
}
|
|
let paths: Vec<&str> = roads.iter().map(|r| r.path.as_str()).collect();
|
|
for road in &roads {
|
|
store.problems.push(
|
|
Problem::error(
|
|
&road.path,
|
|
Some("id"),
|
|
r::ID_DUPLICATE,
|
|
format!(
|
|
"id {id} is used by {}; none of them grants anything",
|
|
paths.join(" and ")
|
|
),
|
|
)
|
|
.at(road.line_of("id")),
|
|
);
|
|
}
|
|
}
|
|
store
|
|
}
|
|
|
|
/// Reads a store folder. Only the three names are read; anything that is not a regular `.md` file inside
|
|
/// `roads/` is a problem and is not read.
|
|
pub fn load_dir(root: &Path) -> std::io::Result<Store> {
|
|
let directory = read_optional(&root.join(DIRECTORY_FILE))?;
|
|
let devices = read_optional(&root.join(DEVICES_FILE))?;
|
|
let mut files = Vec::new();
|
|
let mut problems = Vec::new();
|
|
let roads = root.join(ROADS_DIR);
|
|
if roads.is_dir() {
|
|
for entry in std::fs::read_dir(&roads)? {
|
|
let entry = entry?;
|
|
let name = entry.file_name().to_string_lossy().into_owned();
|
|
let ty = entry.file_type()?;
|
|
let path = format!("{ROADS_DIR}/{name}");
|
|
if !ty.is_file() {
|
|
let what = if ty.is_symlink() {
|
|
"a symbolic link"
|
|
} else {
|
|
"not a regular file"
|
|
};
|
|
problems.push(Problem::error(
|
|
&path,
|
|
None,
|
|
r::FILE_KIND,
|
|
format!("{what}; roads/ holds only road files"),
|
|
));
|
|
continue;
|
|
}
|
|
if !name.ends_with(".md") {
|
|
problems.push(Problem::error(
|
|
&path,
|
|
None,
|
|
r::FILE_KIND,
|
|
"not a road file (.md); roads/ holds only road files",
|
|
));
|
|
continue;
|
|
}
|
|
if entry.metadata()?.len() > crate::road::MAX_FILE as u64 {
|
|
problems.push(Problem::error(
|
|
&path,
|
|
None,
|
|
r::FILE_SIZE,
|
|
format!("larger than {} KiB", crate::road::MAX_FILE / 1024),
|
|
));
|
|
continue;
|
|
}
|
|
files.push((name, std::fs::read(entry.path())?));
|
|
}
|
|
}
|
|
let mut store = Store::from_parts(&files, directory.as_deref(), devices.as_deref());
|
|
store.problems.extend(problems);
|
|
store.problems.sort();
|
|
Ok(store)
|
|
}
|
|
}
|
|
|
|
fn read_optional(path: &Path) -> std::io::Result<Option<Vec<u8>>> {
|
|
match std::fs::read(path) {
|
|
Ok(b) => Ok(Some(b)),
|
|
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
|
|
Err(e) => Err(e),
|
|
}
|
|
}
|