You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FileUtils.java 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Copyright (C) 2010, Google Inc.
  3. * Copyright (C) 2010, Matthias Sohn <matthias.sohn@sap.com>
  4. * Copyright (C) 2010, Jens Baumgart <jens.baumgart@sap.com>
  5. * and other copyright owners as documented in the project's IP log.
  6. *
  7. * This program and the accompanying materials are made available
  8. * under the terms of the Eclipse Distribution License v1.0 which
  9. * accompanies this distribution, is reproduced below, and is
  10. * available at http://www.eclipse.org/org/documents/edl-v10.php
  11. *
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or
  15. * without modification, are permitted provided that the following
  16. * conditions are met:
  17. *
  18. * - Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials provided
  24. * with the distribution.
  25. *
  26. * - Neither the name of the Eclipse Foundation, Inc. nor the
  27. * names of its contributors may be used to endorse or promote
  28. * products derived from this software without specific prior
  29. * written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  32. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  33. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  34. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  36. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  37. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  38. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  39. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  40. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  41. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  42. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  43. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. */
  45. package org.eclipse.jgit.util;
  46. import java.io.File;
  47. import java.io.IOException;
  48. import java.nio.channels.FileLock;
  49. import java.text.MessageFormat;
  50. import org.eclipse.jgit.JGitText;
  51. /**
  52. * File Utilities
  53. */
  54. public class FileUtils {
  55. /**
  56. * Option to delete given {@code File}
  57. */
  58. public static final int NONE = 0;
  59. /**
  60. * Option to recursively delete given {@code File}
  61. */
  62. public static final int RECURSIVE = 1;
  63. /**
  64. * Option to retry deletion if not successful
  65. */
  66. public static final int RETRY = 2;
  67. /**
  68. * Option to skip deletion if file doesn't exist
  69. */
  70. public static final int SKIP_MISSING = 4;
  71. /**
  72. * Option not to throw exceptions when a deletion finally doesn't succeed.
  73. */
  74. public static final int IGNORE_ERRORS = 8;
  75. /**
  76. * Delete file or empty folder
  77. *
  78. * @param f
  79. * {@code File} to be deleted
  80. * @throws IOException
  81. * if deletion of {@code f} fails. This may occur if {@code f}
  82. * didn't exist when the method was called. This can therefore
  83. * cause IOExceptions during race conditions when multiple
  84. * concurrent threads all try to delete the same file.
  85. */
  86. public static void delete(final File f) throws IOException {
  87. delete(f, NONE);
  88. }
  89. /**
  90. * Delete file or folder
  91. *
  92. * @param f
  93. * {@code File} to be deleted
  94. * @param options
  95. * deletion options, {@code RECURSIVE} for recursive deletion of
  96. * a subtree, {@code RETRY} to retry when deletion failed.
  97. * Retrying may help if the underlying file system doesn't allow
  98. * deletion of files being read by another thread.
  99. * @throws IOException
  100. * if deletion of {@code f} fails. This may occur if {@code f}
  101. * didn't exist when the method was called. This can therefore
  102. * cause IOExceptions during race conditions when multiple
  103. * concurrent threads all try to delete the same file. This
  104. * exception is not thrown when IGNORE_ERRORS is set.
  105. */
  106. public static void delete(final File f, int options) throws IOException {
  107. if ((options & SKIP_MISSING) != 0 && !f.exists())
  108. return;
  109. if ((options & RECURSIVE) != 0 && f.isDirectory()) {
  110. final File[] items = f.listFiles();
  111. if (items != null) {
  112. for (File c : items)
  113. delete(c, options);
  114. }
  115. }
  116. if (!f.delete()) {
  117. if ((options & RETRY) != 0 && f.exists()) {
  118. for (int i = 1; i < 10; i++) {
  119. try {
  120. Thread.sleep(100);
  121. } catch (InterruptedException e) {
  122. // ignore
  123. }
  124. if (f.delete())
  125. return;
  126. }
  127. }
  128. if ((options & IGNORE_ERRORS) == 0)
  129. throw new IOException(MessageFormat.format(
  130. JGitText.get().deleteFileFailed, f.getAbsolutePath()));
  131. }
  132. }
  133. /**
  134. * Creates the directory named by this abstract pathname.
  135. *
  136. * @param d
  137. * directory to be created
  138. * @throws IOException
  139. * if creation of {@code d} fails. This may occur if {@code d}
  140. * did exist when the method was called. This can therefore
  141. * cause IOExceptions during race conditions when multiple
  142. * concurrent threads all try to create the same directory.
  143. */
  144. public static void mkdir(final File d)
  145. throws IOException {
  146. mkdir(d, false);
  147. }
  148. /**
  149. * Creates the directory named by this abstract pathname.
  150. *
  151. * @param d
  152. * directory to be created
  153. * @param skipExisting
  154. * if {@code true} skip creation of the given directory if it
  155. * already exists in the file system
  156. * @throws IOException
  157. * if creation of {@code d} fails. This may occur if {@code d}
  158. * did exist when the method was called. This can therefore
  159. * cause IOExceptions during race conditions when multiple
  160. * concurrent threads all try to create the same directory.
  161. */
  162. public static void mkdir(final File d, boolean skipExisting)
  163. throws IOException {
  164. if (!d.mkdir()) {
  165. if (skipExisting && d.isDirectory())
  166. return;
  167. throw new IOException(MessageFormat.format(
  168. JGitText.get().mkDirFailed, d.getAbsolutePath()));
  169. }
  170. }
  171. /**
  172. * Creates the directory named by this abstract pathname, including any
  173. * necessary but nonexistent parent directories. Note that if this operation
  174. * fails it may have succeeded in creating some of the necessary parent
  175. * directories.
  176. *
  177. * @param d
  178. * directory to be created
  179. * @throws IOException
  180. * if creation of {@code d} fails. This may occur if {@code d}
  181. * did exist when the method was called. This can therefore
  182. * cause IOExceptions during race conditions when multiple
  183. * concurrent threads all try to create the same directory.
  184. */
  185. public static void mkdirs(final File d) throws IOException {
  186. mkdirs(d, false);
  187. }
  188. /**
  189. * Creates the directory named by this abstract pathname, including any
  190. * necessary but nonexistent parent directories. Note that if this operation
  191. * fails it may have succeeded in creating some of the necessary parent
  192. * directories.
  193. *
  194. * @param d
  195. * directory to be created
  196. * @param skipExisting
  197. * if {@code true} skip creation of the given directory if it
  198. * already exists in the file system
  199. * @throws IOException
  200. * if creation of {@code d} fails. This may occur if {@code d}
  201. * did exist when the method was called. This can therefore
  202. * cause IOExceptions during race conditions when multiple
  203. * concurrent threads all try to create the same directory.
  204. */
  205. public static void mkdirs(final File d, boolean skipExisting)
  206. throws IOException {
  207. if (!d.mkdirs()) {
  208. if (skipExisting && d.isDirectory())
  209. return;
  210. throw new IOException(MessageFormat.format(
  211. JGitText.get().mkDirsFailed, d.getAbsolutePath()));
  212. }
  213. }
  214. /**
  215. * Atomically creates a new, empty file named by this abstract pathname if
  216. * and only if a file with this name does not yet exist. The check for the
  217. * existence of the file and the creation of the file if it does not exist
  218. * are a single operation that is atomic with respect to all other
  219. * filesystem activities that might affect the file.
  220. * <p>
  221. * Note: this method should not be used for file-locking, as the resulting
  222. * protocol cannot be made to work reliably. The {@link FileLock} facility
  223. * should be used instead.
  224. *
  225. * @param f
  226. * the file to be created
  227. * @throws IOException
  228. * if the named file already exists or if an I/O error occurred
  229. */
  230. public static void createNewFile(File f) throws IOException {
  231. if (!f.createNewFile())
  232. throw new IOException(MessageFormat.format(
  233. JGitText.get().createNewFileFailed, f));
  234. }
  235. }