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