You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by se...@apache.org on 2013/07/10 13:48:38 UTC

svn commit: r1501735 - in /commons/proper/io/trunk/src: changes/changes.xml main/java/org/apache/commons/io/FileUtils.java test/java/org/apache/commons/io/FileUtilsTestCase.java

Author: sebb
Date: Wed Jul 10 11:48:37 2013
New Revision: 1501735

URL: http://svn.apache.org/r1501735
Log:
IO-390 
FileUtils.sizeOfDirectoryAsBigInteger can overflow.
Ensure that recursive calls all use BigInteger

Modified:
    commons/proper/io/trunk/src/changes/changes.xml
    commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java
    commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java

Modified: commons/proper/io/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/changes/changes.xml?rev=1501735&r1=1501734&r2=1501735&view=diff
==============================================================================
--- commons/proper/io/trunk/src/changes/changes.xml (original)
+++ commons/proper/io/trunk/src/changes/changes.xml Wed Jul 10 11:48:37 2013
@@ -47,6 +47,10 @@ The <action> type attribute can be add,u
   <body>
     <!-- The release date is the date RC is cut -->
     <release version="2.5" date="2013-??-??" description="New features and bug fixes.">    
+      <action issue="IO-390" dev="sebb" type="fix">
+         FileUtils.sizeOfDirectoryAsBigInteger can overflow.
+         Ensure that recursive calls all use BigInteger
+      </action>
       <action issue="IO-382" dev="sebb" type="add">
          Chunked IO for large arrays.
          Added writeChunked(byte[], OutputStream) and writeChunked(char[] Writer)

Modified: commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java?rev=1501735&r1=1501734&r2=1501735&view=diff
==============================================================================
--- commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java (original)
+++ commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java Wed Jul 10 11:48:37 2013
@@ -2552,7 +2552,7 @@ public class FileUtils {
         }
 
         if (file.isDirectory()) {
-            return sizeOfDirectoryAsBigInteger(file);
+            return sizeOfDirectoryBig0(file); // internal method
         } else {
             return BigInteger.valueOf(file.length());
         }
@@ -2611,17 +2611,21 @@ public class FileUtils {
      */
     public static BigInteger sizeOfDirectoryAsBigInteger(final File directory) {
         checkDirectory(directory);
+        return sizeOfDirectoryBig0(directory);
+    }
 
+    // Must be called with a directory
+    private static BigInteger sizeOfDirectoryBig0(final File directory) {
         final File[] files = directory.listFiles();
         if (files == null) {  // null if security restricted
             return BigInteger.ZERO;
         }
-        BigInteger size = BigInteger.ZERO;
+        BigInteger size =  BigInteger.ZERO;
 
         for (final File file : files) {
             try {
                 if (!isSymlink(file)) {
-                    size = size.add(BigInteger.valueOf(sizeOf(file)));
+                    size = size.add(sizeOBig0(file));
                 }
             } catch (final IOException ioe) {
                 // Ignore exceptions caught when asking if a File is a symlink.
@@ -2631,6 +2635,15 @@ public class FileUtils {
         return size;
     }
 
+    // internal method; if file does not exist will return 0
+    private static BigInteger sizeOBig0(final File fileOrDir) {
+        if (fileOrDir.isDirectory()) {
+            return sizeOfDirectoryBig0(fileOrDir);
+        } else {
+            return BigInteger.valueOf(fileOrDir.length());
+        }
+    }
+
     /**
      * Checks that the given {@code File} exists and is a directory.
      *

Modified: commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java?rev=1501735&r1=1501734&r2=1501735&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java (original)
+++ commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java Wed Jul 10 11:48:37 2013
@@ -862,6 +862,16 @@ public class FileUtilsTestCase extends F
         file.delete();
     }
 
+    // Compare sizes of a directory tree using long and BigInteger methods
+    public void testCompareSizeOf() {
+        final File start = new File("src/test/java");
+        final long sizeLong1 = FileUtils.sizeOf(start);
+        final BigInteger sizeBig = FileUtils.sizeOfAsBigInteger(start);
+        final long sizeLong2 = FileUtils.sizeOf(start);
+        assertEquals("Size should not change",sizeLong1, sizeLong2);
+        assertEquals("longSize should equal BigSize",sizeLong1, sizeBig.longValue());
+    }
+
     /**
      * Tests the {@link FileUtils#sizeOf(File)} method.
      * @throws Exception



Re: svn commit: r1501735 - in /commons/proper/io/trunk/src: changes/changes.xml main/java/org/apache/commons/io/FileUtils.java test/java/org/apache/commons/io/FileUtilsTestCase.java

Posted by Gary Gregory <ga...@gmail.com>.
Shouldn't the new private be called sizeOfBig0?

Gary

On Jul 10, 2013, at 7:49, "sebb@apache.org" <se...@apache.org> wrote:

> sizeOBig0

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org