From 1c792ba738fc9d992f94e5320364b516a3b7c8ef Mon Sep 17 00:00:00 2001 From: FactorialN <8838579+FactorialN@users.noreply.github.com> Date: Sun, 3 Dec 2023 14:15:58 -0800 Subject: [PATCH] fixed persistency issue --- README.md | 13 +++++++++---- include/direntry.h | 2 +- include/files.h | 3 ++- lib/direntry.cpp | 14 +++++++------- lib/files.cpp | 21 ++++++++++++++++++--- lib/fischl.cpp | 23 ++++++++++++++++++----- 6 files changed, 55 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 43a7cbe..d3936ef 100644 --- a/README.md +++ b/README.md @@ -22,18 +22,23 @@ make # cmake --build . is same ## mount and test normal usage: ```bash -./fischl diskpath mountpoint +./fischl diskpath n mountpoint +``` +diskpath must be provided following ./fischl +l/n must be provided following diskpath indicating whether to load the exisiting file system or create a new one. +for loading: +```bash +./fischl diskpath l mountpoint ``` -diskpath must be the provided following ./fischl if the diskpath need to be accessed by root: ```bash -sudo ./fischl diskpath -o allow_other mountpoint +sudo ./fischl diskpath n -o allow_other mountpoint ``` for debugging: ```bash -sudo ./fischl diskpath -o allow_other -d mountpoint +sudo ./fischl diskpath n -o allow_other -d mountpoint ``` ## run test diff --git a/include/direntry.h b/include/direntry.h index de8289f..a4c6cbb 100644 --- a/include/direntry.h +++ b/include/direntry.h @@ -33,7 +33,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); +FileNode* fischl_add_entry_for_cache(TreeNode *parent, int new_inode_number, const char *fileName, INode_Data *new_inode); 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 */ diff --git a/include/files.h b/include/files.h index 68a55eb..64c7101 100644 --- a/include/files.h +++ b/include/files.h @@ -8,12 +8,13 @@ class FilesOperation { Fs *fs; void create_dot_dotdot(INode_Data*, u_int64_t); - public: +public: TreeNode *root_node; FilesOperation(RawDisk&, Fs*); //int read_datablock(const INode_Data& inode, u_int64_t index, char* buffer); //int write_datablock(INode_Data& inode, u_int64_t index, char* buffer); void initialize_rootinode(); + void initialize(bool load); void printbuffer(const char*,int); void printDirectory(u_int64_t); bool permission_check(int, INode_Data*); diff --git a/lib/direntry.cpp b/lib/direntry.cpp index 2f3d2b4..2c6c1ba 100644 --- a/lib/direntry.cpp +++ b/lib/direntry.cpp @@ -156,7 +156,7 @@ 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){ +FileNode* fischl_add_entry_for_cache(TreeNode *parent, int new_inode_number, const char *fileName, INode_Data *new_inode){ char *Name = strdup(fileName); TreeNode *newDir = NULL; /*If directory, malloc TreeNode, and then create filenode that belongs to Parent hash table content*/ @@ -173,9 +173,7 @@ int fischl_add_entry_for_cache(TreeNode *parent, int new_inode_number, const cha //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; + return newFile; } int fischl_add_entry(TreeNode *parent, int new_inode_number, const char *fileName, INode_Data *new_inode){ @@ -220,6 +218,8 @@ FileNode *fischl_find_entry(Fs *fs, TreeNode *root, const char *path){ TreeNode *current = root; FileNode *file = NULL; + printf("FINDING %s %s %llu\n", path, segment, current->self_info->inode_number); + while (segment != NULL && current != NULL) { if (strcmp(segment, "..") == 0) { // Move up to the parent directory @@ -249,9 +249,8 @@ FileNode *fischl_find_entry(Fs *fs, TreeNode *root, const char *path){ ent.deserialize(buffer+i); //printf("WARNING:%d %llu %llu %s %s\n",__LINE__,inode.inode_num, ent.inode_number, ent.file_name, segment); if (ent.inode_number && strcmp(ent.file_name, segment)==0) { - if(fischl_add_entry_for_cache(current, ent.inode_number, ent.file_name, &inode, file)<0){ - return NULL; - } + file = fischl_add_entry_for_cache(current, ent.inode_number, ent.file_name, &inode); + //printf("DONE !! %llu\n", file->inode_number); break; } } @@ -259,6 +258,7 @@ FileNode *fischl_find_entry(Fs *fs, TreeNode *root, const char *path){ } if (file != NULL && file->subdirectory == NULL) { free(pathCopy); + //printf("FOUND !! %llu\n", file->inode_number); return file; //File found //return current; return filenode } diff --git a/lib/files.cpp b/lib/files.cpp index 8ce02f5..0dda907 100644 --- a/lib/files.cpp +++ b/lib/files.cpp @@ -46,6 +46,20 @@ void FilesOperation::initialize_rootinode() { fs->inode_manager->save_inode(root_inode); } +void FilesOperation::initialize(bool load) { + if (load){ + INode_Data *root_inode = new INode_Data(); + root_inode->inode_num = 1; + fs->inode_manager->load_inode(root_inode); + root_node = fischl_init_entry(1, "/", root_inode); + assert(root_node->self_info!=NULL); + fs->load_superblock(); + } + else{ + initialize_rootinode(); + } +} + void FilesOperation::printDirectory(u_int64_t inode_number) { INode_Data inode; inode.inode_num = inode_number; @@ -97,6 +111,7 @@ INode_Data* FilesOperation::create_new_inode(u_int64_t parent_inode_number, cons bool allocated = false; INode_Data *new_inode = new INode_Data(); fs->inode_manager->new_inode(getuid(), getgid(), mode, new_inode); + //printf("%llu\n",new_inode->inode_num); if ((mode & S_IFMT) == S_IFDIR) { create_dot_dotdot(new_inode, parent_inode_number); fs->inode_manager->save_inode(new_inode); @@ -194,7 +209,7 @@ bool FilesOperation::permission_check(int mask, INode_Data *inode) { mode_t per = (mode_t)inode->metadata.permissions; uid_t uid = (uid_t)inode->metadata.uid; gid_t gid = (gid_t)inode->metadata.gid; - printf("PERMISSION CHECK %d %llu %llu %o\n", mask, uid, gid, per); + //printf("PERMISSION CHECK %d %llu %llu %o\n", mask, uid, gid, per); if(getuid() == uid){ if ((mask & R_OK) && !(per & S_IRUSR)) { return false; // Permission denied for reading @@ -367,7 +382,7 @@ int FilesOperation::fischl_getattr(const char *path, struct stat *stbuf, struct inode.inode_num = fh; fs->inode_manager->load_inode(&inode); - printf("GETATTR PERM %o\n", (mode_t)inode.metadata.permissions); + //printf("GETATTR PERM %o\n", (mode_t)inode.metadata.permissions); //memset(stbuf, 0, sizeof(struct stat)); if ((inode.metadata.permissions & S_IFMT) == S_IFDIR) { @@ -424,7 +439,7 @@ int FilesOperation::fischl_readdir(const char *path, void *buf, fuse_fill_dir_t 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); + printf("%s\t%llu;\t\n", ent.file_name, ent.inode_number); } } } diff --git a/lib/fischl.cpp b/lib/fischl.cpp index f123ea6..242bfa4 100644 --- a/lib/fischl.cpp +++ b/lib/fischl.cpp @@ -22,6 +22,7 @@ static struct options { Fs *fs; FilesOperation *fsop; int show_help; + bool load; } options; #define OPTION(t, p) \ @@ -34,7 +35,7 @@ static const struct fuse_opt option_spec[] = { void* fischl_init(struct fuse_conn_info *conn, struct fuse_config *cfg) { cfg->use_ino = 1; - options.fsop->initialize_rootinode(); + options.fsop->initialize(options.load); } int fischl_create(const char *path, mode_t mode, struct fuse_file_info *fi) { @@ -191,12 +192,14 @@ static void show_help(const char *progname) int fischl(int argc, char *argv[]) { int ret; - if(argc < 2){ - printf("WRONG ARGUMENTS"); + if(argc < 3){ + printf("WRONG ARGUMENTS\n"); return 0; } std::swap(argv[0], argv[1]); - struct fuse_args args = FUSE_ARGS_INIT(argc-1, argv+1); + std::swap(argv[1], argv[2]); + + struct fuse_args args = FUSE_ARGS_INIT(argc-2, argv+2); srand(time(NULL)); // Seed the random number generator //const char* d = (argc < 2) ? "/dev/vdc" : argv[1]; @@ -207,8 +210,18 @@ int fischl(int argc, char *argv[]) else{ options.H = new RealRawDisk(argv[0]); } + if(strcmp(argv[1], "l")==0){ + options.load = true; + } + else if(strcmp(argv[1], "n")==0){ + options.load = false; + } + else{ + printf("WRONG l/n ARGUMENTS\n"); + return 0; + } options.fs = new Fs(options.H); - options.fs->format(); + if(!options.load)options.fs->format(); options.fsop = new FilesOperation(*options.H, options.fs);