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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Copyright 2018 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.

#![warn(missing_docs)]

//! `stash_ctl` provides a terminal interface to the contents of stash.

use anyhow::{format_err, Error};
use fidl::endpoints::create_proxy;
use fidl_fuchsia_mem;
use fuchsia_async as fasync;
use fuchsia_component::client::connect_to_protocol;
use fuchsia_zircon as zx;
use futures::FutureExt;
use std::convert::{TryFrom, TryInto};
use std::env;
use std::str;

use fidl_fuchsia_stash::{KeyValue, ListItem, SecureStoreMarker, StoreMarker, Value};

fn main() -> Result<(), Error> {
    let cfg: StashCtlConfig = env::args().try_into()?;

    let mut executor = fasync::LocalExecutor::new();

    let acc = if cfg.secure {
        let stashserver = connect_to_protocol::<SecureStoreMarker>()?;

        // Identify
        stashserver.identify("stash_ctl")?;

        // Create an accessor
        let (acc, server_end) = create_proxy()?;
        stashserver.create_accessor(false, server_end)?;

        acc
    } else {
        let stashserver = connect_to_protocol::<StoreMarker>()?;

        // Identify
        stashserver.identify("stash_ctl")?;

        // Create an accessor
        let (acc, server_end) = create_proxy()?;
        stashserver.create_accessor(false, server_end)?;

        acc
    };

    // Perform the operation
    match cfg.op {
        StashOperation::Get(k) => {
            let fut = acc.get_value(&k).map(|res| match res {
                Ok(Some(val)) => print_val(&val),
                Ok(None) => println!("no such key: {}", k),
                Err(e) => println!("fidl error encountered: {:?}", e),
            });
            executor.run_singlethreaded(fut);
        }
        StashOperation::Set(k, mut v) => {
            acc.set_value(&k, &mut v)?;
            acc.commit()?;
            println!("{} set successfully", k);
        }
        StashOperation::Delete(k) => {
            acc.delete_value(&k)?;
            acc.commit()?;
            println!("{} deleted successfully", k);
        }
        StashOperation::ListPrefix(k) => {
            let (list_iterator, server_end) = create_proxy()?;
            acc.list_prefix(&k, server_end)?;

            let resp: Result<(), Error> = executor.run_singlethreaded(async {
                loop {
                    let res = list_iterator.get_next().await?;
                    if res.len() == 0 {
                        return Ok(());
                    }
                    print_listitems(res);
                }
            });
            resp?;
        }
        StashOperation::GetPrefix(k) => {
            let (get_iterator, server_end) = create_proxy()?;
            acc.get_prefix(&k, server_end)?;

            let resp: Result<(), Error> = executor.run_singlethreaded(async {
                loop {
                    let res = get_iterator.get_next().await?;
                    if res.len() == 0 {
                        return Ok(());
                    }
                    print_keyvalues(res);
                }
            });
            resp?;
        }
        StashOperation::DeletePrefix(k) => {
            acc.delete_prefix(&k).map(|_| println!("{} prefix deleted successfully", k))?;
            acc.commit()?;
        }
    };
    Ok(())
}

struct StashCtlConfig {
    secure: bool,
    op: StashOperation,
}

enum StashOperation {
    Get(String),
    Set(String, Value),
    Delete(String),
    ListPrefix(String),
    GetPrefix(String),
    DeletePrefix(String),
}

impl TryFrom<env::Args> for StashCtlConfig {
    type Error = Error;
    fn try_from(args: env::Args) -> Result<StashCtlConfig, Error> {
        let mut args = args.peekable();
        // ignore arg[0]
        let _ = args.next();
        let secure = args.peek() == Some(&"--secure".to_string());
        if secure {
            let _ = args.next();
        }
        // take advantage of the fact that `next()` will keep returning `None`
        let op = args.next();
        let key = args.next();
        let ty = args.next();
        let val = args.next();

        let op = match (
            op.as_ref().map(|s| s.as_str()),
            key,
            ty.as_ref().map(|s| s.as_str()),
            val.as_ref().map(|s| s.as_str()),
        ) {
            (Some("get"), Some(key), None, None) => StashOperation::Get(key),
            (Some("set"), Some(key), Some(type_), Some(val)) => {
                StashOperation::Set(key, to_val(type_, val)?)
            }
            (Some("delete"), Some(key), None, None) => StashOperation::Delete(key),
            (Some("list-prefix"), Some(key), None, None) => StashOperation::ListPrefix(key),
            (Some("get-prefix"), Some(key), None, None) => StashOperation::GetPrefix(key),
            (Some("delete-prefix"), Some(key), None, None) => StashOperation::DeletePrefix(key),

            _ => {
                help();
                return Err(format_err!("unable to parse args"));
            }
        };
        Ok(StashCtlConfig { secure, op })
    }
}

fn to_val(typ: &str, input: &str) -> Result<Value, Error> {
    match typ {
        "int" => Ok(Value::Intval(input.parse()?)),
        "float" => Ok(Value::Floatval(input.parse()?)),
        "text" => Ok(Value::Stringval(input.to_string())),
        "bool" => Ok(Value::Boolval(input.parse()?)),
        "bytes" => {
            let bytes = input.as_bytes();
            let vmo = zx::Vmo::create(bytes.len() as u64).map_err(|s| {
                format_err!(format!("error creating bytes buffer, zx status: {}", s))
            })?;
            vmo.write(&bytes, 0).map_err(|s| {
                format_err!(format!("error writing bytes buffer, zx status: {}", s))
            })?;
            Ok(Value::Bytesval(fidl_fuchsia_mem::Buffer { vmo: vmo, size: bytes.len() as u64 }))
        }
        _ => Err(format_err!(format!("unknown type: {}", typ))),
    }
}

fn help() {
    println!(
        r"Usage: stash_ctl [--secure] get NAME
       stash_ctl [--secure] set NAME [int|float|text|bool|bytes] VALUE
       stash_ctl [--secure] delete NAME
       stash_ctl [--secure] list-prefix PREFIX
       stash_ctl [--secure] get-prefix PREFIX
       stash_ctl [--secure] delete-prefix PREFIX

       NOTE: --secure will access a version of the store where the bytes type is disabled"
    )
}

fn print_val(val: &Value) {
    match val {
        Value::Intval(i) => println!("{}", i),
        Value::Floatval(f) => println!("{}", f),
        Value::Boolval(b) => println!("{}", b),
        Value::Stringval(s) => println!("{}", s),
        Value::Bytesval(b) => {
            let mut bytes_buffer = vec![0; b.size as usize]; // TODO: make sure b.size is reasonable
            match b.vmo.read(&mut bytes_buffer, 0) {
                Ok(_) => match str::from_utf8(&bytes_buffer) {
                    Ok(s) => println!("{}", s),
                    Err(e) => println!("error decoding response: {}", e),
                },
                Err(e) => println!("error reading json buffer, zx status: {}", e),
            }
        }
    }
}

fn print_listitems(list: Vec<ListItem>) {
    if list.is_empty() {
        println!("no keys found");
        return;
    }

    list.iter().for_each(|item| println!("{}", item.key));
}

fn print_keyvalues(list: Vec<KeyValue>) {
    if list.is_empty() {
        println!("no keys found");
        return;
    }

    list.iter().for_each(|item| {
        print!("{}: ", item.key);
        print_val(&item.val);
    });
}