Utility: fix bug in CreateDirHierarchy

Fix bug where even the last path would be processed in the loop, making the return call always return an error as the path would've been created by that time. This ensures that we always return the value returned by mkdir when creating the last path
This commit is contained in:
Kawe Mazidjatari 2024-01-28 18:08:32 +01:00
parent 22431e903c
commit 3eb3c27975

View File

@ -28,9 +28,20 @@ int CreateDirHierarchy(const char* const filePath)
int results;
snprintf(fullPath, sizeof(fullPath), "%s", filePath);
V_FixSlashes(fullPath);
const size_t pathLen = strlen(fullPath);
char* const pathEnd = &fullPath[pathLen - 1];
// Strip the trailing slash if there's one
if (*pathEnd == CORRECT_PATH_SEPARATOR)
*pathEnd = '\0';
// Get the pointer to the last dir separator, the last dir is what we want
// to create and return the value of which is why we run that outside the
// loop
const char* const lastDir = strrchr(fullPath, CORRECT_PATH_SEPARATOR);
char* pFullPath = fullPath;
while ((pFullPath = strchr(pFullPath, CORRECT_PATH_SEPARATOR)) != NULL)
{
@ -43,7 +54,14 @@ int CreateDirHierarchy(const char* const filePath)
if (results && errno != EEXIST)
return results;
*pFullPath++ = CORRECT_PATH_SEPARATOR;
*pFullPath = CORRECT_PATH_SEPARATOR;
// Last dir should be created separately, and its return value should
// be returned
if (pFullPath == lastDir)
break;
pFullPath++;
}
// Try to create the final directory in the path.