From 3e187a026408d5ca74202f7e26dd565cde2e87d0 Mon Sep 17 00:00:00 2001 From: Kanstantsin Shautsou Date: Thu, 23 Apr 2015 23:45:19 +0300 Subject: [PATCH] [FIXED JENKINS-10629] Unbroke stream with new tar implementation. Causes bytes lost and truncated tar archive. TarBuffer not used in TarArchiveOutputStream. --- .../main/java/hudson/util/io/TarArchiver.java | 10 +----- core/src/test/java/hudson/FilePathTest.java | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/core/src/main/java/hudson/util/io/TarArchiver.java b/core/src/main/java/hudson/util/io/TarArchiver.java index 4cfa5ce987..600a16cba6 100644 --- a/core/src/main/java/hudson/util/io/TarArchiver.java +++ b/core/src/main/java/hudson/util/io/TarArchiver.java @@ -52,15 +52,7 @@ final class TarArchiver extends Archiver { private final TarArchiveOutputStream tar; TarArchiver(OutputStream out) { - tar = new TarArchiveOutputStream(new BufferedOutputStream(out) { - // TarOutputStream uses TarBuffer internally, - // which flushes the stream for each block. this creates unnecessary - // data stream fragmentation, and flush request to a remote, which slows things down. - @Override - public void flush() throws IOException { - // so don't do anything in flush - } - }); + tar = new TarArchiveOutputStream(out); tar.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_STAR); tar.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); } diff --git a/core/src/test/java/hudson/FilePathTest.java b/core/src/test/java/hudson/FilePathTest.java index 801e184906..43f48eafb9 100644 --- a/core/src/test/java/hudson/FilePathTest.java +++ b/core/src/test/java/hudson/FilePathTest.java @@ -659,4 +659,35 @@ public class FilePathTest { // test conflict subdir src.moveAllChildrenTo(dst); } + + @Issue("JENKINS-10629") + @Test + public void testEOFbrokenFlush() throws IOException, InterruptedException { + final File srcFolder = temp.newFolder("src"); + // simulate magic structure with magic sizes: + // |- dir/pom.xml (2049) + // |- pom.xml (2049) + // \- small.tar (1537) + final File smallTar = new File(srcFolder, "small.tar"); + givenSomeContentInFile(smallTar, 1537); + final File dir = new File(srcFolder, "dir"); + dir.mkdirs(); + final File pomFile = new File(dir, "pom.xml"); + givenSomeContentInFile(pomFile, 2049); + FileUtils.copyFileToDirectory(pomFile, srcFolder); + + final File archive = temp.newFile("archive.tar"); + + // Compress archive + final FilePath tmpDirPath = new FilePath(srcFolder); + int tarred = tmpDirPath.tar(new FileOutputStream(archive), "**"); + assertEquals("One file should have been compressed", 3, tarred); + + // Decompress + final File dstFolder = temp.newFolder("dst"); + dstFolder.mkdirs(); + FilePath outDir = new FilePath(dstFolder); + // and now fail when flush is bad! + tmpDirPath.child("../" + archive.getName()).untar(outDir, TarCompression.NONE); + } }