Skip to main content

tar_framing/
error.rs

1use std::io;
2
3use crate::{ArchiveFormat, BLOCK_SIZE, GnuKind, UstarKind, pax::PaxError, stream::DataOwner};
4
5/// An error encountered at an absolute position in a tar stream.
6#[derive(Debug, thiserror::Error)]
7#[error("at byte {position}: {inner}")]
8pub struct FrameError {
9    /// The absolute byte position associated with the failure.
10    pub position: u64,
11    /// The specific failure encountered at `position`.
12    #[source]
13    pub inner: FrameErrorInner,
14}
15
16impl FrameError {
17    pub(crate) fn at(position: u64, inner: FrameErrorInner) -> Self {
18        Self { position, inner }
19    }
20
21    pub(crate) fn arithmetic_overflow(position: u64, context: &'static str) -> Self {
22        Self::at(position, FrameErrorInner::ArithmeticOverflow { context })
23    }
24
25    pub(crate) fn deleted_pax_record(position: u64, keyword: &'static str) -> Self {
26        Self::at(position, FrameErrorInner::DeletedPaxRecord { keyword })
27    }
28
29    pub(crate) fn invalid_gnu_extension(
30        position: u64,
31        kind: GnuKind,
32        reason: &'static str,
33    ) -> Self {
34        Self::at(
35            position,
36            FrameErrorInner::InvalidGnuExtension { kind, reason },
37        )
38    }
39
40    pub(crate) fn invalid_pax_record(position: u64, source: PaxError) -> Self {
41        Self::at(position, FrameErrorInner::InvalidPaxRecord { source })
42    }
43
44    pub(crate) fn truncated_payload(position: u64, owner: DataOwner, remaining: u64) -> Self {
45        Self::at(
46            position,
47            FrameErrorInner::TruncatedPayload { owner, remaining },
48        )
49    }
50
51    pub(crate) fn unexpected_order(
52        position: u64,
53        expected: &'static str,
54        found: &'static str,
55    ) -> Self {
56        Self::at(
57            position,
58            FrameErrorInner::UnexpectedOrder { expected, found },
59        )
60    }
61}
62
63/// Specific errors that can occur while processing tar frames.
64#[derive(Debug, thiserror::Error)]
65pub enum FrameErrorInner {
66    /// The underlying reader failed.
67    #[error("failed to read tar data")]
68    Io {
69        /// The underlying I/O failure.
70        #[source]
71        source: io::Error,
72    },
73    /// The underlying stream ended in the middle of a logical block.
74    #[error("incomplete tar block: read {read} of {BLOCK_SIZE} bytes")]
75    IncompleteBlock {
76        /// The number of bytes received for the incomplete block.
77        read: usize,
78    },
79    /// A header did not identify either supported archive family.
80    #[error("invalid tar identity: found {found:?}")]
81    InvalidIdentity {
82        /// The bytes found in the combined magic/version fields.
83        found: [u8; 8],
84    },
85    /// A header carried the xstar format signature.
86    #[error("unsupported xstar header")]
87    UnsupportedXstarHeader,
88    /// A header checksum was malformed or did not match its contents.
89    #[error("invalid tar checksum: stored {expected:?}, computed {actual}")]
90    InvalidChecksum {
91        /// The parsed stored checksum, or `None` if its field was malformed.
92        expected: Option<u64>,
93        /// The checksum computed from the header block.
94        actual: u64,
95    },
96    /// A header's size field was not a strict number.
97    /// The underlying format of the number might be octal or GNU-style base256.
98    #[error("invalid tar size field: found {found:?}")]
99    InvalidSize {
100        /// The bytes found in the size field.
101        found: [u8; 12],
102    },
103    /// An ordinary member header's numeric metadata field was invalid.
104    #[error("invalid tar {field} field: found {found:?}")]
105    InvalidNumericField {
106        /// The name of the invalid header field.
107        field: &'static str,
108        /// The bytes found in the invalid header field.
109        found: Vec<u8>,
110    },
111    /// An ordinary pax/ustar or GNU member header's string field had no NUL terminator.
112    #[error("invalid ustar {field} field: missing NUL terminator")]
113    UnterminatedUstarStringField {
114        /// The name of the unterminated header field.
115        field: &'static str,
116    },
117    /// A tar type is not supported within the selected archive family.
118    #[error("unsupported tar typeflag {typeflag:?}")]
119    UnsupportedTypeflag {
120        /// The unsupported typeflag byte.
121        typeflag: u8,
122    },
123    /// A header from another archive family appeared after family detection.
124    #[error("archive format changed from {expected:?} to {found:?}")]
125    FormatMismatch {
126        /// The archive family selected by an earlier header.
127        expected: ArchiveFormat,
128        /// The archive family identified by this header.
129        found: ArchiveFormat,
130    },
131    /// A valid block appeared in a position where another block type was required.
132    #[error("unexpected tar block: expected {expected}, found {found}")]
133    UnexpectedOrder {
134        /// A description of the required block.
135        expected: &'static str,
136        /// A description of the block received.
137        found: &'static str,
138    },
139    /// A pax extended-header record could not be parsed.
140    #[error("{source}")]
141    InvalidPaxRecord {
142        /// The pax parsing failure.
143        #[source]
144        source: PaxError,
145    },
146    /// A metadata extension declares more data than the configured format limit.
147    #[error("{format} extension payload size {size} exceeds configured limit {limit}")]
148    ExtensionTooLarge {
149        /// The archive family containing the extension.
150        format: ArchiveFormat,
151        /// The extension payload size declared in its header.
152        size: u64,
153        /// The configured maximum extension payload size.
154        limit: u64,
155    },
156    /// Global pax extensions contain more data than the configured archive limit.
157    #[error("global pax extension payload total {size} exceeds configured limit {limit}")]
158    GlobalPaxExtensionsTooLarge {
159        /// The total declared size, including the rejected extension.
160        size: u64,
161        /// The configured maximum payload size for the archive.
162        limit: u64,
163    },
164    /// A GNU long-name or long-link extension declares an empty payload or
165    /// contains an invalid value.
166    #[error("malformed GNU {kind:?} metadata payload: {reason}")]
167    InvalidGnuExtension {
168        /// The GNU metadata extension being decoded.
169        kind: GnuKind,
170        /// The reason the metadata value was rejected.
171        reason: &'static str,
172    },
173    /// A pax record removed metadata required to interpret a member.
174    #[error("pax metadata {keyword:?} deletes a required member field")]
175    DeletedPaxRecord {
176        /// The standard pax keyword that deleted its header fallback.
177        keyword: &'static str,
178    },
179    /// An ordinary member has no effective pathname after applying extensions.
180    #[error("ordinary member has an empty effective path")]
181    EmptyMemberPath,
182    /// An effective member path or link target contains an embedded NUL byte.
183    #[error("effective member {field} contains a NUL byte")]
184    NulInMemberName {
185        /// The effective metadata field containing the NUL byte.
186        field: &'static str,
187    },
188    /// A member other than a hard link or symbolic link specifies a link target.
189    #[error("member type {kind:?} cannot have a link target")]
190    UnexpectedLinkTarget {
191        /// The member type identified by the header.
192        kind: UstarKind,
193    },
194    /// A framing offset or record length overflowed.
195    #[error("arithmetic overflow while computing {context}")]
196    ArithmeticOverflow {
197        /// The computation that overflowed.
198        context: &'static str,
199    },
200    /// A member's declared or effective size is invalid for its type.
201    #[error("member type {kind:?} cannot carry payload size {size}")]
202    InvalidMemberSize {
203        /// The member type.
204        kind: UstarKind,
205        /// The rejected declared or effective payload size.
206        size: u64,
207    },
208    /// The stream ended while a payload still required bytes.
209    #[error("unexpected end of stream: {owner:?} payload needs {remaining} more bytes")]
210    TruncatedPayload {
211        /// The kind of payload being read.
212        owner: DataOwner,
213        /// The remaining unpadded payload length.
214        remaining: u64,
215    },
216    /// The stream ended while a required member header was pending.
217    #[error("unexpected end of stream: expected {expected}")]
218    UnexpectedEof {
219        /// A description of the required next input.
220        expected: &'static str,
221    },
222    /// The two-block end marker was absent or incomplete.
223    #[error("missing two-block end-of-archive marker")]
224    MissingEndMarker,
225    /// The first zero terminator block was not followed by a second zero block.
226    #[error("invalid end-of-archive marker: expected a second zero block")]
227    InvalidEndMarker,
228    /// A byte after the end marker was nonzero or exceeded the permitted record padding.
229    #[error("unexpected data after the end-of-archive marker")]
230    TrailingData,
231}