test/lib/jdk/test/lib/util/FileUtils.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 55677 4b1de39beda3
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
45267
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
     1
/*
55271
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
     2
 * Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
45267
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
     4
 *
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
     7
 * published by the Free Software Foundation.
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
     8
 *
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    13
 * accompanied this code).
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    14
 *
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    18
 *
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    21
 * questions.
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    22
 */
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    23
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    24
package jdk.test.lib.util;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    25
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    26
import jdk.test.lib.Platform;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    27
55271
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
    28
import java.io.BufferedReader;
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
    29
import java.io.InputStreamReader;
45267
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    30
import java.io.IOException;
50727
081b132c4dc0 8202292: java/io/FileOutputStream/UnreferencedFOSClosesFd.java fails with "raw fd count wrong"
rriggs
parents: 47249
diff changeset
    31
import java.io.PrintStream;
081b132c4dc0 8202292: java/io/FileOutputStream/UnreferencedFOSClosesFd.java fails with "raw fd count wrong"
rriggs
parents: 47249
diff changeset
    32
import java.io.UncheckedIOException;
081b132c4dc0 8202292: java/io/FileOutputStream/UnreferencedFOSClosesFd.java fails with "raw fd count wrong"
rriggs
parents: 47249
diff changeset
    33
import java.lang.ProcessBuilder.Redirect;
45267
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    34
import java.nio.file.DirectoryNotEmptyException;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    35
import java.nio.file.FileVisitResult;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    36
import java.nio.file.Files;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    37
import java.nio.file.NoSuchFileException;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    38
import java.nio.file.Path;
50727
081b132c4dc0 8202292: java/io/FileOutputStream/UnreferencedFOSClosesFd.java fails with "raw fd count wrong"
rriggs
parents: 47249
diff changeset
    39
import java.nio.file.Paths;
45267
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    40
import java.nio.file.SimpleFileVisitor;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    41
import java.nio.file.attribute.BasicFileAttributes;
50727
081b132c4dc0 8202292: java/io/FileOutputStream/UnreferencedFOSClosesFd.java fails with "raw fd count wrong"
rriggs
parents: 47249
diff changeset
    42
import java.time.Instant;
081b132c4dc0 8202292: java/io/FileOutputStream/UnreferencedFOSClosesFd.java fails with "raw fd count wrong"
rriggs
parents: 47249
diff changeset
    43
import java.time.Duration;
51107
35e64b62b284 8205610: [TESTLIB] Improve listing of open file descriptors
rriggs
parents: 50727
diff changeset
    44
import java.util.Arrays;
45267
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    45
import java.util.ArrayList;
50727
081b132c4dc0 8202292: java/io/FileOutputStream/UnreferencedFOSClosesFd.java fails with "raw fd count wrong"
rriggs
parents: 47249
diff changeset
    46
import java.util.ArrayDeque;
081b132c4dc0 8202292: java/io/FileOutputStream/UnreferencedFOSClosesFd.java fails with "raw fd count wrong"
rriggs
parents: 47249
diff changeset
    47
import java.util.HashSet;
45267
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    48
import java.util.List;
50727
081b132c4dc0 8202292: java/io/FileOutputStream/UnreferencedFOSClosesFd.java fails with "raw fd count wrong"
rriggs
parents: 47249
diff changeset
    49
import java.util.Optional;
55271
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
    50
import java.util.concurrent.atomic.AtomicBoolean;
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
    51
