< prev index next >

test/lib/jdk/test/lib/util/FileUtils.java

Print this page
rev 50699 : 8202292: java/io/FileOutputStream/UnreferencedFOSClosesFd.java fails with "raw fd count wrong"
   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


 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 }
   1 /*
   2  * Copyright (c) 2017, 2018, 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.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      *
  62      * @throws NoSuchFileException
  63      *         if the file does not exist (optional specific exception)
  64      * @throws DirectoryNotEmptyException
  65      *         if the file is a directory and could not otherwise be deleted
  66      *         because the directory is not empty (optional specific exception)
  67      * @throws IOException
  68      *         if an I/O error occurs


 217             System.out.println("\n--- df output ---");
 218             System.out.flush();
 219             Process proc = new ProcessBuilder("df").inheritIO().start();
 220             try {
 221                 proc.waitFor(90, TimeUnit.SECONDS);
 222             } catch (InterruptedException ignored) {
 223             }
 224             try {
 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 }
< prev index next >