1 /* |
|
2 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 package jdk.testlibrary; |
|
25 |
|
26 import java.io.IOException; |
|
27 import java.nio.file.DirectoryNotEmptyException; |
|
28 import java.nio.file.FileVisitResult; |
|
29 import java.nio.file.Files; |
|
30 import java.nio.file.NoSuchFileException; |
|
31 import java.nio.file.Path; |
|
32 import java.nio.file.SimpleFileVisitor; |
|
33 import java.nio.file.attribute.BasicFileAttributes; |
|
34 import java.util.ArrayList; |
|
35 import java.util.List; |
|
36 import java.util.concurrent.TimeUnit; |
|
37 |
|
38 |
|
39 /** |
|
40 * Common library for various test file utility functions. |
|
41 */ |
|
42 public final class FileUtils { |
|
43 |
|
44 private static final boolean isWindows = |
|
45 System.getProperty("os.name").startsWith("Windows"); |
|
46 private static final int RETRY_DELETE_MILLIS = isWindows ? 500 : 0; |
|
47 private static final int MAX_RETRY_DELETE_TIMES = isWindows ? 15 : 0; |
|
48 |
|
49 /** |
|
50 * Deletes a file, retrying if necessary. |
|
51 * |
|
52 * @param path the file to delete |
|
53 * |
|
54 * @throws NoSuchFileException |
|
55 * if the file does not exist (optional specific exception) |
|
56 * @throws DirectoryNotEmptyException |
|
57 * if the file is a directory and could not otherwise be deleted |
|
58 * because the directory is not empty (optional specific exception) |
|
59 * @throws IOException |
|
60 * if an I/O error occurs |
|
61 */ |
|
62 public static void deleteFileWithRetry(Path path) |
|
63 throws IOException |
|
64 { |
|
65 try { |
|
66 deleteFileWithRetry0(path); |
|
67 } catch (InterruptedException x) { |
|
68 throw new IOException("Interrupted while deleting.", x); |
|
69 } |
|
70 } |
|
71 |
|
72 /** |
|
73 * Deletes a file, retrying if necessary. |
|
74 * No exception thrown if file doesn't exist. |
|
75 * |
|
76 * @param path the file to delete |
|
77 * |
|
78 * @throws NoSuchFileException |
|
79 * if the file does not exist (optional specific exception) |
|
80 * @throws DirectoryNotEmptyException |
|
81 * if the file is a directory and could not otherwise be deleted |
|
82 * because the directory is not empty (optional specific exception) |
|
83 * @throws IOException |
|
84 * if an I/O error occurs |
|
85 */ |
|
86 public static void deleteFileIfExistsWithRetry(Path path) |
|
87 throws IOException |
|
88 { |
|
89 try { |
|
90 if(Files.exists(path)) |
|
91 deleteFileWithRetry0(path); |
|
92 } catch (InterruptedException x) { |
|
93 throw new IOException("Interrupted while deleting.", x); |
|
94 } |
|
95 } |
|
96 |
|
97 private static void deleteFileWithRetry0(Path path) |
|
98 throws IOException, InterruptedException |
|
99 { |
|
100 int times = 0; |
|
101 IOException ioe = null; |
|
102 while (true) { |
|
103 try { |
|
104 Files.delete(path); |
|
105 while (Files.exists(path)) { |
|
106 times++; |
|
107 if (times > MAX_RETRY_DELETE_TIMES) |
|
108 throw new IOException("File still exists after " + times + " waits."); |
|
109 Thread.sleep(RETRY_DELETE_MILLIS); |
|
110 } |
|
111 break; |
|
112 } catch (NoSuchFileException | DirectoryNotEmptyException x) { |
|
113 throw x; |
|
114 } catch (IOException x) { |
|
115 // Backoff/retry in case another process is accessing the file |
|
116 times++; |
|
117 if (ioe == null) |
|
118 ioe = x; |
|
119 else |
|
120 ioe.addSuppressed(x); |
|
121 |
|
122 if (times > MAX_RETRY_DELETE_TIMES) |
|
123 throw ioe; |
|
124 Thread.sleep(RETRY_DELETE_MILLIS); |
|
125 } |
|
126 } |
|
127 } |
|
128 |
|
129 /** |
|
130 * Deletes a directory and its subdirectories, retrying if necessary. |
|
131 * |
|
132 * @param dir the directory to delete |
|
133 * |
|
134 * @throws IOException |
|
135 * If an I/O error occurs. Any such exceptions are caught |
|
136 * internally. If only one is caught, then it is re-thrown. |
|
137 * If more than one exception is caught, then the second and |
|
138 * following exceptions are added as suppressed exceptions of the |
|
139 * first one caught, which is then re-thrown. |
|
140 */ |
|
141 public static void deleteFileTreeWithRetry(Path dir) |
|
142 throws IOException |
|
143 { |
|
144 IOException ioe = null; |
|
145 final List<IOException> excs = deleteFileTreeUnchecked(dir); |
|
146 if (!excs.isEmpty()) { |
|
147 ioe = excs.remove(0); |
|
148 for (IOException x : excs) |
|
149 ioe.addSuppressed(x); |
|
150 } |
|
151 if (ioe != null) |
|
152 throw ioe; |
|
153 } |
|
154 |
|
155 public static List<IOException> deleteFileTreeUnchecked(Path dir) { |
|
156 final List<IOException> excs = new ArrayList<>(); |
|
157 try { |
|
158 java.nio.file.Files.walkFileTree(dir, new SimpleFileVisitor<Path>() { |
|
159 @Override |
|
160 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { |
|
161 try { |
|
162 deleteFileWithRetry0(file); |
|
163 } catch (IOException x) { |
|
164 excs.add(x); |
|
165 } catch (InterruptedException x) { |
|
166 excs.add(new IOException("Interrupted while deleting.", x)); |
|
167 return FileVisitResult.TERMINATE; |
|
168 } |
|
169 return FileVisitResult.CONTINUE; |
|
170 } |
|
171 @Override |
|
172 public FileVisitResult postVisitDirectory(Path dir, IOException exc) { |
|
173 try { |
|
174 deleteFileWithRetry0(dir); |
|
175 } catch (IOException x) { |
|
176 excs.add(x); |
|
177 } catch (InterruptedException x) { |
|
178 excs.add(new IOException("Interrupted while deleting.", x)); |
|
179 return FileVisitResult.TERMINATE; |
|
180 } |
|
181 return FileVisitResult.CONTINUE; |
|
182 } |
|
183 @Override |
|
184 public FileVisitResult visitFileFailed(Path file, IOException exc) { |
|
185 excs.add(exc); |
|
186 return FileVisitResult.CONTINUE; |
|
187 } |
|
188 }); |
|
189 } catch (IOException x) { |
|
190 excs.add(x); |
|
191 } |
|
192 return excs; |
|
193 } |
|
194 |
|
195 /** |
|
196 * Checks whether all file systems are accessible. This is performed |
|
197 * by checking free disk space on all mounted file systems via a |
|
198 * separate, spawned process. File systems are considered to be |
|
199 * accessible if this process completes successfully before a given |
|
200 * fixed duration has elapsed. |
|
201 * |
|
202 * @implNote On Unix this executes the {@code df} command in a separate |
|
203 * process and on Windows always returns {@code true}. |
|
204 */ |
|
205 public static boolean areFileSystemsAccessible() throws IOException { |
|
206 boolean areFileSystemsAccessible = true; |
|
207 if (!isWindows) { |
|
208 // try to check whether 'df' hangs |
|
209 System.out.println("\n--- df output ---"); |
|
210 System.out.flush(); |
|
211 Process proc = new ProcessBuilder("df").inheritIO().start(); |
|
212 try { |
|
213 proc.waitFor(90, TimeUnit.SECONDS); |
|
214 } catch (InterruptedException ignored) { |
|
215 } |
|
216 try { |
|
217 int exitValue = proc.exitValue(); |
|
218 if (exitValue != 0) { |
|
219 System.err.printf("df process exited with %d != 0%n", |
|
220 exitValue); |
|
221 areFileSystemsAccessible = false; |
|
222 } |
|
223 } catch (IllegalThreadStateException ignored) { |
|
224 System.err.println("df command apparently hung"); |
|
225 areFileSystemsAccessible = false; |
|
226 } |
|
227 } |
|
228 return areFileSystemsAccessible; |
|
229 } |
|
230 } |
|