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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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.internal.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. * @since 2.0
  74. */
  75. public static final int IGNORE_ERRORS = 8;
  76. /**
  77. * Delete file or empty folder
  78. *
  79. * @param f
  80. * {@code File} to be deleted
  81. * @throws IOException
  82. * if deletion of {@code f} fails. This may occur if {@code f}
  83. * didn't exist when the method was called. This can therefore
  84. * cause IOExceptions during race conditions when multiple
  85. * concurrent threads all try to delete the same file.
  86. */
  87. public static void delete(final File f) throws IOException {
  88. delete(f, NONE);
  89. }
  90. /**
  91. * Delete file or folder
  92. *
  93. * @param f
  94. * {@code File} to be deleted
  95. * @param options
  96. * deletion options, {@code RECURSIVE} for recursive deletion of
  97. * a subtree, {@code RETRY} to retry when deletion failed.
  98. * Retrying may help if the underlying file system doesn't allow
  99. * deletion of files being read by another thread.
  100. * @throws IOException
  101. * if deletion of {@code f} fails. This may occur if {@code f}
  102. * didn't exist when the method was called. This can therefore
  103. * cause IOExceptions during race conditions when multiple
  104. * concurrent threads all try to delete the same file. This
  105. * exception is not thrown when IGNORE_ERRORS is set.
  106. */
  107. public static void delete(final File f, int options) throws IOException {
  108. if ((options & SKIP_MISSING) != 0 && !f.exists())
  109. return;
  110. if ((options & RECURSIVE) != 0 && f.isDirectory()) {
  111. final File[] items = f.listFiles();
  112. if (items != null) {
  113. for (File c : items)
  114. delete(c, options);
  115. }
  116. }
  117. if (!f.delete()) {
  118. if ((options & RETRY) != 0 && f.exists()) {
  119. for (int i = 1; i < 10; i++) {
  120. try {
  121. Thread.sleep(100);
  122. } catch (InterruptedException e) {
  123. // ignore
  124. }
  125. if (f.delete())
  126. return;
  127. }
  128. }
  129. if ((options & IGNORE_ERRORS) == 0)
  130. throw new IOException(MessageFormat.format(
  131. JGitText.get().deleteFileFailed, f.getAbsolutePath()));
  132. }
  133. }
  134. /**
  135. * Creates the directory named by this abstract pathname.
  136. *
  137. * @param d
  138. * directory to be created
  139. * @throws IOException
  140. * if creation of {@code d} fails. This may occur if {@code d}
  141. * did exist when the method was called. This can therefore
  142. * cause IOExceptions during race conditions when multiple
  143. * concurrent threads all try to create the same directory.
  144. */
  145. public static void mkdir(final File d)
  146. throws IOException {
  147. mkdir(d, false);
  148. }
  149. /**
  150. * Creates the directory named by this abstract pathname.
  151. *
  152. * @param d
  153. * directory to be created
  154. * @param skipExisting
  155. * if {@code true} skip creation of the given directory if it
  156. * already exists in the file system
  157. * @throws IOException
  158. * if creation of {@code d} fails. This may occur if {@code d}
  159. * did exist when the method was called. This can therefore
  160. * cause IOExceptions during race conditions when multiple
  161. * concurrent threads all try to create the same directory.
  162. */
  163. public static void mkdir(final File d, boolean skipExisting)
  164. throws IOException {
  165. if (!d.mkdir()) {
  166. if (skipExisting && d.isDirectory())
  167. return;
  168. throw new IOException(MessageFormat.format(
  169. JGitText.get().mkDirFailed, d.getAbsolutePath()));
  170. }
  171. }
  172. /**
  173. * Creates the directory named by this abstract pathname, including any
  174. * necessary but nonexistent parent directories. Note that if this operation
  175. * fails it may have succeeded in creating some of the necessary parent
  176. * directories.
  177. *
  178. * @param d
  179. * directory to be created
  180. * @throws IOException
  181. * if creation of {@code d} fails. This may occur if {@code d}
  182. * did exist when the method was called. This can therefore
  183. * cause IOExceptions during race conditions when multiple
  184. * concurrent threads all try to create the same directory.
  185. */
  186. public static void mkdirs(final File d) throws IOException {
  187. mkdirs(d, false);
  188. }
  189. /**
  190. * Creates the directory named by this abstract pathname, including any
  191. * necessary but nonexistent parent directories. Note that if this operation
  192. * fails it may have succeeded in creating some of the necessary parent
  193. * directories.
  194. *
  195. * @param d
  196. * directory to be created
  197. * @param skipExisting
  198. * if {@code true} skip creation of the given directory if it
  199. * already exists in the file system
  200. * @throws IOException
  201. * if creation of {@code d} fails. This may occur if {@code d}
  202. * did exist when the method was called. This can therefore
  203. * cause IOExceptions during race conditions when multiple
  204. * concurrent threads all try to create the same directory.
  205. */
  206. public static void mkdirs(final File d, boolean skipExisting)
  207. throws IOException {
  208. if (!d.mkdirs()) {
  209. if (skipExisting && d.isDirectory())
  210. return;
  211. throw new IOException(MessageFormat.format(
  212. JGitText.get().mkDirsFailed, d.getAbsolutePath()));
  213. }
  214. }
  215. /**
  216. * Atomically creates a new, empty file named by this abstract pathname if
  217. * and only if a file with this name does not yet exist. The check for the
  218. * existence of the file and the creation of the file if it does not exist
  219. * are a single operation that is atomic with respect to all other
  220. * filesystem activities that might affect the file.
  221. * <p>
  222. * Note: this method should not be used for file-locking, as the resulting
  223. * protocol cannot be made to work reliably. The {@link FileLock} facility
  224. * should be used instead.
  225. *
  226. * @param f
  227. * the file to be created
  228. * @throws IOException
  229. * if the named file already exists or if an I/O error occurred
  230. */
  231. public static void createNewFile(File f) throws IOException {
  232. if (!f.createNewFile())
  233. throw new IOException(MessageFormat.format(
  234. JGitText.get().createNewFileFailed, f));
  235. }
  236. }