model mutability
This commit is contained in:
parent
0229d6c9fd
commit
d2e5c480a9
3 changed files with 42 additions and 30 deletions
|
@ -7,15 +7,15 @@ use crate::repository::Repository;
|
||||||
|
|
||||||
pub struct Engine<'a> {
|
pub struct Engine<'a> {
|
||||||
source_path: &'a Path,
|
source_path: &'a Path,
|
||||||
repository: &'a Repository<'a>,
|
repository: &'a mut Repository<'a>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Engine<'a> {
|
impl<'a> Engine<'a> {
|
||||||
pub fn new(source_path: &'a Path, repository: &'a Repository) -> Self {
|
pub fn new(source_path: &'a Path, repository: &'a mut Repository<'a>) -> Self {
|
||||||
Engine { source_path, repository }
|
Engine { source_path, repository }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn backup(&self) -> Result<(), BakareError> {
|
pub fn backup(&mut self) -> Result<(), BakareError> {
|
||||||
let walker = WalkDir::new(self.source_path);
|
let walker = WalkDir::new(self.source_path);
|
||||||
for maybe_entry in walker {
|
for maybe_entry in walker {
|
||||||
let entry = maybe_entry?;
|
let entry = maybe_entry?;
|
||||||
|
|
|
@ -25,7 +25,7 @@ pub struct Repository<'a> {
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct RepositoryItem<'a> {
|
pub struct RepositoryItem<'a> {
|
||||||
version: ItemVersion<'a>,
|
version: ItemVersion<'a>,
|
||||||
relative_path: Rc<Path>,
|
relative_path: Box<Path>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct RepositoryIterator<'a> {
|
pub struct RepositoryIterator<'a> {
|
||||||
|
@ -66,7 +66,7 @@ impl<'a> Repository<'a> {
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|p| RepositoryItem {
|
.map(|p| RepositoryItem {
|
||||||
version: ItemVersion(""),
|
version: ItemVersion(""),
|
||||||
relative_path: Rc::from(p.path()),
|
relative_path: Box::from(p.path()),
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
@ -89,25 +89,23 @@ impl<'a> Repository<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn store(&self, source_path: &Path) -> Result<(), BakareError> {
|
pub fn store(&mut self, source_path: &Path) -> Result<(), BakareError> {
|
||||||
// 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);
|
let destination_path = self.path.join(source_path);
|
||||||
if source_path.is_dir() {
|
if source_path.is_dir() {
|
||||||
fs::create_dir(destination_path.clone())?;
|
fs::create_dir(destination_path.clone())?;
|
||||||
}
|
}
|
||||||
if source_path.is_file() {}
|
if source_path.is_file() {
|
||||||
|
// TODO: copy file
|
||||||
// TODO create new version, remember source_path
|
self.index.items.push(RepositoryItem {
|
||||||
|
version: ItemVersion(""),
|
||||||
|
relative_path: destination_path.into_boxed_path(),
|
||||||
|
});
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn item(&self, path: &Path) -> Option<&RepositoryItem> {
|
pub fn item(&self, path: &Path) -> Option<&RepositoryItem> {
|
||||||
None
|
self.index.items.iter().find(|i| *i.relative_path == *path)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn newest_version_for(&self, item: RepositoryItem) -> ItemVersion {
|
pub fn newest_version_for(&self, item: RepositoryItem) -> ItemVersion {
|
||||||
|
|
|
@ -26,24 +26,34 @@ fn restore_backed_up_files() -> Result<(), BakareError> {
|
||||||
fn restore_older_version_of_file() -> Result<(), BakareError> {
|
fn restore_older_version_of_file() -> Result<(), BakareError> {
|
||||||
let source = TempSource::new()?;
|
let source = TempSource::new()?;
|
||||||
let repository_path = tempdir()?.into_path();
|
let repository_path = tempdir()?.into_path();
|
||||||
let repository = Repository::open(repository_path.as_path())?;
|
let restore_repository = Repository::open(repository_path.as_path())?;
|
||||||
let backup_engine = backup::Engine::new(source.path(), &repository);
|
|
||||||
let relative_path_text = "some path";
|
let relative_path_text = "some path";
|
||||||
let file_path = source.file_path(relative_path_text);
|
let file_path = source.file_path(relative_path_text);
|
||||||
let new_contents = "totally new contents";
|
let new_contents = "totally new contents";
|
||||||
let restore_target = tempdir()?;
|
let restore_target = tempdir()?;
|
||||||
let restore_engine = restore::Engine::new(&repository, &restore_target.path());
|
let restore_engine = restore::Engine::new(&restore_repository, &restore_target.path());
|
||||||
let old_contents = "some old contents";
|
let old_contents = "some old contents";
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut backup_repository = Repository::open(repository_path.as_path())?;
|
||||||
|
let mut backup_engine = backup::Engine::new(source.path(), &mut backup_repository);
|
||||||
source.write_text_to_file(relative_path_text, old_contents)?;
|
source.write_text_to_file(relative_path_text, old_contents)?;
|
||||||
backup_engine.backup()?;
|
backup_engine.backup()?;
|
||||||
let file_id = repository.item(&file_path);
|
}
|
||||||
|
|
||||||
|
let backup_repository = Repository::open(repository_path.as_path())?;
|
||||||
|
let file_id = backup_repository.item(&file_path);
|
||||||
assert!(file_id.is_some());
|
assert!(file_id.is_some());
|
||||||
let file_id = file_id.unwrap();
|
let file_id = file_id.unwrap();
|
||||||
let old_version = file_id.version();
|
let old_version = file_id.version();
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut backup_repository = Repository::open(repository_path.as_path())?;
|
||||||
|
let mut backup_engine = backup::Engine::new(source.path(), &mut backup_repository);
|
||||||
source.write_text_to_file(relative_path_text, new_contents)?;
|
source.write_text_to_file(relative_path_text, new_contents)?;
|
||||||
backup_engine.backup()?;
|
backup_engine.backup()?;
|
||||||
|
}
|
||||||
|
|
||||||
restore_engine.restore_as_of_version(&file_id, old_version)?;
|
restore_engine.restore_as_of_version(&file_id, old_version)?;
|
||||||
|
|
||||||
|
@ -60,14 +70,18 @@ fn assert_target_file_contents(target: &Path, filename: &str, expected_contents:
|
||||||
|
|
||||||
fn assert_same_after_restore(source_path: &Path) -> Result<(), BakareError> {
|
fn assert_same_after_restore(source_path: &Path) -> Result<(), BakareError> {
|
||||||
let repository_path = tempdir()?.into_path();
|
let repository_path = tempdir()?.into_path();
|
||||||
let repository = Repository::open(repository_path.as_path())?;
|
|
||||||
let backup_engine = backup::Engine::new(source_path, &repository);
|
|
||||||
backup_engine.backup()?;
|
|
||||||
|
|
||||||
let restore_target = tempdir()?;
|
let restore_target = tempdir()?;
|
||||||
let restore_engine = restore::Engine::new(&repository, &restore_target.path());
|
|
||||||
restore_engine.restore_all()?;
|
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut backup_repository = Repository::open(repository_path.as_path())?;
|
||||||
|
let mut backup_engine = backup::Engine::new(source_path, &mut backup_repository);
|
||||||
|
backup_engine.backup()?;
|
||||||
|
}
|
||||||
|
{
|
||||||
|
let mut restore_repository = Repository::open(repository_path.as_path())?;
|
||||||
|
let restore_engine = restore::Engine::new(&mut restore_repository, &restore_target.path());
|
||||||
|
restore_engine.restore_all()?;
|
||||||
|
}
|
||||||
let are_source_and_target_different = is_different(source_path, &restore_target.path()).unwrap();
|
let are_source_and_target_different = is_different(source_path, &restore_target.path()).unwrap();
|
||||||
assert!(!are_source_and_target_different);
|
assert!(!are_source_and_target_different);
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
Loading…
Reference in a new issue