notes-merge: use O_EXCL to avoid overwriting existing files

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> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Jul 7, 2016 at 22:08 UTC deb9c1575c456b9d12ff05fdd2bec516dfb34ae4
1 file changed +1 -5
notes-merge.c
+1 -5
@@ -298,12 +298,8 @@ static void write_buf_to_worktree(const unsigned char *obj,
298 char *path = git_pathdup(NOTES_MERGE_WORKTREE "/%s", sha1_to_hex(obj));
299 if (safe_create_leading_directories_const(path))
300 die_errno("unable to create directory for '%s'", path);
301 - if (file_exists(path))
302 - die("found existing file at '%s'", path);
301
304 - fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0666);
305 - if (fd < 0)
306 - die_errno("failed to open '%s'", path);
302 + fd = xopen(path, O_WRONLY | O_EXCL | O_CREAT, 0666);
303
304 while (size > 0) {
305 long ret = write_in_full(fd, buf, size);