Skip to main content

pkg_cache_config/
config_lib_rust_config_lib_source.rs

1use fidl::unpersist;
2use fidl_cf_sc_internal_pkgcacheconfig::Config as FidlConfig;
3use fuchsia_component_config::{Config as ComponentConfig, Error};
4use fuchsia_inspect::Node;
5use std::convert::TryInto;
6const EXPECTED_CHECKSUM: &[u8] = &[
7    0x9c, 0xba, 0xe0, 0xef, 0xec, 0x84, 0x26, 0x92, 0x10, 0x08, 0x68, 0x6c, 0x82, 0x23, 0x64, 0xc4,
8    0x29, 0x04, 0xe8, 0x19, 0xb2, 0x9a, 0xa9, 0x5c, 0x85, 0xe4, 0x9b, 0x37, 0x0f, 0x7f, 0xa4, 0x58,
9];
10const EXPECTED_CHECKSUM_LENGTH: [u8; 2] = (EXPECTED_CHECKSUM.len() as u16).to_le_bytes();
11#[derive(Debug)]
12pub struct Config {
13    pub all_packages_executable: bool,
14    pub blob_download_resumption_attempts_limit: u32,
15    pub blob_fetch_concurrency_limit: u16,
16    pub blob_network_body_timeout_seconds: u32,
17    pub blob_network_header_timeout_seconds: u32,
18    pub enable_upgradable_packages: bool,
19    pub require_system_image: bool,
20    pub system_image_hash: String,
21    pub use_driver_blob_paging: bool,
22}
23impl Config {
24    #[doc = r" Take the config startup handle and parse its contents."]
25    #[doc = r""]
26    #[doc = r" # Panics"]
27    #[doc = r""]
28    #[doc = r" If the config startup handle was already taken or if it is not valid."]
29    pub fn take_from_startup_handle() -> Self {
30        <Self as ComponentConfig>::take_from_startup_handle()
31    }
32    #[doc = r" Parse `Self` from `vmo`."]
33    pub fn from_vmo(vmo: &zx::Vmo) -> Result<Self, Error> {
34        <Self as ComponentConfig>::from_vmo(vmo)
35    }
36    #[doc = r" Parse `Self` from `bytes`."]
37    pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
38        <Self as ComponentConfig>::from_bytes(bytes)
39    }
40    pub fn record_inspect(&self, inspector_node: &Node) {
41        <Self as ComponentConfig>::record_inspect(self, inspector_node)
42    }
43}
44impl ComponentConfig for Config {
45    #[doc = r" Parse `Self` from `bytes`."]
46    fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
47        let (checksum_len_bytes, bytes) = bytes.split_at_checked(2).ok_or(Error::TooFewBytes)?;
48        let checksum_len_bytes: [u8; 2] =
49            checksum_len_bytes.try_into().expect("previous call guaranteed 2 element slice");
50        let checksum_length = u16::from_le_bytes(checksum_len_bytes) as usize;
51        let (observed_checksum, bytes) =
52            bytes.split_at_checked(checksum_length).ok_or(Error::TooFewBytes)?;
53        if observed_checksum != EXPECTED_CHECKSUM {
54            return Err(Error::ChecksumMismatch {
55                expected_checksum: EXPECTED_CHECKSUM.to_vec(),
56                observed_checksum: observed_checksum.to_vec(),
57            });
58        }
59        let fidl_config: FidlConfig = unpersist(bytes).map_err(Error::Unpersist)?;
60        Ok(Self {
61            all_packages_executable: fidl_config.all_packages_executable,
62            blob_download_resumption_attempts_limit: fidl_config
63                .blob_download_resumption_attempts_limit,
64            blob_fetch_concurrency_limit: fidl_config.blob_fetch_concurrency_limit,
65            blob_network_body_timeout_seconds: fidl_config.blob_network_body_timeout_seconds,
66            blob_network_header_timeout_seconds: fidl_config.blob_network_header_timeout_seconds,
67            enable_upgradable_packages: fidl_config.enable_upgradable_packages,
68            require_system_image: fidl_config.require_system_image,
69            system_image_hash: fidl_config.system_image_hash,
70            use_driver_blob_paging: fidl_config.use_driver_blob_paging,
71        })
72    }
73    fn to_bytes(&self) -> Result<Vec<u8>, Error> {
74        let fidl_config = FidlConfig {
75            all_packages_executable: self.all_packages_executable.clone(),
76            blob_download_resumption_attempts_limit: self
77                .blob_download_resumption_attempts_limit
78                .clone(),
79            blob_fetch_concurrency_limit: self.blob_fetch_concurrency_limit.clone(),
80            blob_network_body_timeout_seconds: self.blob_network_body_timeout_seconds.clone(),
81            blob_network_header_timeout_seconds: self.blob_network_header_timeout_seconds.clone(),
82            enable_upgradable_packages: self.enable_upgradable_packages.clone(),
83            require_system_image: self.require_system_image.clone(),
84            system_image_hash: self.system_image_hash.clone(),
85            use_driver_blob_paging: self.use_driver_blob_paging.clone(),
86        };
87        let mut fidl_bytes = fidl::persist(&fidl_config).map_err(Error::Persist)?;
88        let mut bytes = Vec::with_capacity(
89            EXPECTED_CHECKSUM_LENGTH.len() + EXPECTED_CHECKSUM.len() + fidl_bytes.len(),
90        );
91        bytes.extend_from_slice(&EXPECTED_CHECKSUM_LENGTH);
92        bytes.extend_from_slice(EXPECTED_CHECKSUM);
93        bytes.append(&mut fidl_bytes);
94        Ok(bytes)
95    }
96    fn record_inspect(&self, inspector_node: &Node) {
97        inspector_node.record_bool("all_packages_executable", self.all_packages_executable);
98        inspector_node.record_uint(
99            "blob_download_resumption_attempts_limit",
100            self.blob_download_resumption_attempts_limit as u64,
101        );
102        inspector_node
103            .record_uint("blob_fetch_concurrency_limit", self.blob_fetch_concurrency_limit as u64);
104        inspector_node.record_uint(
105            "blob_network_body_timeout_seconds",
106            self.blob_network_body_timeout_seconds as u64,
107        );
108        inspector_node.record_uint(
109            "blob_network_header_timeout_seconds",
110            self.blob_network_header_timeout_seconds as u64,
111        );
112        inspector_node.record_bool("enable_upgradable_packages", self.enable_upgradable_packages);
113        inspector_node.record_bool("require_system_image", self.require_system_image);
114        inspector_node.record_string("system_image_hash", &self.system_image_hash);
115        inspector_node.record_bool("use_driver_blob_paging", self.use_driver_blob_paging);
116    }
117}