bakare/src/repository.rs

87 lines
2.1 KiB
Rust
Raw Normal View History

2019-01-12 14:39:20 +00:00
use std::error::Error;
use std::fs;
use std::io;
use std::path::Path;
use walkdir::DirEntry;
use crate::error::BakareError;
use crate::ItemVersion;
use crate::IndexViewReadonly;
use crate::IndexVersion;
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,
newest_index_version: IndexVersion
2019-01-12 14:39:20 +00:00
}
2019-01-26 19:27:23 +00:00
pub struct RepositoryItem {
version: ItemVersion
2019-01-26 19:27:23 +00:00
}
pub struct RepositoryIterator {
version: IndexVersion,
index: IndexViewReadonly
}
2019-01-26 20:11:11 +00:00
impl<'a> Iterator for RepositoryIterator {
2019-01-26 19:27:23 +00:00
type Item = RepositoryItem;
fn next(&mut self) -> Option<Self::Item> {
unimplemented!()
}
}
impl RepositoryItem {
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
Ok(Repository { path, index: IndexViewReadonly {} })
2019-01-26 20:11:11 +00:00
}
pub fn iter(&self) -> RepositoryIterator {
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-01-26 19:27:23 +00:00
if source_path.is_dir() {
fs::create_dir(self.path.join(source_path))?;
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, self.path.join(source_path))?;
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!()
}
}
2019-01-26 19:27:23 +00:00