Skip to content

Commit 70ec868

Browse files
committed
Merge branch 'ae/better-template-failure-report'
* ae/better-template-failure-report: Improve error messages when temporary file creation fails
2 parents 06938a3 + 6cf6bb3 commit 70ec868

File tree

4 files changed

+56
-4
lines changed

4 files changed

+56
-4
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,7 @@ TEST_PROGRAMS_NEED_X += test-subprocess
435435
TEST_PROGRAMS_NEED_X += test-svn-fe
436436
TEST_PROGRAMS_NEED_X += test-treap
437437
TEST_PROGRAMS_NEED_X += test-index-version
438+
TEST_PROGRAMS_NEED_X += test-mktemp
438439

439440
TEST_PROGRAMS = $(patsubst %,%$X,$(TEST_PROGRAMS_NEED_X))
440441

t/t0070-fundamental.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,17 @@ test_expect_success 'character classes (isspace, isalpha etc.)' '
1212
test-ctype
1313
'
1414

15+
test_expect_success 'mktemp to nonexistent directory prints filename' '
16+
test_must_fail test-mktemp doesnotexist/testXXXXXX 2>err &&
17+
grep "doesnotexist/test" err
18+
'
19+
20+
test_expect_success POSIXPERM 'mktemp to unwritable directory prints filename' '
21+
mkdir cannotwrite &&
22+
chmod -w cannotwrite &&
23+
test_when_finished "chmod +w cannotwrite" &&
24+
test_must_fail test-mktemp cannotwrite/testXXXXXX 2>err &&
25+
grep "cannotwrite/test" err
26+
'
27+
1528
test_done

test-mktemp.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* test-mktemp.c: code to exercise the creation of temporary files
3+
*/
4+
#include "git-compat-util.h"
5+
6+
int main(int argc, char *argv[])
7+
{
8+
if (argc != 2)
9+
usage("Expected 1 parameter defining the temporary file template");
10+
11+
xmkstemp(xstrdup(argv[1]));
12+
13+
return 0;
14+
}

wrapper.c

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,22 @@ FILE *xfdopen(int fd, const char *mode)
198198
int xmkstemp(char *template)
199199
{
200200
int fd;
201+
char origtemplate[PATH_MAX];
202+
strlcpy(origtemplate, template, sizeof(origtemplate));
201203

202204
fd = mkstemp(template);
203-
if (fd < 0)
204-
die_errno("Unable to create temporary file");
205+
if (fd < 0) {
206+
int saved_errno = errno;
207+
const char *nonrelative_template;
208+
209+
if (!template[0])
210+
template = origtemplate;
211+
212+
nonrelative_template = make_nonrelative_path(template);
213+
errno = saved_errno;
214+
die_errno("Unable to create temporary file '%s'",
215+
nonrelative_template);
216+
}
205217
return fd;
206218
}
207219

@@ -321,10 +333,22 @@ int gitmkstemps(char *pattern, int suffix_len)
321333
int xmkstemp_mode(char *template, int mode)
322334
{
323335
int fd;
336+
char origtemplate[PATH_MAX];
337+
strlcpy(origtemplate, template, sizeof(origtemplate));
324338

325339
fd = git_mkstemp_mode(template, mode);
326-
if (fd < 0)
327-
die_errno("Unable to create temporary file");
340+
if (fd < 0) {
341+
int saved_errno = errno;
342+
const char *nonrelative_template;
343+
344+
if (!template[0])
345+
template = origtemplate;
346+
347+
nonrelative_template = make_nonrelative_path(template);
348+
errno = saved_errno;
349+
die_errno("Unable to create temporary file '%s'",
350+
nonrelative_template);
351+
}
328352
return fd;
329353
}
330354

0 commit comments

Comments
 (0)