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 <arnouten@bzzt.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Arnout Engelen committed
Dec 18, 2010 at 22:28 UTC
6cf6bb3e47ac2f667fa0b27a4222e903ff6fb77c
4 files changed
+56
-4
Makefile
+1
@@ -434,6 +434,7 @@ TEST_PROGRAMS_NEED_X += test-string-pool
434
TEST_PROGRAMS_NEED_X += test-svn-fe
435
TEST_PROGRAMS_NEED_X += test-treap
436
TEST_PROGRAMS_NEED_X += test-index-version
437
+TEST_PROGRAMS_NEED_X += test-mktemp
438
439
TEST_PROGRAMS = $(patsubst %,%$X,$(TEST_PROGRAMS_NEED_X))
440
t/t0070-fundamental.sh
+13
@@ -12,4 +12,17 @@ test_expect_success 'character classes (isspace, isalpha etc.)' '
12
test-ctype
13
'
14
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
+
28
test_done
test-mktemp.c
new
+14
@@ -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
+28
-4
@@ -198,10 +198,22 @@ FILE *xfdopen(int fd, const char *mode)
198
int xmkstemp(char *template)
199
{
200
int fd;
201
+ char origtemplate[PATH_MAX];
202
+ strlcpy(origtemplate, template, sizeof(origtemplate));
203
204
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
+ }
217
return fd;
218
}
219
@@ -321,10 +333,22 @@ int gitmkstemps(char *pattern, int suffix_len)
333
int xmkstemp_mode(char *template, int mode)
334
{
335
int fd;
336
+ char origtemplate[PATH_MAX];
337
+ strlcpy(origtemplate, template, sizeof(origtemplate));
338
339
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
+ }
352
return fd;
353
}
354