You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2006/03/04 20:14:05 UTC

svn commit: r383179 - in /jakarta/commons/proper/io/trunk: RELEASE-NOTES.txt src/java/org/apache/commons/io/FileSystemUtils.java src/test/org/apache/commons/io/FileSystemUtilsTestCase.java

Author: scolebourne
Date: Sat Mar  4 11:14:04 2006
New Revision: 383179

URL: http://svn.apache.org/viewcvs?rev=383179&view=rev
Log:
FileSystemUtils.freeSpaceKb to normalize free space to kb
bug 38574

Modified:
    jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt
    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/viewcvs/jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt?rev=383179&r1=383178&r2=383179&view=diff
==============================================================================
--- jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt (original)
+++ jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt Sat Mar  4 11:14:04 2006
@@ -42,6 +42,9 @@
 - AgeFileFilter/SizeFileFilter
   New file filters that compares against the age and size of the file
 
+- FileSystemUtils.freeSpaceKb(drive)
+  New method that unifies result to be in kilobytes [38574]
+
 - FileUtils.contentEquals(File,File)
   Performance improved by adding length and file location checking
 

Modified: jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileSystemUtils.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileSystemUtils.java?rev=383179&r1=383178&r2=383179&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 Sat Mar  4 11:14:04 2006
@@ -1,5 +1,5 @@
 /*
- * Copyright 2005 The Apache Software Foundation.
+ * Copyright 2005-2006 The Apache Software Foundation.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -97,7 +97,12 @@
 
     //-----------------------------------------------------------------------
     /**
-     * Returns the free space on a drive or volume in a cross-platform manner.
+     * Returns the free space on a drive or volume by invoking
+     * the command line.
+     * This method does not normalize the result, and typically returns
+     * bytes on Windows and Kilobytes on Unix.
+     * See also {@link #freeSpaceKb(String)}.
+     * <p>
      * Note that some OS's are NOT currently supported, including OS/390.
      * <pre>
      * FileSystemUtils.freeSpace("C:");  // Windows
@@ -113,9 +118,33 @@
      * @throws IOException if an error occurs when finding the free space
      */
     public static long freeSpace(String path) throws IOException {
-        return INSTANCE.freeSpaceOS(path, OS);
+        return INSTANCE.freeSpaceOS(path, OS, false);
+    }
+
+    //-----------------------------------------------------------------------
+    /**
+     * Returns the free space on a drive or volume in kilobytes by invoking
+     * the command line.
+     * Note that some OS's are NOT currently supported, including OS/390.
+     * <pre>
+     * FileSystemUtils.freeSpace("C:");  // Windows
+     * FileSystemUtils.freeSpace("/volume");  // *nix
+     * </pre>
+     * The free space is calculated via the command line.
+     * It uses 'dir /-c' on Windows and 'df -k' on *nix.
+     *
+     * @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
+     * @throws IllegalArgumentException if the path is invalid
+     * @throws IllegalStateException if an error occurred in initialisation
+     * @throws IOException if an error occurs when finding the free space
+     * @since Commons IO 1.2
+     */
+    public static long freeSpaceKb(String path) throws IOException {
+        return INSTANCE.freeSpaceOS(path, OS, true);
     }
 
+    //-----------------------------------------------------------------------
     /**
      * Returns the free space on a drive or volume in a cross-platform manner.
      * Note that some OS's are NOT currently supported, including OS/390.
@@ -128,20 +157,21 @@
      *
      * @param path  the path to get free space for, not null, not empty on Unix
      * @param os  the operating system code
+     * @param kb  whether to normalize to kilobytes
      * @return the amount of free drive space on the drive or volume
      * @throws IllegalArgumentException if the path is invalid
      * @throws IllegalStateException if an error occurred in initialisation
      * @throws IOException if an error occurs when finding the free space
      */
