summaryrefslogtreecommitdiffstats
path: root/parse.c
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2021-10-19 12:24:22 -0400
committerTavian Barnes <tavianator@tavianator.com>2021-10-19 12:28:17 -0400
commit9b50adaaaa4fedc8bda6fcf32595ecf7a682fa8b (patch)
tree3ed5f7cbdc1063b8e5c6d9f9a5ac5ea2542e1250 /parse.c
parentdceb5658eae528ae18dbd7d249ab4ca60b3958fe (diff)
downloadbfs-9b50adaaaa4fedc8bda6fcf32595ecf7a682fa8b.tar.xz
parse: Switch back to O_TRUNC from explicit ftruncate()
O_TRUNC is not equivalent to an unconditional ftruncate(), e.g. for device files. This reverts the behaviour change from 78944c81, which was never released and is not supported in other find implementations.
Diffstat (limited to 'parse.c')
-rw-r--r--parse.c23
1 files changed, 7 insertions, 16 deletions
diff --git a/parse.c b/parse.c
index c257f4e..aea7b4f 100644
--- a/parse.c
+++ b/parse.c
@@ -336,9 +336,8 @@ static int expr_open(struct parser_state *state, struct expr *expr, const char *
FILE *file = NULL;
CFILE *cfile = NULL;
- CFILE *dedup = NULL;
- file = xfopen(path, O_WRONLY | O_CREAT | O_CLOEXEC);
+ file = xfopen(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC);
if (!file) {
goto fail;
}
@@ -348,18 +347,12 @@ static int expr_open(struct parser_state *state, struct expr *expr, const char *
goto fail;
}
- dedup = bfs_ctx_dedup(ctx, cfile, path);
+ CFILE *dedup = bfs_ctx_dedup(ctx, cfile, path);
if (!dedup) {
goto fail;
}
- if (dedup == cfile) {
- // O_TRUNC was omitted above to avoid repeatedly truncating the same file, so do it
- // manually here
- if (ftruncate(fileno(file), 0) != 0) {
- goto fail;
- }
- } else {
+ if (dedup != cfile) {
cfclose(cfile);
}
@@ -368,12 +361,10 @@ static int expr_open(struct parser_state *state, struct expr *expr, const char *
fail:
parse_error(state, "${blu}%s${rs} ${bld}%s${rs}: %m.\n", expr->argv[0], path);
- if (!dedup) {
- if (cfile) {
- cfclose(cfile);
- } else if (file) {
- fclose(file);
- }
+ if (cfile) {
+ cfclose(cfile);
+ } else if (file) {
+ fclose(file);
}
return -1;
}