Treat non-existent data blocks as all zeroes
This commit is contained in:
parent
e55ad22620
commit
9326191911
@ -54,42 +54,47 @@ impl File {
|
||||
let start_block = (pos / self.block_size as u64) as u32;
|
||||
let offset = (pos % self.block_size as u64) as usize;
|
||||
let length = core::cmp::min(bytes_to_end, buf.len() as u64);
|
||||
let _ = fs
|
||||
.reader
|
||||
.read_block_offset(
|
||||
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)?;
|
||||
if let Some(phys_block) = self.inode.log_to_phys_block(fs, start_block) {
|
||||
let _ = fs
|
||||
.reader
|
||||
.read_block_offset(phys_block, offset as u32)?
|
||||
.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 block = start_block + 1;
|
||||
while remaining_bytes != 0 {
|
||||
let mut chunk_start_block = self
|
||||
.inode
|
||||
.log_to_phys_block(&fs, block)
|
||||
.ok_or_else(|| io::Error::new(io::ErrorKind::UnexpectedEof, "EOF reached"))?;
|
||||
let mut chunk_num_blocks = 1;
|
||||
block += 1;
|
||||
loop {
|
||||
if (chunk_num_blocks as usize * self.block_size) as u64 >= remaining_bytes {
|
||||
break;
|
||||
}
|
||||
let Some(phys_block) = self.inode.log_to_phys_block(&fs, block) else {
|
||||
break;
|
||||
};
|
||||
if phys_block != chunk_start_block + chunk_num_blocks {
|
||||
break;
|
||||
}
|
||||
chunk_num_blocks += 1;
|
||||
if let Some(chunk_start_block) = self.inode.log_to_phys_block(fs, block) {
|
||||
let mut chunk_num_blocks = 1;
|
||||
block += 1;
|
||||
loop {
|
||||
if (chunk_num_blocks as usize * self.block_size) as u64 >= remaining_bytes {
|
||||
break;
|
||||
}
|
||||
let Some(phys_block) = self.inode.log_to_phys_block(fs, block) else {
|
||||
break;
|
||||
};
|
||||
if phys_block != chunk_start_block + chunk_num_blocks {
|
||||
break;
|
||||
}
|
||||
chunk_num_blocks += 1;
|
||||
block += 1;
|
||||
}
|
||||
let _ = fs
|
||||
.reader
|
||||
.read_blocks(chunk_start_block, chunk_num_blocks 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);
|
||||
} 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);
|
||||
}
|
||||
let _ = fs
|
||||
.reader
|
||||
.read_blocks(chunk_start_block, chunk_num_blocks 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);
|
||||
}
|
||||
Ok((length - remaining_bytes) as usize)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user