commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject 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
Date Wed, 10 Jul 2013 11:48:38 GMT
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



Mime
View raw message