Skip to content

Commit c6be22a

Browse files
author
Stephen Colebourne
committed
Handle 512byte blocks such as on OS X
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/io/trunk@385680 13f79535-47bb-0310-9956-ffa450edef68
1 parent 79958bb commit c6be22a

2 files changed

Lines changed: 25 additions & 4 deletions

File tree

src/java/org/apache/commons/io/FileSystemUtils.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,10 @@ public FileSystemUtils() {
100100
* Returns the free space on a drive or volume by invoking
101101
* the command line.
102102
* This method does not normalize the result, and typically returns
103-
* bytes on Windows and Kilobytes on Unix.
104-
* See also {@link #freeSpaceKb(String)}.
103+
* bytes on Windows, 512 byte units on OS X and kilobytes on Unix.
104+
* <p>
105+
* See also {@link #freeSpaceKb(String)} for a better implementation
106+
* which normalizes to kilobytes.
105107
* <p>
106108
* Note that some OS's are NOT currently supported, including OS/390.
107109
* <pre>

src/test/org/apache/commons/io/FileSystemUtilsTestCase.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.io.BufferedReader;
1919
import java.io.File;
2020
import java.io.IOException;
21+
import java.io.InputStreamReader;
2122
import java.io.StringReader;
2223

2324
import junit.framework.Test;
@@ -62,10 +63,28 @@ protected void tearDown() throws Exception {
6263
public void testGetFreeSpace_String() throws Exception {
6364
// test coverage, as we can't check value
6465
if (File.separatorChar == '/') {
65-
// test assumes Unix using 1kb blocks
66+
// have to figure out unix block size
67+
Process proc = Runtime.getRuntime().exec(new String[] {"df", "/"});
68+
boolean kilobyteBlock = true;
69+
BufferedReader r = null;
70+
try {
71+
r = new BufferedReader(new InputStreamReader(proc.getInputStream()));
72+
String line = r.readLine();
73+
if (line.toLowerCase().indexOf("512") >= 0) {
74+
kilobyteBlock = false;
75+
}
76+
} finally {
77+
IOUtils.closeQuietly(r);
78+
}
79+
80+
// now perform the test
6681
long free = FileSystemUtils.freeSpace("/");
6782
long kb = FileSystemUtils.freeSpaceKb("/");
68-
assertEquals((double) free, (double) kb, 256d);
83+
if (kilobyteBlock) {
84+
assertEquals((double) free, (double) kb, 256d);
85+
} else {
86+
assertEquals((double) free / 2d, (double) kb, 256d);
87+
}
6988
} else {
7089
long bytes = FileSystemUtils.freeSpace("");
7190
long kb = FileSystemUtils.freeSpaceKb("");

0 commit comments

Comments
 (0)