component_debug/storage/
list.rsuse crate::io::{Directory, RemoteDirectory};
use crate::path::RemoteComponentStoragePath;
use anyhow::{anyhow, Result};
use fidl::endpoints::create_proxy;
use fidl_fuchsia_io as fio;
use fidl_fuchsia_sys2::StorageAdminProxy;
pub async fn list(storage_admin: StorageAdminProxy, path: String) -> Result<Vec<String>> {
let remote_path = RemoteComponentStoragePath::parse(&path)?;
let (dir_proxy, server) = create_proxy::<fio::DirectoryMarker>();
let server = server.into_channel();
let storage_dir = RemoteDirectory::from_proxy(dir_proxy);
storage_admin
.open_component_storage_by_id(&remote_path.instance_id, server.into())
.await?
.map_err(|e| anyhow!("Could not open component storage: {:?}", e))?;
let dir = if remote_path.relative_path.as_os_str().is_empty() {
storage_dir
} else {
storage_dir.open_dir_readonly(remote_path.relative_path)?
};
dir.entry_names().await
}
#[cfg(test)]
mod test {
use super::*;
use crate::storage::test::setup_fake_storage_admin;
use fidl_fuchsia_io as fio;
use futures::TryStreamExt;
pub fn dirents(names: Vec<&'static str>) -> Vec<u8> {
let mut bytes = vec![];
for name in names {
for _ in 0..8 {
bytes.push(0);
}
bytes.push(name.len() as u8);
bytes.push(fio::DirentType::File.into_primitive());
for byte in name.bytes() {
bytes.push(byte);
}
}
bytes
}
fn setup_fake_directory(mut root_dir: fio::DirectoryRequestStream) {
fuchsia_async::Task::local(async move {
let dirents = dirents(vec!["foo", "bar"]);
let request = root_dir.try_next().await;
if let Ok(Some(fio::DirectoryRequest::Rewind { responder, .. })) = request {
responder.send(0).unwrap();
} else {
panic!("did not get rewind request: {:?}", request)
}
let request = root_dir.try_next().await;
if let Ok(Some(fio::DirectoryRequest::ReadDirents { max_bytes, responder })) = request {
assert!(dirents.len() as u64 <= max_bytes);
responder.send(0, dirents.as_slice()).unwrap();
} else {
panic!("did not get readdirents request: {:?}", request)
}
let request = root_dir.try_next().await;
if let Ok(Some(fio::DirectoryRequest::ReadDirents { responder, .. })) = request {
responder.send(0, &[]).unwrap();
} else {
panic!("did not get readdirents request: {:?}", request)
}
})
.detach();
}
#[fuchsia_async::run_singlethreaded(test)]
async fn test_list_root() -> Result<()> {
let storage_admin = setup_fake_storage_admin("123456", setup_fake_directory);
let dir_entries = list(storage_admin, "123456::.".to_string()).await?;
assert_eq!(dir_entries, vec!["bar", "foo"]);
Ok(())
}
}