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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
pub type RawPackage = raw::Package;
pub type RawVersion = raw::Version;
pub type RawProvider = raw::Provider;
pub type RawDependency = raw::Dependency;
pub type RawVersionFile = raw::VersionFile;
pub type RawDescriptionFile = raw::DescriptionFile;
pub type RawPackageFile = raw::PackageFile;
use std::hash::{Hash, Hasher};

/// This module contains the bindings and structs shared with c++
#[cxx::bridge]
pub mod raw {

	// Some weirdness exists in import order.
	// SourceURI is defined here, but used in the cache.
	// We need to impl vec so it can be put in one.
	impl Vec<SourceURI> {}
	#[derive(Debug)]
	pub struct SourceURI {
		pub uri: String,
		pub path: String,
	}

	pub struct Package {
		ptr: UniquePtr<PkgIterator>,
	}

	pub struct Version {
		ptr: UniquePtr<VerIterator>,
	}

	pub struct Provider {
		ptr: UniquePtr<PrvIterator>,
	}

	pub struct Dependency {
		ptr: UniquePtr<DepIterator>,
	}

	pub struct VersionFile {
		ptr: UniquePtr<VerFileIterator>,
	}

	pub struct DescriptionFile {
		ptr: UniquePtr<DescFileIterator>,
	}

	pub struct PackageFile {
		ptr: UniquePtr<PkgFileIterator>,
		index_file: UniquePtr<IndexFile>,
	}

