2019-01-12 14:39:20 +00:00
|
|
|
use std::fs;
|
|
|
|
use std::path::Path;
|
|
|
|
|
|
|
|
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-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-08-31 17:14:34 +01:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct RepositoryItem<'a> {
|
|
|
|
version: ItemVersion<'a>,
|
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> {
|
|
|
|
type Item = 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;
|
|
|
|
Some(self.index.items[current_item_number])
|
|
|
|
}
|
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-01-26 20:11:11 +00:00
|
|
|
pub fn open(path: &Path) -> Result<Repository, BakareError> {
|
|
|
|
// TODO open index from file
|
|
|
|
|
2019-08-31 17:14:34 +01:00
|
|
|
let version = IndexVersion;
|
|
|
|
Ok(Repository {
|
|
|
|
path,
|
|
|
|
index: IndexViewReadonly {
|
|
|
|
index_version: version,
|
|
|
|
items: vec![],
|
|
|
|
},
|
|
|
|
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-01-26 19:27:23 +00:00
|
|
|
pub fn store(&self, source_path: &Path) -> Result<(), BakareError> {
|
2019-01-12 14:39:20 +00:00
|
|
|
// get file id -> contents hash + original path + time of taking notes
|
|
|
|
// get storage path for File
|
|
|
|
// store file contents
|
|
|
|
// remember File
|
|
|
|
|
2019-08-31 17:14:34 +01:00
|
|
|
let destination_path = self.path.join(source_path);
|
2019-01-26 19:27:23 +00:00
|
|
|
if source_path.is_dir() {
|
2019-08-31 17:14:34 +01:00
|
|
|
fs::create_dir(destination_path.clone())?;
|
2019-01-12 14:39:20 +00:00
|
|
|
}
|
2019-08-31 17:30:50 +01:00
|
|
|
if source_path.is_file() {}
|
2019-01-12 14:39:20 +00:00
|
|
|
|
2019-01-26 19:27:23 +00:00
|
|
|
// TODO create new version, remember source_path
|
2019-01-12 14:39:20 +00:00
|
|
|
|
2019-01-26 19:27:23 +00:00
|
|
|
Ok(())
|
2019-01-12 14:39:20 +00:00
|
|
|
}
|
|
|
|
|
2019-01-26 19:27:23 +00:00
|
|
|
pub fn item(&self, path: &Path) -> Option<RepositoryItem> {
|
2019-08-31 17:30:50 +01:00
|
|
|
None
|
2019-01-12 14:39:20 +00:00
|
|
|
}
|
|
|
|
|
2019-01-26 20:20:03 +00:00
|
|
|
pub fn newest_version_for(&self, source_path: &Path) -> Result<ItemVersion, BakareError> {
|
2019-08-31 17:30:50 +01:00
|
|
|
Err(BakareError::UnknownSourcePathError)
|
2019-01-12 14:39:20 +00:00
|
|
|
}
|
|
|
|
}
|