component_debug/cli/
storage.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
99
100
// Copyright 2023 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::storage::{copy, delete, delete_all, list, make_directory};
use anyhow::{format_err, Result};
use fidl_fuchsia_sys2 as fsys;
use moniker::Moniker;

async fn get_storage_admin(
    realm_query: fsys::RealmQueryProxy,
    storage_provider_moniker: String,
    storage_capability_name: String,
) -> Result<fsys::StorageAdminProxy> {
    let storage_provider_moniker = Moniker::parse_str(&storage_provider_moniker).map_err(|e| {
        format_err!("Error: {} is not a valid moniker ({})", storage_provider_moniker, e)
    })?;

    let (storage_admin, server_end) = fidl::endpoints::create_proxy::<fsys::StorageAdminMarker>();

    realm_query
        .connect_to_storage_admin(
            &storage_provider_moniker.to_string(),
            &storage_capability_name,
            server_end,
        )
        .await?
        .map_err(|e| {
            format_err!(
                "Failed to get StorageAdmin proxy for capability '{}' of instance '{}': {:?}",
                storage_capability_name,
                storage_provider_moniker,
                e
            )
        })?;

    Ok(storage_admin)
}

pub async fn storage_copy_cmd(
    storage_provider_moniker: String,
    storage_capability_name: String,
    source_path: String,
    destination_path: String,
    realm_query: fsys::RealmQueryProxy,
) -> Result<()> {
    let storage_admin =
        get_storage_admin(realm_query, storage_provider_moniker, storage_capability_name).await?;
    copy(storage_admin, source_path, destination_path).await
}

pub async fn storage_list_cmd<W: std::io::Write>(
    storage_provider_moniker: String,
    storage_capability_name: String,
    path: String,
    realm_query: fsys::RealmQueryProxy,
    mut writer: W,
) -> Result<()> {
    let storage_admin =
        get_storage_admin(realm_query, storage_provider_moniker, storage_capability_name).await?;
    let entries = list(storage_admin, path).await?;

    for entry in entries {
        writeln!(writer, "{}", entry)?;
    }
    Ok(())
}

pub async fn storage_make_directory_cmd(
    storage_provider_moniker: String,
    storage_capability_name: String,
    path: String,
    realm_query: fsys::RealmQueryProxy,
) -> Result<()> {
    let storage_admin =
        get_storage_admin(realm_query, storage_provider_moniker, storage_capability_name).await?;
    make_directory(storage_admin, path).await
}

pub async fn storage_delete_cmd(
    storage_provider_moniker: String,
    storage_capability_name: String,
    path: String,
    realm_query: fsys::RealmQueryProxy,
) -> Result<()> {
    let storage_admin =
        get_storage_admin(realm_query, storage_provider_moniker, storage_capability_name).await?;
    delete(storage_admin, path).await
}

pub async fn storage_delete_all_cmd(
    storage_provider_moniker: String,
    storage_capability_name: String,
    moniker: String,
    realm_query: fsys::RealmQueryProxy,
) -> Result<()> {
    let storage_admin =
        get_storage_admin(realm_query, storage_provider_moniker, storage_capability_name).await?;
    delete_all(storage_admin, moniker).await
}