isr_dl_linux/ubuntu/
repository.rs

1use std::io::Read as _;
2
3use flate2::read::GzDecoder;
4use url::Url;
5
6pub use super::error::Error;
7
8#[derive(Debug, Default)]
9pub struct UbuntuRepositoryEntry {
10    pub package: Option<String>,
11    pub version: Option<String>,
12    pub filename: Option<String>,
13
14    pub size: Option<usize>,
15    pub installed_size: Option<usize>,
16
17    pub depends: Option<String>,
18    pub section: Option<String>,
19    pub source: Option<String>,
20
21    pub md5sum: Option<String>,
22    pub sha1: Option<String>,
23    pub sha256: Option<String>,
24    pub sha512: Option<String>,
25}
26
27pub fn fetch(host: Url, arch: &str, dist: &str) -> Result<Vec<UbuntuRepositoryEntry>, Error> {
28    let mut result = Vec::new();
29    let full_url = host.join(&format!("dists/{dist}/main/binary-{arch}/Packages.gz"))?;
30
31    tracing::info!(url = %full_url, "requesting");
32    let response = reqwest::blocking::get(full_url)?.error_for_status()?;
33
34    let data = response.bytes()?;
35    let mut decoder = GzDecoder::new(&data[..]);
36    let mut text = String::new();
37    decoder.read_to_string(&mut text)?;
38
39    let mut entry = UbuntuRepositoryEntry::default();
40    for line in text.lines() {
41        if line.is_empty() {
42            result.push(entry);
43            entry = UbuntuRepositoryEntry::default();
44            continue;
45        }
46
47        if line.starts_with(' ') {
48            continue;
49        }
50
51        let (key, value) = match line.split_once(": ") {
52            Some((key, value)) => (key, value),
53            None => continue,
54        };
55
56        match key {
57            "Package" => entry.package = Some(value.into()),
58            "Version" => entry.version = Some(value.into()),
59            "Filename" => entry.filename = Some(value.into()),
60            "Size" => entry.size = value.parse().ok(),
61            "Installed-Size" => entry.installed_size = value.parse().ok(),
62            "Depends" => entry.depends = Some(value.into()),
63            "Section" => entry.section = Some(value.into()),
64            "Source" => entry.source = Some(value.into()),
65            "MD5sum" => entry.md5sum = Some(value.into()),
66            "SHA1" => entry.sha1 = Some(value.into()),
67            "SHA256" => entry.sha256 = Some(value.into()),
68            "SHA512" => entry.sha512 = Some(value.into()),
69            _ => (),
70        }
71    }
72
73    Ok(result)
74}