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;
|
2018-10-04 16:11:47 +01:00
|
|
|
use storage::Version;
|
2018-10-04 15:29:19 +01:00
|
|
|
use walkdir::DirEntry;
|
|
|
|
use walkdir::WalkDir;
|
|
|
|
|
2018-10-04 15:33:01 +01:00
|
|
|
pub struct Engine<'a> {
|
2018-10-04 15:29:19 +01:00
|
|
|
source_path: &'a Path,
|
|
|
|
repository_path: &'a Path,
|
|
|
|
}
|
|
|
|
|
2018-10-04 15:33:01 +01:00
|
|
|
impl<'a> Engine<'a> {
|
2018-10-04 15:29:19 +01:00
|
|
|
pub fn new(source_path: &'a Path, repository_path: &'a Path) -> Self {
|
2018-10-04 15:33:01 +01:00
|
|
|
Engine {
|
2018-10-04 15:29:19 +01:00
|
|
|
source_path,
|
|
|
|
repository_path,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn backup(&self) -> Result<(), io::Error> {
|
|
|
|
let walker = WalkDir::new(self.source_path);
|
|
|
|
for maybe_entry in walker {
|
|
|
|
let entry = maybe_entry?;
|
|
|
|
if entry.path() != self.source_path {
|
|
|
|
self.process_entry(&entry)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-10-04 16:11:47 +01:00
|
|
|
pub fn file_version(&self, path: &Path) -> Version {
|
|
|
|
Version(0)
|
2018-10-04 15:29:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn process_entry(&self, entry: &DirEntry) -> Result<(), io::Error> {
|
2018-10-04 16:11:47 +01:00
|
|
|
// TODO: remember entry in index
|
|
|
|
|
|
|
|
// TODO: store file data
|
|
|
|
|
2018-10-04 15:29:19 +01:00
|
|
|
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(())
|
|
|
|
}
|
|
|
|
}
|