component_debug/storage/
list.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, Result};
8
9use flex_client::ProxyHasDomain;
10use flex_fuchsia_io as fio;
11use flex_fuchsia_sys2::StorageAdminProxy;
12
13/// List all directories and files in a component's storage.
14/// Returns a vector of names of the directories and files as strings.
15///
16/// # Arguments
17/// * `storage_admin`: The StorageAdminProxy
18/// * `path`: A path on the target component
19pub async fn list(storage_admin: StorageAdminProxy, path: String) -> Result<Vec<String>> {
20    let remote_path = RemoteComponentStoragePath::parse(&path)?;
21
22    let (dir_proxy, server) = storage_admin.domain().create_proxy::<fio::DirectoryMarker>();
23    let server = server.into_channel();
24    let storage_dir = RemoteDirectory::from_proxy(dir_proxy);
25
26    storage_admin
27        .open_component_storage_by_id(&remote_path.instance_id, server.into())
28        .await?
29        .map_err(|e| anyhow!("Could not open component storage: {:?}", e))?;
30
31    let dir = if remote_path.relative_path.as_os_str().is_empty() {
32        storage_dir
33    } else {
34        storage_dir.open_dir_readonly(remote_path.relative_path)?
35    };
36
37    dir.entry_names().await
38}
39
40////////////////////////////////////////////////////////////////////////////////
41// tests
42
43#[cfg(test)]
44mod test {
45    use super::*;
46    use crate::storage::test::setup_fake_storage_admin;
47    use flex_fuchsia_io as fio;
48    use futures::TryStreamExt;
49
50    pub fn dirents(names: Vec<&'static str>) -> Vec<u8> {
51        let mut bytes = vec![];
52        for name in names {
53            // inode: u64
54            for _ in 0..8 {
55                bytes.push(0);
56            }
57            // size: u8
58            bytes.push(name.len() as u8);
59            // type: u8
60            bytes.push(fio::DirentType::File.into_primitive());
61            // name: [u8]
62            for byte in name.bytes() {
63                bytes.push(byte);
64            }
65        }
66        bytes
67    }
68
69    // TODO(xbhatnag): Replace this mock with something more robust like VFS.
70    // Currently VFS is not cross-platform.
71    fn setup_fake_directory(mut root_dir: fio::DirectoryRequestStream) {
72        fuchsia_async::Task::local(async move {
73            let dirents = dirents(vec!["foo", "bar"]);
74
75            // Serve the root directory
76            // Rewind on root directory should succeed
77            let request = root_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 two files in the root directory
85            let request = root_dir.try_next().await;
86            if let Ok(Some(fio::DirectoryRequest::ReadDirents { max_bytes, responder })) = request {
87                assert!(dirents.len() as u64 <= max_bytes);
88                responder.send(0, dirents.as_slice()).unwrap();
89            } else {
90                panic!("did not get readdirents request: {:?}", request)
91            }
92
93            // ReadDirents should not report any more contents
94            let request = root_dir.try_next().await;
95            if let Ok(Some(fio::DirectoryRequest::ReadDirents { responder, .. })) = request {
96                responder.send(0, &[]).unwrap();
97            } else {
98                panic!("did not get readdirents request: {:?}", request)
99            }
100        })
101        .detach();
102    }
103
104    #[fuchsia_async::run_singlethreaded(test)]
105    async fn test_list_root() -> Result<()> {
106        let storage_admin = setup_fake_storage_admin("123456", setup_fake_directory);
107        let dir_entries = list(storage_admin, "123456::.".to_string()).await?;
108
109        assert_eq!(dir_entries, vec!["bar", "foo"]);
110        Ok(())
111    }
112}