ant-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r677870 - in /ant/core/trunk: ./ docs/manual/CoreTasks/ src/main/org/apache/tools/ant/taskdefs/ src/main/org/apache/tools/zip/ src/tests/antunit/taskdefs/
Date Fri, 18 Jul 2008 10:13:29 GMT
Author: bodewig
Date: Fri Jul 18 03:13:28 2008
New Revision: 677870

URL: http://svn.apache.org/viewvc?rev=677870&view=rev
Log:
Made up my mind on the fix for PR 35000.  Empty != broken, so make it two separate use cases.

Modified:
    ant/core/trunk/WHATSNEW
    ant/core/trunk/docs/manual/CoreTasks/unzip.html
    ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Expand.java
    ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Untar.java
    ant/core/trunk/src/main/org/apache/tools/zip/ZipFile.java
    ant/core/trunk/src/tests/antunit/taskdefs/unzip-test.xml

Modified: ant/core/trunk/WHATSNEW
URL: http://svn.apache.org/viewvc/ant/core/trunk/WHATSNEW?rev=677870&r1=677869&r2=677870&view=diff
==============================================================================
--- ant/core/trunk/WHATSNEW (original)
+++ ant/core/trunk/WHATSNEW Fri Jul 18 03:13:28 2008
@@ -58,9 +58,8 @@
    passed in a null or empty InputStream to read from.
    Bugzilla Report 32200
 
- * <unzip> and <untar> will now fail on empty archives (or ZIP
-   archives with an empty central directory).
-   set failOnEmptyArchive to false to restore the old behavior.
+ * <unzip> will now fail when trying to extract certain broken
+   archives that would have been silently ignored in earlier version.
    Bugzilla report 35000.
 
  * Ant's <zip> family of tasks tries to preserve the existing Unix
@@ -226,6 +225,10 @@
    authentication.
    Bugzilla report 33718.
 
+ * a new failOnEmptyArchive attribute on <unzip> and <untar> can now
+   make the task fail the build if it tries to extract an empty
+   archive.
+
 Changes from Ant 1.7.0 TO Ant 1.7.1
 =============================================
 

Modified: ant/core/trunk/docs/manual/CoreTasks/unzip.html
URL: http://svn.apache.org/viewvc/ant/core/trunk/docs/manual/CoreTasks/unzip.html?rev=677870&r1=677869&r2=677870&view=diff
==============================================================================
--- ant/core/trunk/docs/manual/CoreTasks/unzip.html (original)
+++ ant/core/trunk/docs/manual/CoreTasks/unzip.html Fri Jul 18 03:13:28 2008
@@ -114,7 +114,7 @@
     <td valign="top">failOnEmptyArchive</td>
     <td valign="top">whether trying to extract an empty archive is an
       error. <em>since Ant 1.8.0</em></td>
-    <td valign="top" align="center">No, defaults to true</td>
+    <td valign="top" align="center">No, defaults to false</td>
   </tr>
 </table>
 <h3>Examples</h3>

Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Expand.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Expand.java?rev=677870&r1=677869&r2=677870&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Expand.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Expand.java Fri Jul 18 03:13:28
2008
@@ -66,7 +66,7 @@
     private Vector patternsets = new Vector();
     private Union resources = new Union();
     private boolean resourcesSpecified = false;
-    private boolean failOnEmptyArchive = true;
+    private boolean failOnEmptyArchive = false;
 
     private static final String NATIVE_ENCODING = "native-encoding";
 
