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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
use std::cell::RefCell;
use std::fmt;
use std::rc::Rc;
use cxx::{Exception, UniquePtr};
use crate::config::init_config_system;
use crate::depcache::DepCache;
use crate::package;
use crate::package::Package;
use crate::progress::UpdateProgress;
use crate::records::Records;
use crate::util::DiskSpace;
pub type PackageSort = raw::PackageSort;
pub type Sort = raw::Sort;
impl Default for PackageSort {
fn default() -> PackageSort {
PackageSort {
names: false,
upgradable: Sort::Disable,
virtual_pkgs: Sort::Disable,
installed: Sort::Disable,
auto_installed: Sort::Disable,
auto_removable: Sort::Disable,
}
}
}
impl PackageSort {
pub fn names(mut self) -> Self {
self.names = true;
self
}
pub fn upgradable(mut self) -> Self {
self.upgradable = Sort::Enable;
self
}
pub fn not_upgradable(mut self) -> Self {
self.upgradable = Sort::Reverse;
self
}
pub fn include_virtual(mut self) -> Self {
self.virtual_pkgs = Sort::Enable;
self
}
pub fn only_virtual(mut self) -> Self {
self.virtual_pkgs = Sort::Reverse;
self
}
pub fn installed(mut self) -> Self {
self.installed = Sort::Enable;
self
}
pub fn not_installed(mut self) -> Self {
self.installed = Sort::Reverse;
self
}
pub fn auto_installed(mut self) -> Self {
self.auto_installed = Sort::Enable;
self
}
pub fn manually_installed(mut self) -> Self {
self.auto_installed = Sort::Reverse;
self
}
pub fn auto_removable(mut self) -> Self {
self.auto_removable = Sort::Enable;
self
}
pub fn not_auto_removable(mut self) -> Self {
self.auto_removable = Sort::Reverse;
self
}
}
#[derive(Debug)]
pub struct Cache {
pub ptr: Rc<RefCell<UniquePtr<raw::PkgCacheFile>>>,
pub records: Rc<RefCell<Records>>,
depcache: Rc<RefCell<DepCache>>,
}
impl Default for Cache {
fn default() -> Self { Self::new() }
}
impl Cache {
pub fn new() -> Self {
init_config_system();
let cache_ptr = Rc::new(RefCell::new(raw::pkg_cache_create()));
Self {
records: Rc::new(RefCell::new(Records::new(Rc::clone(&cache_ptr)))),
depcache: Rc::new(RefCell::new(DepCache::new(Rc::clone(&cache_ptr)))),
ptr: cache_ptr,
}
}
pub fn clear(&self) { self.depcache.borrow().clear(); }
pub fn update(&self, progress: &mut Box<dyn UpdateProgress>) -> Result<(), Exception> {
raw::cache_update(&self.ptr.borrow(), progress)
}
pub fn sources(&self) -> impl Iterator<Item = raw::SourceFile> + '_ {
raw::source_uris(&self.ptr.borrow()).into_iter()
}
pub fn provides(
&self,
virt_pkg: &Package,
cand_only: bool,
) -> impl Iterator<Item = Package> + '_ {
raw::pkg_provides_list(&self.ptr.borrow(), &virt_pkg.ptr, cand_only)
.into_iter()
.map(|pkg| Package::new(Rc::clone(&self.records), Rc::clone(&self.depcache), pkg))
}
pub fn get<'a>(&'a self, name: &str) -> Option<Package<'a>> {
let mut fields = name.split(':');
let name = fields.next()?;
let arch = fields.next().unwrap_or_default();
let pkg_ptr = self.find_by_name(name, arch);
if pkg_ptr.ptr.is_null() {
return None;
}
Some(Package::new(
Rc::clone(&self.records),
Rc::clone(&self.depcache),
pkg_ptr,
))
}
fn find_by_name(&self, name: &str, arch: &str) -> raw::PackagePtr {
if !arch.is_empty() {
return raw::pkg_cache_find_name_arch(
&self.ptr.borrow(),
name.to_owned(),
arch.to_owned(),
);
}
raw::pkg_cache_find_name(&self.ptr.borrow(), name.to_owned())
}
pub fn packages<'a>(&'a self, sort: &'a PackageSort) -> impl Iterator<Item = Package> + '_ {
let mut pkg_list = raw::pkg_list(&self.ptr.borrow(), sort);
if sort.names {
pkg_list.sort_by_cached_key(|pkg| package::raw::get_fullname(pkg, true));
}
pkg_list
.into_iter()
.map(|pkg| Package::new(Rc::clone(&self.records), Rc::clone(&self.depcache), pkg))
}
pub fn install_count(&self) -> u32 { self.depcache.borrow().install_count() }
pub fn delete_count(&self) -> u32 { self.depcache.borrow().delete_count() }
pub fn keep_count(&self) -> u32 { self.depcache.borrow().keep_count() }
pub fn broken_count(&self) -> u32 { self.depcache.borrow().broken_count() }
pub fn download_size(&self) -> u64 { self.depcache.borrow().download_size() }
pub fn disk_size(&self) -> DiskSpace { self.depcache.borrow().disk_size() }
}
pub struct PackageFile {
pkg_file: RefCell<raw::PackageFile>,
pub cache: Rc<RefCell<UniquePtr<raw::PkgCacheFile>>>,
}
impl PackageFile {
pub fn new(
pkg_file: raw::PackageFile,
cache: Rc<RefCell<UniquePtr<raw::PkgCacheFile>>>,
) -> PackageFile {
PackageFile {
pkg_file: RefCell::new(pkg_file),
cache,
}
}
pub fn filename(&self) -> Option<String> { raw::filename(&self.pkg_file.borrow()).ok() }
pub fn archive(&self) -> Option<String> { raw::archive(&self.pkg_file.borrow()).ok() }
pub fn origin(&self) -> Option<String> { raw::origin(&self.pkg_file.borrow()).ok() }
pub fn codename(&self) -> Option<String> { raw::codename(&self.pkg_file.borrow()).ok() }
pub fn label(&self) -> Option<String> { raw::label(&self.pkg_file.borrow()).ok() }
pub fn site(&self) -> Option<String> { raw::site(&self.pkg_file.borrow()).ok() }
pub fn component(&self) -> Option<String> { raw::component(&self.pkg_file.borrow()).ok() }
pub fn arch(&self) -> Option<String> { raw::arch(&self.pkg_file.borrow()).ok() }
pub fn index_type(&self) -> Option<String> { raw::index_type(&self.pkg_file.borrow()).ok() }
pub fn index(&self) -> u64 { raw::index(&self.pkg_file.borrow()) }
pub fn is_trusted(&self) -> bool {
raw::pkg_file_is_trusted(&self.cache.borrow(), &mut self.pkg_file.borrow_mut())
}
}
impl fmt::Debug for PackageFile {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"PackageFile <\n Filename: {},\n Archive: {},\n Origin: {},\n Codename: \
{},\n Label: {},\n Site: {},\n Component: {},\n Arch: {},\n Index: \
{},\n Index Type: {},\n Trusted: {},\n>",
self.filename().unwrap_or_else(|| String::from("Unknown")),
self.archive().unwrap_or_else(|| String::from("Unknown")),
self.origin().unwrap_or_else(|| String::from("Unknown")),
self.codename().unwrap_or_else(|| String::from("Unknown")),
self.label().unwrap_or_else(|| String::from("Unknown")),
self.site().unwrap_or_else(|| String::from("Unknown")),
self.component().unwrap_or_else(|| String::from("Unknown")),
self.arch().unwrap_or_else(|| String::from("Unknown")),
self.index(),
self.index_type().unwrap_or_else(|| String::from("Unknown")),
self.is_trusted(),
)?;
Ok(())
}
}
impl fmt::Display for PackageFile {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{self:?}")?;
Ok(())
}
}
#[cxx::bridge]
pub mod raw {
#[derive(Debug)]
struct SourceFile {
uri: String,
filename: String,
}
struct PackagePtr {
ptr: UniquePtr<PkgIterator>,
}
struct VersionPtr {
ptr: UniquePtr<VerIterator>,
desc: UniquePtr<DescIterator>,
}
struct PackageFile {
ptr: UniquePtr<PkgFile>,
}
struct VersionFile {
ptr: UniquePtr<VerFileIterator>,
}
#[derive(Debug)]
pub enum Sort {
Disable,
Enable,
Reverse,
}
#[derive(Debug)]
pub struct PackageSort {
pub names: bool,
pub upgradable: Sort,
pub virtual_pkgs: Sort,
pub installed: Sort,
pub auto_installed: Sort,
pub auto_removable: Sort,
}
unsafe extern "C++" {
type PkgCacheFile;
type PkgCache;
type PkgSourceList;
type PkgDepCache;
type PkgIterator;
type PkgFile;
type VerIterator;
type VerFileIterator;
type DescIterator;
type DynUpdateProgress = crate::progress::raw::DynUpdateProgress;
include!("rust-apt/apt-pkg-c/cache.h");
include!("rust-apt/apt-pkg-c/progress.h");
include!("rust-apt/apt-pkg-c/records.h");
pub fn pkg_cache_create() -> UniquePtr<PkgCacheFile>;
pub fn cache_update(
cache: &UniquePtr<PkgCacheFile>,
progress: &mut DynUpdateProgress,
) -> Result<()>;
pub fn source_uris(cache: &UniquePtr<PkgCacheFile>) -> Vec<SourceFile>;
pub fn pkg_list(cache: &UniquePtr<PkgCacheFile>, sort: &PackageSort) -> Vec<PackagePtr>;
pub fn ver_file_list(ver: &VersionPtr) -> Vec<VersionFile>;
pub fn ver_pkg_file_list(ver: &VersionPtr) -> Vec<PackageFile>;
pub fn pkg_version_list(pkg: &PackagePtr) -> Vec<VersionPtr>;
pub fn pkg_provides_list(
cache: &UniquePtr<PkgCacheFile>,
iterator: &PackagePtr,
cand_only: bool,
) -> Vec<PackagePtr>;
pub fn pkg_cache_find_name(cache: &UniquePtr<PkgCacheFile>, name: String) -> PackagePtr;
pub fn pkg_cache_find_name_arch(
cache: &UniquePtr<PkgCacheFile>,
name: String,
arch: String,
) -> PackagePtr;
pub fn filename(pkg_file: &PackageFile) -> Result<String>;
pub fn archive(pkg_file: &PackageFile) -> Result<String>;
pub fn origin(pkg_file: &PackageFile) -> Result<String>;
pub fn codename(pkg_file: &PackageFile) -> Result<String>;
pub fn label(pkg_file: &PackageFile) -> Result<String>;
pub fn site(pkg_file: &PackageFile) -> Result<String>;
pub fn component(pkg_file: &PackageFile) -> Result<String>;
pub fn arch(pkg_file: &PackageFile) -> Result<String>;
pub fn index_type(pkg_file: &PackageFile) -> Result<String>;
pub fn index(pkg_file: &PackageFile) -> u64;
pub fn pkg_file_is_trusted(
cache: &UniquePtr<PkgCacheFile>,
pkg_file: &mut PackageFile,
) -> bool;
}
}
impl fmt::Debug for raw::VersionPtr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"VersionPtr: {}:{}",
package::raw::get_fullname(&package::raw::ver_parent(self), false),
package::raw::ver_str(self)
)?;
Ok(())
}
}
impl fmt::Debug for raw::PkgCacheFile {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "PkgCacheFile: {{ To Be Implemented }}")?;
Ok(())
}
}
impl fmt::Debug for raw::PkgDepCache {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "PkgDepCache: {{ To Be Implemented }}")?;
Ok(())
}
}
impl fmt::Debug for raw::PackagePtr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "PackagePtr: {}", package::raw::get_fullname(self, false))?;
Ok(())
}
}
impl fmt::Display for raw::SourceFile {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Source< Uri: {}, Filename: {}>", self.uri, self.filename)?;
Ok(())
}
}
impl fmt::Debug for raw::PackageFile {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "PackageFile: {{ To Be Implemented }}")?;
Ok(())
}
}
impl fmt::Debug for raw::VersionFile {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "VersionFile: {{ To Be Implemented }}")?;
Ok(())
}
}