[dfs] fixup bugs in dfs_dentry_lookup (#8612)

Signed-off-by: Shell <smokewood@qq.com>
This commit is contained in:
Shell 2024-03-15 14:57:38 +08:00 committed by GitHub
parent 62b0d4fd2e
commit bef4bbd50a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 31 additions and 11 deletions

View File

@ -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(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_unref(struct dfs_dentry *dentry);
struct dfs_dentry *dfs_dentry_ref(struct dfs_dentry *dentry); struct dfs_dentry *dfs_dentry_ref(struct dfs_dentry *dentry);
void dfs_dentry_insert(struct dfs_dentry *dentry); void dfs_dentry_insert(struct dfs_dentry *dentry);

View File

@ -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); 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; struct dfs_dentry *dentry = RT_NULL;
if (mnt == RT_NULL || fullpath == RT_NULL) if (mnt == RT_NULL || path == RT_NULL)
{ {
return dentry; 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)); dentry = (struct dfs_dentry *)rt_calloc(1, sizeof(struct dfs_dentry));
if (dentry) if (dentry)
{ {
char *dentry_path = fullpath; char *dentry_path = path;
int mntpoint_len = strlen(mnt->fullpath); if (!is_rela_path)
if (rt_strncmp(mnt->fullpath, dentry_path, mntpoint_len) == 0)
{ {
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); dentry->mnt = dfs_mnt_ref(mnt);
rt_atomic_store(&(dentry->ref_count), 1); 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; 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) struct dfs_dentry * dfs_dentry_ref(struct dfs_dentry *dentry)
{ {
if (dentry) if (dentry)
@ -206,8 +219,8 @@ struct dfs_dentry *dfs_dentry_lookup(struct dfs_mnt *mnt, const char *path, uint
{ {
DLOG(activate, "dentry"); DLOG(activate, "dentry");
/* not in hash table, create it */ /* not in hash table, create it */
DLOG(msg, "dentry", "dentry", DLOG_MSG, "dfs_dentry_create(%s)", path); DLOG(msg, "dentry", "dentry", DLOG_MSG, "dfs_dentry_create_rela(mnt=%s, path=%s)", mnt->fullpath, path);
dentry = dfs_dentry_create(mnt, (char*)path); dentry = dfs_dentry_create_rela(mnt, (char*)path);
if (dentry) if (dentry)
{ {
DLOG(msg, "dentry", mnt->fs_ops->name, DLOG_MSG, "vnode=fs_ops->lookup(dentry)"); DLOG(msg, "dentry", mnt->fs_ops->name, DLOG_MSG, "vnode=fs_ops->lookup(dentry)");

View File

@ -1767,6 +1767,7 @@ int dfs_file_mmap2(struct dfs_file *file, struct dfs_mmap2_args *mmap2)
#define _COLOR_RED "\033[31m" #define _COLOR_RED "\033[31m"
#define _COLOR_GREEN "\033[32m" #define _COLOR_GREEN "\033[32m"
#define _COLOR_YELLOW "\033[33m"
#define _COLOR_BLUE "\033[34m" #define _COLOR_BLUE "\033[34m"
#define _COLOR_CYAN "\033[36m" #define _COLOR_CYAN "\033[36m"
#define _COLOR_WHITE "\033[37m" #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(_COLOR_GREEN "%-20s" _COLOR_NORMAL, dirent.d_name);
rt_kprintf("%-25lu\n", (unsigned long)stat.st_size); 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", "<CHR>");
}
else else
{ {
rt_kprintf("%-20s", dirent.d_name); rt_kprintf("%-20s", dirent.d_name);
@ -1903,7 +1909,7 @@ void ls(const char *pathname)
} }
else 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); rt_free(fullpath);