Re: [PATCH] Improved error messages when temporary file creation fails
- From
Jonathan Nieder <jrnieder@gmail.com>
- Date
- Dec 7, 2010, 19:09 UTC
- Message-ID
- <20101207190920.GA22587@burratino>
- In-Reply-To
- <20101207181633.GF25767@bzzt.net>
Arnout Engelen wrote:
> + die_errno("Unable to create temporary file '%s' at %s",
> + template, getcwd(NULL, 0));This is a Linux libc/glibc-specific extension, alas. On other platforms it would print "(null)" or segfault.
Here's some other assorted tweaks. I didn't bother to find your old patch in the mailing list archive to take a fuller change description from.
Hope that helps, Jonathan ---
diff --git a/test-mktemp.c b/test-mktemp.c index d392fa7..2e3b134 100644 --- a/test-mktemp.c +++ b/test-mktemp.c @@ -7,10 +7,9 @@ int main(int argc, char *argv[]) { - if (argc != 2) { + if (argc != 2) usage("Expected 1 parameter defining the temporary file template"); - } - xmkstemp(strdup(argv[1])); + xmkstemp(xstrdup(argv[1])); return 0; } diff --git a/wrapper.c b/wrapper.c index 6640c87..cb9c9ad 100644 --- a/wrapper.c +++ b/wrapper.c @@ -197,19 +197,18 @@ FILE *xfdopen(int fd, const char *mode) int xmkstemp(char *template) { int fd; - char origtemplate[255]; - strlcpy(origtemplate, template, 255); + char origtemplate[256]; + strlcpy(origtemplate, template, sizeof(origtemplate)); fd = mkstemp(template); if (fd < 0) { + int saved_errno = errno; + if (!template[0]) template = origtemplate; - - if (is_absolute_path(template)) - die_errno("Unable to create temporary file '%s'", template); - else - die_errno("Unable to create temporary file '%s' at %s", - template, getcwd(NULL, 0)); + template = make_nonrelative_path(template); + errno = saved_errno; + die_errno("Unable to create temporary file '%s'", template); } return fd; } @@ -330,19 +329,18 @@ int gitmkstemps(char *pattern, int suffix_len) int xmkstemp_mode(char *template, int mode) { int fd; - char origtemplate[255]; - strlcpy(origtemplate, template, 255); + char origtemplate[256]; + strlcpy(origtemplate, template, sizeof(origtemplate)); fd = git_mkstemp_mode(template, mode); if (fd < 0) { + int saved_errno = errno; + if (!template[0]) template = origtemplate; - - if (is_absolute_path(template)) - die_errno("Unable to create temporary file '%s'", template); - else - die_errno("Unable to create temporary file '%s' at %s", - template, getcwd(NULL, 0)); + template = make_nonrelative_path(template); + errno = saved_errno; + die_errno("Unable to create temporary file '%s'", template); } return fd; }