git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/2] xopen: explicitly report creation failures

From
René Scharfe <l.s.r@web.de>
Date
Aug 25, 2021, 20:14 UTC
Message-ID
<6a5c3e8e-0216-8b63-38fa-b7b19331d752@web.de>

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>
---
 wrapper.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/wrapper.c b/wrapper.c
index 563ad590df..7c6586af32 100644
--- 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);
--
2.33.0
Next: René Scharfe
Message 1 of 5 in “xopen: explicitly report creation failures”
  1. 1/2 xopen: explicitly report creation failuresRené Scharfe, Aug 25, 2021
  2. 2/2 use xopen() to handle fatal open(2) failuresRené Scharfe, Aug 25, 2021
  3. Carlo ArenasAug 25, 2021
  4. René ScharfeAug 26, 2021
  5. Junio C HamanoAug 26, 2021

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.