]>
Commit | Line | Data |
---|---|---|
ff5d3707 | 1 | extern crate apitest; |
2 | ||
3 | use failure::*; | |
606ce64b | 4 | use std::os::unix::io::AsRawFd; |
ff5d3707 | 5 | |
43eeef28 | 6 | use apitest::tools; |
ff5d3707 | 7 | use apitest::cli::command::*; |
8 | use apitest::api::schema::*; | |
9 | use apitest::api::router::*; | |
10 | use apitest::backup::chunk_store::*; | |
606ce64b | 11 | use apitest::backup::image_index::*; |
43eeef28 | 12 | use serde_json::{Value}; |
ff5d3707 | 13 | |
14 | use apitest::config::datastore; | |
15 | ||
a914a774 | 16 | fn required_string_param<'a>(param: &'a Value, name: &str) -> &'a str { |
17 | param[name].as_str().expect(&format!("missing parameter '{}'", name)) | |
18 | } | |
19 | ||
20 | ||
ff5d3707 | 21 | fn backup_file(param: Value, _info: &ApiMethod) -> Result<Value, Error> { |
22 | ||
a914a774 | 23 | let filename = required_string_param(¶m, "filename"); |
24 | let store = required_string_param(¶m, "store"); | |
25 | ||
26 | let config = datastore::config()?; | |
27 | let (_, store_config) = config.sections.get(store) | |
28 | .ok_or(format_err!("no such datastore '{}'", store))?; | |
29 | ||
30 | let path = store_config["path"].as_str().unwrap(); | |
31 | ||
606ce64b | 32 | let mut chunk_store = ChunkStore::open(path)?; |
a914a774 | 33 | |
34 | println!("Backup file '{}' to '{}'", filename, store); | |
35 | ||
36 | let file = std::fs::File::open(filename)?; | |
606ce64b DM |
37 | let stat = nix::sys::stat::fstat(file.as_raw_fd())?; |
38 | if stat.st_size <= 0 { bail!("got strange file size '{}'", stat.st_size); } | |
39 | let size = stat.st_size as usize; | |
40 | ||
798881a6 | 41 | let mut index = ImageIndexWriter::create(&mut chunk_store, "test1.idx".as_ref(), size)?; |
a914a774 | 42 | |
43eeef28 | 43 | tools::file_chunker(file, 64*1024, |pos, chunk| { |
606ce64b | 44 | index.add_chunk(pos, chunk)?; |
a914a774 | 45 | Ok(true) |
46 | })?; | |
47 | ||
4fbb72a8 DM |
48 | index.close()?; // commit changes |
49 | ||
ff5d3707 | 50 | Ok(Value::Null) |
51 | } | |
52 | ||
53 | ||
54 | fn main() { | |
55 | ||
56 | let cmd_def = CliCommand::new( | |
57 | ApiMethod::new( | |
58 | backup_file, | |
59 | ObjectSchema::new("Create backup from file.") | |
60 | .required("filename", StringSchema::new("Source file name.")) | |
61 | .required("store", StringSchema::new("Datastore name.")) | |
62 | )) | |
f8838fe9 DM |
63 | .arg_param(vec!["filename"]) |
64 | .completion_cb("store", apitest::config::datastore::complete_datastore_name); | |
65 | ||
a914a774 | 66 | |
ff5d3707 | 67 | if let Err(err) = run_cli_command(&cmd_def.into()) { |
68 | eprintln!("Error: {}", err); | |
69 | print_cli_usage(); | |
70 | std::process::exit(-1); | |
71 | } | |
72 | ||
73 | } |