2019-09-01 17:03:27 +01:00
|
|
|
use std::path::Path;
|
2019-09-01 22:17:51 +01:00
|
|
|
use std::{fs, io};
|
2019-09-01 10:36:42 +01:00
|
|
|
|
2019-01-12 14:39:20 +00:00
|
|
|
use crate::error::BakareError;
|
2019-09-07 11:37:31 +01:00
|
|
|
use crate::index::{Index, IndexIterator, ItemVersion};
|
2019-09-01 20:10:00 +01:00
|
|
|
use crate::repository_item::RepositoryItem;
|
2019-09-01 22:16:00 +01:00
|
|
|
use sha2::Digest;
|
|
|
|
use sha2::Sha512;
|
2019-09-01 20:10:00 +01:00
|
|
|
use std::fs::File;
|
2019-09-07 09:01:30 +01:00
|
|
|
use std::io::BufReader;
|
2019-01-12 14:39:20 +00:00
|
|
|
|
2019-01-26 19:27:23 +00:00
|
|
|
/// represents a place where backup is stored an can be restored from.
|
|
|
|
/// right now only on-disk directory storage is supported
|
2019-01-26 20:20:03 +00:00
|
|
|
/// repository always knows the newest version of the index and is responsible for syncing the index to disk
|
|
|
|
/// and making sure that different threads can access index in parallel
|
2019-01-12 14:39:20 +00:00
|
|
|
pub struct Repository<'a> {
|
2019-01-26 19:27:23 +00:00
|
|
|
/// absolute path to where the repository is stored on disk
|
2019-01-12 14:42:53 +00:00
|
|
|
path: &'a Path,
|
2019-09-01 21:19:40 +01:00
|
|
|
index: Index,
|
2019-01-26 19:27:23 +00:00
|
|
|
}
|
|
|
|
|
2019-08-31 17:14:34 +01:00
|
|
|
pub struct RepositoryIterator<'a> {
|
2019-09-01 20:10:00 +01:00
|
|
|
index_iterator: IndexIterator<'a>,
|
2019-09-01 17:03:27 +01:00
|
|
|
}
|
|
|
|
|
2019-08-31 17:14:34 +01:00
|
|
|
impl<'a> Iterator for RepositoryIterator<'a> {
|
2019-09-01 20:10:00 +01:00
|
|
|
type Item = RepositoryItem;
|
2019-01-26 19:27:23 +00:00
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
2019-09-01 20:10:00 +01:00
|
|
|
self.index_iterator.next()
|
2019-01-26 19:27:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-12 14:39:20 +00:00
|
|
|
impl<'a> Repository<'a> {
|
2019-09-01 21:19:40 +01:00
|
|
|
pub fn init(path: &Path) -> Result<(), BakareError> {
|
|
|
|
let index = Index::new(path);
|
2019-09-07 09:51:38 +01:00
|
|
|
index.save()?;
|
2019-09-01 21:19:40 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-09-01 12:57:37 +01:00
|
|
|
pub fn open(path: &Path) -> Result<Repository, BakareError> {
|
|
|
|
if !path.is_absolute() {
|
|
|
|
return Err(BakareError::RepositoryPathNotAbsolute);
|
|
|
|
}
|
|
|
|
|
2019-09-07 09:51:38 +01:00
|
|
|
let index = Index::load(path)?;
|
2019-09-01 20:10:00 +01:00
|
|
|
Ok(Repository { path, index })
|
2019-01-26 20:11:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn iter(&self) -> RepositoryIterator {
|
2019-08-31 17:14:34 +01:00
|
|
|
RepositoryIterator {
|
2019-09-01 20:10:00 +01:00
|
|
|
index_iterator: self.index.iter(),
|
2019-08-31 17:14:34 +01:00
|
|
|
}
|
2019-01-12 14:39:20 +00:00
|
|
|
}
|
|
|
|
|
2019-09-01 11:05:22 +01:00
|
|
|
pub fn store(&mut self, source_path: &Path) -> Result<(), BakareError> {
|
2019-09-01 12:57:37 +01:00
|
|
|
if !source_path.is_absolute() {
|
|
|
|
return Err(BakareError::PathToStoreNotAbsolute);
|
|
|
|
}
|
|
|
|
let destination_path: &str = &(self.path.to_string_lossy() + source_path.to_string_lossy());
|
|
|
|
let destination_path = Path::new(&destination_path);
|
|
|
|
if source_path == destination_path {
|
|
|
|
return Err(BakareError::SourceSameAsRepository);
|
|
|
|
}
|
2019-01-26 19:27:23 +00:00
|
|
|
if source_path.is_dir() {
|
2019-09-01 12:57:37 +01:00
|
|
|
fs::create_dir(destination_path)?;
|
2019-01-12 14:39:20 +00:00
|
|
|
}
|
2019-09-01 22:16:00 +01:00
|
|
|
|
2019-09-01 11:05:22 +01:00
|
|
|
if source_path.is_file() {
|
2019-09-01 12:57:37 +01:00
|
|
|
fs::create_dir_all(destination_path.parent().unwrap())?;
|
2019-09-07 10:29:27 +01:00
|
|
|
let version = Repository::calculate_version(source_path)?;
|
2019-09-01 12:57:37 +01:00
|
|
|
fs::copy(source_path, destination_path)?;
|
2019-09-01 20:10:00 +01:00
|
|
|
|
|
|
|
self.index.remember(RepositoryItem::from(
|
|
|
|
source_path,
|
|
|
|
destination_path,
|
|
|
|
destination_path.strip_prefix(self.path)?,
|
2019-09-01 22:21:34 +01:00
|
|
|
version,
|
2019-09-01 20:10:00 +01:00
|
|
|
));
|
2019-09-07 09:51:38 +01:00
|
|
|
self.index.save()?;
|
2019-09-01 11:05:22 +01:00
|
|
|
}
|
2019-01-26 19:27:23 +00:00
|
|
|
Ok(())
|
2019-01-12 14:39:20 +00:00
|
|
|
}
|
|
|
|
|
2019-09-07 11:37:31 +01:00
|
|
|
fn calculate_version(source_path: &Path) -> Result<ItemVersion, BakareError> {
|
2019-09-01 22:16:00 +01:00
|
|
|
let source_file = File::open(source_path)?;
|
|
|
|
let mut reader = BufReader::new(source_file);
|
|
|
|
let mut hasher = Sha512::new();
|
|
|
|
|
2019-09-01 22:17:51 +01:00
|
|
|
io::copy(&mut reader, &mut hasher)?;
|
2019-09-01 22:16:00 +01:00
|
|
|
|
2019-09-07 11:37:31 +01:00
|
|
|
Ok(hasher.result().as_slice().into())
|
2019-09-01 22:16:00 +01:00
|
|
|
}
|
|
|
|
|
2019-09-01 20:10:00 +01:00
|
|
|
pub fn item_by_source_path(&self, path: &Path) -> Result<Option<RepositoryItem>, BakareError> {
|
2019-09-01 17:39:42 +01:00
|
|
|
if !path.is_absolute() {
|
|
|
|
return Err(BakareError::RepositoryPathNotAbsolute);
|
2019-09-01 17:03:27 +01:00
|
|
|
}
|
2019-09-01 20:10:00 +01:00
|
|
|
Ok(self.iter().find(|i| i.original_source_path() == path))
|
2019-01-12 14:39:20 +00:00
|
|
|
}
|
|
|
|
}
|