Skip to content

Buffer writes to archive entries #4

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

Merged
merged 1 commit into from
Feb 3, 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
18 changes: 13 additions & 5 deletions src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{
borrow::Cow,
cmp,
collections::VecDeque,
convert::TryFrom,
fmt,
io::{Error, ErrorKind, SeekFrom},
marker,
Expand All @@ -16,7 +17,7 @@ use std::{
use tokio::{
fs,
fs::{remove_dir_all, remove_file, OpenOptions},
io::{self, AsyncRead as Read, AsyncReadExt, AsyncSeekExt},
io::{self, AsyncRead as Read, AsyncReadExt, AsyncSeekExt, AsyncWriteExt},
};

/// A read-only view into an entry of an archive.
Expand Down Expand Up @@ -689,22 +690,29 @@ impl<R: Read + Unpin> EntryFields<R> {
}
}
}?;

let size = usize::try_from(self.size).unwrap_or(usize::MAX);
let capacity = cmp::min(size, 128 * 1024);
let mut writer = io::BufWriter::with_capacity(capacity, &mut f);
for io in self.data.drain(..) {
match io {
EntryIo::Data(mut d) => {
let expected = d.limit();
if io::copy(&mut d, &mut f).await? != expected {
if io::copy(&mut d, &mut writer).await? != expected {
return Err(other("failed to write entire file"));
}
}
EntryIo::Pad(d) => {
// TODO: checked cast to i64
let to = SeekFrom::Current(d.limit() as i64);
let size = f.seek(to).await?;
f.set_len(size).await?;
let pad_len = d.limit() as i64;
writer.flush().await?;
let f = writer.get_mut();
let new_size = f.seek(SeekFrom::Current(pad_len)).await?;
f.set_len(new_size).await?;
Comment on lines -702 to +711
Copy link
Member

Choose a reason for hiding this comment

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

Is there documentation on when we need this padding and how the seek/set_len trick works?

}
}
}
writer.flush().await?;
Ok::<fs::File, io::Error>(f)
}
.await
Expand Down