1use std::{ffi::OsString, path::PathBuf};
2
3use uv_macros::DebugNoInline;
4
5use crate::validate_archive_member_name;
6
7#[derive(DebugNoInline, thiserror::Error)]
8pub enum Error {
9 #[error("I/O operation failed during extraction")]
10 Io(#[source] std::io::Error),
11 #[error("Invalid zip file structure")]
12 AsyncZip(#[source] async_zip::error::ZipError),
13 #[error("Invalid tar file")]
14 Tar(
15 #[source]
16 #[from]
17 tokio_tar::TarError,
18 ),
19 #[error("Invalid tar file")]
20 TarCodec(
21 #[source]
22 #[from]
23 tar_codec::ExtractError<tar_codec::DecodeError>,
24 ),
25 #[error(transparent)]
26 DirectoryHash(#[from] crate::dirhash::DirhashError),
27 #[error(
28 "The top-level of the archive must only contain a list directory, but it contains: {0:?}"
29 )]
30 NonSingularArchive(Vec<OsString>),
31 #[error("The top-level of the archive must only contain a list directory, but it's empty")]
32 EmptyArchive,
33 #[error("ZIP local header filename at offset {offset} does not use UTF-8 encoding")]
34 LocalHeaderNotUtf8 { offset: u64 },
35 #[error("ZIP central directory entry filename at index {index} does not use UTF-8 encoding")]
36 CentralDirectoryEntryNotUtf8 { index: u64 },
37 #[error("Bad CRC (got {computed:08x}, expected {expected:08x}) for file: {}", path.display())]
38 BadCrc32 {
39 path: PathBuf,
40 computed: u32,
41 expected: u32,
42 },
43 #[error("Bad uncompressed size (got {computed:08x}, expected {expected:08x}) for file: {}", path.display())]
44 BadUncompressedSize {
45 path: PathBuf,
46 computed: u64,
47 expected: u64,
48 },
49 #[error("Bad compressed size (got {computed:08x}, expected {expected:08x}) for file: {}", path.display())]
50 BadCompressedSize {
51 path: PathBuf,
52 computed: u64,
53 expected: u64,
54 },
55 #[error("ZIP file contains multiple entries with different contents for: {}", path.display())]
56 DuplicateLocalFileHeader { path: PathBuf },
57 #[error("ZIP file contains multiple entries for the same output path: {}", path.display())]
58 DuplicateOutputPath { path: PathBuf },
59 #[error("ZIP file contains a local file header without a corresponding central-directory record entry for: {} ({offset})", path.display())]
60 MissingCentralDirectoryEntry { path: PathBuf, offset: u64 },
61 #[error("ZIP file contains an end-of-central-directory record entry, but no local file header for: {} ({offset}", path.display())]
62 MissingLocalFileHeader { path: PathBuf, offset: u64 },
63 #[error("ZIP file uses conflicting paths for the local file header at {} (got `{}`, expected `{}`)", offset, local_path.display(), central_directory_path.display())]
64 ConflictingPaths {
65 offset: u64,
66 local_path: PathBuf,
67 central_directory_path: PathBuf,
68 },
69 #[error("ZIP file uses conflicting entry types for the local file header and central-directory record for: {} ({offset})", path.display())]
70 ConflictingEntryTypes { path: PathBuf, offset: u64 },
71 #[error("ZIP file uses conflicting compression methods for {} at offset {offset} ({local_method} in the local file header, {central_directory_method} in the central directory)", path.display())]
72 ConflictingCompressionMethods {
73 path: PathBuf,
74 offset: u64,
75 local_method: u16,
76 central_directory_method: u16,
77 },
78 #[error("ZIP file uses conflicting checksums for the local file header and central-directory record (got {local_crc32}, expected {central_directory_crc32}) for: {} ({offset})", path.display())]
79 ConflictingChecksums {
80 path: PathBuf,
81 offset: u64,
82 local_crc32: u32,
83 central_directory_crc32: u32,
84 },
85 #[error("ZIP file uses conflicting compressed sizes (got {local_compressed_size}, expected {central_directory_compressed_size} from the central-directory record) for: {} ({offset})", path.display())]
86 ConflictingCompressedSizes {
87 path: PathBuf,
88 offset: u64,
89 local_compressed_size: u64,
90 central_directory_compressed_size: u64,
91 },
92 #[error("ZIP file uses conflicting uncompressed sizes for the local file header and central-directory record (got {local_uncompressed_size}, expected {central_directory_uncompressed_size}) for: {} ({offset})", path.display())]
93 ConflictingUncompressedSizes {
94 path: PathBuf,
95 offset: u64,
96 local_uncompressed_size: u64,
97 central_directory_uncompressed_size: u64,
98 },
99 #[error("ZIP file contains trailing contents after the end-of-central-directory record")]
100 TrailingContents,
101 #[error(
102 "ZIP file reports a number of entries in the central directory that conflicts with the actual number of entries (got {actual}, expected {expected})"
103 )]
104 ConflictingNumberOfEntries { actual: u64, expected: u64 },
105 #[error("Data descriptor is missing for file: {}", path.display())]
106 MissingDataDescriptor { path: PathBuf },
107 #[error("File contains an unexpected data descriptor: {}", path.display())]
108 UnexpectedDataDescriptor { path: PathBuf },
109 #[error("ZIP file contains a comment that appears to be an embedded ZIP file")]
110 ZipInZip,
111 #[error("ZIP64 end-of-central-directory record contains unsupported extensible data")]
112 ExtensibleData,
113 #[error("ZIP file end-of-central-directory record contains multiple entries with the same path, but conflicting modes: {}", path.display())]
114 DuplicateExecutableFileHeader { path: PathBuf },
115 #[error("Archive contains a file with an empty filename")]
116 EmptyFilename,
117 #[error("Archive contains unacceptable filename: {filename}")]
118 UnacceptableFilename { filename: String },
119 #[error(
120 "Archive contains a file with an unsupported compression method; files must be compressed with 'stored', 'DEFLATE', or 'zstd'"
121 )]
122 UnsupportedCompression,
123}
124
125impl From<async_zip::error::ZipError> for Error {
126 fn from(err: async_zip::error::ZipError) -> Self {
127 match err {
128 async_zip::error::ZipError::CompressionNotSupported(_) => Self::UnsupportedCompression,
129 async_zip::error::ZipError::TrailingContents => Self::TrailingContents,
130 async_zip::error::ZipError::ZipInZip => Self::ZipInZip,
131 async_zip::error::ZipError::FileNameContainsNul { filename } => {
132 let filename = String::from_utf8_lossy(&filename);
133 validate_archive_member_name(&filename)
134 .expect_err("a filename containing an embedded NUL must be rejected")
135 }
136 error => Self::AsyncZip(error),
137 }
138 }
139}
140
141impl Error {
142 pub(crate) fn io_or_zip(err: std::io::Error) -> Self {
146 if err.kind() != std::io::ErrorKind::Other {
147 return Self::Io(err);
148 }
149
150 let err = match err.downcast::<async_zip::error::ZipError>() {
151 Ok(zip_err) => return Self::from(zip_err),
152 Err(err) => err,
153 };
154 Self::Io(err)
155 }
156
157 pub(crate) fn io_or_tar(err: std::io::Error) -> Self {
161 if err.kind() != std::io::ErrorKind::Other {
162 return Self::Io(err);
163 }
164
165 match err.downcast::<tokio_tar::TarError>() {
166 Ok(tar_err) => Self::Tar(tar_err),
167 Err(err) => Self::Io(err),
168 }
169 }
170
171 pub fn is_http_streaming_unsupported(&self) -> bool {
175 matches!(
176 self,
177 Self::AsyncZip(async_zip::error::ZipError::FeatureNotSupported(_))
178 )
179 }
180
181 pub fn is_http_streaming_failed(&self) -> bool {
183 fn contains_reqwest_error(error: &(dyn std::error::Error + 'static)) -> bool {
184 if error.downcast_ref::<reqwest::Error>().is_some() {
185 return true;
186 }
187 if let Some(error) = error.downcast_ref::<std::io::Error>()
190 && let Some(inner) = error.get_ref()
191 && contains_reqwest_error(inner)
192 {
193 return true;
194 }
195 error.source().is_some_and(contains_reqwest_error)
196 }
197
198 if matches!(
199 self,
200 Self::AsyncZip(async_zip::error::ZipError::UpstreamReadError(_))
201 ) {
202 return true;
203 }
204 contains_reqwest_error(self)
205 }
206}