From 87299ece0aafcb16462f83b0fbecc9955ca9773f Mon Sep 17 00:00:00 2001 From: FactorialN <8838579+FactorialN@users.noreply.github.com> Date: Sat, 2 Dec 2023 22:14:02 -0800 Subject: [PATCH] before big change --- include/direntry.h | 22 +++++++++++++++++++++- include/files.h | 1 + lib/direntry.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ lib/files.cpp | 28 +++++++++------------------- lib/fischl.cpp | 7 +++---- 5 files changed, 76 insertions(+), 24 deletions(-) diff --git a/include/direntry.h b/include/direntry.h index e349ed9..146ef57 100644 --- a/include/direntry.h +++ b/include/direntry.h @@ -32,6 +32,7 @@ typedef struct RenameInfo { TreeNode *fischl_init_entry(int new_inode_number, const char *fileName, INode_Data *new_inode); /*the to be added file in add_entry should be parent-child relationship with treenode, otherwise will wrong */ /*see Add_FindFiletest in dir_API.cpp*/ +int fischl_add_entry_for_cache(TreeNode *parent, int new_inode_number, const char *fileName, INode_Data *new_inode, FileNode *file); int fischl_add_entry(TreeNode *parent, int new_inode_number, const char *fileName, INode_Data *new_inode); int fischl_rm_entry(TreeNode *parent, const char *fileName); /*if want to use dir mode use the subdirectory treeNode pointer */ @@ -42,4 +43,23 @@ FileNode *fischl_find_entry(TreeNode *root, const char *path); void freeTree(TreeNode *node); /*for debug use*/ TreeNode *createDirectory(const char *dirName, TreeNode *parent, int hashSize); -TreeNode *find_parentPath(TreeNode *root, const char *path); \ No newline at end of file +TreeNode *find_parentPath(TreeNode *root, const char *path); + +struct DirectoryEntry { + u_int64_t inode_number; + char file_name[256]; + void serialize(char* buffer) { + u_int64_t t = inode_number; + for (int j = 0; j < 8; j++){ + buffer[j] = t & (((u_int64_t)1<<(8))-1); + t >>= 8; + } + strcpy(buffer+8, file_name); + } + void deserialize(char* buffer) { + inode_number = 0; + for (int j = 0; j < 8; j++) + inode_number = inode_number | (((u_int64_t)(unsigned char)buffer[j])<<(8*j)); + strcpy(file_name, buffer+8); + } +}; diff --git a/include/files.h b/include/files.h index 85323bd..b6b1e16 100644 --- a/include/files.h +++ b/include/files.h @@ -28,6 +28,7 @@ class FilesOperation { 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_opendir(const char* path, struct fuse_file_info* fi); int fischl_rmdir(const char *); int fischl_readlink(const char* path, char* buf, size_t size); int fischl_symlink(const char* from, const char* to); diff --git a/lib/direntry.cpp b/lib/direntry.cpp index 2366dde..db604f9 100644 --- a/lib/direntry.cpp +++ b/lib/direntry.cpp @@ -156,6 +156,28 @@ TreeNode *fischl_init_entry(int new_inode_number, const char *fileName, INode_Da return newDir; } +int fischl_add_entry_for_cache(TreeNode *parent, int new_inode_number, const char *fileName, INode_Data *new_inode, FileNode *file){ + char *Name = strdup(fileName); + TreeNode *newDir = NULL; + /*If directory, malloc TreeNode, and then create filenode that belongs to Parent hash table content*/ + if ((new_inode->metadata.permissions & S_IFMT) == S_IFDIR) { + newDir = (TreeNode *)malloc(sizeof(TreeNode)); + newDir->dirName = Name; + newDir->contents = createHashTable(20);//hasSize define 20 + newDir->parent = parent; + } + FileNode *newFile = insertHash(parent->contents, Name, newDir); //newDir == NULL indicates it's a file + //assign INode *new_inode metadata to data member in FileNode structure + newFile->permissions = new_inode->metadata.permissions; + newFile->inode_number = new_inode_number; + //Diretory have its own file information, that is . here + if(newDir != NULL) + newDir->self_info = newFile; + file = newFile; + //free(Name); cannot free name + return 0; +} + int fischl_add_entry(TreeNode *parent, int new_inode_number, const char *fileName, INode_Data *new_inode){ char *Name = strdup(fileName); TreeNode *newDir = NULL; @@ -214,6 +236,26 @@ FileNode *fischl_find_entry(TreeNode *root, const char *path){ } else{ file = lookupHash(current->contents, segment); + if (file == NULL) { + // find on disk whether this exists + INode_Data inode; + inode.inode_num = current->self_info->inode_number; + 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 && strcmp(ent.file_name, segment)) { + if(fischl_add_entry_for_cache(current, ent.inode_number, ent.file_name, &inode, file)<0){ + return NULL; + } + break; + } + } + } + } if (file != NULL && file->subdirectory == NULL) { free(pathCopy); return file; //File found diff --git a/lib/files.cpp b/lib/files.cpp index f44cca6..a552f1f 100644 --- a/lib/files.cpp +++ b/lib/files.cpp @@ -6,25 +6,6 @@ #include #include -struct DirectoryEntry { - u_int64_t inode_number; - char file_name[256]; - void serialize(char* buffer) { - u_int64_t t = inode_number; - for (int j = 0; j < 8; j++){ - buffer[j] = t & (((u_int64_t)1<<(8))-1); - t >>= 8; - } - strcpy(buffer+8, file_name); - } - void deserialize(char* buffer) { - inode_number = 0; - for (int j = 0; j < 8; j++) - inode_number = inode_number | (((u_int64_t)(unsigned char)buffer[j])<<(8*j)); - strcpy(file_name, buffer+8); - } -}; - void FilesOperation::printbuffer(const char* buff, int len) { for(int i=0;iinode_manager->free_inode(&inode); } +int FilesOperation::fischl_opendir(const char* path, struct fuse_file_info* fi) { + u_int64_t fh = namei(path); + if (fh < 0){ + return -1; + } + fi->fh = fh; + return 0; +} + int FilesOperation::fischl_rmdir(const char* path) { char *pathdup = strdup(path); char *lastSlash = strrchr(pathdup, '/'); diff --git a/lib/fischl.cpp b/lib/fischl.cpp index b85d633..6ea0798 100644 --- a/lib/fischl.cpp +++ b/lib/fischl.cpp @@ -61,9 +61,7 @@ static int fischl_readlink(const char* path, char* buf, size_t size) { } static int fischl_opendir(const char* path, struct fuse_file_info* fi) { - u_int64_t fh = options.fsop->namei(path); - fi->fh = fh; - return 0; + return options.fsop->fischl_opendir(path, fi); } 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) { @@ -149,7 +147,7 @@ static const struct fuse_operations fischl_oper = { .unlink = fischl_unlink, .rmdir = fischl_rmdir, .symlink = fischl_symlink, - .rename = fischl_rename, + //.rename = fischl_rename, .link = fischl_link, .chmod = fischl_chmod, .chown = fischl_chown, @@ -201,6 +199,7 @@ int fischl(int argc, char *argv[]) //setupTestDirectory(&options.root); options.H = new FakeRawDisk(23552); + //options.H = new RealRawDisk("/dev/vdb"); options.fs = new Fs(options.H); options.fs->format(); options.fsop = new FilesOperation(*options.H, options.fs);