2018-12-22 11:31:27 +00:00
|
|
|
use crate::storage::Version;
|
2018-10-04 15:33:01 +01:00
|
|
|
use std::fs;
|
2018-10-04 15:29:19 +01:00
|
|
|
use std::io;
|
|
|
|
use std::path::Path;
|
|
|
|
use walkdir::DirEntry;
|
2018-10-04 15:33:01 +01:00
|
|
|
use walkdir::WalkDir;
|
2018-10-04 15:29:19 +01:00
|
|
|
|
2018-10-04 15:33:01 +01:00
|
|
|
pub struct Engine<'a> {
|
2018-10-04 15:29:19 +01:00
|
|
|
repository_path: &'a Path,
|
|
|
|
target_path: &'a Path,
|
|
|
|
}
|
|
|
|
|
2018-10-04 15:33:01 +01:00
|
|
|
pub enum WhatToRestore {
|
2018-10-04 15:29:19 +01:00
|
|
|
All,
|
|
|
|
SpecificPath(String),
|
|
|
|
}
|
|
|
|
|
2018-10-04 15:33:01 +01:00
|
|
|
impl<'a> Engine<'a> {
|
2018-10-04 15:29:19 +01:00
|
|
|
pub fn new(repository_path: &'a Path, target_path: &'a Path) -> Self {
|
2018-10-04 15:33:01 +01:00
|
|
|
Engine {
|
2018-10-04 15:29:19 +01:00
|
|
|
repository_path,
|
|
|
|
target_path,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn restore_all(&self) -> Result<(), io::Error> {
|
2018-10-04 15:33:01 +01:00
|
|
|
self.restore(WhatToRestore::All)
|
2018-10-04 15:29:19 +01:00
|
|
|
}
|
|
|
|
|
2018-10-04 15:33:01 +01:00
|
|
|
fn restore(&self, what: WhatToRestore) -> Result<(), io::Error> {
|
2018-12-22 11:31:27 +00:00
|
|
|
self.restore_as_of_version(what, Version::Newest)
|
2018-10-04 15:29:19 +01:00
|
|
|
}
|
|
|
|
|
2018-10-04 16:11:47 +01:00
|
|
|
pub fn restore_as_of_version(&self, what: WhatToRestore, version: Version) -> Result<(), io::Error> {
|
2018-10-04 15:29:19 +01:00
|
|
|
let walker = WalkDir::new(self.repository_path);
|
|
|
|
for maybe_entry in walker {
|
|
|
|
match maybe_entry {
|
|
|
|
Ok(entry) => {
|
|
|
|
if entry.path() != self.repository_path {
|
|
|
|
self.process_entry(&entry)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(error) => return Err(error.into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn process_entry(&self, entry: &DirEntry) -> Result<(), io::Error> {
|
|
|
|
if entry.file_type().is_dir() {
|
|
|
|
fs::create_dir(self.target_path.join(entry.file_name()))?;
|
|
|
|
}
|
|
|
|
if entry.file_type().is_file() {
|
|
|
|
fs::copy(entry.path(), self.target_path.join(entry.file_name()))?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|