From f1ce23ec2dbd8dbfc289b774470c31b3d41e3229 Mon Sep 17 00:00:00 2001 From: qcloud Date: Thu, 12 Sep 2024 16:55:48 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=BB=E5=86=99=E5=8A=9F=E8=83=BD=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E8=A7=A3=E5=86=B3=E6=B7=BB=E5=8A=A0=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fast_fuse/difuse/src/difuse.c | 67 ++++++++++--------- .../fast_fuse/difuse/test/test.sh | 24 ++++--- 2 files changed, 52 insertions(+), 39 deletions(-) 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 0c2443e79..f1a6d29a4 100644 --- a/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/src/difuse.c +++ b/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/src/difuse.c @@ -36,14 +36,17 @@ static struct dfs_data *allocate_data_block() struct dfs_inode { - uint32_t ino; //inode编号 - int size; //文件大小 - int dir_cnt; // 如果是目录类型文件,下面有几个目录项 - struct dfs_data *data_pointer; //指向数据块的指针 - struct dfs_inode *prev; // LRU 链表前驱指针 - struct dfs_inode *next; // LRU 链表后继指针 + uint32_t ino; // inode编号 + int size; // 文件大小 + int dir_cnt; // 目录项数量 + struct dfs_data *data_pointer; // 数据块指针 + time_t atime; // 最后访问时间 + time_t mtime; // 最后修改时间 + struct dfs_inode *prev; + struct dfs_inode *next; }; + struct dfs_dentry { char fname[255]; @@ -302,9 +305,14 @@ static int di_utimens(const char *path, const struct timespec ts[2], struct fuse return -ENOENT; } + // 设置文件的时间戳 + dentry->inode->atime = ts[0].tv_sec; // 访问时间 + dentry->inode->mtime = ts[1].tv_sec; // 修改时间 + return 0; } + static int di_mkdir(const char *path, mode_t mode) { (void)mode; @@ -338,7 +346,6 @@ static int dfs_create(const char *path, mode_t mode, struct fuse_file_info *fi) static int di_getattr(const char *path, struct stat *di_stat, struct fuse_file_info *fi) { (void)fi; - int ret = 0; memset(di_stat, 0, sizeof(struct stat)); struct dfs_dentry *dentry = look_up(root, path); @@ -357,9 +364,13 @@ static int di_getattr(const char *path, struct stat *di_stat, struct fuse_file_i di_stat->st_size = dentry->inode->size; } - return ret; + di_stat->st_atime = dentry->inode->atime; // 最后访问时间 + di_stat->st_mtime = dentry->inode->mtime; // 最后修改时间 + + return 0; } + /*遍历目录项*/ static int di_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi, enum fuse_readdir_flags flags) { @@ -419,41 +430,34 @@ static int di_read(const char *path, char *buf, size_t size, off_t offset, struc if (offset + size > file_size) size = file_size - offset; - // 初始化缓冲区 - memset(buf, 0, size); - - // 从数据块中读取数据 size_t bytes_read = 0; struct dfs_data *data_block = inode->data_pointer; - // 遍历数据块 + // 遍历数据块,处理偏移和读取 while (data_block != NULL && bytes_read < size) { - // 计算当前块的有效数据长度 - size_t block_size = data_block->size; - if (offset >= block_size) + if (offset >= CHUNK_SIZE) { - offset -= block_size; - } - else - { - // 从当前块读取数据 - size_t to_read = block_size - offset; - if (to_read > size - bytes_read) - to_read = size - bytes_read; - - // 复制数据到缓冲区 - memcpy(buf + bytes_read, ((char *)data_block) + offset, to_read); - bytes_read += to_read; - offset = 0; + offset -= CHUNK_SIZE; + data_block = data_block->next; + continue; } + size_t to_read = CHUNK_SIZE - offset; + if (to_read > size - bytes_read) + to_read = size - bytes_read; + + memcpy(buf + bytes_read, data_block->data + offset, to_read); + bytes_read += to_read; + offset = 0; // 只有第一个块需要处理 offset,之后的块直接从头开始 + data_block = data_block->next; } return bytes_read; } + static int di_write(const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *fi) { (void)fi; @@ -474,9 +478,9 @@ static int di_write(const char *path, const char *buf, size_t size, off_t offset size_t bytes_written = 0; size_t total_offset = offset; - while (data_block != NULL && total_offset >= data_block->size) + while (data_block != NULL && total_offset >= CHUNK_SIZE) { - total_offset -= data_block->size; + total_offset -= CHUNK_SIZE; if (data_block->next == NULL) { data_block->next = allocate_data_block(); @@ -512,6 +516,7 @@ static int di_write(const char *path, const char *buf, size_t size, off_t offset return bytes_written; } + static void *di_init(struct fuse_conn_info *conn, struct fuse_config *cfg) { (void)conn; 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 5dbb6234f..f833d0764 100755 --- a/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/test/test.sh +++ b/eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/test/test.sh @@ -28,12 +28,9 @@ $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 @@ -47,7 +44,22 @@ touch $MOUNT_POINT/dir2/file3 echo "创建的目录和文件结构:" ls -l $MOUNT_POINT -<<<<<<< HEAD +# 进行写入测试 +echo "测试文件内容写入..." +echo "Hello, this is a write test!" > $MOUNT_POINT/dir1/file1 + +# 进行读取测试 +echo "读取文件内容:" +cat $MOUNT_POINT/dir1/file1 + +# 验证写入是否正确 +FILE_CONTENT=$(cat $MOUNT_POINT/dir1/file1) +if [ "$FILE_CONTENT" == "Hello, this is a write test!" ]; then + echo "文件写入和读取成功。" +else + echo "文件写入或读取失败!" +fi + # 删除文件 echo "删除文件 $MOUNT_POINT/dir1/file1 和 $MOUNT_POINT/dir2/file3..." rm $MOUNT_POINT/dir1/file1 @@ -73,7 +85,3 @@ rmdir $MOUNT_POINT/dir2 # 验证目录删除 echo "验证目录删除后的结构:" ls -l $MOUNT_POINT -======= -# 确保脚本退出时卸载文件系统 -trap "fusermount -u $MOUNT_POINT" EXIT ->>>>>>> e040be4f7b8f162b7a4c9b0c5f5bb40158e2b6fb