2020-12-25 21:52:40 +00:00
|
|
|
use crate::repository::Repository;
|
2020-11-08 14:27:26 +00:00
|
|
|
use anyhow::Result;
|
|
|
|
use anyhow::*;
|
2020-12-25 21:52:40 +00:00
|
|
|
use vfs::VfsPath;
|
2019-01-12 14:39:20 +00:00
|
|
|
|
2018-10-04 15:33:01 +01:00
|
|
|
pub struct Engine<'a> {
|
2020-12-25 21:52:40 +00:00
|
|
|
source_path: &'a VfsPath,
|
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 21:52:40 +00:00
|
|
|
pub fn new(source_path: &'a VfsPath, repository: &'a mut Repository) -> Result<Self> {
|
|
|
|
let mut ancestors = vec![];
|
|
|
|
let mut current = Some(source_path.clone());
|
|
|
|
while let Some(path) = current {
|
|
|
|
ancestors.push(path.clone());
|
|
|
|
current = path.parent();
|
|
|
|
}
|
|
|
|
if ancestors.into_iter().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<()> {
|
2020-12-25 21:52:40 +00:00
|
|
|
let walker = self.source_path.walk_dir()?;
|
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?;
|
2020-12-25 21:52:40 +00:00
|
|
|
if &entry != self.source_path {
|
|
|
|
self.repository.store(&entry)?;
|
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
|
|
|
}
|