diff options
author | fxqnlr <[email protected]> | 2024-09-14 18:59:23 +0200 |
---|---|---|
committer | fxqnlr <[email protected]> | 2024-09-14 18:59:23 +0200 |
commit | 0ed94b3f011a2d3c22bdc4affb502720be22c371 (patch) | |
tree | 9fb2cdbdccfa8c9cf5d9b8d3ccc48443fe6706a6 /src/pathinfo.rs | |
parent | 1b99a4a1ed7772c9b68e59f46e493ea5b4715239 (diff) | |
download | arbs-0ed94b3f011a2d3c22bdc4affb502720be22c371.tar arbs-0ed94b3f011a2d3c22bdc4affb502720be22c371.tar.gz arbs-0ed94b3f011a2d3c22bdc4affb502720be22c371.zip |
add restoration of files and packages
Diffstat (limited to 'src/pathinfo.rs')
-rw-r--r-- | src/pathinfo.rs | 44 |
1 files changed, 31 insertions, 13 deletions
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::{ | |||
6 | }; | 6 | }; |
7 | 7 | ||
8 | use serde::{Deserialize, Serialize}; | 8 | use serde::{Deserialize, Serialize}; |
9 | use tracing::info; | 9 | use tracing::{debug, info}; |
10 | 10 | ||
11 | use crate::{ | 11 | use crate::{ |
12 | backup::{Backup, Id}, | 12 | backup::{Backup, Id}, |
@@ -186,6 +186,33 @@ impl PathInfo { | |||
186 | Ok(()) | 186 | Ok(()) |
187 | } | 187 | } |
188 | 188 | ||
189 | pub fn restore(&self, config: &Config, backup_root: &str) -> Result<()> { | ||
190 | if self.is_file { | ||
191 | info!(?self.rel_location, "Restore File"); | ||
192 | let backup_path = if let Some(last_modified) = self.last_modified.clone() { | ||
193 | let backup = Backup::from_index(config, &last_modified)?; | ||
194 | &backup.get_location(config).get_absolute_dir(config) | ||
195 | } else { | ||
196 | backup_root | ||
197 | }; | ||
198 | let backup_loc = format!("{}/{}", backup_path, self.rel_location); | ||
199 | let system_loc = self.get_absolute_path(); | ||
200 | debug!(?backup_loc, ?system_loc, "copy"); | ||
201 | |||
202 | if let Some(parents) = system_loc.parent() { | ||
203 | create_dir_all(parents)?; | ||
204 | } | ||
205 | |||
206 | std::fs::copy(backup_loc, system_loc)?; | ||
207 | } else { | ||
208 | for path in &self.children { | ||
209 | path.restore(config, backup_root)?; | ||
210 | } | ||
211 | } | ||
212 | |||
213 | Ok(()) | ||
214 | } | ||
215 | |||
189 | fn get_abs_path(location_root: &str, rel_location: &str) -> PathBuf { | 216 | fn get_abs_path(location_root: &str, rel_location: &str) -> PathBuf { |
190 | let path = format!("{location_root}/{rel_location}"); | 217 | let path = format!("{location_root}/{rel_location}"); |
191 | PathBuf::from(path) | 218 | PathBuf::from(path) |
@@ -196,10 +223,7 @@ impl PathInfo { | |||
196 | return Err(Error::InvalidDirectory(value.to_string())); | 223 | return Err(Error::InvalidDirectory(value.to_string())); |
197 | }; | 224 | }; |
198 | if split.0.starts_with('~') { | 225 | if split.0.starts_with('~') { |
199 | return Ok(( | 226 | return Ok((split.1.to_string(), LocationRoot::User)); |
200 | split.1.to_string(), | ||
201 | LocationRoot::User, | ||
202 | )); | ||
203 | }; | 227 | }; |
204 | Ok(( | 228 | Ok(( |
205 | split.1.to_string(), | 229 | split.1.to_string(), |
@@ -330,17 +354,11 @@ mod tests { | |||
330 | let mut values_ok: Vec<(&str, (String, LocationRoot))> = Vec::new(); | 354 | let mut values_ok: Vec<(&str, (String, LocationRoot))> = Vec::new(); |
331 | values_ok.push(( | 355 | values_ok.push(( |
332 | "~/.config/nvim", | 356 | "~/.config/nvim", |
333 | ( | 357 | (".config/nvim".to_string(), LocationRoot::User), |
334 | ".config/nvim".to_string(), | ||
335 | LocationRoot::User, | ||
336 | ), | ||
337 | )); | 358 | )); |
338 | values_ok.push(( | 359 | values_ok.push(( |
339 | "u:test/.config/nvim", | 360 | "u:test/.config/nvim", |
340 | ( | 361 | (".config/nvim".to_string(), LocationRoot::User), |
341 | ".config/nvim".to_string(), | ||
342 | LocationRoot::User, | ||
343 | ), | ||
344 | )); | 362 | )); |
345 | values_ok.push(( | 363 | values_ok.push(( |
346 | "r:/.config/nvim", | 364 | "r:/.config/nvim", |