From 4b9514f364f1711555208bd4b9316e7d5c597dc5 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Sun, 8 Sep 2024 18:54:41 +0200 Subject: fixed last modified --- .gitignore | 3 ++- src/error.rs | 2 +- src/main.rs | 4 ++-- src/packages.rs | 2 +- src/pathinfo.rs | 41 +++++++++++++++++++++++++++++++++-------- 5 files changed, 39 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index e39d245..57e4770 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target -/backup-test* +/backup* +tags diff --git a/src/error.rs b/src/error.rs index c43c1fc..0cf4dca 100644 --- a/src/error.rs +++ b/src/error.rs @@ -34,5 +34,5 @@ pub enum Error { Io { #[from] source: std::io::Error, - } + }, } diff --git a/src/main.rs b/src/main.rs index acb728f..e67e535 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,8 +5,8 @@ use packages::{pacman::Pacman, PackageManager}; mod backup; mod config; mod error; -mod pathinfo; mod packages; +mod pathinfo; fn main() -> color_eyre::Result<()> { color_eyre::install()?; @@ -14,7 +14,7 @@ fn main() -> color_eyre::Result<()> { let mut cfg = Config::load()?; cfg.user.push("fx".to_string()); cfg.directories.push("~/.config/nvim".to_string()); - cfg.directories.push("~/.config/hypr".to_string()); + // cfg.directories.push("~/.config/hypr".to_string()); cfg.root = "./backup".to_string(); // cfg.root = "./backup-test".to_string(); // cfg.directories.push("u:/code/proj/fxbaup/backup-test-dir".to_string()); diff --git a/src/packages.rs b/src/packages.rs index 7ac1736..e7b4c3d 100644 --- a/src/packages.rs +++ b/src/packages.rs @@ -9,7 +9,7 @@ pub mod portage; pub struct Package { pub id: String, pub version: String, - pub explicit: bool + pub explicit: bool, } pub trait PackageManager { diff --git a/src/pathinfo.rs b/src/pathinfo.rs index 641e7ef..3e4b5c2 100644 --- a/src/pathinfo.rs +++ b/src/pathinfo.rs @@ -52,6 +52,7 @@ impl PathInfo { // FIX: Check if new last modified is newer than old one last_modified = handle.last_modified.clone(); }; + last_modified = None; children.push(handle); } Self { @@ -94,10 +95,8 @@ impl PathInfo { return Ok(None); }; - let files = last_backup.files.clone(); - let last_file_opt = files.iter().find(|file| file.rel_location == rel_location && file.location_root == *location_root); - + let last_file_opt = Self::find_last_modified(files, rel_location, location_root); let Some(last_file) = last_file_opt else { // File didn't exist last Backup println!("File didn't exist last Backup"); @@ -133,6 +132,32 @@ impl PathInfo { Ok(Some(modified_backup.id.clone())) } + pub fn find_last_modified( + files: Vec, + rel_location: &str, + location_root: &LocationRoot, + ) -> Option { + for path in files { + if path.is_file { + println!("Checking {}", path.rel_location); + println!("File rel: {} ?= {}", path.rel_location, rel_location); + println!("File rot: {} ?= {}", path.location_root, location_root); + if path.rel_location == rel_location && path.location_root == *location_root { + println!("Return Some"); + return Some(path); + }; + } else { + println!("Checking all in {path:?}"); + let is_modified = + PathInfo::find_last_modified(path.children, rel_location, location_root); + if is_modified.is_some() { + return is_modified; + }; + } + } + None + } + pub fn get_absolute_path(&self) -> PathBuf { Self::get_abs_path(&self.location_root.to_string(), &self.rel_location) } @@ -142,11 +167,7 @@ impl PathInfo { return Ok(()); } println!("Save File {:?}", self.rel_location); - if !self.is_file { - for child in &self.children { - child.save(backup_root)?; - } - } else { + if self.is_file { let new_path = format!("{}/{}", backup_root, self.rel_location); // println!("New Path: {new_path}"); // println!("Old Path: {:?}", self.get_absolute_path()); @@ -155,6 +176,10 @@ impl PathInfo { create_dir_all(parent)?; } std::fs::copy(self.get_absolute_path(), new_path)?; + } else { + for child in &self.children { + child.save(backup_root)?; + } }; Ok(()) -- cgit v1.2.3