Skip to content

Commit

Permalink
Abort more noisily
Browse files Browse the repository at this point in the history
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 finished with error ***

    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.
  • Loading branch information
donald committed Aug 17, 2022
1 parent 917e92a commit 98946e7
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions cmirror.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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("\n*** cmirror finished with error ***\n\n", stderr);
_exit(1);
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -1435,7 +1443,7 @@ static void master(char *master_path, char *target) {
if (!res)
die("%m");
if (wstatus) {
_exit(1);
die(NULL);
}
}

Expand Down

0 comments on commit 98946e7

Please sign in to comment.