use std::io;
use std::path::Path;
use crate::ids::{Digest128, FamilyId};
use crate::le::{array, u16_at, u32_at, u64_at};
const MAGIC: [u8; 8] = *b"AFTREC\0\0";
const HEADER_LEN: usize = 48;
const MAX_PAYLOAD: u64 = 256 << 20;
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub(crate) enum Kind {
Manifest,
Horizon,
State,
Lease,
}
impl Kind {
const fn code(self) -> u16 {
match self {
Self::Manifest => 1,
Self::Horizon => 2,
Self::State => 3,
Self::Lease => 4,
}
}
const fn version(self) -> u16 {
1
}
}
#[derive(Debug, thiserror::Error)]
pub(crate) enum RecordError {
#[error("{0}")]
Io(#[from] io::Error),
#[error("the record's bytes do not check ({0})")]
Invalid(&'static str),
#[error("record format version {found} is not supported; this build reads version {supported}")]
UnsupportedVersion { found: u16, supported: u16 },
#[error("payload format {found} is not supported; this build reads up to format {supported}")]
NewerPayload { found: u16, supported: u16 },
#[error("record belongs to family {found}, expected {expected}")]
FamilyMismatch { found: FamilyId, expected: FamilyId },
}
pub(crate) fn encode(kind: Kind, family: FamilyId, number: u64, payload: &[u8]) -> Vec<u8> {
let mut bytes = Vec::with_capacity(HEADER_LEN + payload.len() + Digest128::LEN);
bytes.extend_from_slice(&MAGIC);
bytes.extend_from_slice(&kind.code().to_le_bytes());
bytes.extend_from_slice(&kind.version().to_le_bytes());
bytes.extend_from_slice(&0u32.to_le_bytes());
bytes.extend_from_slice(family.digest().as_bytes());
bytes.extend_from_slice(&number.to_le_bytes());
bytes.extend_from_slice(&(payload.len() as u64).to_le_bytes());
bytes.extend_from_slice(payload);
let checksum = Digest128::of(&bytes);
bytes.extend_from_slice(checksum.as_bytes());
bytes
}
pub(crate) fn decode(
bytes: &[u8],
kind: Kind,
family: FamilyId,
number: u64,
) -> Result<&[u8], RecordError> {
if bytes.len() < HEADER_LEN + Digest128::LEN {
return Err(RecordError::Invalid("shorter than a record"));
}
if array::<8>(bytes, 0) != MAGIC {
return Err(RecordError::Invalid("no record magic"));
}
let payload_len = u64_at(bytes, 40);
if payload_len > MAX_PAYLOAD
|| bytes.len() as u64 != HEADER_LEN as u64 + payload_len + Digest128::LEN as u64
{
return Err(RecordError::Invalid("length does not match the file"));
}
let body_len = bytes.len() - Digest128::LEN;
let stored = Digest128::from_bytes(array(bytes, body_len));
if Digest128::of(&bytes[..body_len]) != stored {
return Err(RecordError::Invalid("checksum"));
}
if u16_at(bytes, 8) != kind.code() || u32_at(bytes, 12) != 0 || u64_at(bytes, 32) != number {
return Err(RecordError::Invalid(
"kind, flags or number differ from the name",
));
}
let version = u16_at(bytes, 10);
if version != kind.version() {
return Err(RecordError::UnsupportedVersion {
found: version,
supported: kind.version(),
});
}
let found = FamilyId::from_digest(Digest128::from_bytes(array(bytes, 16)));
if found != family {
return Err(RecordError::FamilyMismatch {
found,
expected: family,
});
}
Ok(&bytes[HEADER_LEN..body_len])
}
pub(crate) fn write(
path: &Path,
kind: Kind,
family: FamilyId,
number: u64,
payload: &[u8],
) -> io::Result<()> {
crate::fs::write_new_durable(path, &encode(kind, family, number, payload))
}
pub(crate) fn read(
path: &Path,
kind: Kind,
family: FamilyId,
number: u64,
) -> Result<Vec<u8>, RecordError> {
let limit = HEADER_LEN as u64 + MAX_PAYLOAD + Digest128::LEN as u64;
let bytes = crate::fs::read_at_most(path, limit)
.map_err(RecordError::Io)?
.ok_or(RecordError::Invalid("larger than any record"))?;
decode(&bytes, kind, family, number).map(<[u8]>::to_vec)
}
pub(crate) fn newer_version(
path: &Path,
kind: Kind,
family: FamilyId,
number: u64,
) -> Option<RecordError> {
let file = crate::fs::open_read(path).ok()?;
let mut header = [0; 12];
crate::fs::read_exact_at(&file, &mut header, 0).ok()?;
drop(file);
if array::<8>(&header, 0) != MAGIC
|| u16_at(&header, 8) != kind.code()
|| u16_at(&header, 10) <= kind.version()
{
return None;
}
match read(path, kind, family, number) {
Err(error @ RecordError::UnsupportedVersion { found, supported }) if found > supported => {
Some(error)
}
_ => None,
}
}
pub(crate) fn head(path: &Path, limit: u64) -> Option<Vec<u8>> {
use std::io::Read;
let mut bytes = Vec::new();
crate::fs::open_read(path)
.ok()?
.take(limit)
.read_to_end(&mut bytes)
.ok()?;
Some(bytes)
}
pub(crate) fn head_payload(head: &[u8]) -> &[u8] {
head.get(HEADER_LEN..).unwrap_or_default()
}
#[derive(Debug)]
pub(crate) struct Payload<'a> {
bytes: &'a [u8],
}
impl<'a> Payload<'a> {
pub(crate) fn new(bytes: &'a [u8]) -> Self {
Self { bytes }
}
pub(crate) fn take(&mut self, len: usize) -> Result<&'a [u8], RecordError> {
if self.bytes.len() < len {
return Err(RecordError::Invalid("payload ends early"));
}
let (head, rest) = self.bytes.split_at(len);
self.bytes = rest;
Ok(head)
}
pub(crate) fn u32(&mut self) -> Result<u32, RecordError> {
Ok(u32_at(self.take(4)?, 0))
}
pub(crate) fn u64(&mut self) -> Result<u64, RecordError> {
Ok(u64_at(self.take(8)?, 0))
}
pub(crate) fn digest(&mut self) -> Result<Digest128, RecordError> {
Ok(Digest128::from_bytes(array(self.take(Digest128::LEN)?, 0)))
}
pub(crate) fn finish(self) -> Result<(), RecordError> {
if self.bytes.is_empty() {
Ok(())
} else {
Err(RecordError::Invalid("payload has trailing bytes"))
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_rng::TestRng;
fn family(byte: u8) -> FamilyId {
FamilyId::from_digest(Digest128::from_bytes([byte; 16]))
}
#[test]
fn round_trips_and_checks_name_kind_and_family() {
let bytes = encode(Kind::Manifest, family(1), 7, b"payload");
assert_eq!(
decode(&bytes, Kind::Manifest, family(1), 7).unwrap(),
b"payload"
);
assert!(matches!(
decode(&bytes, Kind::Horizon, family(1), 7),
Err(RecordError::Invalid(_))
));
assert!(matches!(
decode(&bytes, Kind::Manifest, family(1), 8),
Err(RecordError::Invalid(_))
));
assert!(matches!(
decode(&bytes, Kind::Manifest, family(2), 7),
Err(RecordError::FamilyMismatch { .. })
));
}
#[test]
fn every_truncation_and_bit_flip_is_refused() {
let bytes = encode(Kind::State, family(3), 2, &TestRng::new(5).bytes(300));
for len in 0..bytes.len() {
assert!(
decode(&bytes[..len], Kind::State, family(3), 2).is_err(),
"len {len}"
);
}
for index in 0..bytes.len() {
for bit in 0..8 {
let mut flipped = bytes.clone();
flipped[index] ^= 1 << bit;
assert!(
decode(&flipped, Kind::State, family(3), 2).is_err(),
"byte {index} bit {bit}"
);
}
}
}
#[test]
fn payload_cursor_refuses_short_and_trailing_bytes() {
let mut short = Payload::new(&[1, 2, 3]);
assert!(short.u32().is_err());
let mut long = Payload::new(&[0; 9]);
long.u64().unwrap();
assert!(long.finish().is_err());
}
}