diff --git a/src/main/org/apache/tools/ant/taskdefs/Tar.java b/src/main/org/apache/tools/ant/taskdefs/Tar.java index db3ccec8b..31bea1329 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Tar.java +++ b/src/main/org/apache/tools/ant/taskdefs/Tar.java @@ -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) { diff --git a/src/main/org/apache/tools/tar/TarEntry.java b/src/main/org/apache/tools/tar/TarEntry.java index 180315f54..3b326343e 100644 --- a/src/main/org/apache/tools/tar/TarEntry.java +++ b/src/main/org/apache/tools/tar/TarEntry.java @@ -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; diff --git a/src/tests/antunit/taskdefs/tar-test.xml b/src/tests/antunit/taskdefs/tar-test.xml index 9611c5d4b..8defb3f9e 100644 --- a/src/tests/antunit/taskdefs/tar-test.xml +++ b/src/tests/antunit/taskdefs/tar-test.xml @@ -17,6 +17,7 @@ --> @@ -30,4 +31,45 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +