use std::collections::HashMap;
use std::fs::File;
use std::path::Path;
use std::sync::{Arc, OnceLock};
use znippy_common::index::{locate_file, ChunkLoc};
use znippy_common::meta_sink_append::read_delta_map;
use znippy_common::{ZnippyArchive, ZnippyReader};
use crate::backend::{unsupported, Archive, Cap, ChunkIter, HttpReply, StreamedGet};
use crate::{ArchiveTokens, ArtifactEntry, ArtifactId, Bytes};
pub const STREAM_MIN_BYTES: u64 = 1024 * 1024;
pub struct ZnippyBase {
name: String,
archive: Option<Arc<ZnippyArchive>>,
reader: Option<Arc<dyn ZnippyReader>>,
tokens: ArchiveTokens,
pieces: Option<Pieces>,
}
struct Pieces {
file: Arc<File>,
len: u64,
id: (u64, u64),
deltas: OnceLock<Option<DeltaMap>>,
plans: DocMemo<Arc<Plan>>,
}
type DeltaMap = HashMap<String, Vec<(u32, String)>>;
struct Plan {
locs: Vec<ChunkLoc>,
bases: Vec<Option<String>>,
}
fn file_id(m: &std::fs::Metadata) -> (u64, u64) {
use std::os::unix::fs::MetadataExt;
(m.dev(), m.ino())
}
impl Pieces {
fn open(path: &Path) -> Option<Self> {
let file = File::open(path).ok()?;
let m = file.metadata().ok()?;
Some(Self {
id: file_id(&m),
len: m.len(),
file: Arc::new(file),
deltas: OnceLock::new(),
plans: DocMemo::new(4096),
})
}
fn plan(&self, path: &Path, rel: &str, size: u64) -> Option<Arc<Plan>> {
let deltas = self
.deltas
.get_or_init(|| {
let rows = read_delta_map(path).ok()?;
let mut by_file: DeltaMap = HashMap::new();
for (p, seq, base) in rows {
by_file.entry(p).or_default().push((seq, base));
}
Some(by_file)
})
.as_ref()?;
self.plans.get_or(rel, || {
let same = std::fs::metadata(path).ok().map(|m| file_id(&m)) == Some(self.id);
if !same {
return None;
}
let locs = locate_file(path, rel).ok()?;
let of_file = deltas.get(rel);
let bases: Vec<Option<String>> = locs
.iter()
.map(|c| {
of_file
.and_then(|d| d.iter().find(|(seq, _)| *seq == c.chunk_seq))
.map(|(_, base)| base.clone())
})
.collect();
plan_holds(&locs, &bases, size, self.len).then(|| Arc::new(Plan { locs, bases }))
})
}
}
fn plan_holds(locs: &[ChunkLoc], bases: &[Option<String>], size: u64, archive_len: u64) -> bool {
let mut at = 0u64;
let mut seq = None;
for (c, base) in locs.iter().zip(bases) {
let in_bounds = c
.blob_offset
.checked_add(c.blob_size)
.is_some_and(|end| end <= archive_len);
let stored_whole = base.is_some() || c.compressed || c.blob_size == c.uncompressed_size;
if !in_bounds
|| !stored_whole
|| c.fdata_offset != at
|| seq.is_some_and(|s| c.chunk_seq <= s)
{
return false;
}
seq = Some(c.chunk_seq);
at += c.uncompressed_size;
}
at == size && (size == 0 || !locs.is_empty())
}
const DELTA_FRAME_SLACK: u64 = 64 * 1024;
struct ArchivePieces {
file: Arc<File>,
archive: Arc<ZnippyArchive>,
plan: Arc<Plan>,
chunk: usize,
at: u64,
piece: usize,
decoded: Option<(Bytes, usize)>,
done: bool,
}
impl ArchivePieces {
fn step(&mut self) -> anyhow::Result<Option<Bytes>> {
use std::os::unix::fs::FileExt;
loop {
if let Some((whole, out)) = &mut self.decoded {
if *out < whole.len() {
let end = (*out + self.piece).min(whole.len());
let p = whole.slice(*out..end);
*out = end;
return Ok(Some(p));
}
self.decoded = None;
self.chunk += 1;
continue;
}
let Some(c) = self.plan.locs.get(self.chunk) else {
return Ok(None);
};
let base = self.plan.bases[self.chunk].as_deref();
if base.is_none() && !c.compressed {
if self.at >= c.blob_size {
self.chunk += 1;
self.at = 0;
continue;
}
let n = (c.blob_size - self.at).min(self.piece as u64) as usize;
let mut buf = vec![0u8; n];
self.file.read_exact_at(&mut buf, c.blob_offset + self.at)?;
self.at += n as u64;
return Ok(Some(Bytes::from(buf)));
}
let chunk = decode_chunk(&self.file, &self.archive, c, base)?;
self.decoded = Some((Bytes::from(chunk), 0));
}
}
}
fn decode_chunk(
file: &File,
archive: &ZnippyArchive,
c: &ChunkLoc,
base: Option<&str>,
) -> anyhow::Result<Vec<u8>> {
use std::os::unix::fs::FileExt;
let mut blob = vec![0u8; c.blob_size as usize];
file.read_exact_at(&mut blob, c.blob_offset)?;
let Some(base) = base else {
if !c.compressed {
return Ok(blob);
}
let mut out = Vec::new();
let limit = usize::try_from(c.uncompressed_size).unwrap_or(usize::MAX);
znippy_common::codec::decompress_into_max(&blob, &mut out, limit)?;
anyhow::ensure!(
out.len() as u64 == c.uncompressed_size,
"a chunk decoded to {} bytes, its index row says {}",
out.len(),
c.uncompressed_size
);
return Ok(out);
};
let instructions = if c.compressed {
let limit = c
.uncompressed_size
.saturating_add(c.uncompressed_size / 64)
.saturating_add(DELTA_FRAME_SLACK);
let mut out = Vec::new();
znippy_common::codec::decompress_into_max(
&blob,
&mut out,
usize::try_from(limit).unwrap_or(usize::MAX),
)?;
out
} else {
blob
};
let base_bytes = archive.extract_file(base)?;
let rebuilt = znippy_common::archive::apply_delta(&base_bytes, &instructions)?;
drop(base_bytes);
anyhow::ensure!(
rebuilt.len() as u64 == c.uncompressed_size,
"a delta chunk rebuilt {} bytes against {base}, its index row says {}",
rebuilt.len(),
c.uncompressed_size
);
anyhow::ensure!(
c.checksum == [0u8; 32]
|| znippy_common::blake3::hash(&rebuilt).as_bytes()[..] == c.checksum[..],
"a delta chunk rebuilt against {base} does not match its checksum"
);
Ok(rebuilt)
}
pub struct RangedFile {
file: Arc<File>,
archive: Arc<ZnippyArchive>,
plan: Arc<Plan>,
len: u64,
held: Option<(usize, Vec<u8>)>,
}
impl RangedFile {
pub fn len(&self) -> u64 {
self.len
}
pub fn is_empty(&self) -> bool {
self.len == 0
}
pub fn read_at(&mut self, offset: u64, buf: &mut [u8]) -> anyhow::Result<()> {
use std::os::unix::fs::FileExt;
let end = offset
.checked_add(buf.len() as u64)
.filter(|&e| e <= self.len)
.ok_or_else(|| {
anyhow::anyhow!(
"a read of {} bytes at {offset} is past the {} byte file",
buf.len(),
self.len
)
})?;
let (mut at, mut out) = (offset, 0usize);
while at < end {
let i = self
.plan
.locs
.partition_point(|c| c.fdata_offset <= at)
.checked_sub(1)
.ok_or_else(|| anyhow::anyhow!("no chunk holds offset {at}"))?;
let c = &self.plan.locs[i];
let into = at - c.fdata_offset;
let n = c.uncompressed_size.saturating_sub(into).min(end - at) as usize;
anyhow::ensure!(n > 0, "no chunk holds offset {at}");
let base = self.plan.bases[i].as_deref();
if base.is_none() && !c.compressed {
self.file
.read_exact_at(&mut buf[out..out + n], c.blob_offset + into)?;
} else {
if self.held.as_ref().map(|(h, _)| *h) != Some(i) {
self.held = None;
let bytes = decode_chunk(&self.file, &self.archive, c, base)?;
self.held = Some((i, bytes));
}
let Some((_, bytes)) = &self.held else {
unreachable!("a chunk was just decoded")
};
let from = into as usize;
buf[out..out + n].copy_from_slice(&bytes[from..from + n]);
}
at += n as u64;
out += n;
}
Ok(())
}
}
impl Iterator for ArchivePieces {
type Item = anyhow::Result<Bytes>;
fn next(&mut self) -> Option<Self::Item> {
if self.done {
return None;
}
match self.step() {
Ok(Some(p)) => Some(Ok(p)),
Ok(None) => {
self.done = true;
None
}
Err(e) => {
self.done = true;
Some(Err(e))
}
}
}
}
impl ZnippyBase {
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
archive: None,
reader: None,
tokens: ArchiveTokens::none(),
pieces: None,
}
}
pub fn with_archive(name: impl Into<String>, path: impl AsRef<Path>) -> anyhow::Result<Self> {
let path = path.as_ref();
let archive = Arc::new(ZnippyArchive::open(path)?);
Ok(Self {
name: name.into(),
reader: Some(Arc::clone(&archive) as Arc<dyn ZnippyReader>),
archive: Some(archive),
tokens: ArchiveTokens::at(path),
pieces: Pieces::open(path),
})
}
pub fn with_reader(name: impl Into<String>, reader: Arc<dyn ZnippyReader>) -> Self {
Self {
name: name.into(),
archive: None,
reader: Some(reader),
tokens: ArchiveTokens::none(),
pieces: None,
}
}
pub fn writable(name: impl Into<String>, path: impl AsRef<Path>) -> Self {
Self {
name: name.into(),
archive: None,
reader: None,
tokens: ArchiveTokens::at(path.as_ref()),
pieces: None,
}
}
pub fn name(&self) -> &str {
&self.name
}
pub fn typed(&self) -> Option<&Arc<ZnippyArchive>> {
self.archive.as_ref()
}
pub fn reader(&self) -> Option<&Arc<dyn ZnippyReader>> {
self.reader.as_ref()
}
pub fn has_archive(&self) -> bool {
self.reader.is_some()
}
pub fn tokens(&self) -> &ArchiveTokens {
&self.tokens
}
pub fn list_files(&self) -> Vec<String> {
self.reader
.as_ref()
.and_then(|r| r.list_files().ok())
.unwrap_or_default()
}
pub fn get_file(&self, relative_path: &str) -> anyhow::Result<Vec<u8>> {
let reader = self
.reader
.as_ref()
.ok_or_else(|| anyhow::anyhow!("No reader configured"))?;
reader.extract_file(relative_path)
}
pub fn fetch_file(&self, relative_path: &str) -> anyhow::Result<Option<Bytes>> {
Ok(self.get_file(relative_path).ok().map(Bytes::from))
}
pub fn file_pieces(&self, rel: &str, piece: usize) -> Option<ChunkIter> {
let pieces = self.pieces.as_ref()?;
let path = self.tokens.path()?;
let archive = self.archive.as_ref()?;
let size = archive.file_size(rel)?;
let plan = pieces.plan(path, rel, size)?;
Some(ChunkIter {
len: Some(size),
pieces: Box::new(ArchivePieces {
file: Arc::clone(&pieces.file),
archive: Arc::clone(archive),
plan,
chunk: 0,
at: 0,
piece: piece.max(1),
decoded: None,
done: false,
}),
})
}
pub fn file_at(&self, rel: &str) -> Option<RangedFile> {
let pieces = self.pieces.as_ref()?;
let path = self.tokens.path()?;
let archive = self.archive.as_ref()?;
let size = archive.file_size(rel)?;
let plan = pieces.plan(path, rel, size)?;
Some(RangedFile {
file: Arc::clone(&pieces.file),
archive: Arc::clone(archive),
plan,
len: size,
held: None,
})
}
pub fn stream_get(
&self,
rel: &str,
content_type: &str,
piece: usize,
) -> anyhow::Result<Option<StreamedGet>> {
let big = self
.reader
.as_ref()
.and_then(|r| r.file_size(rel))
.is_some_and(|n| n >= STREAM_MIN_BYTES);
if !big {
return Ok(None);
}
Ok(self.file_pieces(rel, piece).map(|body| StreamedGet {
headers: vec![("Content-Type".into(), content_type.into())],
body,
}))
}
pub fn for_each_piece(&self, rel: &str, mut f: impl FnMut(&[u8])) -> anyhow::Result<()> {
if let Some(pieces) = self.file_pieces(rel, 1024 * 1024) {
for p in pieces {
f(&p?);
}
return Ok(());
}
f(&self.get_file(rel)?);
Ok(())
}
pub fn sha256_hex_of(&self, rel: &str) -> anyhow::Result<String> {
let mut h = nornir_hash::Sha256Hasher::new();
self.for_each_piece(rel, |p| h.update(p))?;
Ok(h.finalize_hex())
}
pub fn archive(&self) -> anyhow::Result<Archive> {
match &self.reader {
Some(r) => Archive::read(&self.name, r.as_ref()),
None => unsupported(&self.name, Cap::Archive),
}
}
pub fn refuse_put(&self) -> anyhow::Result<()> {
anyhow::bail!("znippy repository '{}' is read-only", self.name)
}
pub fn strip_repo<'a>(&self, suburl: &'a str) -> &'a str {
strip_repo(&self.name, suburl)
}
pub fn list_entries<F>(
&self,
name_filter: Option<&str>,
limit: usize,
content_type: &str,
coordinate: F,
) -> anyhow::Result<Vec<ArtifactEntry>>
where
F: Fn(&str) -> Option<ArtifactId>,
{
let Some(reader) = &self.reader else {
return Ok(Vec::new());
};
let tokens = self.tokens.open();
crate::list_archive_entries(
reader.as_ref(),
tokens.as_ref(),
name_filter,
limit,
content_type,
coordinate,
)
}
}
pub fn path_only(suburl: &str) -> &str {
suburl.split('?').next().unwrap_or(suburl)
}
pub fn strip_repo<'a>(repo: &str, suburl: &'a str) -> &'a str {
let path = suburl.trim_start_matches('/');
path.strip_prefix(repo)
.map(|r| r.trim_start_matches('/'))
.unwrap_or(path)
}
pub fn not_found(reason: &str) -> HttpReply {
(404, Vec::new(), reason.as_bytes().to_vec())
}
pub fn ok(content_type: &str, body: Vec<u8>) -> HttpReply {
(
200,
vec![("Content-Type".into(), content_type.into())],
body,
)
}
pub struct DocMemo<V: Clone = crate::Bytes> {
cap: usize,
map: std::sync::Mutex<std::collections::HashMap<String, V>>,
}
impl<V: Clone> DocMemo<V> {
pub fn new(cap: usize) -> Self {
Self {
cap,
map: std::sync::Mutex::new(std::collections::HashMap::new()),
}
}
pub fn get_or(&self, key: &str, build: impl FnOnce() -> Option<V>) -> Option<V> {
if let Some(hit) = self.map.lock().unwrap_or_else(|p| p.into_inner()).get(key) {
return Some(hit.clone());
}
let built = build()?;
let mut map = self.map.lock().unwrap_or_else(|p| p.into_inner());
if map.len() >= self.cap {
map.clear();
}
map.insert(key.to_string(), built.clone());
Some(built)
}
}
impl<V: Clone> Default for DocMemo<V> {
fn default() -> Self {
Self::new(16_384)
}
}
pub fn ok_bytes(content_type: &str, body: crate::Bytes) -> crate::HttpBytesReply {
(
200,
vec![("Content-Type".into(), content_type.into())],
body,
)
}
pub fn not_found_bytes(reason: &str) -> crate::HttpBytesReply {
(
404,
Vec::new(),
crate::Bytes::copy_from_slice(reason.as_bytes()),
)
}
pub fn sha256_hex(data: &[u8]) -> String {
nornir_hash::sha256_hex(data)
}
#[macro_export]
macro_rules! znippy_read_only {
($base:ident $(, $key:ident = $val:ident)* $(,)?) => {
fn name(&self) -> &str {
self.$base.name()
}
fn is_writable(&self) -> bool {
false
}
fn put(&self, _id: &$crate::ArtifactId, _data: &[u8]) -> $crate::anyhow::Result<()> {
self.$base.refuse_put()
}
fn caps(&self) -> $crate::Caps {
$crate::Caps::znippy(self.$base.has_archive())
}
fn archive(&self) -> $crate::anyhow::Result<$crate::Archive> {
self.$base.archive()
}
$crate::znippy_parts!($($key = $val),*);
};
}
#[macro_export]
macro_rules! znippy_parts {
($($key:ident = $val:ident),* $(,)?) => {
$($crate::znippy_parts!(@ $key $val);)*
};
(@ format $f:ident) => {
fn format(&self) -> $crate::ArtifactFormat {
$crate::ArtifactFormat::$f
}
};
(@ get $get:ident) => {
fn handle_http2_request(
&self,
method: &str,
suburl: &str,
_body: &[u8],
) -> $crate::anyhow::Result<$crate::HttpReply> {
if method == "HEAD" {
return self
.handle_http2_request("GET", suburl, &[])
.map($crate::backend::head_of);
}
self.$get(suburl)
}
};
(@ read_via $via:ident) => {
fn read_http2_bytes(
&self,
head: bool,
suburl: &str,
) -> $crate::anyhow::Result<$crate::HttpBytesReply> {
self.$via(if head { "HEAD" } else { "GET" }, suburl, &[])
}
};
(@ get_bytes $get:ident) => {
fn handle_http2_request(
&self,
method: &str,
suburl: &str,
body: &[u8],
) -> $crate::anyhow::Result<$crate::HttpReply> {
self.handle_http2_request_bytes(method, suburl, body)
.map(|(s, h, b)| (s, h, b.to_vec()))
}
$crate::znippy_parts!(@ read_via handle_http2_request_bytes);
fn handle_http2_request_bytes(
&self,
method: &str,
suburl: &str,
_body: &[u8],
) -> $crate::anyhow::Result<$crate::HttpBytesReply> {
if method == "HEAD" {
return self
.handle_http2_request_bytes("GET", suburl, &[])
.map($crate::backend::head_of_bytes);
}
self.$get(suburl)
}
};
}
#[macro_export]
macro_rules! znippy_ctors {
($base:ident) => {
$crate::znippy_ctors!(@ctors $base);
#[must_use]
pub fn list_files(&self) -> Vec<String> {
self.$base.list_files()
}
};
($base:ident, $paths:ident) => {
$crate::znippy_ctors!(@ctors $base);
fn $paths(&self) -> &[String] {
self.$paths.get_or_init(|| self.$base.list_files())
}
#[must_use]
pub fn list_files(&self) -> Vec<String> {
self.$paths().to_vec()
}
};
(@ctors $base:ident) => {
#[must_use]
pub fn new(name: String) -> Self {
Self::from_base($crate::ZnippyBase::new(name))
}
pub fn with_archive(
name: String,
archive_path: ::std::path::PathBuf,
) -> $crate::anyhow::Result<Self> {
Ok(Self::from_base($crate::ZnippyBase::with_archive(
name,
archive_path,
)?))
}
#[must_use]
pub fn with_reader(
name: String,
reader: ::std::sync::Arc<dyn $crate::znippy_common::ZnippyReader>,
) -> Self {
Self::from_base($crate::ZnippyBase::with_reader(name, reader))
}
pub fn get_file(&self, relative_path: &str) -> $crate::anyhow::Result<Vec<u8>> {
self.$base.get_file(relative_path)
}
};
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_repo_prefix_is_stripped_once() {
assert_eq!(strip_repo("pkgs", "/pkgs/a/b"), "a/b");
assert_eq!(strip_repo("pkgs", "pkgs/a"), "a");
assert_eq!(strip_repo("pkgs", "/other/a"), "other/a");
}
#[test]
fn no_archive_is_unsupported_not_empty() {
let b = ZnippyBase::new("r");
assert!(!b.has_archive());
assert!(crate::is_unsupported(&b.archive().unwrap_err()));
assert!(b.refuse_put().is_err());
assert!(b.list_entries(None, 0, "x", |_| None).unwrap().is_empty());
}
#[test]
fn sha256_hex_is_the_known_vector() {
assert_eq!(
sha256_hex(b"abc"),
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
);
}
}