import java.util.concurrent.atomic.AtomicReference;
45267
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    52
import java.util.concurrent.TimeUnit;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    53
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    54
/**
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    55
 * Common library for various test file utility functions.
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    56
 */
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    57
public final class FileUtils {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    58
    private static final boolean IS_WINDOWS = Platform.isWindows();
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    59
    private static final int RETRY_DELETE_MILLIS = IS_WINDOWS ? 500 : 0;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    60
    private static final int MAX_RETRY_DELETE_TIMES = IS_WINDOWS ? 15 : 0;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    61
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    62
    /**
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    63
     * Deletes a file, retrying if necessary.
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    64
     *
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    65
     * @param path  the file to delete
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    66
     *
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    67
     * @throws NoSuchFileException
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    68
     *         if the file does not exist (optional specific exception)
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    69
     * @throws DirectoryNotEmptyException
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    70
     *         if the file is a directory and could not otherwise be deleted
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    71
     *         because the directory is not empty (optional specific exception)
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    72
     * @throws IOException
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    73
     *         if an I/O error occurs
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    74
     */
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    75
    public static void deleteFileWithRetry(Path path) throws IOException {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    76
        try {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    77
            deleteFileWithRetry0(path);
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    78
        } catch (InterruptedException x) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    79
            throw new IOException("Interrupted while deleting.", x);
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    80
        }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    81
    }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    82
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    83
    /**
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    84
     * Deletes a file, retrying if necessary.
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    85
     * No exception thrown if file doesn't exist.
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    86
     *
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    87
     * @param path  the file to delete
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    88
     *
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    89
     * @throws NoSuchFileException
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    90
     *         if the file does not exist (optional specific exception)
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    91
     * @throws DirectoryNotEmptyException
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    92
     *         if the file is a directory and could not otherwise be deleted
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    93
     *         because the directory is not empty (optional specific exception)
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    94
     * @throws IOException
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    95
     *         if an I/O error occurs
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    96
     */
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    97
    public static void deleteFileIfExistsWithRetry(Path path) throws IOException {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    98
        try {
55677
4b1de39beda3 8227438: [TESTLIB] Determine if file exists by Files.exists in function FileUtils.deleteFileIfExistsWithRetry
fyuan
parents: 55271
diff changeset
    99
            if (!Files.notExists(path)) {
45267
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   100
                deleteFileWithRetry0(path);
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   101
            }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   102
        } catch (InterruptedException x) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   103
            throw new IOException("Interrupted while deleting.", x);
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   104
        }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   105
    }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   106
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   107
    private static void deleteFileWithRetry0(Path path)
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   108
            throws IOException, InterruptedException {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   109
        int times = 0;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   110
        IOException ioe = null;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   111
        while (true) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   112
            try {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   113
                Files.delete(path);
47249
82c43b3c0ea0 8185458: Added comment for Files.notExists in FileUtils test library
anazarov
parents: 45920
diff changeset
   114
                // 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
   115
                while (!Files.notExists(path)) {
45267
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   116
                    times++;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   117
                    if (times > MAX_RETRY_DELETE_TIMES) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   118
                        throw new IOException("File still exists after " + times + " waits.");
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   119
                    }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   120
                    Thread.sleep(RETRY_DELETE_MILLIS);
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   121
                }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   122
                break;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   123
            } catch (NoSuchFileException | DirectoryNotEmptyException x) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   124
                throw x;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   125
            } catch (IOException x) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   126
                // Backoff/retry in case another process is accessing the file
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   127
                times++;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   128
                if (ioe == null) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   129
                    ioe = x;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   130
                } else {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   131
                    ioe.addSuppressed(x);
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   132
                }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   133
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   134
                if (times > MAX_RETRY_DELETE_TIMES) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   135
                    throw ioe;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   136
                }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   137
                Thread.sleep(RETRY_DELETE_MILLIS);
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   138
            }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   139
        }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   140
    }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   141
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   142
    /**
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   143
     * Deletes a directory and its subdirectories, retrying if necessary.
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   144
     *
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   145
     * @param dir  the directory to delete
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   146
     *
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   147
     * @throws  IOException
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   148
     *          If an I/O error occurs. Any such exceptions are caught
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   149
     *          internally. If only one is caught, then it is re-thrown.
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   150
     *          If more than one exception is caught, then the second and
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   151
     *          following exceptions are added as suppressed exceptions of the
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   152
     *          first one caught, which is then re-thrown.
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   153
     */
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   154
    public static void deleteFileTreeWithRetry(Path dir) throws IOException {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   155
        IOException ioe = null;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   156
        final List<IOException> excs = deleteFileTreeUnchecked(dir);
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   157
        if (!excs.isEmpty()) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   158
            ioe = excs.remove(0);
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   159
            for (IOException x : excs) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   160
                ioe.addSuppressed(x);
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   161
            }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   162
        }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   163
        if (ioe != null) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   164
            throw ioe;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   165
        }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   166
    }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   167
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   168
    public static List<IOException> deleteFileTreeUnchecked(Path dir) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   169
        final List<IOException> excs = new ArrayList<>();
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   170
        try {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   171
            java.nio.file.Files.walkFileTree(dir, new SimpleFileVisitor<>() {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   172
                @Override
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   173
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   174
                    try {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   175
                        deleteFileWithRetry0(file);
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   176
                    } catch (IOException x) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   177
                        excs.add(x);
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   178
                    } catch (InterruptedException x) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   179
                        excs.add(new IOException("Interrupted while deleting.", x));
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   180
                        return FileVisitResult.TERMINATE;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   181
                    }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   182
                    return FileVisitResult.CONTINUE;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   183
                }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   184
                @Override
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   185
                public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   186
                    try {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   187
                        deleteFileWithRetry0(dir);
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   188
                    } catch (IOException x) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   189
                        excs.add(x);
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   190
                    } catch (InterruptedException x) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   191
                        excs.add(new IOException("Interrupted while deleting.", x));
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   192
                        return FileVisitResult.TERMINATE;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   193
                    }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   194
                    return FileVisitResult.CONTINUE;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   195
                }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   196
                @Override
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   197
                public FileVisitResult visitFileFailed(Path file, IOException exc) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   198
                    excs.add(exc);
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   199
                    return FileVisitResult.CONTINUE;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   200
                }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   201
            });
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   202
        } catch (IOException x) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   203
            excs.add(x);
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   204
        }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   205
        return excs;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   206
    }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   207
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   208
    /**
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   209
     * Checks whether all file systems are accessible. This is performed
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   210
     * by checking free disk space on all mounted file systems via a
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   211
     * separate, spawned process. File systems are considered to be
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   212
     * accessible if this process completes successfully before a given
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   213
     * fixed duration has elapsed.
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   214
     *
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   215
     * @implNote On Unix this executes the {@code df} command in a separate
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   216
     * process and on Windows always returns {@code true}.
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   217
     */
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   218
    public static boolean areFileSystemsAccessible() throws IOException {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   219
        boolean areFileSystemsAccessible = true;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   220
        if (!IS_WINDOWS) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   221
            // try to check whether 'df' hangs
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   222
            System.out.println("\n--- df output ---");
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   223
            System.out.flush();
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   224
            Process proc = new ProcessBuilder("df").inheritIO().start();
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   225
            try {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   226
                proc.waitFor(90, TimeUnit.SECONDS);
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   227
            } catch (InterruptedException ignored) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   228
            }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   229
            try {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   230
                int exitValue = proc.exitValue();
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   231
                if (exitValue != 0) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   232
                    System.err.printf("df process exited with %d != 0%n",
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   233
                        exitValue);
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   234
                    areFileSystemsAccessible = false;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   235
                }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   236
            } catch (IllegalThreadStateException ignored) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   237
                System.err.println("df command apparently hung");
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   238
                areFileSystemsAccessible = false;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   239
            }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   240
        }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   241
        return areFileSystemsAccessible;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   242
    }
