component_debug/storage/
make_directory.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2021 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use 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;

/// Create a new directory in a component's storage.
///
/// # Arguments
/// * `storage_admin`: The StorageAdminProxy
/// * `path`: The name of a new directory on the target component
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");
    }

    // Open the storage
    storage_admin
        .open_component_storage_by_id(&remote_path.instance_id, server.into())
        .await?
        .map_err(|e| anyhow!("Could not open component storage: {:?}", e))?;

    // Send a request to create the directory
    let dir = storage_dir.create_dir(remote_path.relative_path, false)?;

    // Verify that we can actually read the contents of the directory created
    dir.entry_names().await?;

    Ok(())
}

////////////////////////////////////////////////////////////////////////////////
// tests

#[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;

    // TODO(xbhatnag): Replace this mock with something more robust like VFS.
    // Currently VFS is not cross-platform.
    fn setup_fake_directory(mut root_dir: fio::DirectoryRequestStream) {
        fuchsia_async::Task::local(async move {
            // Serve the root directory
            // Root directory should get Open call with CREATE flag
            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);
                };

            // Serve the new test directory
            let mut test_dir = node_to_directory(object.into());

            // Rewind on new directory should succeed
            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)
            }

            // ReadDirents should report no contents in the new directory
            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
    }
}