--- /dev/null
+use failure::*;
+
+use std::path::Path;
+
+use crate::config::datastore;
+use super::chunk_store::*;
+use super::image_index::*;
+
+pub struct DataStore {
+ chunk_store: ChunkStore,
+}
+
+impl DataStore {
+
+ pub fn open(store_name: &str) -> Result<Self, Error> {
+
+ let config = datastore::config()?;
+ let (_, store_config) = config.sections.get(store_name)
+ .ok_or(format_err!("no such datastore '{}'", store_name))?;
+
+ let path = store_config["path"].as_str().unwrap();
+
+ let chunk_store = ChunkStore::open(path)?;
+
+ Ok(Self {
+ chunk_store: chunk_store,
+ })
+ }
+
+ pub fn create_image_writer<P: AsRef<Path>>(&mut self, filename: P, size: usize) -> Result<ImageIndexWriter, Error> {
+
+ let index = ImageIndexWriter::create(&mut self.chunk_store, filename.as_ref(), size)?;
+
+ Ok(index)
+ }
+
+ pub fn open_image_reader<P: AsRef<Path>>(&mut self, filename: P) -> Result<ImageIndexReader, Error> {
+
+ let index = ImageIndexReader::open(&mut self.chunk_store, filename.as_ref())?;
+
+ Ok(index)
+ }
+}
use apitest::api::router::*;
use apitest::backup::chunk_store::*;
use apitest::backup::image_index::*;
+use apitest::backup::datastore::*;
use serde_json::{Value};
use apitest::config::datastore;
let filename = required_string_param(¶m, "filename");
let store = required_string_param(¶m, "store");
- let config = datastore::config()?;
- let (_, store_config) = config.sections.get(store)
- .ok_or(format_err!("no such datastore '{}'", store))?;
-
- let path = store_config["path"].as_str().unwrap();
-
- let mut chunk_store = ChunkStore::open(path)?;
+ let mut datastore = DataStore::open(store)?;
println!("Backup file '{}' to '{}'", filename, store);
if stat.st_size <= 0 { bail!("got strange file size '{}'", stat.st_size); }
let size = stat.st_size as usize;
- let mut index = ImageIndexWriter::create(&mut chunk_store, target.as_ref(), size)?;
+ let mut index = datastore.create_image_writer(target, size)?;
tools::file_chunker(file, 64*1024, |pos, chunk| {
index.add_chunk(pos, chunk)?;
index.close()?; // commit changes
}
- let idx = ImageIndexReader::open(&mut chunk_store, target.as_ref())?;
+ let idx = datastore.open_image_reader(target)?;
idx.print_info();
Ok(Value::Null)