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    0x5d, 0xa6, 0x5c, 0xa6, 0x27, 0x11, 0x34, 0x47, 0x32, 0x39, 0x0d, 0x55, 0x63, 0x4b, 0xbd, 0xf7,
8    0x9d, 0x3c, 0xa3, 0xb6, 0x83, 0xc8, 0x89, 0x56, 0xc0, 0xb4, 0x74, 0x91, 0xdc, 0xdc, 0xb3, 0x5f,
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}
22impl Config {
23    #[doc = r" Take the config startup handle and parse its contents."]
24    #[doc = r""]
25    #[doc = r" # Panics"]
26    #[doc = r""]
27    #[doc = r" If the config startup handle was already taken or if it is not valid."]
28    pub fn take_from_startup_handle() -> Self {
29        <Self as ComponentConfig>::take_from_startup_handle()
30    }
31    #[doc = r" Parse `Self` from `vmo`."]
32    pub fn from_vmo(vmo: &zx::Vmo) -> Result<Self, Error> {
33        <Self as ComponentConfig>::from_vmo(vmo)
34    }
35    #[doc = r" Parse `Self` from `bytes`."]
36    pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
37        <Self as ComponentConfig>::from_bytes(bytes)
38    }
39    pub fn record_inspect(&self, inspector_node: &Node) {
40        <Self as ComponentConfig>::record_inspect(self, inspector_node)
41    }
42}
43impl ComponentConfig for Config {
44    #[doc = r" Parse `Self` from `bytes`."]
45    fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
46        let (checksum_len_bytes, bytes) = bytes.split_at_checked(2).ok_or(Error::TooFewBytes)?;
47        let checksum_len_bytes: [u8; 2] =
48            checksum_len_bytes.try_into().expect("previous call guaranteed 2 element slice");
49        let checksum_length = u16::from_le_bytes(checksum_len_bytes) as usize;
50        let (observed_checksum, bytes) =
51            bytes.split_at_checked(checksum_length).ok_or(Error::TooFewBytes)?;
52        if observed_checksum != EXPECTED_CHECKSUM {
53            return Err(Error::ChecksumMismatch {
54                expected_checksum: EXPECTED_CHECKSUM.to_vec(),
55                observed_checksum: observed_checksum.to_vec(),
56            });
57        }
58        let fidl_config: FidlConfig = unpersist(bytes).map_err(Error::Unpersist)?;
59        Ok(Self {
60            all_packages_executable: fidl_config.all_packages_executable,
61            blob_download_resumption_attempts_limit: fidl_config
62                .blob_download_resumption_attempts_limit,
63            blob_fetch_concurrency_limit: fidl_config.blob_fetch_concurrency_limit,
64            blob_network_body_timeout_seconds: fidl_config.blob_network_body_timeout_seconds,
65            blob_network_header_timeout_seconds: fidl_config.blob_network_header_timeout_seconds,
66            enable_upgradable_packages: fidl_config.enable_upgradable_packages,
67            require_system_image: fidl_config.require_system_image,
68            system_image_hash: fidl_config.system_image_hash,
69        })
70    }
71    fn to_bytes(&self) -> Result<Vec<u8>, Error> {
72        let fidl_config = FidlConfig {
73            all_packages_executable: self.all_packages_executable.clone(),
74            blob_download_resumption_attempts_limit: self
75                .blob_download_resumption_attempts_limit
76                .clone(),
77            blob_fetch_concurrency_limit: self.blob_fetch_concurrency_limit.clone(),
78            blob_network_body_timeout_seconds: self.blob_network_body_timeout_seconds.clone(),
79            blob_network_header_timeout_seconds: self.blob_network_header_timeout_seconds.clone(),
80            enable_upgradable_packages: self.enable_upgradable_packages.clone(),
81            require_system_image: self.require_system_image.clone(),
82            system_image_hash: self.system_image_hash.clone(),
83        };
84        let mut fidl_bytes = fidl::persist(&fidl_config).map_err(Error::Persist)?;
85        let mut bytes = Vec::with_capacity(
86            EXPECTED_CHECKSUM_LENGTH.len() + EXPECTED_CHECKSUM.len() + fidl_bytes.len(),
87        );
88        bytes.extend_from_slice(&EXPECTED_CHECKSUM_LENGTH);
89        bytes.extend_from_slice(EXPECTED_CHECKSUM);
90        bytes.append(&mut fidl_bytes);
91        Ok(bytes)
92    }
93    fn record_inspect(&self, inspector_node: &Node) {
94        inspector_node.record_bool("all_packages_executable", self.all_packages_executable);
95        inspector_node.record_uint(
96            "blob_download_resumption_attempts_limit",
97            self.blob_download_resumption_attempts_limit as u64,
98        );
99        inspector_node
100            .record_uint("blob_fetch_concurrency_limit", self.blob_fetch_concurrency_limit as u64);
101        inspector_node.record_uint(
102            "blob_network_body_timeout_seconds",
103            self.blob_network_body_timeout_seconds as u64,
104        );
105        inspector_node.record_uint(
106            "blob_network_header_timeout_seconds",
107            self.blob_network_header_timeout_seconds as u64,
108        );
109        inspector_node.record_bool("enable_upgradable_packages", self.enable_upgradable_packages);
110        inspector_node.record_bool("require_system_image", self.require_system_image);
111        inspector_node.record_string("system_image_hash", &self.system_image_hash);
112    }
113}