Skip to content

Commit

Permalink
Merge pull request #893 from liuslient/develop
Browse files Browse the repository at this point in the history
读写功能问题解决添加脚本
  • Loading branch information
LinkinPF authored Sep 13, 2024
2 parents b3873cd + f1ce23e commit c041560
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 39 deletions.
67 changes: 36 additions & 31 deletions eBPF_Supermarket/Filesystem_Subsystem/fast_fuse/difuse/src/difuse.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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)
{
Expand Down Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -73,7 +85,3 @@ rmdir $MOUNT_POINT/dir2
# 验证目录删除
echo "验证目录删除后的结构:"
ls -l $MOUNT_POINT
=======
# 确保脚本退出时卸载文件系统
trap "fusermount -u $MOUNT_POINT" EXIT
>>>>>>> e040be4f7b8f162b7a4c9b0c5f5bb40158e2b6fb

0 comments on commit c041560

Please sign in to comment.