-    long freeSpaceOS(String path, int os) throws IOException {
+    long freeSpaceOS(String path, int os, boolean kb) throws IOException {
         if (path == null) {
             throw new IllegalArgumentException("Path must not be empty");
         }
         switch (os) {
             case WINDOWS:
-                return freeSpaceWindows(path);
+                return (kb ? freeSpaceWindows(path) / 1024 : freeSpaceWindows(path));
             case UNIX:
-                return freeSpaceUnix(path);
+                return freeSpaceUnix(path, kb);
             case OTHER:
                 throw new IllegalStateException("Unsupported operating system");
             default:
@@ -245,17 +275,19 @@
      * Find free space on the *nix platform using the 'df' command.
      *
      * @param path  the path to get free space for
+     * @param kb  whether to normalize to kilobytes
      * @return the amount of free drive space on the volume
      * @throws IOException if an error occurs
      */
-    long freeSpaceUnix(String path) throws IOException {
+    long freeSpaceUnix(String path, boolean kb) 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[] cmdAttribs = new String[] {"df", path};
+        String[] cmdAttribs = 
+            (kb ? new String[] {"df", "-k", path} : new String[] {"df", path});
 
         // read the output from the command until we come to the second line
         long bytes = -1;

Modified: jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileSystemUtilsTestCase.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileSystemUtilsTestCase.java?rev=383179&r1=383178&r2=383179&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 Sat Mar  4 11:14:04 2006
@@ -1,5 +1,5 @@
 /*
- * Copyright 2005 The Apache Software Foundation.
+ * Copyright 2005-2006 The Apache Software Foundation.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -62,9 +62,14 @@
     public void testGetFreeSpace_String() throws Exception {
         // test coverage, as we can't check value
         if (File.separatorChar == '/') {
-            assertEquals(true, FileSystemUtils.freeSpace("/") > 0);
+            // test assumes Unix using 1kb blocks
+            long free = FileSystemUtils.freeSpace("/");
+            long kb = FileSystemUtils.freeSpaceKb("/");
+            assertEquals((double) free, (double) kb, 256d);
         } else {
-            assertEquals(true, FileSystemUtils.freeSpace("") > 0);
+            long bytes = FileSystemUtils.freeSpace("");
+            long kb = FileSystemUtils.freeSpaceKb("");
+            assertEquals((double) bytes / 1024, (double) kb, 256d);
         }
     }
 
@@ -72,7 +77,11 @@
     public void testGetFreeSpaceOS_String_NullPath() throws Exception {
         FileSystemUtils fsu = new FileSystemUtils();
         try {
-            fsu.freeSpaceOS(null, 1);
+            fsu.freeSpaceOS(null, 1, false);
+            fail();
+        } catch (IllegalArgumentException ex) {}
+        try {
+            fsu.freeSpaceOS(null, 1, true);
             fail();
         } catch (IllegalArgumentException ex) {}
     }
@@ -80,7 +89,11 @@
     public void testGetFreeSpaceOS_String_InitError() throws Exception {
         FileSystemUtils fsu = new FileSystemUtils();
         try {
-            fsu.freeSpaceOS("", -1);
+            fsu.freeSpaceOS("", -1, false);
+            fail();
+        } catch (IllegalStateException ex) {}
+        try {
+            fsu.freeSpaceOS("", -1, true);
             fail();
         } catch (IllegalStateException ex) {}
     }
@@ -88,7 +101,11 @@
     public void testGetFreeSpaceOS_String_Other() throws Exception {
         FileSystemUtils fsu = new FileSystemUtils();
         try {
-            fsu.freeSpaceOS("", 0);
+            fsu.freeSpaceOS("", 0, false);
+            fail();
+        } catch (IllegalStateException ex) {}
+        try {
+            fsu.freeSpaceOS("", 0, true);
             fail();
         } catch (IllegalStateException ex) {}
     }
@@ -99,16 +116,18 @@
                 return 12345L;
             }
         };
-        assertEquals(12345L, fsu.freeSpaceOS("", 1));
+        assertEquals(12345L, fsu.freeSpaceOS("", 1, false));
+        assertEquals(12345L / 1024, fsu.freeSpaceOS("", 1, true));
     }
 
     public void testGetFreeSpaceOS_String_Unix() throws Exception {
         FileSystemUtils fsu = new FileSystemUtils() {
-            protected long freeSpaceUnix(String path) throws IOException {
-                return 12345L;
+            protected long freeSpaceUnix(String path, boolean kb) throws IOException {
+                return (kb ? 12345L : 54321);
             }
         };
-        assertEquals(12345L, fsu.freeSpaceOS("", 2));
+        assertEquals(54321L, fsu.freeSpaceOS("", 2, false));
+        assertEquals(12345L, fsu.freeSpaceOS("", 2, true));
     }
 
     //-----------------------------------------------------------------------
@@ -232,7 +251,11 @@
             }
         };
         try {
-            fsu.freeSpaceUnix("");
+            fsu.freeSpaceUnix("", false);
+            fail();
+        } catch (IllegalArgumentException ex) {}
+        try {
+            fsu.freeSpaceUnix("", true);
             fail();
         } catch (IllegalArgumentException ex) {}
     }
@@ -247,7 +270,20 @@
                 return new BufferedReader(reader);
             }
         };
-        assertEquals(1472504L, fsu.freeSpaceUnix("/home/users/s"));
+        assertEquals(1472504L, fsu.freeSpaceUnix("/home/users/s", false));
+    }
+
+    public void testGetFreeSpaceUnix_String_NormalResponseKb() throws Exception {
+        String lines =
+            "Filesystem           1K-blocks      Used Available Use% Mounted on\n" +
+            "xxx:/home/users/s     14428928  12956424   1472504  90% /home/users/s";
+        final StringReader reader = new StringReader(lines);
+        FileSystemUtils fsu = new FileSystemUtils() {
+            protected BufferedReader openProcessStream(String[] params) {
+                return new BufferedReader(reader);
+            }
+        };
+        assertEquals(1472504L, fsu.freeSpaceUnix("/home/users/s", true));
     }
 
     public void testGetFreeSpaceUnix_String_LongResponse() throws Exception {
@@ -261,7 +297,21 @@
                 return new BufferedReader(reader);
             }
         };
-        assertEquals(1472504L, fsu.freeSpaceUnix("/home/users/s"));
+        assertEquals(1472504L, fsu.freeSpaceUnix("/home/users/s", false));
+    }
+
+    public void testGetFreeSpaceUnix_String_LongResponseKb() throws Exception {
+        String lines =
+            "Filesystem           1K-blocks      Used Available Use% Mounted on\n" +
+            "xxx-yyyyyyy-zzz:/home/users/s\n" +
+            "                      14428928  12956424   1472504  90% /home/users/s";
+        final StringReader reader = new StringReader(lines);
+        FileSystemUtils fsu = new FileSystemUtils() {
+            protected BufferedReader openProcessStream(String[] params) {
+                return new BufferedReader(reader);
+            }
+        };
+        assertEquals(1472504L, fsu.freeSpaceUnix("/home/users/s", true));
     }
 
     public void testGetFreeSpaceUnix_String_EmptyResponse() throws Exception {
@@ -273,7 +323,11 @@
             }
         };
         try {
-            fsu.freeSpaceUnix("/home/users/s");
+            fsu.freeSpaceUnix("/home/users/s", false);
+            fail();
+        } catch (IOException ex) {}
+        try {
+            fsu.freeSpaceUnix("/home/users/s", true);
             fail();
         } catch (IOException ex) {}
     }
@@ -289,7 +343,11 @@
             }
         };
         try {
-            fsu.freeSpaceUnix("/home/users/s");
+            fsu.freeSpaceUnix("/home/users/s", false);
+            fail();
+        } catch (IOException ex) {}
+        try {
+            fsu.freeSpaceUnix("/home/users/s", true);
             fail();
         } catch (IOException ex) {}
     }
@@ -305,7 +363,11 @@
             }
         };
         try {
-            fsu.freeSpaceUnix("/home/users/s");
+            fsu.freeSpaceUnix("/home/users/s", false);
+            fail();
+        } catch (IOException ex) {}
+        try {
+            fsu.freeSpaceUnix("/home/users/s", true);
             fail();
         } catch (IOException ex) {}
     }
@@ -321,7 +383,11 @@
             }
         };
         try {
-            fsu.freeSpaceUnix("/home/users/s");
+            fsu.freeSpaceUnix("/home/users/s", false);
+            fail();
+        } catch (IOException ex) {}
+        try {
+            fsu.freeSpaceUnix("/home/users/s", true);
             fail();
         } catch (IOException ex) {}
     }
@@ -337,7 +403,11 @@
             }
         };
         try {
-            fsu.freeSpaceUnix("/home/users/s");
+            fsu.freeSpaceUnix("/home/users/s", false);
+            fail();
+        } catch (IOException ex) {}
+        try {
+            fsu.freeSpaceUnix("/home/users/s", true);
             fail();
         } catch (IOException ex) {}
     }



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org