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
extern crate chrono;
extern crate deb_architectures;
#[macro_use]
extern crate smart_default;

mod entry;
mod image_size;
mod time;

pub use self::entry::*;
pub use self::image_size::*;

use self::time::get_time;
use chrono::{DateTime, Utc};
use std::collections::BTreeMap;
use std::path::Path;
use std::str::FromStr;
use std::{fs, io};

/// The dist release file is a file in the apt repository that points to all other dist files in the archive.
#[derive(Debug, SmartDefault, Clone, PartialEq)]
pub struct DistRelease {
    pub architectures: Vec<String>,
    pub codename: String,
    pub components: Vec<String>,
    #[default = "Utc::now()"]
    pub date: DateTime<Utc>,
    pub description: String,
    pub label: String,
    pub origin: String,
    pub suite: String,
    pub version: String,
    pub sums: BTreeMap<String, EntryComponents>,
}

impl DistRelease {
    pub fn from_file<P: AsRef<Path>>(path: P) -> io::Result<Self> {
        fs::read_to_string(path).and_then(|string| string.parse::<Self>())
    }
}

impl FromStr for DistRelease {
    type Err = io::Error;

    fn from_str(input: &str) -> Result<Self, Self::Err> {
        let mut iterator = input.lines();

        let mut release = DistRelease::default();

        #[derive(Copy, Clone)]
        enum Variant {
            Archs,
            Codename,
            Components,
            Date,
            Description,
            Label,
            Origin,
            Suite,
            Version,
        }

        let mut entries = vec![
            ("Architectures:", Variant::Archs),
            ("Codename:", Variant::Codename),
            ("Components:", Variant::Components),
            ("Date:", Variant::Date),
            ("Description:", Variant::Description),
            ("Label:", Variant::Label),
            ("Origin:", Variant::Origin),
            ("Suite:", Variant::Suite),
            ("Version:", Variant::Version),
        ];

        let mut remove = None;

        fn get_string(value: &str) -> String {
            value.trim().to_owned()
        }

        fn get_vec(value: &str) -> Vec<String> {
            value.split_whitespace().map(String::from).collect()
        }

        while !entries.is_empty() {
            let line = iterator.next().unwrap();

            for (id, &(ref key, variant)) in entries.iter().enumerate() {
                if line.starts_with(key) {
                    remove = Some(id);

                    let value = &line[key.len()..];

                    match variant {
                        Variant::Archs => release.architectures = get_vec(value),
                        Variant::Codename => release.codename = get_string(value),
                        Variant::Components => release.components = get_vec(value),
                        Variant::Date => release.date = get_time(value)?,
                        Variant::Description => release.description = get_string(value),
                        Variant::Label => release.label = get_string(value),
                        Variant::Origin => release.origin = get_string(value),
                        Variant::Suite => release.suite = get_string(value),
                        Variant::Version => release.version = get_string(value),
                    }
                }
            }

            if let Some(pos) = remove.take() {
                entries.remove(pos);
            } else {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidData,
                    format!("unknown key in release file: {}", line),
                ));
            }
        }

        let mut active_hash = String::new();
        let mut active_components = EntryComponents::default();

        for line in iterator {
            if line.starts_with(' ') {
                match line.parse::<ReleaseEntry>() {
                    Ok(mut entry) => {
                        let mut path = String::new();
                        ::std::mem::swap(&mut path, &mut entry.path);
                        let base = match path.find('.') {
                            Some(pos) => &path[..pos],
                            None => &path,
                        };

                        match path.find('/') {
                            Some(pos) => {
                                let component = &path[..pos];
                                // TODO: Prevent this allocation.
                                entry.path = path[pos + 1..].to_owned();

                                active_components
                                    .components
                                    .entry(component.into())
                                    .and_modify(|e| {
                                        e.entry(base.into())
                                            .and_modify(|e| e.push(entry.to_owned()))
                                            .or_insert_with(|| vec![entry.to_owned()]);
                                    })
                                    .or_insert_with(|| {
                                        let mut map = BTreeMap::new();
                                        map.insert(base.into(), vec![entry.to_owned()]);
                                        map
                                    });
                            }
                            None => {
                                entry.path = path.clone();
                                active_components
                                    .base
                                    .entry(base.into())
                                    .and_modify(|e| e.push(entry.to_owned()))
                                    .or_insert_with(|| vec![entry.to_owned()]);
                            }
                        }
                    }
                    Err(why) => {
                        return Err(io::Error::new(
                            io::ErrorKind::InvalidData,
                            format!("invalid checksum entry: {}", why),
                        ))
                    }
                }
            } else {
                if !active_components.is_empty() {
                    release
                        .sums
                        .insert(active_hash.clone(), active_components.clone());
                    active_components.clear();
                }

                active_hash.clear();
                active_hash.push_str(line.trim());
                active_hash.pop();
            }
        }

        if !active_components.is_empty() {
            release.sums.insert(active_hash, active_components);
        }

        Ok(release)
    }
}

/// Stores the entries for each component for this checksum method.
#[derive(Debug, Default, Clone, Hash, PartialEq)]
pub struct EntryComponents {
    pub base: BTreeMap<String, Vec<ReleaseEntry>>,
    pub components: BTreeMap<String, BTreeMap<String, Vec<ReleaseEntry>>>,
}

impl EntryComponents {
    pub fn clear(&mut self) {
        self.base.clear();
        self.components.clear();
    }

    pub fn is_empty(&self) -> bool {
        self.base.is_empty() && self.components.is_empty()
    }
}