From 0ed94b3f011a2d3c22bdc4affb502720be22c371 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Sat, 14 Sep 2024 18:59:23 +0200 Subject: add restoration of files and packages --- src/pathinfo.rs | 44 +++++++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 13 deletions(-) (limited to 'src/pathinfo.rs') diff --git a/src/pathinfo.rs b/src/pathinfo.rs index 03b8a6b..1231ff8 100644 --- a/src/pathinfo.rs +++ b/src/pathinfo.rs @@ -6,7 +6,7 @@ use std::{ }; use serde::{Deserialize, Serialize}; -use tracing::info; +use tracing::{debug, info}; use crate::{ backup::{Backup, Id}, @@ -186,6 +186,33 @@ impl PathInfo { Ok(()) } + pub fn restore(&self, config: &Config, backup_root: &str) -> Result<()> { + if self.is_file { + info!(?self.rel_location, "Restore File"); + let backup_path = if let Some(last_modified) = self.last_modified.clone() { + let backup = Backup::from_index(config, &last_modified)?; + &backup.get_location(config).get_absolute_dir(config) + } else { + backup_root + }; + let backup_loc = format!("{}/{}", backup_path, self.rel_location); + let system_loc = self.get_absolute_path(); + debug!(?backup_loc, ?system_loc, "copy"); + + if let Some(parents) = system_loc.parent() { + create_dir_all(parents)?; + } + + std::fs::copy(backup_loc, system_loc)?; + } else { + for path in &self.children { + path.restore(config, backup_root)?; + } + } + + Ok(()) + } + fn get_abs_path(location_root: &str, rel_location: &str) -> PathBuf { let path = format!("{location_root}/{rel_location}"); PathBuf::from(path) @@ -196,10 +223,7 @@ impl PathInfo { return Err(Error::InvalidDirectory(value.to_string())); }; if split.0.starts_with('~') { - return Ok(( - split.1.to_string(), - LocationRoot::User, - )); + return Ok((split.1.to_string(), LocationRoot::User)); }; Ok(( split.1.to_string(), @@ -330,17 +354,11 @@ mod tests { let mut values_ok: Vec<(&str, (String, LocationRoot))> = Vec::new(); values_ok.push(( "~/.config/nvim", - ( - ".config/nvim".to_string(), - LocationRoot::User, - ), + (".config/nvim".to_string(), LocationRoot::User), )); values_ok.push(( "u:test/.config/nvim", - ( - ".config/nvim".to_string(), - LocationRoot::User, - ), + (".config/nvim".to_string(), LocationRoot::User), )); values_ok.push(( "r:/.config/nvim", -- cgit v1.2.3