1#![allow(clippy::let_unit_value)]
6
7mod anchored_packages;
8mod cache_packages;
9mod errors;
10mod path_hash_mapping;
11mod system_image;
12
13pub use crate::anchored_packages::AnchoredPackages;
14pub use crate::cache_packages::CachePackages;
15pub use crate::errors::{
16 AllowListError, CachePackagesInitError, PathHashMappingError, StaticPackagesInitError,
17};
18pub use crate::path_hash_mapping::{Bootfs, PathHashMapping, StaticPackages};
19pub use crate::system_image::{ExecutabilityRestrictions, SystemImage};
20
21static PKGFS_BOOT_ARG_KEY: &str = "zircon.system.pkgfs.cmd";
22static PKGFS_BOOT_ARG_VALUE_PREFIX: &str = "bin/pkgsvr+";
23
24pub fn get_system_image_hash(
25 system_image_hash: &str,
26) -> Result<fuchsia_hash::Hash, SystemImageHashError> {
27 let hash = system_image_hash
28 .strip_prefix(PKGFS_BOOT_ARG_VALUE_PREFIX)
29 .ok_or_else(|| SystemImageHashError::BadPrefix(system_image_hash.to_string()))?;
30 hash.parse().map_err(SystemImageHashError::BadHash)
31}
32
33#[derive(Debug, thiserror::Error)]
34pub enum SystemImageHashError {
35 #[error(
36 "boot arg for key {} does not start with {}: {:?}",
37 PKGFS_BOOT_ARG_KEY,
38 PKGFS_BOOT_ARG_VALUE_PREFIX,
39 .0
40 )]
41 BadPrefix(String),
42
43 #[error("boot arg for key {} has invalid hash {:?}", PKGFS_BOOT_ARG_KEY, .0)]
44 BadHash(#[source] fuchsia_hash::ParseHashError),
45}
46
47#[cfg(test)]
48mod test_get_system_image_hash {
49 use super::*;
50 use assert_matches::assert_matches;
51
52 #[test]
53 fn bad_prefix() {
54 assert_matches!(
55 get_system_image_hash("bad-prefix"),
56 Err(SystemImageHashError::BadPrefix(prefix)) if prefix == "bad-prefix"
57 );
58 }
59
60 #[test]
61 fn bad_hash() {
62 assert_matches!(
63 get_system_image_hash("bin/pkgsvr+bad-hash"),
64 Err(SystemImageHashError::BadHash(_))
65 );
66 }
67
68 #[test]
69 fn success() {
70 assert_eq!(
71 get_system_image_hash(
72 "bin/pkgsvr+0000000000000000000000000000000000000000000000000000000000000000"
73 )
74 .unwrap(),
75 fuchsia_hash::Hash::from([0; 32])
76 );
77 }
78}