bakare/src/backup.rs

39 lines
1.1 KiB
Rust
Raw Normal View History

2021-05-16 19:15:07 +01:00
use std::path::Path;
2020-12-25 21:52:40 +00:00
use crate::repository::Repository;
use anyhow::Result;
use anyhow::*;
2021-05-16 19:15:07 +01:00
use walkdir::WalkDir;
2019-01-12 14:39:20 +00:00
2018-10-04 15:33:01 +01:00
pub struct Engine<'a> {
2021-05-16 19:15:07 +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> {
2021-05-16 19:15:07 +01:00
pub fn new(source_path: &'a Path, repository: &'a mut Repository) -> Result<Self> {
2020-12-25 21:52:40 +00:00
let mut ancestors = vec![];
2021-05-16 19:15:07 +01:00
let mut current = Some(source_path.to_path_buf());
2020-12-25 21:52:40 +00:00
while let Some(path) = current {
2021-05-16 19:15:07 +01:00
ancestors.push(path.to_path_buf());
current = path.parent().map(|p| p.to_path_buf());
2020-12-25 21:52:40 +00:00
}
2021-05-16 19:15:07 +01:00
if ancestors.into_iter().any(|a| a == repository.path()) {
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
}
pub fn backup(&mut self) -> Result<()> {
2021-05-16 19:15:07 +01:00
let walker = WalkDir::new(self.source_path);
2018-10-04 15:29:19 +01:00
for maybe_entry in walker {
let entry = maybe_entry?;
2021-05-16 19:15:07 +01:00
if entry.path() != self.source_path {
2021-10-22 21:20:53 +01:00
self.repository.store(entry.path())?;
}
2018-10-04 15:29:19 +01:00
}
self.repository.save_index()?;
2018-10-04 15:29:19 +01:00
Ok(())
}
}