]> git.ipfire.org Git - thirdparty/git.git/commitdiff
xopen: explicitly report creation failures
authorRené Scharfe <l.s.r@web.de>
Wed, 25 Aug 2021 20:14:09 +0000 (22:14 +0200)
committerJunio C Hamano <gitster@pobox.com>
Wed, 25 Aug 2021 21:39:06 +0000 (14:39 -0700)
If the flags O_CREAT and O_EXCL are both given then open(2) is supposed
to create the file and error out if it already exists.  The error
message in that case looks like this:

fatal: could not open 'foo' for writing: File exists

Without further context this is confusing: Why should the existence of
the file pose a problem?  Isn't that a requirement for writing to it?

Add a more specific error message for that case to tell the user that we
actually don't expect the file to preexist, so the example becomes:

fatal: unable to create 'foo': File exists

Signed-off-by: René Scharfe <l.s.r@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
wrapper.c

index 563ad590df1f2963767858c2d490a3893f34ab7f..7c6586af3210009e991ccd06756ce94ce8de5962 100644 (file)
--- a/wrapper.c
+++ b/wrapper.c
@@ -193,7 +193,9 @@ int xopen(const char *path, int oflag, ...)
                if (errno == EINTR)
                        continue;
 
-               if ((oflag & O_RDWR) == O_RDWR)
+               if ((oflag & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
+                       die_errno(_("unable to create '%s'"), path);
+               else if ((oflag & O_RDWR) == O_RDWR)
                        die_errno(_("could not open '%s' for reading and writing"), path);
                else if ((oflag & O_WRONLY) == O_WRONLY)
                        die_errno(_("could not open '%s' for writing"), path);