2020-11-08 14:27:26 +00:00
|
|
|
use anyhow::Result;
|
|
|
|
use anyhow::*;
|
2018-10-04 15:29:19 +01:00
|
|
|
use std::path::Path;
|
2019-01-12 14:39:20 +00:00
|
|
|
|
2018-10-04 15:29:19 +01:00
|
|
|
use walkdir::WalkDir;
|
|
|
|
|
2019-01-12 14:39:20 +00:00
|
|
|
use crate::repository::Repository;
|
|
|
|
|
2018-10-04 15:33:01 +01:00
|
|
|
pub struct Engine<'a> {
|
2018-10-04 15:29:19 +01:00
|
|
|
source_path: &'a Path,
|
2020-12-25 16:29:00 +00:00
|
|
|
repository: &'a mut Repository,
|
2018-12-22 16:09:22 +00:00
|
|
|
}
|
|
|
|
|
2018-10-04 15:33:01 +01:00
|
|
|
impl<'a> Engine<'a> {
|
2020-12-25 16:29:00 +00:00
|
|
|
pub fn new(source_path: &'a Path, repository: &'a mut Repository) -> Result<Self> {
|
2019-09-07 15:08:47 +01:00
|
|
|
if source_path.ancestors().any(|a| a == repository.path()) {
|
2020-11-08 14:27:26 +00:00
|
|
|
return Err(anyhow!("source same as repository"));
|
2019-09-07 15:08:47 +01:00
|
|
|
}
|
|
|
|
Ok(Engine { source_path, repository })
|
2018-10-04 15:29:19 +01:00
|
|
|
}
|
|
|
|
|
2020-11-08 14:27:26 +00:00
|
|
|
pub fn backup(&mut self) -> Result<()> {
|
2018-10-04 15:29:19 +01:00
|
|
|
let walker = WalkDir::new(self.source_path);
|
2020-11-08 14:27:26 +00:00
|
|
|
let save_every = 16;
|
|
|
|
let mut save_counter = 0;
|
2018-10-04 15:29:19 +01:00
|
|
|
for maybe_entry in walker {
|
|
|
|
let entry = maybe_entry?;
|
|
|
|
if entry.path() != self.source_path {
|
2019-01-26 19:27:23 +00:00
|
|
|
self.repository.store(entry.path())?;
|
2018-10-04 15:29:19 +01:00
|
|
|
}
|
2020-11-08 14:27:26 +00:00
|
|
|
save_counter += 1;
|
|
|
|
if save_counter == save_every {
|
|
|
|
save_counter = 0;
|
|
|
|
self.repository.save_index()?;
|
|
|
|
}
|
2018-10-04 15:29:19 +01:00
|
|
|
}
|
2020-11-08 14:27:26 +00:00
|
|
|
self.repository.save_index()?;
|
2018-10-04 15:29:19 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
2018-10-06 22:15:03 +01:00
|
|
|
}
|