@@ -164,15 +164,19 @@
                     getLocation());
         }
         try {
-            zf = new ZipFile(srcF, encoding, failOnEmptyArchive);
+            zf = new ZipFile(srcF, encoding);
+            boolean empty = true;
             Enumeration e = zf.getEntries();
             while (e.hasMoreElements()) {
+                empty = false;
                 ZipEntry ze = (ZipEntry) e.nextElement();
                 extractFile(fileUtils, srcF, dir, zf.getInputStream(ze),
                             ze.getName(), new Date(ze.getTime()),
                             ze.isDirectory(), mapper);
             }
-
+            if (empty && getFailOnEmptyArchive()) {
+                throw new BuildException("archive '" + srcF + "' is empty");
+            }
             log("expand complete", Project.MSG_VERBOSE);
         } catch (IOException ioe) {
             throw new BuildException(

Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Untar.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Untar.java?rev=677870&r1=677869&r2=677870&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Untar.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Untar.java Fri Jul 18 03:13:28 2008
@@ -158,7 +158,7 @@
                             te.isDirectory(), mapper);
             }
             if (empty && getFailOnEmptyArchive()) {
-                throw new BuildException("archive is empty");
+                throw new BuildException("archive '" + name + "' is empty");
             }
             log("expand complete", Project.MSG_VERBOSE);
         } finally {

Modified: ant/core/trunk/src/main/org/apache/tools/zip/ZipFile.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/zip/ZipFile.java?rev=677870&r1=677869&r2=677870&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/zip/ZipFile.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/zip/ZipFile.java Fri Jul 18 03:13:28 2008
@@ -146,78 +146,10 @@
      * @throws IOException if an error occurs while reading the file.
      */
     public ZipFile(File f, String encoding) throws IOException {
-        this(f, encoding, false);
-    }
-
-    /**
-     * Opens the given file for reading, assuming the platform's
-     * native encoding for file names.
-     *
-     * @param f the archive.
-     * @param mustNotBeEmpty whether an empty central directory should
-     * case an error
-     *
-     * @throws IOException if an error occurs while reading the file.
-     *
-     * @since Ant 1.8.0
-     */
-    public ZipFile(File f, boolean mustNotBeEmpty) throws IOException {
-        this(f, null, mustNotBeEmpty);
-    }
-
-    /**
-     * Opens the given file for reading, assuming the platform's
-     * native encoding for file names.
-     *
-     * @param name name of the archive.
-     * @param mustNotBeEmpty whether an empty central directory should
-     * case an error
-     *
-     * @throws IOException if an error occurs while reading the file.
-     *
-     * @since Ant 1.8.0
-     */
-    public ZipFile(String name, boolean mustNotBeEmpty) throws IOException {
-        this(new File(name), null, mustNotBeEmpty);
-    }
-
-    /**
-     * Opens the given file for reading, assuming the specified
-     * encoding for file names.
-     *
-     * @param name name of the archive.
-     * @param encoding the encoding to use for file names
-     * @param mustNotBeEmpty whether an empty central directory should
-     * case an error
-     *
-     * @throws IOException if an error occurs while reading the file.
-     *
-     * @since Ant 1.8.0
-     */
-    public ZipFile(String name, String encoding,
-                   boolean mustNotBeEmpty) throws IOException {
-        this(new File(name), encoding, mustNotBeEmpty);
-    }
-
-    /**
-     * Opens the given file for reading, assuming the specified
-     * encoding for file names.
-     *
-     * @param f the archive.
-     * @param encoding the encoding to use for file names
-     * @param mustNotBeEmpty whether an empty central directory should
-     * case an error
-     *
-     * @throws IOException if an error occurs while reading the file.
-     *
-     * @since Ant 1.8.0
-     */
-    public ZipFile(File f, String encoding,
-                   boolean mustNotBeEmpty) throws IOException {
         this.encoding = encoding;
         archive = new RandomAccessFile(f, "r");
         try {
-            populateFromCentralDirectory(mustNotBeEmpty);
+            populateFromCentralDirectory();
             resolveLocalFileHeaderData();
         } catch (IOException e) {
             try {
@@ -334,7 +266,7 @@
      * the central directory alone, but not the data that requires the
      * local file header or additional data to be read.</p>
      */
-    private void populateFromCentralDirectory(boolean mustNotBeEmpty)
+    private void populateFromCentralDirectory()
         throws IOException {
         positionAtCentralDirectory();
 
@@ -344,9 +276,9 @@
         archive.readFully(signatureBytes);
         long sig = ZipLong.getValue(signatureBytes);
         final long cfhSig = ZipLong.getValue(ZipOutputStream.CFH_SIG);
-        if (mustNotBeEmpty && sig != cfhSig) {
+        if (sig != cfhSig && startsWithLocalFileHeader()) {
             throw new IOException("central directory is empty, can't expand"
-                                  + " archive.");
+                                  + " corrupt archive.");
         }
         while (sig == cfhSig) {
             archive.readFully(cfh);
@@ -582,6 +514,22 @@
     }
 
     /**
+     * Checks whether the archive starts with a LFH.  If it doesn't,
+     * it may be an empty archive.
+     */
+    private boolean startsWithLocalFileHeader() throws IOException {
+        archive.seek(0);
+        final byte[] start = new byte[WORD];
+        archive.readFully(start);
+        for (int i = 0; i < start.length; i++) {
+            if (start[i] != ZipOutputStream.LFH_SIG[i]) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    /**
      * InputStream that delegates requests to the underlying
      * RandomAccessFile, making sure that only bytes from a certain
      * range can be read.

Modified: ant/core/trunk/src/tests/antunit/taskdefs/unzip-test.xml
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/tests/antunit/taskdefs/unzip-test.xml?rev=677870&r1=677869&r2=677870&view=diff
==============================================================================
--- ant/core/trunk/src/tests/antunit/taskdefs/unzip-test.xml (original)
+++ ant/core/trunk/src/tests/antunit/taskdefs/unzip-test.xml Fri Jul 18 03:13:28 2008
@@ -32,7 +32,7 @@
 
   <target name="testFailureOnBrokenCentralDirectoryStructure">
     <au:expectfailure
-       expectedmessage="central directory is empty, can't expand archive.">
+       expectedmessage="central directory is empty, can't expand corrupt archive.">
       <unzip src="broken_cd.zip" dest="${dest.dir}"/>
     </au:expectfailure>
   </target>



Mime
View raw message