component_debug/storage/
mod.rsmod copy;
mod delete;
mod delete_all;
mod list;
mod make_directory;
pub use copy::copy;
pub use delete::delete;
pub use delete_all::delete_all;
pub use list::list;
pub use make_directory::make_directory;
#[cfg(test)]
pub mod test {
use std::collections::HashMap;
use fidl::endpoints::{RequestStream, ServerEnd};
use fidl::handle::AsyncChannel;
use fidl_fuchsia_io as fio;
use fidl_fuchsia_sys2::{StorageAdminProxy, StorageAdminRequest};
use futures::TryStreamExt;
use std::fs::{create_dir, write};
use tempfile::tempdir;
fn setup_oneshot_fake_storage_admin<R: 'static>(mut handle_request: R) -> StorageAdminProxy
where
R: FnMut(fidl::endpoints::Request<<StorageAdminProxy as fidl::endpoints::Proxy>::Protocol>),
{
let (proxy, mut stream) = fidl::endpoints::create_proxy_and_stream::<
<StorageAdminProxy as fidl::endpoints::Proxy>::Protocol,
>();
fuchsia_async::Task::local(async move {
if let Ok(Some(req)) = stream.try_next().await {
handle_request(req);
}
})
.detach();
proxy
}
pub fn node_to_directory(object: ServerEnd<fio::NodeMarker>) -> fio::DirectoryRequestStream {
fio::DirectoryRequestStream::from_channel(AsyncChannel::from_channel(object.into_channel()))
}
pub fn node_to_file(object: ServerEnd<fio::NodeMarker>) -> fio::FileRequestStream {
fio::FileRequestStream::from_channel(AsyncChannel::from_channel(object.into_channel()))
}
pub fn setup_fake_storage_admin(
expected_id: &'static str,
setup_fake_directory_fn: fn(fio::DirectoryRequestStream),
) -> StorageAdminProxy {
setup_oneshot_fake_storage_admin(move |req| match req {
StorageAdminRequest::OpenComponentStorageById { id, object, responder, .. } => {
assert_eq!(expected_id, id);
setup_fake_directory_fn(node_to_directory(object));
responder.send(Ok(())).unwrap();
}
_ => panic!("got unexpected {:?}", req),
})
}
pub fn create_tmp_path_string() -> String {
let tmp_dir = tempdir();
let dir = tmp_dir.as_ref().unwrap();
let tmp_path = String::from(dir.path().to_str().unwrap());
tmp_dir.expect("Could not close file").close().unwrap();
return tmp_path;
}
pub fn setup_fake_storage_admin_with_tmp(
expected_id: &'static str,
seed_files: HashMap<&'static str, &'static str>,
) -> StorageAdminProxy {
setup_oneshot_fake_storage_admin(move |req| match req {
StorageAdminRequest::OpenComponentStorageById { id, object, responder, .. } => {
assert_eq!(expected_id, id);
let tmp_path = create_tmp_path_string();
let () = create_dir(&tmp_path).unwrap();
for (new_file, new_file_contents) in seed_files.iter() {
let new_file_path = format!("{}/{}", tmp_path, new_file);
write(&new_file_path, new_file_contents).unwrap();
}
fuchsia_fs::directory::open_channel_in_namespace(
&tmp_path,
fio::PERM_READABLE | fio::PERM_WRITABLE | fio::Flags::PROTOCOL_DIRECTORY,
ServerEnd::new(object.into_channel()),
)
.unwrap();
responder.send(Ok(())).unwrap();
}
_ => panic!("got unexpected {:?}", req),
})
}
}