	unsafe extern "C++" {
		include!("rust-apt/apt-pkg-c/types.h");
		include!("rust-apt/apt-pkg-c/package.h");

		type PkgIterator;
		type VerIterator;
		type PrvIterator;
		type DepIterator;

		type VerFileIterator;
		type DescFileIterator;
		type PkgFileIterator;
		type IndexFile;

		// Package Declarations

		/// Get the name of the package without the architecture.
		pub fn name(self: &Package) -> &str;

		/// Get the architecture of a package.
		pub fn arch(self: &Package) -> &str;

		/// Get the fullname of the package.
		///
		/// Pretty is a bool that will omit the native arch.
		pub fn fullname(self: &Package, pretty: bool) -> String;

		/// Get the ID of a package.
		pub fn id(self: &Package) -> u32;

		/// Get the current state of a package.
		pub fn current_state(self: &Package) -> u8;

		/// Get the installed state of a package.
		pub fn inst_state(self: &Package) -> u8;

		/// Get the selected state of a package.
		pub fn selected_state(self: &Package) -> u8;

		/// Get a pointer the the currently installed version
		///
		/// Safety: If Version.end() is true,
		/// calling methods on the Version can segfault.
		pub fn unsafe_current_version(self: &Package) -> Version;

		/// Get a pointer to the beginning of the VerIterator
		///
		/// Safety: If Version.end() is true,
		/// calling methods on the Version can segfault.
		pub fn unsafe_version_list(self: &Package) -> Version;

		/// Get the providers of this package
		pub fn unsafe_provides(self: &Package) -> Provider;

		pub fn unsafe_rev_depends(self: &Package) -> Dependency;

		/// True if the package is essential.
		pub fn is_essential(self: &Package) -> bool;

		pub fn raw_next(self: &Package);

		/// This will tell you if the inner PkgIterator is null
		///
		/// The cxx is_null function will still show non null because of
		/// wrappers in c++
		pub fn end(self: &Package) -> bool;

		// A simple way to clone the pointer
		pub fn unique(self: &Package) -> Package;

		// Version Declarations

		/// The version string of the version. "1.4.10".
		pub fn version(self: &Version) -> &str;

		/// The Arch of the version. "amd64".
		pub fn arch(self: &Version) -> &str;

		/// Return the version's parent RawPackage.
		pub fn parent_pkg(self: &Version) -> Package;

		/// The ID of the version.
		pub fn id(self: &Version) -> u32;

		/// The section of the version as shown in `apt show`.
		pub fn section(self: &Version) -> Result<&str>;

		/// The priority string as shown in `apt show`.
		pub fn priority_str(self: &Version) -> Result<&str>;

		/// The size of the .deb file.
		pub fn size(self: &Version) -> u64;

		/// The uncompressed size of the .deb file.
		pub fn installed_size(self: &Version) -> u64;

		/// True if the version is able to be downloaded.
		pub fn is_downloadable(self: &Version) -> bool;

		/// True if the version is currently installed
		pub fn is_installed(self: &Version) -> bool;

		/// Always contains the name, even if it is the same as the binary name
		pub fn source_name(self: &Version) -> &str;

		// Always contains the version string,
		// even if it is the same as the binary version.
		pub fn source_version(self: &Version) -> &str;

		pub fn unsafe_provides(self: &Version) -> Provider;

		pub fn unsafe_depends(self: &Version) -> Dependency;

		// This is for backend records lookups.
		// You can also get package files from here.
		pub fn unsafe_description_file(self: &Version) -> DescriptionFile;

		// You go through here to get the package files.
		pub fn unsafe_version_file(self: &Version) -> VersionFile;

		/// Return the parent package. TODO: This probably isn't going to work
		/// rn pub fn parent(self: &Package) -> bool;
		pub fn raw_next(self: &Version);

		/// This will tell you if the inner PkgIterator is null
		///
		/// The cxx is_null function will still show non null because of
		/// wrappers in c++
		pub fn end(self: &Version) -> bool;

		// A simple way to clone the pointer
		pub fn unique(self: &Version) -> Version;

		// Provider Declarations

		/// The name of what this provider provides
		pub fn name(self: &Provider) -> &str;

		pub fn version_str(self: &Provider) -> Result<&str>;

		/// The Target Package that can satisfy this provides
		pub fn target_pkg(self: &Provider) -> Package;

		pub fn parent_pkg(self: &Dependency) -> Package;

		pub fn parent_ver(self: &Dependency) -> Version;

		/// The Target Version that can satisfy this provides
		pub fn target_ver(self: &Provider) -> Version;

		pub fn raw_next(self: &Provider);

		// A simple way to clone the pointer
		pub fn unique(self: &Provider) -> Provider;

		/// This will tell you if the inner PkgIterator is null
		///
		/// The cxx is_null function will still show non null because of
		/// wrappers in c++
		pub fn end(self: &Provider) -> bool;

		// Dependency Declarations
		/// String representation of the dependency compare type
		/// "","<=",">=","<",">","=","!="
		pub fn comp_type(self: &Dependency) -> Result<&str>;

		pub fn index(self: &Dependency) -> u32;

		/// Should probably maybe potentially convert this to a method in rust?
		/// Just gets the dependency type. Taken from 'pkgcache.cc
		/// pkgCache::DepType'
		pub fn dep_type(self: &Dependency) -> u8;

		pub fn target_ver(self: &Dependency) -> Result<&str>;

		pub fn target_pkg(self: &Dependency) -> Package;

		pub fn all_targets(self: &Dependency) -> Version;

		/// Return true if this dep is Or'd with the next. The last dep in the
		/// or group will return False.
		pub fn compare_op(self: &Dependency) -> bool;

		/// Increment the Dep Iterator once
		pub fn raw_next(self: &Dependency);

		/// Return True if the dep is reverse, false if normal
		pub fn is_reverse(self: &Dependency) -> bool;

		/// Is the pointer null, basically
		pub fn end(self: &Dependency) -> bool;

		// A simple way to clone the pointer
		pub fn unique(self: &Dependency) -> Dependency;

		// PackageFile Declarations

		/// The path to the PackageFile
		pub fn filename(self: &PackageFile) -> Result<&str>;

		/// The Archive of the PackageFile. ex: unstable
		pub fn archive(self: &PackageFile) -> Result<&str>;

		/// The Origin of the PackageFile. ex: Debian
		pub fn origin(self: &PackageFile) -> Result<&str>;

		/// The Codename of the PackageFile. ex: main, non-free
		pub fn codename(self: &PackageFile) -> Result<&str>;

		/// The Label of the PackageFile. ex: Debian
		pub fn label(self: &PackageFile) -> Result<&str>;

		/// The Hostname of the PackageFile. ex: deb.debian.org
		pub fn site(self: &PackageFile) -> Result<&str>;

		/// The Component of the PackageFile. ex: sid
		pub fn component(self: &PackageFile) -> Result<&str>;

		/// The Architecture of the PackageFile. ex: amd64
		pub fn arch(self: &PackageFile) -> Result<&str>;

		/// The Index Type of the PackageFile. Known values are:
		///
		/// Debian Package Index, Debian Translation Index, Debian dpkg status
		/// file,
		pub fn index_type(self: &PackageFile) -> Result<&str>;

		/// The Index number of the PackageFile
		pub fn index(self: &PackageFile) -> u64;

		// /// VersionFile Declarations

		/// Return the package file associated with this version file.
		pub fn pkg_file(self: &VersionFile) -> PackageFile;

		/// Increment the iterator
		pub fn raw_next(self: &VersionFile);

		pub fn index(self: &VersionFile) -> u64;

		pub fn end(self: &VersionFile) -> bool;

		// A simple way to clone the pointer
		pub fn unique(self: &VersionFile) -> VersionFile;

		/// Return the package file associated with this version file.
		pub fn pkg_file(self: &DescriptionFile) -> PackageFile;

		/// Increment the iterator
		pub fn raw_next(self: &DescriptionFile);

		pub fn index(self: &DescriptionFile) -> u64;

		pub fn end(self: &DescriptionFile) -> bool;

		// A simple way to clone the pointer
		pub fn unique(self: &DescriptionFile) -> DescriptionFile;

	}
}

