@matheusbsbs / matheus-bagliano / commits / 756aaf4ac5
Fork 0

Make lazy mkdir more robust.

Linus Torvalds <torvalds@osdl.org> 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.

Shawn Pearce committed Jul 12, 2006 at 01:00 UTC 756aaf4ac5906e79b22001a96dd4f6aec0d1c89d
1 file changed +12 -14
sha1_file.c
+12 -14
@@ -1331,31 +1331,29 @@ char *write_sha1_file_prepare(void *buf,
1331 static int link_temp_to_file(const char *tmpfile, char *filename)
1332 {
1333 int ret;
1334 + char *dir;
1335
1336 if (!link(tmpfile, filename))
1337 return 0;
1338
1339 /*
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.
1341 *
1342 * Re-try the "link()" regardless of whether the mkdir
1343 * succeeds, since a race might mean that somebody
1344 * else succeeded.
1345 */
1346 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;
1357 }
1358 return ret;
1359 }