git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@807513 13f79535-47bb-0310-9956-ffa450edef68master
| @@ -361,6 +361,8 @@ public class Tar extends MatchingTask { | |||
| return; | |||
| } | |||
| boolean preserveLeadingSlashes = false; | |||
| if (tarFileSet != null) { | |||
| String fullpath = tarFileSet.getFullpath(this.getProject()); | |||
| if (fullpath.length() > 0) { | |||
| @@ -379,8 +381,9 @@ public class Tar extends MatchingTask { | |||
| 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 @@ public class Tar extends MatchingTask { | |||
| } | |||
| } | |||
| TarEntry te = new TarEntry(vPath); | |||
| TarEntry te = new TarEntry(vPath, preserveLeadingSlashes); | |||
| te.setModTime(r.getLastModified()); | |||
| // preserve permissions | |||
| if (r instanceof ArchiveResource) { | |||
| @@ -159,9 +159,21 @@ public class TarEntry implements TarConstants { | |||
| * @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 @@ public class TarEntry implements TarConstants { | |||
| 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 @@ public class TarEntry implements TarConstants { | |||
| * @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 @@ public class TarEntry implements TarConstants { | |||
| * 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 @@ public class TarEntry implements TarConstants { | |||
| // 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; | |||
| @@ -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> | |||