From 96ff047957c44344c7d8d373073a7b807a77f85d Mon Sep 17 00:00:00 2001 From: Donald Buczek Date: Wed, 17 Aug 2022 10:47:35 +0200 Subject: [PATCH] Abort more noisily cmirror may abort on various error conditions. There was a complaint, that the error mesasge might be overlooked because of the normal diagnostic output: buczek@theinternet:~$ cmirror /tmp/a /tmp/b --delete theinternet.molgen.mpg.de: creating empty ./1 theinternet.molgen.mpg.de: creating empty ./10 theinternet.molgen.mpg.de: creating empty ./2 theinternet.molgen.mpg.de: creating empty ./3 theinternet.molgen.mpg.de: creating empty ./4 theinternet.molgen.mpg.de: creating empty ./5 theinternet.molgen.mpg.de: creating empty ./6 theinternet.molgen.mpg.de: creating empty ./7 theinternet.molgen.mpg.de: creating empty ./8 theinternet.molgen.mpg.de: creating empty ./9 theinternet.molgen.mpg.de: rm -r ./xx rm: cannot remove './xx/aa': Permission denied buczek@theinternet:~$ To make this more clear, an additional diagnostic output was requested to show that cmirror didn't finish its job: buczek@theinternet:~$ cmirror /tmp/a /tmp/b --delete theinternet.molgen.mpg.de: creating empty ./1 theinternet.molgen.mpg.de: creating empty ./10 theinternet.molgen.mpg.de: creating empty ./2 theinternet.molgen.mpg.de: creating empty ./3 theinternet.molgen.mpg.de: creating empty ./4 theinternet.molgen.mpg.de: creating empty ./5 theinternet.molgen.mpg.de: creating empty ./6 theinternet.molgen.mpg.de: creating empty ./7 theinternet.molgen.mpg.de: creating empty ./8 theinternet.molgen.mpg.de: creating empty ./9 theinternet.molgen.mpg.de: rm -r ./xx rm: cannot remove './xx/aa': Permission denied cmirror aborted buczek@theinternet:~$ Implementation: - Add code to `die()` to optionally show the additional message. This can be selected by a global flag - Let the master set that flag unless `--quiet` is selected - Add code to `die()` so that it can be called with `die(NULL)` to show only the optional additional message but no specific error message. - Replace `_exit(1)` with `die(NULL)` n places where it was used to abort the parent process after a child process, which as the duty to emit the error messages for its operation, has failed. --- cmirror.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/cmirror.c b/cmirror.c index 15b6d06..1e1f5d5 100644 --- a/cmirror.c +++ b/cmirror.c @@ -34,6 +34,7 @@ #define STEAL_POINTER(SRC) ({ void *tmp=*(SRC); *(SRC)=NULL; tmp; }) static const char *log_prefix; +static int noisy_abort; static void warn(const char *restrict fmt, ...) { if (log_prefix) @@ -45,12 +46,16 @@ static void warn(const char *restrict fmt, ...) { } static G_NORETURN void die(const char *restrict fmt, ...) { - if (log_prefix) - fprintf(stderr, "%s: ", log_prefix); - va_list ap; - va_start(ap, fmt); - vfprintf(stderr, fmt, ap); - va_end(ap); + if (fmt) { + if (log_prefix) + fprintf(stderr, "%s: ", log_prefix); + va_list ap; + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + } + if (noisy_abort) + fputs("cmirror aborted\n", stderr); _exit(1); } @@ -355,7 +360,7 @@ static void execvp_checked(char **argv) { int wstatus; waitpid(pid, &wstatus, 0); if (wstatus) - _exit(1); + die(NULL); } static void fileop_rmdir_recurse(char *path) { @@ -674,7 +679,7 @@ static void slave(char *slave_path) { if (error) die("mkdir: %s\n", error->message); if (wait_status) - _exit(1); + die(NULL); } int res = chdir(slave_path); if (res == -1) @@ -1084,6 +1089,9 @@ static void master(char *master_path, char *target) { g_autofree char *slave_path; g_autofree char *slave_user; + if (!quiet) + noisy_abort = 1; + EXCEPTS = g_ptr_array_new(); g_ptr_array_add (EXCEPTS, "./quota.group"); g_ptr_array_add (EXCEPTS, "./quota.user"); @@ -1435,7 +1443,7 @@ static void master(char *master_path, char *target) { if (!res) die("%m"); if (wstatus) { - _exit(1); + die(NULL); } }