Skip to content

Commit 6cf6bb3

Browse files
raboofgitster
authored andcommitted
Improve error messages when temporary file creation fails
Before, when creating a temporary file failed, a generic 'Unable to create temporary file' message was printed. In some cases this could lead to confusion as to which directory should be checked for correct permissions etc. This patch adds the template for the temporary filename to the error message, converting it to an absolute path if needed. A test verifies that the template is indeed printed when pointing to a nonexistent or unwritable directory. A copy of the original template is made in case mkstemp clears the template. Signed-off-by: Arnout Engelen <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 853563d commit 6cf6bb3

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
@@ -434,6 +434,7 @@ TEST_PROGRAMS_NEED_X += test-string-pool
434434
TEST_PROGRAMS_NEED_X += test-svn-fe
435435
TEST_PROGRAMS_NEED_X += test-treap
436436
TEST_PROGRAMS_NEED_X += test-index-version
437+
TEST_PROGRAMS_NEED_X += test-mktemp
437438

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

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)