Skip to content

Commit

Permalink
refactor: Use new do_or_abort_file method
Browse files Browse the repository at this point in the history
  • Loading branch information
Pr0methean committed Jun 15, 2024
1 parent e23f676 commit 405d923
Showing 1 changed file with 22 additions and 42 deletions.
64 changes: 22 additions & 42 deletions src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ impl<W: Write + Seek> Write for ZipWriter<W> {
if self.stats.bytes_written > spec::ZIP64_BYTES_THR
&& !self.files.last_mut().unwrap().1.large_file
{
self.abort_file().unwrap();
let _ = self.abort_file();
return Err(io::Error::new(
io::ErrorKind::Other,
"Large file option has not been set",
Expand Down Expand Up @@ -739,10 +739,7 @@ impl<A: Read + Write + Seek> ZipWriter<A> {

self.writing_to_file = true;
self.writing_raw = true;
if let Err(e) = self.write_all(&copy) {
self.abort_file().unwrap();
return Err(e.into());
}
self.do_or_abort_file(|| self.write_all(&copy))?;

Check failure on line 742 in src/write.rs

View workflow job for this annotation

GitHub Actions / style_and_docs (--all-features)

the trait bound `std::result::Result<_, result::ZipError>: std::convert::From<std::result::Result<(), std::io::Error>>` is not satisfied

Check failure on line 742 in src/write.rs

View workflow job for this annotation

GitHub Actions / Build and test --no-default-features: macOS-latest, msrv

the trait bound `Result<_, ZipError>: From<Result<(), std::io::Error>>` is not satisfied

Check failure on line 742 in src/write.rs

View workflow job for this annotation

GitHub Actions / Build and test : macOS-latest, msrv

the trait bound `Result<_, ZipError>: From<Result<(), std::io::Error>>` is not satisfied

Check failure on line 742 in src/write.rs

View workflow job for this annotation

GitHub Actions / style_and_docs (--all-features)

the trait bound `std::result::Result<_, result::ZipError>: std::convert::From<std::result::Result<(), std::io::Error>>` is not satisfied
self.finish_file()
}

Expand Down Expand Up @@ -845,6 +842,14 @@ impl<W: Write + Seek> ZipWriter<W> {
&self.comment
}

fn do_or_abort_file<T, R: Into<ZipResult<T>>, F: FnOnce() -> R>(&mut self, method: F) -> ZipResult<T> {
let result = method();
if result.is_err() {

Check failure on line 847 in src/write.rs

View workflow job for this annotation

GitHub Actions / style_and_docs (--all-features)

no method named `is_err` found for type parameter `R` in the current scope

Check failure on line 847 in src/write.rs

View workflow job for this annotation

GitHub Actions / Build and test --no-default-features: macOS-latest, msrv

no method named `is_err` found for type parameter `R` in the current scope

Check failure on line 847 in src/write.rs

View workflow job for this annotation

GitHub Actions / Build and test : macOS-latest, msrv

no method named `is_err` found for type parameter `R` in the current scope

Check failure on line 847 in src/write.rs

View workflow job for this annotation

GitHub Actions / style_and_docs (--all-features)

no method named `is_err` found for type parameter `R` in the current scope
let _ = self.abort_file();
}
result.into()
}

/// Start a new file for with the requested options.
fn start_entry<S, SToOwned, T: FileOptionExtension>(
&mut self,
Expand Down Expand Up @@ -910,21 +915,7 @@ impl<W: Write + Seek> ZipWriter<W> {
let index = self.insert_file_data(file)?;
let file = &mut self.files[index];
let writer = self.inner.get_plain();

let block = match file.local_block() {
Ok(block) => block,
Err(e) => {
let _ = self.abort_file();
return Err(e);
}
};
match block.write(writer) {
Ok(()) => (),
Err(e) => {
let _ = self.abort_file();
return Err(e);
}
}
self.do_or_abort_file(|| file.local_block()?.write(writer))?;

Check failure on line 918 in src/write.rs

View workflow job for this annotation

GitHub Actions / style_and_docs (--all-features)

cannot borrow `*self` as mutable more than once at a time

Check failure on line 918 in src/write.rs

View workflow job for this annotation

GitHub Actions / style_and_docs (--all-features)

cannot borrow `*self` as mutable more than once at a time
// file name
writer.write_all(&file.file_name_raw)?;
let zip64_start = writer.stream_position()?;
Expand Down Expand Up @@ -955,21 +946,19 @@ impl<W: Write + Seek> ZipWriter<W> {
}
let extra_data_len = extra_data.len();
if extra_data_len > 0 {
writer.write_all(&extra_data)?;
extra_data_end = writer.stream_position()?;
debug_assert_eq!(extra_data_end % (options.alignment.max(1) as u64), 0);
self.stats.start = extra_data_end;
ExtendedFileOptions::validate_extra_data(&extra_data, header_end - zip64_start)?;
self.do_or_abort_file(|| {

Check failure on line 949 in src/write.rs

View workflow job for this annotation

GitHub Actions / style_and_docs (--all-features)

cannot borrow `*self` as mutable more than once at a time

Check failure on line 949 in src/write.rs

View workflow job for this annotation

GitHub Actions / style_and_docs (--all-features)

closure requires unique access to `self.stats.start` but it is already borrowed

Check failure on line 949 in src/write.rs

View workflow job for this annotation

GitHub Actions / style_and_docs (--all-features)

cannot borrow `*self` as mutable more than once at a time

Check failure on line 949 in src/write.rs

View workflow job for this annotation

GitHub Actions / style_and_docs (--all-features)

closure requires unique access to `self.stats.start` but it is already borrowed
writer.write_all(&extra_data)?;
extra_data_end = writer.stream_position()?;
debug_assert_eq!(extra_data_end % (options.alignment.max(1) as u64), 0);
self.stats.start = extra_data_end;
ExtendedFileOptions::validate_extra_data(&extra_data, header_end - zip64_start)
})?;
file.extra_field = Some(extra_data.into());
} else {
self.stats.start = extra_data_end;
}
if let Some(data) = central_extra_data {
let validation_result = ExtendedFileOptions::validate_extra_data(&data, extra_data_end - zip64_start);
if validation_result.is_err() {
self.abort_file()?;
return validation_result;
}
self.do_or_abort_file(|| ExtendedFileOptions::validate_extra_data(&data, extra_data_end - zip64_start))?;

Check failure on line 961 in src/write.rs

View workflow job for this annotation

GitHub Actions / style_and_docs (--all-features)

cannot borrow `*self` as mutable more than once at a time

Check failure on line 961 in src/write.rs

View workflow job for this annotation

GitHub Actions / style_and_docs (--all-features)

cannot borrow `*self` as mutable more than once at a time
file.central_extra_field = Some(data.clone());
}
debug_assert!(file.data_start.get().is_none());
Expand Down Expand Up @@ -1062,10 +1051,7 @@ impl<W: Write + Seek> ZipWriter<W> {
writer.seek(SeekFrom::Start(file_end))?;
}
if self.flush_on_finish_file {
if let Err(e) = writer.flush() {
self.abort_file()?;
return Err(e.into());
}
self.do_or_abort_file(|| writer.flush())?;

Check failure on line 1054 in src/write.rs

View workflow job for this annotation

GitHub Actions / style_and_docs (--all-features)

the trait bound `std::result::Result<_, result::ZipError>: std::convert::From<std::result::Result<(), std::io::Error>>` is not satisfied

Check failure on line 1054 in src/write.rs

View workflow job for this annotation

GitHub Actions / Build and test --no-default-features: macOS-latest, msrv

the trait bound `Result<_, ZipError>: From<Result<(), std::io::Error>>` is not satisfied

Check failure on line 1054 in src/write.rs

View workflow job for this annotation

GitHub Actions / Build and test : macOS-latest, msrv

the trait bound `Result<_, ZipError>: From<Result<(), std::io::Error>>` is not satisfied

Check failure on line 1054 in src/write.rs

View workflow job for this annotation

GitHub Actions / style_and_docs (--all-features)

the trait bound `std::result::Result<_, result::ZipError>: std::convert::From<std::result::Result<(), std::io::Error>>` is not satisfied
}

self.writing_to_file = false;
Expand Down Expand Up @@ -1142,10 +1128,7 @@ impl<W: Write + Seek> ZipWriter<W> {
options.zopfli_buffer_size,
)?;
self.start_entry(name, options, None)?;
if let Err(e) = self.inner.switch_to(make_new_self) {
self.abort_file().unwrap();
return Err(e);
}
self.do_or_abort_file(|| self.inner.switch_to(make_new_self))?;

Check failure on line 1131 in src/write.rs

View workflow job for this annotation

GitHub Actions / style_and_docs (--all-features)

cannot borrow `*self` as mutable because previous closure requires unique access

Check failure on line 1131 in src/write.rs

View workflow job for this annotation

GitHub Actions / style_and_docs (--all-features)

closure requires unique access to `self.inner` but it is already borrowed

Check failure on line 1131 in src/write.rs

View workflow job for this annotation

GitHub Actions / style_and_docs (--all-features)

cannot borrow `*self` as mutable because previous closure requires unique access

Check failure on line 1131 in src/write.rs

View workflow job for this annotation

GitHub Actions / style_and_docs (--all-features)

closure requires unique access to `self.inner` but it is already borrowed
self.writing_raw = false;
Ok(())
}
Expand Down Expand Up @@ -1421,10 +1404,7 @@ impl<W: Write + Seek> ZipWriter<W> {

self.start_entry(name, options, None)?;
self.writing_to_file = true;
if let Err(e) = self.write_all(target.into().as_bytes()) {
self.abort_file().unwrap();
return Err(e.into());
}
self.do_or_abort_file(|| self.write_all(target.into().as_bytes()))?;

Check failure on line 1407 in src/write.rs

View workflow job for this annotation

GitHub Actions / style_and_docs (--all-features)

the trait bound `std::result::Result<_, result::ZipError>: std::convert::From<std::result::Result<(), std::io::Error>>` is not satisfied

Check failure on line 1407 in src/write.rs

View workflow job for this annotation

GitHub Actions / Build and test --no-default-features: macOS-latest, msrv

the trait bound `Result<_, ZipError>: From<Result<(), std::io::Error>>` is not satisfied

Check failure on line 1407 in src/write.rs

View workflow job for this annotation

GitHub Actions / Build and test : macOS-latest, msrv

the trait bound `Result<_, ZipError>: From<Result<(), std::io::Error>>` is not satisfied

Check failure on line 1407 in src/write.rs

View workflow job for this annotation

GitHub Actions / style_and_docs (--all-features)

the trait bound `std::result::Result<_, result::ZipError>: std::convert::From<std::result::Result<(), std::io::Error>>` is not satisfied
self.writing_raw = false;
self.finish_file()?;

Expand Down

0 comments on commit 405d923

Please sign in to comment.