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 14:08:52 UTC

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

Author: sebb
Date: Wed Jul 10 12:08:52 2013
New Revision: 1501744

URL: http://svn.apache.org/r1501744
Log:
IO-389 FileUtils.sizeOfDirectory can throw IllegalArgumentException

Modified:
    commons/proper/io/trunk/src/changes/changes.xml
    commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.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=1501744&r1=1501743&r2=1501744&view=diff
==============================================================================
--- commons/proper/io/trunk/src/changes/changes.xml (original)
+++ commons/proper/io/trunk/src/changes/changes.xml Wed Jul 10 12:08:52 2013
@@ -47,6 +47,9 @@ 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-389" dev="sebb" type="fix" due-to="Austin Doupnik">
+         FileUtils.sizeOfDirectory can throw IllegalArgumentException
+      </action>
       <action issue="IO-390" dev="sebb" type="fix">
          FileUtils.sizeOfDirectoryAsBigInteger can overflow.
          Ensure that recursive calls all use BigInteger

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=1501744&r1=1501743&r2=1501744&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 12:08:52 2013
@@ -2519,7 +2519,7 @@ public class FileUtils {
         }
 
         if (file.isDirectory()) {
-            return sizeOfDirectory(file);
+            return sizeOfDirectory0(file); // private method; expects directory
         } else {
             return file.length();
         }
@@ -2566,7 +2566,6 @@ public class FileUtils {
      * overflow occurs. See {@link #sizeOfDirectoryAsBigInteger(File)} for an alternative
      * method that does not overflow.
      *
-     *
      * @param directory
      *            directory to inspect, must not be {@code null}
      * @return size of directory in bytes, 0 if directory is security restricted, a negative number when the real total
@@ -2576,7 +2575,11 @@ public class FileUtils {
      */
     public static long sizeOfDirectory(final File directory) {
         checkDirectory(directory);
+        return sizeOfDirectory0(directory);
+    }
 
+    // Private method, must be invoked will a directory parameter
+    private static long sizeOfDirectory0(final File directory) {
         final File[] files = directory.listFiles();
         if (files == null) {  // null if security restricted
             return 0L;
@@ -2586,7 +2589,7 @@ public class FileUtils {
         for (final File file : files) {
             try {
                 if (!isSymlink(file)) {
-                    size += sizeOf(file);
+                    size += sizeOf0(file); // internal method
                     if (size < 0) {
                         break;
                     }
@@ -2599,6 +2602,15 @@ public class FileUtils {
         return size;
     }
 
+    // Internal method - does not check existence
+    private static long sizeOf0(File file) {
+        if (file.isDirectory()) {
+            return sizeOfDirectory0(file);
+        } else {
+            return file.length(); // will be 0 if file does not exist
+        }
+    }
+
     /**
      * Counts the size of a directory recursively (sum of the length of all files).
      *