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

[PATCH] notes-merge: use O_EXCL to avoid overwriting existing files

From
René Scharfe <l.s.r@web.de>
Date
Jul 7, 2016, 20:08 UTC
Message-ID
<577EB6BE.6090504@web.de>

Use the open(2) flag O_EXCL to ensure the file doesn't already exist instead of (racily) calling stat(2) through file_exists(). While at it switch to xopen() to reduce code duplication and get more consistent error messages.

Signed-off-by: Rene Scharfe <l.s.r@web.de>
---
 notes-merge.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
diff --git a/notes-merge.c b/notes-merge.c
index b7814c9..2b29fc4 100644
--- a/notes-merge.c
+++ b/notes-merge.c
@@ -298,12 +298,8 @@ static void write_buf_to_worktree(const unsigned char *obj,
 	char *path = git_pathdup(NOTES_MERGE_WORKTREE "/%s", sha1_to_hex(obj));
 	if (safe_create_leading_directories_const(path))
 		die_errno("unable to create directory for '%s'", path);
-	if (file_exists(path))
-		die("found existing file at '%s'", path);
 
-	fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0666);
-	if (fd < 0)
-		die_errno("failed to open '%s'", path);
+	fd = xopen(path, O_WRONLY | O_EXCL | O_CREAT, 0666);
 
 	while (size > 0) {
 		long ret = write_in_full(fd, buf, size);
-- 
2.9.0
Next: Jeff King
Message 1 of 4 in “notes-merge: use O_EXCL to avoid overwriting existing files”
  1. notes-merge: use O_EXCL to avoid overwriting existing filesRené Scharfe, Jul 7, 2016
  2. Jeff KingJul 7, 2016
  3. Junio C HamanoJul 7, 2016
  4. Jeff KingJul 8, 2016

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.