提交 b70d00fd 编写于 作者: A adinn

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
上级 fa5fbb55
/* /*
* Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -24,7 +24,10 @@ ...@@ -24,7 +24,10 @@
/* @test /* @test
* @bug 4313887 6873621 6979526 7006126 7020517 * @bug 4313887 6873621 6979526 7006126 7020517
* @summary Unit test for java.nio.file.FileStore * @summary Unit test for java.nio.file.FileStore
* @library .. * @key intermittent
* @library .. /lib/testlibrary
* @build jdk.testlibrary.FileUtils
* @run main Basic
*/ */
import java.nio.file.*; import java.nio.file.*;
...@@ -32,6 +35,7 @@ import java.nio.file.attribute.*; ...@@ -32,6 +35,7 @@ import java.nio.file.attribute.*;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.*; import java.util.*;
import jdk.testlibrary.FileUtils;
public class Basic { public class Basic {
...@@ -109,31 +113,36 @@ public class Basic { ...@@ -109,31 +113,36 @@ public class Basic {
/** /**
* Test: Enumerate all FileStores * Test: Enumerate all FileStores
*/ */
FileStore prev = null; if (FileUtils.areFileSystemsAccessible()) {
for (FileStore store: FileSystems.getDefault().getFileStores()) { FileStore prev = null;
System.out.format("%s (name=%s type=%s)\n", store, store.name(), for (FileStore store: FileSystems.getDefault().getFileStores()) {
store.type()); System.out.format("%s (name=%s type=%s)\n", store, store.name(),
store.type());
// check space attributes are accessible
try { // check space attributes are accessible
store.getTotalSpace(); try {
store.getUnallocatedSpace(); store.getTotalSpace();
store.getUsableSpace(); store.getUnallocatedSpace();
} catch (NoSuchFileException nsfe) { store.getUsableSpace();
// ignore exception as the store could have been } catch (NoSuchFileException nsfe) {
// deleted since the iterator was instantiated // ignore exception as the store could have been
System.err.format("%s was not found\n", store); // deleted since the iterator was instantiated
} catch (AccessDeniedException ade) { System.err.format("%s was not found\n", store);
// ignore exception as the lack of ability to access the } catch (AccessDeniedException ade) {
// store due to lack of file permission or similar does not // ignore exception as the lack of ability to access the
// reflect whether the space attributes would be accessible // store due to lack of file permission or similar does not
// were access to be permitted // reflect whether the space attributes would be accessible
System.err.format("%s is inaccessible\n", store); // 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;
} }
} else {
// two distinct FileStores should not be equal System.err.println
assertTrue(!store.equals(prev)); ("Skipping FileStore check due to file system access failure");
prev = store;
} }
} }
} }
...@@ -24,7 +24,9 @@ ...@@ -24,7 +24,9 @@
/* @test /* @test
* @bug 4313887 6838333 * @bug 4313887 6838333
* @summary Unit test for java.nio.file.FileSystem * @summary Unit test for java.nio.file.FileSystem
* @library .. * @library .. /lib/testlibrary
* @build jdk.testlibrary.FileUtils
* @run main Basic
*/ */
import java.io.File; import java.io.File;
...@@ -40,6 +42,7 @@ import java.nio.file.Paths; ...@@ -40,6 +42,7 @@ import java.nio.file.Paths;
import java.nio.file.ProviderNotFoundException; import java.nio.file.ProviderNotFoundException;
import java.util.HashMap; import java.util.HashMap;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import jdk.testlibrary.FileUtils;
/** /**
* Simple santity checks for java.nio.file.FileSystem * Simple santity checks for java.nio.file.FileSystem
...@@ -51,41 +54,17 @@ public class Basic { ...@@ -51,41 +54,17 @@ public class Basic {
throw new RuntimeException(msg); throw new RuntimeException(msg);
} }
static void checkFileStores(String os, FileSystem fs) throws IOException { static void checkFileStores(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;
}
}
// sanity check method // sanity check method
if (checkFileStores) { if (FileUtils.areFileSystemsAccessible()) {
System.out.println("\n--- Begin FileStores ---"); System.out.println("\n--- Begin FileStores ---");
for (FileStore store: fs.getFileStores()) { for (FileStore store: fs.getFileStores()) {
System.out.println(store); System.out.println(store);
} }
System.out.println("--- EndFileStores ---\n"); System.out.println("--- EndFileStores ---\n");
} else { } else {
System.err.println("Skipping FileStore check due to df failure"); System.err.println
("Skipping FileStore check due to file system access failure");
} }
} }
...@@ -114,7 +93,7 @@ public class Basic { ...@@ -114,7 +93,7 @@ public class Basic {
"should use 'file' scheme"); "should use 'file' scheme");
// sanity check FileStores // sanity check FileStores
checkFileStores(os, fs); checkFileStores(fs);
// sanity check supportedFileAttributeViews // sanity check supportedFileAttributeViews
checkSupported(fs, "basic"); checkSupported(fs, "basic");
......
...@@ -33,6 +33,7 @@ import java.nio.file.SimpleFileVisitor; ...@@ -33,6 +33,7 @@ import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit;
/** /**
...@@ -190,5 +191,41 @@ public final class FileUtils { ...@@ -190,5 +191,41 @@ public final class FileUtils {
} }
return excs; 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.
先完成此消息的编辑!
想要评论请 注册