Browse Source

Make <tar> handle empty directories.

PR:             552
Submitted by:	William Webber <william@live.com.au>


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@268862 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 24 years ago
parent
commit
d6e5c9e816
4 changed files with 49 additions and 12 deletions
  1. +2
    -0
      WHATSNEW
  2. +8
    -0
      src/etc/testcases/taskdefs/tar.xml
  3. +29
    -12
      src/main/org/apache/tools/ant/taskdefs/Tar.java
  4. +10
    -0
      src/testcases/org/apache/tools/ant/taskdefs/TarTest.java

+ 2
- 0
WHATSNEW View File

@@ -19,6 +19,8 @@ Other changes:

* new magic property build.rmic to chose the rmic implementation

* <tar> will now add empty directories as well

Fixed bugs:
-----------



+ 8
- 0
src/etc/testcases/taskdefs/tar.xml View File

@@ -20,8 +20,16 @@
basedir="."/>
</target>

<target name="test5">
<mkdir dir="test5dir"/>
<tar tarfile="test5.tar"
basedir="."
includes="test5dir"/>
</target>
<target name="cleanup">
<delete file="test4.tar"/>
<delete file="test5.tar"/>
<delete dir="test5dir"/>
</target>
</project>

+ 29
- 12
src/main/org/apache/tools/ant/taskdefs/Tar.java View File

@@ -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;


+ 10
- 0
src/testcases/org/apache/tools/ant/taskdefs/TarTest.java View File

@@ -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");
}


Loading…
Cancel
Save