Skip to content

Commit 1733832

Browse files
author
Junio C Hamano
committed
Merge branch 'sp/lazy-mkdir'
* sp/lazy-mkdir: Make lazy mkdir more robust.
2 parents 7eae7b9 + 756aaf4 commit 1733832

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

sha1_file.c

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,31 +1331,29 @@ char *write_sha1_file_prepare(void *buf,
13311331
static int link_temp_to_file(const char *tmpfile, char *filename)
13321332
{
13331333
int ret;
1334+
char *dir;
13341335

13351336
if (!link(tmpfile, filename))
13361337
return 0;
13371338

13381339
/*
1339-
* Try to mkdir the last path component if that failed
1340-
* with an ENOENT.
1340+
* Try to mkdir the last path component if that failed.
13411341
*
13421342
* Re-try the "link()" regardless of whether the mkdir
13431343
* succeeds, since a race might mean that somebody
13441344
* else succeeded.
13451345
*/
13461346
ret = errno;
1347-
if (ret == ENOENT) {
1348-
char *dir = strrchr(filename, '/');
1349-
if (dir) {
1350-
*dir = 0;
1351-
mkdir(filename, 0777);
1352-
if (adjust_shared_perm(filename))
1353-
return -2;
1354-
*dir = '/';
1355-
if (!link(tmpfile, filename))
1356-
return 0;
1357-
ret = errno;
1358-
}
1347+
dir = strrchr(filename, '/');
1348+
if (dir) {
1349+
*dir = 0;
1350+
mkdir(filename, 0777);
1351+
if (adjust_shared_perm(filename))
1352+
return -2;
1353+
*dir = '/';
1354+
if (!link(tmpfile, filename))
1355+
return 0;
1356+
ret = errno;
13591357
}
13601358
return ret;
13611359
}

0 commit comments

Comments
 (0)