From 42105f90b2c4aad60271f5d4ad68bba24fb5a5c4 Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Mon, 14 Jul 2014 20:18:47 +0200 Subject: [PATCH] Uses the portable to Windows ftruncate(). --- cmdline/mktest.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/cmdline/mktest.c b/cmdline/mktest.c index 5b25a6e1..1d8f1b7e 100644 --- a/cmdline/mktest.c +++ b/cmdline/mktest.c @@ -516,6 +516,7 @@ void cmd_truncate(const char* path, int size) if (S_ISREG(st.st_mode)) { off_t off; + int f; /* if file is empty, just rewrite it */ if (st.st_size == 0) { @@ -528,12 +529,27 @@ void cmd_truncate(const char* path, int size) off = st.st_size - size; - if (truncate(path, off) != 0) { + f = open(path, O_WRONLY | O_BINARY | O_NOFOLLOW); + if (f < 0) { + /* LCOV_EXCL_START */ + fprintf(stderr, "Error opening file %s\n", path); + exit(EXIT_FAILURE); + /* LCOV_EXCL_STOP */ + } + + if (ftruncate(f, off) != 0) { /* LCOV_EXCL_START */ fprintf(stderr, "Error truncating file %s\n", path); exit(EXIT_FAILURE); /* LCOV_EXCL_STOP */ } + + if (close(f) != 0) { + /* LCOV_EXCL_START */ + fprintf(stderr, "Error closing file %s\n", path); + exit(EXIT_FAILURE); + /* LCOV_EXCL_STOP */ + } } }