Skip to main content

rust_apt/iterators/
files.rs

1use std::cell::OnceCell;
2
3use cxx::UniquePtr;
4
5use crate::raw::{IndexFile, PkgFileIterator, VerFileIterator};
6use crate::{Cache, PackageRecords};
7
8/// Associates a version with a PackageFile
9///
10/// This allows a full description of all Versions in all files
11pub struct VersionFile<'a> {
12	pub(crate) ptr: UniquePtr<VerFileIterator>,
13	cache: &'a Cache,
14}
15
16impl<'a> VersionFile<'a> {
17	pub fn new(ptr: UniquePtr<VerFileIterator>, cache: &'a Cache) -> VersionFile<'a> {
18		VersionFile { ptr, cache }
19	}
20
21	/// Return the PkgRecords Parser for the VersionFile
22	pub fn lookup(&self) -> &PackageRecords { self.cache.records().ver_lookup(&self.ptr) }
23
24	/// Return the PackageFile for this VersionFile
25	pub fn package_file(&self) -> PackageFile<'a> {
26		PackageFile::new(unsafe { self.ptr.package_file() }, self.cache)
27	}
28}
29
30/// Stores information about the files used to generate the cache
31///
32/// Package files are referenced by Version structures to be able to know
33/// after which Packages file includes this Version.
34pub struct PackageFile<'a> {
35	pub(crate) ptr: UniquePtr<PkgFileIterator>,
36	cache: &'a Cache,
37	index: OnceCell<UniquePtr<IndexFile>>,
38}
39
40impl<'a> PackageFile<'a> {
41	pub fn new(ptr: UniquePtr<PkgFileIterator>, cache: &'a Cache) -> PackageFile<'a> {
42		PackageFile {
43			ptr,
44			cache,
45			index: OnceCell::new(),
46		}
47	}
48
49	pub fn index_file(&self) -> &IndexFile {
50		self.index
51			.get_or_init(|| unsafe { self.cache.find_index(self) })
52	}
53
54	/// The priority of this file as shown in `apt-cache policy`.
55	pub fn priority(&self) -> i32 { self.cache.file_priority(self) }
56}
57
58cxx_convert_result!(
59	PackageFile,
60	/// The path to the PackageFile
61	filename() -> &str,
62	/// The Archive of the PackageFile. ex: unstable
63	archive() -> &str,
64	/// The Origin of the PackageFile. ex: Debian
65	origin() -> &str,
66	/// The Codename of the PackageFile. ex: main, non-free
67	codename() -> &str,
68	/// The Label of the PackageFile. ex: Debian
69	label() -> &str,
70	/// The Hostname of the PackageFile. ex: deb.debian.org
71	site() -> &str,
72	/// The Component of the PackageFile. ex: sid
73	component() -> &str,
74	/// The Architecture of the PackageFile. ex: amd64
75	arch() -> &str,
76	/// The Index Type of the PackageFile. Known values are:
77	///
78	/// Debian Package Index, Debian Translation Index
79	/// and Debian dpkg status file,
80	index_type() -> &str,
81);
82
83#[cxx::bridge]
84pub(crate) mod raw {
85	unsafe extern "C++" {
86		include!("rust-apt/apt-pkg-c/package.h");
87
88		type VerFileIterator;
89		type DescIterator;
90		type PkgFileIterator;
91
92		/// The path to the PackageFile
93		pub fn filename(self: &PkgFileIterator) -> Result<&str>;
94
95		/// The Archive of the PackageFile. ex: unstable
96		pub fn archive(self: &PkgFileIterator) -> Result<&str>;
97
98		/// The Origin of the PackageFile. ex: Debian
99		pub fn origin(self: &PkgFileIterator) -> Result<&str>;
100
101		/// The Codename of the PackageFile. ex: main, non-free
102		pub fn codename(self: &PkgFileIterator) -> Result<&str>;
103
104		/// The Label of the PackageFile. ex: Debian
105		pub fn label(self: &PkgFileIterator) -> Result<&str>;
106
107		/// The Hostname of the PackageFile. ex: deb.debian.org
108		pub fn site(self: &PkgFileIterator) -> Result<&str>;
109
110		/// The Component of the PackageFile. ex: sid
111		pub fn component(self: &PkgFileIterator) -> Result<&str>;
112
113		/// The Architecture of the PackageFile. ex: amd64
114		pub fn arch(self: &PkgFileIterator) -> Result<&str>;
115
116		/// The Index Type of the PackageFile. Known values are:
117		///
118		/// Debian Package Index, Debian Translation Index, Debian dpkg status
119		/// file,
120		pub fn index_type(self: &PkgFileIterator) -> Result<&str>;
121
122		/// `true` if the PackageFile contains packages that can be downloaded
123		pub fn is_downloadable(self: &PkgFileIterator) -> bool;
124
125		/// The Index number of the PackageFile
126		#[cxx_name = "Index"]
127		pub fn index(self: &PkgFileIterator) -> u64;
128		/// Clone the pointer.
129		///
130		/// # Safety
131		///
132		/// If the inner pointer is null segfaults can occur.
133		///
134		/// Using [`crate::raw::IntoRawIter::make_safe`] to convert to an Option
135		/// is recommended.
136		///
137		/// The returned UniquePtr cannot outlive the cache.
138		unsafe fn unique(self: &PkgFileIterator) -> UniquePtr<PkgFileIterator>;
139		pub fn raw_next(self: Pin<&mut PkgFileIterator>);
140		pub fn end(self: &PkgFileIterator) -> bool;
141
142		/// Return the package file associated with this version file.
143		///
144		/// # Safety
145		///
146		/// If the inner pointer is null segfaults can occur.
147		///
148		/// Using [`crate::raw::IntoRawIter::make_safe`] to convert to an Option
149		/// is recommended.
150		///
151		/// The returned UniquePtr cannot outlive the cache.
152		unsafe fn package_file(self: &VerFileIterator) -> UniquePtr<PkgFileIterator>;
153
154		#[cxx_name = "Index"]
155		pub fn index(self: &VerFileIterator) -> u64;
156		/// Clone the pointer.
157		///
158		/// # Safety
159		///
160		/// If the inner pointer is null segfaults can occur.
161		///
162		/// Using [`crate::raw::IntoRawIter::make_safe`] to convert to an Option
163		/// is recommended.
164		///
165		/// The returned UniquePtr cannot outlive the cache.
166		unsafe fn unique(self: &VerFileIterator) -> UniquePtr<VerFileIterator>;
167		pub fn raw_next(self: Pin<&mut VerFileIterator>);
168		pub fn end(self: &VerFileIterator) -> bool;
169
170		#[cxx_name = "Index"]
171		pub fn index(self: &DescIterator) -> u64;
172		/// Clone the pointer.
173		///
174		/// # Safety
175		///
176		/// If the inner pointer is null segfaults can occur.
177		///
178		/// Using [`crate::raw::IntoRawIter::make_safe`] to convert to an Option
179		/// is recommended.
180		///
181		/// The returned UniquePtr cannot outlive the cache.
182		unsafe fn unique(self: &DescIterator) -> UniquePtr<DescIterator>;
183		pub fn raw_next(self: Pin<&mut DescIterator>);
184		pub fn end(self: &DescIterator) -> bool;
185	}
186}