From bef4bbd50acf5395b81c6af194f29a41fc1e8cdd Mon Sep 17 00:00:00 2001 From: Shell Date: Fri, 15 Mar 2024 14:57:38 +0800 Subject: [PATCH] [dfs] fixup bugs in dfs_dentry_lookup (#8612) Signed-off-by: Shell --- components/dfs/dfs_v2/include/dfs_dentry.h | 1 + components/dfs/dfs_v2/src/dfs_dentry.c | 33 +++++++++++++++------- components/dfs/dfs_v2/src/dfs_file.c | 8 +++++- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/components/dfs/dfs_v2/include/dfs_dentry.h b/components/dfs/dfs_v2/include/dfs_dentry.h index 190e277b0b..56b0c2dbb8 100644 --- a/components/dfs/dfs_v2/include/dfs_dentry.h +++ b/components/dfs/dfs_v2/include/dfs_dentry.h @@ -41,6 +41,7 @@ struct dfs_dentry }; struct dfs_dentry *dfs_dentry_create(struct dfs_mnt *mnt, char *fullpath); +struct dfs_dentry *dfs_dentry_create_rela(struct dfs_mnt *mnt, char *rela_path); struct dfs_dentry *dfs_dentry_unref(struct dfs_dentry *dentry); struct dfs_dentry *dfs_dentry_ref(struct dfs_dentry *dentry); void dfs_dentry_insert(struct dfs_dentry *dentry); diff --git a/components/dfs/dfs_v2/src/dfs_dentry.c b/components/dfs/dfs_v2/src/dfs_dentry.c index f15aaa9cdd..8623ec051f 100644 --- a/components/dfs/dfs_v2/src/dfs_dentry.c +++ b/components/dfs/dfs_v2/src/dfs_dentry.c @@ -40,11 +40,11 @@ static uint32_t _dentry_hash(struct dfs_mnt *mnt, const char *path) return (val ^ (unsigned long) mnt) & (DFS_DENTRY_HASH_NR - 1); } -struct dfs_dentry *dfs_dentry_create(struct dfs_mnt *mnt, char *fullpath) +static struct dfs_dentry *_dentry_create(struct dfs_mnt *mnt, char *path, rt_bool_t is_rela_path) { struct dfs_dentry *dentry = RT_NULL; - if (mnt == RT_NULL || fullpath == RT_NULL) + if (mnt == RT_NULL || path == RT_NULL) { return dentry; } @@ -52,15 +52,18 @@ struct dfs_dentry *dfs_dentry_create(struct dfs_mnt *mnt, char *fullpath) dentry = (struct dfs_dentry *)rt_calloc(1, sizeof(struct dfs_dentry)); if (dentry) { - char *dentry_path = fullpath; - int mntpoint_len = strlen(mnt->fullpath); - - if (rt_strncmp(mnt->fullpath, dentry_path, mntpoint_len) == 0) + char *dentry_path = path; + if (!is_rela_path) { - dentry_path += mntpoint_len; + int mntpoint_len = strlen(mnt->fullpath); + + if (rt_strncmp(mnt->fullpath, dentry_path, mntpoint_len) == 0) + { + dentry_path += mntpoint_len; + } } - dentry->pathname = strlen(dentry_path) ? rt_strdup(dentry_path) : rt_strdup(fullpath); + dentry->pathname = strlen(dentry_path) ? rt_strdup(dentry_path) : rt_strdup(path); dentry->mnt = dfs_mnt_ref(mnt); rt_atomic_store(&(dentry->ref_count), 1); @@ -72,6 +75,16 @@ struct dfs_dentry *dfs_dentry_create(struct dfs_mnt *mnt, char *fullpath) return dentry; } +struct dfs_dentry *dfs_dentry_create(struct dfs_mnt *mnt, char *fullpath) +{ + return _dentry_create(mnt, fullpath, RT_FALSE); +} + +struct dfs_dentry *dfs_dentry_create_rela(struct dfs_mnt *mnt, char *rela_path) +{ + return _dentry_create(mnt, rela_path, RT_TRUE);; +} + struct dfs_dentry * dfs_dentry_ref(struct dfs_dentry *dentry) { if (dentry) @@ -206,8 +219,8 @@ struct dfs_dentry *dfs_dentry_lookup(struct dfs_mnt *mnt, const char *path, uint { DLOG(activate, "dentry"); /* not in hash table, create it */ - DLOG(msg, "dentry", "dentry", DLOG_MSG, "dfs_dentry_create(%s)", path); - dentry = dfs_dentry_create(mnt, (char*)path); + DLOG(msg, "dentry", "dentry", DLOG_MSG, "dfs_dentry_create_rela(mnt=%s, path=%s)", mnt->fullpath, path); + dentry = dfs_dentry_create_rela(mnt, (char*)path); if (dentry) { DLOG(msg, "dentry", mnt->fs_ops->name, DLOG_MSG, "vnode=fs_ops->lookup(dentry)"); diff --git a/components/dfs/dfs_v2/src/dfs_file.c b/components/dfs/dfs_v2/src/dfs_file.c index e4f9094d67..5f55eaaba1 100644 --- a/components/dfs/dfs_v2/src/dfs_file.c +++ b/components/dfs/dfs_v2/src/dfs_file.c @@ -1767,6 +1767,7 @@ int dfs_file_mmap2(struct dfs_file *file, struct dfs_mmap2_args *mmap2) #define _COLOR_RED "\033[31m" #define _COLOR_GREEN "\033[32m" +#define _COLOR_YELLOW "\033[33m" #define _COLOR_BLUE "\033[34m" #define _COLOR_CYAN "\033[36m" #define _COLOR_WHITE "\033[37m" @@ -1895,6 +1896,11 @@ void ls(const char *pathname) rt_kprintf(_COLOR_GREEN "%-20s" _COLOR_NORMAL, dirent.d_name); rt_kprintf("%-25lu\n", (unsigned long)stat.st_size); } + else if (S_ISCHR(stat.st_mode)) + { + rt_kprintf(_COLOR_YELLOW "%-20s" _COLOR_NORMAL, dirent.d_name); + rt_kprintf("%-25s\n", ""); + } else { rt_kprintf("%-20s", dirent.d_name); @@ -1903,7 +1909,7 @@ void ls(const char *pathname) } else { - rt_kprintf(_COLOR_RED "%-20s" _COLOR_NORMAL, dirent.d_name); + rt_kprintf(_COLOR_RED "%-20s\n" _COLOR_NORMAL, dirent.d_name); } rt_free(fullpath);