1 /*
   2  * Copyright (c) 2017, 2020, 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.BufferedReader;
  29 import java.io.InputStreamReader;
  30 import java.io.IOException;
  31 import java.io.PrintStream;
  32 import java.io.UncheckedIOException;
  33 import java.lang.ProcessBuilder.Redirect;
  34 import java.nio.file.DirectoryNotEmptyException;
  35 import java.nio.file.FileVisitResult;
  36 import java.nio.file.Files;
  37 import java.nio.file.NoSuchFileException;
  38 import java.nio.file.Path;
  39 import java.nio.file.Paths;
  40 import java.nio.file.SimpleFileVisitor;
  41 import java.nio.file.attribute.BasicFileAttributes;
  42 import java.time.Instant;
  43 import java.time.Duration;
  44 import java.util.Arrays;
  45 import java.util.ArrayList;
  46 import java.util.ArrayDeque;
  47 import java.util.HashSet;
  48 import java.util.List;
  49 import java.util.Optional;
  50 import java.util.Set;
  51 import java.util.concurrent.atomic.AtomicBoolean;
  52 import java.util.concurrent.atomic.AtomicReference;
  53 import java.util.concurrent.TimeUnit;
  54 
  55 /**
  56  * Common library for various test file utility functions.
  57  */
  58 public final class FileUtils {
  59     private static final boolean IS_WINDOWS = Platform.isWindows();
  60     private static final int RETRY_DELETE_MILLIS = IS_WINDOWS ? 500 : 0;
  61     private static final int MAX_RETRY_DELETE_TIMES = IS_WINDOWS ? 15 : 0;
  62 
  63     /**
  64      * Deletes a file, retrying if necessary.
  65      *
  66      * @param path  the file to delete
  67      *
  68      * @throws NoSuchFileException
  69      *         if the file does not exist (optional specific exception)
  70      * @throws DirectoryNotEmptyException
  71      *         if the file is a directory and could not otherwise be deleted
  72      *         because the directory is not empty (optional specific exception)
  73      * @throws IOException
  74      *         if an I/O error occurs
  75      */
  76     public static void deleteFileWithRetry(Path path) throws IOException {
  77         try {
  78             deleteFileWithRetry0(path);
  79         } catch (InterruptedException x) {
  80             throw new IOException("Interrupted while deleting.", x);
  81         }
  82     }
  83 
  84     /**
  85      * Deletes a file, retrying if necessary.
  86      * No exception thrown if file doesn't exist.
  87      *
  88      * @param path  the file to delete
  89      *
  90      * @throws NoSuchFileException
  91      *         if the file does not exist (optional specific exception)
  92      * @throws DirectoryNotEmptyException
  93      *         if the file is a directory and could not otherwise be deleted
  94      *         because the directory is not empty (optional specific exception)
  95      * @throws IOException
  96      *         if an I/O error occurs
  97      */
  98     public static void deleteFileIfExistsWithRetry(Path path) throws IOException {
  99         try {
 100             if (!Files.notExists(path)) {
 101                 deleteFileWithRetry0(path);
 102             }
 103         } catch (InterruptedException x) {
 104             throw new IOException("Interrupted while deleting.", x);
 105         }
 106     }
 107 
 108     private static void deleteFileWithRetry0(Path path)
 109             throws IOException, InterruptedException {
 110         int times = 0;
 111         IOException ioe = null;
 112         while (true) {
 113             try {
 114                 Files.delete(path);
 115                 // Checks for absence of the file. Semantics of Files.exists() is not the same.
 116                 while (!Files.notExists(path)) {
 117                     times++;
 118                     if (times > MAX_RETRY_DELETE_TIMES) {
 119                         throw new IOException("File still exists after " + times + " waits.");
 120                     }
 121                     Thread.sleep(RETRY_DELETE_MILLIS);
 122                 }
 123                 break;
 124             } catch (NoSuchFileException | DirectoryNotEmptyException x) {
 125                 throw x;
 126             } catch (IOException x) {
 127                 // Backoff/retry in case another process is accessing the file
 128                 times++;
 129                 if (ioe == null) {
 130                     ioe = x;
 131                 } else {
 132                     ioe.addSuppressed(x);
 133                 }
 134 
 135                 if (times > MAX_RETRY_DELETE_TIMES) {
 136                     throw ioe;
 137                 }
 138                 Thread.sleep(RETRY_DELETE_MILLIS);
 139             }
 140         }
 141     }
 142 
 143     /**
 144      * Deletes a directory and its subdirectories, retrying if necessary.
 145      *
 146      * @param dir  the directory to delete
 147      *
 148      * @throws  IOException
 149      *          If an I/O error occurs. Any such exceptions are caught
 150      *          internally. If only one is caught, then it is re-thrown.
 151      *          If more than one exception is caught, then the second and
 152      *          following exceptions are added as suppressed exceptions of the
 153      *          first one caught, which is then re-thrown.
 154      */
 155     public static void deleteFileTreeWithRetry(Path dir) throws IOException {
 156         IOException ioe = null;
 157         final List<IOException> excs = deleteFileTreeUnchecked(dir);
 158         if (!excs.isEmpty()) {
 159             ioe = excs.remove(0);
 160             for (IOException x : excs) {
 161                 ioe.addSuppressed(x);
 162             }
 163         }
 164         if (ioe != null) {
 165             throw ioe;
 166         }
 167     }
 168 
 169     public static List<IOException> deleteFileTreeUnchecked(Path dir) {
 170         final List<IOException> excs = new ArrayList<>();
 171         try {
 172             java.nio.file.Files.walkFileTree(dir, new SimpleFileVisitor<>() {
 173                 @Override
 174                 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
 175                     try {
 176                         deleteFileWithRetry0(file);
 177                     } catch (IOException x) {
 178                         excs.add(x);
 179                     } catch (InterruptedException x) {
 180                         excs.add(new IOException("Interrupted while deleting.", x));
 181                         return FileVisitResult.TERMINATE;
 182                     }
 183                     return FileVisitResult.CONTINUE;
 184                 }
 185                 @Override
 186                 public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
 187                     try {
 188                         deleteFileWithRetry0(dir);
 189                     } catch (IOException x) {
 190                         excs.add(x);
 191                     } catch (InterruptedException x) {
 192                         excs.add(new IOException("Interrupted while deleting.", x));
 193                         return FileVisitResult.TERMINATE;
 194                     }
 195                     return FileVisitResult.CONTINUE;
 196                 }
 197                 @Override
 198                 public FileVisitResult visitFileFailed(Path file, IOException exc) {
 199                     excs.add(exc);
 200                     return FileVisitResult.CONTINUE;
 201                 }
 202             });
 203         } catch (IOException x) {
 204             excs.add(x);
 205         }
 206         return excs;
 207     }
 208 
 209     /**
 210      * Checks whether all file systems are accessible. This is performed
 211      * by checking free disk space on all mounted file systems via a
 212      * separate, spawned process. File systems are considered to be
 213      * accessible if this process completes successfully before a given
 214      * fixed duration has elapsed.
 215      *
 216      * @implNote On Unix this executes the {@code df} command in a separate
 217      * process and on Windows always returns {@code true}.
 218      */
 219     public static boolean areFileSystemsAccessible() throws IOException {
 220         boolean areFileSystemsAccessible = true;
 221         if (!IS_WINDOWS) {
 222             // try to check whether 'df' hangs
 223             System.out.println("\n--- df output ---");
 224             System.out.flush();
 225             Process proc = new ProcessBuilder("df").inheritIO().start();
 226             try {
 227                 proc.waitFor(90, TimeUnit.SECONDS);
 228             } catch (InterruptedException ignored) {
 229             }
 230             try {
 231                 int exitValue = proc.exitValue();
 232                 if (exitValue != 0) {
 233                     System.err.printf("df process exited with %d != 0%n",
 234                         exitValue);
 235                     areFileSystemsAccessible = false;
 236                 }
 237             } catch (IllegalThreadStateException ignored) {
 238                 System.err.println("df command apparently hung");
 239                 areFileSystemsAccessible = false;
 240             }
 241         }
 242         return areFileSystemsAccessible;
 243     }
 244 
 245     /**
 246      * Checks whether all file systems are accessible and there are no
 247      * duplicate mount points. This is performed by checking free disk
 248      * space on all mounted file systems via a separate, spawned process.
 249      * File systems are considered to be accessible if this process completes
 250      * successfully before a given fixed duration has elapsed.
 251      *
 252      * @implNote On Unix this executes the {@code df} command in a separate
 253      * process and on Windows always returns {@code true}.
 254      *
 255      * @return whether file systems appear to be accessible and duplicate-free
 256      */
 257     public static boolean areMountPointsAccessibleAndUnique() {
 258         if (IS_WINDOWS) return true;
 259 
 260         final AtomicBoolean areMountPointsOK = new AtomicBoolean(true);
 261         Thread thr = new Thread(() -> {
 262             try {
 263                 Process proc = new ProcessBuilder("df").start();
 264                 BufferedReader reader = new BufferedReader
 265                     (new InputStreamReader(proc.getInputStream()));
 266                 // Skip the first line as it is the "df" output header.
 267                 if (reader.readLine() != null ) {
 268                     Set mountPoints = new HashSet();
 269                     String mountPoint = null;
 270                     while ((mountPoint = reader.readLine()) != null) {
 271                         if (!mountPoints.add(mountPoint)) {
 272                             System.err.printf
 273                                 ("Config error: duplicate mount point %s%n",
 274                                 mountPoint);
 275                             areMountPointsOK.set(false);
 276                             break;
 277                         }
 278                     }
 279                 }
 280 
 281                 try {
 282                     proc.waitFor(90, TimeUnit.SECONDS);
 283                 } catch (InterruptedException ignored) {
 284                 }
 285                 try {
 286                     int exitValue = proc.exitValue();
 287                     if (exitValue != 0) {
 288                         System.err.printf("df process exited with %d != 0%n",
 289                             exitValue);
 290                         areMountPointsOK.set(false);
 291                     }
 292                 } catch (IllegalThreadStateException ignored) {
 293                     System.err.println("df command apparently hung");
 294                     areMountPointsOK.set(false);
 295                 }
 296             } catch (IOException ioe) {
 297                 throw new RuntimeException(ioe);
 298             };
 299         });
 300 
 301         final AtomicReference throwableReference =
 302             new AtomicReference<Throwable>();
 303         thr.setUncaughtExceptionHandler(
 304             new Thread.UncaughtExceptionHandler() {
 305                 public void uncaughtException(Thread t, Throwable e) {
 306                     throwableReference.set(e);
 307                 }
 308             });
 309 
 310         thr.start();
 311         try {
 312             thr.join(120*1000L);
 313         } catch (InterruptedException ie) {
 314             throw new RuntimeException(ie);
 315         }
 316 
 317         Throwable uncaughtException = (Throwable)throwableReference.get();
 318         if (uncaughtException != null) {
 319             throw new RuntimeException(uncaughtException);
 320         }
 321 
 322         if (thr.isAlive()) {
 323             throw new RuntimeException("df thread did not join in time");
 324         }
 325 
 326         return areMountPointsOK.get();
 327     }
 328 
 329     /**
 330      * List the open file descriptors (if supported by the 'lsof' command).
 331      * @param ps a printStream to send the output to
 332      * @throws UncheckedIOException if an error occurs
 333      */
 334     public static void listFileDescriptors(PrintStream ps) {
 335 
 336         Optional<String[]> lsof = Arrays.stream(lsCommands)
 337                 .filter(args -> Files.isExecutable(Path.of(args[0])))
 338                 .findFirst();
 339         lsof.ifPresent(args -> {
 340             try {
 341                 ps.printf("Open File Descriptors:%n");
 342                 long pid = ProcessHandle.current().pid();
 343                 ProcessBuilder pb = new ProcessBuilder(args[0], args[1], Integer.toString((int) pid));
 344                 pb.redirectErrorStream(true);   // combine stderr and stdout
 345                 pb.redirectOutput(Redirect.PIPE);
 346 
 347                 Process p = pb.start();
 348                 Instant start = Instant.now();
 349                 p.getInputStream().transferTo(ps);
 350 
 351                 try {
 352                     int timeout = 10;
 353                     if (!p.waitFor(timeout, TimeUnit.SECONDS)) {
 354                         System.out.printf("waitFor timed out: %d%n", timeout);
 355                     }
 356                 } catch (InterruptedException ie) {
 357                     throw new IOException("interrupted", ie);
 358                 }
 359                 ps.println();
 360             } catch (IOException ioe) {
 361                 throw new UncheckedIOException("error listing file descriptors", ioe);
 362             }
 363         });
 364     }
 365 
 366     // Possible command locations and arguments
 367     static String[][] lsCommands = new String[][] {
 368             {"/usr/bin/lsof", "-p"},
 369             {"/usr/sbin/lsof", "-p"},
 370             {"/bin/lsof", "-p"},
 371             {"/sbin/lsof", "-p"},
 372             {"/usr/local/bin/lsof", "-p"},
 373     };
 374 }