merge changes from origin

This commit is contained in:
Ziao 2023-11-21 14:24:52 -08:00
commit c49c1d2302
4 changed files with 152 additions and 89 deletions

View File

@ -19,7 +19,10 @@ typedef struct treeNode {
FileNode *self_info; //self fileNode infromation FileNode *self_info; //self fileNode infromation
} TreeNode; } TreeNode;
/*root directory have its own initialization, so parent wont be NULL*/ /*for root*/
TreeNode *fischl_init_entry(int new_inode_number, const char *fileName, INode *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(TreeNode *parent, int new_inode_number, const char *fileName, INode *new_inode); 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); int fischl_rm_entry(TreeNode *parent, const char *fileName);
/*if want to use dir mode use the subdirectory treeNode pointer */ /*if want to use dir mode use the subdirectory treeNode pointer */

View File

@ -87,7 +87,7 @@ TreeNode *find_parentPath(TreeNode *root, const char *path) {
file = lookupHash(current->contents, segment); file = lookupHash(current->contents, segment);
if (file != NULL && file->subdirectory == NULL) { if (file != NULL && file->subdirectory == NULL) {
free(pathCopy); free(pathCopy);
printf("status current directory %s\n",current->dirName); //printf("status current directory %s\n",current->dirName);
return current; //File found return current; //File found
} }
current = file ? file->subdirectory : NULL; current = file ? file->subdirectory : NULL;
@ -130,7 +130,7 @@ void freeTree(TreeNode *node) {
freeTree(temp->subdirectory); freeTree(temp->subdirectory);
} }
// Free the FileNode if it's not a directory // Free the FileNode if it's not a directory
printf("free who %s\n",temp->name); // printf("free who %s\n",temp->name);
free(temp->name); free(temp->name);
free(temp); free(temp);
} }
@ -150,6 +150,20 @@ void freeTree(TreeNode *node) {
/*********************************Direntry operation****************************************** /*********************************Direntry operation******************************************
********************************************************************************************/ ********************************************************************************************/
//for fake root (mount point)
TreeNode *fischl_init_entry(int new_inode_number, const char *fileName, INode *new_inode) {
TreeNode *newDir = (TreeNode *)malloc(sizeof(TreeNode));
newDir->dirName = strdup(fileName);
newDir->contents = createHashTable(20);//hashSize define 20
newDir->parent = newDir;
FileNode *newFile = (FileNode *)malloc(sizeof(FileNode));
newFile->name = strdup(fileName);
newFile->inode_number = new_inode_number;
newFile->permissions = new_inode->permissions;
newDir->self_info = newFile;
return newDir;
}
int fischl_add_entry(TreeNode *parent, int new_inode_number, const char *fileName, INode *new_inode){ int fischl_add_entry(TreeNode *parent, int new_inode_number, const char *fileName, INode *new_inode){
char *Name = strdup(fileName); char *Name = strdup(fileName);
TreeNode *newDir = NULL; TreeNode *newDir = NULL;
@ -193,17 +207,34 @@ FileNode *fischl_find_entry(TreeNode *root, const char *path){
FileNode *file = NULL; FileNode *file = NULL;
while (segment != NULL && current != NULL) { while (segment != NULL && current != NULL) {
file = lookupHash(current->contents, segment); if (strcmp(segment, "..") == 0) {
if (file != NULL && file->subdirectory == NULL) { // Move up to the parent directory
free(pathCopy); current = current->parent;
return file; //File found if (current == NULL) {
//return current; return filenode // If there's no parent, we've reached the top of the tree, but root itself is same
break;
}
} else if (strcmp(segment, ".") == 0) {
// Stay in the current directory (no action needed)
}
else{
file = lookupHash(current->contents, segment);
if (file != NULL && file->subdirectory == NULL) {
free(pathCopy);
return file; //File found
//return current; return filenode
}
current = file ? file->subdirectory : NULL;
} }
current = file ? file->subdirectory : NULL;
segment = strtok(NULL, "/"); segment = strtok(NULL, "/");
} }
free(pathCopy); free(pathCopy);
if (current != NULL && file == NULL) {
// If we've stopped at a directory and not a file, return the directory's self info
return current->self_info;
}
return file; // NULL if not found return file; // NULL if not found
//return current; return filenode
} }

View File

@ -26,6 +26,7 @@ add_executable(${TARGET_DIR_API}
# Link Google Test to your test executables # Link Google Test to your test executables
target_link_libraries(${TARGET_LAYER0} gtest gtest_main) target_link_libraries(${TARGET_LAYER0} gtest gtest_main)
target_link_libraries(${TARGET_LAYER1_API} gtest gtest_main) target_link_libraries(${TARGET_LAYER1_API} gtest gtest_main)
target_link_libraries(${TARGET_DIR_API} gtest gtest_main)
# add test to activate ctest -VV # add test to activate ctest -VV
add_test(NAME ${TARGET_LAYER0} COMMAND sudo ./${TARGET_LAYER0} ${DIR_PLACE}) add_test(NAME ${TARGET_LAYER0} COMMAND sudo ./${TARGET_LAYER0} ${DIR_PLACE})

View File

@ -8,94 +8,122 @@
#include <assert.h> #include <assert.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <gtest/gtest.h>
#include "fs.h" #include "fs.h"
#include "direntry.h" #include "direntry.h"
int main() { const char* d;
TreeNode *root = createDirectory("/", NULL, 20);
INode inode_file1; //global can be taken
fischl_add_entry(root, 2, "file1",&inode_file1); TreeNode *root;
//I will put this function in create_new_inode function, there will inode number(2) when inode_allocate const char* target_filepath;
INode inode_dir1;
//permission is necessary there to create treeNode or not const char* get_baseName(const char *filename){
u_int64_t ddd_permissions = 0; const char* base_name = strrchr(filename, '/');
inode_dir1.permissions = ddd_permissions | S_IFDIR; if (base_name != NULL) {
fischl_add_entry(root, 3, "dir1",&inode_dir1); base_name++; // Move past the '/' character
//find dir file (from root directory view, root contains dir1/ subdirectory) } else {
FileNode *get_dir1 = fischl_find_entry(root,"/dir1/"); base_name = filename; // No '/' found, use the original string
if(get_dir1 == NULL){
printf("No dir1 under %s\n",root->dirName);
return -1;
}else{
fprintf(stderr,"[%s ,%d]",__func__,__LINE__);
printf(" %s under %s\n",get_dir1->name,root->dirName);
} }
//add file2 under dir1 return base_name;
INode inode_file2; }
if(get_dir1->subdirectory != NULL){
//Treenode dir(you cannot find here), you only can get Filenode dir based on fischl_find_entry Function TEST(DirTest, root_test) {
//So use Filenode->subdirectory will point to the treenode dir, then can add files //Init fake root directory
fischl_add_entry(get_dir1->subdirectory, 4, "file2",&inode_file2); INode inode_root;
printf("add file2 in dir1\n"); u_int64_t file_permissions = 0;
} inode_root.permissions = file_permissions | S_IFDIR;
/**********************************************************/ root = fischl_init_entry(0, "/", &inode_root);//0 is inode number assigned by inode_allocate()
//This is for debugging, and demonstate to you }
TreeNode *get_dir1_tree = find_parentPath(root,"/dir1/file2"); TEST(DirTest, AddFile_test) {
if(get_dir1_tree == get_dir1->subdirectory){ //assume file and dir itself(content,metadata) same,but different name and inode number
fprintf(stderr,"[%s ,%d]",__func__,__LINE__); INode inode_file;
printf(" [Treenode]get_dir1_tree->dirName (%s) same [Filenode]get_dir1->name (%s)\n",get_dir1_tree->dirName,get_dir1->name); INode inode_dir;
}else{ u_int64_t file_permissions = 0;
printf("not same\n"); file_permissions = 0;
} inode_dir.permissions = file_permissions | S_IFDIR;
/**********************************************************/ fischl_add_entry(root, 2, "file1",&inode_file);
fischl_add_entry(root, 3, "dir1",&inode_dir);
}
TEST(DirTest, FindFile_test) {
//find file
target_filepath = "/file1";
FileNode *get_file = fischl_find_entry(root,target_filepath);
EXPECT_TRUE(get_file != NULL);
EXPECT_STREQ(get_file->name, get_baseName(target_filepath));
//find dir
target_filepath = "/dir1/";
FileNode *get_dir = fischl_find_entry(root,target_filepath);
EXPECT_TRUE(get_dir != NULL);//detect this should find success
EXPECT_STREQ(get_dir->name, "dir1");
ASSERT_TRUE(get_dir->subdirectory != NULL);//secure it is directory
//check . function
get_dir = fischl_find_entry(root,"./");
EXPECT_TRUE(get_dir != NULL);//detect this should find success
EXPECT_STREQ(get_dir->name, "/");
ASSERT_TRUE(get_dir->subdirectory != NULL);//secure it is directory
//check .. function
get_dir = fischl_find_entry(root,"..");
EXPECT_TRUE(get_dir != NULL);//detect this should find success
EXPECT_STREQ(get_dir->name, "/");
ASSERT_TRUE(get_dir->subdirectory != NULL);//secure it is directory
}
TEST(DirTest, Add_FindFile_test) {
//add file and dir under subdirectory instead of root
INode inode_file;
INode inode_dir;
u_int64_t file_permissions = 0;
file_permissions = 0;
inode_dir.permissions = file_permissions | S_IFDIR;
/*add with subdirectory*/
//Treenode dir(you cannot find here), you only can get Filenode dir based on fischl_find_entry Function
//So use Filenode->subdirectory will point to the treenode dir, then can add files
FileNode *get_dir = fischl_find_entry(root,"/dir1/");
fischl_add_entry(get_dir->subdirectory, 4, "file2",&inode_file);
//verfication treeNode and Filenode relationship
TreeNode *get_dir_tree = find_parentPath(root,"/dir1/file2");
ASSERT_TRUE(get_dir_tree == get_dir->subdirectory);//treeNode dir should be same as treeNode subdir in that Filenode
//two Ways to get File(include dir itself) information //two Ways to get File(include dir itself) information
FileNode *get_file2 =NULL; FileNode *get_file = NULL;
//1. absolute path, the root(treeNode) will always exist when initialize //1. absolute path, the root(treeNode) will always exist when initialize
get_file2 = fischl_find_entry(root,"/dir1/file2"); get_file = fischl_find_entry(root,"/dir1/file2");
if(get_file2 == NULL){ EXPECT_TRUE(get_file != NULL);
printf("No dir1 under dir1\n"); EXPECT_STREQ(get_file->name,"file2");
return -1;
}else{
fprintf(stderr,"[%s ,%d]",__func__,__LINE__);
printf(" %s under %sdir1/\n",get_file2->name,root->dirName);
}
//2. relative path, the get_dir1(FileNode)->subdirectory(treeNode), use treeNode(dir) to find //2. relative path, the get_dir1(FileNode)->subdirectory(treeNode), use treeNode(dir) to find
get_file2 = fischl_find_entry(get_dir1->subdirectory,"/file2"); get_file = fischl_find_entry(get_dir->subdirectory,"/file2");
if(get_file2 == NULL){ EXPECT_TRUE(get_file != NULL);
printf("No dir1 under %s\n",get_dir1->subdirectory->dirName); EXPECT_STREQ(get_file->name,"file2");
return -1;
}else{
fprintf(stderr,"[%s ,%d]",__func__,__LINE__);
printf(" %s under %s\n",get_file2->name,get_dir1->subdirectory->dirName);
}
/**********************************************************/ /**********************************************************/
//add one more file under dir1 //add one more file under dir1
INode inode_file3; fischl_add_entry(get_dir->subdirectory, 5, "file3",&inode_file);
if(get_dir1->subdirectory != NULL){ //find
fischl_add_entry(get_dir1_tree, 5, "file3",&inode_file3); get_file = fischl_find_entry(get_dir->subdirectory,"./file3");
printf("add file3 in dir1\n"); EXPECT_TRUE(get_file != NULL);
} EXPECT_STREQ(get_file->name,"file3");
FileNode *get_file3 =NULL; //use .. from dir1 to find file1
//fischl_find_entry(get_dir1->subdirectory,"/file3"); are equivalent get_file = fischl_find_entry(get_dir->subdirectory,"../file1");
get_file3 = fischl_find_entry(get_dir1_tree,"/file3"); EXPECT_TRUE(get_file != NULL);
if(get_file3 == NULL){ EXPECT_STREQ(get_file->name,"file1");
printf("No dir1 under %s\n",get_dir1_tree->dirName); //check dir1 with .
return -1; get_dir = fischl_find_entry(get_dir->subdirectory,".");
}else{ EXPECT_TRUE(get_dir != NULL);//detect this should find success
printf(" %s under %s\n",get_file3->name,get_dir1_tree->dirName); EXPECT_STREQ(get_dir->name, "dir1");
} ASSERT_TRUE(get_dir->subdirectory != NULL);//secure it is directory
// pressure test //check root with dir1
INode inode_fileN; get_dir = fischl_find_entry(get_dir->subdirectory,"..");
for(int i=6;i<100;i++) { EXPECT_TRUE(get_dir != NULL);//detect this should find success
fischl_add_entry(get_dir1_tree, i, (std::string("file")+std::to_string(i)).c_str(), &inode_fileN); EXPECT_STREQ(get_dir->name, "/");
} ASSERT_TRUE(get_dir->subdirectory != NULL);//secure it is directory
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); int main(int argc, char **argv) {
assert(get_fileN->inode_number == i); d = (argc < 2) ? "/dev/vdc" : argv[1];//how to do with this?
} ::testing::InitGoogleTest(&argc, argv);
int result = RUN_ALL_TESTS();
// Cleanup // Cleanup
freeTree(root); freeTree(root);
return result;
return 0;
} }