bakare/src/repository.rs

180 lines
5.6 KiB
Rust
Raw Normal View History

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-09-01 17:03:27 +01:00
use walkdir::WalkDir;
2019-01-12 14:39:20 +00:00
use crate::error::BakareError;
use crate::IndexVersion;
use crate::IndexViewReadonly;
use crate::ItemVersion;
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
/// 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,
index: IndexViewReadonly<'a>,
newest_index_version: IndexVersion,
2019-01-12 14:39:20 +00:00
}
2019-09-01 17:03:27 +01:00
#[derive(Clone, Debug, PartialOrd, PartialEq, Ord, Eq)]
pub struct RepositoryItem<'a> {
version: ItemVersion<'a>,
2019-09-01 11:05:22 +01:00
relative_path: Box<Path>,
2019-09-01 17:03:27 +01:00
absolute_path: Box<Path>,
2019-01-26 19:27:23 +00:00
}
pub struct RepositoryIterator<'a> {
version: IndexVersion,
index: &'a IndexViewReadonly<'a>,
current_item_number: usize,
}
2019-01-26 20:11:11 +00:00
2019-09-01 17:03:27 +01:00
impl<'a> RepositoryItem<'a> {
pub fn save(&self, save_to: &Path) -> Result<(), BakareError> {
if !save_to.is_absolute() {
return Err(BakareError::PathToStoreNotAbsolute);
}
let target_path = save_to.join(self.relative_path.clone());
let parent = target_path.parent().unwrap();
if !parent.exists() {
println!("Creating {}", parent.display());
fs::create_dir_all(parent)?;
}
if !self.absolute_path.exists() {
return Err(BakareError::CorruptedRepoNoFile);
}
println!("Saving {} to {}", self.absolute_path.display(), target_path.display());
fs::copy(self.absolute_path.clone(), target_path.clone())?;
Ok(())
}
}
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-09-01 17:30:45 +01:00
if self.index.items.is_empty() || self.current_item_number > self.index.items.len() - 1 {
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-01-26 19:27:23 +00:00
}
}
impl<'a> RepositoryItem<'a> {
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
pub fn open(path: &Path) -> Result<Repository, BakareError> {
if !path.is_absolute() {
return Err(BakareError::RepositoryPathNotAbsolute);
}
let all_files = Repository::get_all_files_recursively(path)?;
2019-09-01 17:03:27 +01:00
let all_items: Result<Vec<RepositoryItem>, BakareError> = all_files
2019-09-01 10:36:42 +01:00
.into_iter()
2019-09-01 17:03:27 +01:00
.map(|p| {
let relative_path = Box::from(p.strip_prefix(path)?);
Ok(RepositoryItem {
version: ItemVersion(""),
relative_path,
absolute_path: Box::from(p),
})
2019-09-01 10:36:42 +01:00
})
.collect();
2019-09-01 17:03:27 +01:00
let mut all_items = all_items?;
all_items.sort();
2019-01-26 20:11:11 +00:00
let version = IndexVersion;
2019-09-01 12:57:37 +01:00
println!("opened repository at {} - {} items present", path.display(), all_items.len());
Ok(Repository {
path,
index: IndexViewReadonly {
index_version: version,
2019-09-01 10:36:42 +01:00
items: all_items,
},
newest_index_version: version,
})
2019-01-26 20:11:11 +00:00
}
pub fn iter(&self) -> RepositoryIterator {
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 17:03:27 +01:00
relative_path: Box::from(destination_path.strip_prefix(self.path)?),
absolute_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 17:03:27 +01:00
let relative_path = {
if path.is_relative() {
Some(path)
} else {
path.strip_prefix(self.path).ok()
}
};
if let Some(relative_path) = relative_path {
self.index.items.iter().find(|i| *i.relative_path == *relative_path)
} else {
None
}
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
}
2019-09-01 17:03:27 +01:00
fn get_all_files_recursively(path: &Path) -> Result<Vec<Box<Path>>, BakareError> {
let walker = WalkDir::new(path);
let mut result = vec![];
for maybe_entry in walker {
let entry = maybe_entry?;
if entry.path() == path {
continue;
}
if entry.path().is_file() {
result.push(Box::from(entry.path()));
}
}
Ok(result)
}
2019-01-12 14:39:20 +00:00
}