2019-01-12 14:39:20 +00:00
|
|
|
use std::fs;
|
2019-09-01 17:03:27 +01:00
|
|
|
use std::path::Path;
|
2019-09-01 10:36:42 +01:00
|
|
|
|
2019-01-12 14:39:20 +00:00
|
|
|
use crate::error::BakareError;
|
2019-09-01 20:10:00 +01:00
|
|
|
use crate::index::{Index, IndexIterator};
|
|
|
|
use crate::repository_item::RepositoryItem;
|
|
|
|
use std::fs::File;
|
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);
|
|
|
|
let index_file = File::create(index.index_file_path())?;
|
|
|
|
serde_cbor::to_writer(index_file, &index)?;
|
|
|
|
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-01 20:10:00 +01:00
|
|
|
let index_file_path = path.join("index");
|
|
|
|
let index_file = File::open(index_file_path)?;
|
2019-09-01 21:19:40 +01:00
|
|
|
let index: Index = serde_cbor::from_reader(index_file)?;
|
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 11:05:22 +01:00
|
|
|
if source_path.is_file() {
|
2019-09-01 12:57:37 +01:00
|
|
|
println!("storing {} as {}", source_path.display(), destination_path.display());
|
|
|
|
fs::create_dir_all(destination_path.parent().unwrap())?;
|
|
|
|
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 11:05:22 +01:00
|
|
|
}
|
2019-01-26 19:27:23 +00:00
|
|
|
Ok(())
|
2019-01-12 14:39:20 +00: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
|
|
|
println!(
|
|
|
|
"trying to find {} in a repo [{}] of {} items",
|
|
|
|
path.display(),
|
|
|
|
self.path.display(),
|
2019-09-01 20:10:00 +01:00
|
|
|
self.index.len()
|
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
|
|
|
}
|
|
|
|
}
|