diff --git a/WHATSNEW b/WHATSNEW index fdcf70d56..c0107c801 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -19,6 +19,8 @@ Other changes: * new magic property build.rmic to chose the rmic implementation +* will now add empty directories as well + Fixed bugs: ----------- diff --git a/src/etc/testcases/taskdefs/tar.xml b/src/etc/testcases/taskdefs/tar.xml index 06935fbdf..28046926c 100644 --- a/src/etc/testcases/taskdefs/tar.xml +++ b/src/etc/testcases/taskdefs/tar.xml @@ -20,8 +20,16 @@ basedir="."/> + + + + + + diff --git a/src/main/org/apache/tools/ant/taskdefs/Tar.java b/src/main/org/apache/tools/ant/taskdefs/Tar.java index da1183932..9ac6834c1 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Tar.java +++ b/src/main/org/apache/tools/ant/taskdefs/Tar.java @@ -236,8 +236,10 @@ public class Tar extends MatchingTask { TarFileSet tarFileSet) throws IOException { - FileInputStream fIn = new FileInputStream(file); - + FileInputStream fIn = null; + if (file.isDirectory() && vPath.length() != 0 + && vPath.charAt(vPath.length() - 1) != '/') + vPath = vPath + "/"; try { if (vPath.length() >= TarConstants.NAMELEN) { if (longFileMode.equalsIgnoreCase(OMIT)) { @@ -259,26 +261,31 @@ public class Tar extends MatchingTask { } TarEntry te = new TarEntry(vPath); - te.setSize(file.length()); te.setModTime(file.lastModified()); if (!file.isDirectory()) { + te.setSize(file.length()); te.setMode(tarFileSet.getMode()); - } + } te.setUserName(tarFileSet.getUserName()); te.setGroupName(tarFileSet.getGroup()); tOut.putNextEntry(te); - byte[] buffer = new byte[8 * 1024]; - int count = 0; - do { - tOut.write(buffer, 0, count); - count = fIn.read(buffer, 0, buffer.length); - } while (count != -1); + if (!file.isDirectory()) { + fIn = new FileInputStream(file); + + byte[] buffer = new byte[8 * 1024]; + int count = 0; + do { + tOut.write(buffer, 0, count); + count = fIn.read(buffer, 0, buffer.length); + } while (count != -1); + } tOut.closeEntry(); } finally { - fIn.close(); + if (fIn != null) + fIn.close(); } } @@ -306,10 +313,20 @@ public class Tar extends MatchingTask { super(); } + /** + * Get a list of files and directories specified in the fileset. + * @return a list of file and directory names, relative to + * the baseDir for the project. + */ public String[] getFiles(Project p) { if (files == null) { DirectoryScanner ds = getDirectoryScanner(p); - files = ds.getIncludedFiles(); + String[] directories = ds.getIncludedDirectories(); + String[] filesPerSe = ds.getIncludedFiles(); + files = new String [directories.length + filesPerSe.length]; + System.arraycopy(directories, 0, files, 0, directories.length); + System.arraycopy(filesPerSe, 0, files, directories.length, + filesPerSe.length); } return files; diff --git a/src/testcases/org/apache/tools/ant/taskdefs/TarTest.java b/src/testcases/org/apache/tools/ant/taskdefs/TarTest.java index 64ee7593b..4f7a03282 100644 --- a/src/testcases/org/apache/tools/ant/taskdefs/TarTest.java +++ b/src/testcases/org/apache/tools/ant/taskdefs/TarTest.java @@ -83,6 +83,16 @@ public class TarTest extends TaskdefsTest { expectBuildException("test4", "tar cannot include itself"); } + public void test5() { + executeTarget("test5"); + java.io.File f + = new java.io.File("src/etc/testcases/taskdefs/test5.tar"); + + if (!f.exists()) { + fail("Tarring a directory failed"); + } + } + public void tearDown() { executeTarget("cleanup"); }