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
  69      */
  70     public static void deleteFileWithRetry(Path path) throws IOException {
  71         try {
  72             deleteFileWithRetry0(path);
  73         } catch (InterruptedException x) {
  74             throw new IOException("Interrupted while deleting.", x);
  75         }
  76     }
  77 
  78     /**
  79      * Deletes a file, retrying if necessary.
  80      * No exception thrown if file doesn't exist.
  81      *
  82      * @param path  the file to delete
  83      *
  84      * @throws NoSuchFileException
  85      *         if the file does not exist (optional specific exception)
  86      * @throws DirectoryNotEmptyException
  87      *         if the file is a directory and could not otherwise be deleted
  88      *         because the directory is not empty (optional specific exception)
  89      * @throws IOException
  90      *         if an I/O error occurs
  91      */
  92     public static void deleteFileIfExistsWithRetry(Path path) throws IOException {
  93         try {
  94             if (Files.exists(path)) {
  95                 deleteFileWithRetry0(path);
  96             }
  97         } catch (InterruptedException x) {
  98             throw new IOException("Interrupted while deleting.", x);
  99         }
 100     }
 101 
 102     private static void deleteFileWithRetry0(Path path)
 103             throws IOException, InterruptedException {
 104         int times = 0;
 105         IOException ioe = null;
 106         while (true) {
 107             try {
 108                 Files.delete(path);
 109                 // Checks for absence of the file. Semantics of Files.exists() is not the same.
 110                 while (!Files.notExists(path)) {
 111                     times++;
 112                     if (times > MAX_RETRY_DELETE_TIMES) {
 113                         throw new IOException("File still exists after " + times + " waits.");
 114                     }
 115                     Thread.sleep(RETRY_DELETE_MILLIS);
 116                 }
 117                 break;
 118             } catch (NoSuchFileException | DirectoryNotEmptyException x) {
 119                 throw x;
 120             } catch (IOException x) {
 121                 // Backoff/retry in case another process is accessing the file
 122                 times++;
 123                 if (ioe == null) {
 124                     ioe = x;
 125                 } else {
 126                     ioe.addSuppressed(x);
 127                 }
 128 
 129                 if (times > MAX_RETRY_DELETE_TIMES) {
 130                     throw ioe;
 131                 }
 132                 Thread.sleep(RETRY_DELETE_MILLIS);
 133             }
 134         }
 135     }
 136 
 137     /**
 138      * Deletes a directory and its subdirectories, retrying if necessary.
 139      *
 140      * @param dir  the directory to delete
 141      *
 142      * @throws  IOException
 143      *          If an I/O error occurs. Any such exceptions are caught
 144      *          internally. If only one is caught, then it is re-thrown.
 145      *          If more than one exception is caught, then the second and
 146      *          following exceptions are added as suppressed exceptions of the
 147      *          first one caught, which is then re-thrown.
 148      */
 149     public static void deleteFileTreeWithRetry(Path dir) throws IOException {
 150         IOException ioe = null;
 151         final List<IOException> excs = deleteFileTreeUnchecked(dir);
 152         if (!excs.isEmpty()) {
 153             ioe = excs.remove(0);
 154             for (IOException x : excs) {
 155                 ioe.addSuppressed(x);
 156             }
 157         }
 158         if (ioe != null) {
 159             throw ioe;
 160         }
 161     }
 162 
 163     public static List<IOException> deleteFileTreeUnchecked(Path dir) {
 164         final List<IOException> excs = new ArrayList<>();
 165         try {
 166             java.nio.file.Files.walkFileTree(dir, new SimpleFileVisitor<>() {
 167                 @Override
 168                 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
 169                     try {
 170                         deleteFileWithRetry0(file);
 171                     } catch (IOException x) {
 172                         excs.add(x);
 173                     } catch (InterruptedException x) {
 174                         excs.add(new IOException("Interrupted while deleting.", x));
 175                         return FileVisitResult.TERMINATE;
 176                     }
 177                     return FileVisitResult.CONTINUE;
 178                 }
 179                 @Override
 180                 public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
 181                     try {
 182                         deleteFileWithRetry0(dir);
 183                     } catch (IOException x) {
 184                         excs.add(x);
 185                     } catch (InterruptedException x) {
 186                         excs.add(new IOException("Interrupted while deleting.", x));
 187                         return FileVisitResult.TERMINATE;
 188                     }
 189                     return FileVisitResult.CONTINUE;
 190                 }
 191                 @Override
 192                 public FileVisitResult visitFileFailed(Path file, IOException exc) {
 193                     excs.add(exc);
 194                     return FileVisitResult.CONTINUE;
 195                 }
 196             });
 197         } catch (IOException x) {
 198             excs.add(x);
 199         }
 200         return excs;
 201     }
 202 
 203     /**
 204      * Checks whether all file systems are accessible. This is performed
 205      * by checking free disk space on all mounted file systems via a
 206      * separate, spawned process. File systems are considered to be
 207      * accessible if this process completes successfully before a given
 208      * fixed duration has elapsed.
 209      *
 210      * @implNote On Unix this executes the {@code df} command in a separate
 211      * process and on Windows always returns {@code true}.
 212      */
 213     public static boolean areFileSystemsAccessible() throws IOException {
 214         boolean areFileSystemsAccessible = true;
 215         if (!IS_WINDOWS) {
 216             // try to check whether 'df' hangs
 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 }