Skip to content

Commit

Permalink
Merge branch 'sp/lazy-mkdir'
Browse files Browse the repository at this point in the history
* sp/lazy-mkdir:
  Make lazy mkdir more robust.
  • Loading branch information
Junio C Hamano committed Jul 14, 2006
2 parents 7eae7b9 + 756aaf4 commit 1733832
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions sha1_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -1331,31 +1331,29 @@ char *write_sha1_file_prepare(void *buf,
static int link_temp_to_file(const char *tmpfile, char *filename)
{
int ret;
char *dir;

if (!link(tmpfile, filename))
return 0;

/*
* Try to mkdir the last path component if that failed
* with an ENOENT.
* Try to mkdir the last path component if that failed.
*
* Re-try the "link()" regardless of whether the mkdir
* succeeds, since a race might mean that somebody
* else succeeded.
*/
ret = errno;
if (ret == ENOENT) {
char *dir = strrchr(filename, '/');
if (dir) {
*dir = 0;
mkdir(filename, 0777);
if (adjust_shared_perm(filename))
return -2;
*dir = '/';
if (!link(tmpfile, filename))
return 0;
ret = errno;
}
dir = strrchr(filename, '/');
if (dir) {
*dir = 0;
mkdir(filename, 0777);
if (adjust_shared_perm(filename))
return -2;
*dir = '/';
if (!link(tmpfile, filename))
return 0;
ret = errno;
}
return ret;
}
Expand Down

0 comments on commit 1733832

Please sign in to comment.