]>
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::*; |
529de6c7 | 12 | use apitest::backup::datastore::*; |
43eeef28 | 13 | use serde_json::{Value}; |
ff5d3707 | 14 | |
15 | use apitest::config::datastore; | |
16 | ||
a914a774 | 17 | fn required_string_param<'a>(param: &'a Value, name: &str) -> &'a str { |
18 | param[name].as_str().expect(&format!("missing parameter '{}'", name)) | |
19 | } | |
20 | ||
21 | ||
ff5d3707 | 22 | fn backup_file(param: Value, _info: &ApiMethod) -> Result<Value, Error> { |
23 | ||
a914a774 | 24 | let filename = required_string_param(¶m, "filename"); |
25 | let store = required_string_param(¶m, "store"); | |
26 | ||
529de6c7 | 27 | let mut datastore = DataStore::open(store)?; |
a914a774 | 28 | |
29 | println!("Backup file '{}' to '{}'", filename, store); | |
30 | ||
4818c8b6 | 31 | let target = "test1.idx"; |
606ce64b | 32 | |
4818c8b6 DM |
33 | { |
34 | let file = std::fs::File::open(filename)?; | |
35 | let stat = nix::sys::stat::fstat(file.as_raw_fd())?; | |
36 | if stat.st_size <= 0 { bail!("got strange file size '{}'", stat.st_size); } | |
37 | let size = stat.st_size as usize; | |
a914a774 | 38 | |
529de6c7 | 39 | let mut index = datastore.create_image_writer(target, size)?; |
a914a774 | 40 | |
4818c8b6 DM |
41 | tools::file_chunker(file, 64*1024, |pos, chunk| { |
42 | index.add_chunk(pos, chunk)?; | |
43 | Ok(true) | |
44 | })?; | |
45 | ||
46 | index.close()?; // commit changes | |
47 | } | |
48 | ||
529de6c7 | 49 | let idx = datastore.open_image_reader(target)?; |
4818c8b6 | 50 | idx.print_info(); |
4fbb72a8 | 51 | |
ff5d3707 | 52 | Ok(Value::Null) |
53 | } | |
54 | ||
55 | ||
56 | fn main() { | |
57 | ||
58 | let cmd_def = CliCommand::new( | |
59 | ApiMethod::new( | |
60 | backup_file, | |
61 | ObjectSchema::new("Create backup from file.") | |
62 | .required("filename", StringSchema::new("Source file name.")) | |
63 | .required("store", StringSchema::new("Datastore name.")) | |
64 | )) | |
f8838fe9 DM |
65 | .arg_param(vec!["filename"]) |
66 | .completion_cb("store", apitest::config::datastore::complete_datastore_name); | |
67 | ||
a914a774 | 68 | |
ff5d3707 | 69 | if let Err(err) = run_cli_command(&cmd_def.into()) { |
70 | eprintln!("Error: {}", err); | |
71 | print_cli_usage(); | |
72 | std::process::exit(-1); | |
73 | } | |
74 | ||
75 | } |