author | anazarov |
Fri, 22 Sep 2017 11:49:53 -0700 | |
changeset 47249 | 82c43b3c0ea0 |
parent 45920 | ddc079e73172 |
child 50727 | 081b132c4dc0 |
permissions | -rw-r--r-- |
45267 | 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.test.lib.util; |
|
25 |
||
26 |
import jdk.test.lib.Platform; |
|
27 |
||
28 |
import java.io.IOException; |
|
29 |
import java.nio.file.DirectoryNotEmptyException; |
|
30 |
import java.nio.file.FileVisitResult; |
|
31 |
import java.nio.file.Files; |
|
32 |
import java.nio.file.NoSuchFileException; |
|
33 |
import java.nio.file.Path; |
|
34 |
import java.nio.file.SimpleFileVisitor; |
|
35 |
import java.nio.file.attribute.BasicFileAttributes; |
|
36 |
import java.util.ArrayList; |
|
37 |
import java.util.List; |
|
38 |
import java.util.concurrent.TimeUnit; |
|
39 |
||
40 |
||
41 |
/** |
|
42 |
* Common library for various test file utility functions. |
|
43 |
*/ |
|
44 |
public final class FileUtils { |
|
45 |
private static final boolean IS_WINDOWS = Platform.isWindows(); |
|
46 |
private static final int RETRY_DELETE_MILLIS = IS_WINDOWS ? 500 : 0; |
|
47 |
private static final int MAX_RETRY_DELETE_TIMES = IS_WINDOWS ? 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) throws IOException { |
|
63 |
try { |
|
64 |
deleteFileWithRetry0(path); |
|
65 |
} catch (InterruptedException x) { |
|
66 |
throw new IOException("Interrupted while deleting.", x); |
|
67 |
} |
|
68 |
} |
|
69 |
||
70 |
/** |
|
71 |
* Deletes a file, retrying if necessary. |
|
72 |
* No exception thrown if file doesn't exist. |
|
73 |
* |
|
74 |
* @param path the file to delete |
|
75 |
* |
|
76 |
* @throws NoSuchFileException |
|
77 |
* if the file does not exist (optional specific exception) |
|
78 |
* @throws DirectoryNotEmptyException |
|
79 |
* if the file is a directory and could not otherwise be deleted |
|
80 |
* because the directory is not empty (optional specific exception) |
|
81 |
* @throws IOException |
|
82 |
* if an I/O error occurs |
|
83 |
*/ |
|
84 |
public static void deleteFileIfExistsWithRetry(Path path) throws IOException { |
|
85 |
try { |
|
86 |
if (Files.exists(path)) { |
|
87 |
deleteFileWithRetry0(path); |
|
88 |
} |
|
89 |
} catch (InterruptedException x) { |
|
90 |
throw new IOException("Interrupted while deleting.", x); |
|
91 |
} |
|
92 |
} |
|
93 |
||
94 |
private static void deleteFileWithRetry0(Path path) |
|
95 |
throws IOException, InterruptedException { |
|
96 |
int times = 0; |
|
97 |
IOException ioe = null; |
|
98 |
while (true) { |
|
99 |
try { |
|
100 |
Files.delete(path); |
|
47249
82c43b3c0ea0
8185458: Added comment for Files.notExists in FileUtils test library
anazarov
parents:
45920
diff
changeset
|
101 |
// Checks for absence of the file. Semantics of Files.exists() is not the same. |
45920
ddc079e73172
8184961: jdk.test.lib.util.FileUtils.deleteFileWithRetry0 should wait for absence of a file
anazarov
parents:
45267
diff
changeset
|
102 |
while (!Files.notExists(path)) { |
45267 | 103 |
times++; |
104 |
if (times > MAX_RETRY_DELETE_TIMES) { |
|
105 |
throw new IOException("File still exists after " + times + " waits."); |
|
106 |
} |
|
107 |
Thread.sleep(RETRY_DELETE_MILLIS); |
|
108 |
} |
|
109 |
break; |
|
110 |
} catch (NoSuchFileException | DirectoryNotEmptyException x) { |
|
111 |
throw x; |
|
112 |
} catch (IOException x) { |
|
113 |
// Backoff/retry in case another process is accessing the file |
|
114 |
times++; |
|
115 |
if (ioe == null) { |
|
116 |
ioe = x; |
|
117 |
} else { |
|
118 |
ioe.addSuppressed(x); |
|
119 |
} |
|
120 |
||
121 |
if (times > MAX_RETRY_DELETE_TIMES) { |
|
122 |
throw ioe; |
|
123 |
} |
|
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) throws IOException { |
|
142 |
IOException ioe = null; |
|
143 |
final List<IOException> excs = deleteFileTreeUnchecked(dir); |
|
144 |
if (!excs.isEmpty()) { |
|
145 |
ioe = excs.remove(0); |
|
146 |
for (IOException x : excs) { |
|
147 |
ioe.addSuppressed(x); |
|
148 |
} |
|
149 |
} |
|
150 |
if (ioe != null) { |
|
151 |
throw ioe; |
|
152 |
} |
|
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<>() { |
|
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 (!IS_WINDOWS) { |
|
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 |
} |