]>
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 | ||
4818c8b6 | 36 | let target = "test1.idx"; |
606ce64b | 37 | |
4818c8b6 DM |
38 | { |
39 | let file = std::fs::File::open(filename)?; | |
40 | let stat = nix::sys::stat::fstat(file.as_raw_fd())?; | |
41 | if stat.st_size <= 0 { bail!("got strange file size '{}'", stat.st_size); } | |
42 | let size = stat.st_size as usize; | |
a914a774 | 43 | |
4818c8b6 | 44 | let mut index = ImageIndexWriter::create(&mut chunk_store, target.as_ref(), size)?; |
a914a774 | 45 | |
4818c8b6 DM |
46 | tools::file_chunker(file, 64*1024, |pos, chunk| { |
47 | index.add_chunk(pos, chunk)?; | |
48 | Ok(true) | |
49 | })?; | |
50 | ||
51 | index.close()?; // commit changes | |
52 | } | |
53 | ||
54 | let idx = ImageIndexReader::open(&mut chunk_store, target.as_ref())?; | |
55 | idx.print_info(); | |
4fbb72a8 | 56 | |
ff5d3707 | 57 | Ok(Value::Null) |
58 | } | |
59 | ||
60 | ||
61 | fn main() { | |
62 | ||
63 | let cmd_def = CliCommand::new( | |
64 | ApiMethod::new( | |
65 | backup_file, | |
66 | ObjectSchema::new("Create backup from file.") | |
67 | .required("filename", StringSchema::new("Source file name.")) | |
68 | .required("store", StringSchema::new("Datastore name.")) | |
69 | )) | |
f8838fe9 DM |
70 | .arg_param(vec!["filename"]) |
71 | .completion_cb("store", apitest::config::datastore::complete_datastore_name); | |
72 | ||
a914a774 | 73 | |
ff5d3707 | 74 | if let Err(err) = run_cli_command(&cmd_def.into()) { |
75 | eprintln!("Error: {}", err); | |
76 | print_cli_usage(); | |
77 | std::process::exit(-1); | |
78 | } | |
79 | ||
80 | } |