Skip to content

Commit 7a63c9e

Browse files
committed
Merge branch 'js/fopen-harder'
Some codepaths used fopen(3) when opening a fixed path in $GIT_DIR (e.g. COMMIT_EDITMSG) that is meant to be left after the command is done. This however did not work well if the repository is set to be shared with core.sharedRepository and the umask of the previous user is tighter. They have been made to work better by calling unlink(2) and retrying after fopen(3) fails with EPERM. * js/fopen-harder: Handle more file writes correctly in shared repos commit: allow editing the commit message even in shared repos
2 parents 85705cf + ea56518 commit 7a63c9e

File tree

5 files changed

+17
-3
lines changed

5 files changed

+17
-3
lines changed

builtin/commit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
761761
hook_arg2 = "";
762762
}
763763

764-
s->fp = fopen(git_path(commit_editmsg), "w");
764+
s->fp = fopen_for_writing(git_path(commit_editmsg));
765765
if (s->fp == NULL)
766766
die_errno(_("could not open '%s'"), git_path(commit_editmsg));
767767

builtin/fast-export.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,7 @@ static void export_marks(char *file)
880880
FILE *f;
881881
int e = 0;
882882

883-
f = fopen(file, "w");
883+
f = fopen_for_writing(file);
884884
if (!f)
885885
die_errno("Unable to open marks file %s for writing.", file);
886886

builtin/fetch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,7 @@ static void check_not_current_branch(struct ref *ref_map)
840840
static int truncate_fetch_head(void)
841841
{
842842
const char *filename = git_path_fetch_head();
843-
FILE *fp = fopen(filename, "w");
843+
FILE *fp = fopen_for_writing(filename);
844844

845845
if (!fp)
846846
return error(_("cannot open %s: %s\n"), filename, strerror(errno));

git-compat-util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,7 @@ extern int xmkstemp_mode(char *template, int mode);
733733
extern int odb_mkstemp(char *template, size_t limit, const char *pattern);
734734
extern int odb_pack_keep(char *name, size_t namesz, const unsigned char *sha1);
735735
extern char *xgetcwd(void);
736+
extern FILE *fopen_for_writing(const char *path);
736737

737738
#define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), (alloc) * sizeof(*(x)))
738739

wrapper.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,19 @@ FILE *xfdopen(int fd, const char *mode)
391391
return stream;
392392
}
393393

394+
FILE *fopen_for_writing(const char *path)
395+
{
396+
FILE *ret = fopen(path, "w");
397+
398+
if (!ret && errno == EPERM) {
399+
if (!unlink(path))
400+
ret = fopen(path, "w");
401+
else
402+
errno = EPERM;
403+
}
404+
return ret;
405+
}
406+
394407
int xmkstemp(char *template)
395408
{
396409
int fd;

0 commit comments

Comments
 (0)