do symlinks last, fix index errors being fatal, change exit code

This commit is contained in:
Mark 2025-06-06 00:25:53 +02:00
parent ed090e5962
commit a742bffddc
5 changed files with 117 additions and 47 deletions

View File

@ -1,7 +1,7 @@
[package] [package]
name = "rembackup" name = "rembackup"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2024"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@ -1,5 +1,5 @@
use std::{ use std::{
fs, io, fs,
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
@ -12,12 +12,25 @@ pub fn apply_indexchanges(
source: &Path, source: &Path,
index: &Path, index: &Path,
target: &Option<PathBuf>, target: &Option<PathBuf>,
changes: &Vec<IndexChange>, changes: &[IndexChange],
gib_total: Option<f64>, gib_total: Option<f64>,
) -> io::Result<()> { ) -> usize {
let o = apply_indexchanges_int(source, index, target, changes, gib_total); // do symlinks last, as they cd, which can fail,
// and if it does, it would be fatal and stop the backup.
let (mut changes, symlink_additions) =
changes.into_iter().partition::<Vec<_>, _>(|c| match c {
IndexChange::AddDir(..)
| IndexChange::AddFile(..)
| IndexChange::RemoveFile(..)
| IndexChange::RemoveDir(..) => true,
IndexChange::AddSymlink(..) => false,
});
changes.extend(symlink_additions);
let mut failures = changes.len();
apply_indexchanges_int(source, index, target, &changes, gib_total, &mut failures);
eprintln!(); eprintln!();
o failures
} }
fn eprint_constants(changes_total: usize, gib_total: f64) -> (usize, usize, usize) { fn eprint_constants(changes_total: usize, gib_total: f64) -> (usize, usize, usize) {
@ -63,7 +76,6 @@ fn eprint_status(
let pending_prog = " ".repeat(rightpad); let pending_prog = " ".repeat(rightpad);
eprint!( eprint!(
"\r{changes_pad}{changes_applied}/{changes_total} | {gib_pad}{gib_transferred}/{gib_total:.1}GiB [{completed_prog_min}{completed_prog_max}>{pending_prog}]", "\r{changes_pad}{changes_applied}/{changes_total} | {gib_pad}{gib_transferred}/{gib_total:.1}GiB [{completed_prog_min}{completed_prog_max}>{pending_prog}]",
); );
} }
@ -71,9 +83,10 @@ pub fn apply_indexchanges_int(
source: &Path, source: &Path,
index: &Path, index: &Path,
target: &Option<PathBuf>, target: &Option<PathBuf>,
changes: &Vec<IndexChange>, changes: &[&IndexChange],
gib_total: Option<f64>, gib_total: Option<f64>,
) -> io::Result<()> { failures: &mut usize,
) {
let changes_total = changes.len(); let changes_total = changes.len();
let gib_total = gib_total.unwrap_or_else(|| { let gib_total = gib_total.unwrap_or_else(|| {
changes changes
@ -87,7 +100,7 @@ pub fn apply_indexchanges_int(
}) })
.sum() .sum()
}); });
let (prog_width, changes_len_width, gib_len_width) = eprint_constants(changes.len(), gib_total); let (prog_width, changes_len_width, gib_len_width) = eprint_constants(changes_total, gib_total);
let mut gib_transferred = 0.0; let mut gib_transferred = 0.0;
eprint_status( eprint_status(
0, 0,
@ -114,8 +127,14 @@ pub fn apply_indexchanges_int(
true true
}; };
if ok { if ok {
fs::create_dir_all(&index.join(dir))?; *failures -= 1;
let t = index.join(dir);
if let Err(e) = fs::create_dir_all(&t) {
eprintln!("\n[warn] couldn't create index directory {t:?}: {e}");
}
} }
} else {
*failures -= 1;
} }
} }
IndexChange::AddFile(file, index_file) => { IndexChange::AddFile(file, index_file) => {
@ -133,25 +152,42 @@ pub fn apply_indexchanges_int(
true true
}; };
if ok { if ok {
fs::write(&index.join(file), index_file.save())?; *failures -= 1;
let t = index.join(file);
if let Err(e) = fs::write(&t, index_file.save()) {
eprintln!("\n[warn] couldn't save index file {t:?}: {e}");
}
} }
} }
IndexChange::AddSymlink(file, link_target) => { IndexChange::AddSymlink(file, link_target) => {
let cwd = std::env::current_dir()?; let cwd = match std::env::current_dir() {
Ok(cwd) => cwd,
Err(e) => {
eprintln!("\n[err] fatal: couldn't get cwd: {e}");
return;
}
};
let ok = if let Some(target) = target { let ok = if let Some(target) = target {
let t = target.join(file); let t = target.join(file);
if let Some(p) = t.parent() { if let Some(p) = t.parent() {
let t = t.file_name().expect("a file should always have a filename"); let t = t.file_name().expect("a file should always have a filename");
std::env::set_current_dir(&p)?; if let Err(e) = std::env::set_current_dir(&p) {
let _ = std::fs::remove_file(t); eprintln!("\n[warn] couldn't cd to {p:?}: {e}");
if let Err(e) = std::os::unix::fs::symlink(&link_target, t) {
eprintln!(
"\n[warn] couldn't set file {t:?} to be a symlink to {link_target:?}: {e}"
);
false false
} else { } else {
std::env::set_current_dir(&cwd)?; let _ = std::fs::remove_file(t);
true if let Err(e) = std::os::unix::fs::symlink(&link_target, t) {
eprintln!(
"\n[warn] couldn't set file {t:?} to be a symlink to {link_target:?}: {e}"
);
false
} else {
if let Err(e) = std::env::set_current_dir(&cwd) {
eprintln!("\n[err] fatal: couldn't cd back to {cwd:?}: {e}");
return;
}
true
}
} }
} else { } else {
eprintln!("\n[warn] symlink path was empty"); eprintln!("\n[warn] symlink path was empty");
@ -161,27 +197,42 @@ pub fn apply_indexchanges_int(
true true
}; };
if ok { if ok {
*failures -= 1;
let index_file = index.join(file); let index_file = index.join(file);
if let Some(p) = index_file.parent() { if let Some(p) = index_file.parent() {
std::env::set_current_dir(&p)?; if let Err(e) = std::env::set_current_dir(&p) {
std::os::unix::fs::symlink( eprintln!("\n[warn] couldn't cd to {p:?}: {e}");
link_target, } else {
index_file if let Err(e) = std::os::unix::fs::symlink(
.file_name() link_target,
.expect("a file should always have a filename"), index_file
)?; .file_name()
.expect("a file should always have a filename"),
) {
eprintln!(
"\n[warn] couldn't set index file {index_file:?} to be a symlink to {link_target:?}: {e}"
);
}
}
} else { } else {
eprintln!("\n[warn] couldn't get parent for index file's path, so could not create the symlink"); eprintln!(
"\n[warn] couldn't get parent for index file's path, so could not create the symlink"
);
} }
} }
std::env::set_current_dir(&cwd)?; if let Err(e) = std::env::set_current_dir(&cwd) {
eprintln!("\n[err] fatal: couldn't cd back to {cwd:?}: {e}");
return;
}
} }
IndexChange::RemoveFile(file) => { IndexChange::RemoveFile(file) => {
let i = index.join(file); let i = index.join(file);
let ok = if let Some(target) = target { let ok = if let Some(target) = target {
let t = target.join(file); let t = target.join(file);
if let Err(e) = fs::remove_file(&t) { 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."); 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 false
} else { } else {
true true
@ -190,7 +241,10 @@ pub fn apply_indexchanges_int(
true true
}; };
if ok { if ok {
fs::remove_file(i)?; *failures -= 1;
if let Err(e) = fs::remove_file(&i) {
eprintln!("\n[warn] couldn't remove index file {i:?}: {e:?}");
}
} }
} }
IndexChange::RemoveDir(dir) => { IndexChange::RemoveDir(dir) => {
@ -198,7 +252,9 @@ pub fn apply_indexchanges_int(
let ok = if let Some(target) = target { let ok = if let Some(target) = target {
let t = target.join(dir); let t = target.join(dir);
if let Err(e) = fs::remove_dir_all(&t) { 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."); 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 false
} else { } else {
true true
@ -207,14 +263,17 @@ pub fn apply_indexchanges_int(
true true
}; };
if ok { if ok {
fs::remove_dir_all(i)?; *failures -= 1;
if let Err(e) = fs::remove_dir_all(&i) {
eprintln!("\n[warn] couldn't remove index directory {i:?}: {e:?}");
}
} }
} }
} }
{ {
eprint_status( eprint_status(
i + 1, i + 1,
changes.len(), changes_total,
gib_transferred, gib_transferred,
gib_total, gib_total,
prog_width, prog_width,
@ -223,5 +282,4 @@ pub fn apply_indexchanges_int(
); );
} }
} }
Ok(())
} }

View File

@ -4,6 +4,11 @@ use clap::Parser;
use crate::update_index::Settings; use crate::update_index::Settings;
/// rembackup,
/// a simple backup tool for local or remote backups.
/// run with --help for more help.
///
/// rembackup copies files from <source> to <target> using and storing information in <index>.
#[derive(Parser)] #[derive(Parser)]
#[command(author, version)] #[command(author, version)]
pub struct Args { pub struct Args {

View File

@ -17,7 +17,8 @@ mod update_index;
const EXIT_IGNORE_FAILED: u8 = 200; const EXIT_IGNORE_FAILED: u8 = 200;
const EXIT_DIFF_FAILED: u8 = 20; const EXIT_DIFF_FAILED: u8 = 20;
const EXIT_APPLY_FAILED: u8 = 30; const EXIT_APPLY_FAILED_ONE: u8 = 100;
const EXIT_APPLY_FAILED_ALL: u8 = 200;
fn main() { fn main() {
// get args // get args
@ -27,7 +28,7 @@ fn main() {
let cwd = match std::env::current_dir() { let cwd = match std::env::current_dir() {
Ok(v) => Some(v), Ok(v) => Some(v),
Err(e) => { Err(e) => {
eprintln!("[WARN] Couldn't get current directory (CWD): {e}"); eprintln!("[warn] Couldn't get current directory (CWD): {e}");
None None
} }
}; };
@ -178,7 +179,7 @@ fn main() {
if !args.noconfirm { if !args.noconfirm {
loop { loop {
if args.target.is_none() { 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!\nType 'Ok' and press enter to continue."); 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!\nType 'Ok' and press enter to continue.");
} else { } else {
eprintln!("Exclude unwanted directories/files using --ignore,\nor press enter to apply the changes."); eprintln!("Exclude unwanted directories/files using --ignore,\nor press enter to apply the changes.");
} }
@ -195,18 +196,19 @@ fn main() {
} }
} }
} }
match apply_indexchanges( let failure_count = apply_indexchanges(
&args.source, &args.source,
&args.index, &args.index,
&args.target, &args.target,
&changes, &changes,
Some(add_file_total_size_gib), Some(add_file_total_size_gib),
) { );
Ok(()) => {} eprintln!("[info] encountered {failure_count} failures");
Err(e) => { if failure_count > 0 {
eprintln!("Failed to apply: {e}"); exit(
exit(EXIT_APPLY_FAILED as _); (EXIT_APPLY_FAILED_ONE as u64 + failure_count.ilog2() as u64)
} .min(EXIT_APPLY_FAILED_ALL as u64) as _,
);
} }
} }
} }

View File

@ -14,10 +14,15 @@ use crate::{
#[derive(Clone, Default, Args)] #[derive(Clone, Default, Args)]
pub struct Settings { pub struct Settings {
/// don't sort the changes that form the backup. disables sort options. /// don't sort the changes that form the backup
///
/// disables sort options.
/// symlinks will still be created last.
#[arg(long)] #[arg(long)]
pub dont_sort: bool, pub dont_sort: bool,
/// start with smaller directories rather than larger ones /// start with smaller directories rather than larger ones
///
/// symlinks will still be created last.
#[arg(long)] #[arg(long)]
pub smallest_first: bool, pub smallest_first: bool,
/// show changes in the order in which they will be applied, not reversed /// show changes in the order in which they will be applied, not reversed