Skip to content

Commit

Permalink
fast_fuse:添加删除功能 (#871)
Browse files Browse the repository at this point in the history
* fuse文件系统示例

* 添加文件创建功能

* 添加touch创建文件功能

* 解决问题

* 添加删除功能

* 修改

---------

Co-authored-by: qcloud <[email protected]>
  • Loading branch information
liuslient and qcloud authored Jul 30, 2024
1 parent 73686c5 commit 31cd156
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
Binary file modified eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/src/difuse
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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[])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit 31cd156

Please sign in to comment.