Author: bodewig
Date: Tue Aug 25 08:04:14 2009
New Revision: 807513

URL: http://svn.apache.org/viewvc?rev=807513&view=rev
Log:
Make preserveLeadingSlashes in tar work again, was broken by revision 755231

Modified:
    ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Tar.java
    ant/core/trunk/src/main/org/apache/tools/tar/TarEntry.java
    ant/core/trunk/src/tests/antunit/taskdefs/tar-test.xml

Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Tar.java
URL: 
http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Tar.java?rev=807513&r1=807512&r2=807513&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Tar.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Tar.java Tue Aug 25 
08:04:14 2009
@@ -361,6 +361,8 @@
             return;
         }
 
+        boolean preserveLeadingSlashes = false;
+
         if (tarFileSet != null) {
             String fullpath = tarFileSet.getFullpath(this.getProject());
             if (fullpath.length() > 0) {
@@ -379,8 +381,9 @@
                 vPath = prefix + vPath;
             }
 
-            if (vPath.startsWith("/")
-                && !tarFileSet.getPreserveLeadingSlashes()) {
+            preserveLeadingSlashes = tarFileSet.getPreserveLeadingSlashes();
+
+            if (vPath.startsWith("/") && !preserveLeadingSlashes) {
                 int l = vPath.length();
                 if (l <= 1) {
                     // we would end up adding "" to the archive
@@ -415,7 +418,7 @@
             }
         }
 
-        TarEntry te = new TarEntry(vPath);
+        TarEntry te = new TarEntry(vPath, preserveLeadingSlashes);
         te.setModTime(r.getLastModified());
         // preserve permissions
         if (r instanceof ArchiveResource) {

Modified: ant/core/trunk/src/main/org/apache/tools/tar/TarEntry.java
URL: 
http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/tar/TarEntry.java?rev=807513&r1=807512&r2=807513&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/tar/TarEntry.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/tar/TarEntry.java Tue Aug 25 
08:04:14 2009
@@ -159,9 +159,21 @@
      * @param name the entry name
      */
     public TarEntry(String name) {
+        this(name, false);
+    }
+
+    /**
+     * Construct an entry with only a name. This allows the programmer
+     * to construct the entry's header "by hand". File is set to null.
+     *
+     * @param name the entry name
+     * @param preserveLeadingSlashes whether to allow leading slashes
+     * in the name.
+     */
+    public TarEntry(String name, boolean preserveLeadingSlashes) {
         this();
 
-        name = normalizeFileName(name);
+        name = normalizeFileName(name, preserveLeadingSlashes);
         boolean isDir = name.endsWith("/");
 
         this.devMajor = 0;
@@ -203,7 +215,7 @@
 
         this.file = file;
 
-        String fileName = normalizeFileName(file.getPath());
+        String fileName = normalizeFileName(file.getPath(), false);
         this.linkName = new StringBuffer("");
         this.name = new StringBuffer(fileName);
 
@@ -299,7 +311,7 @@
      * @param name This entry's new name.
      */
     public void setName(String name) {
-        this.name = new StringBuffer(normalizeFileName(name));
+        this.name = new StringBuffer(normalizeFileName(name, false));
     }
 
     /**
@@ -608,7 +620,8 @@
      * Strips Windows' drive letter as well as any leading slashes,
      * turns path separators into forward slahes.
      */
-    private static String normalizeFileName(String fileName) {
+    private static String normalizeFileName(String fileName,
+                                            boolean preserveLeadingSlashes) {
         String osname = System.getProperty("os.name").toLowerCase(Locale.US);
 
         if (osname != null) {
@@ -640,7 +653,7 @@
         // No absolute pathnames
         // Windows (and Posix?) paths can start with "\\NetworkDrive\",
         // so we loop on starting /'s.
-        while (fileName.startsWith("/")) {
+        while (!preserveLeadingSlashes && fileName.startsWith("/")) {
             fileName = fileName.substring(1);
         }
         return fileName;

Modified: ant/core/trunk/src/tests/antunit/taskdefs/tar-test.xml
URL: 
http://svn.apache.org/viewvc/ant/core/trunk/src/tests/antunit/taskdefs/tar-test.xml?rev=807513&r1=807512&r2=807513&view=diff
==============================================================================
--- ant/core/trunk/src/tests/antunit/taskdefs/tar-test.xml (original)
+++ ant/core/trunk/src/tests/antunit/taskdefs/tar-test.xml Tue Aug 25 08:04:14 
2009
@@ -17,6 +17,7 @@
 -->
 
 <project name="tar-test" default="antunit"
+         xmlns:cond="antlib:org.apache.tools.ant.types.conditions"
          xmlns:au="antlib:org.apache.ant.antunit">
   <import file="../antunit-base.xml" />
 
@@ -30,4 +31,45 @@
       <tarfileset  prefix="pre" refid="xml.fileset" />
     </tar>
   </target>
+
+  <target name="testRemoveLeadingSlashes" depends="setUp">
+    <tar destfile="${output}/testtar.tar">
+      <tarfileset file="${ant.file}" fullpath="/foo.xml"/>
+    </tar>
+    <au:assertTrue>
+      <cond:resourceexists>
+        <tarentry name="foo.xml">
+          <file file="${output}/testtar.tar"/>
+        </tarentry>
+      </cond:resourceexists>
+    </au:assertTrue>
+    <au:assertFalse>
+      <cond:resourceexists>
+        <tarentry name="/foo.xml">
+          <file file="${output}/testtar.tar"/>
+        </tarentry>
+      </cond:resourceexists>
+    </au:assertFalse>
+  </target>
+
+  <target name="testPreserveLeadingSlashes" depends="setUp">
+    <tar destfile="${output}/testtar.tar">
+      <tarfileset file="${ant.file}" fullpath="/foo.xml"
+                  preserveleadingslashes="true"/>
+    </tar>
+    <au:assertTrue>
+      <cond:resourceexists>
+        <tarentry name="/foo.xml">
+          <file file="${output}/testtar.tar"/>
+        </tarentry>
+      </cond:resourceexists>
+    </au:assertTrue>
+    <au:assertFalse>
+      <cond:resourceexists>
+        <tarentry name="foo.xml">
+          <file file="${output}/testtar.tar"/>
+        </tarentry>
+      </cond:resourceexists>
+    </au:assertFalse>
+  </target>
 </project>


Reply via email to