Skip to main content

archivist_config/
archivist-config_rust_config_lib_source.rs

1use fidl::unpersist;
2use fidl_cf_sc_internal_archivistconfig::Config as FidlConfig;
3use fuchsia_component_config::{Config as ComponentConfig, Error};
4use fuchsia_inspect::{ArrayProperty, Node};
5use std::convert::TryInto;
6const EXPECTED_CHECKSUM: &[u8] = &[
7    0xc6, 0xf6, 0x8a, 0x24, 0xfb, 0x8a, 0x7c, 0xb6, 0x74, 0x9f, 0x02, 0x6a, 0xed, 0x90, 0x3a, 0x22,
8    0xbc, 0xa3, 0x52, 0xec, 0xb9, 0xe9, 0x0c, 0x8b, 0xea, 0xe8, 0x4c, 0xf5, 0xfe, 0xc6, 0xb9, 0x6f,
9];
10const EXPECTED_CHECKSUM_LENGTH: [u8; 2] = (EXPECTED_CHECKSUM.len() as u16).to_le_bytes();
11#[derive(Debug)]
12pub struct Config {
13    pub allow_serial_logs: Vec<String>,
14    pub bind_services: Vec<String>,
15    pub component_initial_interests: Vec<String>,
16    pub deny_serial_log_tags: Vec<String>,
17    pub enable_klog: bool,
18    pub fuchsia_diagnostics_sample_min_period_seconds: i64,
19    pub logs_max_cached_original_bytes: u64,
20    pub maximum_concurrent_snapshots_per_reader: u64,
21    pub num_threads: u8,
22    pub per_component_batch_timeout_seconds: i64,
23    pub pipelines_path: String,
24}
25impl Config {
26    #[doc = r" Take the config startup handle and parse its contents."]
27    #[doc = r""]
28    #[doc = r" # Panics"]
29    #[doc = r""]
30    #[doc = r" If the config startup handle was already taken or if it is not valid."]
31    pub fn take_from_startup_handle() -> Self {
32        <Self as ComponentConfig>::take_from_startup_handle()
33    }
34    #[doc = r" Parse `Self` from `vmo`."]
35    pub fn from_vmo(vmo: &zx::Vmo) -> Result<Self, Error> {
36        <Self as ComponentConfig>::from_vmo(vmo)
37    }
38    #[doc = r" Parse `Self` from `bytes`."]
39    pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
40        <Self as ComponentConfig>::from_bytes(bytes)
41    }
42    pub fn record_inspect(&self, inspector_node: &Node) {
43        <Self as ComponentConfig>::record_inspect(self, inspector_node)
44    }
45}
46impl ComponentConfig for Config {
47    #[doc = r" Parse `Self` from `bytes`."]
48    fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
49        let (checksum_len_bytes, bytes) = bytes.split_at_checked(2).ok_or(Error::TooFewBytes)?;
50        let checksum_len_bytes: [u8; 2] =
51            checksum_len_bytes.try_into().expect("previous call guaranteed 2 element slice");
52        let checksum_length = u16::from_le_bytes(checksum_len_bytes) as usize;
53        let (observed_checksum, bytes) =
54            bytes.split_at_checked(checksum_length).ok_or(Error::TooFewBytes)?;
55        if observed_checksum != EXPECTED_CHECKSUM {
56            return Err(Error::ChecksumMismatch {
57                expected_checksum: EXPECTED_CHECKSUM.to_vec(),
58                observed_checksum: observed_checksum.to_vec(),
59            });
60        }
61        let fidl_config: FidlConfig = unpersist(bytes).map_err(Error::Unpersist)?;
62        Ok(Self {
63            allow_serial_logs: fidl_config.allow_serial_logs,
64            bind_services: fidl_config.bind_services,
65            component_initial_interests: fidl_config.component_initial_interests,
66            deny_serial_log_tags: fidl_config.deny_serial_log_tags,
67            enable_klog: fidl_config.enable_klog,
68            fuchsia_diagnostics_sample_min_period_seconds: fidl_config
69                .fuchsia_diagnostics_sample_min_period_seconds,
70            logs_max_cached_original_bytes: fidl_config.logs_max_cached_original_bytes,
71            maximum_concurrent_snapshots_per_reader: fidl_config
72                .maximum_concurrent_snapshots_per_reader,
73            num_threads: fidl_config.num_threads,
74            per_component_batch_timeout_seconds: fidl_config.per_component_batch_timeout_seconds,
75            pipelines_path: fidl_config.pipelines_path,
76        })
77    }
78    fn to_bytes(&self) -> Result<Vec<u8>, Error> {
79        let fidl_config = FidlConfig {
80            allow_serial_logs: self.allow_serial_logs.clone(),
81            bind_services: self.bind_services.clone(),
82            component_initial_interests: self.component_initial_interests.clone(),
83            deny_serial_log_tags: self.deny_serial_log_tags.clone(),
84            enable_klog: self.enable_klog.clone(),
85            fuchsia_diagnostics_sample_min_period_seconds: self
86                .fuchsia_diagnostics_sample_min_period_seconds
87                .clone(),
88            logs_max_cached_original_bytes: self.logs_max_cached_original_bytes.clone(),
89            maximum_concurrent_snapshots_per_reader: self
90                .maximum_concurrent_snapshots_per_reader
91                .clone(),
92            num_threads: self.num_threads.clone(),
93            per_component_batch_timeout_seconds: self.per_component_batch_timeout_seconds.clone(),
94            pipelines_path: self.pipelines_path.clone(),
95        };
96        let mut fidl_bytes = fidl::persist(&fidl_config).map_err(Error::Persist)?;
97        let mut bytes = Vec::with_capacity(
98            EXPECTED_CHECKSUM_LENGTH.len() + EXPECTED_CHECKSUM.len() + fidl_bytes.len(),
99        );
100        bytes.extend_from_slice(&EXPECTED_CHECKSUM_LENGTH);
101        bytes.extend_from_slice(EXPECTED_CHECKSUM);
102        bytes.append(&mut fidl_bytes);
103        Ok(bytes)
104    }
105    fn record_inspect(&self, inspector_node: &Node) {
106        let arr =
107            inspector_node.create_string_array("allow_serial_logs", self.allow_serial_logs.len());
108        for i in 0..self.allow_serial_logs.len() {
109            arr.set(i, &self.allow_serial_logs[i]);
110        }
111        inspector_node.record(arr);
112        let arr = inspector_node.create_string_array("bind_services", self.bind_services.len());
113        for i in 0..self.bind_services.len() {
114            arr.set(i, &self.bind_services[i]);
115        }
116        inspector_node.record(arr);
117        let arr = inspector_node.create_string_array(
118            "component_initial_interests",
119            self.component_initial_interests.len(),
120        );
121        for i in 0..self.component_initial_interests.len() {
122            arr.set(i, &self.component_initial_interests[i]);
123        }
124        inspector_node.record(arr);
125        let arr = inspector_node
126            .create_string_array("deny_serial_log_tags", self.deny_serial_log_tags.len());
127        for i in 0..self.deny_serial_log_tags.len() {
128            arr.set(i, &self.deny_serial_log_tags[i]);
129        }
130        inspector_node.record(arr);
131        inspector_node.record_bool("enable_klog", self.enable_klog);
132        inspector_node.record_int(
133            "fuchsia_diagnostics_sample_min_period_seconds",
134            self.fuchsia_diagnostics_sample_min_period_seconds,
135        );
136        inspector_node
137            .record_uint("logs_max_cached_original_bytes", self.logs_max_cached_original_bytes);
138        inspector_node.record_uint(
139            "maximum_concurrent_snapshots_per_reader",
140            self.maximum_concurrent_snapshots_per_reader,
141        );
142        inspector_node.record_uint("num_threads", self.num_threads as u64);
143        inspector_node.record_int(
144            "per_component_batch_timeout_seconds",
145            self.per_component_batch_timeout_seconds,
146        );
147        inspector_node.record_string("pipelines_path", &self.pipelines_path);
148    }
149}