t: add a test helper to truncate files

In a future commit, we're going to work with some large files which will be at least 4 GiB in size. To take advantage of the sparseness functionality on most Unix systems and avoid running the system out of disk, it would be convenient to use truncate(2) to simply create a sparse file of sufficient size. However, the GNU truncate(1) utility isn't portable, so let's write a tiny test helper that does the work for us. Signed-off-by: brian m. carlson <bk2204@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

brian m. carlson committed Oct 12, 2023 at 16:09 UTC 678eb55f5da174fce21f686f0073d56904c081c9
4 files changed +28
Makefile
+1
@@ -852,6 +852,7 @@ TEST_BUILTINS_OBJS += test-submodule-nested-repo-config.o
852 TEST_BUILTINS_OBJS += test-submodule.o
853 TEST_BUILTINS_OBJS += test-subprocess.o
854 TEST_BUILTINS_OBJS += test-trace2.o
855 +TEST_BUILTINS_OBJS += test-truncate.o
856 TEST_BUILTINS_OBJS += test-urlmatch-normalization.o
857 TEST_BUILTINS_OBJS += test-userdiff.o
858 TEST_BUILTINS_OBJS += test-wildmatch.o
t/helper/test-tool.c
+1
@@ -86,6 +86,7 @@ static struct test_cmd cmds[] = {
86 { "submodule-nested-repo-config", cmd__submodule_nested_repo_config },
87 { "subprocess", cmd__subprocess },
88 { "trace2", cmd__trace2 },
89 + { "truncate", cmd__truncate },
90 { "userdiff", cmd__userdiff },
91 { "urlmatch-normalization", cmd__urlmatch_normalization },
92 { "xml-encode", cmd__xml_encode },
t/helper/test-tool.h
+1
@@ -79,6 +79,7 @@ int cmd__submodule_config(int argc, const char **argv);
79 int cmd__submodule_nested_repo_config(int argc, const char **argv);
80 int cmd__subprocess(int argc, const char **argv);
81 int cmd__trace2(int argc, const char **argv);
82 +int cmd__truncate(int argc, const char **argv);
83 int cmd__userdiff(int argc, const char **argv);
84 int cmd__urlmatch_normalization(int argc, const char **argv);
85 int cmd__xml_encode(int argc, const char **argv);
t/helper/test-truncate.c new
+25
@@ -0,0 +1,25 @@
1 +#include "test-tool.h"
2 +#include "git-compat-util.h"
3 +
4 +/*
5 + * Truncate a file to the given size.
6 + */
7 +int cmd__truncate(int argc, const char **argv)
8 +{
9 + char *p = NULL;
10 + uintmax_t sz = 0;
11 + int fd = -1;
12 +
13 + if (argc != 3)
14 + die("expected filename and size");
15 +
16 + sz = strtoumax(argv[2], &p, 0);
17 + if (*p)
18 + die("invalid size");
19 +
20 + fd = xopen(argv[1], O_WRONLY | O_CREAT, 0600);
21 +
22 + if (ftruncate(fd, (off_t) sz) < 0)
23 + die_errno("failed to truncate file");
24 + return 0;
25 +}