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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use core::fmt;
/// Errors that can occur during CPIO archive operations.
#[derive(Debug)]
pub enum Error {
/// An I/O error occurred while reading or writing the archive.
Io(hadris_io::Error),
/// The header magic bytes are not `070701` or `070702`.
InvalidMagic {
/// Six bytes read from the archive magic field.
found: [u8; 6],
},
/// A header field contains non-hexadecimal characters.
InvalidHexField {
/// Name of the malformed header field.
field: &'static str,
},
/// The entry filename is empty or could not be read.
InvalidFilename,
/// A decoded header violates a newc field invariant.
InvalidHeader {
/// Description of the violated invariant.
reason: &'static str,
},
/// The archive ended without a `TRAILER!!!` sentinel.
MissingTrailer,
/// The CRC checksum in a `070702` entry does not match the computed value.
ChecksumMismatch {
/// Checksum stored in the entry header.
expected: u32,
/// Checksum computed from the entry contents.
computed: u32,
},
/// A caller-provided data buffer does not match the entry's file size.
BufferSizeMismatch {
/// The entry's file size in bytes.
expected: usize,
/// The length of the provided buffer.
actual: usize,
},
/// A hard link references a target path that was not seen earlier in the archive.
#[cfg(feature = "write")]
UnresolvedHardLink {
/// Inode number assigned to the unresolved target.
ino: u32,
},
/// The filename exceeds the maximum length representable in a newc header.
#[cfg(feature = "write")]
FilenameTooLong,
/// The file data exceeds the maximum size representable in a newc header (4 GiB).
#[cfg(feature = "write")]
FileTooLarge,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Io(e) => write!(f, "I/O error: {e:?}"),
Self::InvalidMagic { found } => {
write!(
f,
"invalid CPIO magic: expected 070701 or 070702, found {:?}",
core::str::from_utf8(found).unwrap_or("<invalid>")
)
}
Self::InvalidHexField { field } => {
write!(f, "invalid hex field: {field}")
}
Self::InvalidFilename => write!(f, "invalid filename"),
Self::InvalidHeader { reason } => write!(f, "invalid CPIO header: {reason}"),
Self::MissingTrailer => write!(f, "missing TRAILER!!! sentinel"),
Self::ChecksumMismatch { expected, computed } => {
write!(
f,
"checksum mismatch: expected {expected:#010x}, computed {computed:#010x}"
)
}
Self::BufferSizeMismatch { expected, actual } => {
write!(
f,
"buffer size mismatch: entry is {expected} bytes, buffer is {actual} bytes"
)
}
#[cfg(feature = "write")]
Self::UnresolvedHardLink { ino } => {
write!(f, "unresolved hard link: inode {ino}")
}
#[cfg(feature = "write")]
Self::FilenameTooLong => write!(f, "filename too long"),
#[cfg(feature = "write")]
Self::FileTooLarge => write!(f, "file too large"),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for Error {}
impl<E: hadris_io::IoError> From<hadris_io::Error<E>> for Error {
fn from(e: hadris_io::Error<E>) -> Self {
Self::Io(e.erase())
}
}
/// Convenience type alias for `core::result::Result<T, Error>`.
pub type Result<T> = core::result::Result<T, Error>;