Skip to content

Commit 6512f64

Browse files
author
Niall Pemberton
committed
IO-160 FileSystemUtils.freeSpace fails on solaris - thanks to Mike Bristow for the patch
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@645787 13f79535-47bb-0310-9956-ffa450edef68
1 parent bcc797b commit 6512f64

2 files changed

Lines changed: 20 additions & 9 deletions

File tree

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ public class FileSystemUtils {
6464

6565
/** The operating system flag. */
6666
private static final int OS;
67+
68+
/** The path to df */
69+
private static String dfPath = "df";
70+
6771
static {
6872
int os = OTHER;
6973
try {
@@ -76,16 +80,18 @@ public class FileSystemUtils {
7680
if (osName.indexOf("windows") != -1) {
7781
os = WINDOWS;
7882
} else if (osName.indexOf("linux") != -1 ||
79-
osName.indexOf("sun os") != -1 ||
80-
osName.indexOf("sunos") != -1 ||
81-
osName.indexOf("solaris") != -1 ||
8283
osName.indexOf("mpe/ix") != -1 ||
8384
osName.indexOf("freebsd") != -1 ||
8485
osName.indexOf("irix") != -1 ||
8586
osName.indexOf("digital unix") != -1 ||
8687
osName.indexOf("unix") != -1 ||
8788
osName.indexOf("mac os x") != -1) {
8889
os = UNIX;
90+
} else if (osName.indexOf("sun os") != -1 ||
91+
osName.indexOf("sunos") != -1 ||
92+
osName.indexOf("solaris") != -1) {
93+
os = POSIX_UNIX;
94+
dfPath = "/usr/xpg4/bin/df";
8995
} else if (osName.indexOf("hp-ux") != -1 ||
9096
osName.indexOf("aix") != -1) {
9197
os = POSIX_UNIX;
@@ -116,7 +122,7 @@ public FileSystemUtils() {
116122
* of {@link #freeSpaceKb(String)} which returns a result in kilobytes.
117123
* <p>
118124
* Note that some OS's are NOT currently supported, including OS/390,
119-
* OpenVMS and and SunOS 5. (SunOS is supported by <code>freeSpaceKb</code>.)
125+
* OpenVMS.
120126
* <pre>
121127
* FileSystemUtils.freeSpace("C:"); // Windows
122128
* FileSystemUtils.freeSpace("/volume"); // *nix
@@ -317,14 +323,14 @@ long freeSpaceUnix(String path, boolean kb, boolean posix) throws IOException {
317323
flags += "P";
318324
}
319325
String[] cmdAttribs =
320-
(flags.length() > 1 ? new String[] {"df", flags, path} : new String[] {"df", path});
326+
(flags.length() > 1 ? new String[] {dfPath, flags, path} : new String[] {dfPath, path});
321327

322328
// perform the command, asking for up to 3 lines (header, interesting, overflow)
323329
List<String> lines = performCommand(cmdAttribs, 3);
324330
if (lines.size() < 2) {
325331
// unknown problem, throw exception
326332
throw new IOException(
327-
"Command line 'df' did not return info as expected " +
333+
"Command line '" + dfPath + "' did not return info as expected " +
328334
"for path '" + path + "'- response was " + lines);
329335
}
330336
String line2 = lines.get(1); // the line we're interested in
@@ -338,7 +344,7 @@ long freeSpaceUnix(String path, boolean kb, boolean posix) throws IOException {
338344
tok = new StringTokenizer(line3, " ");
339345
} else {
340346
throw new IOException(
341-
"Command line 'df' did not return data as expected " +
347+
"Command line '" + dfPath + "' did not return data as expected " +
342348
"for path '" + path + "'- check path is valid");
343349
}
344350
} else {
@@ -364,14 +370,14 @@ long parseBytes(String freeSpace, String path) throws IOException {
364370
long bytes = Long.parseLong(freeSpace);
365371
if (bytes < 0) {
366372
throw new IOException(
367-
"Command line 'df' did not find free space in response " +
373+
"Command line '" + dfPath + "' did not find free space in response " +
368374
"for path '" + path + "'- check path is valid");
369375
}
370376
return bytes;
371377

372378
} catch (NumberFormatException ex) {
373379
throw new IOException(
374-
"Command line 'df' did not return numeric data as expected " +
380+
"Command line '" + dfPath + "' did not return numeric data as expected " +
375381
"for path '" + path + "'- check path is valid");
376382
}
377383
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,13 @@ public void testGetFreeSpace_String() throws Exception {
6868
// have to figure out unix block size
6969
String[] cmd = null;
7070
String osName = System.getProperty("os.name");
71+
osName = osName.toLowerCase();
72+
7173
if (osName.indexOf("hp-ux") >= 0 || osName.indexOf("aix") >= 0) {
7274
cmd = new String[] {"df", "-P", "/"};
75+
} else if (osName.indexOf("sunos") >= 0 || osName.indexOf("sun os") >= 0
76+
|| osName.indexOf("solaris") >= 0) {
77+
cmd = new String[] {"/usr/xpg4/bin/df", "-P", "/"};
7378
} else {
7479
cmd = new String[] {"df", "/"};
7580
}

0 commit comments

Comments
 (0)