From 489997e11eca6166817bbc6140e321e6db82c4d8 Mon Sep 17 00:00:00 2001 From: FactorialN <8838579+FactorialN@users.noreply.github.com> Date: Thu, 30 Nov 2023 18:57:02 -0800 Subject: [PATCH] some stable state --- include/files.h | 1 + lib/files.cpp | 46 +++++++++++++++++++++++++++++++--------------- lib/fischl.cpp | 2 +- 3 files changed, 33 insertions(+), 16 deletions(-) diff --git a/include/files.h b/include/files.h index 8cd467c..5379139 100644 --- a/include/files.h +++ b/include/files.h @@ -25,6 +25,7 @@ class FilesOperation { 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_releasedir(const char* path, struct fuse_file_info *fi); int fischl_unlink (const char *); int fischl_open (const char *, struct fuse_file_info *);//open file int fischl_release (const char *, struct fuse_file_info *);//close file diff --git a/lib/files.cpp b/lib/files.cpp index faffbef..22536c0 100644 --- a/lib/files.cpp +++ b/lib/files.cpp @@ -288,20 +288,25 @@ int FilesOperation::fischl_getattr(const char *path, struct stat *stbuf, struct (void) fi; int res = 0; u_int64_t fh = namei(path); + + if (fh == -1){ + return -ENOENT; + } + + INode_Data inode; + inode.inode_num = fh; fs->inode_manager->load_inode(&inode); memset(stbuf, 0, sizeof(struct stat)); - if (strcmp(path, "/") == 0) { + if ((inode.metadata.permissions & S_IFMT) == S_IFDIR) { stbuf->st_mode = S_IFDIR | 0755; - stbuf->st_nlink = 2; - } else if (fh != -1) { + stbuf->st_nlink = inode.metadata.reference_count; + } else { stbuf->st_mode = S_IFREG | 0444; - stbuf->st_nlink = 1; - // TO DO: make this the correct value - stbuf->st_size = 3; - } else - res = -ENOENT; - + stbuf->st_nlink = inode.metadata.reference_count; + stbuf->st_size = inode.metadata.size; + } + perror(std::to_string(inode.metadata.size).c_str()); return res; } @@ -332,6 +337,14 @@ int FilesOperation::fischl_readdir(const char *path, void *buf, fuse_fill_dir_t return 0; } +int FilesOperation::fischl_releasedir(const char *path, struct fuse_file_info *fi){ + if(fischl_find_entry(root_node, path) == NULL) + return -ENOENT; + //do with file descriptor that cannot be used + fi->fh = -1; + return 0;//SUCESS +} + void FilesOperation::unlink_inode(u_int64_t inode_number) { INode_Data inode; inode.inode_num = inode_number; @@ -459,10 +472,12 @@ int FilesOperation::fischl_write(const char *path, const char *buf, size_t size, // Assuming inode is correctly initialized here based on 'path' inode.inode_num = fi->fh; fs->inode_manager->load_inode(&inode); - size_t len = (inode.metadata.size/IO_BLOCK_SIZE) * IO_BLOCK_SIZE; // Assuming each block is 4096 bytes + //size_t len = (inode.metadata.size/IO_BLOCK_SIZE) * IO_BLOCK_SIZE; // Assuming each block is 4096 bytes - size_t bytes_write = 0; - size_t block_index = offset / IO_BLOCK_SIZE; // Starting block index + char buffer[size]; + strcpy(buffer, buf); + size_t bytes_write = fs->write(&inode, buffer, size, offset); + /*size_t block_index = offset / IO_BLOCK_SIZE; // Starting block index size_t block_offset = offset % IO_BLOCK_SIZE; // Offset within the first block while (bytes_write < size) { char block_buffer[IO_BLOCK_SIZE]; // Temporary buffer for each block @@ -473,7 +488,7 @@ int FilesOperation::fischl_write(const char *path, const char *buf, size_t size, bytes_write += copy_size; block_index++; block_offset = 0; // Only the first block might have a non-zero offset - } + }*/ fs->inode_manager->save_inode(&inode); return bytes_write; // Return the actual number of bytes read } @@ -494,7 +509,8 @@ int FilesOperation::fischl_read(const char *path, char *buf, size_t size, off_t // Assuming inode is correctly initialized here based on 'path' inode.inode_num = fi->fh; fs->inode_manager->load_inode(&inode); - size_t len = (inode.metadata.size/IO_BLOCK_SIZE) * IO_BLOCK_SIZE; // Assuming each block is 4096 bytes + size_t bytes_read = fs->read(&inode, buf, size, offset); + /*size_t len = (inode.metadata.size/IO_BLOCK_SIZE) * IO_BLOCK_SIZE; // Assuming each block is 4096 bytes if (offset >= len) return 0; // Offset is beyond the end of the file if (offset + size > len) size = len - offset; // Adjust size if it goes beyond EOF @@ -513,7 +529,7 @@ int FilesOperation::fischl_read(const char *path, char *buf, size_t size, off_t bytes_read += copy_size; block_index++; block_offset = 0; // Only the first block might have a non-zero offset - } + }*/ return bytes_read; // Return the actual number of bytes read } \ No newline at end of file diff --git a/lib/fischl.cpp b/lib/fischl.cpp index 6e2d68c..4ad6573 100644 --- a/lib/fischl.cpp +++ b/lib/fischl.cpp @@ -182,7 +182,7 @@ static const struct fuse_operations fischl_oper = { */ .opendir = fischl_opendir, .readdir = fischl_readdir, - //.releasedir = fischl_releasedir, + .releasedir = fischl_releasedir, .init = fischl_init, .destroy = fischl_destroy, .access = fischl_access,