before big change

This commit is contained in:
FactorialN 2023-12-02 22:14:02 -08:00
parent f816ea919c
commit 87299ece0a
5 changed files with 76 additions and 24 deletions

View File

@ -32,6 +32,7 @@ typedef struct RenameInfo {
TreeNode *fischl_init_entry(int new_inode_number, const char *fileName, INode_Data *new_inode); 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 */ /*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*/ /*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);
int fischl_add_entry(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); 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 */
@ -42,4 +43,23 @@ FileNode *fischl_find_entry(TreeNode *root, const char *path);
void freeTree(TreeNode *node); void freeTree(TreeNode *node);
/*for debug use*/ /*for debug use*/
TreeNode *createDirectory(const char *dirName, TreeNode *parent, int hashSize); TreeNode *createDirectory(const char *dirName, TreeNode *parent, int hashSize);
TreeNode *find_parentPath(TreeNode *root, const char *path); TreeNode *find_parentPath(TreeNode *root, const char *path);
struct DirectoryEntry {
u_int64_t inode_number;
char file_name[256];
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, file_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(file_name, buffer+8);
}
};

View File

@ -28,6 +28,7 @@ class FilesOperation {
int fischl_readdir(const char *, void *, fuse_fill_dir_t, off_t, struct fuse_file_info *, enum fuse_readdir_flags); int fischl_readdir(const char *, void *, fuse_fill_dir_t, off_t, struct fuse_file_info *, enum fuse_readdir_flags);
int fischl_releasedir(const char* path, struct fuse_file_info *fi); int fischl_releasedir(const char* path, struct fuse_file_info *fi);
int fischl_unlink (const char *); int fischl_unlink (const char *);
int fischl_opendir(const char* path, struct fuse_file_info* fi);
int fischl_rmdir(const char *); int fischl_rmdir(const char *);
int fischl_readlink(const char* path, char* buf, size_t size); int fischl_readlink(const char* path, char* buf, size_t size);
int fischl_symlink(const char* from, const char* to); int fischl_symlink(const char* from, const char* to);

View File

@ -156,6 +156,28 @@ TreeNode *fischl_init_entry(int new_inode_number, const char *fileName, INode_Da
return newDir; return newDir;
} }
int fischl_add_entry_for_cache(TreeNode *parent, int new_inode_number, const char *fileName, INode_Data *new_inode, FileNode *file){
char *Name = strdup(fileName);
TreeNode *newDir = NULL;
/*If directory, malloc TreeNode, and then create filenode that belongs to Parent hash table content*/
if ((new_inode->metadata.permissions & S_IFMT) == S_IFDIR) {
newDir = (TreeNode *)malloc(sizeof(TreeNode));
newDir->dirName = Name;
newDir->contents = createHashTable(20);//hasSize define 20
newDir->parent = parent;
}
FileNode *newFile = insertHash(parent->contents, Name, newDir); //newDir == NULL indicates it's a file
//assign INode *new_inode metadata to data member in FileNode structure
newFile->permissions = new_inode->metadata.permissions;
newFile->inode_number = new_inode_number;
//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;
}
int fischl_add_entry(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){
char *Name = strdup(fileName); char *Name = strdup(fileName);
TreeNode *newDir = NULL; TreeNode *newDir = NULL;
@ -214,6 +236,26 @@ FileNode *fischl_find_entry(TreeNode *root, const char *path){
} }
else{ else{
file = lookupHash(current->contents, segment); file = lookupHash(current->contents, segment);
if (file == NULL) {
// find on disk whether this exists
INode_Data inode;
inode.inode_num = current->self_info->inode_number;
fs->inode_manager->load_inode(&inode);
char buffer[IO_BLOCK_SIZE] = {0};
for (u_int64_t idx=0; idx<inode.metadata.size/IO_BLOCK_SIZE; idx++) {
fs->read(&inode, buffer, IO_BLOCK_SIZE, idx*IO_BLOCK_SIZE);
DirectoryEntry ent;
for(int i=0;i<=IO_BLOCK_SIZE-264;i+=264){
ent.deserialize(buffer+i);
if (ent.inode_number && strcmp(ent.file_name, segment)) {
if(fischl_add_entry_for_cache(current, ent.inode_number, ent.file_name, &inode, file)<0){
return NULL;
}
break;
}
}
}
}
if (file != NULL && file->subdirectory == NULL) { if (file != NULL && file->subdirectory == NULL) {
free(pathCopy); free(pathCopy);
return file; //File found return file; //File found

View File

@ -6,25 +6,6 @@
#include <sstream> #include <sstream>
#include <cassert> #include <cassert>
struct DirectoryEntry {
u_int64_t inode_number;
char file_name[256];
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, file_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(file_name, buffer+8);
}
};
void FilesOperation::printbuffer(const char* buff, int len) { void FilesOperation::printbuffer(const char* buff, int len) {
for(int i=0;i<len;i++){ for(int i=0;i<len;i++){
printf("%x ",buff[i]); printf("%x ",buff[i]);
@ -401,6 +382,15 @@ void FilesOperation::unlink_inode(u_int64_t inode_number) {
fs->inode_manager->free_inode(&inode); fs->inode_manager->free_inode(&inode);
} }
int FilesOperation::fischl_opendir(const char* path, struct fuse_file_info* fi) {
u_int64_t fh = namei(path);
if (fh < 0){
return -1;
}
fi->fh = fh;
return 0;
}
int FilesOperation::fischl_rmdir(const char* path) { int FilesOperation::fischl_rmdir(const char* path) {
char *pathdup = strdup(path); char *pathdup = strdup(path);
char *lastSlash = strrchr(pathdup, '/'); char *lastSlash = strrchr(pathdup, '/');

View File

@ -61,9 +61,7 @@ static int fischl_readlink(const char* path, char* buf, size_t size) {
} }
static int fischl_opendir(const char* path, struct fuse_file_info* fi) { static int fischl_opendir(const char* path, struct fuse_file_info* fi) {
u_int64_t fh = options.fsop->namei(path); return options.fsop->fischl_opendir(path, fi);
fi->fh = fh;
return 0;
} }
static int fischl_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t ft, struct fuse_file_info *fi, enum fuse_readdir_flags flg) { static int fischl_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t ft, struct fuse_file_info *fi, enum fuse_readdir_flags flg) {
@ -149,7 +147,7 @@ static const struct fuse_operations fischl_oper = {
.unlink = fischl_unlink, .unlink = fischl_unlink,
.rmdir = fischl_rmdir, .rmdir = fischl_rmdir,
.symlink = fischl_symlink, .symlink = fischl_symlink,
.rename = fischl_rename, //.rename = fischl_rename,
.link = fischl_link, .link = fischl_link,
.chmod = fischl_chmod, .chmod = fischl_chmod,
.chown = fischl_chown, .chown = fischl_chown,
@ -201,6 +199,7 @@ int fischl(int argc, char *argv[])
//setupTestDirectory(&options.root); //setupTestDirectory(&options.root);
options.H = new FakeRawDisk(23552); options.H = new FakeRawDisk(23552);
//options.H = new RealRawDisk("/dev/vdb");
options.fs = new Fs(options.H); options.fs = new Fs(options.H);
options.fs->format(); options.fs->format();
options.fsop = new FilesOperation(*options.H, options.fs); options.fsop = new FilesOperation(*options.H, options.fs);