|
|
@ -55,8 +55,7 @@ void FilesOperation::initialize(bool load) {
|
|
|
|
root_node = fischl_init_entry(1, "/", root_inode);
|
|
|
|
root_node = fischl_init_entry(1, "/", root_inode);
|
|
|
|
assert(root_node->self_info != NULL);
|
|
|
|
assert(root_node->self_info != NULL);
|
|
|
|
fs->load_superblock();
|
|
|
|
fs->load_superblock();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
else{
|
|
|
|
|
|
|
|
initialize_rootinode();
|
|
|
|
initialize_rootinode();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -117,7 +116,9 @@ INode_Data *FilesOperation::create_new_inode(u_int64_t parent_inode_number,
|
|
|
|
bool allocated = false;
|
|
|
|
bool allocated = false;
|
|
|
|
INode_Data *new_inode = new INode_Data();
|
|
|
|
INode_Data *new_inode = new INode_Data();
|
|
|
|
fs->inode_manager->new_inode(getuid(), getgid(), mode, new_inode);
|
|
|
|
fs->inode_manager->new_inode(getuid(), getgid(), mode, new_inode);
|
|
|
|
printf("NEW INODE %llu %llu %llu %o\n",new_inode->inode_num, new_inode->metadata.uid, new_inode->metadata.gid, (mode_t)new_inode->metadata.permissions);
|
|
|
|
printf("NEW INODE %llu %llu %llu %o\n", new_inode->inode_num,
|
|
|
|
|
|
|
|
new_inode->metadata.uid, new_inode->metadata.gid,
|
|
|
|
|
|
|
|
(mode_t)new_inode->metadata.permissions);
|
|
|
|
if ((mode & S_IFMT) == S_IFDIR) {
|
|
|
|
if ((mode & S_IFMT) == S_IFDIR) {
|
|
|
|
create_dot_dotdot(new_inode, parent_inode_number);
|
|
|
|
create_dot_dotdot(new_inode, parent_inode_number);
|
|
|
|
fs->inode_manager->save_inode(new_inode);
|
|
|
|
fs->inode_manager->save_inode(new_inode);
|
|
|
@ -212,14 +213,17 @@ u_int64_t FilesOperation::disk_namei(const char *path) {
|
|
|
|
|
|
|
|
|
|
|
|
u_int64_t FilesOperation::namei(const char *path) {
|
|
|
|
u_int64_t FilesOperation::namei(const char *path) {
|
|
|
|
FileNode *filenode = fischl_load_entry(root_node, path);
|
|
|
|
FileNode *filenode = fischl_load_entry(root_node, path);
|
|
|
|
if (filenode) return filenode->inode_number;
|
|
|
|
if (filenode)
|
|
|
|
else return -1;
|
|
|
|
return filenode->inode_number;
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool FilesOperation::permission_check(int mask, INode_Data *inode) {
|
|
|
|
bool FilesOperation::permission_check(int mask, INode_Data *inode) {
|
|
|
|
mode_t per = (mode_t)inode->metadata.permissions;
|
|
|
|
mode_t per = (mode_t)inode->metadata.permissions;
|
|
|
|
uid_t uid = (uid_t)inode->metadata.uid;
|
|
|
|
uid_t uid = (uid_t)inode->metadata.uid;
|
|
|
|
gid_t gid = (gid_t)inode->metadata.gid;
|
|
|
|
gid_t gid = (gid_t)inode->metadata.gid;
|
|
|
|
|
|
|
|
if (getuid() == 0) return true;
|
|
|
|
// printf("PERMISSION CHECK %d %llu %llu %o\n", mask, uid, gid, per);
|
|
|
|
// printf("PERMISSION CHECK %d %llu %llu %o\n", mask, uid, gid, per);
|
|
|
|
if (getuid() == uid) {
|
|
|
|
if (getuid() == uid) {
|
|
|
|
if ((mask & R_OK) && !(per & S_IRUSR)) {
|
|
|
|
if ((mask & R_OK) && !(per & S_IRUSR)) {
|
|
|
@ -234,8 +238,7 @@ bool FilesOperation::permission_check(int mask, INode_Data *inode) {
|
|
|
|
return false; // Permission denied for executing
|
|
|
|
return false; // Permission denied for executing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else if (getgid() == gid) {
|
|
|
|
else if(getgid() == gid){
|
|
|
|
|
|
|
|
if ((mask & R_OK) && !(per & S_IRGRP)) {
|
|
|
|
if ((mask & R_OK) && !(per & S_IRGRP)) {
|
|
|
|
return false; // Permission denied for reading
|
|
|
|
return false; // Permission denied for reading
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -248,8 +251,7 @@ bool FilesOperation::permission_check(int mask, INode_Data *inode) {
|
|
|
|
return false; // Permission denied for executing
|
|
|
|
return false; // Permission denied for executing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
else{
|
|
|
|
|
|
|
|
if ((mask & R_OK) && !(per & S_IROTH)) {
|
|
|
|
if ((mask & R_OK) && !(per & S_IROTH)) {
|
|
|
|
return false; // Permission denied for reading
|
|
|
|
return false; // Permission denied for reading
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -265,7 +267,8 @@ bool FilesOperation::permission_check(int mask, INode_Data *inode) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool FilesOperation::permission_check_by_inode_num(int mask, u_int64_t inode_num) {
|
|
|
|
bool FilesOperation::permission_check_by_inode_num(int mask,
|
|
|
|
|
|
|
|
u_int64_t inode_num) {
|
|
|
|
INode_Data inode;
|
|
|
|
INode_Data inode;
|
|
|
|
inode.inode_num = inode_num;
|
|
|
|
inode.inode_num = inode_num;
|
|
|
|
|
|
|
|
|
|
|
@ -302,9 +305,12 @@ int FilesOperation::fischl_mkdir(const char *path, mode_t mode) {
|
|
|
|
char *ParentPath = pathdup; // pathdup are separated by pathdup, so it take
|
|
|
|
char *ParentPath = pathdup; // pathdup are separated by pathdup, so it take
|
|
|
|
// <parent path> only
|
|
|
|
// <parent path> only
|
|
|
|
|
|
|
|
|
|
|
|
FileNode *parent_filenode = strlen(ParentPath)? fischl_load_entry(root_node, ParentPath): root_node->self_info;
|
|
|
|
FileNode *parent_filenode = strlen(ParentPath)
|
|
|
|
|
|
|
|
? fischl_load_entry(root_node, ParentPath)
|
|
|
|
|
|
|
|
: root_node->self_info;
|
|
|
|
if (parent_filenode == NULL) {
|
|
|
|
if (parent_filenode == NULL) {
|
|
|
|
fprintf(stderr,"[%s ,%d] ParentPath:{%s} not found\n",__func__,__LINE__, ParentPath);
|
|
|
|
fprintf(stderr, "[%s ,%d] ParentPath:{%s} not found\n", __func__, __LINE__,
|
|
|
|
|
|
|
|
ParentPath);
|
|
|
|
free(pathdup);
|
|
|
|
free(pathdup);
|
|
|
|
return -ENOENT; // parentpath directory does not exist
|
|
|
|
return -ENOENT; // parentpath directory does not exist
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -313,11 +319,15 @@ int FilesOperation::fischl_mkdir(const char *path, mode_t mode) {
|
|
|
|
return -EACCES;
|
|
|
|
return -EACCES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//printf("%s, %llu, %s\n", parent_filenode->name, parent_inode_number, newDirname);
|
|
|
|
// printf("%s, %llu, %s\n", parent_filenode->name, parent_inode_number,
|
|
|
|
//make new inode
|
|
|
|
// newDirname); make new inode
|
|
|
|
INode_Data* ret = create_new_inode(parent_inode_number, newDirname, mode|S_IFDIR);//specify S_IFDIR as directory
|
|
|
|
INode_Data *ret =
|
|
|
|
if (ret == NULL) return -1;//ENOSPC but create_new_inode handle ENAMETOOLONG EEXIST
|
|
|
|
create_new_inode(parent_inode_number, newDirname,
|
|
|
|
fischl_add_entry(parent_filenode->subdirectory, ret->inode_num, newDirname, ret);
|
|
|
|
mode | S_IFDIR); // specify S_IFDIR as directory
|
|
|
|
|
|
|
|
if (ret == NULL)
|
|
|
|
|
|
|
|
return -1; // ENOSPC but create_new_inode handle ENAMETOOLONG EEXIST
|
|
|
|
|
|
|
|
fischl_add_entry(parent_filenode->subdirectory, ret->inode_num, newDirname,
|
|
|
|
|
|
|
|
ret);
|
|
|
|
free(pathdup);
|
|
|
|
free(pathdup);
|
|
|
|
return 0; // SUCCESS
|
|
|
|
return 0; // SUCCESS
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -328,13 +338,20 @@ int FilesOperation::fischl_mknod(const char* path, mode_t mode, dev_t dev) {
|
|
|
|
// check path
|
|
|
|
// check path
|
|
|
|
char *pathdup = strdup(path);
|
|
|
|
char *pathdup = strdup(path);
|
|
|
|
char *lastSlash = strrchr(pathdup, '/');
|
|
|
|
char *lastSlash = strrchr(pathdup, '/');
|
|
|
|
*lastSlash = '\0'; // Split the string into parent path and new directory name; <parent path>\0<direcotry name>
|
|
|
|
*lastSlash = '\0'; // Split the string into parent path and new directory
|
|
|
|
char *newFilename = lastSlash+1; //\0<direcotry name>, get from <direcotry name>
|
|
|
|
// name; <parent path>\0<direcotry name>
|
|
|
|
char *ParentPath = pathdup;//pathdup are separated by pathdup, so it take <parent path> only
|
|
|
|
char *newFilename =
|
|
|
|
// fprintf(stderr,"[%s ,%d] ParentPath:%s, strlen=%d\n",__func__,__LINE__, ParentPath, strlen(ParentPath));
|
|
|
|
lastSlash + 1; //\0<direcotry name>, get from <direcotry name>
|
|
|
|
FileNode *parent_filenode = strlen(ParentPath)? fischl_load_entry(root_node, ParentPath): root_node->self_info;
|
|
|
|
char *ParentPath = pathdup; // pathdup are separated by pathdup, so it take
|
|
|
|
|
|
|
|
// <parent path> only
|
|
|
|
|
|
|
|
// fprintf(stderr,"[%s ,%d] ParentPath:%s, strlen=%d\n",__func__,__LINE__,
|
|
|
|
|
|
|
|
// ParentPath, strlen(ParentPath));
|
|
|
|
|
|
|
|
FileNode *parent_filenode = strlen(ParentPath)
|
|
|
|
|
|
|
|
? fischl_load_entry(root_node, ParentPath)
|
|
|
|
|
|
|
|
: root_node->self_info;
|
|
|
|
if (parent_filenode == NULL) {
|
|
|
|
if (parent_filenode == NULL) {
|
|
|
|
fprintf(stderr,"[%s ,%d] ParentPath:{%s} not found\n",__func__,__LINE__, ParentPath);
|
|
|
|
fprintf(stderr, "[%s ,%d] ParentPath:{%s} not found\n", __func__, __LINE__,
|
|
|
|
|
|
|
|
ParentPath);
|
|
|
|
free(pathdup);
|
|
|
|
free(pathdup);
|
|
|
|
return -1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -344,26 +361,36 @@ int FilesOperation::fischl_mknod(const char* path, mode_t mode, dev_t dev) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// make new inode
|
|
|
|
// make new inode
|
|
|
|
INode_Data *ret = create_new_inode(parent_inode_number, newFilename, mode);
|
|
|
|
INode_Data *ret = create_new_inode(parent_inode_number, newFilename, mode);
|
|
|
|
if (ret == NULL) return -1;//ENOSPC but create_new_inode handle ENAMETOOLONG EEXIST
|
|
|
|
if (ret == NULL)
|
|
|
|
|
|
|
|
return -1; // ENOSPC but create_new_inode handle ENAMETOOLONG EEXIST
|
|
|
|
// make new node
|
|
|
|
// make new node
|
|
|
|
fischl_add_entry(parent_filenode->subdirectory, ret->inode_num, newFilename, ret);
|
|
|
|
fischl_add_entry(parent_filenode->subdirectory, ret->inode_num, newFilename,
|
|
|
|
|
|
|
|
ret);
|
|
|
|
free(pathdup);
|
|
|
|
free(pathdup);
|
|
|
|
return 0; // SUCESS
|
|
|
|
return 0; // SUCESS
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
regular file
|
|
|
|
regular file
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
int FilesOperation::fischl_create(const char* path, mode_t mode, struct fuse_file_info* fi) {
|
|
|
|
int FilesOperation::fischl_create(const char *path, mode_t mode,
|
|
|
|
|
|
|
|
struct fuse_file_info *fi) {
|
|
|
|
// check path
|
|
|
|
// check path
|
|
|
|
char *pathdup = strdup(path);
|
|
|
|
char *pathdup = strdup(path);
|
|
|
|
char *lastSlash = strrchr(pathdup, '/');
|
|
|
|
char *lastSlash = strrchr(pathdup, '/');
|
|
|
|
*lastSlash = '\0'; // Split the string into parent path and new directory name; <parent path>\0<direcotry name>
|
|
|
|
*lastSlash = '\0'; // Split the string into parent path and new directory
|
|
|
|
char *newFilename = lastSlash+1; //\0<direcotry name>, get from <direcotry name>
|
|
|
|
// name; <parent path>\0<direcotry name>
|
|
|
|
char *ParentPath = pathdup;//pathdup are separated by pathdup, so it take <parent path> only
|
|
|
|
char *newFilename =
|
|
|
|
// fprintf(stderr,"[%s ,%d] ParentPath:%s, strlen=%d\n",__func__,__LINE__, ParentPath, strlen(ParentPath));
|
|
|
|
lastSlash + 1; //\0<direcotry name>, get from <direcotry name>
|
|
|
|
FileNode *parent_filenode = strlen(ParentPath)? fischl_load_entry(root_node, ParentPath): root_node->self_info;
|
|
|
|
char *ParentPath = pathdup; // pathdup are separated by pathdup, so it take
|
|
|
|
|
|
|
|
// <parent path> only
|
|
|
|
|
|
|
|
// fprintf(stderr,"[%s ,%d] ParentPath:%s, strlen=%d\n",__func__,__LINE__,
|
|
|
|
|
|
|
|
// ParentPath, strlen(ParentPath));
|
|
|
|
|
|
|
|
FileNode *parent_filenode = strlen(ParentPath)
|
|
|
|
|
|
|
|
? fischl_load_entry(root_node, ParentPath)
|
|
|
|
|
|
|
|
: root_node->self_info;
|
|
|
|
if (parent_filenode == NULL) {
|
|
|
|
if (parent_filenode == NULL) {
|
|
|
|
fprintf(stderr,"[%s ,%d] ParentPath:{%s} not found\n",__func__,__LINE__, ParentPath);
|
|
|
|
fprintf(stderr, "[%s ,%d] ParentPath:{%s} not found\n", __func__, __LINE__,
|
|
|
|
|
|
|
|
ParentPath);
|
|
|
|
free(pathdup);
|
|
|
|
free(pathdup);
|
|
|
|
return -1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -373,11 +400,14 @@ int FilesOperation::fischl_create(const char* path, mode_t mode, struct fuse_fil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// make new inode
|
|
|
|
// make new inode
|
|
|
|
INode_Data *ret = create_new_inode(parent_inode_number, newFilename, mode);
|
|
|
|
INode_Data *ret = create_new_inode(parent_inode_number, newFilename, mode);
|
|
|
|
if (ret == NULL) return -1;//ENOSPC but create_new_inode handle ENAMETOOLONG EEXIST
|
|
|
|
if (ret == NULL)
|
|
|
|
|
|
|
|
return -1; // ENOSPC but create_new_inode handle ENAMETOOLONG EEXIST
|
|
|
|
// make new node in RAM
|
|
|
|
// make new node in RAM
|
|
|
|
fischl_add_entry(parent_filenode->subdirectory, ret->inode_num, newFilename, ret);
|
|
|
|
fischl_add_entry(parent_filenode->subdirectory, ret->inode_num, newFilename,
|
|
|
|
|
|
|
|
ret);
|
|
|
|
// directly give inode number rather than create file descriptor table
|
|
|
|
// directly give inode number rather than create file descriptor table
|
|
|
|
fi->fh = ret->inode_num;//assign file descriptor as inode number to fuse system
|
|
|
|
fi->fh =
|
|
|
|
|
|
|
|
ret->inode_num; // assign file descriptor as inode number to fuse system
|
|
|
|
free(pathdup);
|
|
|
|
free(pathdup);
|
|
|
|
return 0; // SUCESS
|
|
|
|
return 0; // SUCESS
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -406,7 +436,8 @@ int FilesOperation::fischl_getattr(const char *path, struct stat *stbuf,
|
|
|
|
stbuf->st_uid = inode.metadata.uid;
|
|
|
|
stbuf->st_uid = inode.metadata.uid;
|
|
|
|
stbuf->st_gid = inode.metadata.gid;
|
|
|
|
stbuf->st_gid = inode.metadata.gid;
|
|
|
|
stbuf->st_atime = (time_t)(inode.metadata.access_time / 1000000000ULL);
|
|
|
|
stbuf->st_atime = (time_t)(inode.metadata.access_time / 1000000000ULL);
|
|
|
|
stbuf->st_mtime = (time_t)(inode.metadata.modification_time / 1000000000ULL);
|
|
|
|
stbuf->st_mtime =
|
|
|
|
|
|
|
|
(time_t)(inode.metadata.modification_time / 1000000000ULL);
|
|
|
|
stbuf->st_size = IO_BLOCK_SIZE;
|
|
|
|
stbuf->st_size = IO_BLOCK_SIZE;
|
|
|
|
stbuf->st_ino = inode.inode_num;
|
|
|
|
stbuf->st_ino = inode.inode_num;
|
|
|
|
} else if (S_ISLNK(inode.metadata.permissions)) {
|
|
|
|
} else if (S_ISLNK(inode.metadata.permissions)) {
|
|
|
@ -415,7 +446,8 @@ int FilesOperation::fischl_getattr(const char *path, struct stat *stbuf,
|
|
|
|
stbuf->st_uid = inode.metadata.uid;
|
|
|
|
stbuf->st_uid = inode.metadata.uid;
|
|
|
|
stbuf->st_gid = inode.metadata.gid;
|
|
|
|
stbuf->st_gid = inode.metadata.gid;
|
|
|
|
stbuf->st_atime = (time_t)(inode.metadata.access_time / 1000000000ULL);
|
|
|
|
stbuf->st_atime = (time_t)(inode.metadata.access_time / 1000000000ULL);
|
|
|
|
stbuf->st_mtime = (time_t)(inode.metadata.modification_time / 1000000000ULL);
|
|
|
|
stbuf->st_mtime =
|
|
|
|
|
|
|
|
(time_t)(inode.metadata.modification_time / 1000000000ULL);
|
|
|
|
stbuf->st_size = inode.metadata.size;
|
|
|
|
stbuf->st_size = inode.metadata.size;
|
|
|
|
stbuf->st_ino = inode.inode_num;
|
|
|
|
stbuf->st_ino = inode.inode_num;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
@ -423,9 +455,12 @@ int FilesOperation::fischl_getattr(const char *path, struct stat *stbuf,
|
|
|
|
stbuf->st_nlink = inode.metadata.reference_count;
|
|
|
|
stbuf->st_nlink = inode.metadata.reference_count;
|
|
|
|
stbuf->st_uid = inode.metadata.uid;
|
|
|
|
stbuf->st_uid = inode.metadata.uid;
|
|
|
|
stbuf->st_gid = inode.metadata.gid;
|
|
|
|
stbuf->st_gid = inode.metadata.gid;
|
|
|
|
//printf("GETATTR %llu %llu %llu %o\n", inode.inode_num, inode.metadata.uid, inode.metadata.gid, (mode_t)inode.metadata.permissions);
|
|
|
|
// printf("GETATTR %llu %llu %llu %o\n", inode.inode_num,
|
|
|
|
|
|
|
|
// inode.metadata.uid, inode.metadata.gid,
|
|
|
|
|
|
|
|
// (mode_t)inode.metadata.permissions);
|
|
|
|
stbuf->st_atime = (time_t)(inode.metadata.access_time / 1000000000ULL);
|
|
|
|
stbuf->st_atime = (time_t)(inode.metadata.access_time / 1000000000ULL);
|
|
|
|
stbuf->st_mtime = (time_t)(inode.metadata.modification_time / 1000000000ULL);
|
|
|
|
stbuf->st_mtime =
|
|
|
|
|
|
|
|
(time_t)(inode.metadata.modification_time / 1000000000ULL);
|
|
|
|
stbuf->st_size = inode.metadata.size;
|
|
|
|
stbuf->st_size = inode.metadata.size;
|
|
|
|
stbuf->st_ino = inode.inode_num;
|
|
|
|
stbuf->st_ino = inode.inode_num;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -466,7 +501,8 @@ int FilesOperation::fischl_readdir(const char *path, void *buf,
|
|
|
|
return 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int FilesOperation::fischl_releasedir(const char *path, struct fuse_file_info *fi){
|
|
|
|
int FilesOperation::fischl_releasedir(const char *path,
|
|
|
|
|
|
|
|
struct fuse_file_info *fi) {
|
|
|
|
if (fischl_load_entry(root_node, path) == NULL)
|
|
|
|
if (fischl_load_entry(root_node, path) == NULL)
|
|
|
|
return -ENOENT;
|
|
|
|
return -ENOENT;
|
|
|
|
// do with file descriptor that cannot be used
|
|
|
|
// do with file descriptor that cannot be used
|
|
|
@ -478,7 +514,8 @@ void FilesOperation::unlink_inode(u_int64_t inode_number) {
|
|
|
|
INode_Data inode;
|
|
|
|
INode_Data inode;
|
|
|
|
inode.inode_num = inode_number;
|
|
|
|
inode.inode_num = inode_number;
|
|
|
|
fs->inode_manager->load_inode(&inode);
|
|
|
|
fs->inode_manager->load_inode(&inode);
|
|
|
|
if (inode.metadata.reference_count > 1 && (inode.metadata.permissions & S_IFMT) != S_IFDIR){
|
|
|
|
if (inode.metadata.reference_count > 1 &&
|
|
|
|
|
|
|
|
(inode.metadata.permissions & S_IFMT) != S_IFDIR) {
|
|
|
|
inode.metadata.reference_count -= 1;
|
|
|
|
inode.metadata.reference_count -= 1;
|
|
|
|
fs->inode_manager->save_inode(&inode);
|
|
|
|
fs->inode_manager->save_inode(&inode);
|
|
|
|
return;
|
|
|
|
return;
|
|
|
@ -501,7 +538,8 @@ 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) {
|
|
|
|
int FilesOperation::fischl_opendir(const char *path,
|
|
|
|
|
|
|
|
struct fuse_file_info *fi) {
|
|
|
|
|
|
|
|
|
|
|
|
u_int64_t fh = namei(path);
|
|
|
|
u_int64_t fh = namei(path);
|
|
|
|
|
|
|
|
|
|
|
@ -547,7 +585,8 @@ int FilesOperation::fischl_rmdir(const char* path) {
|
|
|
|
return -EACCES;
|
|
|
|
return -EACCES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
char rw_buffer[IO_BLOCK_SIZE] = {0};
|
|
|
|
char rw_buffer[IO_BLOCK_SIZE] = {0};
|
|
|
|
for (u_int64_t idx=0; idx<parent_INode.metadata.size/IO_BLOCK_SIZE; idx++) {
|
|
|
|
for (u_int64_t idx = 0; idx < parent_INode.metadata.size / IO_BLOCK_SIZE;
|
|
|
|
|
|
|
|
idx++) {
|
|
|
|
fs->read(&parent_INode, rw_buffer, IO_BLOCK_SIZE, idx * IO_BLOCK_SIZE);
|
|
|
|
fs->read(&parent_INode, rw_buffer, IO_BLOCK_SIZE, idx * IO_BLOCK_SIZE);
|
|
|
|
DirectoryEntry ent;
|
|
|
|
DirectoryEntry ent;
|
|
|
|
for (int i = 0; i <= IO_BLOCK_SIZE - 264; i += 264) {
|
|
|
|
for (int i = 0; i <= IO_BLOCK_SIZE - 264; i += 264) {
|
|
|
@ -621,7 +660,6 @@ int FilesOperation::fischl_chown(const char *path, uid_t uid, gid_t gid,
|
|
|
|
return 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int FilesOperation::fischl_unlink(const char *path) {
|
|
|
|
int FilesOperation::fischl_unlink(const char *path) {
|
|
|
|
|
|
|
|
|
|
|
|
char *pathdup = strdup(path);
|
|
|
|
char *pathdup = strdup(path);
|
|
|
@ -650,7 +688,8 @@ int FilesOperation::fischl_unlink(const char* path) {
|
|
|
|
return -EACCES;
|
|
|
|
return -EACCES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
char rw_buffer[IO_BLOCK_SIZE] = {0};
|
|
|
|
char rw_buffer[IO_BLOCK_SIZE] = {0};
|
|
|
|
for (u_int64_t idx=0; idx<parent_INode.metadata.size/IO_BLOCK_SIZE; idx++) {
|
|
|
|
for (u_int64_t idx = 0; idx < parent_INode.metadata.size / IO_BLOCK_SIZE;
|
|
|
|
|
|
|
|
idx++) {
|
|
|
|
fs->read(&parent_INode, rw_buffer, IO_BLOCK_SIZE, idx * IO_BLOCK_SIZE);
|
|
|
|
fs->read(&parent_INode, rw_buffer, IO_BLOCK_SIZE, idx * IO_BLOCK_SIZE);
|
|
|
|
DirectoryEntry ent;
|
|
|
|
DirectoryEntry ent;
|
|
|
|
for (int i = 0; i <= IO_BLOCK_SIZE - 264; i += 264) {
|
|
|
|
for (int i = 0; i <= IO_BLOCK_SIZE - 264; i += 264) {
|
|
|
@ -687,8 +726,8 @@ int FilesOperation::fischl_unlink(const char* path) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int FilesOperation::fischl_open(const char *path, struct fuse_file_info *fi) {
|
|
|
|
int FilesOperation::fischl_open(const char *path, struct fuse_file_info *fi) {
|
|
|
|
/*Creation (O_CREAT, O_EXCL, O_NOCTTY) flags will be filtered out / handled by the kernel.
|
|
|
|
/*Creation (O_CREAT, O_EXCL, O_NOCTTY) flags will be filtered out / handled by
|
|
|
|
if no files will use create function
|
|
|
|
the kernel. if no files will use create function
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
FileNode *get_file;
|
|
|
|
FileNode *get_file;
|
|
|
|
if ((get_file = fischl_load_entry(root_node, path)) == NULL)
|
|
|
|
if ((get_file = fischl_load_entry(root_node, path)) == NULL)
|
|
|
@ -715,15 +754,16 @@ int FilesOperation::fischl_open(const char *path, struct fuse_file_info *fi){
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//if need to do with flag fi->flags ((fi->flags & O_ACCMODE)). Initial setting ALL access
|
|
|
|
// if need to do with flag fi->flags ((fi->flags & O_ACCMODE)). Initial
|
|
|
|
//create function will handle file descriptor fi->fh
|
|
|
|
// setting ALL access create function will handle file descriptor fi->fh
|
|
|
|
fi->fh = get_file->inode_number;
|
|
|
|
fi->fh = get_file->inode_number;
|
|
|
|
return 0; // SUCESS
|
|
|
|
return 0; // SUCESS
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int FilesOperation::fischl_release(const char *path, struct fuse_file_info *fi){
|
|
|
|
int FilesOperation::fischl_release(const char *path,
|
|
|
|
/*Creation (O_CREAT, O_EXCL, O_NOCTTY) flags will be filtered out / handled by the kernel.
|
|
|
|
struct fuse_file_info *fi) {
|
|
|
|
if no files will use create function
|
|
|
|
/*Creation (O_CREAT, O_EXCL, O_NOCTTY) flags will be filtered out / handled by
|
|
|
|
|
|
|
|
the kernel. if no files will use create function
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
FileNode *get_file;
|
|
|
|
FileNode *get_file;
|
|
|
|
if ((get_file = fischl_load_entry(root_node, path)) == NULL)
|
|
|
|
if ((get_file = fischl_load_entry(root_node, path)) == NULL)
|
|
|
@ -733,7 +773,8 @@ int FilesOperation::fischl_release(const char *path, struct fuse_file_info *fi){
|
|
|
|
return 0; // SUCESS
|
|
|
|
return 0; // SUCESS
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int FilesOperation::fischl_write(const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *fi){
|
|
|
|
int FilesOperation::fischl_write(const char *path, const char *buf, size_t size,
|
|
|
|
|
|
|
|
off_t offset, struct fuse_file_info *fi) {
|
|
|
|
/** Write data to an open file
|
|
|
|
/** Write data to an open file
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Write should return exactly the number of bytes requested
|
|
|
|
* Write should return exactly the number of bytes requested
|
|
|
@ -747,14 +788,17 @@ int FilesOperation::fischl_write(const char *path, const char *buf, size_t size,
|
|
|
|
// FileNode *get_file;
|
|
|
|
// FileNode *get_file;
|
|
|
|
// if((get_file = fischl_load_entry(root_node, path)) == NULL)
|
|
|
|
// if((get_file = fischl_load_entry(root_node, path)) == NULL)
|
|
|
|
// return -ENOENT;
|
|
|
|
// return -ENOENT;
|
|
|
|
// Caution! this based on content in file are multiple of IO_BLOCK_SIZE, not the exact write size.
|
|
|
|
// Caution! this based on content in file are multiple of IO_BLOCK_SIZE, not
|
|
|
|
// based on current write_datablock API implement, when write_datablock pass with actual size not index this function should be fixed
|
|
|
|
// the exact write size. based on current write_datablock API implement, when
|
|
|
|
|
|
|
|
// write_datablock pass with actual size not index this function should be
|
|
|
|
|
|
|
|
// fixed
|
|
|
|
|
|
|
|
|
|
|
|
INode_Data inode;
|
|
|
|
INode_Data inode;
|
|
|
|
// Assuming inode is correctly initialized here based on 'path'
|
|
|
|
// Assuming inode is correctly initialized here based on 'path'
|
|
|
|
inode.inode_num = fi->fh;
|
|
|
|
inode.inode_num = fi->fh;
|
|
|
|
fs->inode_manager->load_inode(&inode);
|
|
|
|
fs->inode_manager->load_inode(&inode);
|
|
|
|
//size_t len = (inode.metadata.size/IO_BLOCK_SIZE) * IO_BLOCK_SIZE; // Assuming each block is 4096 bytes
|
|
|
|
// size_t len = (inode.metadata.size/IO_BLOCK_SIZE) * IO_BLOCK_SIZE; //
|
|
|
|
|
|
|
|
// Assuming each block is 4096 bytes
|
|
|
|
// Determine the length of the buffer
|
|
|
|
// Determine the length of the buffer
|
|
|
|
// Allocate memory for the new buffer
|
|
|
|
// Allocate memory for the new buffer
|
|
|
|
char *buffer = (char *)malloc(size);
|
|
|
|
char *buffer = (char *)malloc(size);
|
|
|
@ -764,16 +808,19 @@ int FilesOperation::fischl_write(const char *path, const char *buf, size_t size,
|
|
|
|
size_t block_offset = offset % IO_BLOCK_SIZE; // Offset within the first block
|
|
|
|
size_t block_offset = offset % IO_BLOCK_SIZE; // Offset within the first block
|
|
|
|
while (bytes_write < size) {
|
|
|
|
while (bytes_write < size) {
|
|
|
|
char block_buffer[IO_BLOCK_SIZE]; // Temporary buffer for each block
|
|
|
|
char block_buffer[IO_BLOCK_SIZE]; // Temporary buffer for each block
|
|
|
|
size_t copy_size = std::min(size - bytes_write, IO_BLOCK_SIZE - block_offset);
|
|
|
|
size_t copy_size = std::min(size - bytes_write, IO_BLOCK_SIZE -
|
|
|
|
memcpy(block_buffer + block_offset, buf + bytes_write, copy_size);
|
|
|
|
block_offset); memcpy(block_buffer + block_offset, buf + bytes_write,
|
|
|
|
fs->write(&inode, block_buffer, IO_BLOCK_SIZE, block_index*IO_BLOCK_SIZE);
|
|
|
|
copy_size); fs->write(&inode, block_buffer, IO_BLOCK_SIZE,
|
|
|
|
// fprintf(stderr,"[%s ,%d] inode.size %d, block_index %d, block_buffer %s\n",__func__,__LINE__, inode.size, block_index, block_buffer);
|
|
|
|
block_index*IO_BLOCK_SIZE);
|
|
|
|
bytes_write += copy_size;
|
|
|
|
// fprintf(stderr,"[%s ,%d] inode.size %d, block_index %d, block_buffer
|
|
|
|
block_index++;
|
|
|
|
%s\n",__func__,__LINE__, inode.size, block_index, block_buffer); bytes_write
|
|
|
|
block_offset = 0; // Only the first block might have a non-zero offset
|
|
|
|
+= copy_size; block_index++; block_offset = 0; // Only the first block might
|
|
|
|
|
|
|
|
have a non-zero offset
|
|
|
|
}*/
|
|
|
|
}*/
|
|
|
|
fs->inode_manager->save_inode(&inode);
|
|
|
|
fs->inode_manager->save_inode(&inode);
|
|
|
|
free(buffer);
|
|
|
|
free(buffer);
|
|
|
|
|
|
|
|
if (bytes_write < 0)
|
|
|
|
|
|
|
|
return -errno;
|
|
|
|
return bytes_write; // Return the actual number of bytes read
|
|
|
|
return bytes_write; // Return the actual number of bytes read
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -810,13 +857,20 @@ int FilesOperation::fischl_symlink(const char* to, const char* from){
|
|
|
|
// printf("SYMLINK %s %s\n", from, to);
|
|
|
|
// printf("SYMLINK %s %s\n", from, to);
|
|
|
|
char *pathdup = strdup(from);
|
|
|
|
char *pathdup = strdup(from);
|
|
|
|
char *lastSlash = strrchr(pathdup, '/');
|
|
|
|
char *lastSlash = strrchr(pathdup, '/');
|
|
|
|
*lastSlash = '\0'; // Split the string into parent path and new directory name; <parent path>\0<direcotry name>
|
|
|
|
*lastSlash = '\0'; // Split the string into parent path and new directory
|
|
|
|
char *newFilename = lastSlash+1; //\0<direcotry name>, get from <direcotry name>
|
|
|
|
// name; <parent path>\0<direcotry name>
|
|
|
|
char *ParentPath = pathdup;//pathdup are separated by pathdup, so it take <parent path> only
|
|
|
|
char *newFilename =
|
|
|
|
// fprintf(stderr,"[%s ,%d] ParentPath:%s, strlen=%d\n",__func__,__LINE__, ParentPath, strlen(ParentPath));
|
|
|
|
lastSlash + 1; //\0<direcotry name>, get from <direcotry name>
|
|
|
|
FileNode *parent_filenode = strlen(ParentPath)? fischl_load_entry(root_node, ParentPath): root_node->self_info;
|
|
|
|
char *ParentPath = pathdup; // pathdup are separated by pathdup, so it take
|
|
|
|
|
|
|
|
// <parent path> only
|
|
|
|
|
|
|
|
// fprintf(stderr,"[%s ,%d] ParentPath:%s, strlen=%d\n",__func__,__LINE__,
|
|
|
|
|
|
|
|
// ParentPath, strlen(ParentPath));
|
|
|
|
|
|
|
|
FileNode *parent_filenode = strlen(ParentPath)
|
|
|
|
|
|
|
|
? fischl_load_entry(root_node, ParentPath)
|
|
|
|
|
|
|
|
: root_node->self_info;
|
|
|
|
if (parent_filenode == NULL) {
|
|
|
|
if (parent_filenode == NULL) {
|
|
|
|
fprintf(stderr,"[%s ,%d] ParentPath:{%s} not found\n",__func__,__LINE__, ParentPath);
|
|
|
|
fprintf(stderr, "[%s ,%d] ParentPath:{%s} not found\n", __func__, __LINE__,
|
|
|
|
|
|
|
|
ParentPath);
|
|
|
|
free(pathdup);
|
|
|
|
free(pathdup);
|
|
|
|
return -ENOENT;
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -825,10 +879,13 @@ int FilesOperation::fischl_symlink(const char* to, const char* from){
|
|
|
|
return -EACCES;
|
|
|
|
return -EACCES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// make new inode
|
|
|
|
// make new inode
|
|
|
|
INode_Data* ret = create_new_inode(parent_inode_number, newFilename, S_IFLNK|0777);
|
|
|
|
INode_Data *ret =
|
|
|
|
if (ret == NULL) return -1;//ENOSPC but create_new_inode handle ENAMETOOLONG EEXIST
|
|
|
|
create_new_inode(parent_inode_number, newFilename, S_IFLNK | 0777);
|
|
|
|
|
|
|
|
if (ret == NULL)
|
|
|
|
|
|
|
|
return -1; // ENOSPC but create_new_inode handle ENAMETOOLONG EEXIST
|
|
|
|
// make new node in RAM
|
|
|
|
// make new node in RAM
|
|
|
|
fischl_add_entry(parent_filenode->subdirectory, ret->inode_num, newFilename, ret);
|
|
|
|
fischl_add_entry(parent_filenode->subdirectory, ret->inode_num, newFilename,
|
|
|
|
|
|
|
|
ret);
|
|
|
|
size_t size = strlen(to);
|
|
|
|
size_t size = strlen(to);
|
|
|
|
char *buffer = (char *)malloc(size);
|
|
|
|
char *buffer = (char *)malloc(size);
|
|
|
|
memcpy(buffer, to, size);
|
|
|
|
memcpy(buffer, to, size);
|
|
|
@ -841,7 +898,9 @@ int FilesOperation::fischl_symlink(const char* to, const char* from){
|
|
|
|
return 0; // SUCESS
|
|
|
|
return 0; // SUCESS
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int FilesOperation::insert_inode_to(u_int64_t parent_inode_number, const char* name, INode_Data *new_inode, bool check_replace) {
|
|
|
|
int FilesOperation::insert_inode_to(u_int64_t parent_inode_number,
|
|
|
|
|
|
|
|
const char *name, INode_Data *new_inode,
|
|
|
|
|
|
|
|
bool check_replace) {
|
|
|
|
// trys to create a file under parent directory
|
|
|
|
// trys to create a file under parent directory
|
|
|
|
if (strlen(name) >= 256) {
|
|
|
|
if (strlen(name) >= 256) {
|
|
|
|
perror("Name too long, cannot create file or directory");
|
|
|
|
perror("Name too long, cannot create file or directory");
|
|
|
@ -851,7 +910,8 @@ int FilesOperation::insert_inode_to(u_int64_t parent_inode_number, const char* n
|
|
|
|
inode.inode_num = parent_inode_number;
|
|
|
|
inode.inode_num = parent_inode_number;
|
|
|
|
fs->inode_manager->load_inode(&inode);
|
|
|
|
fs->inode_manager->load_inode(&inode);
|
|
|
|
if ((inode.metadata.permissions & S_IFMT) != S_IFDIR) {
|
|
|
|
if ((inode.metadata.permissions & S_IFMT) != S_IFDIR) {
|
|
|
|
fprintf(stderr,"[%s ,%d] please create under directory\n",__func__,__LINE__);
|
|
|
|
fprintf(stderr, "[%s ,%d] please create under directory\n", __func__,
|
|
|
|
|
|
|
|
__LINE__);
|
|
|
|
return -1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -865,13 +925,14 @@ int FilesOperation::insert_inode_to(u_int64_t parent_inode_number, const char* n
|
|
|
|
if (strcmp(ent.file_name, name) == 0 && ent.inode_number != 0) {
|
|
|
|
if (strcmp(ent.file_name, name) == 0 && ent.inode_number != 0) {
|
|
|
|
if (check_replace) {
|
|
|
|
if (check_replace) {
|
|
|
|
if ((new_inode->metadata.permissions & S_IFMT) == S_IFDIR) {
|
|
|
|
if ((new_inode->metadata.permissions & S_IFMT) == S_IFDIR) {
|
|
|
|
fprintf(stderr,"[%s ,%d] %s/ already exists\n",__func__,__LINE__, name);
|
|
|
|
fprintf(stderr, "[%s ,%d] %s/ already exists\n", __func__, __LINE__,
|
|
|
|
|
|
|
|
name);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
fprintf(stderr,"[%s ,%d] %s already exists\n",__func__,__LINE__, name);
|
|
|
|
fprintf(stderr, "[%s ,%d] %s already exists\n", __func__, __LINE__,
|
|
|
|
|
|
|
|
name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
else{
|
|
|
|
|
|
|
|
// printf("RENAME HAPPENS %s %s\n", );
|
|
|
|
// printf("RENAME HAPPENS %s %s\n", );
|
|
|
|
ent.inode_number = new_inode->inode_num;
|
|
|
|
ent.inode_number = new_inode->inode_num;
|
|
|
|
ent.serialize(r_buffer + i);
|
|
|
|
ent.serialize(r_buffer + i);
|
|
|
@ -930,13 +991,20 @@ int FilesOperation::fischl_link(const char* from, const char* to){
|
|
|
|
// check path
|
|
|
|
// check path
|
|
|
|
char *pathdup = strdup(to);
|
|
|
|
char *pathdup = strdup(to);
|
|
|
|
char *lastSlash = strrchr(pathdup, '/');
|
|
|
|
char *lastSlash = strrchr(pathdup, '/');
|
|
|
|
*lastSlash = '\0'; // Split the string into parent path and new directory name; <parent path>\0<direcotry name>
|
|
|
|
*lastSlash = '\0'; // Split the string into parent path and new directory
|
|
|
|
char *newFilename = lastSlash+1; //\0<direcotry name>, get from <direcotry name>
|
|
|
|
// name; <parent path>\0<direcotry name>
|
|
|
|
char *ParentPath = pathdup;//pathdup are separated by pathdup, so it take <parent path> only
|
|
|
|
char *newFilename =
|
|
|
|
// fprintf(stderr,"[%s ,%d] ParentPath:%s, strlen=%d\n",__func__,__LINE__, ParentPath, strlen(ParentPath));
|
|
|
|
lastSlash + 1; //\0<direcotry name>, get from <direcotry name>
|
|
|
|
FileNode *parent_filenode = strlen(ParentPath)? fischl_load_entry(root_node, ParentPath): root_node->self_info;
|
|
|
|
char *ParentPath = pathdup; // pathdup are separated by pathdup, so it take
|
|
|
|
|
|
|
|
// <parent path> only
|
|
|
|
|
|
|
|
// fprintf(stderr,"[%s ,%d] ParentPath:%s, strlen=%d\n",__func__,__LINE__,
|
|
|
|
|
|
|
|
// ParentPath, strlen(ParentPath));
|
|
|
|
|
|
|
|
FileNode *parent_filenode = strlen(ParentPath)
|
|
|
|
|
|
|
|
? fischl_load_entry(root_node, ParentPath)
|
|
|
|
|
|
|
|
: root_node->self_info;
|
|
|
|
if (parent_filenode == NULL) {
|
|
|
|
if (parent_filenode == NULL) {
|
|
|
|
fprintf(stderr,"[%s ,%d] ParentPath:{%s} not found\n",__func__,__LINE__, ParentPath);
|
|
|
|
fprintf(stderr, "[%s ,%d] ParentPath:{%s} not found\n", __func__, __LINE__,
|
|
|
|
|
|
|
|
ParentPath);
|
|
|
|
free(pathdup);
|
|
|
|
free(pathdup);
|
|
|
|
return -1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -951,14 +1019,15 @@ int FilesOperation::fischl_link(const char* from, const char* to){
|
|
|
|
|
|
|
|
|
|
|
|
ret.metadata.reference_count += 1;
|
|
|
|
ret.metadata.reference_count += 1;
|
|
|
|
fs->inode_manager->save_inode(&ret);
|
|
|
|
fs->inode_manager->save_inode(&ret);
|
|
|
|
fischl_add_entry(parent_filenode->subdirectory, ret.inode_num, newFilename, &ret);
|
|
|
|
fischl_add_entry(parent_filenode->subdirectory, ret.inode_num, newFilename,
|
|
|
|
|
|
|
|
&ret);
|
|
|
|
free(pathdup);
|
|
|
|
free(pathdup);
|
|
|
|
return 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: rename dir and rename fail
|
|
|
|
// TODO: rename dir and rename fail
|
|
|
|
int FilesOperation::fischl_rename(const char *old_path, const char *new_path, unsigned int flags){
|
|
|
|
int FilesOperation::fischl_rename(const char *old_path, const char *new_path,
|
|
|
|
|
|
|
|
unsigned int flags) {
|
|
|
|
// find old path
|
|
|
|
// find old path
|
|
|
|
char *pathdup = strdup(old_path);
|
|
|
|
char *pathdup = strdup(old_path);
|
|
|
|
char *lastSlash = strrchr(pathdup, '/');
|
|
|
|
char *lastSlash = strrchr(pathdup, '/');
|
|
|
@ -974,10 +1043,14 @@ int FilesOperation::fischl_rename(const char *old_path, const char *new_path, un
|
|
|
|
rename_info.exchangeExist = false;
|
|
|
|
rename_info.exchangeExist = false;
|
|
|
|
rename_info.oldParentNode = fischl_load_entry(root_node, oldParentPath);
|
|
|
|
rename_info.oldParentNode = fischl_load_entry(root_node, oldParentPath);
|
|
|
|
// if path end with / means to rename directory
|
|
|
|
// if path end with / means to rename directory
|
|
|
|
rename_info.oldFileNode = strlen(filename)? fischl_load_entry(rename_info.oldParentNode->subdirectory, filename):rename_info.oldParentNode;
|
|
|
|
rename_info.oldFileNode =
|
|
|
|
|
|
|
|
strlen(filename)
|
|
|
|
|
|
|
|
? fischl_load_entry(rename_info.oldParentNode->subdirectory, filename)
|
|
|
|
|
|
|
|
: rename_info.oldParentNode;
|
|
|
|
|
|
|
|
|
|
|
|
if (rename_info.oldFileNode == NULL) {
|
|
|
|
if (rename_info.oldFileNode == NULL) {
|
|
|
|
fprintf(stderr,"[%s ,%d] path %s not found by fischl_load_entry\n",__func__,__LINE__, old_path);
|
|
|
|
fprintf(stderr, "[%s ,%d] path %s not found by fischl_load_entry\n",
|
|
|
|
|
|
|
|
__func__, __LINE__, old_path);
|
|
|
|
free(pathdup);
|
|
|
|
free(pathdup);
|
|
|
|
return -ENOENT;
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -985,23 +1058,33 @@ int FilesOperation::fischl_rename(const char *old_path, const char *new_path, un
|
|
|
|
// find new path
|
|
|
|
// find new path
|
|
|
|
char *new_pathdup = strdup(new_path);
|
|
|
|
char *new_pathdup = strdup(new_path);
|
|
|
|
lastSlash = strrchr(new_pathdup, '/');
|
|
|
|
lastSlash = strrchr(new_pathdup, '/');
|
|
|
|
*lastSlash = '\0'; // Split the string into parent path and new directory name; <parent path>\0<direcotry name>
|
|
|
|
*lastSlash = '\0'; // Split the string into parent path and new directory
|
|
|
|
char *newParentPath = new_pathdup;//pathdup are separated by pathdup, so it take <parent path> only
|
|
|
|
// name; <parent path>\0<direcotry name>
|
|
|
|
|
|
|
|
char *newParentPath = new_pathdup; // pathdup are separated by pathdup, so it
|
|
|
|
|
|
|
|
// take <parent path> only
|
|
|
|
// put new path info in rename struct
|
|
|
|
// put new path info in rename struct
|
|
|
|
rename_info.newName = lastSlash+1; //\0<direcotry name>, get from <direcotry name>
|
|
|
|
rename_info.newName =
|
|
|
|
|
|
|
|
lastSlash + 1; //\0<direcotry name>, get from <direcotry name>
|
|
|
|
rename_info.newFileNode = fischl_load_entry(root_node, new_path);
|
|
|
|
rename_info.newFileNode = fischl_load_entry(root_node, new_path);
|
|
|
|
rename_info.newParentNode = strlen(newParentPath)? fischl_load_entry(root_node, newParentPath): root_node->self_info;
|
|
|
|
rename_info.newParentNode = strlen(newParentPath)
|
|
|
|
|
|
|
|
? fischl_load_entry(root_node, newParentPath)
|
|
|
|
|
|
|
|
: root_node->self_info;
|
|
|
|
|
|
|
|
|
|
|
|
if(!permission_check_by_inode_num(W_OK, rename_info.oldParentNode->inode_number)){
|
|
|
|
if (!permission_check_by_inode_num(W_OK,
|
|
|
|
|
|
|
|
rename_info.oldParentNode->inode_number)) {
|
|
|
|
return -EACCES;
|
|
|
|
return -EACCES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!permission_check_by_inode_num(W_OK, rename_info.newParentNode->inode_number)){
|
|
|
|
if (!permission_check_by_inode_num(W_OK,
|
|
|
|
|
|
|
|
rename_info.newParentNode->inode_number)) {
|
|
|
|
return -EACCES;
|
|
|
|
return -EACCES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!permission_check_by_inode_num(W_OK, rename_info.oldFileNode->inode_number)){
|
|
|
|
if (!permission_check_by_inode_num(W_OK,
|
|
|
|
|
|
|
|
rename_info.oldFileNode->inode_number)) {
|
|
|
|
return -EACCES;
|
|
|
|
return -EACCES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(rename_info.newFileNode != NULL && !permission_check_by_inode_num(W_OK, rename_info.newFileNode->inode_number)){
|
|
|
|
if (rename_info.newFileNode != NULL &&
|
|
|
|
|
|
|
|
!permission_check_by_inode_num(W_OK,
|
|
|
|
|
|
|
|
rename_info.newFileNode->inode_number)) {
|
|
|
|
return -EACCES;
|
|
|
|
return -EACCES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -1009,7 +1092,8 @@ int FilesOperation::fischl_rename(const char *old_path, const char *new_path, un
|
|
|
|
if (flags & RENAME_NOREPLACE) {
|
|
|
|
if (flags & RENAME_NOREPLACE) {
|
|
|
|
// Check if newpath exists and return error if it does
|
|
|
|
// Check if newpath exists and return error if it does
|
|
|
|
if (rename_info.newFileNode != NULL) {
|
|
|
|
if (rename_info.newFileNode != NULL) {
|
|
|
|
fprintf(stderr,"[%s ,%d] newpath has already existed\n",__func__,__LINE__);
|
|
|
|
fprintf(stderr, "[%s ,%d] newpath has already existed\n", __func__,
|
|
|
|
|
|
|
|
__LINE__);
|
|
|
|
free(new_pathdup); // new path
|
|
|
|
free(new_pathdup); // new path
|
|
|
|
free(pathdup); // old path
|
|
|
|
free(pathdup); // old path
|
|
|
|
return -1;
|
|
|
|
return -1;
|
|
|
@ -1019,7 +1103,8 @@ int FilesOperation::fischl_rename(const char *old_path, const char *new_path, un
|
|
|
|
if (flags & RENAME_EXCHANGE) {
|
|
|
|
if (flags & RENAME_EXCHANGE) {
|
|
|
|
// Ensure both oldpath and newpath exist and exchange them atomically
|
|
|
|
// Ensure both oldpath and newpath exist and exchange them atomically
|
|
|
|
if (rename_info.newFileNode == NULL) {
|
|
|
|
if (rename_info.newFileNode == NULL) {
|
|
|
|
fprintf(stderr,"[%s ,%d] newpath does not exist cannot exchange\n",__func__,__LINE__);
|
|
|
|
fprintf(stderr, "[%s ,%d] newpath does not exist cannot exchange\n",
|
|
|
|
|
|
|
|
__func__, __LINE__);
|
|
|
|
free(new_pathdup);
|
|
|
|
free(new_pathdup);
|
|
|
|
free(pathdup);
|
|
|
|
free(pathdup);
|
|
|
|
return -1;
|
|
|
|
return -1;
|
|
|
@ -1032,7 +1117,8 @@ int FilesOperation::fischl_rename(const char *old_path, const char *new_path, un
|
|
|
|
|
|
|
|
|
|
|
|
bool change_flag = false;
|
|
|
|
bool change_flag = false;
|
|
|
|
// delete record on old path
|
|
|
|
// delete record on old path
|
|
|
|
for (u_int64_t idx=0; idx<parent_INode.metadata.size/IO_BLOCK_SIZE; idx++) {
|
|
|
|
for (u_int64_t idx = 0; idx < parent_INode.metadata.size / IO_BLOCK_SIZE;
|
|
|
|
|
|
|
|
idx++) {
|
|
|
|
fs->read(&parent_INode, rw_buffer, IO_BLOCK_SIZE, idx * IO_BLOCK_SIZE);
|
|
|
|
fs->read(&parent_INode, rw_buffer, IO_BLOCK_SIZE, idx * IO_BLOCK_SIZE);
|
|
|
|
DirectoryEntry ent;
|
|
|
|
DirectoryEntry ent;
|
|
|
|
for (int i = 0; i <= IO_BLOCK_SIZE - 264; i += 264) {
|
|
|
|
for (int i = 0; i <= IO_BLOCK_SIZE - 264; i += 264) {
|
|
|
@ -1056,14 +1142,16 @@ int FilesOperation::fischl_rename(const char *old_path, const char *new_path, un
|
|
|
|
|
|
|
|
|
|
|
|
change_flag = false;
|
|
|
|
change_flag = false;
|
|
|
|
// delete record on old path
|
|
|
|
// delete record on old path
|
|
|
|
for (u_int64_t idx=0; idx<parent_INode.metadata.size/IO_BLOCK_SIZE; idx++) {
|
|
|
|
for (u_int64_t idx = 0; idx < parent_INode.metadata.size / IO_BLOCK_SIZE;
|
|
|
|
|
|
|
|
idx++) {
|
|
|
|
fs->read(&parent_INode, rw_buffer, IO_BLOCK_SIZE, idx * IO_BLOCK_SIZE);
|
|
|
|
fs->read(&parent_INode, rw_buffer, IO_BLOCK_SIZE, idx * IO_BLOCK_SIZE);
|
|
|
|
DirectoryEntry ent;
|
|
|
|
DirectoryEntry ent;
|
|
|
|
for (int i = 0; i <= IO_BLOCK_SIZE - 264; i += 264) {
|
|
|
|
for (int i = 0; i <= IO_BLOCK_SIZE - 264; i += 264) {
|
|
|
|
ent.deserialize(rw_buffer + i);
|
|
|
|
ent.deserialize(rw_buffer + i);
|
|
|
|
if (ent.inode_number == rename_info.oldFileNode->inode_number) {
|
|
|
|
if (ent.inode_number == rename_info.oldFileNode->inode_number) {
|
|
|
|
change_flag = true; // should be change
|
|
|
|
change_flag = true; // should be change
|
|
|
|
strncpy(ent.file_name, rename_info.newName, sizeof(ent.file_name) - 1);
|
|
|
|
strncpy(ent.file_name, rename_info.newName,
|
|
|
|
|
|
|
|
sizeof(ent.file_name) - 1);
|
|
|
|
ent.file_name[sizeof(ent.file_name) - 1] = '\0';
|
|
|
|
ent.file_name[sizeof(ent.file_name) - 1] = '\0';
|
|
|
|
ent.serialize(rw_buffer + i);
|
|
|
|
ent.serialize(rw_buffer + i);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
@ -1075,8 +1163,10 @@ int FilesOperation::fischl_rename(const char *old_path, const char *new_path, un
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unlink_inode(rename_info.oldFileNode->inode_number);
|
|
|
|
fischl_rm_entry(rename_info.oldParentNode->subdirectory, filename);
|
|
|
|
fischl_rm_entry(rename_info.oldParentNode->subdirectory, filename);
|
|
|
|
fischl_rm_entry(rename_info.newParentNode->subdirectory, rename_info.newName);
|
|
|
|
fischl_rm_entry(rename_info.newParentNode->subdirectory,
|
|
|
|
|
|
|
|
rename_info.newName);
|
|
|
|
return 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -1084,7 +1174,6 @@ int FilesOperation::fischl_rename(const char *old_path, const char *new_path, un
|
|
|
|
// Hard Disk rename
|
|
|
|
// Hard Disk rename
|
|
|
|
if (rename_info.oldFileNode->subdirectory != NULL) { // secure its directory
|
|
|
|
if (rename_info.oldFileNode->subdirectory != NULL) { // secure its directory
|
|
|
|
// remove its record from subdirectory; find .. from subdirectory
|
|
|
|
// remove its record from subdirectory; find .. from subdirectory
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// remove its record from parent
|
|
|
|
// remove its record from parent
|
|
|
|
INode_Data parent_INode;
|
|
|
|
INode_Data parent_INode;
|
|
|
@ -1096,12 +1185,14 @@ int FilesOperation::fischl_rename(const char *old_path, const char *new_path, un
|
|
|
|
INode_Data ret;
|
|
|
|
INode_Data ret;
|
|
|
|
ret.inode_num = rename_info.oldFileNode->inode_number;
|
|
|
|
ret.inode_num = rename_info.oldFileNode->inode_number;
|
|
|
|
fs->inode_manager->load_inode(&ret);
|
|
|
|
fs->inode_manager->load_inode(&ret);
|
|
|
|
if(insert_inode_to(rename_info.newParentNode->inode_number, rename_info.newName, &ret, false)<0){
|
|
|
|
if (insert_inode_to(rename_info.newParentNode->inode_number,
|
|
|
|
|
|
|
|
rename_info.newName, &ret, false) < 0) {
|
|
|
|
return -1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bool change_flag = false;
|
|
|
|
bool change_flag = false;
|
|
|
|
// delete record on old path
|
|
|
|
// delete record on old path
|
|
|
|
for (u_int64_t idx=0; idx<parent_INode.metadata.size/IO_BLOCK_SIZE; idx++) {
|
|
|
|
for (u_int64_t idx = 0; idx < parent_INode.metadata.size / IO_BLOCK_SIZE;
|
|
|
|
|
|
|
|
idx++) {
|
|
|
|
fs->read(&parent_INode, rw_buffer, IO_BLOCK_SIZE, idx * IO_BLOCK_SIZE);
|
|
|
|
fs->read(&parent_INode, rw_buffer, IO_BLOCK_SIZE, idx * IO_BLOCK_SIZE);
|
|
|
|
DirectoryEntry ent;
|
|
|
|
DirectoryEntry ent;
|
|
|
|
for (int i = 0; i <= IO_BLOCK_SIZE - 264; i += 264) {
|
|
|
|
for (int i = 0; i <= IO_BLOCK_SIZE - 264; i += 264) {
|
|
|
@ -1121,7 +1212,8 @@ int FilesOperation::fischl_rename(const char *old_path, const char *new_path, un
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (rename_info.newParentNode != NULL) {
|
|
|
|
if (rename_info.newParentNode != NULL) {
|
|
|
|
fischl_rm_entry(rename_info.newParentNode->subdirectory, rename_info.newName);
|
|
|
|
fischl_rm_entry(rename_info.newParentNode->subdirectory,
|
|
|
|
|
|
|
|
rename_info.newName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fischl_rm_entry(rename_info.oldParentNode->subdirectory, filename);
|
|
|
|
fischl_rm_entry(rename_info.oldParentNode->subdirectory, filename);
|
|
|
|
|
|
|
|
|
|
|
@ -1149,6 +1241,8 @@ int FilesOperation::fischl_truncate(const char *path, off_t offset,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res = fs->truncate(&inode, offset);
|
|
|
|
res = fs->truncate(&inode, offset);
|
|
|
|
fs->inode_manager->save_inode(&inode);
|
|
|
|
fs->inode_manager->save_inode(&inode);
|
|
|
|
|
|
|
|
if (res < 0)
|
|
|
|
|
|
|
|
return -errno;
|
|
|
|
return res;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -1171,7 +1265,8 @@ int FilesOperation::fischl_read(const char *path, char *buf, size_t size,
|
|
|
|
// Assuming inode is correctly initialized here based on 'path'
|
|
|
|
// Assuming inode is correctly initialized here based on 'path'
|
|
|
|
inode.inode_num = fi->fh;
|
|
|
|
inode.inode_num = fi->fh;
|
|
|
|
fs->inode_manager->load_inode(&inode);
|
|
|
|
fs->inode_manager->load_inode(&inode);
|
|
|
|
//printf("OUT READ %llu %llu %llu\n", inode.inode_num, inode.single_indirect_block, inode.double_indirect_block);
|
|
|
|
// printf("OUT READ %llu %llu %llu\n", inode.inode_num,
|
|
|
|
|
|
|
|
// inode.single_indirect_block, inode.double_indirect_block);
|
|
|
|
ssize_t bytes_read = fs->read(&inode, buf, size, offset);
|
|
|
|
ssize_t bytes_read = fs->read(&inode, buf, size, offset);
|
|
|
|
// printf("BYTES_READ %d\n",int(bytes_read));
|
|
|
|
// printf("BYTES_READ %d\n",int(bytes_read));
|
|
|
|
// for (int i = 0; i < bytes_read; i++)printf("%x", buf[i]&0xff);
|
|
|
|
// for (int i = 0; i < bytes_read; i++)printf("%x", buf[i]&0xff);
|
|
|
@ -1199,10 +1294,15 @@ int FilesOperation::fischl_read(const char *path, char *buf, size_t size,
|
|
|
|
buf, block_buffer); bytes_read += copy_size; block_index++; block_offset = 0;
|
|
|
|
buf, block_buffer); bytes_read += copy_size; block_index++; block_offset = 0;
|
|
|
|
// Only the first block might have a non-zero offset
|
|
|
|
// Only the first block might have a non-zero offset
|
|
|
|
}*/
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (bytes_read < 0)
|
|
|
|
|
|
|
|
return -errno;
|
|
|
|
return bytes_read; // Return the actual number of bytes read
|
|
|
|
return bytes_read; // Return the actual number of bytes read
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int FilesOperation::fischl_utimens(const char *path, const struct timespec tv[2], struct fuse_file_info *fi){
|
|
|
|
int FilesOperation::fischl_utimens(const char *path,
|
|
|
|
|
|
|
|
const struct timespec tv[2],
|
|
|
|
|
|
|
|
struct fuse_file_info *fi) {
|
|
|
|
(void)fi;
|
|
|
|
(void)fi;
|
|
|
|
int res = 0;
|
|
|
|
int res = 0;
|
|
|
|
u_int64_t fh = namei(path);
|
|
|
|
u_int64_t fh = namei(path);
|
|
|
@ -1214,8 +1314,10 @@ int FilesOperation::fischl_utimens(const char *path, const struct timespec tv[2]
|
|
|
|
INode_Data inode;
|
|
|
|
INode_Data inode;
|
|
|
|
inode.inode_num = fh;
|
|
|
|
inode.inode_num = fh;
|
|
|
|
fs->inode_manager->load_inode(&inode);
|
|
|
|
fs->inode_manager->load_inode(&inode);
|
|
|
|
inode.metadata.access_time = (u_int64_t)tv[0].tv_sec * 1000000000ULL + tv[0].tv_nsec;
|
|
|
|
inode.metadata.access_time =
|
|
|
|
inode.metadata.modification_time = (u_int64_t)tv[1].tv_sec * 1000000000ULL + tv[1].tv_nsec;
|
|
|
|
(u_int64_t)tv[0].tv_sec * 1000000000ULL + tv[0].tv_nsec;
|
|
|
|
|
|
|
|
inode.metadata.modification_time =
|
|
|
|
|
|
|
|
(u_int64_t)tv[1].tv_sec * 1000000000ULL + tv[1].tv_nsec;
|
|
|
|
fs->inode_manager->save_inode(&inode);
|
|
|
|
fs->inode_manager->save_inode(&inode);
|
|
|
|
return 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|