alpm_srcinfo/source_info/package_base.rs
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
//! Handling of metadata found in the `pkgbase` section of SRCINFO data.
use std::collections::{HashMap, HashSet};
use alpm_types::{
Architecture,
Epoch,
License,
MakepkgOption,
Name,
OpenPGPIdentifier,
OptionalDependency,
PackageDescription,
PackageRelation,
PackageRelease,
PackageVersion,
RelativePath,
SkippableChecksum,
Source,
Url,
Version,
digests::{Blake2b512, Md5, Sha1, Sha224, Sha256, Sha384, Sha512},
};
use super::{
lints::{
duplicate_architecture,
missing_architecture_for_property,
non_spdx_license,
unsafe_checksum,
},
package::PackageArchitecture,
relation::RelationOrSoname,
};
use crate::{
error::{SourceInfoError, lint, unrecoverable},
parser::{self, PackageBaseProperty, RawPackageBase, SharedMetaProperty},
};
#[cfg(doc)]
use crate::{
merged::MergedPackage,
source_info::{Package, SourceInfo},
};
/// Package base metadata based on the `pkgbase` section in SRCINFO data.
///
/// All values in this struct act as default values for all [`Package`]s in the scope of specific
/// SRCINFO data.
///
/// A [`MergedPackage`] (a full view on a package's metadata) can be created using
/// [`SourceInfo::packages_for_architecture`].
#[derive(Debug, Clone)]
pub struct PackageBase {
pub name: Name,
pub description: Option<PackageDescription>,
pub url: Option<Url>,
pub changelog: Option<RelativePath>,
pub licenses: Vec<License>,
// Build or package management related meta fields
pub install: Option<RelativePath>,
pub groups: Vec<String>,
pub options: Vec<MakepkgOption>,
pub backups: Vec<RelativePath>,
// These metadata fields are PackageBase specific
pub version: Version,
pub pgp_fingerprints: Vec<OpenPGPIdentifier>,
// Architectures and architecture specific properties
pub architectures: HashSet<Architecture>,
pub architecture_properties: HashMap<Architecture, PackageBaseArchitecture>,
pub dependencies: Vec<RelationOrSoname>,
pub optional_dependencies: Vec<OptionalDependency>,
pub provides: Vec<RelationOrSoname>,
pub conflicts: Vec<PackageRelation>,
pub replaces: Vec<PackageRelation>,
// The following dependencies are build-time specific dependencies.
// `makepkg` expects all dependencies for all split packages to be specified in the
// PackageBase.
pub check_dependencies: Vec<PackageRelation>,
pub make_dependencies: Vec<PackageRelation>,
pub sources: Vec<Source>,
pub no_extracts: Vec<String>,
pub b2_checksums: Vec<SkippableChecksum<Blake2b512>>,
pub md5_checksums: Vec<SkippableChecksum<Md5>>,
pub sha1_checksums: Vec<SkippableChecksum<Sha1>>,
pub sha224_checksums: Vec<SkippableChecksum<Sha224>>,
pub sha256_checksums: Vec<SkippableChecksum<Sha256>>,
pub sha384_checksums: Vec<SkippableChecksum<Sha384>>,
pub sha512_checksums: Vec<SkippableChecksum<Sha512>>,
}
/// Architecture specific package base properties for use in [`PackageBase`].
///
/// For each [`Architecture`] defined in [`PackageBase::architectures`] a
/// [`PackageBaseArchitecture`] is present in [`PackageBase::architecture_properties`].
#[derive(Default, Debug, Clone)]
pub struct PackageBaseArchitecture {
pub dependencies: Vec<RelationOrSoname>,
pub optional_dependencies: Vec<OptionalDependency>,
pub provides: Vec<RelationOrSoname>,
pub conflicts: Vec<PackageRelation>,
pub replaces: Vec<PackageRelation>,
// The following dependencies are build-time specific dependencies.
// `makepkg` expects all dependencies for all split packages to be specified in the
// PackageBase.
pub check_dependencies: Vec<PackageRelation>,
pub make_dependencies: Vec<PackageRelation>,
pub sources: Vec<Source>,
pub no_extracts: Vec<String>,
pub b2_checksums: Vec<SkippableChecksum<Blake2b512>>,
pub md5_checksums: Vec<SkippableChecksum<Md5>>,
pub sha1_checksums: Vec<SkippableChecksum<Sha1>>,
pub sha224_checksums: Vec<SkippableChecksum<Sha224>>,
pub sha256_checksums: Vec<SkippableChecksum<Sha256>>,
pub sha384_checksums: Vec<SkippableChecksum<Sha384>>,
pub sha512_checksums: Vec<SkippableChecksum<Sha512>>,
}
impl PackageBaseArchitecture {
/// Merges in the architecture specific properties of a package.
///
/// Each existing field of `properties` overrides the architecture-independent pendant on
/// `self`.
pub fn merge_package_properties(&mut self, properties: PackageArchitecture) {
if let Some(dependencies) = properties.dependencies {
self.dependencies = dependencies;
}
if let Some(optional_dependencies) = properties.optional_dependencies {
self.optional_dependencies = optional_dependencies;
}
if let Some(provides) = properties.provides {
self.provides = provides;
}
if let Some(conflicts) = properties.conflicts {
self.conflicts = conflicts;
}
if let Some(replaces) = properties.replaces {
self.replaces = replaces;
}
}
}
/// Handles all potentially architecture specific Vector entries in the [`PackageBase::from_parsed`]
/// function.
///
/// If no architecture is encountered, it simply adds the value on the [`PackageBase`] itself.
/// Otherwise, it's added to the respective [`PackageBase::architecture_properties`].
///
/// Furthermore, adds linter warnings if an architecture is encountered that doesn't exist in the
/// [`PackageBase::architectures`].
macro_rules! package_base_arch_prop {
(
$line:ident,
$errors:ident,
$architectures:ident,
$architecture_properties:ident,
$arch_property:ident,
$field_name:ident,
) => {
// Check if the property is architecture specific.
// If so, we have to perform some checks and preparation
if let Some(architecture) = $arch_property.architecture {
// Make sure the architecture specific properties are initialized.
let architecture_properties = $architecture_properties
.entry(architecture)
.or_insert(PackageBaseArchitecture::default());
// Set the architecture specific value.
architecture_properties
.$field_name
.push($arch_property.value);
// Throw an error for all architecture specific properties that don't have
// an explicit `arch` statement. This is considered bad style.
// Also handle the special `Any` [Architecture], which allows all architectures.
if !$architectures.contains(&architecture)
&& !$architectures.contains(&Architecture::Any)
{
missing_architecture_for_property($errors, $line, architecture);
}
} else {
$field_name.push($arch_property.value)
}
};
}
impl PackageBase {
/// Creates a new [`PackageBase`] instance from a [`RawPackageBase`].
///
/// # Parameters
///
/// - `line_start`: The number of preceding lines, so that error/lint messages can reference the
/// correct lines.
/// - `parsed`: The [`RawPackageBase`] representation of the SRCINFO data. The input guarantees
/// that the keyword definitions have been parsed correctly, but not yet that they represent
/// valid SRCINFO data as a whole.
/// - `errors`: All errors and lints encountered during the creation of the [`PackageBase`].
///
/// # Errors
///
/// This function does not return a [`Result`], but instead relies on aggregating all lints,
/// warnings and errors in `errors`. This allows to keep the function call recoverable, so
/// that all errors and lints can be returned all at once.
pub fn from_parsed(
line_start: usize,
parsed: RawPackageBase,
errors: &mut Vec<SourceInfoError>,
) -> Self {
let mut description = None;
let mut url = None;
let mut licenses = Vec::new();
let mut changelog = None;
let mut architectures = HashSet::new();
let mut architecture_properties = HashMap::new();
// Build or package management related meta fields
let mut install = None;
let mut groups = Vec::new();
let mut options = Vec::new();
let mut backups = Vec::new();
// These metadata fields are PackageBase specific
// This one is expected!
let mut package_release: Option<PackageRelease> = None;
// This one is optional.
let mut package_epoch: Option<Epoch> = None;
let mut package_version: Option<PackageVersion> = None;
let mut pgp_fingerprints = Vec::new();
let mut dependencies = Vec::new();
let mut optional_dependencies = Vec::new();
let mut provides = Vec::new();
let mut conflicts = Vec::new();
let mut replaces = Vec::new();
// The following dependencies are build-time specific dependencies.
// `makepkg` expects all dependencies for all split packages to be specified in the
// PackageBase.
let mut check_dependencies = Vec::new();
let mut make_dependencies = Vec::new();
let mut sources = Vec::new();
let mut no_extracts = Vec::new();
let mut b2_checksums = Vec::new();
let mut md5_checksums = Vec::new();
let mut sha1_checksums = Vec::new();
let mut sha224_checksums = Vec::new();
let mut sha256_checksums = Vec::new();
let mut sha384_checksums = Vec::new();
let mut sha512_checksums = Vec::new();
// First up check all input for potential architecture declarations.
// We need this to do proper linting when doing our actual pass through the file.
for (index, prop) in parsed.properties.iter().enumerate() {
// We're only interested in architecture properties.
let PackageBaseProperty::MetaProperty(SharedMetaProperty::Architecture(architecture)) =
prop
else {
continue;
};
// Calculate the actual line in the document based on any preceding lines.
// We have to add one, as lines aren't 0 indexed.
let line = index + line_start;
// Lint to make sure there aren't duplicate architectures declarations.
if architectures.contains(architecture) {
duplicate_architecture(errors, line, *architecture);
}
// Add the architecture in case it hasn't already.
architectures.insert(*architecture);
architecture_properties
.entry(*architecture)
.or_insert(PackageBaseArchitecture::default());
}
// If no architecture is set, `makepkg` simply uses the host system as the default value.
// In practice this translates to `any`, as this package is valid to be build on any
// system as long as `makepkg` is executed on that system.
if architectures.is_empty() {
errors.push(lint(
None,
"No architecture has been specified. Assuming `any`.",
));
architectures.insert(Architecture::Any);
architecture_properties
.entry(Architecture::Any)
.or_insert(PackageBaseArchitecture::default());
}
for (index, prop) in parsed.properties.into_iter().enumerate() {
// Calculate the actual line in the document based on any preceding lines.
let line = index + line_start;
match prop {
// Skip empty lines and comments
PackageBaseProperty::EmptyLine | PackageBaseProperty::Comment(_) => continue,
PackageBaseProperty::PackageVersion(inner) => package_version = Some(inner),
PackageBaseProperty::PackageRelease(inner) => package_release = Some(inner),
PackageBaseProperty::PackageEpoch(inner) => package_epoch = Some(inner),
PackageBaseProperty::ValidPgpKeys(inner) => {
if let OpenPGPIdentifier::OpenPGPKeyId(_) = &inner {
errors.push(lint(
Some(line),
concat!(
"OpenPGP Key IDs are highly discouraged, as the length doesn't guarantee uniqueness.",
"\nUse an OpenPGP v4 fingerprint instead.",
)
));
}
pgp_fingerprints.push(inner);
}
PackageBaseProperty::CheckDependency(arch_property) => {
package_base_arch_prop!(
line,
errors,
architectures,
architecture_properties,
arch_property,
check_dependencies,
)
}
PackageBaseProperty::MakeDependency(arch_property) => {
package_base_arch_prop!(
line,
errors,
architectures,
architecture_properties,
arch_property,
make_dependencies,
)
}
PackageBaseProperty::MetaProperty(shared_meta_property) => {
match shared_meta_property {
SharedMetaProperty::Description(inner) => description = Some(inner),
SharedMetaProperty::Url(inner) => url = Some(inner),
SharedMetaProperty::License(inner) => {
// Create lints for non-spdx licenses.
if let License::Unknown(_) = &inner {
non_spdx_license(errors, line, inner.to_string());
}
licenses.push(inner)
}
// We already handled those above.
SharedMetaProperty::Architecture(_) => continue,
SharedMetaProperty::Changelog(inner) => changelog = Some(inner),
SharedMetaProperty::Install(inner) => install = Some(inner),
SharedMetaProperty::Group(inner) => groups.push(inner),
SharedMetaProperty::Option(inner) => options.push(inner),
SharedMetaProperty::Backup(inner) => backups.push(inner),
}
}
PackageBaseProperty::RelationProperty(relation_property) => match relation_property
{
parser::RelationProperty::Dependency(arch_property) => package_base_arch_prop!(
line,
errors,
architectures,
architecture_properties,
arch_property,
dependencies,
),
parser::RelationProperty::OptionalDependency(arch_property) => {
package_base_arch_prop!(
line,
errors,
architectures,
architecture_properties,
arch_property,
optional_dependencies,
)
}
parser::RelationProperty::Provides(arch_property) => package_base_arch_prop!(
line,
errors,
architectures,
architecture_properties,
arch_property,
provides,
),
parser::RelationProperty::Conflicts(arch_property) => package_base_arch_prop!(
line,
errors,
architectures,
architecture_properties,
arch_property,
conflicts,
),
parser::RelationProperty::Replaces(arch_property) => package_base_arch_prop!(
line,
errors,
architectures,
architecture_properties,
arch_property,
replaces,
),
},
PackageBaseProperty::SourceProperty(source_property) => match source_property {
parser::SourceProperty::Source(arch_property) => package_base_arch_prop!(
line,
errors,
architectures,
architecture_properties,
arch_property,
sources,
),
parser::SourceProperty::NoExtract(arch_property) => package_base_arch_prop!(
line,
errors,
architectures,
architecture_properties,
arch_property,
no_extracts,
),
parser::SourceProperty::B2Checksum(arch_property) => package_base_arch_prop!(
line,
errors,
architectures,
architecture_properties,
arch_property,
b2_checksums,
),
parser::SourceProperty::Md5Checksum(arch_property) => {
unsafe_checksum(errors, line, "md5");
package_base_arch_prop!(
line,
errors,
architectures,
architecture_properties,
arch_property,
md5_checksums,
);
}
parser::SourceProperty::Sha1Checksum(arch_property) => {
unsafe_checksum(errors, line, "sha1");
package_base_arch_prop!(
line,
errors,
architectures,
architecture_properties,
arch_property,
sha1_checksums,
);
}
parser::SourceProperty::Sha224Checksum(arch_property) => {
package_base_arch_prop!(
line,
errors,
architectures,
architecture_properties,
arch_property,
sha224_checksums,
)
}
parser::SourceProperty::Sha256Checksum(arch_property) => {
package_base_arch_prop!(
line,
errors,
architectures,
architecture_properties,
arch_property,
sha256_checksums,
)
}
parser::SourceProperty::Sha384Checksum(arch_property) => {
package_base_arch_prop!(
line,
errors,
architectures,
architecture_properties,
arch_property,
sha384_checksums,
)
}
parser::SourceProperty::Sha512Checksum(arch_property) => {
package_base_arch_prop!(
line,
errors,
architectures,
architecture_properties,
arch_property,
sha512_checksums,
)
}
},
}
}
// Handle a missing package_version
if package_version.is_none() {
errors.push(unrecoverable(
None,
"pkgbase section doesn't contain a 'pkgver' keyword assignment",
));
// Set a package version nevertheless, so we continue parsing the rest of the file.
package_version = Some(PackageVersion::new("0".to_string()).unwrap());
}
PackageBase {
name: parsed.name,
description,
url,
licenses,
changelog,
architectures,
architecture_properties,
install,
groups,
options,
backups,
version: Version::new(package_version.unwrap(), package_epoch, package_release),
pgp_fingerprints,
dependencies,
optional_dependencies,
provides,
conflicts,
replaces,
check_dependencies,
make_dependencies,
sources,
no_extracts,
b2_checksums,
md5_checksums,
sha1_checksums,
sha224_checksums,
sha256_checksums,
sha384_checksums,
sha512_checksums,
}
}
}