This commit is contained in:
Cyryl Płotnicki 2018-10-04 15:33:01 +01:00
parent f97e45b572
commit 5ec019d16c
4 changed files with 21 additions and 27 deletions

View file

@ -1,17 +1,17 @@
use std::fs;
use std::io; use std::io;
use std::path::Path; use std::path::Path;
use walkdir::DirEntry; use walkdir::DirEntry;
use walkdir::WalkDir; use walkdir::WalkDir;
use std::fs;
pub struct BackupEngine<'a> { pub struct Engine<'a> {
source_path: &'a Path, source_path: &'a Path,
repository_path: &'a Path, repository_path: &'a Path,
} }
impl<'a> BackupEngine<'a> { impl<'a> Engine<'a> {
pub fn new(source_path: &'a Path, repository_path: &'a Path) -> Self { pub fn new(source_path: &'a Path, repository_path: &'a Path) -> Self {
BackupEngine { Engine {
source_path, source_path,
repository_path, repository_path,
} }
@ -33,7 +33,6 @@ impl<'a> BackupEngine<'a> {
} }
fn process_entry(&self, entry: &DirEntry) -> Result<(), io::Error> { fn process_entry(&self, entry: &DirEntry) -> Result<(), io::Error> {
if entry.file_type().is_dir() { if entry.file_type().is_dir() {
fs::create_dir(self.repository_path.join(entry.file_name()))?; fs::create_dir(self.repository_path.join(entry.file_name()))?;
} }
@ -43,4 +42,3 @@ impl<'a> BackupEngine<'a> {
Ok(()) Ok(())
} }
} }

View file

@ -1,3 +1 @@
fn main() {} fn main() {}

View file

@ -1,36 +1,36 @@
use std::fs;
use std::io; use std::io;
use std::path::Path; use std::path::Path;
use walkdir::WalkDir;
use walkdir::DirEntry; use walkdir::DirEntry;
use std::fs; use walkdir::WalkDir;
pub struct RestoreEngine<'a> { pub struct Engine<'a> {
repository_path: &'a Path, repository_path: &'a Path,
target_path: &'a Path, target_path: &'a Path,
} }
pub enum RestoreDescriptor { pub enum WhatToRestore {
All, All,
SpecificPath(String), SpecificPath(String),
} }
impl<'a> RestoreEngine<'a> { impl<'a> Engine<'a> {
pub fn new(repository_path: &'a Path, target_path: &'a Path) -> Self { pub fn new(repository_path: &'a Path, target_path: &'a Path) -> Self {
RestoreEngine { Engine {
repository_path, repository_path,
target_path, target_path,
} }
} }
pub fn restore_all(&self) -> Result<(), io::Error> { pub fn restore_all(&self) -> Result<(), io::Error> {
self.restore(RestoreDescriptor::All) self.restore(WhatToRestore::All)
} }
fn restore(&self, what: RestoreDescriptor) -> Result<(), io::Error> { fn restore(&self, what: WhatToRestore) -> Result<(), io::Error> {
self.restore_as_of_version(what, 0) self.restore_as_of_version(what, 0)
} }
pub fn restore_as_of_version(&self, what: RestoreDescriptor, version: u64) -> Result<(), io::Error> { pub fn restore_as_of_version(&self, what: WhatToRestore, version: u64) -> Result<(), io::Error> {
let walker = WalkDir::new(self.repository_path); let walker = WalkDir::new(self.repository_path);
for maybe_entry in walker { for maybe_entry in walker {
match maybe_entry { match maybe_entry {
@ -55,4 +55,3 @@ impl<'a> RestoreEngine<'a> {
Ok(()) Ok(())
} }
} }

View file

@ -1,7 +1,10 @@
extern crate bakare; extern crate bakare;
extern crate tempfile;
extern crate dir_diff; extern crate dir_diff;
extern crate tempfile;
use bakare::backup;
use bakare::restore;
use bakare::restore::WhatToRestore::SpecificPath;
use dir_diff::is_different; use dir_diff::is_different;
use std::fs::File; use std::fs::File;
use std::io::Error; use std::io::Error;
@ -10,9 +13,6 @@ use std::io::Write;
use std::path::Path; use std::path::Path;
use tempfile::tempdir; use tempfile::tempdir;
use tempfile::TempDir; use tempfile::TempDir;
use bakare::backup::BackupEngine;
use bakare::restore::RestoreDescriptor::SpecificPath;
use bakare::restore::RestoreEngine;
#[test] #[test]
fn restore_backed_up_files() -> Result<(), Error> { fn restore_backed_up_files() -> Result<(), Error> {
@ -30,11 +30,11 @@ fn restore_backed_up_files() -> Result<(), Error> {
fn restore_older_version_of_file() -> Result<(), Error> { fn restore_older_version_of_file() -> Result<(), Error> {
let source = Source::new()?; let source = Source::new()?;
let repository = tempdir()?; let repository = tempdir()?;
let backup_engine = BackupEngine::new(source.path(), repository.path()); let backup_engine = backup::Engine::new(source.path(), repository.path());
let path = "some path"; let path = "some path";
let new_contents = "totally new contents"; let new_contents = "totally new contents";
let restore_target = tempdir()?; let restore_target = tempdir()?;
let restore_engine = RestoreEngine::new(repository.path(), &restore_target.path()); let restore_engine = restore::Engine::new(repository.path(), &restore_target.path());
let old_contents = "some old contents"; let old_contents = "some old contents";
source.write_text_to_file(path, old_contents)?; source.write_text_to_file(path, old_contents)?;
@ -62,11 +62,11 @@ fn assert_target_file_contents(target: &Path, filename: &str, expected_contents:
} }
fn assert_same_after_restore(source_path: &Path, repository_path: &Path) -> Result<(), Error> { fn assert_same_after_restore(source_path: &Path, repository_path: &Path) -> Result<(), Error> {
let backup_engine = BackupEngine::new(source_path, repository_path); let backup_engine = backup::Engine::new(source_path, repository_path);
backup_engine.backup()?; backup_engine.backup()?;
let restore_target = tempdir()?; let restore_target = tempdir()?;
let restore_engine = RestoreEngine::new(repository_path, &restore_target.path()); let restore_engine = restore::Engine::new(repository_path, &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();
@ -91,4 +91,3 @@ impl Source {
self.directory.path() self.directory.path()
} }
} }