From 9368512d69711c2995e0c42cfc5df7773d60db2a Mon Sep 17 00:00:00 2001 From: josesimoes Date: Fri, 3 May 2024 21:04:13 +0100 Subject: [PATCH] Rework path creation to simplify code --- .../ChibiOS/_littlefs/littlefs_FS_Driver.cpp | 92 +++++++++---------- 1 file changed, 42 insertions(+), 50 deletions(-) diff --git a/targets/ChibiOS/_littlefs/littlefs_FS_Driver.cpp b/targets/ChibiOS/_littlefs/littlefs_FS_Driver.cpp index 5744082591..b72ae76033 100644 --- a/targets/ChibiOS/_littlefs/littlefs_FS_Driver.cpp +++ b/targets/ChibiOS/_littlefs/littlefs_FS_Driver.cpp @@ -847,13 +847,14 @@ HRESULT LITTLEFS_FS_Driver::SetAttributes(const VOLUME_ID *volume, const char *p HRESULT LITTLEFS_FS_Driver::CreateDirectory(const VOLUME_ID *volume, const char *path) { lfs_t *fsDrive = NULL; - lfs_info info; int32_t result = LFS_ERR_OK; char normalizedPath[FS_MAX_DIRECTORY_LENGTH]; - char *workingPath; char tempPath[FS_MAX_DIRECTORY_LENGTH + 1]; - char *tempPathP = tempPath; - int32_t pathIndex = 0; + char *segment; + + // TODO: remove this + int i = 0; + (void)i; if (NormalizePath(path, normalizedPath, sizeof(normalizedPath)) < 0) { @@ -866,61 +867,54 @@ HRESULT LITTLEFS_FS_Driver::CreateDirectory(const VOLUME_ID *volume, const char if (fsDrive) { - // store pointer to tempPath - tempPathP = tempPath; + memset(tempPath, 0, sizeof(tempPath)); - // create all the diretories in the path, walking the path - do - { - // find the next path separator - workingPath = strchr(&normalizedPath[pathIndex], '/'); - - if (workingPath) - { - // copy the path part to tempPath - memcpy(tempPathP, &normalizedPath[pathIndex], workingPath - &normalizedPath[pathIndex]); + // iterate over the path segments and create the directories + segment = strtok(normalizedPath, "/"); - // store pointer to tempPath - tempPathP += (workingPath - &normalizedPath[pathIndex]); + while (segment && (result == LFS_ERR_OK || result == LFS_ERR_EXIST)) + { + strcat(tempPath, segment); - // move to the next part of the path - pathIndex = workingPath - normalizedPath + 1; - } - else - { - break; - } + result = lfs_mkdir(fsDrive, tempPath); - // add null terminator - *tempPathP = '\0'; + // TODO: remove this + // // hard coded paths for debugging + // if (i == 0) + // { + // result = lfs_mkdir(fsDrive, "temp"); + // } + // else if (i == 1) + // { + // result = lfs_mkdir(fsDrive, "temp/testdir"); + // } + // else if (i == 2) + // { + // result = lfs_mkdir(fsDrive, "temp/testdir/subdir"); + // } - // create the directory - result = lfs_mkdir(fsDrive, tempPath); + // i++; - if (workingPath) + if (result != LFS_ERR_OK && result != LFS_ERR_EXIST) { - // add the separator - *tempPathP = '/'; - - // update pointer - tempPathP++; + __NOP(); + return CLR_E_FILE_IO; } - } while (workingPath && (result == LFS_ERR_OK || result == LFS_ERR_EXIST)); - - // create the directory - if (lfs_mkdir(fsDrive, normalizedPath) != LFS_ERR_OK) - { - return CLR_E_FILE_IO; + segment = strtok(NULL, "/"); } // sanity check for success - if (lfs_stat(fsDrive, normalizedPath, &info) != LFS_ERR_OK) + lfs_info info; + int32_t dirExists = lfs_stat(fsDrive, normalizedPath, &info); + _ASSERTE(dirExists == LFS_ERR_OK || dirExists == LFS_ERR_EXIST); + + // sanity check for success + if (result == LFS_ERR_EXIST) { - return CLR_E_FILE_IO; + return S_OK; } - - if (result == LFS_ERR_OK) + else if (result == LFS_ERR_OK) { // need to set the attributes for the directory // TODO: need to check if DIR attr work the same way as file attr @@ -939,13 +933,11 @@ HRESULT LITTLEFS_FS_Driver::CreateDirectory(const VOLUME_ID *volume, const char // done here return S_OK; } - // if the directory already exists, return success - else if (result == LFS_ERR_EXIST) + else { - return S_OK; + __NOP(); + return CLR_E_FILE_IO; } - - return CLR_E_FILE_IO; } return CLR_E_INVALID_DRIVER;