Skip to content

Commit 678eb55

Browse files
bk2204gitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 43c8a30 commit 678eb55

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,7 @@ TEST_BUILTINS_OBJS += test-submodule-nested-repo-config.o
852852
TEST_BUILTINS_OBJS += test-submodule.o
853853
TEST_BUILTINS_OBJS += test-subprocess.o
854854
TEST_BUILTINS_OBJS += test-trace2.o
855+
TEST_BUILTINS_OBJS += test-truncate.o
855856
TEST_BUILTINS_OBJS += test-urlmatch-normalization.o
856857
TEST_BUILTINS_OBJS += test-userdiff.o
857858
TEST_BUILTINS_OBJS += test-wildmatch.o

t/helper/test-tool.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ static struct test_cmd cmds[] = {
8686
{ "submodule-nested-repo-config", cmd__submodule_nested_repo_config },
8787
{ "subprocess", cmd__subprocess },
8888
{ "trace2", cmd__trace2 },
89+
{ "truncate", cmd__truncate },
8990
{ "userdiff", cmd__userdiff },
9091
{ "urlmatch-normalization", cmd__urlmatch_normalization },
9192
{ "xml-encode", cmd__xml_encode },

t/helper/test-tool.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ int cmd__submodule_config(int argc, const char **argv);
7979
int cmd__submodule_nested_repo_config(int argc, const char **argv);
8080
int cmd__subprocess(int argc, const char **argv);
8181
int cmd__trace2(int argc, const char **argv);
82+
int cmd__truncate(int argc, const char **argv);
8283
int cmd__userdiff(int argc, const char **argv);
8384
int cmd__urlmatch_normalization(int argc, const char **argv);
8485
int cmd__xml_encode(int argc, const char **argv);

t/helper/test-truncate.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

0 commit comments

Comments
 (0)