50727
081b132c4dc0 8202292: java/io/FileOutputStream/UnreferencedFOSClosesFd.java fails with "raw fd count wrong"
rriggs
parents: 47249
diff changeset
   243
081b132c4dc0 8202292: java/io/FileOutputStream/UnreferencedFOSClosesFd.java fails with "raw fd count wrong"
rriggs
parents: 47249
diff changeset
   244
    /**
55271
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   245
     * Checks whether all file systems are accessible. This is performed
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   246
     * by checking free disk space on all mounted file systems via a
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   247
     * separate, spawned process. File systems are considered to be
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   248
     * accessible if this process completes successfully before a given
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   249
     * fixed duration has elapsed.
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   250
     *
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   251
     * @implNote On Unix this executes the {@code df} command in a separate
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   252
     * process and on Windows always returns {@code true}.
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   253
     *
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   254
     * @return whether file systems appear to be accessible
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   255
     *
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   256
     * @throws RuntimeException if there are duplicate mount points or some
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   257
     * other execution problem occurs
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   258
     */
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   259
    public static boolean areAllMountPointsAccessible() {
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   260
        final AtomicBoolean areMountPointsOK = new AtomicBoolean(true);
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   261
        if (!IS_WINDOWS) {
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   262
            Thread thr = new Thread(() -> {
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   263
                try {
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   264
                    Process proc = new ProcessBuilder("df").start();
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   265
                    BufferedReader reader = new BufferedReader
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   266
                        (new InputStreamReader(proc.getInputStream()));
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   267
                    // Skip the first line as it is the "df" output header.
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   268
                    if (reader.readLine() != null ) {
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   269
                        String prevMountPoint = null, mountPoint = null;
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   270
                        while ((mountPoint = reader.readLine()) != null) {
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   271
                            if (prevMountPoint != null &&
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   272
                                mountPoint.equals(prevMountPoint)) {
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   273
                                throw new RuntimeException
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   274
                                    ("System configuration error: " +
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   275
                                    "duplicate mount point " + mountPoint +
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   276
                                    " detected");
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   277
                            }
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   278
                            prevMountPoint = mountPoint;
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   279
                        }
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   280
                    }
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   281
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   282
                    try {
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   283
                        proc.waitFor(90, TimeUnit.SECONDS);
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   284
                    } catch (InterruptedException ignored) {
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   285
                    }
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   286
                    try {
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   287
                        int exitValue = proc.exitValue();
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   288
                        if (exitValue != 0) {
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   289
                            System.err.printf("df process exited with %d != 0%n",
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   290
                                exitValue);
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   291
                            areMountPointsOK.set(false);
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   292
                        }
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   293
                    } catch (IllegalThreadStateException ignored) {
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   294
                        System.err.println("df command apparently hung");
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   295
                        areMountPointsOK.set(false);
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   296
                    }
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   297
                } catch (IOException ioe) {
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   298
                    throw new RuntimeException(ioe);
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   299
                };
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   300
            });
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   301
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   302
            final AtomicReference throwableReference =
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   303
                new AtomicReference<Throwable>();
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   304
            thr.setUncaughtExceptionHandler(
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   305
                new Thread.UncaughtExceptionHandler() {
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   306
                    public void uncaughtException(Thread t, Throwable e) {
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   307
                        throwableReference.set(e);
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   308
                    }
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   309
                });
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   310
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   311
            thr.start();
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   312
            try {
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   313
                thr.join(120*1000L);
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   314
            } catch (InterruptedException ie) {
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   315
                throw new RuntimeException(ie);
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   316
            }
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   317
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   318
            Throwable uncaughtException = (Throwable)throwableReference.get();
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   319
            if (uncaughtException != null) {
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   320
                throw new RuntimeException(uncaughtException);
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   321
            }
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   322
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   323
            if (thr.isAlive()) {
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   324
                throw new RuntimeException("df thread did not join in time");
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   325
            }
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   326
        }
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   327
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   328
        return areMountPointsOK.get();
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   329
    }
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   330
5c7c3662c386 8224617: (fs) java/nio/file/FileStore/Basic.java found filesystem twice
bpb
parents: 51107
diff changeset
   331
    /**
50727
081b132c4dc0 8202292: java/io/FileOutputStream/UnreferencedFOSClosesFd.java fails with "raw fd count wrong"
rriggs
parents: 47249
diff changeset
   332
     * List the open file descriptors (if supported by the 'lsof' command).
081b132c4dc0 8202292: java/io/FileOutputStream/UnreferencedFOSClosesFd.java fails with "raw fd count wrong"
rriggs
parents: 47249
diff changeset
   333
     * @param ps a printStream to send the output to
081b132c4dc0 8202292: java/io/FileOutputStream/UnreferencedFOSClosesFd.java fails with "raw fd count wrong"
rriggs
parents: 47249
diff changeset
   334
     * @throws UncheckedIOException if an error occurs
081b132c4dc0 8202292: java/io/FileOutputStream/UnreferencedFOSClosesFd.java fails with "raw fd count wrong"
rriggs
parents: 47249
diff changeset
   335
     */
