Author: bodewig
Date: Sat Feb 22 07:00:42 2014
New Revision: 1570800

URL: http://svn.apache.org/r1570800
Log:
COMPRESS-265 tariling backslashes are as bad for PAX headers as trailing 
slashes - on Windows

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=1570800&r1=1570799&r2=1570800&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/changes/changes.xml (original)
+++ commons/proper/compress/trunk/src/changes/changes.xml Sat Feb 22 07:00:42 
2014
@@ -72,6 +72,11 @@ The <action> type attribute can be add,u
         bytes of a STORED entry if it was the very first entry of the
         archive.
       </action>
+      <action issue="COMPRESS-265" type="fix" date="2014-02-22">
+        When writing PAX/POSIX headers for TAR entries with
+        backslashes or certain non-ASCII characters in their name
+        TarArchiveOutputStream could fail.
+      </action>
     </release>
     <release version="1.7" date="2014-01-20"
              description="Release 1.7">

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=1570800&r1=1570799&r2=1570800&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
 Sat Feb 22 07:00:42 2014
@@ -443,11 +443,6 @@ public class TarArchiveOutputStream exte
         if (name.length() >= TarConstants.NAMELEN) {
             name = name.substring(0, TarConstants.NAMELEN - 1);
         }
-        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);
-        }
         TarArchiveEntry pex = new TarArchiveEntry(name,
                                                   
TarConstants.LF_PAX_EXTENDED_HEADER_LC);
 
@@ -484,7 +479,9 @@ public class TarArchiveOutputStream exte
         StringBuilder result = new StringBuilder(length);
         for (int i = 0; i < length; i++) {
             char stripped = (char) (name.charAt(i) & 0x7F);
-            if (stripped != 0) { // would be read as Trailing null
+            if (shouldBeReplaced(stripped)) {
+                result.append("_");
+            } else {
                 result.append(stripped);
             }
         }
@@ -492,6 +489,16 @@ public class TarArchiveOutputStream exte
     }
 
     /**
+     * @return true if the character could lead to problems when used
+     * inside a TarArchiveEntry name for a PAX header.
+     */
+    private boolean shouldBeReplaced(char c) {
+        return c == 0 // would be read as Trailing null
+            || c == '/' // when used as last character TAE will consider the 
PAX header a directory
+            || c == '\\'; // same as '/' as slashes get "normalized" on Windows
+    }
+
+    /**
      * Write an EOF (end of archive) record to the tar archive.
      * An EOF record consists of a record of all zeros.
      */

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=1570800&r1=1570799&r2=1570800&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
 Sat Feb 22 07:00:42 2014
@@ -469,6 +469,27 @@ public class TarArchiveOutputStreamTest 
     }
 
     /**
+     * @see "https://issues.apache.org/jira/browse/COMPRESS-265";
+     */
+    public void testWriteNonAsciiNameWithUnfortunateNamePosixMode() throws 
Exception {
+        String n = "f\u00f6\u00f6\u00dc";
+        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());
+        assertFalse(e.isDirectory());
+        tin.close();
+    }
+
+    /**
      * @see "https://issues.apache.org/jira/browse/COMPRESS-237";
      */
     public void testWriteLongLinkNameErrorMode() throws Exception {


Reply via email to