🆕 完成了ls的功能

This commit is contained in:
fslongjin
2022-05-29 14:36:46 +08:00
parent a4157bb4a7
commit 9ee6d33318
8 changed files with 106 additions and 16 deletions

View File

@ -1,5 +1,8 @@
#include "VFS.h"
#include <common/kprint.h>
#include <common/dirent.h>
#include <common/errno.h>
#include <mm/mm.h>
#include <mm/slab.h>
// 为filesystem_type_t结构体实例化一个链表头
@ -138,9 +141,27 @@ struct vfs_dir_entry_t *vfs_path_walk(char *path, uint64_t flags)
/**
* @brief 填充dentry
*
*
*/
int vfs_fill_dentry(void *buf, ino_t d_ino, char *name, int namelen, unsigned char type, off_t offset)
{
struct dirent *dent = (struct dirent *)buf;
// 如果尝试访问内核空间,则返回错误
if (!(verify_area((uint64_t)buf, sizeof(struct dirent) + namelen)))
return -EFAULT;
// ====== 填充dirent结构体 =====
memset(buf, 0, sizeof(struct dirent) + namelen);
memcpy(dent->d_name, name, namelen);
dent->d_name[namelen] = '\0';
// 暂时不显示目录下的记录数
dent->d_reclen = 0;
dent->d_ino = d_ino;
dent->d_off = offset;
dent->d_type = type;
// 返回dirent的总大小
return sizeof(struct dirent) + namelen;
}

View File

@ -930,6 +930,8 @@ int64_t fat32_readdir(struct vfs_file_t *file_ptr, void *dirent, vfs_filldir_t f
}
}
uint64_t dentry_type = 0; // 传递给filler的dentry类型数据
char *dir_name = NULL;
int name_len = 0;
// ==== 此时已经将文件夹的目录项起始簇的簇号读取到cluster变量中 ===
@ -1012,6 +1014,7 @@ int64_t fat32_readdir(struct vfs_file_t *file_ptr, void *dirent, vfs_filldir_t f
}
// 读取目录项成功,返回
dentry_type = dentry->DIR_Attr;
goto find_dir_success;
}
else // 不存在长目录项
@ -1035,7 +1038,10 @@ int64_t fat32_readdir(struct vfs_file_t *file_ptr, void *dirent, vfs_filldir_t f
// 如果当前短目录项为文件夹,则直接返回,不需要读取扩展名
if (dentry->DIR_Attr & ATTR_DIRECTORY)
{
dentry_type = dentry->DIR_Attr;
goto find_dir_success;
}
// 是文件,增加 .
dir_name[name_len++] = '.';
@ -1056,6 +1062,7 @@ int64_t fat32_readdir(struct vfs_file_t *file_ptr, void *dirent, vfs_filldir_t f
if (total_len == 8) // 没有扩展名
dir_name[--name_len] = '\0';
dentry_type = dentry->DIR_Attr;
goto find_dir_success;
}
}
@ -1072,7 +1079,12 @@ find_dir_success:;
// 将文件夹位置坐标加32即指向下一个目录项
file_ptr->position += 32;
// todo: 计算ino_t
return filler(dirent, 0, dir_name, name_len, 0, 0);
if(dentry_type & ATTR_DIRECTORY)
dentry_type = VFS_ATTR_DIR;
else
dentry_type = VFS_ATTR_FILE;
return filler(dirent, 0, dir_name, name_len, dentry_type, 0);
}
struct vfs_inode_operations_t fat32_inode_ops =