-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathreader.rs
315 lines (270 loc) · 10.3 KB
/
reader.rs
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
/*! .deb file reading functionality. */
use {
crate::{
binary_package_control::BinaryPackageControlFile,
control::ControlParagraphReader,
error::{DebianError, Result},
},
std::{
io::{Cursor, Read},
ops::{Deref, DerefMut},
},
};
fn reader_from_filename(
extension: &str,
data: std::io::Cursor<Vec<u8>>,
) -> Result<Box<dyn Read + Send>> {
match extension {
"" => Ok(Box::new(data)),
".gz" => Ok(Box::new(libflate::gzip::Decoder::new(data)?)),
".xz" => Ok(Box::new(xz2::read::XzDecoder::new(data))),
".zst" => Ok(Box::new(zstd::Decoder::new(data)?)),
_ => Err(DebianError::DebUnknownCompression(extension.to_string())),
}
}
fn reader_from_filename_async(
extension: &str,
data: futures::io::Cursor<Vec<u8>>,
) -> Result<Box<dyn futures::AsyncRead + Unpin + Send>> {
match extension {
"" => Ok(Box::new(data)),
".gz" => Ok(Box::new(
async_compression::futures::bufread::GzipDecoder::new(data),
)),
".xz" => Ok(Box::new(
async_compression::futures::bufread::XzDecoder::new(data),
)),
".zst" => Ok(Box::new(
async_compression::futures::bufread::ZstdDecoder::new(data),
)),
_ => Err(DebianError::DebUnknownCompression(extension.to_string())),
}
}
/// A reader of .deb files.
///
/// A .deb binary package file is an ar archive with 3 entries:
///
/// 1. `debian-binary` holding the version of the binary package format.
/// 2. `control.tar` holding package metadata.
/// 3. `data.tar[.<ext>]` holding file content.
pub struct BinaryPackageReader<R: Read + Send> {
archive: ar::Archive<R>,
}
impl<R: Read + Send> BinaryPackageReader<R> {
/// Construct a new instance from a reader.
pub fn new(reader: R) -> Result<Self> {
Ok(Self {
archive: ar::Archive::new(reader),
})
}
/// Obtain the next entry from the underlying ar archive.
///
/// The entry will be converted to an enum that richly represents its content.
pub fn next_entry(&mut self) -> Option<Result<BinaryPackageEntry>> {
if let Some(entry) = self.archive.next_entry() {
match entry {
Ok(mut entry) => {
// We could do this in the domain of bytes. But filenames should be ASCII,
// so converting to strings feels reasonably safe.
let filename = String::from_utf8_lossy(entry.header().identifier()).to_string();
let mut data = vec![];
match entry.read_to_end(&mut data) {
Ok(_) => {}
Err(e) => {
return Some(Err(e.into()));
}
}
if filename == "debian-binary" {
Some(Ok(BinaryPackageEntry::DebianBinary(std::io::Cursor::new(
data,
))))
} else if let Some(tail) = filename.strip_prefix("control.tar") {
match reader_from_filename(tail, std::io::Cursor::new(data)) {
Ok(res) => Some(Ok(BinaryPackageEntry::Control(ControlTarReader {
archive: tar::Archive::new(res),
}))),
Err(e) => Some(Err(e)),
}
} else if let Some(tail) = filename.strip_prefix("data.tar") {
match reader_from_filename_async(tail, futures::io::Cursor::new(data)) {
Ok(res) => Some(Ok(BinaryPackageEntry::Data(DataTarReader {
archive: async_tar::Archive::new(res),
}))),
Err(e) => Some(Err(e)),
}
} else {
Some(Err(DebianError::DebUnknownBinaryPackageEntry(
filename.to_string(),
)))
}
}
Err(e) => Some(Err(e.into())),
}
} else {
None
}
}
}
/// Represents an entry in a .deb archive.
pub enum BinaryPackageEntry {
/// The `debian-binary` file.
DebianBinary(std::io::Cursor<Vec<u8>>),
/// The `control.tar` tar archive.
Control(ControlTarReader),
/// The `data.tar[.<ext>]` tar archive.
Data(DataTarReader),
}
/// A reader for `control.tar` files.
pub struct ControlTarReader {
archive: tar::Archive<Box<dyn Read + Send>>,
}
impl Deref for ControlTarReader {
type Target = tar::Archive<Box<dyn Read + Send>>;
fn deref(&self) -> &Self::Target {
&self.archive
}
}
impl DerefMut for ControlTarReader {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.archive
}
}
impl ControlTarReader {
/// Obtain the entries in the `control.tar` file.
///
/// This can only be called once, immediately after the reader/archive is opened.
/// It is a glorified wrapper around [tar::Archive::entries()] and has the same
/// semantics.
pub fn entries(&mut self) -> Result<ControlTarEntries<'_>> {
let entries = self.archive.entries()?;
Ok(ControlTarEntries { entries })
}
}
/// Represents entries in a `control.tar` file.
///
/// Ideally this type wouldn't exist. It is a glorified wrapper around
/// [tar::Entries] that is needed to placate the borrow checker.
pub struct ControlTarEntries<'a> {
entries: tar::Entries<'a, Box<dyn Read + Send>>,
}
impl<'a> Iterator for ControlTarEntries<'a> {
type Item = Result<ControlTarEntry<'a>>;
fn next(&mut self) -> Option<Self::Item> {
match self.entries.next() {
Some(Ok(entry)) => Some(Ok(ControlTarEntry { inner: entry })),
Some(Err(e)) => Some(Err(e.into())),
None => None,
}
}
}
/// A wrapper around [tar::Entry] for representing content in `control.tar` files.
///
/// Facilitates access to the raw [tar::Entry] as well as for obtaining a higher
/// level type that decodes known files within `control.tar` files.
pub struct ControlTarEntry<'a> {
inner: tar::Entry<'a, Box<dyn Read + Send>>,
}
impl<'a> Deref for ControlTarEntry<'a> {
type Target = tar::Entry<'a, Box<dyn Read + Send>>;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl<'a> DerefMut for ControlTarEntry<'a> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}
impl<'a> ControlTarEntry<'a> {
/// Attempt to convert this tar entry to a [ControlTarFile].
///
///
pub fn to_control_file(&mut self) -> Result<(&'_ tar::Header, ControlTarFile)> {
let path_bytes = self.inner.path_bytes().to_vec();
let path = String::from_utf8_lossy(&path_bytes);
let mut data = vec![];
self.inner.read_to_end(&mut data)?;
match path.trim_start_matches("./") {
"control" => {
let mut reader = ControlParagraphReader::new(Cursor::new(data));
let paragraph = reader.next().ok_or(DebianError::ControlFileNoParagraph)??;
let control = BinaryPackageControlFile::from(paragraph);
Ok((self.inner.header(), ControlTarFile::Control(control)))
}
"conffiles" => Ok((self.inner.header(), ControlTarFile::Conffiles(data))),
"triggers" => Ok((self.inner.header(), ControlTarFile::Triggers(data))),
"shlibs" => Ok((self.inner.header(), ControlTarFile::Shlibs(data))),
"symbols" => Ok((self.inner.header(), ControlTarFile::Symbols(data))),
"preinst" => Ok((self.inner.header(), ControlTarFile::Preinst(data))),
"postinst" => Ok((self.inner.header(), ControlTarFile::Postinst(data))),
"prerm" => Ok((self.inner.header(), ControlTarFile::Prerm(data))),
"postrm" => Ok((self.inner.header(), ControlTarFile::Postrm(data))),
_ => Ok((self.inner.header(), ControlTarFile::Other(path_bytes, data))),
}
}
}
/// Represents a parsed file in a `control.tar` archive.
///
/// Each variant encodes a known file in a `control.tar` archive.
pub enum ControlTarFile {
/// The `control` file.
Control(BinaryPackageControlFile<'static>),
/// The `conffiles` file.
Conffiles(Vec<u8>),
/// The `triggers` file.
Triggers(Vec<u8>),
/// The `shlibs` file.
Shlibs(Vec<u8>),
/// The `symbols` file.
Symbols(Vec<u8>),
/// The `preinst` file.
Preinst(Vec<u8>),
/// The `postinst` file.
Postinst(Vec<u8>),
/// The `prerm` file.
Prerm(Vec<u8>),
/// The `postrm` file.
Postrm(Vec<u8>),
/// An unclassified file.
///
/// First element is the path name as bytes. Second is the raw file content.
Other(Vec<u8>, Vec<u8>),
}
/// A reader for `data.tar` files.
pub struct DataTarReader {
archive: async_tar::Archive<Box<dyn futures::io::AsyncRead + Unpin + Send>>,
}
impl Deref for DataTarReader {
type Target = async_tar::Archive<Box<dyn futures::io::AsyncRead + Unpin + Send>>;
fn deref(&self) -> &Self::Target {
&self.archive
}
}
impl DerefMut for DataTarReader {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.archive
}
}
impl DataTarReader {
/// Obtain the inner [async_tar::Archive] to which this instance is bound.
pub fn into_inner(self) -> async_tar::Archive<Box<dyn futures::io::AsyncRead + Unpin + Send>> {
self.archive
}
}
/// Resolve the `control` file from the `control.tar` file within a `.deb` archive.
pub fn resolve_control_file(reader: impl Read + Send) -> Result<BinaryPackageControlFile<'static>> {
let mut reader = BinaryPackageReader::new(reader)?;
while let Some(entry) = reader.next_entry() {
if let BinaryPackageEntry::Control(mut control) = entry? {
for entry in control.entries()? {
if let ControlTarFile::Control(control) = entry?.to_control_file()?.1 {
return Ok(control);
}
}
}
}
Err(DebianError::ControlFileNotFound)
}