From bcbd23003f0aa1b3b0ac76eb1502e82f93add390 Mon Sep 17 00:00:00 2001 From: FactorialN <8838579+FactorialN@users.noreply.github.com> Date: Thu, 30 Nov 2023 17:36:26 -0800 Subject: [PATCH] a correct readdir --- include/files.h | 1 + lib/files.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ lib/fischl.cpp | 35 +++-------------------------------- 3 files changed, 52 insertions(+), 32 deletions(-) diff --git a/include/files.h b/include/files.h index 53b70a3..8cd467c 100644 --- a/include/files.h +++ b/include/files.h @@ -23,6 +23,7 @@ class FilesOperation { int fischl_mkdir(const char*, mode_t); int fischl_mknod(const char*, mode_t, dev_t);//for special file int fischl_create(const char *, mode_t, struct fuse_file_info *);//for regular file + int fischl_getattr(const char *path, struct stat *stbuf, struct fuse_file_info *fi); int fischl_readdir(const char *, void *, fuse_fill_dir_t, off_t, struct fuse_file_info *, enum fuse_readdir_flags); int fischl_unlink (const char *); int fischl_open (const char *, struct fuse_file_info *);//open file diff --git a/lib/files.cpp b/lib/files.cpp index 876da86..f73c058 100644 --- a/lib/files.cpp +++ b/lib/files.cpp @@ -283,6 +283,54 @@ int FilesOperation::fischl_create(const char* path, mode_t mode, struct fuse_fil return 0;//SUCESS } +int FilesOperation::fischl_getattr(const char *path, struct stat *stbuf, struct fuse_file_info *fi) { + + (void) fi; + int res = 0; + u_int64_t fh = namei(path); + + memset(stbuf, 0, sizeof(struct stat)); + if (strcmp(path, "/") == 0) { + stbuf->st_mode = S_IFDIR | 0755; + stbuf->st_nlink = 2; + } else if (fh != -1) { + stbuf->st_mode = S_IFREG | 0444; + stbuf->st_nlink = 1; + // TO DO: make this the correct value + stbuf->st_size = 3; + } else + res = -ENOENT; + + return res; +} + +int FilesOperation::fischl_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t ft, struct fuse_file_info *fi, enum fuse_readdir_flags flg) { + //check path + u_int64_t fh = namei(path); + + if (fh == -1){ + return -1; + } + + INode_Data inode; + inode.inode_num = fh; + fs->inode_manager->load_inode(&inode); + char buffer[IO_BLOCK_SIZE] = {0}; + for (u_int64_t idx=0; idxread(&inode, buffer, IO_BLOCK_SIZE, idx*IO_BLOCK_SIZE); + DirectoryEntry ent; + for(int i=0;i<=IO_BLOCK_SIZE-264;i+=264){ + ent.deserialize(buffer+i); + if (ent.inode_number) { + filler(buf, ent.file_name, NULL, 0, FUSE_FILL_DIR_PLUS); + //printf("%s\t%llu;\t", ent.file_name, ent.inode_number); + } + } + } + + return 0; +} + void FilesOperation::unlink_inode(u_int64_t inode_number) { INode_Data inode; inode.inode_num = inode_number; diff --git a/lib/fischl.cpp b/lib/fischl.cpp index 3e84bb1..6e2d68c 100644 --- a/lib/fischl.cpp +++ b/lib/fischl.cpp @@ -48,24 +48,7 @@ void fischl_destroy(void* private_data) { } static int fischl_getattr(const char *path, struct stat *stbuf, struct fuse_file_info *fi) { - - (void) fi; - int res = 0; - u_int64_t fh = options.fsop->namei(path); - - memset(stbuf, 0, sizeof(struct stat)); - if (strcmp(path, "/") == 0) { - stbuf->st_mode = S_IFDIR | 0755; - stbuf->st_nlink = 2; - } else if (fh != -1) { - stbuf->st_mode = S_IFREG | 0444; - stbuf->st_nlink = 1; - // TO DO: make this the correct value - stbuf->st_size = 3; - } else - res = -ENOENT; - - return res; + return options.fsop->fischl_getattr(path, stbuf, fi); } static int fischl_access(const char* path, int mask) { @@ -84,20 +67,8 @@ static int fischl_opendir(const char* path, struct fuse_file_info* fi) { return 0; } -static int fischl_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t ft, struct fuse_file_info *fi, enum fuse_readdir_flags) { - //check path - u_int64_t fh = options.fsop->namei(path); - - options.fsop->printDirectory(fh); - - char a[][6] = {".", "..", "a.txt"}; - - // Iterate through the directory entries and fill the buffer using 'filler' - for (size_t i = 0; i < 3; ++i) { - filler(buf, a[i], NULL, 0, FUSE_FILL_DIR_PLUS); - } - - return 0; +static int fischl_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t ft, struct fuse_file_info *fi, enum fuse_readdir_flags flg) { + return options.fsop->fischl_readdir(path, buf, filler, ft, fi, flg); } static int fischl_mknod(const char* path, mode_t mode, dev_t rdev) {