Skip to main content

aft_store/segment/
error.rs

1//! Errors from writing and reading segments.
2
3use std::fmt;
4use std::io;
5use std::path::{Path, PathBuf};
6
7use super::format::SectionId;
8use crate::ids::FamilyId;
9
10/// A failure to write or read one segment file, naming the file.
11#[derive(Debug)]
12pub struct SegmentError {
13    path: PathBuf,
14    kind: ErrorKind,
15}
16
17impl SegmentError {
18    pub(crate) fn new(path: &Path, kind: ErrorKind) -> Self {
19        Self {
20            path: path.to_path_buf(),
21            kind,
22        }
23    }
24
25    #[must_use]
26    pub fn path(&self) -> &Path {
27        &self.path
28    }
29
30    #[must_use]
31    pub fn kind(&self) -> &ErrorKind {
32        &self.kind
33    }
34
35    /// True when the file's bytes failed a check: it was torn, truncated or
36    /// changed after it was written. Callers count these and rebuild the
37    /// affected data.
38    #[must_use]
39    pub fn is_corruption(&self) -> bool {
40        matches!(self.kind, ErrorKind::Corrupt(_) | ErrorKind::Malformed(_))
41    }
42}
43
44impl fmt::Display for SegmentError {
45    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46        write!(f, "{}: {}", self.path.display(), self.kind)
47    }
48}
49
50impl std::error::Error for SegmentError {}
51
52/// What went wrong with a segment.
53#[derive(Debug, thiserror::Error)]
54pub enum ErrorKind {
55    #[error("{0}")]
56    Io(#[from] io::Error),
57    #[error("not a segment file")]
58    NotASegment,
59    #[error(
60        "segment format version {found} is not supported; this build reads version {supported}"
61    )]
62    UnsupportedVersion { found: u16, supported: u16 },
63    #[error("segment belongs to family {found}, expected {expected}")]
64    FamilyMismatch { found: FamilyId, expected: FamilyId },
65    #[error("{0} failed its checksum")]
66    Corrupt(Region),
67    #[error("malformed segment: {0}")]
68    Malformed(&'static str),
69    #[error("required section {id} version {version} is not understood by this build")]
70    UnknownRequiredSection { id: SectionId, version: u16 },
71    #[error("section {id} has no bytes at {end} (its length is {len})")]
72    OutOfRange { id: SectionId, end: u64, len: u64 },
73    #[error("segment would exceed {limit} bytes")]
74    TooLarge { limit: u64 },
75    #[error("section {0} was written twice")]
76    DuplicateSection(SectionId),
77    #[error("section {0} was left unfinished")]
78    UnfinishedSection(SectionId),
79}
80
81/// The part of a segment whose checksum failed.
82#[derive(Clone, Copy, PartialEq, Eq, Debug)]
83pub enum Region {
84    Header,
85    SectionTable,
86    ChecksumTable,
87    /// One 64 KiB block of a section.
88    Block {
89        section: SectionId,
90        block: u64,
91    },
92    /// A section's bytes as a whole.
93    Section(SectionId),
94}
95
96impl fmt::Display for Region {
97    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
98        match self {
99            Self::Header => f.write_str("the header"),
100            Self::SectionTable => f.write_str("the section table"),
101            Self::ChecksumTable => f.write_str("the checksum table"),
102            Self::Block { section, block } => write!(f, "block {block} of section {section}"),
103            Self::Section(section) => write!(f, "section {section}"),
104        }
105    }
106}