iquery/commands/
target.rs1use crate::commands::types::DiagnosticsProvider;
6use crate::commands::utils::*;
7use crate::types::Error;
8use diagnostics_data::{Data, Inspect};
9use diagnostics_reader::{ArchiveReader, RetryConfig};
10use fidl::endpoints::DiscoverableProtocolMarker;
11use fidl_fuchsia_diagnostics::{ArchiveAccessorMarker, ArchiveAccessorProxy, Selector};
12use fidl_fuchsia_sys2 as fsys2;
13use moniker::Moniker;
14
15const ROOT_ARCHIVIST: &str = "bootstrap/archivist";
16
17pub struct ArchiveAccessorProvider {
18 query_proxy: fsys2::RealmQueryProxy,
19}
20
21impl ArchiveAccessorProvider {
22 pub fn new(proxy: fsys2::RealmQueryProxy) -> Self {
23 Self { query_proxy: proxy }
24 }
25}
26
27impl DiagnosticsProvider for ArchiveAccessorProvider {
28 async fn snapshot(
29 &self,
30 accessor: Option<&str>,
31 selectors: impl IntoIterator<Item = Selector>,
32 ) -> Result<Vec<Data<Inspect>>, Error> {
33 let archive = connect_to_accessor_selector(accessor, &self.query_proxy).await?;
34 ArchiveReader::inspect()
35 .with_archive(archive)
36 .retry(RetryConfig::never())
37 .add_selectors(selectors.into_iter())
38 .snapshot()
39 .await
40 .map_err(Error::Fetch)
41 }
42
43 async fn get_accessor_paths(&self) -> Result<Vec<String>, Error> {
44 get_accessor_selectors(&self.query_proxy).await
45 }
46
47 fn realm_query(&self) -> &fsys2::RealmQueryProxy {
48 &self.query_proxy
49 }
50}
51
52pub async fn connect_to_accessor_selector(
57 selector: Option<&str>,
58 query_proxy: &fsys2::RealmQueryProxy,
59) -> Result<ArchiveAccessorProxy, Error> {
60 match selector {
61 Some(s) => {
62 let Some((component, accessor_name)) = s.rsplit_once(":") else {
63 return Err(Error::invalid_accessor(s));
64 };
65 let Ok(moniker) = Moniker::try_from(component) else {
66 return Err(Error::invalid_accessor(s));
67 };
68 connect_accessor::<ArchiveAccessorMarker>(&moniker, accessor_name, query_proxy).await
69 }
70 None => {
71 let moniker = Moniker::try_from(ROOT_ARCHIVIST).unwrap();
72 connect_accessor::<ArchiveAccessorMarker>(
73 &moniker,
74 ArchiveAccessorMarker::PROTOCOL_NAME,
75 query_proxy,
76 )
77 .await
78 }
79 }
80}