From d31226996bc1fb5bb89ed7238a229439539e5e29 Mon Sep 17 00:00:00 2001 From: GabrielDertoni Date: Sun, 5 Jan 2025 12:19:57 -0300 Subject: [PATCH 1/2] use generics instead of dynamic dispatch internally in tar::Builder --- src/builder.rs | 47 +++++++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index 54413605..2f906591 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -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( + 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(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 { @@ -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( + dst: &mut W, path: &Path, name: Option<&Path>, options: BuilderOptions, @@ -641,8 +645,8 @@ fn append_path_with_name( } #[cfg(unix)] -fn append_special( - dst: &mut dyn Write, +fn append_special( + dst: &mut W, path: &Path, stat: &fs::Metadata, mode: HeaderMode, @@ -684,8 +688,8 @@ fn append_special( Ok(()) } -fn append_file( - dst: &mut dyn Write, +fn append_file( + dst: &mut W, path: &Path, file: &mut fs::File, options: BuilderOptions, @@ -718,8 +722,8 @@ fn append_file( Ok(()) } -fn append_dir( - dst: &mut dyn Write, +fn append_dir( + dst: &mut W, path: &Path, src_path: &Path, options: BuilderOptions, @@ -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( + 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 @@ -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( + dst: &mut W, header: &mut Header, link_name: &Path, ) -> io::Result<()> { @@ -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( + 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 @@ -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( + dst: &mut W, path: &Path, meta: &fs::Metadata, mode: HeaderMode, @@ -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( + dst: &mut W, path: &Path, src_path: &Path, options: BuilderOptions, From b47bf84c3f388d08a6ac30192fa80cfdaf7ee17e Mon Sep 17 00:00:00 2001 From: GabrielDertoni Date: Sun, 12 Jan 2025 16:48:13 -0300 Subject: [PATCH 2/2] fix test to not read archive while writing to it --- tests/all.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/all.rs b/tests/all.rs index 0ad67f98..2ac495b8 100644 --- a/tests/all.rs +++ b/tests/all.rs @@ -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> = Builder::new(BufWriter::new(file)); @@ -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);