Skip to content

Commit

Permalink
Merge pull request #627 from bgilbert/iso9660
Browse files Browse the repository at this point in the history
Some ISO 9660 refactoring
  • Loading branch information
bgilbert authored Sep 21, 2021
2 parents 741799e + 8f84ea4 commit 7db4782
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 146 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ serde = { version = "^1.0", features = ["derive"] }
serde_json = "^1.0"
structopt = "0.3"
tempfile = ">= 3.1, < 4"
thiserror = "1.0"
url = ">= 2.1, < 3.0"
uuid = { version = "^0.8", features = ["v4"] }
walkdir = "^2.3"
Expand Down
4 changes: 1 addition & 3 deletions src/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,7 @@ where
let byte_limit = saved.map(|saved| saved.get_offset()).transpose()?.flatten();
let mut limit_reader: Box<dyn Read> = match byte_limit {
None => Box::new(decompress_reader),
Some((limit, conflict)) => {
Box::new(LimitReader::new(decompress_reader, limit, Some(conflict)))
}
Some((limit, conflict)) => Box::new(LimitReader::new(decompress_reader, limit, conflict)),
};

// Read the first MiB of input and, if requested, check it against the
Expand Down
41 changes: 15 additions & 26 deletions src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,11 @@ pub struct LimitReader<R: Read> {
source: R,
length: u64,
remaining: u64,
conflict: Option<String>,
conflict: String,
}

impl<R: Read> LimitReader<R> {
pub fn new(source: R, length: u64, conflict: Option<String>) -> Self {
pub fn new(source: R, length: u64, conflict: String) -> Self {
Self {
source,
length,
Expand All @@ -238,17 +238,14 @@ impl<R: Read> Read for LimitReader<R> {
}
let allowed = self.remaining.min(buf.len() as u64);
if allowed == 0 {
return match self.conflict {
None => return Ok(0),
// reached the limit; only error if we're not at EOF
Some(ref msg) => match self.source.read(&mut buf[..1]) {
Ok(0) => Ok(0),
Ok(_) => Err(io::Error::new(
io::ErrorKind::Other,
format!("collision with {} at offset {}", msg, self.length),
)),
Err(e) => Err(e),
},
// reached the limit; only error if we're not at EOF
return match self.source.read(&mut buf[..1]) {
Ok(0) => Ok(0),
Ok(_) => Err(io::Error::new(
io::ErrorKind::Other,
format!("collision with {} at offset {}", self.conflict, self.length),
)),
Err(e) => Err(e),
};
}
let count = self.source.read(&mut buf[..allowed as usize])?;
Expand Down Expand Up @@ -478,7 +475,7 @@ mod tests {

// limit larger than file
let mut file = Cursor::new(data.clone());
let mut lim = LimitReader::new(&mut file, 150, Some("foo".into()));
let mut lim = LimitReader::new(&mut file, 150, "foo".into());
let mut buf = [0u8; 60];
assert_eq!(lim.read(&mut buf).unwrap(), 60);
assert_eq!(buf[..], data[0..60]);
Expand All @@ -488,7 +485,7 @@ mod tests {

// limit exactly equal to file
let mut file = Cursor::new(data.clone());
let mut lim = LimitReader::new(&mut file, 100, Some("foo".into()));
let mut lim = LimitReader::new(&mut file, 100, "foo".into());
let mut buf = [0u8; 60];
assert_eq!(lim.read(&mut buf).unwrap(), 60);
assert_eq!(buf[..], data[0..60]);
Expand All @@ -498,7 +495,7 @@ mod tests {

// buffer smaller than limit
let mut file = Cursor::new(data.clone());
let mut lim = LimitReader::new(&mut file, 90, Some("foo".into()));
let mut lim = LimitReader::new(&mut file, 90, "foo".into());
let mut buf = [0u8; 60];
assert_eq!(lim.read(&mut buf).unwrap(), 60);
assert_eq!(buf[..], data[0..60]);
Expand All @@ -511,7 +508,7 @@ mod tests {

// buffer exactly equal to limit
let mut file = Cursor::new(data.clone());
let mut lim = LimitReader::new(&mut file, 60, Some("foo".into()));
let mut lim = LimitReader::new(&mut file, 60, "foo".into());
let mut buf = [0u8; 60];
assert_eq!(lim.read(&mut buf).unwrap(), 60);
assert_eq!(buf[..], data[0..60]);
Expand All @@ -522,21 +519,13 @@ mod tests {

// buffer larger than limit
let mut file = Cursor::new(data.clone());
let mut lim = LimitReader::new(&mut file, 50, Some("foo".into()));
let mut lim = LimitReader::new(&mut file, 50, "foo".into());
let mut buf = [0u8; 60];
assert_eq!(lim.read(&mut buf).unwrap(), 50);
assert_eq!(buf[..50], data[0..50]);
assert_eq!(
lim.read(&mut buf).unwrap_err().to_string(),
"collision with foo at offset 50"
);

// test no collision
let mut file = Cursor::new(data.clone());
let mut lim = LimitReader::new(&mut file, 50, None);
let mut buf = [0u8; 60];
assert_eq!(lim.read(&mut buf).unwrap(), 50);
assert_eq!(buf[..50], data[0..50]);
assert_eq!(lim.read(&mut buf).unwrap(), 0);
}
}
Loading

0 comments on commit 7db4782

Please sign in to comment.