081b132c4dc0 8202292: java/io/FileOutputStream/UnreferencedFOSClosesFd.java fails with "raw fd count wrong"
rriggs
parents: 47249
diff changeset
   336
    public static void listFileDescriptors(PrintStream ps) {
51107
35e64b62b284 8205610: [TESTLIB] Improve listing of open file descriptors
rriggs
parents: 50727
diff changeset
   337
35e64b62b284 8205610: [TESTLIB] Improve listing of open file descriptors
rriggs
parents: 50727
diff changeset
   338
        Optional<String[]> lsof = Arrays.stream(lsCommands)
35e64b62b284 8205610: [TESTLIB] Improve listing of open file descriptors
rriggs
parents: 50727
diff changeset
   339
                .filter(args -> Files.isExecutable(Path.of(args[0])))
50727
081b132c4dc0 8202292: java/io/FileOutputStream/UnreferencedFOSClosesFd.java fails with "raw fd count wrong"
rriggs
parents: 47249
diff changeset
   340
                .findFirst();
51107
35e64b62b284 8205610: [TESTLIB] Improve listing of open file descriptors
rriggs
parents: 50727
diff changeset
   341
        lsof.ifPresent(args -> {
50727
081b132c4dc0 8202292: java/io/FileOutputStream/UnreferencedFOSClosesFd.java fails with "raw fd count wrong"
rriggs
parents: 47249
diff changeset
   342
            try {
081b132c4dc0 8202292: java/io/FileOutputStream/UnreferencedFOSClosesFd.java fails with "raw fd count wrong"
rriggs
parents: 47249
diff changeset
   343
                ps.printf("Open File Descriptors:%n");
081b132c4dc0 8202292: java/io/FileOutputStream/UnreferencedFOSClosesFd.java fails with "raw fd count wrong"
rriggs
parents: 47249
diff changeset
   344
                long pid = ProcessHandle.current().pid();
51107
35e64b62b284 8205610: [TESTLIB] Improve listing of open file descriptors
rriggs
parents: 50727
diff changeset
   345
                ProcessBuilder pb = new ProcessBuilder(args[0], args[1], Integer.toString((int) pid));
50727
081b132c4dc0 8202292: java/io/FileOutputStream/UnreferencedFOSClosesFd.java fails with "raw fd count wrong"
rriggs
parents: 47249
diff changeset
   346
                pb.redirectErrorStream(true);   // combine stderr and stdout
081b132c4dc0 8202292: java/io/FileOutputStream/UnreferencedFOSClosesFd.java fails with "raw fd count wrong"
rriggs
parents: 47249
diff changeset
   347
                pb.redirectOutput(Redirect.PIPE);
081b132c4dc0 8202292: java/io/FileOutputStream/UnreferencedFOSClosesFd.java fails with "raw fd count wrong"
rriggs
parents: 47249
diff changeset
   348
081b132c4dc0 8202292: java/io/FileOutputStream/UnreferencedFOSClosesFd.java fails with "raw fd count wrong"
rriggs
parents: 47249
diff changeset
   349
                Process p = pb.start();
081b132c4dc0 8202292: java/io/FileOutputStream/UnreferencedFOSClosesFd.java fails with "raw fd count wrong"
rriggs
parents: 47249
diff changeset
   350
                Instant start = Instant.now();
081b132c4dc0 8202292: java/io/FileOutputStream/UnreferencedFOSClosesFd.java fails with "raw fd count wrong"
rriggs
parents: 47249
diff changeset
   351
                p.getInputStream().transferTo(ps);
081b132c4dc0 8202292: java/io/FileOutputStream/UnreferencedFOSClosesFd.java fails with "raw fd count wrong"
rriggs
parents: 47249
diff changeset
   352
081b132c4dc0 8202292: java/io/FileOutputStream/UnreferencedFOSClosesFd.java fails with "raw fd count wrong"
rriggs
parents: 47249
diff changeset
   353
                try {
081b132c4dc0 8202292: java/io/FileOutputStream/UnreferencedFOSClosesFd.java fails with "raw fd count wrong"
rriggs
parents: 47249
diff changeset
   354
                    int timeout = 10;
081b132c4dc0 8202292: java/io/FileOutputStream/UnreferencedFOSClosesFd.java fails with "raw fd count wrong"
rriggs
parents: 47249
diff changeset
   355
                    if (!p.waitFor(timeout, TimeUnit.SECONDS)) {
081b132c4dc0 8202292: java/io/FileOutputStream/UnreferencedFOSClosesFd.java fails with "raw fd count wrong"
rriggs
parents: 47249
diff changeset
   356
                        System.out.printf("waitFor timed out: %d%n", timeout);
081b132c4dc0 8202292: java/io/FileOutputStream/UnreferencedFOSClosesFd.java fails with "raw fd count wrong"
rriggs
parents: 47249
diff changeset
   357
                    }
081b132c4dc0 8202292: java/io/FileOutputStream/UnreferencedFOSClosesFd.java fails with "raw fd count wrong"
rriggs
parents: 47249
diff changeset
   358
                } catch (InterruptedException ie) {
081b132c4dc0 8202292: java/io/FileOutputStream/UnreferencedFOSClosesFd.java fails with "raw fd count wrong"
rriggs
parents: 47249
diff changeset
   359
                    throw new IOException("interrupted", ie);
081b132c4dc0 8202292: java/io/FileOutputStream/UnreferencedFOSClosesFd.java fails with "raw fd count wrong"
rriggs
parents: 47249
diff changeset
   360
                }
081b132c4dc0 8202292: java/io/FileOutputStream/UnreferencedFOSClosesFd.java fails with "raw fd count wrong"
rriggs
parents: 47249
diff changeset
   361
                ps.println();
081b132c4dc0 8202292: java/io/FileOutputStream/UnreferencedFOSClosesFd.java fails with "raw fd count wrong"
rriggs
parents: 47249
diff changeset
   362
            } catch (IOException ioe) {
081b132c4dc0 8202292: java/io/FileOutputStream/UnreferencedFOSClosesFd.java fails with "raw fd count wrong"
rriggs
parents: 47249
diff changeset
   363
                throw new UncheckedIOException("error listing file descriptors", ioe);
081b132c4dc0 8202292: java/io/FileOutputStream/UnreferencedFOSClosesFd.java fails with "raw fd count wrong"
rriggs
parents: 47249
diff changeset
   364
            }
081b132c4dc0 8202292: java/io/FileOutputStream/UnreferencedFOSClosesFd.java fails with "raw fd count wrong"
rriggs
parents: 47249
diff changeset
   365
        });
081b132c4dc0 8202292: java/io/FileOutputStream/UnreferencedFOSClosesFd.java fails with "raw fd count wrong"
rriggs
parents: 47249
diff changeset
   366
    }
