fix: 修复littlefs的多分区挂载时,对pathname的处理逻辑异常

close:#I3WWFA

Signed-off-by: li_zan <371442490@qq.com>
This commit is contained in:
li_zan 2021-06-22 11:45:59 +08:00
parent 139f83af39
commit b55cfc1ba7
2 changed files with 18 additions and 14 deletions

View File

@ -120,24 +120,28 @@ BOOL CheckDirIsOpen(const char *dirName)
return FALSE; return FALSE;
} }
int GetFirstLevelPathLen(const char *pathName)
{
int len = 1;
for (int i = 1; i < strlen(pathName) + 1; i++) {
if (pathName[i] == '/') {
break;
}
len++;
}
return len;
}
BOOL CheckPathIsMounted(const char *pathName, struct FileOpInfo **fileOpInfo) BOOL CheckPathIsMounted(const char *pathName, struct FileOpInfo **fileOpInfo)
{ {
char tmpName[LITTLEFS_MAX_LFN_LEN] = {0}; char tmpName[LITTLEFS_MAX_LFN_LEN] = {0};
int mountPathNameLen; int len = GetFirstLevelPathLen(pathName);
int len = strlen(pathName) + 1;
pthread_mutex_lock(&g_FslocalMutex); pthread_mutex_lock(&g_FslocalMutex);
for (int i = 0; i < LOSCFG_LFS_MAX_MOUNT_SIZE; i++) { for (int i = 0; i < LOSCFG_LFS_MAX_MOUNT_SIZE; i++) {
if (g_fsOp[i].useFlag == 1) { if (g_fsOp[i].useFlag == 1) {
mountPathNameLen = strlen(g_fsOp[i].dirName); (void)strncpy_s(tmpName, LITTLEFS_MAX_LFN_LEN, pathName, len);
if (len < mountPathNameLen + 1) {
pthread_mutex_unlock(&g_FslocalMutex);
return FALSE;
}
(void)strncpy_s(tmpName, LITTLEFS_MAX_LFN_LEN, pathName, mountPathNameLen);
tmpName[mountPathNameLen] = '\0';
if (strcmp(tmpName, g_fsOp[i].dirName) == 0) { if (strcmp(tmpName, g_fsOp[i].dirName) == 0) {
*fileOpInfo = &(g_fsOp[i]); *fileOpInfo = &(g_fsOp[i]);
pthread_mutex_unlock(&g_FslocalMutex); pthread_mutex_unlock(&g_FslocalMutex);
@ -156,7 +160,7 @@ struct FileOpInfo *AllocMountRes(const char* target, struct FileOps *fileOps)
if (g_fsOp[i].useFlag == 0 && strcmp(target, g_littlefsMntName[i]) == 0) { if (g_fsOp[i].useFlag == 0 && strcmp(target, g_littlefsMntName[i]) == 0) {
g_fsOp[i].useFlag = 1; g_fsOp[i].useFlag = 1;
g_fsOp[i].fsVops = fileOps; g_fsOp[i].fsVops = fileOps;
g_fsOp[i].dirName == strdup(target); g_fsOp[i].dirName = strdup(target);
pthread_mutex_unlock(&g_FslocalMutex); pthread_mutex_unlock(&g_FslocalMutex);
return &(g_fsOp[i]); return &(g_fsOp[i]);
} }
@ -530,7 +534,7 @@ struct dirent *LfsReaddir(DIR *dir)
} }
ret = lfs_dir_read(dirInfo->lfsHandle, (lfs_dir_t *)(&(dirInfo->dir)), &lfsInfo); ret = lfs_dir_read(dirInfo->lfsHandle, (lfs_dir_t *)(&(dirInfo->dir)), &lfsInfo);
if (ret == 0) { if (ret == TRUE) {
pthread_mutex_lock(&g_FslocalMutex); pthread_mutex_lock(&g_FslocalMutex);
(void)strncpy_s(g_nameValue.d_name, sizeof(g_nameValue.d_name), lfsInfo.name, strlen(lfsInfo.name) + 1); (void)strncpy_s(g_nameValue.d_name, sizeof(g_nameValue.d_name), lfsInfo.name, strlen(lfsInfo.name) + 1);
if (lfsInfo.type == LFS_TYPE_DIR) { if (lfsInfo.type == LFS_TYPE_DIR) {

View File

@ -38,7 +38,7 @@ static_library("posix") {
"src/pthread_mutex.c", "src/pthread_mutex.c",
"src/semaphore.c", "src/semaphore.c",
"src/time.c", "src/time.c",
"//third_party/musl/src/string/strdup.c", "//third_party/musl/porting/liteos_m/kernel/src/string/strdup.c",
] ]
include_dirs = [ include_dirs = [