component_debug/storage/
make_directory.rsuse crate::io::{Directory, RemoteDirectory};
use crate::path::RemoteComponentStoragePath;
use anyhow::{anyhow, bail, Result};
use fidl::endpoints::create_proxy;
use fidl_fuchsia_io as fio;
use fidl_fuchsia_sys2::StorageAdminProxy;
pub async fn make_directory(storage_admin: StorageAdminProxy, path: String) -> Result<()> {
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);
if remote_path.relative_path.as_os_str().is_empty() {
bail!("Remote path cannot be the root");
}
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 = storage_dir.create_dir(remote_path.relative_path, false)?;
dir.entry_names().await?;
Ok(())
}
#[cfg(test)]
mod test {
use super::*;
use crate::storage::test::{node_to_directory, setup_fake_storage_admin};
use fidl_fuchsia_io as fio;
use futures::TryStreamExt;
fn setup_fake_directory(mut root_dir: fio::DirectoryRequestStream) {
fuchsia_async::Task::local(async move {
let request = root_dir.try_next().await;
let object =
if let Ok(Some(fio::DirectoryRequest::Open3 { flags, path, object, .. })) = request
{
assert_eq!(path, "test");
assert!(flags.intersects(fio::Flags::FLAG_MAYBE_CREATE));
assert!(flags.intersects(fio::Flags::PROTOCOL_DIRECTORY));
object
} else {
panic!("did not get open request: {:?}", request);
};
let mut test_dir = node_to_directory(object.into());
let request = test_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 = test_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_make_directory() -> Result<()> {
let storage_admin = setup_fake_storage_admin("123456", setup_fake_directory);
make_directory(storage_admin, "123456::test".to_string()).await
}
}