PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/org/apache/commons/io/FileSystemUtils.java

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