diff --git a/WHATSNEW b/WHATSNEW index d5827d00b..8e8696283 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -70,6 +70,9 @@ Fixed bugs: Other changes: -------------- +* TarFileset takes in three new attributes - fullpath, prefix + and preserveLeadingSlashes. + * attempts to rename the directory, if everything inside it is included, before performing file-by-file moves. This attempt will be done only if filtering is off and if mappers are not used. This diff --git a/src/etc/testcases/taskdefs/tar.xml b/src/etc/testcases/taskdefs/tar.xml index ef8f0635c..58b56be07 100644 --- a/src/etc/testcases/taskdefs/tar.xml +++ b/src/etc/testcases/taskdefs/tar.xml @@ -44,6 +44,15 @@ + + + + + + + + + @@ -52,6 +61,8 @@ + + diff --git a/src/main/org/apache/tools/ant/taskdefs/Tar.java b/src/main/org/apache/tools/ant/taskdefs/Tar.java index 4f80deff8..111812c92 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Tar.java +++ b/src/main/org/apache/tools/ant/taskdefs/Tar.java @@ -265,6 +265,10 @@ public class Tar extends MatchingTask { for (Enumeration e = filesets.elements(); e.hasMoreElements();) { TarFileSet fs = (TarFileSet)e.nextElement(); String[] files = fs.getFiles(project); + if (files.length > 1 && fs.getFullpath().length() > 0) { + throw new BuildException("fullpath attribute may only be specified for " + + "filesets that specify a single file."); + } for (int i = 0; i < files.length; i++) { File f = new File(fs.getDir(project), files[i]); String name = files[i].replace(File.separatorChar,'/'); @@ -291,13 +295,34 @@ public class Tar extends MatchingTask { { FileInputStream fIn = null; - // don't add "" to the archive - if (vPath.length() <= 0) { - return; + String fullpath = tarFileSet.getFullpath(); + if (fullpath.length() > 0) { + vPath = fullpath; + } else { + // don't add "" to the archive + if (vPath.length() <= 0) { + return; + } + + if (file.isDirectory() && !vPath.endsWith("/")) { + vPath += "/"; + } + + String prefix = tarFileSet.getPrefix(); + // '/' is appended for compatibility with the zip task. + if (prefix.length() > 0 && !prefix.endsWith("/")) { + prefix = prefix + "/"; + } + vPath = prefix + vPath; } - if (file.isDirectory() && !vPath.endsWith("/")) { - vPath += "/"; + if (vPath.startsWith("/") && !tarFileSet.getPreserveLeadingSlashes()) { + int l = vPath.length(); + if (l <= 1) { + // we would end up adding "" to the archive + return; + } + vPath = vPath.substring(1, l); } try { @@ -320,13 +345,7 @@ public class Tar extends MatchingTask { } } - String prefix = tarFileSet.getPrefix(); - // '/' is appended for compatibility with the zip task. - if(prefix.length() > 0 && !prefix.endsWith("/")) { - prefix = prefix + "/"; - } - - TarEntry te = new TarEntry(prefix + vPath); + TarEntry te = new TarEntry(vPath); te.setModTime(file.lastModified()); if (!file.isDirectory()) { te.setSize(file.length()); @@ -371,7 +390,9 @@ public class Tar extends MatchingTask { private String userName = ""; private String groupName = ""; private String prefix = ""; - + private String fullpath = ""; + private boolean preserveLeadingSlashes = false; + public TarFileSet(FileSet fileset) { super(fileset); } @@ -430,6 +451,22 @@ public class Tar extends MatchingTask { public String getPrefix() { return prefix; } + + public void setFullpath(String fullpath) { + this.fullpath = fullpath; + } + + public String getFullpath() { + return fullpath; + } + + public void setPreserveLeadingSlashes(boolean b) { + this.preserveLeadingSlashes = b; + } + + public boolean getPreserveLeadingSlashes() { + return preserveLeadingSlashes; + } } /** diff --git a/src/testcases/org/apache/tools/ant/taskdefs/TarTest.java b/src/testcases/org/apache/tools/ant/taskdefs/TarTest.java index 5ebcccae8..76b9d7210 100644 --- a/src/testcases/org/apache/tools/ant/taskdefs/TarTest.java +++ b/src/testcases/org/apache/tools/ant/taskdefs/TarTest.java @@ -115,6 +115,15 @@ public class TarTest extends BuildFileTest { } } + public void test8() { + executeTarget("test8"); + java.io.File f1 + = new java.io.File("src/etc/testcases/taskdefs/test8.xml"); + if (! f1.exists()) { + fail("The fullpath attribute or the preserveLeadingSlashes attribute does not work propertly"); + } + } + public void tearDown() { executeTarget("cleanup"); }