component_debug/storage/
make_directory.rs

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.
4
5use crate::io::{Directory, RemoteDirectory};
6use crate::path::RemoteComponentStoragePath;
7use anyhow::{anyhow, bail, Result};
8
9use flex_client::ProxyHasDomain;
10use flex_fuchsia_io as fio;
11use flex_fuchsia_sys2::StorageAdminProxy;
12
13/// 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<()> {
19    let remote_path = RemoteComponentStoragePath::parse(&path)?;
20
21    let (dir_proxy, server) = storage_admin.domain().create_proxy::<fio::DirectoryMarker>();
22    let server = server.into_channel();
23    let storage_dir = RemoteDirectory::from_proxy(dir_proxy);
24
25    if remote_path.relative_path.as_os_str().is_empty() {
26        bail!("Remote path cannot be the root");
27    }
28
29    // Open the storage
30    storage_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))?;
34
35    // Send a request to create the directory
36    let dir = storage_dir.create_dir(remote_path.relative_path, false)?;
37
38    // Verify that we can actually read the contents of the directory created
39    dir.entry_names().await?;
40
41    Ok(())
42}
43
44////////////////////////////////////////////////////////////////////////////////
45// tests
46
47#[cfg(test)]
48mod test {
49    use super::*;
50    use crate::storage::test::{node_to_directory, setup_fake_storage_admin};
51    use flex_fuchsia_io as fio;
52    use futures::TryStreamExt;
53
54    // TODO(xbhatnag): Replace this mock with something more robust like VFS.
55    // Currently VFS is not cross-platform.
56    fn 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
60            let request = root_dir.try_next().await;
61            let object = if let Ok(Some(fio::DirectoryRequest::Open {
62                flags, path, object, ..
63            })) = request
64            {
65                assert_eq!(path, "test");
66                assert!(flags.intersects(fio::Flags::FLAG_MAYBE_CREATE));
67                assert!(flags.intersects(fio::Flags::PROTOCOL_DIRECTORY));
68                object
69            } else {
70                panic!("did not get open request: {:?}", request);
71            };
72
73            // Serve the new test directory
74            let mut test_dir = node_to_directory(object.into());
75
76            // Rewind on new directory should succeed
77            let request = test_dir.try_next().await;
78            if let Ok(Some(fio::DirectoryRequest::Rewind { responder, .. })) = request {
79                responder.send(0).unwrap();
80            } else {
81                panic!("did not get rewind request: {:?}", request)
82            }
83
84            // ReadDirents should report no contents in the new directory
85            let request = test_dir.try_next().await;
86            if let Ok(Some(fio::DirectoryRequest::ReadDirents { responder, .. })) = request {
87                responder.send(0, &[]).unwrap();
88            } else {
89                panic!("did not get readdirents request: {:?}", request)
90            }
91        })
92        .detach();
93    }
94
95    #[fuchsia_async::run_singlethreaded(test)]
96    async fn test_make_directory() -> Result<()> {
97        let storage_admin = setup_fake_storage_admin("123456", setup_fake_directory);
98        make_directory(storage_admin, "123456::test".to_string()).await
99    }
100}