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