test/lib/jdk/test/lib/util/FileUtils.java
author anazarov
Fri, 22 Sep 2017 11:49:53 -0700
changeset 47249 82c43b3c0ea0
parent 45920 ddc079e73172
child 50727 081b132c4dc0
permissions -rw-r--r--
8185458: Added comment for Files.notExists in FileUtils test library Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
45267
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
     1
/*
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
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
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    28
import java.io.IOException;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    29
import java.nio.file.DirectoryNotEmptyException;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    30
import java.nio.file.FileVisitResult;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    31
import java.nio.file.Files;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    32
import java.nio.file.NoSuchFileException;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    33
import java.nio.file.Path;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    34
import java.nio.file.SimpleFileVisitor;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    35
import java.nio.file.attribute.BasicFileAttributes;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    36
import java.util.ArrayList;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    37
import java.util.List;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    38
import java.util.concurrent.TimeUnit;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    39
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    40
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    41
/**
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    42
 * Common library for various test file utility functions.
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    43
 */
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    44
public final class FileUtils {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    45
    private static final boolean IS_WINDOWS = Platform.isWindows();
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    46
    private static final int RETRY_DELETE_MILLIS = IS_WINDOWS ? 500 : 0;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    47
    private static final int MAX_RETRY_DELETE_TIMES = IS_WINDOWS ? 15 : 0;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    48
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    49
    /**
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    50
     * Deletes a file, retrying if necessary.
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    51
     *
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    52
     * @param path  the file to delete
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
     * @throws NoSuchFileException
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    55
     *         if the file does not exist (optional specific exception)
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    56
     * @throws DirectoryNotEmptyException
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    57
     *         if the file is a directory and could not otherwise be deleted
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    58
     *         because the directory is not empty (optional specific exception)
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    59
     * @throws IOException
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    60
     *         if an I/O error occurs
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
    public static void deleteFileWithRetry(Path path) throws IOException {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    63
        try {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    64
            deleteFileWithRetry0(path);
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    65
        } catch (InterruptedException x) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    66
            throw new IOException("Interrupted while deleting.", x);
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    67
        }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    68
    }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    69
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    70
    /**
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    71
     * Deletes a file, retrying if necessary.
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    72
     * No exception thrown if file doesn't exist.
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    73
     *
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    74
     * @param path  the file to delete
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    75
     *
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    76
     * @throws NoSuchFileException
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    77
     *         if the file does not exist (optional specific exception)
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    78
     * @throws DirectoryNotEmptyException
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    79
     *         if the file is a directory and could not otherwise be deleted
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    80
     *         because the directory is not empty (optional specific exception)
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    81
     * @throws IOException
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    82
     *         if an I/O error occurs
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
    public static void deleteFileIfExistsWithRetry(Path path) throws IOException {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    85
        try {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    86
            if (Files.exists(path)) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    87
                deleteFileWithRetry0(path);
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
        } catch (InterruptedException x) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    90
            throw new IOException("Interrupted while deleting.", x);
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    91
        }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    92
    }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    93
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    94
    private static void deleteFileWithRetry0(Path path)
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    95
            throws IOException, InterruptedException {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    96
        int times = 0;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    97
        IOException ioe = null;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    98
        while (true) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
    99
            try {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   100
                Files.delete(path);
47249
82c43b3c0ea0 8185458: Added comment for Files.notExists in FileUtils test library
anazarov
parents: 45920
diff changeset
   101
                // 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
   102
                while (!Files.notExists(path)) {
45267
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   103
                    times++;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   104
                    if (times > MAX_RETRY_DELETE_TIMES) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   105
                        throw new IOException("File still exists after " + times + " waits.");
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
                    Thread.sleep(RETRY_DELETE_MILLIS);
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   108
                }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   109
                break;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   110
            } catch (NoSuchFileException | DirectoryNotEmptyException x) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   111
                throw x;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   112
            } catch (IOException x) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   113
                // Backoff/retry in case another process is accessing the file
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   114
                times++;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   115
                if (ioe == null) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   116
                    ioe = x;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   117
                } else {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   118
                    ioe.addSuppressed(x);
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
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   121
                if (times > MAX_RETRY_DELETE_TIMES) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   122
                    throw ioe;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   123
                }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   124
                Thread.sleep(RETRY_DELETE_MILLIS);
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   125
            }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   126
        }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   127
    }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   128
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   129
    /**
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   130
     * Deletes a directory and its subdirectories, retrying if necessary.
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   131
     *
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   132
     * @param dir  the directory to delete
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
     * @throws  IOException
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   135
     *          If an I/O error occurs. Any such exceptions are caught
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   136
     *          internally. If only one is caught, then it is re-thrown.
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   137
     *          If more than one exception is caught, then the second and
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   138
     *          following exceptions are added as suppressed exceptions of the
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   139
     *          first one caught, which is then re-thrown.
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
    public static void deleteFileTreeWithRetry(Path dir) throws IOException {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   142
        IOException ioe = null;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   143
        final List<IOException> excs = deleteFileTreeUnchecked(dir);
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   144
        if (!excs.isEmpty()) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   145
            ioe = excs.remove(0);
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   146
            for (IOException x : excs) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   147
                ioe.addSuppressed(x);
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   148
            }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   149
        }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   150
        if (ioe != null) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   151
            throw ioe;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   152
        }
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
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   155
    public static List<IOException> deleteFileTreeUnchecked(Path dir) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   156
        final List<IOException> excs = new ArrayList<>();
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   157
        try {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   158
            java.nio.file.Files.walkFileTree(dir, new SimpleFileVisitor<>() {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   159
                @Override
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   160
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   161
                    try {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   162
                        deleteFileWithRetry0(file);
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   163
                    } catch (IOException x) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   164
                        excs.add(x);
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   165
                    } catch (InterruptedException x) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   166
                        excs.add(new IOException("Interrupted while deleting.", x));
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   167
                        return FileVisitResult.TERMINATE;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   168
                    }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   169
                    return FileVisitResult.CONTINUE;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   170
                }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   171
                @Override
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   172
                public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   173
                    try {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   174
                        deleteFileWithRetry0(dir);
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   175
                    } catch (IOException x) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   176
                        excs.add(x);
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   177
                    } catch (InterruptedException x) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   178
                        excs.add(new IOException("Interrupted while deleting.", x));
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   179
                        return FileVisitResult.TERMINATE;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   180
                    }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   181
                    return FileVisitResult.CONTINUE;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   182
                }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   183
                @Override
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   184
                public FileVisitResult visitFileFailed(Path file, IOException exc) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   185
                    excs.add(exc);
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   186
                    return FileVisitResult.CONTINUE;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   187
                }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   188
            });
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   189
        } catch (IOException x) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   190
            excs.add(x);
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   191
        }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   192
        return excs;
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
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
     * Checks whether all file systems are accessible. This is performed
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   197
     * by checking free disk space on all mounted file systems via a
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   198
     * separate, spawned process. File systems are considered to be
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   199
     * accessible if this process completes successfully before a given
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   200
     * fixed duration has elapsed.
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
     * @implNote On Unix this executes the {@code df} command in a separate
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   203
     * process and on Windows always returns {@code true}.
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
    public static boolean areFileSystemsAccessible() throws IOException {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   206
        boolean areFileSystemsAccessible = true;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   207
        if (!IS_WINDOWS) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   208
            // try to check whether 'df' hangs
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   209
            System.out.println("\n--- df output ---");
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   210
            System.out.flush();
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   211
            Process proc = new ProcessBuilder("df").inheritIO().start();
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   212
            try {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   213
                proc.waitFor(90, TimeUnit.SECONDS);
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   214
            } catch (InterruptedException ignored) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   215
            }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   216
            try {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   217
                int exitValue = proc.exitValue();
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   218
                if (exitValue != 0) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   219
                    System.err.printf("df process exited with %d != 0%n",
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   220
                        exitValue);
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   221
                    areFileSystemsAccessible = false;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   222
                }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   223
            } catch (IllegalThreadStateException ignored) {
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   224
                System.err.println("df command apparently hung");
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   225
                areFileSystemsAccessible = false;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   226
            }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   227
        }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   228
        return areFileSystemsAccessible;
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   229
    }
a08485a70636 8180887: move FileUtils to top level testlibrary
iignatyev
parents:
diff changeset
   230
}