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

use generics instead of dynamic dispatch internally in tar::Builder #395

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
47 changes: 29 additions & 18 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,14 +583,18 @@ impl Drop for EntryWriter<'_> {
}
}

fn append(mut dst: &mut dyn Write, header: &Header, mut data: &mut dyn Read) -> io::Result<()> {
fn append<W: Write + ?Sized>(
mut dst: &mut W,
header: &Header,
mut data: &mut dyn Read,
) -> io::Result<()> {
dst.write_all(header.as_bytes())?;
let len = io::copy(&mut data, &mut dst)?;
pad_zeroes(&mut dst, len)?;
Ok(())
}

fn pad_zeroes(dst: &mut dyn Write, len: u64) -> io::Result<()> {
fn pad_zeroes<W: Write + ?Sized>(dst: &mut W, len: u64) -> io::Result<()> {
let buf = [0; BLOCK_SIZE as usize];
let remaining = BLOCK_SIZE - (len % BLOCK_SIZE);
if remaining < BLOCK_SIZE {
Expand All @@ -599,8 +603,8 @@ fn pad_zeroes(dst: &mut dyn Write, len: u64) -> io::Result<()> {
Ok(())
}

fn append_path_with_name(
dst: &mut dyn Write,
fn append_path_with_name<W: Write + ?Sized>(
dst: &mut W,
path: &Path,
name: Option<&Path>,
options: BuilderOptions,
Expand Down Expand Up @@ -641,8 +645,8 @@ fn append_path_with_name(
}

#[cfg(unix)]
fn append_special(
dst: &mut dyn Write,
fn append_special<W: Write + ?Sized>(
dst: &mut W,
path: &Path,
stat: &fs::Metadata,
mode: HeaderMode,
Expand Down Expand Up @@ -684,8 +688,8 @@ fn append_special(
Ok(())
}

fn append_file(
dst: &mut dyn Write,
fn append_file<W: Write + ?Sized>(
dst: &mut W,
path: &Path,
file: &mut fs::File,
options: BuilderOptions,
Expand Down Expand Up @@ -718,8 +722,8 @@ fn append_file(
Ok(())
}

fn append_dir(
dst: &mut dyn Write,
fn append_dir<W: Write + ?Sized>(
dst: &mut W,
path: &Path,
src_path: &Path,
options: BuilderOptions,
Expand All @@ -743,7 +747,11 @@ fn prepare_header(size: u64, entry_type: u8) -> Header {
header
}

fn prepare_header_path(dst: &mut dyn Write, header: &mut Header, path: &Path) -> io::Result<()> {
fn prepare_header_path<W: Write + ?Sized>(
dst: &mut W,
header: &mut Header,
path: &Path,
) -> io::Result<()> {
// Try to encode the path directly in the header, but if it ends up not
// working (probably because it's too long) then try to use the GNU-specific
// long name extension by emitting an entry which indicates that it's the
Expand Down Expand Up @@ -775,8 +783,8 @@ fn prepare_header_path(dst: &mut dyn Write, header: &mut Header, path: &Path) ->
Ok(())
}

fn prepare_header_link(
dst: &mut dyn Write,
fn prepare_header_link<W: Write + ?Sized>(
dst: &mut W,
header: &mut Header,
link_name: &Path,
) -> io::Result<()> {
Expand Down Expand Up @@ -823,7 +831,10 @@ fn prepare_header_sparse(
}

/// Write extra sparse headers into `dst` for those entries that did not fit in the main header.
fn append_extended_sparse_headers(dst: &mut dyn Write, entries: &SparseEntries) -> io::Result<()> {
fn append_extended_sparse_headers<W: Write + ?Sized>(
dst: &mut W,
entries: &SparseEntries,
) -> io::Result<()> {
// The first `GNU_SPARSE_HEADERS_COUNT` entries are written to the main header, so skip them.
let mut it = entries
.entries
Expand All @@ -850,8 +861,8 @@ fn append_extended_sparse_headers(dst: &mut dyn Write, entries: &SparseEntries)
Ok(())
}

fn append_fs(
dst: &mut dyn Write,
fn append_fs<W: Write + ?Sized>(
dst: &mut W,
path: &Path,
meta: &fs::Metadata,
mode: HeaderMode,
Expand All @@ -868,8 +879,8 @@ fn append_fs(
dst.write_all(header.as_bytes())
}

fn append_dir_all(
dst: &mut dyn Write,
fn append_dir_all<W: Write + ?Sized>(
dst: &mut W,
path: &Path,
src_path: &Path,
options: BuilderOptions,
Expand Down
4 changes: 3 additions & 1 deletion tests/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,8 @@ fn pax_simple() {
fn pax_simple_write() {
let td = t!(TempBuilder::new().prefix("tar-rs").tempdir());
let pax_path = td.path().join("pax.tar");
let test2 = td.path().join("test2");
t!(std::fs::write(&test2, "Hello, world"));
let file: File = t!(File::create(&pax_path));
let mut ar: Builder<BufWriter<File>> = Builder::new(BufWriter::new(file));

Expand All @@ -967,7 +969,7 @@ fn pax_simple_write() {
];

t!(ar.append_pax_extensions(pax_extensions));
t!(ar.append_file("test2", &mut t!(File::open(&pax_path))));
t!(ar.append_file("test2", &mut t!(File::open(&test2))));
t!(ar.finish());
drop(ar);

Expand Down
Loading