Merge branch 'sp/lazy-mkdir'
* sp/lazy-mkdir: Make lazy mkdir more robust.
This commit is contained in:
commit
1733832d8e
26
sha1_file.c
26
sha1_file.c
@ -1331,31 +1331,29 @@ char *write_sha1_file_prepare(void *buf,
|
|||||||
static int link_temp_to_file(const char *tmpfile, char *filename)
|
static int link_temp_to_file(const char *tmpfile, char *filename)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
|
char *dir;
|
||||||
|
|
||||||
if (!link(tmpfile, filename))
|
if (!link(tmpfile, filename))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Try to mkdir the last path component if that failed
|
* Try to mkdir the last path component if that failed.
|
||||||
* with an ENOENT.
|
|
||||||
*
|
*
|
||||||
* Re-try the "link()" regardless of whether the mkdir
|
* Re-try the "link()" regardless of whether the mkdir
|
||||||
* succeeds, since a race might mean that somebody
|
* succeeds, since a race might mean that somebody
|
||||||
* else succeeded.
|
* else succeeded.
|
||||||
*/
|
*/
|
||||||
ret = errno;
|
ret = errno;
|
||||||
if (ret == ENOENT) {
|
dir = strrchr(filename, '/');
|
||||||
char *dir = strrchr(filename, '/');
|
if (dir) {
|
||||||
if (dir) {
|
*dir = 0;
|
||||||
*dir = 0;
|
mkdir(filename, 0777);
|
||||||
mkdir(filename, 0777);
|
if (adjust_shared_perm(filename))
|
||||||
if (adjust_shared_perm(filename))
|
return -2;
|
||||||
return -2;
|
*dir = '/';
|
||||||
*dir = '/';
|
if (!link(tmpfile, filename))
|
||||||
if (!link(tmpfile, filename))
|
return 0;
|
||||||
return 0;
|
ret = errno;
|
||||||
ret = errno;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user