Skip to content

Commit

Permalink
Merge branch 'js/fopen-harder'
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Junio C Hamano committed Jan 20, 2016
2 parents 85705cf + ea56518 commit 7a63c9e
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion builtin/commit.c
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
hook_arg2 = "";
}

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

Expand Down
2 changes: 1 addition & 1 deletion builtin/fast-export.c
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,7 @@ static void export_marks(char *file)
FILE *f;
int e = 0;

f = fopen(file, "w");
f = fopen_for_writing(file);
if (!f)
die_errno("Unable to open marks file %s for writing.", file);

Expand Down
2 changes: 1 addition & 1 deletion builtin/fetch.c
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ static void check_not_current_branch(struct ref *ref_map)
static int truncate_fetch_head(void)
{
const char *filename = git_path_fetch_head();
FILE *fp = fopen(filename, "w");
FILE *fp = fopen_for_writing(filename);

if (!fp)
return error(_("cannot open %s: %s\n"), filename, strerror(errno));
Expand Down
1 change: 1 addition & 0 deletions git-compat-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,7 @@ extern int xmkstemp_mode(char *template, int mode);
extern int odb_mkstemp(char *template, size_t limit, const char *pattern);
extern int odb_pack_keep(char *name, size_t namesz, const unsigned char *sha1);
extern char *xgetcwd(void);
extern FILE *fopen_for_writing(const char *path);

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

Expand Down
13 changes: 13 additions & 0 deletions wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,19 @@ FILE *xfdopen(int fd, const char *mode)
return stream;
}

FILE *fopen_for_writing(const char *path)
{
FILE *ret = fopen(path, "w");

if (!ret && errno == EPERM) {
if (!unlink(path))
ret = fopen(path, "w");
else
errno = EPERM;
}
return ret;
}

int xmkstemp(char *template)
{
int fd;
Expand Down

0 comments on commit 7a63c9e

Please sign in to comment.