Hi all!

The current Tar task does not include directories as such in the Tar
file.  This means that it is impossible to add an empty directory in a
tar file using this task (as one might wish to do if, for instance,
one were creating a skeleton directory structure).  There may be a
good reason for this, but if not, attached is a patch which causes the
Tar task to include directories included by the TarFileSet in the
resulting tar file, as well as files.  There is also a simple test.

William
-- 
William Webber                               [EMAIL PROTECTED]
Senior Programmer                             
PeopleWeb Australia                          http://www.peopleweb.com
Index: src/etc/testcases/taskdefs/tar.xml
===================================================================
RCS file: /home/cvspublic/jakarta-ant/src/etc/testcases/taskdefs/tar.xml,v
retrieving revision 1.3
diff -u -r1.3 tar.xml
--- src/etc/testcases/taskdefs/tar.xml  2001/03/02 15:57:27     1.3
+++ src/etc/testcases/taskdefs/tar.xml  2001/03/07 04:42:00
@@ -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>
Index: src/main/org/apache/tools/ant/taskdefs/Tar.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Tar.java,v
retrieving revision 1.14
diff -u -r1.14 Tar.java
--- src/main/org/apache/tools/ant/taskdefs/Tar.java     2001/03/02 15:58:18     
1.14
+++ src/main/org/apache/tools/ant/taskdefs/Tar.java     2001/03/07 04:42:01
@@ -236,8 +236,10 @@
                            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 @@
             }
 
             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 @@
             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;
Index: src/testcases/org/apache/tools/ant/taskdefs/TarTest.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-ant/src/testcases/org/apache/tools/ant/taskdefs/TarTest.java,v
retrieving revision 1.3
diff -u -r1.3 TarTest.java
--- src/testcases/org/apache/tools/ant/taskdefs/TarTest.java    2001/03/02 
16:00:55     1.3
+++ src/testcases/org/apache/tools/ant/taskdefs/TarTest.java    2001/03/07 
04:42:03
@@ -83,6 +83,16 @@
         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");
     }

Reply via email to