Verified Commit 542d0a9f authored by Jeremy Soller's avatar Jeremy Soller
Browse files

Make PackageSrc::read_at always read buf.len bytes

parent 2bf58075
......@@ -8,11 +8,11 @@ use crate::{Entry, Error, HEADER_SIZE, Header};
pub trait PackageSrc {
type Err: From<Error>;
fn read_at(&mut self, offset: u64, buf: &mut [u8]) -> Result<usize, Self::Err>;
fn header(&self) -> Header;
/// Users of implementors of `PackageSrc` should use `header` instead of `read_header` for
/// cheap header access.
/// Implementors of `PackageSrc` should call this function during initialization and store
......@@ -23,7 +23,7 @@ pub trait PackageSrc {
let header = Header::new(&header_data, &public_key)?;
Ok(header.clone())
}
fn read_entries(&mut self) -> Result<Vec<Entry>, Self::Err> {
let header = self.header();
let entries_size = header.entries_size()
......@@ -35,25 +35,25 @@ pub trait PackageSrc {
let entries = header.entries(&entries_data)?;
Ok(entries.to_vec())
}
/// Read from this src at a given entry's data with a given offset within that entry
fn read_entry(&mut self, entry: Entry, offset: usize, buf: &mut [u8]) -> Result<usize, Self::Err> {
if offset as u64 > entry.size {
return Ok(0);
}
let mut end = usize::try_from(entry.size - offset as u64)
.map_err(Error::TryFromInt)?;
if end > buf.len() {
end = buf.len();
}
let offset =
HEADER_SIZE as u64 +
self.header().entries_size()? +
entry.offset + offset as u64;
self.read_at(offset as u64, &mut buf[..end])
}
}
......@@ -78,11 +78,11 @@ impl<'a> PackageBuf<'a> {
impl PackageSrc for PackageBuf<'_> {
type Err = Error;
fn header(&self) -> Header {
self.header
}
fn read_at(&mut self, offset: u64, buf: &mut [u8]) -> Result<usize, Error> {
let start = usize::try_from(offset)
.map_err(Error::TryFromInt)?;
......@@ -96,7 +96,6 @@ impl PackageSrc for PackageBuf<'_> {
end = len;
}
buf.copy_from_slice(&self.src[start..end]);
Ok(end.checked_sub(start).unwrap())
Ok(buf.len())
}
}
......@@ -51,7 +51,8 @@ impl PackageSrc for PackageFile {
fn read_at(&mut self, offset: u64, buf: &mut [u8]) -> Result<usize, Self::Err> {
self.src.seek(SeekFrom::Start(offset))?;
Ok(self.src.read(buf)?)
self.src.read_exact(buf)?;
Ok(buf.len())
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment