>>>>> "IM" == Iulian Musat <[EMAIL PROTECTED]> writes:

 IM> Hello everybody!  I found a bug related to building tar files
 IM> with tar task.

Thanks Iulian for reporting this, I've appended a trivial patch with
your solution for completeness.

I haven't been aware of this feature of the Tar task and thought I
should use it in the Untar task (if Ant is run by a Java 2 VM). The
second patch enables Untar to reset the modification time of extracted
files.

 IM> Suggestion: Probably a function to translate Java time to unix
 IM> time will be a good idea (take a look into
 IM> org.apache.tools.ant.tar.TarEntry: there are a lot of "/ 1000"

You're probably right. I don't know whether the org.apache.tools.tar
package is an exclusive part of jakarta-ant or if there are other
projects with copies of it. Therefore I didn't dare to touch it.

Stefan

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.1
diff -u -r1.1 Tar.java
--- src/main/org/apache/tools/ant/taskdefs/Tar.java	2000/02/10 18:04:29	1.1
+++ src/main/org/apache/tools/ant/taskdefs/Tar.java	2000/06/07 09:56:06
@@ -122,7 +122,7 @@
 
         TarEntry te = new TarEntry(vPath);
         te.setSize(file.length());
-        te.setModTime(file.lastModified() / 1000);
+        te.setModTime(file.lastModified());
         tOut.putNextEntry(te);
 
         byte[] buffer = new byte[8 * 1024];
Index: src/main/org/apache/tools/ant/taskdefs/Untar.java
===================================================================
RCS file: /home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Untar.java,v
retrieving revision 1.1
diff -u -r1.1 Untar.java
--- src/main/org/apache/tools/ant/taskdefs/Untar.java	2000/05/24 14:35:22	1.1
+++ src/main/org/apache/tools/ant/taskdefs/Untar.java	2000/06/07 09:56:22
@@ -57,6 +57,9 @@
 import org.apache.tools.ant.*;
 import org.apache.tools.tar.*;
 import java.io.*;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
 /**
  * Untar a file.
  *
@@ -74,6 +77,23 @@
      * @exception BuildException Thrown in unrecoverable error.
      */
     public void execute() throws BuildException {
+
+        Method setLastModified = null;
+        Long[] times = null;
+        // 1.0 is ruled out anyway, so this ensures 1.2 or above
+        if (project.getJavaVersion() != Project.JAVA_1_1) {
+            try {
+                setLastModified = 
+                    java.io.File.class.getMethod("setLastModified", 
+                                                 new Class[] {Long.TYPE});
+
+                times = new Long[1];
+            } catch (Exception e) {
+                project.log("File.setLastModified(long) not found",
+                            Project.MSG_VERBOSE);
+            }
+        }
+                    
         try {
             if (source == null) {
                 throw new BuildException("No source specified");
@@ -115,8 +135,21 @@
 
                         fos.close();
                     }
-                } catch( FileNotFoundException ex ) {
-                    System.out.println("FileNotFoundException: " +  te.getName()  );
+
+                    if (setLastModified != null) {
+                        times[0] = new Long(te.getModTime().getTime());
+                        try {
+                            setLastModified.invoke(f, times);
+                        } catch (Exception e) {
+                            project.log("cannot invoke File.setLastModified(long)",
+                                        Project.MSG_VERBOSE);
+                            setLastModified = null;
+                        }
+                    }
+
+                } catch(FileNotFoundException ex) {
+                    project.log("FileNotFoundException: " + te.getName(),
+                                Project.MSG_WARN);
                 }
             }
         } catch (IOException ioe) {

Reply via email to