Skip to content

Commit 756aaf4

Browse files
spearceJunio C Hamano
authored andcommitted
Make lazy mkdir more robust.
Linus Torvalds <[email protected]> wrote: It's entirely possible that we should just make that whole if (ret == ENOENT) go away. Yes, it's the right error code if a subdirectory is missing, and yes, POSIX requires it, and yes, WXP is probably just a horrible piece of sh*t, but on the other hand, I don't think git really has any serious reason to even care.
1 parent d3ba675 commit 756aaf4

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)