diff --git a/fs/fat/os_adapt/fatfs.c b/fs/fat/os_adapt/fatfs.c index abb315808b6be5f77c611c05aeaf425c23389956..e5d9df2dab30f849d929e66749d4ba4c2a01e126 100644 --- a/fs/fat/os_adapt/fatfs.c +++ b/fs/fat/os_adapt/fatfs.c @@ -1836,7 +1836,7 @@ ERROR_OUT: return -fatfs_2_vfs(result); } -int fatfs_rmdir(struct Vnode *parent, struct Vnode *vp, char *name) +int fatfs_rmdir(struct Vnode *parent, struct Vnode *vp, const char *name) { FATFS *fs = (FATFS *)vp->originMount->data; DIR_FILE *dfp = (DIR_FILE *)vp->data; @@ -1898,7 +1898,7 @@ int fatfs_reclaim(struct Vnode *vp) return 0; } -int fatfs_unlink(struct Vnode *parent, struct Vnode *vp, char *name) +int fatfs_unlink(struct Vnode *parent, struct Vnode *vp, const char *name) { FATFS *fs = (FATFS *)vp->originMount->data; DIR_FILE *dfp = (DIR_FILE *)vp->data; diff --git a/fs/fat/os_adapt/fatfs.h b/fs/fat/os_adapt/fatfs.h index 5d3297009ade61974e9f8973012ac7513fe97876..4c1f7821848792032fbaa2d4ee06d3fb05269455 100644 --- a/fs/fat/os_adapt/fatfs.h +++ b/fs/fat/os_adapt/fatfs.h @@ -131,8 +131,8 @@ int fatfs_closedir(struct Vnode *vnode, struct fs_dirent_s *dir); int fatfs_rename(struct Vnode *oldvnode, struct Vnode *newparent, const char *oldname, const char *newname); int fatfs_mkfs (struct Vnode *device, int sectors, int option); int fatfs_mkdir(struct Vnode *parent, const char *name, mode_t mode, struct Vnode **vpp); -int fatfs_rmdir(struct Vnode *parent, struct Vnode *vp, char *name); -int fatfs_unlink(struct Vnode *parent, struct Vnode *vp, char *name); +int fatfs_rmdir(struct Vnode *parent, struct Vnode *vp, const char *name); +int fatfs_unlink(struct Vnode *parent, struct Vnode *vp, const char *name); int fatfs_ioctl(struct file *filep, int req, unsigned long arg); int fatfs_fscheck(struct Vnode* vnode, struct fs_dirent_s *dir); diff --git a/fs/include/fs/vnode.h b/fs/include/fs/vnode.h index 0312b5ea259f076bc77ed7b877a42a862a10f695..677d61cd770390aedcaf47ac10e99f219f688f61 100644 --- a/fs/include/fs/vnode.h +++ b/fs/include/fs/vnode.h @@ -85,8 +85,8 @@ struct VnodeOps { int (*Open)(struct Vnode *vnode, int fd, int mode, int flags); int (*Close)(struct Vnode *vnode); int (*Reclaim)(struct Vnode *vnode); - int (*Unlink)(struct Vnode *parent, struct Vnode *vnode, char *fileName); - int (*Rmdir)(struct Vnode *parent, struct Vnode *vnode, char *dirName); + int (*Unlink)(struct Vnode *parent, struct Vnode *vnode, const char *fileName); + int (*Rmdir)(struct Vnode *parent, struct Vnode *vnode, const char *dirName); int (*Mkdir)(struct Vnode *parent, const char *dirName, mode_t mode, struct Vnode **vnode); int (*Readdir)(struct Vnode *vnode, struct fs_dirent_s *dir); int (*Opendir)(struct Vnode *vnode, struct fs_dirent_s *dir); diff --git a/fs/jffs2/src/vfs_jffs2.c b/fs/jffs2/src/vfs_jffs2.c index 00602238b973b82a9d493a450f2badbad2e273b6..cb651a2ba0c843e1f3e3fbb781bbc59912fbea1f 100644 --- a/fs/jffs2/src/vfs_jffs2.c +++ b/fs/jffs2/src/vfs_jffs2.c @@ -603,7 +603,7 @@ int VfsJffs2Chattr(struct Vnode *pVnode, struct IATTR *attr) return ret; } -int VfsJffs2Rmdir(struct Vnode *parentVnode, struct Vnode *targetVnode, char *path) +int VfsJffs2Rmdir(struct Vnode *parentVnode, struct Vnode *targetVnode, const char *path) { int ret; @@ -620,7 +620,7 @@ int VfsJffs2Rmdir(struct Vnode *parentVnode, struct Vnode *targetVnode, char *pa return ret; } -int VfsJffs2Unlink(struct Vnode *parentVnode, struct Vnode *targetVnode, char *path) +int VfsJffs2Unlink(struct Vnode *parentVnode, struct Vnode *targetVnode, const char *path) { int ret; diff --git a/fs/vfs/operation/fs_file_mapping.c b/fs/vfs/operation/fs_file_mapping.c index c73776444eccd8dc969a253305e7ce82e139a747..496b22ae03a6deafde5121bd54adf10dfc736811 100644 --- a/fs/vfs/operation/fs_file_mapping.c +++ b/fs/vfs/operation/fs_file_mapping.c @@ -270,11 +270,27 @@ int update_file_path(const char *old_path, const char *new_path) continue; } int len = strlen(new_path) + 1; - filp->f_path = zalloc(len); - strncpy_s(filp->f_path, strlen(new_path) + 1, new_path, len); + char *tmp_path = LOS_MemAlloc(m_aucSysMem0, len); + if (tmp_path == NULL) { + PRINT_ERR("%s-%d: Mem alloc failed, path length(%d)\n", __FUNCTION__, __LINE__, len); + ret = VFS_ERROR; + goto out; + } + ret = strncpy_s(tmp_path, strlen(new_path) + 1, new_path, len); + if (ret != 0) { + (VOID)LOS_MemFree(m_aucSysMem0, tmp_path); + PRINT_ERR("%s-%d: strcpy failed.\n", __FUNCTION__, __LINE__); + ret = VFS_ERROR; + goto out; + } + free(filp->f_path); + filp->f_path = tmp_path; } + ret = LOS_OK; + +out: (VOID)LOS_MuxUnlock(&g_file_mapping.lock); (void)sem_post(&f_list->fl_sem); - return LOS_OK; + return ret; } #endif diff --git a/fs/vfs/path_cache.c b/fs/vfs/path_cache.c index 2d607e2cddd48c444859424752c0ae3799bfa83a..c6d716443b158582c41a2ad4636c0c6849706140 100644 --- a/fs/vfs/path_cache.c +++ b/fs/vfs/path_cache.c @@ -82,7 +82,7 @@ static uint32_t NameHash(const char *name, int len, struct Vnode *dvp) { uint32_t hash; hash = fnv_32_buf(name, len, FNV1_32_INIT); - hash = fnv_32_buf(&dvp, sizeof(dvp), hash); + hash = fnv_32_buf(&dvp, sizeof(struct Vnode *), hash); return hash; } diff --git a/fs/vfs/vnode.c b/fs/vfs/vnode.c index b206cdf6ebe816af7812cf6a6f50568f0182def5..5bc90dcb3f0c86169c14a90477c4c40731ecfdd5 100644 --- a/fs/vfs/vnode.c +++ b/fs/vfs/vnode.c @@ -245,9 +245,6 @@ int VnodeDrop() static char *NextName(char *pos, uint8_t *len) { char *name = NULL; - if (*pos == '\0') { - return NULL; - } while (*pos != 0 && *pos == '/') { pos++; } @@ -319,6 +316,7 @@ static int Step(char **currentDir, struct Vnode **currentVnode, uint32_t flags) } nextDir = NextName(*currentDir, &len); if (nextDir == NULL) { + // there is '/' at the end of the *currentDir. *currentDir = NULL; return LOS_OK; } @@ -361,7 +359,6 @@ int VnodeLookup(const char *path, struct Vnode **result, uint32_t flags) struct Vnode *startVnode = NULL; char *normalizedPath = NULL; - int ret = PreProcess(path, &startVnode, &normalizedPath); if (ret != LOS_OK) { PRINT_ERR("[VFS]lookup failed, invalid path=%s err = %d\n", path, ret); @@ -377,9 +374,9 @@ int VnodeLookup(const char *path, struct Vnode **result, uint32_t flags) char *currentDir = normalizedPath; struct Vnode *currentVnode = startVnode; - while (currentDir && *currentDir != '\0') { + while (*currentDir != '\0') { ret = Step(¤tDir, ¤tVnode, flags); - if (*currentDir == '\0') { + if (currentDir == NULL || *currentDir == '\0') { // return target or parent vnode as result *result = currentVnode; } else if (VfsVnodePermissionCheck(currentVnode, EXEC_OP)) { diff --git a/syscall/fs_syscall.c b/syscall/fs_syscall.c index 2df23ff41006b4e79ccba098498c196760044ed3..b869ee2a19eca7195ceb69d8d467a8a6a465bcbf 100644 --- a/syscall/fs_syscall.c +++ b/syscall/fs_syscall.c @@ -270,13 +270,11 @@ int SysOpen(const char *path, int oflags, ...) mode_t mode = DEFAULT_FILE_MODE; /* 0666: File read-write properties. */ char *pathRet = NULL; - if (path == NULL && *path == 0) { - return -EINVAL; - } - - ret = UserPathCopy(path, &pathRet); - if (ret != 0) { - return ret; + if (path != NULL) { + ret = UserPathCopy(path, &pathRet); + if (ret != 0) { + goto ERROUT_PATH_FREE; + } } procFd = AllocProcessFd(); @@ -310,15 +308,16 @@ int SysOpen(const char *path, int oflags, ...) return procFd; ERROUT: - if (pathRet != NULL) { - LOS_MemFree(OS_SYS_MEM_ADDR, pathRet); - } if (ret >= 0) { AssociateSystemFd(procFd, ret); ret = procFd; } else { FreeProcessFd(procFd); } +ERROUT_PATH_FREE: + if (pathRet != NULL) { + LOS_MemFree(OS_SYS_MEM_ADDR, pathRet); + } return ret; }