Skip to content

Commit 9e6f885

Browse files
mhaggergitster
authored andcommitted
safe_create_leading_directories(): always restore slash at end of loop
Always restore the slash that we scribbled over at the end of the loop, rather than also fixing it up at each premature exit from the loop. This makes it harder to forget to do the cleanup as new paths are added to the code. Signed-off-by: Michael Haggerty <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bf10cf7 commit 9e6f885

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

sha1_file.c

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,9 @@ int mkdir_in_gitdir(const char *path)
108108
int safe_create_leading_directories(char *path)
109109
{
110110
char *next_component = path + offset_1st_component(path);
111+
int ret = 0;
111112

112-
while (next_component) {
113+
while (!ret && next_component) {
113114
struct stat st;
114115
char *slash = strchr(next_component, '/');
115116

@@ -125,25 +126,20 @@ int safe_create_leading_directories(char *path)
125126
*slash = '\0';
126127
if (!stat(path, &st)) {
127128
/* path exists */
128-
if (!S_ISDIR(st.st_mode)) {
129-
*slash = '/';
130-
return -3;
131-
}
129+
if (!S_ISDIR(st.st_mode))
130+
ret = -3;
132131
} else if (mkdir(path, 0777)) {
133132
if (errno == EEXIST &&
134-
!stat(path, &st) && S_ISDIR(st.st_mode)) {
133+
!stat(path, &st) && S_ISDIR(st.st_mode))
135134
; /* somebody created it since we checked */
136-
} else {
137-
*slash = '/';
138-
return -1;
139-
}
135+
else
136+
ret = -1;
140137
} else if (adjust_shared_perm(path)) {
141-
*slash = '/';
142-
return -2;
138+
ret = -2;
143139
}
144140
*slash = '/';
145141
}
146-
return 0;
142+
return ret;
147143
}
148144

149145
int safe_create_leading_directories_const(const char *path)

0 commit comments

Comments
 (0)