2019-01-12 14:39:20 +00:00
|
|
|
use std::fs;
|
2019-09-01 10:36:42 +01:00
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
|
|
|
|
use walkdir::{DirEntry, WalkDir};
|
2019-01-12 14:39:20 +00:00
|
|
|
|
|
|
|
use crate::error::BakareError;
|
2019-01-26 20:20:03 +00:00
|
|
|
use crate::IndexVersion;
|
2019-08-31 17:14:34 +01:00
|
|
|
use crate::IndexViewReadonly;
|
|
|
|
use crate::ItemVersion;
|
2019-09-01 10:36:42 +01:00
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::rc::Rc;
|
|
|
|
use std::sync::Arc;
|
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-08-31 17:14:34 +01:00
|
|
|
index: IndexViewReadonly<'a>,
|
|
|
|
newest_index_version: IndexVersion,
|
2019-01-12 14:39:20 +00:00
|
|
|
}
|
|
|
|
|
2019-09-01 12:57:37 +01:00
|
|
|
#[derive(Clone, Debug)]
|
2019-08-31 17:14:34 +01:00
|
|
|
pub struct RepositoryItem<'a> {
|
|
|
|
version: ItemVersion<'a>,
|
2019-09-01 11:05:22 +01:00
|
|
|
relative_path: Box<Path>,
|
2019-01-26 19:27:23 +00:00
|
|
|
}
|
|
|
|
|
2019-08-31 17:14:34 +01:00
|
|
|
pub struct RepositoryIterator<'a> {
|
2019-01-26 20:20:03 +00:00
|
|
|
version: IndexVersion,
|
2019-08-31 17:14:34 +01:00
|
|
|
index: &'a IndexViewReadonly<'a>,
|
|
|
|
current_item_number: usize,
|
2019-01-26 20:20:03 +00:00
|
|
|
}
|
2019-01-26 20:11:11 +00:00
|
|
|
|
2019-08-31 17:14:34 +01:00
|
|
|
impl<'a> Iterator for RepositoryIterator<'a> {
|
2019-09-01 10:36:42 +01:00
|
|
|
type Item = &'a RepositoryItem<'a>;
|
2019-01-26 19:27:23 +00:00
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
2019-08-31 17:30:50 +01:00
|
|
|
if self.index.items.is_empty() || self.current_item_number >= self.index.items.len() - 1 {
|
2019-08-31 17:14:34 +01:00
|
|
|
None
|
|
|
|
} else {
|
|
|
|
let current_item_number = self.current_item_number;
|
|
|
|
self.current_item_number += 1;
|
2019-09-01 10:36:42 +01:00
|
|
|
Some(&self.index.items[current_item_number])
|
2019-08-31 17:14:34 +01:00
|
|
|
}
|
2019-01-26 19:27:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-31 17:14:34 +01:00
|
|
|
impl<'a> RepositoryItem<'a> {
|
2019-01-26 20:20:03 +00:00
|
|
|
pub fn version(&self) -> &ItemVersion {
|
2019-01-26 19:27:23 +00:00
|
|
|
&self.version
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-12 14:39:20 +00:00
|
|
|
impl<'a> Repository<'a> {
|
2019-09-01 12:57:37 +01:00
|
|
|
fn get_all_files_recursively(path: &Path) -> Result<Vec<Box<Path>>, BakareError> {
|
|
|
|
let path_text = path.to_string_lossy();
|
2019-09-01 10:36:42 +01:00
|
|
|
let walker = WalkDir::new(path);
|
2019-09-01 12:57:37 +01:00
|
|
|
|
|
|
|
let mut result = vec![];
|
|
|
|
|
|
|
|
for maybe_entry in walker {
|
|
|
|
let entry = maybe_entry?;
|
|
|
|
if entry.path() != path {
|
|
|
|
if entry.path().is_file() {
|
|
|
|
result.push(Box::from(path));
|
|
|
|
}
|
|
|
|
if entry.path().is_dir() {
|
|
|
|
let children = Repository::get_all_files_recursively(entry.path())?;
|
|
|
|
|
|
|
|
for child in children {
|
|
|
|
result.push(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
pub fn open(path: &Path) -> Result<Repository, BakareError> {
|
|
|
|
if !path.is_absolute() {
|
|
|
|
return Err(BakareError::RepositoryPathNotAbsolute);
|
|
|
|
}
|
|
|
|
println!("opening repository at {}", path.display());
|
|
|
|
|
|
|
|
let all_files = Repository::get_all_files_recursively(path)?;
|
2019-09-01 10:36:42 +01:00
|
|
|
let all_items: Vec<RepositoryItem> = all_files
|
|
|
|
.into_iter()
|
|
|
|
.map(|p| RepositoryItem {
|
|
|
|
version: ItemVersion(""),
|
2019-09-01 12:57:37 +01:00
|
|
|
relative_path: p,
|
2019-09-01 10:36:42 +01:00
|
|
|
})
|
|
|
|
.collect();
|
2019-01-26 20:11:11 +00:00
|
|
|
|
2019-08-31 17:14:34 +01:00
|
|
|
let version = IndexVersion;
|
2019-09-01 12:57:37 +01:00
|
|
|
println!("opened repository at {} - {} items present", path.display(), all_items.len());
|
2019-08-31 17:14:34 +01:00
|
|
|
Ok(Repository {
|
|
|
|
path,
|
|
|
|
index: IndexViewReadonly {
|
|
|
|
index_version: version,
|
2019-09-01 10:36:42 +01:00
|
|
|
items: all_items,
|
2019-08-31 17:14:34 +01:00
|
|
|
},
|
|
|
|
newest_index_version: version,
|
|
|
|
})
|
2019-01-26 20:11:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn iter(&self) -> RepositoryIterator {
|
2019-08-31 17:14:34 +01:00
|
|
|
RepositoryIterator {
|
|
|
|
index: &self.index,
|
|
|
|
version: self.index.index_version,
|
|
|
|
current_item_number: 0,
|
|
|
|
}
|
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 11:05:22 +01:00
|
|
|
self.index.items.push(RepositoryItem {
|
|
|
|
version: ItemVersion(""),
|
2019-09-01 12:57:37 +01:00
|
|
|
relative_path: Box::from(destination_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 10:36:42 +01:00
|
|
|
pub fn item(&self, path: &Path) -> Option<&RepositoryItem> {
|
2019-09-01 11:05:22 +01:00
|
|
|
self.index.items.iter().find(|i| *i.relative_path == *path)
|
2019-01-12 14:39:20 +00:00
|
|
|
}
|
|
|
|
|
2019-09-01 10:36:42 +01:00
|
|
|
pub fn newest_version_for(&self, item: RepositoryItem) -> ItemVersion {
|
|
|
|
ItemVersion("")
|
2019-01-12 14:39:20 +00:00
|
|
|
}
|
|
|
|
}
|