提交 32cc0264 编写于 作者: B bpb

8176237: (fs) java/nio/file/FileStore/Basic.java should conditionally check FileStores

Summary: On Unix platforms, spawn a 'df' process and skip FileStore check if it hangs
Reviewed-by: alanb, chegar
上级 ccefa5e9
/*
* Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -24,8 +24,11 @@
/* @test
* @bug 4313887 6873621 6979526 7006126 7020517
* @summary Unit test for java.nio.file.FileStore
* @library ..
* @key intermittent
* @library ..
* @library .. /lib/testlibrary
* @build jdk.testlibrary.FileUtils
* @run main Basic
*/
import java.nio.file.*;
......@@ -33,6 +36,8 @@ import java.nio.file.attribute.*;
import java.io.File;
import java.io.IOException;
import java.util.*;
import jdk.testlibrary.FileUtils;
public class Basic {
......@@ -110,31 +115,36 @@ public class Basic {
/**
* Test: Enumerate all FileStores
*/
FileStore prev = null;
for (FileStore store: FileSystems.getDefault().getFileStores()) {
System.out.format("%s (name=%s type=%s)\n", store, store.name(),
store.type());
// check space attributes are accessible
try {
store.getTotalSpace();
store.getUnallocatedSpace();
store.getUsableSpace();
} catch (NoSuchFileException nsfe) {
// ignore exception as the store could have been
// deleted since the iterator was instantiated
System.err.format("%s was not found\n", store);
} catch (AccessDeniedException ade) {
// ignore exception as the lack of ability to access the
// store due to lack of file permission or similar does not
// reflect whether the space attributes would be accessible
// were access to be permitted
System.err.format("%s is inaccessible\n", store);
if (FileUtils.areFileSystemsAccessible()) {
FileStore prev = null;
for (FileStore store: FileSystems.getDefault().getFileStores()) {
System.out.format("%s (name=%s type=%s)\n", store, store.name(),
store.type());
// check space attributes are accessible
try {
store.getTotalSpace();
store.getUnallocatedSpace();
store.getUsableSpace();
} catch (NoSuchFileException nsfe) {
// ignore exception as the store could have been
// deleted since the iterator was instantiated
System.err.format("%s was not found\n", store);
} catch (AccessDeniedException ade) {
// ignore exception as the lack of ability to access the
// store due to lack of file permission or similar does not
// reflect whether the space attributes would be accessible
// were access to be permitted
System.err.format("%s is inaccessible\n", store);
}
// two distinct FileStores should not be equal
assertTrue(!store.equals(prev));
prev = store;
}
// two distinct FileStores should not be equal
assertTrue(!store.equals(prev));
prev = store;
} else {
System.err.println
("Skipping FileStore check due to file system access failure");
}
}
}
......@@ -41,7 +41,6 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.ProviderNotFoundException;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;
import jdk.testlibrary.FileUtils;
/**
......@@ -54,41 +53,17 @@ public class Basic {
throw new RuntimeException(msg);
}
static void checkFileStores(String os, FileSystem fs) throws IOException {
boolean checkFileStores = true;
if (!os.equals("Windows")) {
// try to check whether 'df' hangs
System.out.println("\n--- Begin df output ---");
System.out.flush();
Process proc = new ProcessBuilder("df").inheritIO().start();
try {
proc.waitFor(90, TimeUnit.SECONDS);
} catch (InterruptedException ignored) {
}
System.out.println("--- End df output ---\n");
System.out.flush();
try {
int exitValue = proc.exitValue();
if (exitValue != 0) {
System.err.printf("df process exited with %d != 0%n",
exitValue);
checkFileStores = false;
}
} catch (IllegalThreadStateException ignored) {
System.err.println("df command apparently hung");
checkFileStores = false;
}
}
static void checkFileStores(FileSystem fs) throws IOException {
// sanity check method
if (checkFileStores) {
if (FileUtils.areFileSystemsAccessible()) {
System.out.println("\n--- Begin FileStores ---");
for (FileStore store: fs.getFileStores()) {
System.out.println(store);
}
System.out.println("--- EndFileStores ---\n");
} else {
System.err.println("Skipping FileStore check due to df failure");
System.err.println
("Skipping FileStore check due to file system access failure");
}
}
......@@ -133,7 +108,7 @@ public class Basic {
"should use 'file' scheme");
// sanity check FileStores
checkFileStores(os, fs);
checkFileStores(fs);
// sanity check supportedFileAttributeViews
checkSupported(fs, "basic");
......
/*
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -33,6 +33,7 @@ import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
......@@ -190,4 +191,40 @@ public final class FileUtils {
}
return excs;
}
/**
* Checks whether all file systems are accessible. This is performed
* by checking free disk space on all mounted file systems via a
* separate, spawned process. File systems are considered to be
* accessible if this process completes successfully before a given
* fixed duration has elapsed.
*
* @implNote On Unix this executes the {@code df} command in a separate
* process and on Windows always returns {@code true}.
*/
public static boolean areFileSystemsAccessible() throws IOException {
boolean areFileSystemsAccessible = true;
if (!isWindows) {
// try to check whether 'df' hangs
System.out.println("\n--- df output ---");
System.out.flush();
Process proc = new ProcessBuilder("df").inheritIO().start();
try {
proc.waitFor(90, TimeUnit.SECONDS);
} catch (InterruptedException ignored) {
}
try {
int exitValue = proc.exitValue();
if (exitValue != 0) {
System.err.printf("df process exited with %d != 0%n",
exitValue);
areFileSystemsAccessible = false;
}
} catch (IllegalThreadStateException ignored) {
System.err.println("df command apparently hung");
areFileSystemsAccessible = false;
}
}
return areFileSystemsAccessible;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册