summaryrefslogtreecommitdiffstats
path: root/src/pkginfo.rs
blob: 2cf449ed8a23a7dce5c3b125926ecd5ace32a586 (plain)
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
use std::io::Read;

#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct PkgInfo {
    pub name: String,
    pub version: String,
    pub arch: String,
    pub desc: String,
    pub url: String,
    pub license: Vec<String>,
    pub groups: Vec<String>,
    pub depends: Vec<String>,
    pub makedepends: Vec<String>,
    pub checkdepends: Vec<String>,
    pub optdepends: Vec<String>,
    pub provides: Vec<String>,
    pub conflicts: Vec<String>,
    pub replaces: Vec<String>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Installed {
    pub name: String,
    pub version: String,
    pub arch: String,
}

fn member(bytes: &[u8], suffix: &[u8]) -> Result<String, String> {
    let dec = zstd::Decoder::new(std::io::Cursor::new(bytes)).map_err(|e| e.to_string())?;
    let mut ar = tar::Archive::new(dec);
    for e in ar.entries().map_err(|e| e.to_string())? {
        let mut e = e.map_err(|e| e.to_string())?;
        if e.path_bytes().as_ref().ends_with(suffix) {
            let mut s = String::new();
            e.read_to_string(&mut s).map_err(|e| e.to_string())?;
            return Ok(s);
        }
    }
    Err(format!("no {} in package", String::from_utf8_lossy(suffix)))
}

pub fn pkginfo_text(pkg_bytes: &[u8]) -> Result<String, String> {
    member(pkg_bytes, b".PKGINFO")
}

pub fn buildinfo_text(pkg_bytes: &[u8]) -> Result<String, String> {
    member(pkg_bytes, b".BUILDINFO")
}

pub fn parse_pkginfo(text: &str) -> PkgInfo {
    let mut p = PkgInfo::default();
    for (k, v) in fields(text) {
        match k {
            "pkgname" => p.name = v.to_string(),
            "pkgver" => p.version = v.to_string(),
            "pkgarch" => p.arch = v.to_string(),
            "pkgdesc" => p.desc = v.to_string(),
            "url" => p.url = v.to_string(),
            "license" => p.license.push(v.to_string()),
            "group" => p.groups.push(v.to_string()),
            "depend" => p.depends.push(v.to_string()),
            "makedepend" => p.makedepends.push(v.to_string()),
            "checkdepend" => p.checkdepends.push(v.to_string()),
            "optdepend" => p.optdepends.push(v.to_string()),
            "provides" => p.provides.push(v.to_string()),
            "conflict" => p.conflicts.push(v.to_string()),
            "replaces" => p.replaces.push(v.to_string()),
            _ => {}
        }
    }
    p
}

/// Parse the `.BUILDINFO` `installed` block.
///
/// The first entry shares the line with the key — `installed =  glibc 2.39-1
/// x86_64 -` — so the header line has to be parsed as well as skipped.
pub fn parse_installed(text: &str) -> Vec<Installed> {
    let mut out = Vec::new();
    let mut in_block = false;
    for line in text.lines() {
        if line.trim_start().starts_with("installed") {
            in_block = true;
            if let Some(rest) = line.split_once('=') {
                push_entry(&mut out, rest.1);
            }
            continue;
        }
        if !in_block {
            continue;
        }
        if !line.starts_with(' ') && !line.starts_with('\t') {
            in_block = false;
            continue;
        }
        push_entry(&mut out, line);
    }
    out
}

fn push_entry(out: &mut Vec<Installed>, line: &str) {
    let mut it = line.split_whitespace();
    if let (Some(name), Some(version)) = (it.next(), it.next()) {
        out.push(Installed {
            name: name.to_string(),
            version: version.to_string(),
            arch: it.next().unwrap_or_default().to_string(),
        });
    }
}

fn fields(text: &str) -> impl Iterator<Item = (&str, &str)> {
    text.lines().filter_map(|l| l.split_once('=')).map(|(k, v)| (k.trim(), v.trim()))
}