Author: bodewig
Date: Thu Dec 27 21:34:35 2012
New Revision: 1426334

URL: http://svn.apache.org/viewvc?rev=1426334&view=rev
Log:
COMPRESS-203 the "file name" of a PAX header must not end with a slash or it is 
mistaken as a directory

Modified:
    commons/proper/compress/trunk/src/changes/changes.xml
    
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStream.java
    
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStreamTest.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=1426334&r1=1426333&r2=1426334&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/changes/changes.xml (original)
+++ commons/proper/compress/trunk/src/changes/changes.xml Thu Dec 27 21:34:35 
2012
@@ -74,6 +74,10 @@ The <action> type attribute can be add,u
         would write unreadable names in GNU mode or even cause errors
         in POSIX mode for file names longer than 66 characters.
       </action>
+      <action type="fix" date="2012-12-27" issue="COMPRESS-203">
+        Writing TAR PAX headers failed if the generated entry name
+        ended with a "/".
+      </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/TarArchiveOutputStream.java
URL: 
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStream.java?rev=1426334&r1=1426333&r2=1426334&view=diff
==============================================================================
--- 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStream.java
 (original)
+++ 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStream.java
 Thu Dec 27 21:34:35 2012
@@ -454,6 +454,11 @@ public class TarArchiveOutputStream exte
     void writePaxHeaders(String entryName,
                          Map<String, String> headers) throws IOException {
         String name = "./PaxHeaders.X/" + stripTo7Bits(entryName);
+        while (name.endsWith("/")) {
+            // TarEntry's constructor would think this is a directory
+            // and not allow any data to be written
+            name = name.substring(0, name.length() - 1);
+        }
         if (name.length() >= TarConstants.NAMELEN) {
             name = name.substring(0, TarConstants.NAMELEN - 1);
         }

Modified: 
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStreamTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStreamTest.java?rev=1426334&r1=1426333&r2=1426334&view=diff
==============================================================================
--- 
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStreamTest.java
 (original)
+++ 
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStreamTest.java
 Thu Dec 27 21:34:35 2012
@@ -372,4 +372,59 @@ public class TarArchiveOutputStreamTest 
         tin.close();
     }
 
+    /**
+     * @see https://issues.apache.org/jira/browse/COMPRESS-203
+     */
+    public void testWriteLongDirectoryNameGnuMode() throws Exception {
+        testWriteLongDirectoryName(TarArchiveOutputStream.LONGFILE_GNU);
+    }
+
+    /**
+     * @see https://issues.apache.org/jira/browse/COMPRESS-203
+     */
+    public void testWriteLongDirectoryNamePosixMode() throws Exception {
+        testWriteLongDirectoryName(TarArchiveOutputStream.LONGFILE_POSIX);
+    }
+
+    private void testWriteLongDirectoryName(int mode) throws Exception {
+        String n = "01234567890123456789012345678901234567890123456789"
+            + "01234567890123456789012345678901234567890123456789"
+            + "01234567890123456789012345678901234567890123456789/";
+        TarArchiveEntry t = new TarArchiveEntry(n);
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        TarArchiveOutputStream tos = new TarArchiveOutputStream(bos, "ASCII");
+        tos.setLongFileMode(mode);
+        tos.putArchiveEntry(t);
+        tos.closeArchiveEntry();
+        tos.close();
+        byte[] data = bos.toByteArray();
+        TarArchiveInputStream tin =
+            new TarArchiveInputStream(new ByteArrayInputStream(data));
+        TarArchiveEntry e = tin.getNextTarEntry();
+        assertEquals(n, e.getName());
+        assertTrue(e.isDirectory());
+        tin.close();
+    }
+
+    /**
+     * @see https://issues.apache.org/jira/browse/COMPRESS-203
+     */
+    public void testWriteNonAsciiDirectoryNamePosixMode() throws Exception {
+        String n = "f\u00f6\u00f6/";
+        TarArchiveEntry t = new TarArchiveEntry(n);
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        TarArchiveOutputStream tos = new TarArchiveOutputStream(bos);
+        tos.setAddPaxHeadersForNonAsciiNames(true);
+        tos.putArchiveEntry(t);
+        tos.closeArchiveEntry();
+        tos.close();
+        byte[] data = bos.toByteArray();
+        TarArchiveInputStream tin =
+            new TarArchiveInputStream(new ByteArrayInputStream(data));
+        TarArchiveEntry e = tin.getNextTarEntry();
+        assertEquals(n, e.getName());
+        assertTrue(e.isDirectory());
+        tin.close();
+    }
+
 }


Reply via email to