Skip to content

Commit 70e0519

Browse files
committed
IO-389 FileUtils.sizeOfDirectory can throw IllegalArgumentException
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1501744 13f79535-47bb-0310-9956-ffa450edef68
1 parent 7ae7af9 commit 70e0519

2 files changed

Lines changed: 18 additions & 3 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ The <action> type attribute can be add,update,fix,remove.
4747
<body>
4848
<!-- The release date is the date RC is cut -->
4949
<release version="2.5" date="2013-??-??" description="New features and bug fixes.">
50+
<action issue="IO-389" dev="sebb" type="fix" due-to="Austin Doupnik">
51+
FileUtils.sizeOfDirectory can throw IllegalArgumentException
52+
</action>
5053
<action issue="IO-390" dev="sebb" type="fix">
5154
FileUtils.sizeOfDirectoryAsBigInteger can overflow.
5255
Ensure that recursive calls all use BigInteger

src/main/java/org/apache/commons/io/FileUtils.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2519,7 +2519,7 @@ public static long sizeOf(final File file) {
25192519
}
25202520

25212521
if (file.isDirectory()) {
2522-
return sizeOfDirectory(file);
2522+
return sizeOfDirectory0(file); // private method; expects directory
25232523
} else {
25242524
return file.length();
25252525
}
@@ -2566,7 +2566,6 @@ public static BigInteger sizeOfAsBigInteger(final File file) {
25662566
* overflow occurs. See {@link #sizeOfDirectoryAsBigInteger(File)} for an alternative
25672567
* method that does not overflow.
25682568
*
2569-
*
25702569
* @param directory
25712570
* directory to inspect, must not be {@code null}
25722571
* @return size of directory in bytes, 0 if directory is security restricted, a negative number when the real total
@@ -2576,7 +2575,11 @@ public static BigInteger sizeOfAsBigInteger(final File file) {
25762575
*/
25772576
public static long sizeOfDirectory(final File directory) {
25782577
checkDirectory(directory);
2578+
return sizeOfDirectory0(directory);
2579+
}
25792580

2581+
// Private method, must be invoked will a directory parameter
2582+
private static long sizeOfDirectory0(final File directory) {
25802583
final File[] files = directory.listFiles();
25812584
if (files == null) { // null if security restricted
25822585
return 0L;
@@ -2586,7 +2589,7 @@ public static long sizeOfDirectory(final File directory) {
25862589
for (final File file : files) {
25872590
try {
25882591
if (!isSymlink(file)) {
2589-
size += sizeOf(file);
2592+
size += sizeOf0(file); // internal method
25902593
if (size < 0) {
25912594
break;
25922595
}
@@ -2599,6 +2602,15 @@ public static long sizeOfDirectory(final File directory) {
25992602
return size;
26002603
}
26012604

2605+
// Internal method - does not check existence
2606+
private static long sizeOf0(File file) {
2607+
if (file.isDirectory()) {
2608+
return sizeOfDirectory0(file);
2609+
} else {
2610+
return file.length(); // will be 0 if file does not exist
2611+
}
2612+
}
2613+
26022614
/**
26032615
* Counts the size of a directory recursively (sum of the length of all files).
26042616
*

0 commit comments

Comments
 (0)