fix(system/file): fix bugged recursion in CreateDirectory implementation

This commit is contained in:
phaneron 2023-11-17 16:12:37 -05:00
parent 747cdf796f
commit 0ed54befe3

View file

@ -374,42 +374,45 @@ bool MakeAbsolutePath(FileParms* parms) {
} }
bool CreateDirectory(FileParms* parms) { bool CreateDirectory(FileParms* parms) {
constexpr size_t temp_size = 300;
auto path = parms->filename; auto path = parms->filename;
auto recursive = parms->flag != 0; auto recursive = parms->flag != 0;
char temp[300] = {0}; char temp_path[temp_size] = {0};
auto p = path;
size_t count = 0; String::Copy(temp_path, path, temp_size);
File::Path::ForceTrailingSeparator(temp_path, temp_size, '\\');
if (recursive) { if (recursive) {
while (*p != '\0') { auto p = temp_path;
if (*p == '\\' || *p == '/') { if (isalpha(p[0]) && p[1] == ':') {
count++; p += 2
int32_t len = p - path;
if (len == 0) {
len = 1;
} }
String::Copy(temp, path, len);
temp[len] = '\0';
if (::GetFileAttributes(temp) == INVALID_FILE_ATTRIBUTES) { // Loop through path and call CreateDirectory on path elements
if (!::CreateDirectory(temp, nullptr)) { for (auto p = temp_path + 1; *p != '\0'; p++) {
if (::GetLastError() != ERROR_ALREADY_EXISTS) { if (*p == '\\') {
*p = 0;
auto attributes = ::GetFileAttributes(temp_path);
// test path
if (attributes == INVALID_FILE_ATTRIBUTES) {
// path does not exist, create directory
if (!::CreateDirectory(temp_path, nullptr)) {
return false; return false;
} }
} } else if ((attributes & FILE_ATTRIBUTE_DIRECTORY) == 0) {
} // not a directory
} return false;
p++;
}
} }
if (count == 0) { *p = '\\';
// No directories were made because there are no path separators. }
// Make only one directory: }
} else {
String::Copy(temp, path, 300); // Create only the supplied directory.
if (::GetFileAttributes(temp) == INVALID_FILE_ATTRIBUTES) { if (::GetFileAttributes(temp) == INVALID_FILE_ATTRIBUTES) {
if (!::CreateDirectory(temp, nullptr)) { if (!::CreateDirectory(temp, nullptr)) {
if (::GetLastError() != ERROR_ALREADY_EXISTS) { if (::GetLastError() != ERROR_ALREADY_EXISTS) {