View Javadoc
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.BufferedReader;
20  import java.io.File;
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.io.InputStreamReader;
24  import java.io.OutputStream;
25  import java.nio.charset.Charset;
26  import java.util.ArrayList;
27  import java.util.Arrays;
28  import java.util.List;
29  import java.util.Locale;
30  import java.util.StringTokenizer;
31  
32  /**
33   * General File System utilities.
34   * <p>
35   * This class provides static utility methods for general file system
36   * functions not provided via the JDK {@link java.io.File File} class.
37   * <p>
38   * The current functions provided are:
39   * <ul>
40   * <li>Get the free space on a drive
41   * </ul>
42   *
43   * @since 1.1
44   */
45  public class FileSystemUtils {
46  
47      /** Singleton instance, used mainly for testing. */
48      private static final FileSystemUtils INSTANCE = new FileSystemUtils();
49  
50      /** Operating system state flag for error. */
51      private static final int INIT_PROBLEM = -1;
52      /** Operating system state flag for neither Unix nor Windows. */
53      private static final int OTHER = 0;
54      /** Operating system state flag for Windows. */
55      private static final int WINDOWS = 1;
56      /** Operating system state flag for Unix. */
57      private static final int UNIX = 2;
58      /** Operating system state flag for Posix flavour Unix. */
59      private static final int POSIX_UNIX = 3;
60  
61      /** The operating system flag. */
62      private static final int OS;
63  
64      /** The path to df */
65      private static final String DF;
66  
67      static {
68          int os = OTHER;
69          String dfPath = "df";
70          try {
71              String osName = System.getProperty("os.name");
72              if (osName == null) {
73                  throw new IOException("os.name not found");
74              }
75              osName = osName.toLowerCase(Locale.ENGLISH);
76              // match
77              if (osName.contains("windows")) {
78                  os = WINDOWS;
79              } else if (osName.contains("linux") ||
80                      osName.contains("mpe/ix") ||
81                      osName.contains("freebsd") ||
82                      osName.contains("openbsd") ||
83                      osName.contains("irix") ||
84                      osName.contains("digital unix") ||
85                      osName.contains("unix") ||
86                      osName.contains("mac os x")) {
87                  os = UNIX;
88              } else if (osName.contains("sun os") ||
89                      osName.contains("sunos") ||
90                      osName.contains("solaris")) {
91                  os = POSIX_UNIX;
92                  dfPath = "/usr/xpg4/bin/df";
93              } else if (osName.contains("hp-ux") ||
94                      osName.contains("aix")) {
95                  os = POSIX_UNIX;
96              } else {
97                  os = OTHER;
98              }
99  
100         } catch (final Exception ex) {
101             os = INIT_PROBLEM;
102         }
103         OS = os;
104         DF = dfPath;
105     }
106 
107     /**
108      * Instances should NOT be constructed in standard programming.
109      */
110     public FileSystemUtils() {
111         super();
112     }
113 
114     //-----------------------------------------------------------------------
115     /**
116      * Returns the free space on a drive or volume by invoking
117      * the command line.
118      * This method does not normalize the result, and typically returns
119      * bytes on Windows, 512 byte units on OS X and kilobytes on Unix.
120      * As this is not very useful, this method is deprecated in favour
121      * of {@link #freeSpaceKb(String)} which returns a result in kilobytes.
122      * <p>
123      * Note that some OS's are NOT currently supported, including OS/390,
124      * OpenVMS.
125      * <pre>
126      * FileSystemUtils.freeSpace("C:");       // Windows
127      * FileSystemUtils.freeSpace("/volume");  // *nix
128      * </pre>
129      * The free space is calculated via the command line.
130      * It uses 'dir /-c' on Windows and 'df' on *nix.
131      *
132      * @param path  the path to get free space for, not null, not empty on Unix
133      * @return the amount of free drive space on the drive or volume
134      * @throws IllegalArgumentException if the path is invalid
135      * @throws IllegalStateException if an error occurred in initialisation
136      * @throws IOException if an error occurs when finding the free space
137      * @since 1.1, enhanced OS support in 1.2 and 1.3
138      * @deprecated Use freeSpaceKb(String)
139      *  Deprecated from 1.3, may be removed in 2.0
140      */
141     @Deprecated
142     public static long freeSpace(final String path) throws IOException {
143         return INSTANCE.freeSpaceOS(path, OS, false, -1);
144     }
145 
146     //-----------------------------------------------------------------------
147     /**
148      * Returns the free space on a drive or volume in kilobytes by invoking
149      * the command line.
150      * <pre>
151      * FileSystemUtils.freeSpaceKb("C:");       // Windows
152      * FileSystemUtils.freeSpaceKb("/volume");  // *nix
153      * </pre>
154      * The free space is calculated via the command line.
155      * It uses 'dir /-c' on Windows, 'df -kP' on AIX/HP-UX and 'df -k' on other Unix.
156      * <p>
157      * In order to work, you must be running Windows, or have a implementation of
158      * Unix df that supports GNU format when passed -k (or -kP). If you are going
159      * to rely on this code, please check that it works on your OS by running
160      * some simple tests to compare the command line with the output from this class.
161      * If your operating system isn't supported, please raise a JIRA call detailing
162      * the exact result from df -k and as much other detail as possible, thanks.
163      *
164      * @param path  the path to get free space for, not null, not empty on Unix
165      * @return the amount of free drive space on the drive or volume in kilobytes
166      * @throws IllegalArgumentException if the path is invalid
167      * @throws IllegalStateException if an error occurred in initialisation
168      * @throws IOException if an error occurs when finding the free space
169      * @since 1.2, enhanced OS support in 1.3
170      * @deprecated As of 2.6 removed without replacement. Please use {@link java.nio.file.FileStore#getUsableSpace()}.
171      */
172     @Deprecated
173     public static long freeSpaceKb(final String path) throws IOException {
174         return freeSpaceKb(path, -1);
175     }
176     /**
177      * Returns the free space on a drive or volume in kilobytes by invoking
178      * the command line.
179      * <pre>
180      * FileSystemUtils.freeSpaceKb("C:");       // Windows
181      * FileSystemUtils.freeSpaceKb("/volume");  // *nix
182      * </pre>
183      * The free space is calculated via the command line.
184      * It uses 'dir /-c' on Windows, 'df -kP' on AIX/HP-UX and 'df -k' on other Unix.
185      * <p>
186      * In order to work, you must be running Windows, or have a implementation of
187      * Unix df that supports GNU format when passed -k (or -kP). If you are going
188      * to rely on this code, please check that it works on your OS by running
189      * some simple tests to compare the command line with the output from this class.
190      * If your operating system isn't supported, please raise a JIRA call detailing
191      * the exact result from df -k and as much other detail as possible, thanks.
192      *
193      * @param path  the path to get free space for, not null, not empty on Unix
194      * @param timeout The timeout amount in milliseconds or no timeout if the value
195      *  is zero or less
196      * @return the amount of free drive space on the drive or volume in kilobytes
197      * @throws IllegalArgumentException if the path is invalid
198      * @throws IllegalStateException if an error occurred in initialisation
199      * @throws IOException if an error occurs when finding the free space
200      * @since 2.0
201      * @deprecated As of 2.6 removed without replacement. Please use {@link java.nio.file.FileStore#getUsableSpace()}.
202      */
203     @Deprecated
204     public static long freeSpaceKb(final String path, final long timeout) throws IOException {
205         return INSTANCE.freeSpaceOS(path, OS, true, timeout);
206     }
207 
208     /**
209      * Returns the disk size of the volume which holds the working directory.
210      * <p>
211      * Identical to:
212      * <pre>
213      * freeSpaceKb(new File(".").getAbsolutePath())
214      * </pre>
215      * @return the amount of free drive space on the drive or volume in kilobytes
216      * @throws IllegalStateException if an error occurred in initialisation
217      * @throws IOException if an error occurs when finding the free space
218      * @since 2.0
219      * @deprecated As of 2.6 removed without replacement. Please use {@link java.nio.file.FileStore#getUsableSpace()}.
220      */
221     @Deprecated
222     public static long freeSpaceKb() throws IOException {
223         return freeSpaceKb(-1);
224     }
225 
226     /**
227      * Returns the disk size of the volume which holds the working directory.
228      * <p>
229      * Identical to:
230      * <pre>
231      * freeSpaceKb(new File(".").getAbsolutePath())
232      * </pre>
233      * @param timeout The timeout amount in milliseconds or no timeout if the value
234      *  is zero or less
235      * @return the amount of free drive space on the drive or volume in kilobytes
236      * @throws IllegalStateException if an error occurred in initialisation
237      * @throws IOException if an error occurs when finding the free space
238      * @since 2.0
239      * @deprecated As of 2.6 removed without replacement. Please use {@link java.nio.file.FileStore#getUsableSpace()}.
240      */
241     @Deprecated
242     public static long freeSpaceKb(final long timeout) throws IOException {
243         return freeSpaceKb(new File(".").getAbsolutePath(), timeout);
244     }
245 
246     //-----------------------------------------------------------------------
247     /**
248      * Returns the free space on a drive or volume in a cross-platform manner.
249      * Note that some OS's are NOT currently supported, including OS/390.
250      * <pre>
251      * FileSystemUtils.freeSpace("C:");  // Windows
252      * FileSystemUtils.freeSpace("/volume");  // *nix
253      * </pre>
254      * The free space is calculated via the command line.
255      * It uses 'dir /-c' on Windows and 'df' on *nix.
256      *
257      * @param path  the path to get free space for, not null, not empty on Unix
258      * @param os  the operating system code
259      * @param kb  whether to normalize to kilobytes
260      * @param timeout The timeout amount in milliseconds or no timeout if the value
261      *  is zero or less
262      * @return the amount of free drive space on the drive or volume
263      * @throws IllegalArgumentException if the path is invalid
264      * @throws IllegalStateException if an error occurred in initialisation
265      * @throws IOException if an error occurs when finding the free space
266      */
267     long freeSpaceOS(final String path, final int os, final boolean kb, final long timeout) throws IOException {
268         if (path == null) {
269             throw new IllegalArgumentException("Path must not be null");
270         }
271         switch (os) {
272             case WINDOWS:
273                 return kb ? freeSpaceWindows(path, timeout) / FileUtils.ONE_KB : freeSpaceWindows(path, timeout);
274             case UNIX:
275                 return freeSpaceUnix(path, kb, false, timeout);
276             case POSIX_UNIX:
277                 return freeSpaceUnix(path, kb, true, timeout);
278             case OTHER:
279                 throw new IllegalStateException("Unsupported operating system");
280             default:
281                 throw new IllegalStateException(
282                   "Exception caught when determining operating system");
283         }
284     }
285 
286     //-----------------------------------------------------------------------
287     /**
288      * Find free space on the Windows platform using the 'dir' command.
289      *
290      * @param path  the path to get free space for, including the colon
291      * @param timeout The timeout amount in milliseconds or no timeout if the value
292      *  is zero or less
293      * @return the amount of free drive space on the drive
294      * @throws IOException if an error occurs
295      */
296     long freeSpaceWindows(String path, final long timeout) throws IOException {
297         path = FilenameUtils.normalize(path, false);
298         if (path.length() > 0 && path.charAt(0) != '"') {
299             path = "\"" + path + "\"";
300         }
301 
302         // build and run the 'dir' command
303         final String[] cmdAttribs = new String[] {"cmd.exe", "/C", "dir /a /-c " + path};
304 
305         // read in the output of the command to an ArrayList
306         final List<String> lines = performCommand(cmdAttribs, Integer.MAX_VALUE, timeout);
307 
308         // now iterate over the lines we just read and find the LAST
309         // non-empty line (the free space bytes should be in the last element
310         // of the ArrayList anyway, but this will ensure it works even if it's
311         // not, still assuming it is on the last non-blank line)
312         for (int i = lines.size() - 1; i >= 0; i--) {
313             final String line = lines.get(i);
314             if (line.length() > 0) {
315                 return parseDir(line, path);
316             }
317         }
318         // all lines are blank
319         throw new IOException(
320                 "Command line 'dir /-c' did not return any info " +
321                 "for path '" + path + "'");
322     }
323 
324     /**
325      * Parses the Windows dir response last line
326      *
327      * @param line  the line to parse
328      * @param path  the path that was sent
329      * @return the number of bytes
330      * @throws IOException if an error occurs
331      */
332     long parseDir(final String line, final String path) throws IOException {
333         // read from the end of the line to find the last numeric
334         // character on the line, then continue until we find the first
335         // non-numeric character, and everything between that and the last
336         // numeric character inclusive is our free space bytes count
337         int bytesStart = 0;
338         int bytesEnd = 0;
339         int j = line.length() - 1;
340         innerLoop1: while (j >= 0) {
341             final char c = line.charAt(j);
342             if (Character.isDigit(c)) {
343               // found the last numeric character, this is the end of
344               // the free space bytes count
345               bytesEnd = j + 1;
346               break innerLoop1;
347             }
348             j--;
349         }
350         innerLoop2: while (j >= 0) {
351             final char c = line.charAt(j);
352             if (!Character.isDigit(c) && c != ',' && c != '.') {
353               // found the next non-numeric character, this is the
354               // beginning of the free space bytes count
355               bytesStart = j + 1;
356               break innerLoop2;
357             }
358             j--;
359         }
360         if (j < 0) {
361             throw new IOException(
362                     "Command line 'dir /-c' did not return valid info " +
363                     "for path '" + path + "'");
364         }
365 
366         // remove commas and dots in the bytes count
367         final StringBuilder buf = new StringBuilder(line.substring(bytesStart, bytesEnd));
368         for (int k = 0; k < buf.length(); k++) {
369             if (buf.charAt(k) == ',' || buf.charAt(k) == '.') {
370                 buf.deleteCharAt(k--);
371             }
372         }
373         return parseBytes(buf.toString(), path);
374     }
375 
376     //-----------------------------------------------------------------------
377     /**
378      * Find free space on the *nix platform using the 'df' command.
379      *
380      * @param path  the path to get free space for
381      * @param kb  whether to normalize to kilobytes
382      * @param posix  whether to use the POSIX standard format flag
383      * @param timeout The timeout amount in milliseconds or no timeout if the value
384      *  is zero or less
385      * @return the amount of free drive space on the volume
386      * @throws IOException if an error occurs
387      */
388     long freeSpaceUnix(final String path, final boolean kb, final boolean posix, final long timeout)
389             throws IOException {
390         if (path.isEmpty()) {
391             throw new IllegalArgumentException("Path must not be empty");
392         }
393 
394         // build and run the 'dir' command
395         String flags = "-";
396         if (kb) {
397             flags += "k";
398         }
399         if (posix) {
400             flags += "P";
401         }
402         final String[] cmdAttribs =
403             flags.length() > 1 ? new String[] {DF, flags, path} : new String[] {DF, path};
404 
405         // perform the command, asking for up to 3 lines (header, interesting, overflow)
406         final List<String> lines = performCommand(cmdAttribs, 3, timeout);
407         if (lines.size() < 2) {
408             // unknown problem, throw exception
409             throw new IOException(
410                     "Command line '" + DF + "' did not return info as expected " +
411                     "for path '" + path + "'- response was " + lines);
412         }
413         final String line2 = lines.get(1); // the line we're interested in
414 
415         // Now, we tokenize the string. The fourth element is what we want.
416         StringTokenizer tok = new StringTokenizer(line2, " ");
417         if (tok.countTokens() < 4) {
418             // could be long Filesystem, thus data on third line
419             if (tok.countTokens() == 1 && lines.size() >= 3) {
420                 final String line3 = lines.get(2); // the line may be interested in
421                 tok = new StringTokenizer(line3, " ");
422             } else {
423                 throw new IOException(
424                         "Command line '" + DF + "' did not return data as expected " +
425                         "for path '" + path + "'- check path is valid");
426             }
427         } else {
428             tok.nextToken(); // Ignore Filesystem
429         }
430         tok.nextToken(); // Ignore 1K-blocks
431         tok.nextToken(); // Ignore Used
432         final String freeSpace = tok.nextToken();
433         return parseBytes(freeSpace, path);
434     }
435 
436     //-----------------------------------------------------------------------
437     /**
438      * Parses the bytes from a string.
439      *
440      * @param freeSpace  the free space string
441      * @param path  the path
442      * @return the number of bytes
443      * @throws IOException if an error occurs
444      */
445     long parseBytes(final String freeSpace, final String path) throws IOException {
446         try {
447             final long bytes = Long.parseLong(freeSpace);
448             if (bytes < 0) {
449                 throw new IOException(
450                         "Command line '" + DF + "' did not find free space in response " +
451                         "for path '" + path + "'- check path is valid");
452             }
453             return bytes;
454 
455         } catch (final NumberFormatException ex) {
456             throw new IOException(
457                     "Command line '" + DF + "' did not return numeric data as expected " +
458                     "for path '" + path + "'- check path is valid", ex);
459         }
460     }
461 
462     //-----------------------------------------------------------------------
463     /**
464      * Performs the os command.
465      *
466      * @param cmdAttribs  the command line parameters
467      * @param max The maximum limit for the lines returned
468      * @param timeout The timeout amount in milliseconds or no timeout if the value
469      *  is zero or less
470      * @return the lines returned by the command, converted to lower-case
471      * @throws IOException if an error occurs
472      */
473     List<String> performCommand(final String[] cmdAttribs, final int max, final long timeout) throws IOException {
474         // this method does what it can to avoid the 'Too many open files' error
475         // based on trial and error and these links:
476         // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4784692
477         // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4801027
478         // http://forum.java.sun.com/thread.jspa?threadID=533029&messageID=2572018
479         // however, its still not perfect as the JDK support is so poor
480         // (see commons-exec or Ant for a better multi-threaded multi-os solution)
481 
482         final List<String> lines = new ArrayList<>(20);
483         Process proc = null;
484         InputStream in = null;
485         OutputStream out = null;
486         InputStream err = null;
487         BufferedReader inr = null;
488         try {
489 
490             final Thread monitor = ThreadMonitor.start(timeout);
491 
492             proc = openProcess(cmdAttribs);
493             in = proc.getInputStream();
494             out = proc.getOutputStream();
495             err = proc.getErrorStream();
496             // default charset is most likely appropriate here
497             inr = new BufferedReader(new InputStreamReader(in, Charset.defaultCharset()));
498             String line = inr.readLine();
499             while (line != null && lines.size() < max) {
500                 line = line.toLowerCase(Locale.ENGLISH).trim();
501                 lines.add(line);
502                 line = inr.readLine();
503             }
504 
505             proc.waitFor();
506 
507             ThreadMonitor.stop(monitor);
508 
509             if (proc.exitValue() != 0) {
510                 // os command problem, throw exception
511                 throw new IOException(
512                         "Command line returned OS error code '" + proc.exitValue() +
513                         "' for command " + Arrays.asList(cmdAttribs));
514             }
515             if (lines.isEmpty()) {
516                 // unknown problem, throw exception
517                 throw new IOException(
518                         "Command line did not return any info " +
519                         "for command " + Arrays.asList(cmdAttribs));
520             }
521 
522             inr.close();
523             inr = null;
524 
525             in.close();
526             in = null;
527 
528             if (out != null) {
529                 out.close();
530                 out = null;
531             }
532 
533             if (err != null) {
534                 err.close();
535                 err = null;
536             }
537 
538             return lines;
539 
540         } catch (final InterruptedException ex) {
541             throw new IOException(
542                     "Command line threw an InterruptedException " +
543                     "for command " + Arrays.asList(cmdAttribs) + " timeout=" + timeout, ex);
544         } finally {
545             IOUtils.closeQuietly(in);
546             IOUtils.closeQuietly(out);
547             IOUtils.closeQuietly(err);
548             IOUtils.closeQuietly(inr);
549             if (proc != null) {
550                 proc.destroy();
551             }
552         }
553     }
554 
555     /**
556      * Opens the process to the operating system.
557      *
558      * @param cmdAttribs  the command line parameters
559      * @return the process
560      * @throws IOException if an error occurs
561      */
562     Process openProcess(final String[] cmdAttribs) throws IOException {
563         return Runtime.getRuntime().exec(cmdAttribs);
564     }
565 
566 }