1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use crate::prelude::*;
use anyhow::{Context, Result};
use bincode::{deserialize, serialized_size};

use crate::{error::ValidationErrorKind, Error};

pub(crate) const BLOB_VERSION: u32 = 1;
pub(crate) const BLOB_MAGIC_BYTE: u64 = 0xdeaf_abcd;

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Header {
    pub(crate) magic_byte: u64,
    pub(crate) version: u32,
    pub(crate) flags: u64,
}

impl Header {
    pub(crate) const fn new() -> Self {
        Self {
            magic_byte: BLOB_MAGIC_BYTE,
            version: BLOB_VERSION,
            flags: 0,
        }
    }

    pub(crate) async fn from_file(file: &File, file_name: &Path) -> Result<Self> {
        let size = serialized_size(&Header::new()).expect("failed to serialize default header");
        let buf = file
            .read_exact_at_allocate(size as usize, 0)
            .await
            .map_err(|err| err.into_bincode_if_unexpected_eof())
            .with_context(|| format!("failed to read from file: {:?}", file_name))?;
        let header: Self = deserialize(&buf)
            .map_err(|err| Error::from(err))
            .with_context(|| format!("failed to deserialize header from file: {:?}", file_name))?;
        header.validate().context("header validation failed")?;
        Ok(header)
    }

    fn validate(&self) -> Result<()> {
        self.validate_without_version()?;
        if self.version != BLOB_VERSION {
            let cause = format!(
                "old blob version: {}, expected: {}",
                self.version, BLOB_VERSION
            );
            return Err(Error::validation(ValidationErrorKind::BlobVersion, cause).into());
        }

        Ok(())
    }

    pub fn validate_without_version(&self) -> Result<()> {
        if self.magic_byte != BLOB_MAGIC_BYTE {
            let param = ValidationErrorKind::BlobMagicByte;
            return Err(Error::validation(param, "blob header magic byte mismatch").into());
        }
        Ok(())
    }

    #[inline]
    pub(crate) fn serialized_size(&self) -> u64 {
        serialized_size(&self).expect("blob header size")
    }
}