1// Copyright 2021 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
45use crate::io::{Directory, RemoteDirectory};
6use crate::path::RemoteComponentStoragePath;
7use anyhow::{anyhow, bail, Result};
89use flex_client::ProxyHasDomain;
10use flex_fuchsia_io as fio;
11use flex_fuchsia_sys2::StorageAdminProxy;
1213/// Create a new directory in a component's storage.
14///
15/// # Arguments
16/// * `storage_admin`: The StorageAdminProxy
17/// * `path`: The name of a new directory on the target component
18pub async fn make_directory(storage_admin: StorageAdminProxy, path: String) -> Result<()> {
19let remote_path = RemoteComponentStoragePath::parse(&path)?;
2021let (dir_proxy, server) = storage_admin.domain().create_proxy::<fio::DirectoryMarker>();
22let server = server.into_channel();
23let storage_dir = RemoteDirectory::from_proxy(dir_proxy);
2425if remote_path.relative_path.as_os_str().is_empty() {
26bail!("Remote path cannot be the root");
27 }
2829// Open the storage
30storage_admin
31 .open_component_storage_by_id(&remote_path.instance_id, server.into())
32 .await?
33.map_err(|e| anyhow!("Could not open component storage: {:?}", e))?;
3435// Send a request to create the directory
36let dir = storage_dir.create_dir(remote_path.relative_path, false)?;
3738// Verify that we can actually read the contents of the directory created
39dir.entry_names().await?;
4041Ok(())
42}
4344////////////////////////////////////////////////////////////////////////////////
45// tests
4647#[cfg(test)]
48mod test {
49use super::*;
50use crate::storage::test::{node_to_directory, setup_fake_storage_admin};
51use flex_fuchsia_io as fio;
52use futures::TryStreamExt;
5354// TODO(xbhatnag): Replace this mock with something more robust like VFS.
55 // Currently VFS is not cross-platform.
56fn setup_fake_directory(mut root_dir: fio::DirectoryRequestStream) {
57 fuchsia_async::Task::local(async move {
58// Serve the root directory
59 // Root directory should get Open call with CREATE flag
60let request = root_dir.try_next().await;
61let object = if let Ok(Some(fio::DirectoryRequest::Open {
62 flags, path, object, ..
63 })) = request
64 {
65assert_eq!(path, "test");
66assert!(flags.intersects(fio::Flags::FLAG_MAYBE_CREATE));
67assert!(flags.intersects(fio::Flags::PROTOCOL_DIRECTORY));
68 object
69 } else {
70panic!("did not get open request: {:?}", request);
71 };
7273// Serve the new test directory
74let mut test_dir = node_to_directory(object.into());
7576// Rewind on new directory should succeed
77let request = test_dir.try_next().await;
78if let Ok(Some(fio::DirectoryRequest::Rewind { responder, .. })) = request {
79 responder.send(0).unwrap();
80 } else {
81panic!("did not get rewind request: {:?}", request)
82 }
8384// ReadDirents should report no contents in the new directory
85let request = test_dir.try_next().await;
86if let Ok(Some(fio::DirectoryRequest::ReadDirents { responder, .. })) = request {
87 responder.send(0, &[]).unwrap();
88 } else {
89panic!("did not get readdirents request: {:?}", request)
90 }
91 })
92 .detach();
93 }
9495#[fuchsia_async::run_singlethreaded(test)]
96async fn test_make_directory() -> Result<()> {
97let storage_admin = setup_fake_storage_admin("123456", setup_fake_directory);
98 make_directory(storage_admin, "123456::test".to_string()).await
99}
100}