This commit is contained in:
Cyryl Płotnicki 2019-01-12 14:42:53 +00:00
parent 2a96cd2396
commit 4e76d046d1
5 changed files with 17 additions and 31 deletions

View file

@ -14,16 +14,12 @@ use crate::Version;
pub struct Engine<'a> { pub struct Engine<'a> {
source_path: &'a Path, source_path: &'a Path,
repository: &'a Repository<'a> repository: &'a 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 Repository) -> Self {
Engine { Engine { source_path, repository }
source_path,
repository,
}
} }
pub fn backup(&self) -> Result<(), BakareError> { pub fn backup(&self) -> Result<(), BakareError> {
@ -36,5 +32,4 @@ impl<'a> Engine<'a> {
} }
Ok(()) Ok(())
} }
} }

View file

@ -5,8 +5,8 @@ use std::io;
use failure::Fail; use failure::Fail;
pub mod error;
pub mod backup; pub mod backup;
pub mod error;
pub mod restore; pub mod restore;
pub mod source; pub mod source;
@ -18,5 +18,3 @@ struct RepositoryRelativePath {}
struct Index<'a> { struct Index<'a> {
versions: HashMap<&'a RepositoryRelativePath, &'a Version>, versions: HashMap<&'a RepositoryRelativePath, &'a Version>,
} }

View file

@ -10,16 +10,14 @@ use crate::Version;
/// represents a place where backup is stored an can be restored from. E.g. a directory, a cloud service etc /// represents a place where backup is stored an can be restored from. E.g. a directory, a cloud service etc
pub struct Repository<'a> { pub struct Repository<'a> {
path: &'a Path path: &'a Path,
} }
pub struct StoredItemId; pub struct StoredItemId;
pub struct RelativePath; pub struct RelativePath;
impl<'a> Repository<'a> { impl<'a> Repository<'a> {
pub fn new(path: &Path) -> Result<Repository, BakareError> { pub fn new(path: &Path) -> Result<Repository, BakareError> {
Ok(Repository { Ok(Repository { path })
path
})
} }
pub fn store_entry(&self, entry: &DirEntry) -> Result<(), BakareError> { pub fn store_entry(&self, entry: &DirEntry) -> Result<(), BakareError> {
@ -65,4 +63,3 @@ impl<'a> Iterator for &Repository<'a> {
unimplemented!() unimplemented!()
} }
} }

View file

@ -16,13 +16,9 @@ pub struct Engine<'a> {
target_path: &'a Path, target_path: &'a Path,
} }
impl<'a> Engine<'a> { impl<'a> Engine<'a> {
pub fn new(repository: &'a Repository, target_path: &'a Path) -> Self { pub fn new(repository: &'a Repository, target_path: &'a Path) -> Self {
Engine { Engine { repository, target_path }
repository,
target_path,
}
} }
pub fn restore_all(&self) -> Result<(), BakareError> { pub fn restore_all(&self) -> Result<(), BakareError> {

View file

@ -3,25 +3,23 @@ use bakare::restore;
use bakare::source::Source; use bakare::source::Source;
use bakare::error::BakareError;
use bakare::repository::Repository;
use dir_diff::is_different; use dir_diff::is_different;
use std::fs::File; use std::fs::File;
use std::io::Read; use std::io::Read;
use std::path::Path; use std::path::Path;
use tempfile::tempdir; use tempfile::tempdir;
use bakare::error::BakareError;
use bakare::repository::Repository;
#[test] #[test]
fn restore_backed_up_files() -> Result<(), BakareError> { fn restore_backed_up_files() -> Result<(), BakareError> {
let source = Source::new()?; let source = Source::new()?;
let repository_path = tempdir()?.into_path();
let repository = Repository::new(repository_path.as_path())?;
source.write_text_to_file("first", "some contents")?; source.write_text_to_file("first", "some contents")?;
source.write_text_to_file("second", "some contents")?; source.write_text_to_file("second", "some contents")?;
source.write_text_to_file("third", "some other contents")?; source.write_text_to_file("third", "some other contents")?;
assert_same_after_restore(source.path(), &repository) assert_same_after_restore(source.path())
} }
#[test] #[test]
@ -51,9 +49,6 @@ fn restore_older_version_of_file() -> Result<(), BakareError> {
Ok(()) Ok(())
} }
// TODO: restore latest version by default
// TODO: deduplicate data
fn assert_target_file_contents(target: &Path, filename: &str, expected_contents: &str) -> Result<(), BakareError> { fn assert_target_file_contents(target: &Path, filename: &str, expected_contents: &str) -> Result<(), BakareError> {
let restored_path = target.join(filename); let restored_path = target.join(filename);
let mut actual_contents = String::new(); let mut actual_contents = String::new();
@ -62,15 +57,20 @@ fn assert_target_file_contents(target: &Path, filename: &str, expected_contents:
Ok(()) Ok(())
} }
fn assert_same_after_restore(source_path: &Path, repository: &Repository) -> Result<(), BakareError> { fn assert_same_after_restore(source_path: &Path) -> Result<(), BakareError> {
let backup_engine = backup::Engine::new(source_path, repository); let repository_path = tempdir()?.into_path();
let repository = Repository::new(repository_path.as_path())?;
let backup_engine = backup::Engine::new(source_path, &repository);
backup_engine.backup()?; backup_engine.backup()?;
let restore_target = tempdir()?; let restore_target = tempdir()?;
let restore_engine = restore::Engine::new(repository, &restore_target.path()); let restore_engine = restore::Engine::new(&repository, &restore_target.path());
restore_engine.restore_all()?; 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(())
} }
// TODO: restore latest version by default
// TODO: deduplicate data