From ee66c10796376133e71e46e4dbe1454c6ac13593 Mon Sep 17 00:00:00 2001 From: Victor Date: Fri, 17 Nov 2023 14:37:26 -0800 Subject: [PATCH] simplify checking path way in mkdir and mknod --- lib/files.cpp | 71 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 47 insertions(+), 24 deletions(-) diff --git a/lib/files.cpp b/lib/files.cpp index 846c22f..525b376 100644 --- a/lib/files.cpp +++ b/lib/files.cpp @@ -1,7 +1,13 @@ +//#include "fuse.h" add this when layer3 #include "files.h" #include #include +/********************************************************************* + Directory Entry definition and function + + +*********************************************************************/ struct DirectoryEntry { u_int64_t inode_number; char file_name[56]; @@ -20,6 +26,21 @@ struct DirectoryEntry { strcpy(file_name, buffer+8); } }; +//Directory collection +typedef struct { + //DirectoryEntry entries[MAX_ENTRIES]; with tree structure + unsigned int num_entries; +} Directory; +/* + * fishcl_add_entry() + * + * adds a file entry to the specified directory, using the same + * semantics as fishcl_find_entry(). It returns NULL if it failed. + * + */ +static int fishcl_add_entry(Directory *dir, unsigned int inode_number, const char *name){ + +} FilesOperation::FilesOperation(RawDisk& disk_): disk(disk_) { inop.initialize(disk); @@ -155,46 +176,48 @@ u_int64_t FilesOperation::namei(const char* path) { // path = "/notnonemptydir/foo" should raise error } +/**/ u_int64_t FilesOperation::fischl_mkdir(const char* path, mode_t mode) { + //check path char *pathdup = strdup(path); - char *ptr = strrchr(pathdup, '/'); - char *newfilename = ptr+1; - char *prefix = new char[ptr-pathdup+1]; - for(int i=0;i\0 + char *newDirname = lastSlash+1; //\0, get from + char *ParentPath = pathdup;//pathdup are separated by pathdup, so it take only + + u_int64_t parent_inode_number = namei(ParentPath); if (parent_inode_number == (u_int64_t)-1) { - printf("fischl_mkdir failed because namei failed to find inode for %s\n", prefix); + printf("fischl_mkdir failed because namei failed to find inode for %s\n", ParentPath); delete pathdup; - delete [] prefix; return -1; } - u_int64_t ret = mkfile(parent_inode_number, newfilename, 1); + //make new inode + u_int64_t ret = mkfile(parent_inode_number, newDirname, 1);/* mode|S_IFDIR if need to call with dir*/ delete pathdup; - delete [] prefix; return ret; + //create with struct inode *__fishcl_new_inode(, that is change mkfile to fishcl_new_inode + + //after new_inode(mkfile), go to fischl_add_entry record + + } u_int64_t FilesOperation::fischl_mknod(const char* path, mode_t mode) { + //check path char *pathdup = strdup(path); - char *ptr = strrchr(pathdup, '/'); - char *newfilename = ptr+1; - char *prefix = new char[ptr-pathdup+1]; - for(int i=0;i\0 + char *newFilename = lastSlash+1; //\0, get from + char *ParentPath = pathdup;//pathdup are separated by pathdup, so it take only + + u_int64_t parent_inode_number = namei(ParentPath); if (parent_inode_number == (u_int64_t)-1) { - printf("fischl_mknod failed because namei failed to find inode for %s", prefix); + printf("fischl_mkdir failed because namei failed to find inode for %s\n", ParentPath); delete pathdup; - delete [] prefix; return -1; } - u_int64_t ret = mkfile(parent_inode_number, newfilename, 0); + //make new inode + u_int64_t ret = mkfile(parent_inode_number, newFilename, 0); delete pathdup; - delete [] prefix; return ret; } \ No newline at end of file