impl raw::Package {
	pub fn current_version(&self) -> Option<RawVersion> {
		let ver_list = self.unsafe_current_version();

		match ver_list.end() {
			true => None,
			false => Some(ver_list),
		}
	}

	pub fn version_list(&self) -> Option<RawVersion> {
		let ver_list = self.unsafe_version_list();

		match ver_list.end() {
			true => None,
			false => Some(ver_list),
		}
	}

	pub fn provides_list(&self) -> Option<RawProvider> {
		let ver_list = self.unsafe_provides();

		match ver_list.end() {
			true => None,
			false => Some(ver_list),
		}
	}

	pub fn rev_depends_list(&self) -> Option<RawDependency> {
		let rev_dep_list = self.unsafe_rev_depends();

		match rev_dep_list.end() {
			true => None,
			false => Some(rev_dep_list),
		}
	}

	/// True if the Package is installed.
	pub fn is_installed(&self) -> bool { self.current_version().is_some() }

	/// True if the package has versions.
	///
	/// If a package has no versions it is considered virtual.
	pub fn has_versions(&self) -> bool { self.version_list().is_some() }

	/// True if the package provides any other packages.
	pub fn has_provides(&self) -> bool { self.provides_list().is_some() }
}

impl raw::Version {
	pub fn provides_list(&self) -> Option<RawProvider> {
		let ver_list = self.unsafe_provides();

		match ver_list.end() {
			true => None,
			false => Some(ver_list),
		}
	}

	pub fn depends(&self) -> Option<RawDependency> {
		let ver_list = self.unsafe_depends();

		match ver_list.end() {
			true => None,
			false => Some(ver_list),
		}
	}

	pub fn version_files(&self) -> Option<RawVersionFile> {
		let ver_list = self.unsafe_version_file();

		match ver_list.end() {
			true => None,
			false => Some(ver_list),
		}
	}

	pub fn description_files(&self) -> Option<RawDescriptionFile> {
		let ver_list = self.unsafe_description_file();

		match ver_list.end() {
			true => None,
			false => Some(ver_list),
		}
	}
}

macro_rules! raw_iter {
	($structname: ident) => {
		impl Iterator for $structname {
			type Item = $structname;

			fn next(&mut self) -> Option<Self::Item> {
				match self.end() {
					true => None,
					false => {
						let ptr = self.unique();
						self.raw_next();
						Some(ptr)
					},
				}
			}
		}
	};
}

