Treat non-existent data blocks as all zeroes

This commit is contained in:
pjht 2024-09-16 13:50:13 -05:00
parent e55ad22620
commit 9326191911
Signed by: pjht
GPG Key ID: CA239FC6934E6F3A

View File

@ -54,29 +54,27 @@ impl File {
let start_block = (pos / self.block_size as u64) as u32; let start_block = (pos / self.block_size as u64) as u32;
let offset = (pos % self.block_size as u64) as usize; let offset = (pos % self.block_size as u64) as usize;
let length = core::cmp::min(bytes_to_end, buf.len() as u64); let length = core::cmp::min(bytes_to_end, buf.len() as u64);
if let Some(phys_block) = self.inode.log_to_phys_block(fs, start_block) {
let _ = fs let _ = fs
.reader .reader
.read_block_offset( .read_block_offset(phys_block, offset as u32)?
self.inode
.log_to_phys_block(&fs, start_block)
.ok_or_else(|| io::Error::new(io::ErrorKind::UnexpectedEof, "EOF reached"))?,
offset as u32,
)?
.read(buf)?; .read(buf)?;
} else {
let buf_len = buf.len();
buf[..usize::min(self.block_size, buf_len)].fill(0);
}
let mut remaining_bytes = length.saturating_sub((self.block_size - offset) as u64); let mut remaining_bytes = length.saturating_sub((self.block_size - offset) as u64);
let mut block = start_block + 1; let mut block = start_block + 1;
while remaining_bytes != 0 { while remaining_bytes != 0 {
let mut chunk_start_block = self if let Some(chunk_start_block) = self.inode.log_to_phys_block(fs, block) {
.inode
.log_to_phys_block(&fs, block)
.ok_or_else(|| io::Error::new(io::ErrorKind::UnexpectedEof, "EOF reached"))?;
let mut chunk_num_blocks = 1; let mut chunk_num_blocks = 1;
block += 1; block += 1;
loop { loop {
if (chunk_num_blocks as usize * self.block_size) as u64 >= remaining_bytes { if (chunk_num_blocks as usize * self.block_size) as u64 >= remaining_bytes {
break; break;
} }
let Some(phys_block) = self.inode.log_to_phys_block(&fs, block) else { let Some(phys_block) = self.inode.log_to_phys_block(fs, block) else {
break; break;
}; };
if phys_block != chunk_start_block + chunk_num_blocks { if phys_block != chunk_start_block + chunk_num_blocks {
@ -89,7 +87,14 @@ impl File {
.reader .reader
.read_blocks(chunk_start_block, chunk_num_blocks as usize)? .read_blocks(chunk_start_block, chunk_num_blocks as usize)?
.read(&mut buf[((length - remaining_bytes) as usize)..])?; .read(&mut buf[((length - remaining_bytes) as usize)..])?;
remaining_bytes = remaining_bytes.saturating_sub((self.block_size * chunk_num_blocks as usize) as u64); remaining_bytes = remaining_bytes
.saturating_sub((self.block_size * chunk_num_blocks as usize) as u64);
} else {
let tmp = &mut buf[((length - remaining_bytes) as usize)..];
let tmp_len = tmp.len();
tmp[..usize::min(self.block_size, tmp_len)].fill(0);
remaining_bytes = remaining_bytes.saturating_sub(1024);
}
} }
Ok((length - remaining_bytes) as usize) Ok((length - remaining_bytes) as usize)
} }