bakare/src/repository.rs

117 lines
3.3 KiB
Rust
Raw Normal View History

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;
use crate::IndexVersion;
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
/// 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 10:36:42 +01:00
#[derive(Clone)]
pub struct RepositoryItem<'a> {
version: ItemVersion<'a>,
2019-09-01 10:36:42 +01:00
relative_path: Rc<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
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 {
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-01-26 20:11:11 +00:00
pub fn open(path: &Path) -> Result<Repository, BakareError> {
2019-09-01 10:36:42 +01:00
let walker = WalkDir::new(path);
let all_files: Result<Vec<DirEntry>, _> = walker
.into_iter()
.filter_entry(|e| e.path() != path && !e.path().is_dir())
.collect();
let all_files = all_files?;
let all_items: Vec<RepositoryItem> = all_files
.into_iter()
.map(|p| RepositoryItem {
version: ItemVersion(""),
relative_path: Rc::from(p.path()),
})
.collect();
2019-01-26 20:11:11 +00:00
let version = IndexVersion;
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-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-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-09-01 10:36:42 +01: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-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
}
}