Author: scolebourne Date: Fri Aug 25 14:39:06 2006 New Revision: 436964 URL: http://svn.apache.org/viewvc?rev=436964&view=rev Log: IO-83 - Fix freeSpace for AIX and HP-UX code from Magnus Grimsell, also from James Urie
Modified: jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt jakarta/commons/proper/io/trunk/project.xml jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileSystemUtils.java jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileSystemUtilsTestCase.java Modified: jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt URL: http://svn.apache.org/viewvc/jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt?rev=436964&r1=436963&r2=436964&view=diff ============================================================================== --- jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt (original) +++ jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt Fri Aug 25 14:39:06 2006 @@ -33,7 +33,10 @@ Bug fixes from 1.2 ------------------ - LineIterator now implements Iterator - (It was always supposed to...) + - It was always supposed to... + +- FileSystemUtils.freeSpace/freeSpaceKb [IO-83] + - These should now work on AIX and HP-UX Enhancements from 1.2 Modified: jakarta/commons/proper/io/trunk/project.xml URL: http://svn.apache.org/viewvc/jakarta/commons/proper/io/trunk/project.xml?rev=436964&r1=436963&r2=436964&view=diff ============================================================================== --- jakarta/commons/proper/io/trunk/project.xml (original) +++ jakarta/commons/proper/io/trunk/project.xml Fri Aug 25 14:39:06 2006 @@ -189,6 +189,9 @@ <name>Chris Eldredge</name> </contributor> <contributor> + <name>Magnus Grimsell</name> + </contributor> + <contributor> <name>Jim Harrington</name> </contributor> <contributor> @@ -212,6 +215,9 @@ </contributor> <contributor> <name>Masato Tezuka</name> + </contributor> + <contributor> + <name>James Urie</name> </contributor> <contributor> <name>Frank W. Zammetti</name> Modified: jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileSystemUtils.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileSystemUtils.java?rev=436964&r1=436963&r2=436964&view=diff ============================================================================== --- jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileSystemUtils.java (original) +++ jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileSystemUtils.java Fri Aug 25 14:39:06 2006 @@ -35,6 +35,8 @@ * @author Frank W. Zammetti * @author Stephen Colebourne * @author Thomas Ledoux + * @author James Urie + * @author Magnus Grimsell * @version $Id$ * @since Commons IO 1.1 */ @@ -51,6 +53,8 @@ private static final int WINDOWS = 1; /** Operating system state flag for Unix. */ private static final int UNIX = 2; + /** Operating system state flag for Posix flavour Unix. */ + private static final int POSIX_UNIX = 3; /** The operating system flag. */ private static final int OS; @@ -70,14 +74,15 @@ osName.indexOf("sunos") != -1 || osName.indexOf("solaris") != -1 || osName.indexOf("mpe/ix") != -1 || - osName.indexOf("hp-ux") != -1 || - osName.indexOf("aix") != -1 || osName.indexOf("freebsd") != -1 || osName.indexOf("irix") != -1 || osName.indexOf("digital unix") != -1 || osName.indexOf("unix") != -1 || osName.indexOf("mac os x") != -1) { os = UNIX; + } else if (osName.indexOf("hp-ux") != -1 || + osName.indexOf("aix") != -1) { + os = POSIX_UNIX; } else { os = OTHER; } @@ -133,7 +138,7 @@ * FileSystemUtils.freeSpaceKb("/volume"); // *nix * </pre> * The free space is calculated via the command line. - * It uses 'dir /-c' on Windows and 'df -k' on *nix. + * It uses 'dir /-c' on Windows, 'df -kP' on AIX/HP-UX and 'df -k' on other Unix. * * @param path the path to get free space for, not null, not empty on Unix * @return the amount of free drive space on the drive or volume in kilobytes @@ -173,7 +178,9 @@ case WINDOWS: return (kb ? freeSpaceWindows(path) / 1024 : freeSpaceWindows(path)); case UNIX: - return freeSpaceUnix(path, kb); + return freeSpaceUnix(path, kb, false); + case POSIX_UNIX: + return freeSpaceUnix(path, kb, true); case OTHER: throw new IllegalStateException("Unsupported operating system"); default: @@ -278,19 +285,27 @@ * * @param path the path to get free space for * @param kb whether to normalize to kilobytes + * @param posix whether to use the posix standard format flag * @return the amount of free drive space on the volume * @throws IOException if an error occurs */ - long freeSpaceUnix(String path, boolean kb) throws IOException { + long freeSpaceUnix(String path, boolean kb, boolean posix) throws IOException { if (path.length() == 0) { throw new IllegalArgumentException("Path must not be empty"); } path = FilenameUtils.normalize(path); // build and run the 'dir' command + String flags = "-"; + if (kb) { + flags += "k"; + } + if (posix) { + flags += "P"; + } String[] cmdAttribs = - (kb ? new String[] {"df", "-k", path} : new String[] {"df", path}); - + (flags.length() > 1 ? new String[] {"df", flags, path} : new String[] {"df", path}); + // read the output from the command until we come to the second line long bytes = -1; BufferedReader in = null; Modified: jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileSystemUtilsTestCase.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileSystemUtilsTestCase.java?rev=436964&r1=436963&r2=436964&view=diff ============================================================================== --- jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileSystemUtilsTestCase.java (original) +++ jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileSystemUtilsTestCase.java Fri Aug 25 14:39:06 2006 @@ -30,8 +30,7 @@ /** * This is used to test FileSystemUtils. * - * @author Stephen Colebourne - * @version $Id$ + * @version $Id: FileSystemUtilsTestCase.java 385680 2006-03-13 22:27:09Z scolebourne $ */ public class FileSystemUtilsTestCase extends FileBasedTestCase { @@ -64,7 +63,14 @@ // test coverage, as we can't check value if (File.separatorChar == '/') { // have to figure out unix block size - Process proc = Runtime.getRuntime().exec(new String[] {"df", "/"}); + String[] cmd = null; + String osName = System.getProperty("os.name"); + if (osName.indexOf("hp-ux") >= 0 || osName.indexOf("aix") >= 0) { + cmd = new String[] {"df", "-P", "/"}; + } else { + cmd = new String[] {"df", "/"}; + } + Process proc = Runtime.getRuntime().exec(cmd); boolean kilobyteBlock = true; BufferedReader r = null; try { @@ -141,7 +147,7 @@ public void testGetFreeSpaceOS_String_Unix() throws Exception { FileSystemUtils fsu = new FileSystemUtils() { - protected long freeSpaceUnix(String path, boolean kb) throws IOException { + protected long freeSpaceUnix(String path, boolean kb, boolean posix) throws IOException { return (kb ? 12345L : 54321); } }; @@ -270,13 +276,22 @@ } }; try { - fsu.freeSpaceUnix("", false); + fsu.freeSpaceUnix("", false, false); + fail(); + } catch (IllegalArgumentException ex) {} + try { + fsu.freeSpaceUnix("", true, false); fail(); } catch (IllegalArgumentException ex) {} try { - fsu.freeSpaceUnix("", true); + fsu.freeSpaceUnix("", true, true); fail(); } catch (IllegalArgumentException ex) {} + try { + fsu.freeSpaceUnix("", false, true); + fail(); + } catch (IllegalArgumentException ex) {} + } public void testGetFreeSpaceUnix_String_NormalResponse() throws Exception { @@ -289,7 +304,7 @@ return new BufferedReader(reader); } }; - assertEquals(1472504L, fsu.freeSpaceUnix("/home/users/s", false)); + assertEquals(1472504L, fsu.freeSpaceUnix("/home/users/s", false, false)); } public void testGetFreeSpaceUnix_String_NormalResponseKb() throws Exception { @@ -302,7 +317,7 @@ return new BufferedReader(reader); } }; - assertEquals(1472504L, fsu.freeSpaceUnix("/home/users/s", true)); + assertEquals(1472504L, fsu.freeSpaceUnix("/home/users/s", true, false)); } public void testGetFreeSpaceUnix_String_LongResponse() throws Exception { @@ -316,7 +331,7 @@ return new BufferedReader(reader); } }; - assertEquals(1472504L, fsu.freeSpaceUnix("/home/users/s", false)); + assertEquals(1472504L, fsu.freeSpaceUnix("/home/users/s", false, false)); } public void testGetFreeSpaceUnix_String_LongResponseKb() throws Exception { @@ -330,7 +345,7 @@ return new BufferedReader(reader); } }; - assertEquals(1472504L, fsu.freeSpaceUnix("/home/users/s", true)); + assertEquals(1472504L, fsu.freeSpaceUnix("/home/users/s", true, false)); } public void testGetFreeSpaceUnix_String_EmptyResponse() throws Exception { @@ -342,11 +357,19 @@ } }; try { - fsu.freeSpaceUnix("/home/users/s", false); + fsu.freeSpaceUnix("/home/users/s", false, false); + fail(); + } catch (IOException ex) {} + try { + fsu.freeSpaceUnix("/home/users/s", true, false); + fail(); + } catch (IOException ex) {} + try { + fsu.freeSpaceUnix("/home/users/s", false, true); fail(); } catch (IOException ex) {} try { - fsu.freeSpaceUnix("/home/users/s", true); + fsu.freeSpaceUnix("/home/users/s", true, true); fail(); } catch (IOException ex) {} } @@ -362,11 +385,19 @@ } }; try { - fsu.freeSpaceUnix("/home/users/s", false); + fsu.freeSpaceUnix("/home/users/s", false, false); fail(); } catch (IOException ex) {} try { - fsu.freeSpaceUnix("/home/users/s", true); + fsu.freeSpaceUnix("/home/users/s", true, false); + fail(); + } catch (IOException ex) {} + try { + fsu.freeSpaceUnix("/home/users/s", false, true); + fail(); + } catch (IOException ex) {} + try { + fsu.freeSpaceUnix("/home/users/s", true, true); fail(); } catch (IOException ex) {} } @@ -382,11 +413,19 @@ } }; try { - fsu.freeSpaceUnix("/home/users/s", false); + fsu.freeSpaceUnix("/home/users/s", false, false); + fail(); + } catch (IOException ex) {} + try { + fsu.freeSpaceUnix("/home/users/s", true, false); + fail(); + } catch (IOException ex) {} + try { + fsu.freeSpaceUnix("/home/users/s", false, true); fail(); } catch (IOException ex) {} try { - fsu.freeSpaceUnix("/home/users/s", true); + fsu.freeSpaceUnix("/home/users/s", true, true); fail(); } catch (IOException ex) {} } @@ -402,11 +441,19 @@ } }; try { - fsu.freeSpaceUnix("/home/users/s", false); + fsu.freeSpaceUnix("/home/users/s", false, false); + fail(); + } catch (IOException ex) {} + try { + fsu.freeSpaceUnix("/home/users/s", true, false); fail(); } catch (IOException ex) {} try { - fsu.freeSpaceUnix("/home/users/s", true); + fsu.freeSpaceUnix("/home/users/s", false, true); + fail(); + } catch (IOException ex) {} + try { + fsu.freeSpaceUnix("/home/users/s", true, true); fail(); } catch (IOException ex) {} } @@ -422,11 +469,19 @@ } }; try { - fsu.freeSpaceUnix("/home/users/s", false); + fsu.freeSpaceUnix("/home/users/s", false, false); + fail(); + } catch (IOException ex) {} + try { + fsu.freeSpaceUnix("/home/users/s", true, false); + fail(); + } catch (IOException ex) {} + try { + fsu.freeSpaceUnix("/home/users/s", false, true); fail(); } catch (IOException ex) {} try { - fsu.freeSpaceUnix("/home/users/s", true); + fsu.freeSpaceUnix("/home/users/s", true, true); fail(); } catch (IOException ex) {} } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]