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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
use std::cell::OnceCell;
use std::cmp::Ordering;
use std::collections::HashMap;
use std::fmt;
use cxx::UniquePtr;
use crate::raw::{IntoRawIter, VerIterator};
use crate::util::cmp_versions;
use crate::{
Cache, DepType, Dependency, Package, PackageFile, PackageRecords, Provider, VersionFile,
create_depends_map,
};
/// Represents a single Version of a package.
pub struct Version<'a> {
pub(crate) ptr: UniquePtr<VerIterator>,
cache: &'a Cache,
depends_map: OnceCell<HashMap<DepType, Vec<Dependency<'a>>>>,
}
impl Clone for Version<'_> {
fn clone(&self) -> Self {
Self {
ptr: unsafe { self.ptr.unique() },
cache: self.cache,
depends_map: self.depends_map.clone(),
}
}
}
impl<'a> Version<'a> {
pub fn new(ptr: UniquePtr<VerIterator>, cache: &'a Cache) -> Version<'a> {
Version {
ptr,
cache,
depends_map: OnceCell::new(),
}
}
/// Returns a list of providers
pub fn provides(&self) -> impl Iterator<Item = Provider<'a>> {
unsafe { self.ptr.provides() }
.raw_iter()
.map(|p| Provider::new(p, self.cache))
}
pub fn version_files(&self) -> impl Iterator<Item = VersionFile<'a>> {
unsafe { self.ptr.version_files() }
.raw_iter()
.map(|v| VersionFile::new(v, self.cache))
}
/// Returns an iterator of PackageFiles (Origins) for the version
pub fn package_files(&self) -> impl Iterator<Item = PackageFile<'a>> {
self.version_files().map(|v| v.package_file())
}
/// Return the version's parent package.
pub fn parent(&self) -> Package<'a> { Package::new(self.cache, unsafe { self.parent_pkg() }) }
/// Returns a reference to the Dependency Map owned by the Version
///
/// Dependencies are in a `Vec<Dependency>`
///
/// The Dependency struct represents an Or Group of dependencies.
/// The base deps are located in `Dependency.base_deps`
///
/// For example where we use the `DepType::Depends` key:
///
/// ```
/// use rust_apt::{new_cache, DepType};
/// let cache = new_cache!().unwrap();
/// let pkg = cache.get("apt").unwrap();
/// let version = pkg.candidate().unwrap();
/// for dep in version.depends_map().get(&DepType::Depends).unwrap() {
/// if dep.is_or() {
/// for base_dep in dep.iter() {
/// println!("{}", base_dep.name())
/// }
/// } else {
/// // is_or is false so there is only one BaseDep
/// println!("{}", dep.first().name())
/// }
/// }
/// ```
pub fn depends_map(&self) -> &HashMap<DepType, Vec<Dependency<'a>>> {
self.depends_map
.get_or_init(|| create_depends_map(self.cache, unsafe { self.depends().make_safe() }))
}
/// Returns a reference Vector, if it exists, for the given key.
///
/// See the doc for `depends_map()` for more information.
pub fn get_depends(&self, key: &DepType) -> Option<&Vec<Dependency<'a>>> {
self.depends_map().get(key)
}
/// Returns a Reference Vector, if it exists, for "Enhances".
pub fn enhances(&self) -> Option<&Vec<Dependency<'a>>> { self.get_depends(&DepType::Enhances) }
/// Returns a Reference Vector, if it exists,
/// for "Depends" and "PreDepends".
pub fn dependencies(&self) -> Option<Vec<&Dependency<'a>>> {
let mut ret_vec: Vec<&Dependency> = Vec::new();
for dep_type in [DepType::Depends, DepType::PreDepends] {
if let Some(dep_list) = self.get_depends(&dep_type) {
for dep in dep_list {
ret_vec.push(dep)
}
}
}
if ret_vec.is_empty() {
return None;
}
Some(ret_vec)
}
/// Returns a Reference Vector, if it exists, for "Recommends".
pub fn recommends(&self) -> Option<&Vec<Dependency<'a>>> {
self.get_depends(&DepType::Recommends)
}
/// Returns a Reference Vector, if it exists, for "suggests".
pub fn suggests(&self) -> Option<&Vec<Dependency<'a>>> { self.get_depends(&DepType::Suggests) }
/// Move the PkgRecords into the correct place for the Description
fn desc_lookup(&self) -> Option<&PackageRecords> {
let desc = unsafe { self.translated_desc().make_safe()? };
Some(self.cache.records().desc_lookup(&desc))
}
/// Get the translated long description
pub fn description(&self) -> Option<String> { self.desc_lookup()?.long_desc() }
/// Get the translated short description
pub fn summary(&self) -> Option<String> { self.desc_lookup()?.short_desc() }
/// Get data from the specified record field
///
/// # Returns:
/// * Some String or None if the field doesn't exist.
///
/// # Example:
/// ```
/// use rust_apt::new_cache;
/// use rust_apt::records::RecordField;
///
/// let cache = new_cache!().unwrap();
/// let pkg = cache.get("apt").unwrap();
/// let cand = pkg.candidate().unwrap();
///
/// println!("{}", cand.get_record(RecordField::Maintainer).unwrap());
/// // Or alternatively you can just pass any string
/// println!("{}", cand.get_record("Description-md5").unwrap());
/// ```
pub fn get_record<T: ToString + ?Sized>(&self, field: &T) -> Option<String> {
self.version_files()
.next()?
.lookup()
.get_field(field.to_string())
}
/// Get the hash specified. If there isn't one returns None
/// `version.hash("md5sum")`
pub fn hash<T: ToString + ?Sized>(&self, hash_type: &T) -> Option<String> {
self.version_files()
.next()?
.lookup()
.hash_find(hash_type.to_string())
}
/// Get the sha256 hash. If there isn't one returns None
/// This is equivalent to `version.hash("sha256")`
pub fn sha256(&self) -> Option<String> { self.hash("sha256") }
/// Get the sha512 hash. If there isn't one returns None
/// This is equivalent to `version.hash("sha512")`
pub fn sha512(&self) -> Option<String> { self.hash("sha512") }
/// Returns an Iterator of URIs for the Version.
pub fn uris(&self) -> impl Iterator<Item = String> + 'a {
self.version_files().filter_map(|v| {
let pkg_file = v.package_file();
if !pkg_file.is_downloadable() {
return None;
}
Some(pkg_file.index_file().archive_uri(&v.lookup().filename()))
})
}
/// Set this version as the candidate.
pub fn set_candidate(&self) { self.cache.depcache().set_candidate_version(self); }
/// The priority of the Version as shown in `apt policy`.
pub fn priority(&self) -> i32 { self.cache.priority(self) }
}
// Implementations for comparing versions.
impl PartialEq for Version<'_> {
fn eq(&self, other: &Self) -> bool {
matches!(
cmp_versions(self.version(), other.version()),
Ordering::Equal
)
}
}
impl Ord for Version<'_> {
fn cmp(&self, other: &Self) -> Ordering { cmp_versions(self.version(), other.version()) }
}
impl PartialOrd for Version<'_> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { Some(self.cmp(other)) }
}
impl fmt::Display for Version<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.version())?;
Ok(())
}
}
impl fmt::Debug for Version<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let parent = self.parent();
f.debug_struct("Version")
.field("pkg", &parent.name())
.field("arch", &self.arch())
.field("version", &self.version())
.field(
"is_candidate",
&parent.candidate().is_some_and(|cand| self == &cand),
)
.field("is_installed", &self.is_installed())
.finish_non_exhaustive()
}
}
#[cxx::bridge]
pub(crate) mod raw {
impl CxxVector<VerIterator> {}
unsafe extern "C++" {
include!("rust-apt/apt-pkg-c/package.h");
type VerIterator;
type PkgIterator = crate::iterators::PkgIterator;
type PrvIterator = crate::iterators::PrvIterator;
type DepIterator = crate::iterators::DepIterator;
type DescIterator = crate::iterators::DescIterator;
type VerFileIterator = crate::iterators::VerFileIterator;
/// The version string of the version. "1.4.10".
pub fn version(self: &VerIterator) -> &str;
/// The Arch of the version. "amd64".
pub fn arch(self: &VerIterator) -> &str;
/// Return the version's parent PkgIterator.
///
/// # Safety
///
/// The returned UniquePtr cannot outlive the cache.
unsafe fn parent_pkg(self: &VerIterator) -> UniquePtr<PkgIterator>;
/// The section of the version as shown in `apt show`.
pub fn section(self: &VerIterator) -> Result<&str>;
/// The priority string as shown in `apt show`.
pub fn priority_str(self: &VerIterator) -> Result<&str>;
/// The size of the .deb file.
pub fn size(self: &VerIterator) -> u64;
/// The uncompressed size of the .deb file.
pub fn installed_size(self: &VerIterator) -> u64;
// TODO: Possibly return an enum
pub fn multi_arch(self: &VerIterator) -> u8;
/// String representing MultiArch flag
/// same, foreign, allowed, none
pub fn multi_arch_type(self: &VerIterator) -> &str;
/// True if the version is able to be downloaded.
#[cxx_name = "Downloadable"]
pub fn is_downloadable(self: &VerIterator) -> bool;
/// True if the version is currently installed
pub fn is_installed(self: &VerIterator) -> bool;
/// Always contains the name, even if it is the same as the binary name
pub fn source_name(self: &VerIterator) -> &str;
// Always contains the version string,
// even if it is the same as the binary version.
pub fn source_version(self: &VerIterator) -> &str;
/// Return Providers Iterator
///
/// # Safety
///
/// If the inner pointer is null segfaults can occur.
///
/// Using [`crate::raw::IntoRawIter::make_safe`] to convert to an Option
/// is recommended.
///
/// The returned UniquePtr cannot outlive the cache.
unsafe fn provides(self: &VerIterator) -> UniquePtr<PrvIterator>;
/// Return Dependency Iterator
///
/// # Safety
///
/// If the inner pointer is null segfaults can occur.
///
/// Using [`crate::raw::IntoRawIter::make_safe`] to convert to an Option
/// is recommended.
///
/// The returned UniquePtr cannot outlive the cache.
unsafe fn depends(self: &VerIterator) -> UniquePtr<DepIterator>;
/// Return the version files.
/// You go through here to get the package files.
///
/// # Safety
///
/// If the inner pointer is null segfaults can occur.
///
/// Using [`crate::raw::IntoRawIter::make_safe`] to convert to an Option
/// is recommended.
///
/// The returned UniquePtr cannot outlive the cache.
unsafe fn version_files(self: &VerIterator) -> UniquePtr<VerFileIterator>;
/// This is for backend records lookups.
///
/// # Safety
///
/// If the inner pointer is null segfaults can occur.
///
/// Using [`crate::raw::IntoRawIter::make_safe`] to convert to an Option
/// is recommended.
///
/// The returned UniquePtr cannot outlive the cache.
unsafe fn translated_desc(self: &VerIterator) -> UniquePtr<DescIterator>;
#[cxx_name = "Index"]
pub fn index(self: &VerIterator) -> u64;
/// Clone the pointer.
///
/// # Safety
///
/// The returned UniquePtr cannot outlive the cache.
unsafe fn unique(self: &VerIterator) -> UniquePtr<VerIterator>;
pub fn raw_next(self: Pin<&mut VerIterator>);
pub fn end(self: &VerIterator) -> bool;
}
}