From 5eba5cb9215edff62e2bd7070bb690913cdae7c7 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Fri, 12 Feb 2016 15:26:04 -0500 Subject: Consolidate some error reporting logic. --- eval.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'eval.c') diff --git a/eval.c b/eval.c index a045d39..f08bab5 100644 --- a/eval.c +++ b/eval.c @@ -35,6 +35,14 @@ struct eval_state { struct stat statbuf; }; +/** + * Report an error that occurs during evaluation. + */ +static void eval_error(struct eval_state *state) { + print_error(state->cmdline->colors, state->ftwbuf->path, errno); + state->ret = -1; +} + /** * Perform a stat() call if necessary. */ @@ -44,8 +52,7 @@ static const struct stat *fill_statbuf(struct eval_state *state) { if (fstatat(ftwbuf->at_fd, ftwbuf->at_path, &state->statbuf, AT_SYMLINK_NOFOLLOW) == 0) { ftwbuf->statbuf = &state->statbuf; } else { - state->ret = -1; - perror("fstatat()"); + eval_error(state); } } return ftwbuf->statbuf; @@ -197,7 +204,7 @@ bool eval_delete(const struct expr *expr, struct eval_state *state) { } if (unlinkat(ftwbuf->at_fd, ftwbuf->at_path, flag) != 0) { - print_error(state->cmdline->colors, ftwbuf->path, errno); + eval_error(state); state->action = BFTW_STOP; } @@ -214,15 +221,13 @@ bool eval_empty(const struct expr *expr, struct eval_state *state) { if (ftwbuf->typeflag == BFTW_DIR) { int dfd = openat(ftwbuf->at_fd, ftwbuf->at_path, O_DIRECTORY); if (dfd < 0) { - state->ret = -1; - perror("openat()"); + eval_error(state); goto done; } DIR *dir = fdopendir(dfd); if (!dir) { - state->ret = -1; - perror("fdopendir()"); + eval_error(state); close(dfd); goto done; } -- cgit v1.2.3