Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add finisher drop implementation to BzEncoder #121

Merged
merged 5 commits into from
Jan 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 52 additions & 6 deletions src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub struct BzEncoder<W: Write> {
obj: Option<W>,
buf: Vec<u8>,
done: bool,
panicked: bool,
}

/// A compression stream which will have compressed data written to it and
Expand All @@ -21,6 +22,7 @@ pub struct BzDecoder<W: Write> {
obj: Option<W>,
buf: Vec<u8>,
done: bool,
panicked: bool,
}

impl<W: Write> BzEncoder<W> {
Expand All @@ -32,17 +34,21 @@ impl<W: Write> BzEncoder<W> {
obj: Some(obj),
buf: Vec::with_capacity(32 * 1024),
done: false,
panicked: false,
}
}

fn dump(&mut self) -> io::Result<()> {
while !self.buf.is_empty() {
let n = match self.obj.as_mut().unwrap().write(&self.buf) {
Ok(n) => n,
self.panicked = true;
let r = self.obj.as_mut().unwrap().write(&self.buf);
self.panicked = false;

match r {
Ok(n) => self.buf.drain(..n),
Err(ref err) if err.kind() == io::ErrorKind::Interrupted => continue,
Err(err) => return Err(err),
};
self.buf.drain(..n);
}
Ok(())
}
Expand Down Expand Up @@ -162,6 +168,7 @@ impl<W: Write> BzDecoder<W> {
obj: Some(obj),
buf: Vec::with_capacity(32 * 1024),
done: false,
panicked: false,
}
}

Expand All @@ -180,12 +187,15 @@ impl<W: Write> BzDecoder<W> {

fn dump(&mut self) -> io::Result<()> {
while !self.buf.is_empty() {
let n = match self.obj.as_mut().unwrap().write(&self.buf) {
Ok(n) => n,
self.panicked = true;
let r = self.obj.as_mut().unwrap().write(&self.buf);
self.panicked = false;

match r {
Ok(n) => self.buf.drain(..n),
Err(ref err) if err.kind() == io::ErrorKind::Interrupted => continue,
Err(err) => return Err(err),
};
self.buf.drain(..n);
}
Ok(())
}
Expand Down Expand Up @@ -287,6 +297,14 @@ impl<W: Write> Drop for BzDecoder<W> {
}
}

impl<W: Write> Drop for BzEncoder<W> {
fn drop(&mut self) {
if self.obj.is_some() && !self.panicked {
let _ = self.try_finish();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::io::BufWriter skips the flush on Drop if a panic happened while writing to the inner writer. It does so by having a panicked field which is set to true before writing to the inner writer and set to false immediately after writing. This avoids a double panic if the inner writer would panic on the second write too.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you implying that we should be doing something similar here?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to do so yeah.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not understand what you want me to tell here or how to apply the panicked field in this implementation.

}
}
}

#[cfg(test)]
mod tests {
use super::{BzDecoder, BzEncoder};
Expand Down Expand Up @@ -383,4 +401,32 @@ mod tests {
.into_inner()
}
}

#[test]
fn terminate_on_drop() {
// Test that dropping the BzEncoder flushes bytes to the output, so that
// we get a valid, decompressable datastream
//
// see https://github.com/trifectatechfoundation/bzip2-rs/pull/121
let s = "12345".repeat(100);

let mut compressed = Vec::new();
{
let mut c: Box<dyn std::io::Write> =
Box::new(BzEncoder::new(&mut compressed, Compression::default()));
c.write_all(b"12834").unwrap();
c.write_all(s.as_bytes()).unwrap();
c.flush().unwrap();
}
assert!(!compressed.is_empty());

let uncompressed = {
let mut d = BzDecoder::new(Vec::new());
d.write_all(&compressed).unwrap();
d.finish().unwrap()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without the Drop implementation, this line fails with:

called `Result::unwrap()` on an `Err` value: Custom { kind: UnexpectedEof, error: "Input EOF reached before logical stream end" }

};
assert_eq!(&uncompressed[0..5], b"12834");
assert_eq!(uncompressed.len(), s.len() + "12834".len());
assert!(format!("12834{}", s).as_bytes() == &*uncompressed);
}
}
Loading