bakare/src/repository.rs

101 lines
2.7 KiB
Rust
Raw Normal View History

2019-01-12 14:39:20 +00:00
use std::fs;
use std::path::Path;
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
}
#[derive(Copy, Clone)]
pub struct RepositoryItem<'a> {
version: ItemVersion<'a>,
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
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> {
if self.current_item_number >= self.index.items.len() - 1 {
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
}
}
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-01-26 20:11:11 +00:00
pub fn open(path: &Path) -> Result<Repository, BakareError> {
// TODO open index from file
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 {
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
let destination_path = self.path.join(source_path);
2019-01-26 19:27:23 +00:00
if source_path.is_dir() {
fs::create_dir(destination_path.clone())?;
2019-01-12 14:39:20 +00:00
}
2019-01-26 19:27:23 +00:00
if source_path.is_file() {
fs::copy(source_path, destination_path.clone())?;
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-01-12 14:39:20 +00:00
unimplemented!()
}
pub fn newest_version_for(&self, source_path: &Path) -> Result<ItemVersion, BakareError> {
2019-01-12 14:39:20 +00:00
unimplemented!()
}
}