From 5e03e584dba51b8f11b9cedc83f316a6a0007b2c Mon Sep 17 00:00:00 2001 From: Mark Date: Thu, 5 Oct 2023 15:57:36 +0200 Subject: [PATCH] when no target is specified, show an extra warning and just build an index. with this, you can use rembackup on the `target` server to generate a new index if, for some reason, your local index gets messed up and has to be regenerated --- src/apply_indexchanges.rs | 78 ++++++++++++++++++++++++++++----------- src/args.rs | 5 ++- src/main.rs | 3 ++ 3 files changed, 64 insertions(+), 22 deletions(-) diff --git a/src/apply_indexchanges.rs b/src/apply_indexchanges.rs index f92f881..28849c6 100755 --- a/src/apply_indexchanges.rs +++ b/src/apply_indexchanges.rs @@ -1,4 +1,7 @@ -use std::{fs, io, path::Path}; +use std::{ + fs, io, + path::{Path, PathBuf}, +}; use crate::{indexchanges::IndexChange, repr_file::ReprFile}; @@ -8,7 +11,7 @@ use crate::{indexchanges::IndexChange, repr_file::ReprFile}; pub fn apply_indexchanges( source: &Path, index: &Path, - target: &Path, + target: &Option, changes: &Vec, ) -> io::Result<()> { let o = apply_indexchanges_int(source, index, target, changes); @@ -18,7 +21,7 @@ pub fn apply_indexchanges( pub fn apply_indexchanges_int( source: &Path, index: &Path, - target: &Path, + target: &Option, changes: &Vec, ) -> io::Result<()> { let len_width = changes.len().to_string().len(); @@ -32,42 +35,75 @@ pub fn apply_indexchanges_int( for (i, change) in changes.iter().enumerate() { match change { IndexChange::AddDir(dir) => { - let t = target.join(dir); - if let Some(e) = fs::create_dir(&t).err().and_then(|e| { - if matches!(e.kind(), io::ErrorKind::AlreadyExists) { - None + let ok = if let Some(target) = target { + let t = target.join(dir); + if let Some(e) = fs::create_dir(&t).err().and_then(|e| { + if matches!(e.kind(), io::ErrorKind::AlreadyExists) { + None + } else { + Some(e) + } + }) { + eprintln!("\n[warn] couldn't create directory {t:?}: {e}"); + false } else { - Some(e) + true } - }) { - eprintln!("\n[warn] couldn't create directory {t:?}: {e}"); } else { + true + }; + if ok { fs::create_dir(&index.join(dir))?; } } IndexChange::AddFile(file, index_file) => { - let s = source.join(file); - let t = target.join(file); - if let Err(e) = fs::copy(&s, &t) { - eprintln!("\n[warn] couldn't copy file from {s:?} to {t:?}: {e}"); + let ok = if let Some(target) = target { + let s = source.join(file); + let t = target.join(file); + if let Err(e) = fs::copy(&s, &t) { + eprintln!("\n[warn] couldn't copy file from {s:?} to {t:?}: {e}"); + false + } else { + true + } + } else { + true + }; + if ok { + fs::write(&index.join(file), index_file.save())?; } - fs::write(&index.join(file), index_file.save())?; } IndexChange::RemoveFile(file) => { let i = index.join(file); - let t = target.join(file); - if let Err(e) = fs::remove_file(&t) { - eprintln!("\n[warn] couldn't remove file {t:?}, keeping index file {i:?}: {e:?}\n If this error keeps appearing, check if the file was deleted on the target system but still exists in the index. if yes, consider manually deleting it."); + let ok = if let Some(target) = target { + let t = target.join(file); + if let Err(e) = fs::remove_file(&t) { + eprintln!("\n[warn] couldn't remove file {t:?}, keeping index file {i:?}: {e:?}\n If this error keeps appearing, check if the file was deleted on the target system but still exists in the index. if yes, consider manually deleting it."); + false + } else { + true + } } else { + true + }; + if ok { fs::remove_file(i)?; } } IndexChange::RemoveDir(dir) => { let i = index.join(dir); - let t = target.join(dir); - if let Err(e) = fs::remove_dir_all(&t) { - eprintln!("\n[warn] couldn't remove directory {t:?}, keeping index files under {i:?}: {e:?}\n If this error keeps appearing, check if the directory was deleted on the target system but still exists in the index. if yes, consider manually deleting it."); + let ok = if let Some(target) = target { + let t = target.join(dir); + if let Err(e) = fs::remove_dir_all(&t) { + eprintln!("\n[warn] couldn't remove directory {t:?}, keeping index files under {i:?}: {e:?}\n If this error keeps appearing, check if the directory was deleted on the target system but still exists in the index. if yes, consider manually deleting it."); + false + } else { + true + } } else { + true + }; + if ok { fs::remove_dir_all(i)?; } } diff --git a/src/args.rs b/src/args.rs index d05e89b..a470d9d 100755 --- a/src/args.rs +++ b/src/args.rs @@ -5,10 +5,13 @@ use clap::Parser; #[derive(Parser)] #[command(author, version)] pub struct Args { + /// the data to be backed up #[arg()] pub source: PathBuf, + /// the index used to determine which files have been modified #[arg()] pub index: PathBuf, + /// where your backup will be stored #[arg()] - pub target: PathBuf, + pub target: Option, } diff --git a/src/main.rs b/src/main.rs index 966a830..df08826 100755 --- a/src/main.rs +++ b/src/main.rs @@ -45,6 +45,9 @@ fn main() { eprintln!(" - remove file"); eprintln!(" [-] remove directory (and all contents!)"); eprintln!("Press Enter to to apply these actions."); + if args.target.is_none() { + eprintln!("[WARN] You didn't set a `target` directory!\n[WARN] Be careful not to update your index without actually applying the changes to the `target` filesystem!"); + } // apply changes if std::io::stdin().read_line(&mut String::new()).is_ok() { match apply_indexchanges(&args.source, &args.index, &args.target, &changes) {