You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by bo...@apache.org on 2013/01/20 20:10:25 UTC

svn commit: r1435913 - in /commons/proper/compress/trunk/src: changes/ main/java/org/apache/commons/compress/archivers/tar/ test/java/org/apache/commons/compress/archivers/tar/ test/resources/

Author: bodewig
Date: Sun Jan 20 19:10:24 2013
New Revision: 1435913

URL: http://svn.apache.org/viewvc?rev=1435913&view=rev
Log:
COMPRESS-206 read second Tar-EOF record as well, if there is one.  Based on patch by Peter De Maeyer

Added:
    commons/proper/compress/trunk/src/test/resources/archive_with_trailer.tar
      - copied, changed from r1435891, commons/proper/compress/trunk/src/test/resources/bla.tar
Modified:
    commons/proper/compress/trunk/src/changes/changes.xml
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarBuffer.java
    commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java

Modified: commons/proper/compress/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/changes/changes.xml?rev=1435913&r1=1435912&r2=1435913&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/changes/changes.xml (original)
+++ commons/proper/compress/trunk/src/changes/changes.xml Sun Jan 20 19:10:24 2013
@@ -133,6 +133,11 @@ The <action> type attribute can be add,u
           ZipFile's initialization has been improved for non-Zip64
           archives.
         </action>
+        <action type="fix" date="2013-01-20" issue="COMPRESS-206"
+                due-to="Peter De Maeyer">
+          TarArchiveInputStream could leave the second EOF record
+          inside the stream it had just finished reading.
+        </action>
     </release>
     <release version="1.4.1" date="2012-05-23"
              description="Release 1.4.1">

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java?rev=1435913&r1=1435912&r2=1435913&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java (original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java Sun Jan 20 19:10:24 2013
@@ -315,6 +315,7 @@ public class TarArchiveInputStream exten
             hasHitEOF = true;
         } else if (buffer.isEOFRecord(headerBuf)) {
             hasHitEOF = true;
+            buffer.tryToConsumeSecondEOFRecord();
         }
 
         return hasHitEOF ? null : headerBuf;

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarBuffer.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarBuffer.java?rev=1435913&r1=1435912&r2=1435913&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarBuffer.java (original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarBuffer.java Sun Jan 20 19:10:24 2013
@@ -405,4 +405,30 @@ class TarBuffer { // Not public, because
             inStream = null;
         }
     }
+
+    /**
+     * Tries to read the next record rewinding the stream if if is not a EOF record.
+     *
+     * <p>This is meant to protect against cases where a tar
+     * implemenation has written only one EOF record when two are
+     * expected.  Actually this won't help since a non-conforming
+     * implementation likely won't fill full blocks consisting of - be
+     * default - ten records either so we probably have already read
+     * beyond the archive anyway.</p>
+     */
+    void tryToConsumeSecondEOFRecord() throws IOException {
+        boolean shouldReset = true;
+        boolean marked = inStream.markSupported();
+        if (marked) {
+            inStream.mark(recordSize);
+        }
+        try {
+            shouldReset = !isEOFRecord(readRecord());
+        } finally {
+            if (shouldReset && marked) {
+                inStream.reset();
+            }
+        }
+    }
+
 }

Modified: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java?rev=1435913&r1=1435912&r2=1435913&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java (original)
+++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java Sun Jan 20 19:10:24 2013
@@ -18,6 +18,7 @@
 
 package org.apache.commons.compress.archivers.tar;
 
+import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
@@ -26,6 +27,7 @@ import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.InputStream;
 import java.io.IOException;
 import java.net.URI;
 import java.net.URL;
@@ -164,6 +166,23 @@ public class TarArchiveInputStreamTest {
         tis.close();
     }
 
+    @Test
+    public void shouldConsumeArchiveCompletely() throws Exception {
+        InputStream is = TarArchiveInputStreamTest.class
+            .getResourceAsStream("/archive_with_trailer.tar");
+        TarArchiveInputStream tar = new TarArchiveInputStream(is);
+        while (tar.getNextTarEntry() != null) {
+            // just consume the archive
+            ;
+        }
+        byte[] expected = new byte[] {
+            'H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!', '\n'
+        };
+        byte[] actual = new byte[expected.length];
+        is.read(actual);
+        assertArrayEquals(expected, actual);
+    }
+
     private TarArchiveInputStream getTestStream(String name) {
         return new TarArchiveInputStream(
                 TarArchiveInputStreamTest.class.getResourceAsStream(name));

Copied: commons/proper/compress/trunk/src/test/resources/archive_with_trailer.tar (from r1435891, commons/proper/compress/trunk/src/test/resources/bla.tar)
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/resources/archive_with_trailer.tar?p2=commons/proper/compress/trunk/src/test/resources/archive_with_trailer.tar&p1=commons/proper/compress/trunk/src/test/resources/bla.tar&r1=1435891&r2=1435913&rev=1435913&view=diff
==============================================================================
Binary files - no diff available.