2017-03-10 11:43:32 +00:00
|
|
|
mod config;
|
|
|
|
mod bundle_map;
|
|
|
|
mod integrity;
|
|
|
|
mod basic_io;
|
2017-03-15 07:27:27 +00:00
|
|
|
mod info;
|
2017-03-15 11:32:44 +00:00
|
|
|
mod metadata;
|
2017-03-15 20:53:05 +00:00
|
|
|
mod backup;
|
2017-03-16 08:42:30 +00:00
|
|
|
mod error;
|
2017-03-10 11:43:32 +00:00
|
|
|
|
|
|
|
use std::mem;
|
|
|
|
use std::cmp::max;
|
|
|
|
use std::path::{PathBuf, Path};
|
|
|
|
use std::fs;
|
|
|
|
|
|
|
|
use super::index::Index;
|
|
|
|
use super::bundle::{BundleDb, BundleWriter};
|
|
|
|
use super::chunker::Chunker;
|
|
|
|
|
2017-03-16 08:42:30 +00:00
|
|
|
pub use self::error::RepositoryError;
|
2017-03-10 11:43:32 +00:00
|
|
|
pub use self::config::Config;
|
2017-03-15 11:32:44 +00:00
|
|
|
pub use self::metadata::Inode;
|
|
|
|
pub use self::basic_io::Chunk;
|
2017-03-15 20:53:05 +00:00
|
|
|
pub use self::backup::Backup;
|
2017-03-15 07:27:27 +00:00
|
|
|
use self::bundle_map::BundleMap;
|
2017-03-10 11:43:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
#[derive(Eq, Debug, PartialEq, Clone, Copy)]
|
|
|
|
pub enum Mode {
|
|
|
|
Content, Meta
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Repository {
|
|
|
|
path: PathBuf,
|
|
|
|
config: Config,
|
|
|
|
index: Index,
|
|
|
|
bundle_map: BundleMap,
|
|
|
|
next_content_bundle: u32,
|
|
|
|
next_meta_bundle: u32,
|
|
|
|
bundles: BundleDb,
|
|
|
|
content_bundle: Option<BundleWriter>,
|
|
|
|
meta_bundle: Option<BundleWriter>,
|
|
|
|
chunker: Chunker
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl Repository {
|
2017-03-16 08:42:30 +00:00
|
|
|
pub fn create<P: AsRef<Path>>(path: P, config: Config) -> Result<Self, RepositoryError> {
|
2017-03-10 11:43:32 +00:00
|
|
|
let path = path.as_ref().to_owned();
|
2017-03-16 08:42:30 +00:00
|
|
|
try!(fs::create_dir(&path));
|
2017-03-10 11:43:32 +00:00
|
|
|
let bundles = try!(BundleDb::create(
|
|
|
|
path.join("bundles"),
|
|
|
|
config.compression.clone(),
|
|
|
|
None, //FIXME: store encryption in config
|
|
|
|
config.checksum
|
2017-03-16 08:42:30 +00:00
|
|
|
));
|
|
|
|
let index = try!(Index::create(&path.join("index")));
|
|
|
|
try!(config.save(path.join("config.yaml")));
|
2017-03-10 11:43:32 +00:00
|
|
|
let bundle_map = BundleMap::create();
|
2017-03-16 08:42:30 +00:00
|
|
|
try!(bundle_map.save(path.join("bundles.map")));
|
|
|
|
try!(fs::create_dir(&path.join("backups")));
|
2017-03-10 11:43:32 +00:00
|
|
|
Ok(Repository{
|
|
|
|
path: path,
|
|
|
|
chunker: config.chunker.create(),
|
|
|
|
config: config,
|
|
|
|
index: index,
|
|
|
|
bundle_map: bundle_map,
|
|
|
|
next_content_bundle: 1,
|
|
|
|
next_meta_bundle: 0,
|
|
|
|
bundles: bundles,
|
|
|
|
content_bundle: None,
|
|
|
|
meta_bundle: None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-03-16 08:42:30 +00:00
|
|
|
pub fn open<P: AsRef<Path>>(path: P) -> Result<Self, RepositoryError> {
|
2017-03-10 11:43:32 +00:00
|
|
|
let path = path.as_ref().to_owned();
|
2017-03-16 08:42:30 +00:00
|
|
|
let config = try!(Config::load(path.join("config.yaml")));
|
2017-03-10 11:43:32 +00:00
|
|
|
let bundles = try!(BundleDb::open(
|
|
|
|
path.join("bundles"),
|
|
|
|
config.compression.clone(),
|
|
|
|
None, //FIXME: load encryption from config
|
|
|
|
config.checksum
|
2017-03-16 08:42:30 +00:00
|
|
|
));
|
|
|
|
let index = try!(Index::open(&path.join("index")));
|
|
|
|
let bundle_map = try!(BundleMap::load(path.join("bundles.map")));
|
2017-03-10 11:43:32 +00:00
|
|
|
let mut repo = Repository {
|
|
|
|
path: path,
|
|
|
|
chunker: config.chunker.create(),
|
|
|
|
config: config,
|
|
|
|
index: index,
|
|
|
|
bundle_map: bundle_map,
|
|
|
|
next_content_bundle: 0,
|
|
|
|
next_meta_bundle: 0,
|
|
|
|
bundles: bundles,
|
|
|
|
content_bundle: None,
|
|
|
|
meta_bundle: None,
|
|
|
|
};
|
|
|
|
repo.next_meta_bundle = repo.next_free_bundle_id();
|
|
|
|
repo.next_content_bundle = repo.next_free_bundle_id();
|
|
|
|
Ok(repo)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2017-03-16 08:42:30 +00:00
|
|
|
fn save_bundle_map(&self) -> Result<(), RepositoryError> {
|
|
|
|
try!(self.bundle_map.save(self.path.join("bundles.map")));
|
|
|
|
Ok(())
|
2017-03-10 11:43:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn next_free_bundle_id(&self) -> u32 {
|
|
|
|
let mut id = max(self.next_content_bundle, self.next_meta_bundle) + 1;
|
|
|
|
while self.bundle_map.get(id).is_some() {
|
|
|
|
id += 1;
|
|
|
|
}
|
|
|
|
id
|
|
|
|
}
|
|
|
|
|
2017-03-16 08:42:30 +00:00
|
|
|
pub fn flush(&mut self) -> Result<(), RepositoryError> {
|
2017-03-10 11:43:32 +00:00
|
|
|
if self.content_bundle.is_some() {
|
|
|
|
let mut finished = None;
|
|
|
|
mem::swap(&mut self.content_bundle, &mut finished);
|
|
|
|
{
|
2017-03-16 08:42:30 +00:00
|
|
|
let bundle = try!(self.bundles.add_bundle(finished.unwrap()));
|
2017-03-15 07:27:27 +00:00
|
|
|
self.bundle_map.set(self.next_content_bundle, bundle);
|
2017-03-10 11:43:32 +00:00
|
|
|
}
|
|
|
|
self.next_content_bundle = self.next_free_bundle_id()
|
|
|
|
}
|
|
|
|
if self.meta_bundle.is_some() {
|
|
|
|
let mut finished = None;
|
|
|
|
mem::swap(&mut self.meta_bundle, &mut finished);
|
|
|
|
{
|
2017-03-16 08:42:30 +00:00
|
|
|
let bundle = try!(self.bundles.add_bundle(finished.unwrap()));
|
2017-03-15 07:27:27 +00:00
|
|
|
self.bundle_map.set(self.next_meta_bundle, bundle);
|
2017-03-10 11:43:32 +00:00
|
|
|
}
|
|
|
|
self.next_meta_bundle = self.next_free_bundle_id()
|
|
|
|
}
|
2017-03-16 08:42:30 +00:00
|
|
|
try!(self.save_bundle_map());
|
2017-03-10 11:43:32 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Repository {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.flush().expect("Failed to write last bundles")
|
|
|
|
}
|
|
|
|
}
|