alpm_srcinfo/source_info/v1/
merged.rs

1//! Provides fully resolved package metadata derived from SRCINFO data.
2use alpm_types::{
3    Architecture,
4    Architectures,
5    FullVersion,
6    License,
7    MakepkgOption,
8    Name,
9    OpenPGPIdentifier,
10    OptionalDependency,
11    PackageDescription,
12    PackageRelation,
13    RelationOrSoname,
14    RelativeFilePath,
15    SkippableChecksum,
16    Source,
17    Url,
18    digests::{Blake2b512, Crc32Cksum, Md5, Sha1, Sha224, Sha256, Sha384, Sha512},
19};
20use serde::{Deserialize, Serialize};
21
22#[cfg(doc)]
23use crate::source_info::v1::package::Override;
24use crate::{
25    SourceInfoV1,
26    source_info::v1::{
27        package::Package,
28        package_base::{PackageBase, PackageBaseArchitecture},
29    },
30};
31
32/// Fully resolved metadata of a single package based on SRCINFO data.
33///
34/// This struct incorporates all [`PackageBase`] properties and the [`Package`] specific overrides
35/// in an architecture-specific representation of a package. It can be created using
36/// [`SourceInfoV1::packages_for_architecture`].
37#[derive(Clone, Debug, Deserialize, Serialize)]
38pub struct MergedPackage {
39    /// The alpm-package-name for the package.
40    pub name: Name,
41    /// The description for the package.
42    pub description: Option<PackageDescription>,
43    /// The upstream URL for the package.
44    pub url: Option<Url>,
45    /// The list of licenses that apply to the package.
46    pub licenses: Vec<License>,
47    /// The alpm-architecture for the package.
48    pub architecture: Architecture,
49    /// The optional relative path to a changelog file for the package.
50    pub changelog: Option<RelativeFilePath>,
51
52    // Build or package management related meta fields
53    /// The optional relative path to an alpm-install-scriptlet for the package.
54    pub install: Option<RelativeFilePath>,
55    /// The list of alpm-package-groups the package is part of.
56    pub groups: Vec<String>,
57    /// The list of build tool options used when builidng the package.
58    pub options: Vec<MakepkgOption>,
59    /// The list of relative paths to files in the package that should be backed up.
60    pub backups: Vec<RelativeFilePath>,
61
62    /// The full version of the package.
63    pub version: FullVersion,
64    /// The list of OpenPGP fingerprints of OpenPGP certificates used for the verification of
65    /// upstream sources.
66    pub pgp_fingerprints: Vec<OpenPGPIdentifier>,
67
68    /// The list of run-time dependencies.
69    pub dependencies: Vec<RelationOrSoname>,
70    /// The list of optional dependencies.
71    pub optional_dependencies: Vec<OptionalDependency>,
72    /// The list of provisions.
73    pub provides: Vec<RelationOrSoname>,
74    /// The list of conflicts.
75    pub conflicts: Vec<PackageRelation>,
76    /// The list of replacements.
77    pub replaces: Vec<PackageRelation>,
78    /// The list of test dependencies.
79    pub check_dependencies: Vec<PackageRelation>,
80    /// The list of build dependencies.
81    pub make_dependencies: Vec<PackageRelation>,
82
83    /// The list of sources for the package.
84    pub sources: Vec<MergedSource>,
85    /// The list of sources for the package that are not extracted.
86    pub no_extracts: Vec<String>,
87}
88
89/// An iterator over all packages of a specific architecture.
90#[derive(Clone, Debug)]
91pub struct MergedPackagesIterator<'a> {
92    pub(crate) architecture: Architecture,
93    pub(crate) source_info: &'a SourceInfoV1,
94    pub(crate) package_iterator: std::slice::Iter<'a, Package>,
95}
96
97impl Iterator for MergedPackagesIterator<'_> {
98    type Item = MergedPackage;
99
100    fn next(&mut self) -> Option<MergedPackage> {
101        // Search for the next package that is valid for the the architecture we're looping over.
102        let package = self.package_iterator.find(|package| {
103            // If the package provides target architecture overrides, use those, otherwise
104            // fallback to package base architectures.
105            let architectures = match &package.architectures {
106                Some(value) => value,
107                None => &self.source_info.base.architectures,
108            };
109
110            match &self.architecture {
111                // If the packages are filtered by `any`, make sure that the package also has `any`.
112                Architecture::Any => *architectures == Architectures::Any,
113                // A specific architecture has been requested.
114                // The package must have that architecture in its list or be viable for `any`
115                // architecture.
116                Architecture::Some(iterator_arch) => match architectures {
117                    Architectures::Any => true,
118                    Architectures::Some(arch_vec) => arch_vec.contains(iterator_arch),
119                },
120            }
121        })?;
122
123        Some(MergedPackage::from_base_and_package(
124            self.architecture.clone(),
125            &self.source_info.base,
126            package,
127        ))
128    }
129}
130
131/// A merged representation of source related information.
132///
133/// SRCINFO provides this info as separate lists. This struct resolves that list representation and
134/// provides a convenient aggregated representation for a single source.
135#[derive(Clone, Debug, Deserialize, Serialize)]
136pub struct MergedSource {
137    /// The source.
138    pub source: Source,
139    /// The optional Blake2 hash digest of `source`.
140    pub b2_checksum: Option<SkippableChecksum<Blake2b512>>,
141    /// The optional MD-5 hash digest of `source`.
142    pub md5_checksum: Option<SkippableChecksum<Md5>>,
143    /// The optional SHA-1 hash digest of `source`.
144    pub sha1_checksum: Option<SkippableChecksum<Sha1>>,
145    /// The optional SHA-224 hash digest of `source`.
146    pub sha224_checksum: Option<SkippableChecksum<Sha224>>,
147    /// The optional SHA-256 hash digest of `source`.
148    pub sha256_checksum: Option<SkippableChecksum<Sha256>>,
149    /// The optional SHA-384 hash digest of `source`.
150    pub sha384_checksum: Option<SkippableChecksum<Sha384>>,
151    /// The optional SHA-512 hash digest of `source`.
152    pub sha512_checksum: Option<SkippableChecksum<Sha512>>,
153    /// The optional CRC-32/CKSUM hash digest of `source`.
154    pub crc_checksum: Option<SkippableChecksum<Crc32Cksum>>,
155}
156
157/// A convenience iterator to build a list of [`MergedSource`] from the disjoint vectors of sources
158/// and digests.
159///
160/// The checksums and sources are by convention all in the same order, which makes this quite
161/// convenient to convert into a aggregated struct representation.
162#[derive(Clone, Debug)]
163pub struct MergedSourceIterator<'a> {
164    sources: std::slice::Iter<'a, Source>,
165    b2_checksums: std::slice::Iter<'a, SkippableChecksum<Blake2b512>>,
166    md5_checksums: std::slice::Iter<'a, SkippableChecksum<Md5>>,
167    sha1_checksums: std::slice::Iter<'a, SkippableChecksum<Sha1>>,
168    sha224_checksums: std::slice::Iter<'a, SkippableChecksum<Sha224>>,
169    sha256_checksums: std::slice::Iter<'a, SkippableChecksum<Sha256>>,
170    sha384_checksums: std::slice::Iter<'a, SkippableChecksum<Sha384>>,
171    sha512_checksums: std::slice::Iter<'a, SkippableChecksum<Sha512>>,
172    crc_checksums: std::slice::Iter<'a, SkippableChecksum<Crc32Cksum>>,
173}
174
175impl Iterator for MergedSourceIterator<'_> {
176    type Item = MergedSource;
177
178    fn next(&mut self) -> Option<MergedSource> {
179        let source = self.sources.next()?;
180
181        Some(MergedSource {
182            source: source.clone(),
183            b2_checksum: self.b2_checksums.next().cloned(),
184            md5_checksum: self.md5_checksums.next().cloned(),
185            sha1_checksum: self.sha1_checksums.next().cloned(),
186            sha224_checksum: self.sha224_checksums.next().cloned(),
187            sha256_checksum: self.sha256_checksums.next().cloned(),
188            sha384_checksum: self.sha384_checksums.next().cloned(),
189            sha512_checksum: self.sha512_checksums.next().cloned(),
190            crc_checksum: self.crc_checksums.next().cloned(),
191        })
192    }
193}
194
195impl MergedPackage {
196    /// Creates the fully resolved, architecture-specific metadata representation of a package.
197    ///
198    /// Takes an [`Architecture`] (which defines the architecture for which to create the
199    /// representation), as well as a [`PackageBase`] and a [`Package`] (from which to derive the
200    /// metadata).
201    ///
202    /// The metadata representation is created using the following steps:
203    /// 1. [`MergedPackage::from_base`] is called to create a basic representation of a
204    ///    [`MergedPackage`] based on the default values in [`PackageBase`].
205    /// 2. All architecture-agnostic fields of the [`Package`] are merged into the
206    ///    [`MergedPackage`].
207    /// 3. The architecture-specific properties of the [`PackageBase`] and [`Package`] are
208    ///    extracted.
209    /// 4. [`PackageBaseArchitecture::merge_package_properties`] is called to merge the
210    ///    architecture-specific properties of the [`Package`] into those of the [`PackageBase`].
211    /// 5. The combined architecture-specific properties are merged into the [`MergedPackage`].
212    pub fn from_base_and_package<A: Into<Architecture>>(
213        architecture: A,
214        base: &PackageBase,
215        package: &Package,
216    ) -> MergedPackage {
217        let name = package.name.clone();
218        let architecture = &architecture.into();
219
220        // Step 1
221        let mut merged_package = Self::from_base(architecture.clone(), name, base);
222
223        // Step 2
224        merged_package.merge_package(package);
225
226        // Get the architecture specific properties from the PackageBase.
227        // Use an empty default without any properties as default if none are found,
228        // or when the architecture is 'any'.
229        let mut architecture_properties = if let Architecture::Some(system_arch) = &architecture
230            && let Some(properties) = base.architecture_properties.get(system_arch)
231        {
232            properties.clone()
233        } else {
234            PackageBaseArchitecture::default()
235        };
236
237        // Apply package specific overrides for architecture specific properties.
238        if let Architecture::Some(system_arch) = architecture
239            && let Some(package_properties) = package.architecture_properties.get(system_arch)
240        {
241            architecture_properties.merge_package_properties(package_properties.clone());
242        }
243
244        // Merge the architecture specific properties into the final MergedPackage.
245        merged_package.merge_architecture_properties(&architecture_properties);
246
247        merged_package
248    }
249
250    /// Creates a basic, architecture-specific, but incomplete [`MergedPackage`].
251    ///
252    /// Takes an [`Architecture`] (which defines the architecture for which to create the
253    /// representation), a [`Name`] which defines the name of the package and a [`PackageBase`]
254    /// which provides the initial data.
255    ///
256    /// # Note
257    ///
258    /// The returned [`MergedPackage`] is not complete, as it neither contains package-specific nor
259    /// architecture-specific overrides for its fields.
260    /// Use [`from_base_and_package`](MergedPackage::from_base_and_package) to create a fully
261    /// resolved representation of a package.
262    pub fn from_base<A: Into<Architecture>>(
263        architecture: A,
264        name: Name,
265        base: &PackageBase,
266    ) -> MergedPackage {
267        // Merge all source related info into aggregated structs.
268        let merged_sources = MergedSourceIterator {
269            sources: base.sources.iter(),
270            b2_checksums: base.b2_checksums.iter(),
271            md5_checksums: base.md5_checksums.iter(),
272            sha1_checksums: base.sha1_checksums.iter(),
273            sha224_checksums: base.sha224_checksums.iter(),
274            sha256_checksums: base.sha256_checksums.iter(),
275            sha384_checksums: base.sha384_checksums.iter(),
276            sha512_checksums: base.sha512_checksums.iter(),
277            crc_checksums: base.crc_checksums.iter(),
278        };
279
280        // If the [`PackageBase`] is compatible with any architecture, then we set the architecture
281        // of the package to 'any' regardless of the requested architecture, as 'any' subsumes them
282        // all.
283        let architecture = match &base.architectures {
284            Architectures::Any => &Architecture::Any,
285            Architectures::Some(_) => &architecture.into(),
286        };
287
288        MergedPackage {
289            name,
290            description: base.description.clone(),
291            url: base.url.clone(),
292            licenses: base.licenses.clone(),
293            architecture: architecture.clone(),
294            changelog: base.changelog.clone(),
295            install: base.install.clone(),
296            groups: base.groups.clone(),
297            options: base.options.clone(),
298            backups: base.backups.clone(),
299            version: base.version.clone(),
300            pgp_fingerprints: base.pgp_fingerprints.clone(),
301            dependencies: base.dependencies.clone(),
302            optional_dependencies: base.optional_dependencies.clone(),
303            provides: base.provides.clone(),
304            conflicts: base.conflicts.clone(),
305            replaces: base.replaces.clone(),
306            check_dependencies: base.check_dependencies.clone(),
307            make_dependencies: base.make_dependencies.clone(),
308            sources: merged_sources.collect(),
309            no_extracts: base.no_extracts.clone(),
310        }
311    }
312
313    /// Merges the non-architecture specific fields of a [`Package`] into `self`.
314    ///
315    /// Any field on `package` that is not [`Override::No`] overrides the pendant on `self`.
316    fn merge_package(&mut self, package: &Package) {
317        let package = package.clone();
318
319        // If the [`Package`] is compatible with any architecture, then we set the architecture of
320        // the package to 'any' regardless of the requested architecture, as 'any' subsumes them
321        // all.
322        if let Some(value) = package.architectures {
323            if matches!(value, Architectures::Any) {
324                self.architecture = Architecture::Any
325            }
326        };
327
328        package.description.merge_option(&mut self.description);
329        package.url.merge_option(&mut self.url);
330        package.changelog.merge_option(&mut self.changelog);
331        package.licenses.merge_vec(&mut self.licenses);
332        package.install.merge_option(&mut self.install);
333        package.groups.merge_vec(&mut self.groups);
334        package.options.merge_vec(&mut self.options);
335        package.backups.merge_vec(&mut self.backups);
336        package.dependencies.merge_vec(&mut self.dependencies);
337        package
338            .optional_dependencies
339            .merge_vec(&mut self.optional_dependencies);
340        package.provides.merge_vec(&mut self.provides);
341        package.conflicts.merge_vec(&mut self.conflicts);
342        package.replaces.merge_vec(&mut self.replaces);
343    }
344
345    /// Merges in architecture-specific overrides for fields.
346    ///
347    /// Takes a [`PackageBaseArchitecture`] and extends the non-architecture specific values
348    /// with the architecture specific ones.
349    /// This is an accumulative and non-destructive operation.
350    fn merge_architecture_properties(&mut self, base_architecture: &PackageBaseArchitecture) {
351        // Merge all source related info into aggregated structs.
352        let merged_sources = MergedSourceIterator {
353            sources: base_architecture.sources.iter(),
354            b2_checksums: base_architecture.b2_checksums.iter(),
355            md5_checksums: base_architecture.md5_checksums.iter(),
356            sha1_checksums: base_architecture.sha1_checksums.iter(),
357            sha224_checksums: base_architecture.sha224_checksums.iter(),
358            sha256_checksums: base_architecture.sha256_checksums.iter(),
359            sha384_checksums: base_architecture.sha384_checksums.iter(),
360            sha512_checksums: base_architecture.sha512_checksums.iter(),
361            crc_checksums: base_architecture.crc_checksums.iter(),
362        };
363
364        self.dependencies
365            .extend_from_slice(&base_architecture.dependencies);
366        self.optional_dependencies
367            .extend_from_slice(&base_architecture.optional_dependencies);
368        self.provides.extend_from_slice(&base_architecture.provides);
369        self.conflicts
370            .extend_from_slice(&base_architecture.conflicts);
371        self.replaces.extend_from_slice(&base_architecture.replaces);
372        self.check_dependencies
373            .extend_from_slice(&base_architecture.check_dependencies);
374        self.make_dependencies
375            .extend_from_slice(&base_architecture.make_dependencies);
376
377        self.sources
378            .extend_from_slice(&merged_sources.collect::<Vec<MergedSource>>());
379    }
380}