commit wza changes before merge

This commit is contained in:
Ziao 2023-11-21 14:22:18 -08:00
parent 051a04ea75
commit 1c22210209
4 changed files with 49 additions and 14 deletions

View File

@ -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;

View File

@ -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);

View File

@ -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
)

View File

@ -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);