/* * 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.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.nio.charset.Charset; import java.time.Duration; import java.util.Arrays; import java.util.List; import java.util.Locale; import java.util.Objects; import java.util.StringTokenizer; import java.util.stream.Collectors; /** * 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 initialization * @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(final String path) throws IOException { return INSTANCE.freeSpaceOS(path, OS, false, Duration.ofMillis(-1)); } /** * Returns the free space for the working directory * in kibibytes (1024 bytes) by invoking the command line. *
* Identical to: *
* freeSpaceKb(FileUtils.current().getAbsolutePath()) ** @return the amount of free drive space on the drive or volume in kilobytes * @throws IllegalStateException if an error occurred in initialization * @throws IOException if an error occurs when finding the free space * @since 2.0 * @deprecated As of 2.6 deprecated without replacement. Please use {@link java.nio.file.FileStore#getUsableSpace()}. */ @Deprecated public static long freeSpaceKb() throws IOException { return freeSpaceKb(-1); } /** * Returns the free space for the working directory * in kibibytes (1024 bytes) by invoking the command line. *
* Identical to: *
* freeSpaceKb(FileUtils.current().getAbsolutePath()) ** @param timeout The timeout 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 initialization * @throws IOException if an error occurs when finding the free space * @since 2.0 * @deprecated As of 2.6 deprecated without replacement. Please use {@link java.nio.file.FileStore#getUsableSpace()}. */ @Deprecated public static long freeSpaceKb(final long timeout) throws IOException { return freeSpaceKb(FileUtils.current().getAbsolutePath(), timeout); } /** * Returns the free space on a drive or volume in kibibytes (1024 bytes) * 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 an 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 initialization * @throws IOException if an error occurs when finding the free space * @since 1.2, enhanced OS support in 1.3 * @deprecated As of 2.6 deprecated without replacement. Please use {@link java.nio.file.FileStore#getUsableSpace()}. */ @Deprecated public static long freeSpaceKb(final String path) throws IOException { return freeSpaceKb(path, -1); } /** * Returns the free space on a drive or volume in kibibytes (1024 bytes) * 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 an 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 timeout 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 initialization * @throws IOException if an error occurs when finding the free space * @since 2.0 * @deprecated As of 2.6 deprecated without replacement. Please use {@link java.nio.file.FileStore#getUsableSpace()}. */ @Deprecated public static long freeSpaceKb(final String path, final long timeout) throws IOException { return INSTANCE.freeSpaceOS(path, OS, true, Duration.ofMillis(timeout)); } /** * Instances should NOT be constructed in standard programming. */ public FileSystemUtils() { } /** * 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 timeout 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 initialization * @throws IOException if an error occurs when finding the free space */ long freeSpaceOS(final String path, final int os, final boolean kb, final Duration timeout) throws IOException { Objects.requireNonNull(path, "path"); 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 *nix platform using the 'df' command. * * @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 * @param timeout The timeout amount in milliseconds or no timeout if the value * is zero or less * @return the amount of free drive space on the volume * @throws IOException If an I/O error occurs */ long freeSpaceUnix(final String path, final boolean kb, final boolean posix, final Duration timeout) throws IOException { if (path.isEmpty()) { throw new IllegalArgumentException("Path must not be empty"); } // build and run the 'dir' command String flags = "-"; if (kb) { flags += "k"; } if (posix) { flags += "P"; } final String[] cmdAttribs = flags.length() > 1 ? new String[] { DF, flags, path } : new String[] { DF, path }; // perform the command, asking for up to 3 lines (header, interesting, overflow) final List