Skip to content

Commit 4a90a35

Browse files
committed
Refactor recursive directory creation logic
- Remove early redundant GetFileAttributes existence check - Introduce do-while loop to traverse and verify each parent segment - Move end-pointer initialization after traversal loop - Simplifies the control flow and improves robustness when creating nested directories.
1 parent 873d05d commit 4a90a35

File tree

1 file changed

+10
-18
lines changed

1 file changed

+10
-18
lines changed

src/filesystem_utils.c

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -201,16 +201,6 @@ bool CreateDirectoriesRecursively(const char *dir)
201201
return false;
202202
}
203203

204-
DWORD dir_attr = GetFileAttributes(dir);
205-
if (dir_attr != INVALID_FILE_ATTRIBUTES) {
206-
if (dir_attr & FILE_ATTRIBUTE_DIRECTORY) {
207-
return true;
208-
} else {
209-
APP_ERROR("Directory name conflicts with a file(%s)", dir);
210-
return false;
211-
}
212-
}
213-
214204
bool result = false;
215205

216206
size_t path_len = strlen(dir);
@@ -223,12 +213,8 @@ bool CreateDirectoriesRecursively(const char *dir)
223213
memcpy(path, dir, path_len);
224214
path[path_len] = '\0';
225215

226-
char *end = path + path_len;
227-
char *p = end;
228-
for (; p >= path; p--) {
229-
if (!is_path_separator(*p)) continue;
230-
231-
*p = '\0';
216+
char *p = path + path_len;
217+
do {
232218
DWORD path_attr = GetFileAttributes(path);
233219
if (path_attr != INVALID_FILE_ATTRIBUTES) {
234220
if (path_attr & FILE_ATTRIBUTE_DIRECTORY) {
@@ -241,15 +227,21 @@ bool CreateDirectoriesRecursively(const char *dir)
241227
} else {
242228
DWORD err = GetLastError();
243229
if (err == ERROR_FILE_NOT_FOUND || err == ERROR_PATH_NOT_FOUND) {
244-
continue;
230+
// continue;
245231
} else {
246232
APP_ERROR("Cannot access the directory, Error=%lu", err);
247233

248234
goto cleanup;
249235
}
250236
}
251-
}
252237

238+
while (p > path && !is_path_separator(*p)) {
239+
p--;
240+
}
241+
*p = '\0';
242+
} while (p >= path);
243+
244+
char *end = path + path_len;
253245
for (; p < end; p++) {
254246
if (*p) continue;
255247

0 commit comments

Comments
 (0)