Skip to content

Commit deb9c15

Browse files
rscharfegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0b65a8d commit deb9c15

File tree

1 file changed

+1
-5
lines changed

1 file changed

+1
-5
lines changed

notes-merge.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -298,12 +298,8 @@ static void write_buf_to_worktree(const unsigned char *obj,
298298
char *path = git_pathdup(NOTES_MERGE_WORKTREE "/%s", sha1_to_hex(obj));
299299
if (safe_create_leading_directories_const(path))
300300
die_errno("unable to create directory for '%s'", path);
301-
if (file_exists(path))
302-
die("found existing file at '%s'", path);
303301

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);
307303

308304
while (size > 0) {
309305
long ret = write_in_full(fd, buf, size);

0 commit comments

Comments
 (0)