raw_iter!(RawPackage);
raw_iter!(RawVersion);
raw_iter!(RawProvider);
raw_iter!(RawDependency);
raw_iter!(RawVersionFile);
raw_iter!(RawDescriptionFile);

// TODO: Maybe make some internal macros and export them so
// this can be used in the higher level package.rs
macro_rules! raw_hash {
	($structname: ident) => {
		impl Hash for $structname {
			fn hash<H: Hasher>(&self, state: &mut H) { self.id().hash(state); }
		}

		impl PartialEq for $structname {
			fn eq(&self, other: &$structname) -> bool { self.id() == other.id() }
		}

		impl Eq for $structname {}
	};
}

raw_hash!(RawPackage);
raw_hash!(RawVersion);

#[cfg(test)]
mod raw_tests {
	use crate::raw::cache::raw::create_cache;
	use crate::raw::package::RawVersionFile;

	#[test]
	fn test() {
		crate::config::init_config_system();

		let debs: Vec<String> = vec![];
		let cache = create_cache(&debs).unwrap();

		let pkg = cache.find_pkg("apt").unwrap();
		dbg!(pkg.name());

		let installed = pkg.current_version().unwrap();

		dbg!(installed.version());

		for pkg in cache.begin().unwrap() {
			println!("ID: {}", pkg.id());
			println!("Name: {}", pkg.name());
			println!("Arch: {}", pkg.arch());
			println!("FullName: {}", pkg.fullname(false));
			println!("current_state: {}", pkg.current_state());
			println!("inst_state: {}", pkg.inst_state());
			println!("selected_state: {}\n", pkg.selected_state());

			match pkg.version_list() {
				Some(versions) => {
					for ver in versions {
						println!("Version of '{}'", pkg.name());

						println!("    Version: {}", &ver.version());
						println!("    Arch: {}", &ver.arch());
						println!("    Section: {}", &ver.section().unwrap_or_default());
						println!("    Source Pkg: {}", &ver.source_name());
						println!("    Source Version: {}\n", &ver.source_version());

						println!("End: {}\n\n", pkg.end());
					}
				},
				None => {
					println!("'{}' is a Virtual Package\n", pkg.name());
				},
			}
		}
	}

	#[test]
	fn raw_provides() {
		crate::config::init_config_system();

		let debs: Vec<String> = vec![];
		let cache = create_cache(&debs).unwrap();

		// Check Native Arch
		let pkg = cache.find_pkg("www-browser").unwrap();
		println!("{}:{}", pkg.name(), pkg.arch());

		for provider in pkg.provides_list().unwrap() {
			println!("Provider: {}", provider.name());
			println!(
				"  Pkg: {}, Version: {}",
				provider.target_pkg().name(),
				provider.target_ver().version()
			);
		}

		let pkg = cache.find_pkg("apt").unwrap();
		let cand = pkg.current_version().unwrap();

		for provider in cand.provides_list().unwrap() {
			println!("Provider: {}", provider.name());
			println!(
				"  Pkg: {}, Version: {}",
				provider.target_pkg().name(),
				provider.target_ver().version()
			);
		}
	}

	#[test]
	fn raw_depends() {
		crate::config::init_config_system();

		let debs: Vec<String> = vec![];
		let cache = create_cache(&debs).unwrap();

		let pkg = cache.find_pkg("apt").unwrap();
		let cand = pkg.current_version().unwrap();

		for dep in cand.depends().unwrap() {
			println!(
				"Dep: {}, Comp Op: {}",
				dep.target_pkg().name(),
				dep.compare_op()
			);
			for dep_ver in dep.all_targets() {
				println!("Version: {}", dep_ver.version())
			}
		}
	}

