fs: implement readdir function

this also fixes flawed file parsing in ext2_open_file
This commit is contained in:
Michael Shaw 2023-10-20 00:50:53 -04:00
commit 0eb461410c
7 changed files with 292 additions and 67 deletions

View file

@ -19,6 +19,14 @@
#define EXT2_DE_SOCK 6
#define EXT2_DE_SYMLINK 7
#define EXT2_INO_FIFO 0x1000
#define EXT2_INO_CHARDEV 0x2000
#define EXT2_INO_DIR 0x4000
#define EXT2_INO_BLKDEV 0x6000
#define EXT2_INO_REG 0x8000
#define EXT2_INO_SYMLINK 0xA000
#define EXT2_INO_SOCK 0xC000
#define EXT2_FLAG_LONG_FILESIZE 0x0002
struct ext2_superblock {

View file

@ -8,6 +8,15 @@
#define VFS_TYPE_EXT2 0x0001
#define VFS_FILE_FILE 0x0001
#define VFS_FILE_DIR 0x0002
#define VFS_FILE_CHARDEV 0x0003
#define VFS_FILE_BLKDEV 0x0004
#define VFS_FILE_FIFO 0x0005
#define VFS_FILE_SOCK 0x0006
#define VFS_FILE_SYMLINK 0x0007
struct inode {
uint16_t mode;
uint16_t uid;
@ -37,16 +46,25 @@ struct inode {
struct dirent {
uint64_t inode;
uint64_t offset;
uint16_t reclen;
uint8_t type;
char name[256];
};
typedef struct _DIR {
struct fs *fs;
struct dirent *dirents;
size_t num_dirents;
struct file *file;
int fd;
uint64_t pos;
} DIR;
struct file {
struct inode inode;
uint32_t inode_num; /* inode number */
uint32_t type; /* type of the file */
uint64_t size; /* size of the file */
char *path; /* path to the file */
@ -66,11 +84,12 @@ struct file_descriptor {
};
struct fs_ops {
int (*read)(void *fs, struct file *file, void *buf, size_t size);
int (*write)(void *fs, struct file *file, void *buf, size_t size);
int (*open)(void *fs, struct file *file, const char *path);
int (*close)(void *fs, struct file *file);
int (*seek)(void *fs, struct file *file, size_t offset);
int (*read)(struct fs *fs, struct file *file, void *buf, size_t size);
int (*write)(struct fs *fs, struct file *file, void *buf, size_t size);
int (*open)(struct fs *fs, struct file *file, const char *path);
int (*close)(struct fs *fs, struct file *file);
int (*seek)(struct fs *fs, struct file *file, size_t offset);
int (*readdir)(struct fs *fs, uint32_t ino, struct dirent **dir);
};
struct fs {
@ -82,5 +101,10 @@ struct fs {
};
void vfs_init(const char *rootfs);
int write(int fd, const void *buf, size_t count);
int read(int fd, void *buf, size_t count);
int open(const char *pathname, int flags);
int close(int fd);
int seek(int fd, size_t offset, int whence);
#endif /* _VFS_H_ */