Restoring older versions works

This commit is contained in:
Cyryl Płotnicki 2019-09-07 13:58:32 +01:00
parent a3f979c32d
commit 8c43efcdce
4 changed files with 30 additions and 16 deletions

View file

@ -86,6 +86,20 @@ impl<'a> Repository<'a> {
Ok(hasher.result().as_slice().into()) Ok(hasher.result().as_slice().into())
} }
pub fn item_by_source_path_and_version(
&self,
path: &Path,
version: &ItemVersion,
) -> Result<Option<RepositoryItem>, BakareError> {
if !path.is_absolute() {
return Err(BakareError::RepositoryPathNotAbsolute);
}
Ok(self
.iter()
.find(|i| i.original_source_path() == path && i.version() == version))
}
pub fn item_by_source_path(&self, path: &Path) -> Result<Option<RepositoryItem>, BakareError> { pub fn item_by_source_path(&self, path: &Path) -> Result<Option<RepositoryItem>, BakareError> {
if !path.is_absolute() { if !path.is_absolute() {
return Err(BakareError::RepositoryPathNotAbsolute); return Err(BakareError::RepositoryPathNotAbsolute);

View file

@ -38,6 +38,7 @@ impl RepositoryItem {
if !self.absolute_path.exists() { if !self.absolute_path.exists() {
return Err(BakareError::CorruptedRepoNoFile); return Err(BakareError::CorruptedRepoNoFile);
} }
println!("restoring {} to {}", &self.absolute_path.display(), &target_path.display());
fs::copy(&self.absolute_path, &target_path)?; fs::copy(&self.absolute_path, &target_path)?;
Ok(()) Ok(())

View file

@ -25,14 +25,8 @@ impl<'a> Engine<'a> {
Ok(()) Ok(())
} }
fn restore(&self, item: &RepositoryItem) -> Result<(), BakareError> { pub fn restore(&self, item: &RepositoryItem) -> Result<(), BakareError> {
println!("restoring {}", item);
item.save(self.target_path)?; item.save(self.target_path)?;
Ok(()) Ok(())
} }
pub fn restore_as_of_version(&self, item: &RepositoryItem, version: &ItemVersion) -> Result<(), BakareError> {
println!("restoring {}", item);
Ok(())
}
} }

View file

@ -71,11 +71,13 @@ fn restore_older_version_of_file() -> Result<(), BakareError> {
backup_engine.backup()?; backup_engine.backup()?;
} }
let old_version = {
let reading_repository = Repository::open(repository_path.as_path())?; let reading_repository = Repository::open(repository_path.as_path())?;
let item = reading_repository.item_by_source_path(&file_path)?; let item = reading_repository.item_by_source_path(&file_path)?;
assert!(item.is_some()); assert!(item.is_some());
let item = item.unwrap(); let item = item.unwrap();
let old_version = item.version(); item.version().clone()
};
{ {
let new_contents = "totally new contents"; let new_contents = "totally new contents";
@ -88,14 +90,17 @@ fn restore_older_version_of_file() -> Result<(), BakareError> {
let restore_repository = Repository::open(repository_path.as_path())?; let restore_repository = Repository::open(repository_path.as_path())?;
let restore_target = tempdir()?; let restore_target = tempdir()?;
let restore_engine = restore::Engine::new(&restore_repository, &restore_target.path())?; let restore_engine = restore::Engine::new(&restore_repository, &restore_target.path())?;
restore_engine.restore_as_of_version(&item, old_version)?; let old_item = restore_repository.item_by_source_path_and_version(&file_path, &old_version)?;
restore_engine.restore(&old_item.unwrap())?;
assert_target_file_contents(restore_target.path(), relative_path_text, old_contents) let source_file_full_path = source.file_path(relative_path_text);
let restored_file_path = restore_target.path().join(source_file_full_path.strip_prefix("/")?);
assert_target_file_contents(&restored_file_path, old_contents)
} }
fn assert_target_file_contents(target: &Path, filename: &str, expected_contents: &str) -> Result<(), BakareError> { fn assert_target_file_contents(restored_path: &Path, expected_contents: &str) -> Result<(), BakareError> {
let restored_path = target.join(filename);
let mut actual_contents = String::new(); let mut actual_contents = String::new();
assert!(restored_path.exists(), "Expected '{}' to be there", restored_path.display());
File::open(restored_path)?.read_to_string(&mut actual_contents)?; File::open(restored_path)?.read_to_string(&mut actual_contents)?;
assert_eq!(expected_contents, actual_contents); assert_eq!(expected_contents, actual_contents);
Ok(()) Ok(())