1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.io;
18
19 import java.io.IOException;
20 import java.nio.file.Files;
21 import java.nio.file.Path;
22 import java.nio.file.Paths;
23 import java.util.Objects;
24
25 /**
26 * General File System utilities.
27 * <p>
28 * This class provides static utility methods for general file system functions not provided before Java 6's {@link java.io.File File} class.
29 * </p>
30 * <p>
31 * The current functions provided are:
32 * </p>
33 * <ul>
34 * <li>Get the free space on a drive</li>
35 * </ul>
36 *
37 * @since 1.1
38 * @deprecated As of 2.6 deprecated without replacement. Use equivalent methods in {@link java.nio.file.FileStore} instead,
39 * {@code Files.getFileStore(Paths.get("/home")).getUsableSpace()} or iterate over {@code FileSystems.getDefault().getFileStores()}
40 */
41 @Deprecated
42 public class FileSystemUtils {
43
44 /**
45 * Gets the number of kibibytes (1024 bytes) available to this Java virtual machine on the given file store.
46 * <p>
47 * Note that some OS's are NOT currently supported, including OS/390, OpenVMS.
48 * </p>
49 *
50 * <pre>
51 * FileSystemUtils.freeSpace("C:"); // Windows
52 * FileSystemUtils.freeSpace("/volume"); // *nix
53 * </pre>
54 *
55 * @param path the path to get free space for, not null, not empty on UNIX
56 * @return the amount of free drive space on the drive or volume
57 * @throws IOException if an I/O error occurs.
58 * @throws IllegalArgumentException if the path is invalid.
59 * @since 1.1, enhanced OS support in 1.2 and 1.3
60 * @deprecated Use freeSpaceKb(String) Deprecated from 1.3, may be removed in 2.0
61 */
62 @Deprecated
63 public static long freeSpace(final String path) throws IOException {
64 return getFreeSpace(path);
65 }
66
67 /**
68 * Gets the number of kibibytes (1024 bytes) available to this Java virtual machine on the current file store.
69 * <p>
70 * Identical to:
71 * </p>
72 *
73 * <pre>
74 * freeSpaceKb(FileUtils.current().getAbsolutePath())
75 * </pre>
76 *
77 * @return the amount of free drive space on the drive or volume in kilobytes
78 * @throws IOException if an I/O error occurs.
79 * @throws IllegalArgumentException if the path is invalid.
80 * @since 2.0
81 * @deprecated As of 2.6 deprecated without replacement. Please use {@link java.nio.file.FileStore#getUsableSpace()}.
82 */
83 @Deprecated
84 public static long freeSpaceKb() throws IOException {
85 return freeSpaceKb(-1);
86 }
87
88 /**
89 * Gets the number of kibibytes (1024 bytes) available to this Java virtual machine on the current file store.
90 * <p>
91 * Identical to:
92 * </p>
93 *
94 * <pre>
95 * freeSpaceKb(FileUtils.current().getAbsolutePath())
96 * </pre>
97 *
98 * @param timeout ignored.
99 * @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 }