Ext2FS: Support reading from file holes
It's perfectly valid for ext2 inodes to have blocks with index 0. It means that no physical block was allocated for that area of an inode and we should treat it as if it's filled with zeroes. Fixes #6139.
This commit is contained in:
parent
b5b38d372c
commit
642b428793
Notes:
sideshowbarker
2024-07-18 20:36:04 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/642b428793b
1 changed files with 10 additions and 6 deletions
|
@ -896,14 +896,19 @@ ssize_t Ext2FSInode::read_bytes(off_t offset, ssize_t count, UserOrKernelBuffer&
|
|||
|
||||
for (auto bi = first_block_logical_index; remaining_count && bi <= last_block_logical_index; bi = bi.value() + 1) {
|
||||
auto block_index = m_block_list[bi.value()];
|
||||
VERIFY(block_index.value());
|
||||
size_t offset_into_block = (bi == first_block_logical_index) ? offset_into_first_block : 0;
|
||||
size_t num_bytes_to_copy = min((off_t)block_size - offset_into_block, remaining_count);
|
||||
auto buffer_offset = buffer.offset(nread);
|
||||
auto result = fs().read_block(block_index, &buffer_offset, num_bytes_to_copy, offset_into_block, allow_cache);
|
||||
if (result.is_error()) {
|
||||
dmesgln("Ext2FSInode[{}]::read_bytes(): Failed to read block {} (index {})", identifier(), block_index.value(), bi);
|
||||
return result.error();
|
||||
if (block_index.value() == 0) {
|
||||
// This is a hole, act as if it's filled with zeroes.
|
||||
if (!buffer_offset.memset(0, num_bytes_to_copy))
|
||||
return -EFAULT;
|
||||
} else {
|
||||
auto result = fs().read_block(block_index, &buffer_offset, num_bytes_to_copy, offset_into_block, allow_cache);
|
||||
if (result.is_error()) {
|
||||
dmesgln("Ext2FSInode[{}]::read_bytes(): Failed to read block {} (index {})", identifier(), block_index.value(), bi);
|
||||
return result.error();
|
||||
}
|
||||
}
|
||||
remaining_count -= num_bytes_to_copy;
|
||||
nread += num_bytes_to_copy;
|
||||
|
@ -1784,5 +1789,4 @@ KResult Ext2FS::prepare_to_unmount() const
|
|||
m_inode_cache.clear();
|
||||
return KSuccess;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue