diff --git a/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/src/difuse b/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/src/difuse index 02094cfda..8b255be73 100755 Binary files a/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/src/difuse and b/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/src/difuse differ diff --git a/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/src/difuse.c b/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/src/difuse.c index 387b0304c..03cdec001 100644 --- a/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/src/difuse.c +++ b/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/src/difuse.c @@ -151,6 +151,25 @@ void add_child_dentry(struct dfs_dentry *parent, struct dfs_dentry *child) parent->child = child; } +static int remove_child_dentry(struct dfs_dentry *parent, struct dfs_dentry *child) +{ + struct dfs_dentry *prev_child = NULL; + struct dfs_dentry *cur_child = parent->child; + + while (cur_child != NULL && cur_child != child) + { + prev_child = cur_child; + cur_child = cur_child->brother; + } + if (cur_child == NULL) + return 0; + + if (prev_child == NULL) + parent->child = cur_child->brother; + else prev_child->brother = cur_child->brother; + return 1; +} + struct dfs_dentry *traverse_path(struct dfs_dentry *start_dentry, const char *path, int ftype, int create) { struct dfs_dentry *dentry = start_dentry; @@ -220,6 +239,51 @@ struct dfs_dentry *lookup_or_create_dentry(const char *path, struct dfs_dentry * /*功能函数*/ +static int di_unlink(const char *path) +{ + struct dfs_dentry *dentry = look_up(root, path); + + if (dentry == NULL) + return -ENOENT; + if (dentry->ftype != FILE_TYPE) + return -EISDIR; + + if (remove_child_dentry(dentry->parent, dentry)) + { + lru_remove(dentry); + unsigned int index = hash(dentry->fname); + hash_table[index] = NULL; + free(dentry->inode); + free(dentry); + return 0; + } + return -ENOENT; +} + +static int di_rmdir(const char *path) +{ + struct dfs_dentry *dentry = look_up(root, path); + + if (dentry == NULL) + return -ENOENT; + if (dentry->ftype != DIRECTORY_TYPE) + return -ENOTDIR; + if (dentry->child != NULL) + return -ENOTEMPTY; + + // 移除子目录项 + if (remove_child_dentry(dentry->parent, dentry)) + { + lru_remove(dentry); + unsigned int index = hash(dentry->fname); + hash_table[index] = NULL; + free(dentry->inode); + free(dentry); + return 0; + } + return -ENOENT; +} + static int di_utimens(const char *path, const struct timespec ts[2], struct fuse_file_info *fi) { (void)fi; @@ -369,6 +433,8 @@ static struct fuse_operations difs_ops = { .mkdir = di_mkdir, .create = dfs_create, .utimens = di_utimens, + .unlink = di_unlink, + .rmdir = di_rmdir, }; int main(int argc, char *argv[]) diff --git a/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/test/test.sh b/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/test/test.sh index 55967cbd7..5dbb6234f 100755 --- a/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/test/test.sh +++ b/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/test/test.sh @@ -28,6 +28,12 @@ $FUSE_EXEC -f -d "$MOUNT_POINT" & FUSE_PID=$! sleep 2 # 等待文件系统完全挂载 +<<<<<<< HEAD +# 确保脚本退出时卸载文件系统 +trap "fusermount -u $MOUNT_POINT" EXIT + +======= +>>>>>>> e040be4f7b8f162b7a4c9b0c5f5bb40158e2b6fb # 创建目录 mkdir $MOUNT_POINT/dir1 mkdir $MOUNT_POINT/dir2 @@ -41,5 +47,33 @@ touch $MOUNT_POINT/dir2/file3 echo "创建的目录和文件结构:" ls -l $MOUNT_POINT +<<<<<<< HEAD +# 删除文件 +echo "删除文件 $MOUNT_POINT/dir1/file1 和 $MOUNT_POINT/dir2/file3..." +rm $MOUNT_POINT/dir1/file1 +rm $MOUNT_POINT/dir2/file3 + +# 验证文件删除 +echo "验证文件删除后的结构:" +ls -l $MOUNT_POINT/dir1 +ls -l $MOUNT_POINT/dir2 + +# 尝试删除非空目录 +echo "尝试删除非空目录 $MOUNT_POINT/dir1 (应失败)..." +rmdir $MOUNT_POINT/dir1 || echo "无法删除非空目录 $MOUNT_POINT/dir1, 操作成功。" + +# 删除剩余文件 +rm $MOUNT_POINT/dir1/file2 + +# 删除空目录 +echo "删除空目录 $MOUNT_POINT/dir1 和 $MOUNT_POINT/dir2..." +rmdir $MOUNT_POINT/dir1 +rmdir $MOUNT_POINT/dir2 + +# 验证目录删除 +echo "验证目录删除后的结构:" +ls -l $MOUNT_POINT +======= # 确保脚本退出时卸载文件系统 trap "fusermount -u $MOUNT_POINT" EXIT +>>>>>>> e040be4f7b8f162b7a4c9b0c5f5bb40158e2b6fb