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.4KB

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