001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.io;
018
019import java.io.IOException;
020import java.nio.file.Files;
021import java.nio.file.Path;
022import java.nio.file.Paths;
023import java.util.Objects;
024
025/**
026 * General File System utilities.
027 * <p>
028 * This class provides static utility methods for general file system functions not provided before Java 6's {@link java.io.File File} class.
029 * </p>
030 * <p>
031 * The current functions provided are:
032 * </p>
033 * <ul>
034 * <li>Get the free space on a drive</li>
035 * </ul>
036 *
037 * @since 1.1
038 * @deprecated As of 2.6 deprecated without replacement. Use equivalent methods in {@link java.nio.file.FileStore} instead,
039 *             {@code Files.getFileStore(Paths.get("/home")).getUsableSpace()} or iterate over {@code FileSystems.getDefault().getFileStores()}
040 */
041@Deprecated
042public class FileSystemUtils {
043
044    /**
045     * Gets the number of kibibytes (1024 bytes) available to this Java virtual machine on the given file store.
046     * <p>
047     * Note that some OS's are NOT currently supported, including OS/390, OpenVMS.
048     * </p>
049     *
050     * <pre>
051     * FileSystemUtils.freeSpace("C:"); // Windows
052     * FileSystemUtils.freeSpace("/volume"); // *nix
053     * </pre>
054     *
055     * @param path the path to get free space for, not null, not empty on UNIX
056     * @return the amount of free drive space on the drive or volume
057     * @throws IOException              if an I/O error occurs.
058     * @throws IllegalArgumentException if the path is invalid.
059     * @since 1.1, enhanced OS support in 1.2 and 1.3
060     * @deprecated Use freeSpaceKb(String) Deprecated from 1.3, may be removed in 2.0
061     */
062    @Deprecated
063    public static long freeSpace(final String path) throws IOException {
064        return getFreeSpace(path);
065    }
066
067    /**
068     * Gets the number of kibibytes (1024 bytes) available to this Java virtual machine on the current file store.
069     * <p>
070     * Identical to:
071     * </p>
072     *
073     * <pre>
074     * freeSpaceKb(FileUtils.current().getAbsolutePath())
075     * </pre>
076     *
077     * @return the amount of free drive space on the drive or volume in kilobytes
078     * @throws IOException              if an I/O error occurs.
079     * @throws IllegalArgumentException if the path is invalid.
080     * @since 2.0
081     * @deprecated As of 2.6 deprecated without replacement. Please use {@link java.nio.file.FileStore#getUsableSpace()}.
082     */
083    @Deprecated
084    public static long freeSpaceKb() throws IOException {
085        return freeSpaceKb(-1);
086    }
087
088    /**
089     * Gets the number of kibibytes (1024 bytes) available to this Java virtual machine on the current file store.
090     * <p>
091     * Identical to:
092     * </p>
093     *
094     * <pre>
095     * freeSpaceKb(FileUtils.current().getAbsolutePath())
096     * </pre>
097     *
098     * @param timeout ignored.
099     * @return the amount of free drive space on the drive or volume in kilobytes
100     * @throws IOException              if an I/O error occurs.
101     * @throws IllegalArgumentException if the path is invalid.
102     * @since 2.0
103     * @deprecated As of 2.6 deprecated without replacement. Please use {@link java.nio.file.FileStore#getUsableSpace()}.
104     */
105    @Deprecated
106    public static long freeSpaceKb(final long timeout) throws IOException {
107        return freeSpaceKb(FileUtils.current().getAbsolutePath(), timeout);
108    }
109
110    /**
111     * Gets the number of kibibytes (1024 bytes) available to this Java virtual machine on the given file store.
112     *
113     * <pre>
114     * FileSystemUtils.freeSpaceKb("C:"); // Windows
115     * FileSystemUtils.freeSpaceKb("/volume"); // *nix
116     * </pre>
117     *
118     * @param path the path to get free space for, not null, not empty on UNIX
119     * @return the amount of free drive space on the drive or volume in kilobytes
120     * @throws IOException              if an I/O error occurs.
121     * @throws IllegalArgumentException if the path is invalid.
122     * @since 1.2, enhanced OS support in 1.3
123     * @deprecated As of 2.6 deprecated without replacement. Please use {@link java.nio.file.FileStore#getUsableSpace()}.
124     */
125    @Deprecated
126    public static long freeSpaceKb(final String path) throws IOException {
127        return freeSpaceKb(path, -1);
128    }
129
130    /**
131     * Gets the number of kibibytes (1024 bytes) available to this Java virtual machine on the given file store.
132     *
133     * <pre>
134     * FileSystemUtils.freeSpaceKb("C:"); // Windows
135     * FileSystemUtils.freeSpaceKb("/volume"); // *nix
136     * </pre>
137     *
138     * @param path    the path to get free space for, not null, not empty on UNIX
139     * @param timeout ignored.
140     * @return the amount of free drive space on the drive or volume in kilobytes
141     * @throws IOException              if an I/O error occurs.
142     * @throws IllegalArgumentException if the path is invalid.
143     * @since 2.0
144     * @deprecated As of 2.6 deprecated without replacement. Please use {@link java.nio.file.FileStore#getUsableSpace()}.
145     */
146    @Deprecated
147    public static long freeSpaceKb(final String path, final long timeout) throws IOException {
148        return getFreeSpace(path) / FileUtils.ONE_KB;
149    }
150
151    /**
152     * Gets the number of bytes available to this Java virtual machine on the given file store.
153     *
154     * <pre>
155     * FileSystemUtils.freeSpace("C:"); // Windows
156     * FileSystemUtils.freeSpace("/volume"); // *nix
157     * </pre>
158     *
159     * @param pathStr the path to get free space for, not null, not empty on UNIX
160     * @return the amount of free drive space on the drive or volume
161     * @throws IOException              if an I/O error occurs.
162     * @throws IllegalArgumentException if the path is invalid.
163     */
164    static long getFreeSpace(final String pathStr) throws IOException {
165        final Path path = Paths.get(Objects.requireNonNull(pathStr, "pathStr"));
166        if (Files.exists(path)) {
167            // Need an absolute path for input like "" to work
168            return Files.getFileStore(path.toAbsolutePath()).getUsableSpace();
169            // return path.toAbsolutePath().toFile().getUsableSpace();
170        }
171        throw new IllegalArgumentException(path.toString());
172    }
173
174    /**
175     * Instances should NOT be constructed in standard programming.
176     *
177     * @deprecated TODO Make private in 3.0.
178     */
179    @Deprecated
180    public FileSystemUtils() {
181        // empty
182    }
183
184}