@matheusbsbs / matheus-bagliano / commits / 5f0bdf50c2
Fork 0

safe_create_leading_directories(): make it about "leading" directories

We used to allow callers to pass "foo/bar/" to make sure both "foo" and "foo/bar" exist and have good permissions, but this interface is too error prone. If a caller mistakenly passes a path with trailing slashes (perhaps it forgot to verify the user input) even when it wants to later mkdir "bar" itself, it will find that it cannot mkdir "bar". If such a caller does not bother to check the error for EEXIST, it may even errorneously die(). Because we have no existing callers to use that obscure feature, this patch removes it to avoid confusion. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Sep 2, 2008 at 14:10 UTC 5f0bdf50c2eb640b4321acb9b34b3cb401f5ddfb
1 file changed +5 -1
sha1_file.c
+5 -1
@@ -99,7 +99,11 @@ int safe_create_leading_directories(char *path)
99 pos = strchr(pos, '/');
100 if (!pos)
101 break;
102 - *pos = 0;
102 + while (*++pos == '/')
103 + ;
104 + if (!*pos)
105 + break;
106 + *--pos = '\0';
107 if (!stat(path, &st)) {
108 /* path exists */
109 if (!S_ISDIR(st.st_mode)) {