diff --git a/include/direntry.h b/include/direntry.h index ff4792d..859668b 100644 --- a/include/direntry.h +++ b/include/direntry.h @@ -5,20 +5,6 @@ typedef struct fileNode { char *Symbolink; struct treeNode *subdirectory; struct fileNode *next; - 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, 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(name, buffer+8); - } } FileNode; typedef struct { @@ -35,6 +21,7 @@ typedef struct treeNode { /*root directory have its own initialization, so parent wont be NULL*/ int fischl_add_entry(TreeNode *parent, int new_inode_number, const char *fileName, INode *new_inode); +int fischl_rm_entry(TreeNode *parent, const char *fileName); /*if want to use dir mode use the subdirectory treeNode pointer */ //e.g. FileNode *Dirnode = fischl_find_entry(); can see file inside with Dirnode->subdirectory //e.g. go to the current Dirnode parent directory, use TreeNode *get_Dir_parent = Dirnode->subdirectory->parent; diff --git a/lib/direntry.cpp b/lib/direntry.cpp index 12eb1c9..738de41 100644 --- a/lib/direntry.cpp +++ b/lib/direntry.cpp @@ -43,6 +43,29 @@ FileNode *lookupHash(HashTable *h, char *key) { return NULL; // Not found } +bool removeHash(HashTable *h, char *key) { + unsigned int hashval = hash(h, key); + FileNode *node = h->table[hashval]; + if (node == NULL) return false; + if (strcmp(node->name, key) == 0) { + h->table[hashval] = node->next; + return true; + } + FileNode *prev = NULL; + bool foundit = false; + while (node != NULL) { + if (strcmp(node->name, key) == 0) break; + prev = node; + node = node->next; + } + if (node == NULL) { + return false; + } else { + prev->next = node->next; + return true; + } +} + TreeNode *createDirectory(const char *dirName, TreeNode *parent, int hashSize) { TreeNode *newDir = (TreeNode *)malloc(sizeof(TreeNode)); newDir->dirName = strdup(dirName); @@ -148,6 +171,20 @@ int fischl_add_entry(TreeNode *parent, int new_inode_number, const char *fileNam return 0; } +int fischl_rm_entry(TreeNode *parent, const char *fileName) { + char *fileName_dup = strdup(fileName); + if (parent->contents == NULL) return -1; + FileNode *file = NULL; + file = lookupHash(parent->contents, fileName_dup); + if (file == NULL) return -1; + if (file->subdirectory != NULL) freeTree(file->subdirectory); + removeHash(parent->contents, fileName_dup); + free(file->name); + free(file); + delete fileName_dup; +} + + FileNode *fischl_find_entry(TreeNode *root, const char *path){ //support . and .. function char *pathCopy = strdup(path); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 82ccfd4..77b3786 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -14,6 +14,7 @@ add_executable(${TARGET_LAYER1_API} layer1_API.cpp ) add_executable(${TARGET_LAYER2_API} + ../lib/direntry.cpp ../lib/files.cpp layer2_API.cpp ) diff --git a/test/dir_API.cpp b/test/dir_API.cpp index fb93b31..5d56cb6 100644 --- a/test/dir_API.cpp +++ b/test/dir_API.cpp @@ -84,6 +84,16 @@ int main() { }else{ printf(" %s under %s\n",get_file3->name,get_dir1_tree->dirName); } + // pressure test + INode inode_fileN; + for(int i=6;i<100;i++) { + fischl_add_entry(get_dir1_tree, i, (std::string("file")+std::to_string(i)).c_str(), &inode_fileN); + } + for(int i=6;i<100;i++){ + FileNode *get_fileN = fischl_find_entry(get_dir1_tree, (std::string("file")+std::to_string(i)).c_str()); + assert(get_fileN != NULL); + assert(get_fileN->inode_number == i); + } // Cleanup freeTree(root);