51107
35e64b62b284 8205610: [TESTLIB] Improve listing of open file descriptors
rriggs
parents: 50727
diff changeset
   367
35e64b62b284 8205610: [TESTLIB] Improve listing of open file descriptors
rriggs
parents: 50727
diff changeset
   368
    // Possible command locations and arguments
35e64b62b284 8205610: [TESTLIB] Improve listing of open file descriptors
rriggs
parents: 50727
diff changeset
   369
    static String[][] lsCommands = new String[][] {
35e64b62b284 8205610: [TESTLIB] Improve listing of open file descriptors
rriggs
parents: 50727
diff changeset
   370
            {"/usr/bin/lsof", "-p"},
35e64b62b284 8205610: [TESTLIB] Improve listing of open file descriptors
rriggs
parents: 50727
diff changeset
   371
            {"/usr/sbin/lsof", "-p"},
35e64b62b284 8205610: [TESTLIB] Improve listing of open file descriptors
rriggs
parents: 50727
diff changeset
   372
            {"/bin/lsof", "-p"},
35e64b62b284 8205610: [TESTLIB] Improve listing of open file descriptors
rriggs
parents: 50727
diff changeset
   373
            {"/sbin/lsof", "-p"},
35e64b62b284 8205610: [TESTLIB] Improve listing of open file descriptors
rriggs
parents: 50727
diff changeset
   374
            {"/usr/local/bin/lsof", "-p"},
35e64b62b284 8205610: [TESTLIB] Improve listing of open file descriptors
rriggs
parents: 50727
diff changeset
   375
            {"/usr/bin/pfiles", "-F"},   // Solaris
35e64b62b284 8205610: [TESTLIB] Improve listing of open file descriptors
rriggs
parents: 50727
diff changeset
   376
    };
45267
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   377
}