From 0879126bb44aa9761a920946f35f5aa697717445 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Thu, 10 Nov 2022 12:53:45 -0500 Subject: Try to report I/O errors earlier and only once --- src/eval.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) (limited to 'src/eval.c') diff --git a/src/eval.c b/src/eval.c index f756617..dd147c9 100644 --- a/src/eval.c +++ b/src/eval.c @@ -108,6 +108,20 @@ static void eval_report_error(struct bfs_eval *state) { } } +/** + * Report an I/O error that occurs during evaluation. + */ +static void eval_io_error(const struct bfs_expr *expr, struct bfs_eval *state) { + if (expr->path) { + eval_error(state, "'%s': %m.\n", expr->path); + } else { + eval_error(state, "(standard output): %m.\n"); + } + + // Don't report the error again in bfs_ctx_free() + clearerr(expr->cfile->file); +} + /** * Perform a bfs_stat() call if necessary. */ @@ -732,7 +746,7 @@ done: return true; error: - eval_report_error(state); + eval_io_error(expr, state); return true; } @@ -741,7 +755,7 @@ error: */ bool eval_fprint(const struct bfs_expr *expr, struct bfs_eval *state) { if (cfprintf(expr->cfile, "%pP\n", state->ftwbuf) < 0) { - eval_report_error(state); + eval_io_error(expr, state); } return true; } @@ -753,7 +767,7 @@ bool eval_fprint0(const struct bfs_expr *expr, struct bfs_eval *state) { const char *path = state->ftwbuf->path; size_t length = strlen(path) + 1; if (fwrite(path, 1, length, expr->cfile->file) != length) { - eval_report_error(state); + eval_io_error(expr, state); } return true; } @@ -763,7 +777,7 @@ bool eval_fprint0(const struct bfs_expr *expr, struct bfs_eval *state) { */ bool eval_fprintf(const struct bfs_expr *expr, struct bfs_eval *state) { if (bfs_printf(expr->cfile, expr->printf, state->ftwbuf) != 0) { - eval_report_error(state); + eval_io_error(expr, state); } return true; @@ -803,7 +817,7 @@ bool eval_fprintx(const struct bfs_expr *expr, struct bfs_eval *state) { return true; error: - eval_report_error(state); + eval_io_error(expr, state); return true; } -- cgit v1.2.3