22 */
23
24 package jdk.test.lib.util;
25
26 import jdk.test.lib.Platform;
27
28 import java.io.IOException;
29 import java.io.PrintStream;
30 import java.io.UncheckedIOException;
31 import java.lang.ProcessBuilder.Redirect;
32 import java.nio.file.DirectoryNotEmptyException;
33 import java.nio.file.FileVisitResult;
34 import java.nio.file.Files;
35 import java.nio.file.NoSuchFileException;
36 import java.nio.file.Path;
37 import java.nio.file.Paths;
38 import java.nio.file.SimpleFileVisitor;
39 import java.nio.file.attribute.BasicFileAttributes;
40 import java.time.Instant;
41 import java.time.Duration;
42 import java.util.ArrayList;
43 import java.util.ArrayDeque;
44 import java.util.HashSet;
45 import java.util.List;
46 import java.util.Optional;
47 import java.util.concurrent.TimeUnit;
48
49 /**
50 * Common library for various test file utility functions.
51 */
52 public final class FileUtils {
53 private static final boolean IS_WINDOWS = Platform.isWindows();
54 private static final int RETRY_DELETE_MILLIS = IS_WINDOWS ? 500 : 0;
55 private static final int MAX_RETRY_DELETE_TIMES = IS_WINDOWS ? 15 : 0;
56
57 /**
58 * Deletes a file, retrying if necessary.
59 *
60 * @param path the file to delete
61 *
225 int exitValue = proc.exitValue();
226 if (exitValue != 0) {
227 System.err.printf("df process exited with %d != 0%n",
228 exitValue);
229 areFileSystemsAccessible = false;
230 }
231 } catch (IllegalThreadStateException ignored) {
232 System.err.println("df command apparently hung");
233 areFileSystemsAccessible = false;
234 }
235 }
236 return areFileSystemsAccessible;
237 }
238
239 /**
240 * List the open file descriptors (if supported by the 'lsof' command).
241 * @param ps a printStream to send the output to
242 * @throws UncheckedIOException if an error occurs
243 */
244 public static void listFileDescriptors(PrintStream ps) {
245 List<String> lsofDirs = List.of("/usr/bin", "/usr/sbin");
246 Optional<Path> lsof = lsofDirs.stream()
247 .map(s -> Paths.get(s, "lsof"))
248 .filter(f -> Files.isExecutable(f))
249 .findFirst();
250 lsof.ifPresent(exe -> {
251 try {
252 ps.printf("Open File Descriptors:%n");
253 long pid = ProcessHandle.current().pid();
254 ProcessBuilder pb = new ProcessBuilder(exe.toString(), "-p", Integer.toString((int) pid));
255 pb.redirectErrorStream(true); // combine stderr and stdout
256 pb.redirectOutput(Redirect.PIPE);
257
258 Process p = pb.start();
259 Instant start = Instant.now();
260 p.getInputStream().transferTo(ps);
261
262 try {
263 int timeout = 10;
264 if (!p.waitFor(timeout, TimeUnit.SECONDS)) {
265 System.out.printf("waitFor timed out: %d%n", timeout);
266 }
267 } catch (InterruptedException ie) {
268 throw new IOException("interrupted", ie);
269 }
270 ps.println();
271 } catch (IOException ioe) {
272 throw new UncheckedIOException("error listing file descriptors", ioe);
273 }
274 });
275 }
276 }
|
22 */
23
24 package jdk.test.lib.util;
25
26 import jdk.test.lib.Platform;
27
28 import java.io.IOException;
29 import java.io.PrintStream;
30 import java.io.UncheckedIOException;
31 import java.lang.ProcessBuilder.Redirect;
32 import java.nio.file.DirectoryNotEmptyException;
33 import java.nio.file.FileVisitResult;
34 import java.nio.file.Files;
35 import java.nio.file.NoSuchFileException;
36 import java.nio.file.Path;
37 import java.nio.file.Paths;
38 import java.nio.file.SimpleFileVisitor;
39 import java.nio.file.attribute.BasicFileAttributes;
40 import java.time.Instant;
41 import java.time.Duration;
42 import java.util.Arrays;
43 import java.util.ArrayList;
44 import java.util.ArrayDeque;
45 import java.util.HashSet;
46 import java.util.List;
47 import java.util.Optional;
48 import java.util.concurrent.TimeUnit;
49
50 /**
51 * Common library for various test file utility functions.
52 */
53 public final class FileUtils {
54 private static final boolean IS_WINDOWS = Platform.isWindows();
55 private static final int RETRY_DELETE_MILLIS = IS_WINDOWS ? 500 : 0;
56 private static final int MAX_RETRY_DELETE_TIMES = IS_WINDOWS ? 15 : 0;
57
58 /**
59 * Deletes a file, retrying if necessary.
60 *
61 * @param path the file to delete
62 *
226 int exitValue = proc.exitValue();
227 if (exitValue != 0) {
228 System.err.printf("df process exited with %d != 0%n",
229 exitValue);
230 areFileSystemsAccessible = false;
231 }
232 } catch (IllegalThreadStateException ignored) {
233 System.err.println("df command apparently hung");
234 areFileSystemsAccessible = false;
235 }
236 }
237 return areFileSystemsAccessible;
238 }
239
240 /**
241 * List the open file descriptors (if supported by the 'lsof' command).
242 * @param ps a printStream to send the output to
243 * @throws UncheckedIOException if an error occurs
244 */
245 public static void listFileDescriptors(PrintStream ps) {
246
247 Optional<String[]> lsof = Arrays.stream(lsCommands)
248 .filter(args -> Files.isExecutable(Path.of(args[0])))
249 .findFirst();
250 lsof.ifPresent(args -> {
251 try {
252 ps.printf("Open File Descriptors:%n");
253 long pid = ProcessHandle.current().pid();
254 ProcessBuilder pb = new ProcessBuilder(args[0], args[1], Integer.toString((int) pid));
255 pb.redirectErrorStream(true); // combine stderr and stdout
256 pb.redirectOutput(Redirect.PIPE);
257
258 Process p = pb.start();
259 Instant start = Instant.now();
260 p.getInputStream().transferTo(ps);
261
262 try {
263 int timeout = 10;
264 if (!p.waitFor(timeout, TimeUnit.SECONDS)) {
265 System.out.printf("waitFor timed out: %d%n", timeout);
266 }
267 } catch (InterruptedException ie) {
268 throw new IOException("interrupted", ie);
269 }
270 ps.println();
271 } catch (IOException ioe) {
272 throw new UncheckedIOException("error listing file descriptors", ioe);
273 }
274 });
275 }
276
277 // Possible command locations and arguments
278 static String[][] lsCommands = new String[][] {
279 {"/usr/bin/lsof", "-p"},
280 {"/usr/sbin/lsof", "-p"},
281 {"/bin/lsof", "-p"},
282 {"/sbin/lsof", "-p"},
283 {"/usr/local/bin/lsof", "-p"},
284 {"/usr/bin/pfiles", "-F"}, // Solaris
285 };
286 }
|