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 <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Jan 6, 2014 at 14:45 UTC 9e6f885d146c58b23b166a99b93f115735b7bf22
1 file changed +9 -13
sha1_file.c
+9 -13
@@ -108,8 +108,9 @@ int mkdir_in_gitdir(const char *path)
108 int safe_create_leading_directories(char *path)
109 {
110 char *next_component = path + offset_1st_component(path);
111 + int ret = 0;
112
112 - while (next_component) {
113 + while (!ret && next_component) {
114 struct stat st;
115 char *slash = strchr(next_component, '/');
116
@@ -125,25 +126,20 @@ int safe_create_leading_directories(char *path)
126 *slash = '\0';
127 if (!stat(path, &st)) {
128 /* 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;
131 } else if (mkdir(path, 0777)) {
132 if (errno == EEXIST &&
134 - !stat(path, &st) && S_ISDIR(st.st_mode)) {
133 + !stat(path, &st) && S_ISDIR(st.st_mode))
134 ; /* somebody created it since we checked */
136 - } else {
137 - *slash = '/';
138 - return -1;
139 - }
135 + else
136 + ret = -1;
137 } else if (adjust_shared_perm(path)) {
141 - *slash = '/';
142 - return -2;
138 + ret = -2;
139 }
140 *slash = '/';
141 }
146 - return 0;
142 + return ret;
143 }
144
145 int safe_create_leading_directories_const(const char *path)