diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2022-11-10 12:53:45 -0500 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2022-11-10 12:53:45 -0500 |
commit | 0879126bb44aa9761a920946f35f5aa697717445 (patch) | |
tree | cac70ef04178a87986de9388a9acd46ab92c21c2 /src/eval.c | |
parent | f98a1c4a1cf61ff7d6483388ca1fac365fb0b31b (diff) | |
download | bfs-0879126bb44aa9761a920946f35f5aa697717445.tar.xz |
Try to report I/O errors earlier and only once
Diffstat (limited to 'src/eval.c')
-rw-r--r-- | src/eval.c | 24 |
1 files changed, 19 insertions, 5 deletions
@@ -109,6 +109,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. */ static const struct bfs_stat *eval_stat(struct bfs_eval *state) { @@ -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; } |