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
#![deny(
missing_docs,
trivial_casts,
trivial_numeric_casts,
unused_extern_crates,
unused_import_braces,
unused_qualifications,
unused_results
)]
pub enum RemotePackageType {
#[cfg(feature = "debian")]
Deb,
#[cfg(feature = "rpm")]
Rpm,
}
#[derive(thiserror::Error, Debug)]
pub enum PkgError {
#[cfg(feature = "debian")]
#[error("Debpkg Error")]
DebPkgError(#[from] debpkg::Error),
#[cfg(feature = "rpm")]
#[error("fez Error")]
RpmError(#[from] ::fez::RPMError),
#[cfg(feature = "http")]
#[error("HTTP Error")]
HTTPError(#[from] reqwest::Error),
#[error("Failed to infer package type")]
InferError,
#[cfg(feature = "debian")]
#[error("Debian field not found: {0}")]
DebianControlFieldNotFound(String),
#[error("Package type cannot be queried (inferred: {0})")]
UnknownPackageType(String),
}
pub trait RemotePackage {
fn package_type(&self) -> RemotePackageType;
fn package_name(&self) -> Result<&str, PkgError>;
fn package_version(&self) -> Result<&str, PkgError>;
fn package_iteration(&self) -> Option<&str>;
fn package_arch(&self) -> Result<&str, PkgError>;
}
#[cfg(feature = "debian")]
pub mod debian;
#[cfg(feature = "rpm")]
pub mod rpm;
#[cfg(feature = "http")]
pub fn from_url(url: &str) -> Result<Box<dyn RemotePackage>, PkgError> {
use std::io::Read;
let client = reqwest::blocking::Client::new();
let response = client.get(url).send()?;
let mut reader = response.take(1024);
let mut infer_buf = vec![];
let _ = reader
.read_to_end(&mut infer_buf)
.map_err(|_| PkgError::InferError)?;
let ext = infer::get(&infer_buf).map(|t| t.extension());
let is_deb = infer::archive::is_deb(&infer_buf);
let is_rpm = infer::archive::is_rpm(&infer_buf);
let rsp = std::io::Cursor::new(infer_buf).chain(reader.into_inner());
#[cfg(feature = "debian")]
if is_deb {
let pkg = debian::DebianRemotePackage::new_from_read(rsp)?;
return Ok(Box::new(pkg));
}
#[cfg(feature = "rpm")]
if is_rpm {
let pkg = rpm::RpmRemotePackage::new_from_read(rsp)?;
return Ok(Box::new(pkg));
}
Err(PkgError::UnknownPackageType(
ext.unwrap_or("unknown").to_owned(),
))
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(feature = "http")]
fn test_from_url(url: &str, package_name: &str) -> Result<(), Box<dyn std::error::Error>> {
let package = from_url(url).expect("Failed to download package");
println!(
"{}:{}:{}:{}",
package.package_name()?,
package.package_version()?,
package.package_arch()?,
package.package_iteration().unwrap_or("no iteration")
);
assert_eq!(package.package_name()?, package_name);
Ok(())
}
#[cfg(all(feature = "http", feature = "rpm"))]
#[test]
fn test_from_url_rpm() -> Result<(), Box<dyn std::error::Error>> {
test_from_url(
"https://artifacts.elastic.co/downloads/kibana/kibana-8.2.1-x86_64.rpm",
"kibana",
)
}
#[cfg(all(feature = "http", feature = "debian"))]
#[test]
fn test_from_url_deb() -> Result<(), Box<dyn std::error::Error>> {
test_from_url(
"http://cz.archive.ubuntu.com/ubuntu/pool/universe/d/debian-faq/debian-faq_10.1_all.deb",
"debian-faq",
)
}
}