2018-09-02 18:04:21 +01:00
|
|
|
extern crate core;
|
2018-09-02 11:13:37 +01:00
|
|
|
extern crate crypto;
|
|
|
|
extern crate dir_diff;
|
2018-09-02 14:43:17 +01:00
|
|
|
extern crate tempfile;
|
|
|
|
extern crate walkdir;
|
2018-09-02 11:13:37 +01:00
|
|
|
|
2018-09-02 18:04:21 +01:00
|
|
|
use std::fs;
|
|
|
|
use std::io;
|
2018-09-02 11:13:37 +01:00
|
|
|
use std::path::Path;
|
2018-09-02 14:43:17 +01:00
|
|
|
use walkdir::DirEntry;
|
|
|
|
use walkdir::WalkDir;
|
|
|
|
|
|
|
|
struct BackupEngine<'a> {
|
|
|
|
source_path: &'a Path,
|
|
|
|
repository_path: &'a Path,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> BackupEngine<'a> {
|
|
|
|
fn new(source_path: &'a Path, repository_path: &'a Path) -> Self {
|
|
|
|
BackupEngine {
|
|
|
|
source_path,
|
|
|
|
repository_path,
|
|
|
|
}
|
|
|
|
}
|
2018-09-02 11:13:37 +01:00
|
|
|
|
2018-09-02 18:04:21 +01:00
|
|
|
fn backup(&self) -> Result<(), io::Error> {
|
2018-09-02 14:43:17 +01:00
|
|
|
let walker = WalkDir::new(self.source_path);
|
|
|
|
for maybe_entry in walker {
|
|
|
|
match maybe_entry {
|
2018-09-02 18:04:21 +01:00
|
|
|
Ok(entry) => {
|
|
|
|
if entry.path() != self.source_path {
|
|
|
|
self.process_entry(entry)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(error) => return Err(error.into()),
|
2018-09-02 14:43:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
2018-09-02 11:13:37 +01:00
|
|
|
}
|
|
|
|
|
2018-09-02 18:04:21 +01:00
|
|
|
fn process_entry(&self, entry: DirEntry) -> Result<(), io::Error> {
|
|
|
|
if entry.file_type().is_dir() {
|
|
|
|
fs::create_dir(self.repository_path.join(entry.file_name()))?;
|
|
|
|
}
|
|
|
|
if entry.file_type().is_file() {
|
|
|
|
fs::copy(entry.path(), self.repository_path.join(entry.file_name()))?;
|
|
|
|
}
|
|
|
|
Ok(())
|
2018-09-02 14:43:17 +01:00
|
|
|
}
|
2018-09-02 11:13:37 +01:00
|
|
|
}
|
|
|
|
|
2018-09-02 18:04:21 +01:00
|
|
|
struct RestoreEngine<'a> {
|
|
|
|
repository_path: &'a Path,
|
|
|
|
target_path: &'a Path,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> RestoreEngine<'a> {
|
|
|
|
fn new(repository_path: &'a Path, target_path: &'a Path) -> Self {
|
|
|
|
RestoreEngine {
|
|
|
|
repository_path,
|
|
|
|
target_path,
|
|
|
|
}
|
2018-09-02 11:13:37 +01:00
|
|
|
}
|
|
|
|
|
2018-09-02 18:04:21 +01:00
|
|
|
fn restore(&self) -> Result<(), io::Error> {
|
|
|
|
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(())
|
|
|
|
}
|
2018-09-02 11:13:37 +01:00
|
|
|
}
|
2018-08-17 17:58:18 +01:00
|
|
|
|
2018-09-01 09:44:13 +01:00
|
|
|
mod rustback {
|
2018-08-17 17:58:18 +01:00
|
|
|
|
2018-08-18 18:39:40 +01:00
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod should {
|
|
|
|
|
2018-09-01 09:44:13 +01:00
|
|
|
use super::*;
|
2018-09-02 14:43:17 +01:00
|
|
|
use dir_diff::is_different;
|
2018-09-01 09:44:13 +01:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::Error;
|
2018-09-02 18:04:21 +01:00
|
|
|
use std::io::Write;
|
2018-09-02 11:13:37 +01:00
|
|
|
use tempfile::tempdir;
|
2018-09-01 09:44:13 +01:00
|
|
|
|
2018-08-18 18:39:40 +01:00
|
|
|
#[test]
|
2018-09-01 09:44:13 +01:00
|
|
|
fn be_able_to_restore_backed_up_files() -> Result<(), Error> {
|
2018-09-02 11:13:37 +01:00
|
|
|
let source = tempdir()?;
|
2018-09-02 13:27:04 +01:00
|
|
|
|
|
|
|
File::create(source.path().join("first"))?.write_all("some contents".as_bytes())?;
|
|
|
|
File::create(source.path().join("second"))?.write_all("some contents".as_bytes())?;
|
|
|
|
File::create(source.path().join("third"))?.write_all("some other contents".as_bytes())?;
|
2018-09-01 09:44:13 +01:00
|
|
|
|
2018-09-02 14:18:58 +01:00
|
|
|
let repository = tempdir()?;
|
|
|
|
let backup_engine = BackupEngine::new(&source.path(), &repository.path());
|
2018-09-02 18:04:21 +01:00
|
|
|
backup_engine.backup()?;
|
2018-09-01 09:44:13 +01:00
|
|
|
|
2018-09-02 14:18:58 +01:00
|
|
|
let restore_target = tempdir()?;
|
|
|
|
let restore_engine = RestoreEngine::new(&repository.path(), &restore_target.path());
|
2018-09-02 18:04:21 +01:00
|
|
|
restore_engine.restore()?;
|
2018-09-01 09:44:13 +01:00
|
|
|
|
2018-09-02 14:43:17 +01:00
|
|
|
let are_source_and_target_different =
|
|
|
|
is_different(&source.path(), &restore_target.path()).unwrap();
|
|
|
|
assert!(!are_source_and_target_different);
|
2018-09-01 09:44:13 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2018-08-18 18:39:40 +01:00
|
|
|
|
2018-09-01 09:44:13 +01:00
|
|
|
}
|