	#[test]
	fn raw_files() {
		crate::config::init_config_system();

		let debs: Vec<String> = vec![];
		let cache = create_cache(&debs).unwrap();

		let pkg = cache.find_pkg("apt").unwrap();
		let cand = pkg.current_version().unwrap();
		let depcache = cache.create_depcache();

		// Apt should have a candidate as well as current version
		assert!(!depcache.unsafe_candidate_version(&pkg).ptr.is_null());

		let ver_files: Vec<RawVersionFile> = cand.version_files().unwrap().collect();

		println!("Ver Files: {}", ver_files.len());
		println!("Desc Files: {}", cand.description_files().unwrap().count());

		for file in ver_files {
			let pkg_file = file.pkg_file();
			#[rustfmt::skip] // Skip Formatting the string.
			println!(
				"PackageFile: {{\n  \
				  FileName: {},\n  \
				  Archive: {},\n  \
				  Origin: {}\n  \
				  Label: {}\n  \
				  Site: {}\n  \
				  Arch: {}\n  \
				  Component: {}\n  \
				  Index Type: {}\n  \
				  Index: {}\n\
				}}",
				pkg_file.filename().unwrap_or("Unknown"),
				pkg_file.archive().unwrap_or("Unknown"),
				pkg_file.origin().unwrap_or("Unknown"),
				pkg_file.label().unwrap_or("Unknown"),
				pkg_file.site().unwrap_or("Unknown"),
				pkg_file.arch().unwrap_or("Unknown"),
				pkg_file.component().unwrap_or("Unknown"),
				pkg_file.index_type().unwrap_or("Unknown"),
				pkg_file.index(),
			);
		}
	}

	#[test]
	fn raw_depcache() {
		crate::config::init_config_system();

		let debs: Vec<String> = vec![];
		let cache = create_cache(&debs).unwrap();

		let pkg = cache.find_pkg("apt").unwrap();

		let depcache = cache.create_depcache();
		dbg!(depcache.is_upgradable(&pkg));

		depcache.mark_delete(&pkg, false);

		dbg!(depcache.marked_delete(&pkg));

		dbg!(depcache.delete_count());

		let mut progress = crate::raw::progress::NoOpProgress::new_box();
		depcache.full_upgrade(&mut progress).unwrap();

		for pkg in cache.begin().unwrap() {
			if depcache.marked_upgrade(&pkg) {
				println!("Upgrade => {}", pkg.fullname(false));
			}
		}
	}

	#[test]
	fn source_uris() {
		crate::config::init_config_system();

		let debs: Vec<String> = vec![];
		let cache = create_cache(&debs).unwrap();
		dbg!(cache.source_uris());
	}

	#[test]
	fn priority() {
		crate::config::init_config_system();

		let debs: Vec<String> = vec![];
		let cache = create_cache(&debs).unwrap();
		let pkg = cache.find_pkg("apt").unwrap();
		let depcache = cache.create_depcache();
		let cand = depcache.unsafe_candidate_version(&pkg);

		dbg!(cache.priority(&cand));
	}

	#[test]
	fn records() {
		crate::config::init_config_system();

		let debs: Vec<String> = vec![];
		let cache = create_cache(&debs).unwrap();
		let pkg = cache.find_pkg("apt").unwrap();
		let depcache = cache.create_depcache();
		let cand = depcache.unsafe_candidate_version(&pkg);

		let records = cache.create_records();

		records.ver_file_lookup(&cand.version_files().unwrap().next().unwrap());
		dbg!(records.short_desc().unwrap());
		records.desc_file_lookup(&cand.description_files().unwrap().next().unwrap());
		dbg!(records.long_desc().unwrap());

		let mut pkg_file = cand.version_files().unwrap().next().unwrap().pkg_file();
		cache.find_index(&mut pkg_file);

		dbg!(cache.is_trusted(&mut pkg_file));

		dbg!(records.ver_uri(&pkg_file).unwrap());
	}

	#[test]
	fn update() {
		// This test Requires root
		crate::config::init_config_system();

		let debs: Vec<String> = vec![];
		let cache = create_cache(&debs).unwrap();
		let mut progress = crate::raw::progress::AptAcquireProgress::new_box();

		cache.update(&mut progress).unwrap();
	}

	#[test]
	fn pacman() {
		crate::config::init_config_system();

		let debs: Vec<String> = vec![];
		let cache = create_cache(&debs).unwrap();
		let _pacman = crate::raw::pkgmanager::raw::create_pkgmanager(&cache);
		let _resolve = crate::raw::pkgmanager::raw::create_problem_resolver(&cache);
	}
}