/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.commons.io; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Locale; import java.util.StringTokenizer; /** * General File System utilities. *
* This class provides static utility methods for general file system * functions not provided via the JDK {@link java.io.File File} class. *
* The current functions provided are: *
* Note that some OS's are NOT currently supported, including OS/390, * OpenVMS. *
* FileSystemUtils.freeSpace("C:"); // Windows
* FileSystemUtils.freeSpace("/volume"); // *nix
*
* The free space is calculated via the command line.
* It uses 'dir /-c' on Windows and 'df' 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
* @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 1.1, enhanced OS support in 1.2 and 1.3
* @deprecated Use freeSpaceKb(String)
* Deprecated from 1.3, may be removed in 2.0
*/
@Deprecated
public static long freeSpace(String path) throws IOException {
return INSTANCE.freeSpaceOS(path, OS, false, -1);
}
//-----------------------------------------------------------------------
/**
* Returns the free space on a drive or volume in kilobytes by invoking
* the command line.
*
* FileSystemUtils.freeSpaceKb("C:"); // Windows
* FileSystemUtils.freeSpaceKb("/volume"); // *nix
*
* The free space is calculated via the command line.
* It uses 'dir /-c' on Windows, 'df -kP' on AIX/HP-UX and 'df -k' on other Unix.
* * In order to work, you must be running Windows, or have a implementation of * Unix df that supports GNU format when passed -k (or -kP). If you are going * to rely on this code, please check that it works on your OS by running * some simple tests to compare the command line with the output from this class. * If your operating system isn't supported, please raise a JIRA call detailing * the exact result from df -k and as much other detail as possible, thanks. * * @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 1.2, enhanced OS support in 1.3 */ public static long freeSpaceKb(String path) throws IOException { return freeSpaceKb(path, -1); } /** * Returns the free space on a drive or volume in kilobytes by invoking * the command line. *
* FileSystemUtils.freeSpaceKb("C:"); // Windows
* FileSystemUtils.freeSpaceKb("/volume"); // *nix
*
* The free space is calculated via the command line.
* It uses 'dir /-c' on Windows, 'df -kP' on AIX/HP-UX and 'df -k' on other Unix.
* * In order to work, you must be running Windows, or have a implementation of * Unix df that supports GNU format when passed -k (or -kP). If you are going * to rely on this code, please check that it works on your OS by running * some simple tests to compare the command line with the output from this class. * If your operating system isn't supported, please raise a JIRA call detailing * the exact result from df -k and as much other detail as possible, thanks. * * @param path the path to get free space for, not null, not empty on Unix * @param timeout The timout amount in milliseconds or no timeout if the value * is zero or less * @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 2.0 */ public static long freeSpaceKb(String path, long timeout) throws IOException { return INSTANCE.freeSpaceOS(path, OS, true, timeout); } /** * Returns the disk size of the volume which holds the working directory. *
* Identical to: *
* freeSpaceKb(new File(".").getAbsolutePath())
*
* @return the amount of free drive space on the drive or volume in kilobytes
* @throws IllegalStateException if an error occurred in initialisation
* @throws IOException if an error occurs when finding the free space
* @since 2.0
*/
public static long freeSpaceKb() throws IOException {
return freeSpaceKb(-1);
}
/**
* Returns the disk size of the volume which holds the working directory.
* * Identical to: *
* freeSpaceKb(new File(".").getAbsolutePath())
*
* @param timeout The timout amount in milliseconds or no timeout if the value
* is zero or less
* @return the amount of free drive space on the drive or volume in kilobytes
* @throws IllegalStateException if an error occurred in initialisation
* @throws IOException if an error occurs when finding the free space
* @since 2.0
*/
public static long freeSpaceKb(long timeout) throws IOException {
return freeSpaceKb(new File(".").getAbsolutePath(), timeout);
}
//-----------------------------------------------------------------------
/**
* 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.
*
* FileSystemUtils.freeSpace("C:"); // Windows
* FileSystemUtils.freeSpace("/volume"); // *nix
*
* The free space is calculated via the command line.
* It uses 'dir /-c' on Windows and 'df' on *nix.
*
* @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
* @param timeout The timout amount in milliseconds or no timeout if the value
* is zero or less
* @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, boolean kb, long timeout) throws IOException {
if (path == null) {
throw new IllegalArgumentException("Path must not be empty");
}
switch (os) {
case WINDOWS:
return kb ? freeSpaceWindows(path, timeout) / FileUtils.ONE_KB : freeSpaceWindows(path, timeout);
case UNIX:
return freeSpaceUnix(path, kb, false, timeout);
case POSIX_UNIX:
return freeSpaceUnix(path, kb, true, timeout);
case OTHER:
throw new IllegalStateException("Unsupported operating system");
default:
throw new IllegalStateException(
"Exception caught when determining operating system");
}
}
//-----------------------------------------------------------------------
/**
* Find free space on the Windows platform using the 'dir' command.
*
* @param path the path to get free space for, including the colon
* @param timeout The timout amount in milliseconds or no timeout if the value
* is zero or less
* @return the amount of free drive space on the drive
* @throws IOException if an error occurs
*/
long freeSpaceWindows(String path, long timeout) throws IOException {
path = FilenameUtils.normalize(path, false);
if (path.length() > 0 && path.charAt(0) != '"') {
path = "\"" + path + "\"";
}
// build and run the 'dir' command
String[] cmdAttribs = new String[] {"cmd.exe", "/C", "dir /a /-c " + path};
// read in the output of the command to an ArrayList
List