Merge remote-tracking branch 'origin/layer2dev' into layer2merge
This commit is contained in:
commit
01ac7e9d11
@ -449,8 +449,28 @@ int FilesOperation::fischl_write(const char *path, const char *buf, size_t size,
|
||||
// FileNode *get_file;
|
||||
// if((get_file = fischl_find_entry(root_node, path)) == NULL)
|
||||
// return -ENOENT;
|
||||
// Caution! this based on content in file are multiple of IO_BLOCK_SIZE, not 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 inode;
|
||||
// Assuming inode is correctly initialized here based on 'path'
|
||||
inode.inode_construct(fi->fh, disk);
|
||||
size_t len = inode.size * IO_BLOCK_SIZE; // Assuming each block is 4096 bytes
|
||||
|
||||
return size;
|
||||
size_t bytes_write = 0;
|
||||
size_t block_index = offset / IO_BLOCK_SIZE; // Starting block index
|
||||
size_t block_offset = offset % IO_BLOCK_SIZE; // Offset within the first block
|
||||
while (bytes_write < size) {
|
||||
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);
|
||||
memcpy(block_buffer + block_offset, buf + bytes_write, copy_size);
|
||||
write_datablock(inode, block_index, block_buffer);
|
||||
// fprintf(stderr,"[%s ,%d] inode.size %d, block_index %d, block_buffer %s\n",__func__,__LINE__, inode.size, block_index, block_buffer);
|
||||
bytes_write += copy_size;
|
||||
block_index++;
|
||||
block_offset = 0; // Only the first block might have a non-zero offset
|
||||
}
|
||||
inode.inode_save(disk);
|
||||
return bytes_write; // Return the actual number of bytes read
|
||||
}
|
||||
|
||||
int FilesOperation::fischl_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi){
|
||||
|
@ -65,23 +65,22 @@ TEST(FileOperationTest, WriteTest) {
|
||||
char buffer[IO_BLOCK_SIZE] = {0};
|
||||
INode_Data inode;
|
||||
u_int64_t get_disk_inum;
|
||||
struct fuse_file_info fi;
|
||||
|
||||
//file test
|
||||
get_disk_inum = fsop->disk_namei("/test");
|
||||
inode.inode_num = get_disk_inum;
|
||||
fs->inode_manager->load_inode(&inode);
|
||||
fsop->fischl_open("/test", &fi);
|
||||
EXPECT_EQ(fi.fh, get_disk_inum);
|
||||
buffer[0] = '1';
|
||||
fsop->write_datablock(inode, 0, buffer);
|
||||
fs->inode_manager->save_inode(&inode);
|
||||
fsop->fischl_write("/test", buffer, sizeof(buffer), 0, &fi);
|
||||
//other file baz
|
||||
get_disk_inum = fsop->disk_namei("/foo/bar/baz");
|
||||
inode.inode_num = get_disk_inum;
|
||||
fs->inode_manager->load_inode(&inode);
|
||||
buffer[0] = '4';
|
||||
fsop->write_datablock(inode, 3, buffer);
|
||||
fsop->fischl_open("/foo/bar/baz", &fi);
|
||||
EXPECT_EQ(fi.fh, get_disk_inum);
|
||||
fsop->fischl_write("/foo/bar/baz", buffer, sizeof(buffer), 3*IO_BLOCK_SIZE, &fi);
|
||||
buffer[0] = '5';
|
||||
fsop->write_datablock(inode, 101, buffer);
|
||||
fs->inode_manager->save_inode(&inode);
|
||||
// TODO: guard against overwriting directory datablocks
|
||||
fsop->fischl_write("/foo/bar/baz", buffer, sizeof(buffer), 101*IO_BLOCK_SIZE, &fi);
|
||||
}
|
||||
|
||||
TEST(FileOperationTest, RamDiskTest) {
|
||||
@ -141,11 +140,18 @@ TEST(FileOperationTest, ReadTest) {
|
||||
|
||||
//read baz file
|
||||
get_file_inum= fsop->namei("/foo/bar/baz");
|
||||
inode.inode_num = get_file_inum;
|
||||
fs->inode_manager->load_inode(&inode);
|
||||
fsop->read_datablock(inode, 3, read_buffer);
|
||||
// inode.inode_construct(get_file_inum, *H);
|
||||
// fsop->read_datablock(inode, 3, read_buffer);
|
||||
// EXPECT_EQ(read_buffer[0], '4');
|
||||
// fsop->read_datablock(inode, 101, read_buffer);
|
||||
// EXPECT_EQ(read_buffer[0], '5');
|
||||
|
||||
//read baz file again with fischl_read API
|
||||
fsop->fischl_open("/foo/bar/baz", &fi);
|
||||
EXPECT_EQ(fi.fh, get_file_inum);
|
||||
fsop->fischl_read("/foo/bar/baz", read_buffer, sizeof(read_buffer), 3*IO_BLOCK_SIZE, &fi);
|
||||
EXPECT_EQ(read_buffer[0], '4');
|
||||
fsop->read_datablock(inode, 101, read_buffer);
|
||||
fsop->fischl_read("/foo/bar/baz", read_buffer, sizeof(read_buffer), 101*IO_BLOCK_SIZE, &fi);
|
||||
EXPECT_EQ(read_buffer[0], '5');
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user