下面列出了怎么用java.nio.file.FileSystemLoopException的API类实例代码及写法,或者点击链接到github查看源代码。
private void validateFileSystemLoopException(Path start, Path... causes) {
try (Stream<Path> s = Files.walk(start, FileVisitOption.FOLLOW_LINKS)) {
try {
int count = s.mapToInt(p -> 1).reduce(0, Integer::sum);
fail("Should got FileSystemLoopException, but got " + count + "elements.");
} catch (UncheckedIOException uioe) {
IOException ioe = uioe.getCause();
if (ioe instanceof FileSystemLoopException) {
FileSystemLoopException fsle = (FileSystemLoopException) ioe;
boolean match = false;
for (Path cause: causes) {
if (fsle.getFile().equals(cause.toString())) {
match = true;
break;
}
}
assertTrue(match);
} else {
fail("Unexpected UncheckedIOException cause " + ioe.toString());
}
}
} catch(IOException ex) {
fail("Unexpected IOException " + ex);
}
}
private void validateFileSystemLoopException(Path start, Path... causes) {
try (Stream<Path> s = Files.walk(start, FileVisitOption.FOLLOW_LINKS)) {
try {
int count = s.mapToInt(p -> 1).reduce(0, Integer::sum);
fail("Should got FileSystemLoopException, but got " + count + "elements.");
} catch (UncheckedIOException uioe) {
IOException ioe = uioe.getCause();
if (ioe instanceof FileSystemLoopException) {
FileSystemLoopException fsle = (FileSystemLoopException) ioe;
boolean match = false;
for (Path cause: causes) {
if (fsle.getFile().equals(cause.toString())) {
match = true;
break;
}
}
assertTrue(match);
} else {
fail("Unexpected UncheckedIOException cause " + ioe.toString());
}
}
} catch(IOException ex) {
fail("Unexpected IOException " + ex);
}
}
private void validateFileSystemLoopException(Path start, Path... causes) {
try (Stream<Path> s = Files.walk(start, FileVisitOption.FOLLOW_LINKS)) {
try {
int count = s.mapToInt(p -> 1).reduce(0, Integer::sum);
fail("Should got FileSystemLoopException, but got " + count + "elements.");
} catch (UncheckedIOException uioe) {
IOException ioe = uioe.getCause();
if (ioe instanceof FileSystemLoopException) {
FileSystemLoopException fsle = (FileSystemLoopException) ioe;
boolean match = false;
for (Path cause: causes) {
if (fsle.getFile().equals(cause.toString())) {
match = true;
break;
}
}
assertTrue(match);
} else {
fail("Unexpected UncheckedIOException cause " + ioe.toString());
}
}
} catch(IOException ex) {
fail("Unexpected IOException " + ex);
}
}
private void validateFileSystemLoopException(Path start, Path... causes) {
try (Stream<Path> s = Files.walk(start, FileVisitOption.FOLLOW_LINKS)) {
try {
int count = s.mapToInt(p -> 1).reduce(0, Integer::sum);
fail("Should got FileSystemLoopException, but got " + count + "elements.");
} catch (UncheckedIOException uioe) {
IOException ioe = uioe.getCause();
if (ioe instanceof FileSystemLoopException) {
FileSystemLoopException fsle = (FileSystemLoopException) ioe;
boolean match = false;
for (Path cause: causes) {
if (fsle.getFile().equals(cause.toString())) {
match = true;
break;
}
}
assertTrue(match);
} else {
fail("Unexpected UncheckedIOException cause " + ioe.toString());
}
}
} catch(IOException ex) {
fail("Unexpected IOException " + ex);
}
}
private void validateFileSystemLoopException(Path start, Path... causes) {
try (Stream<Path> s = Files.walk(start, FileVisitOption.FOLLOW_LINKS)) {
try {
int count = s.mapToInt(p -> 1).reduce(0, Integer::sum);
fail("Should got FileSystemLoopException, but got " + count + "elements.");
} catch (UncheckedIOException uioe) {
IOException ioe = uioe.getCause();
if (ioe instanceof FileSystemLoopException) {
FileSystemLoopException fsle = (FileSystemLoopException) ioe;
boolean match = false;
for (Path cause: causes) {
if (fsle.getFile().equals(cause.toString())) {
match = true;
break;
}
}
assertTrue(match);
} else {
fail("Unexpected UncheckedIOException cause " + ioe.toString());
}
}
} catch(IOException ex) {
fail("Unexpected IOException " + ex);
}
}
private void validateFileSystemLoopException(Path start, Path... causes) {
try (Stream<Path> s = Files.walk(start, FileVisitOption.FOLLOW_LINKS)) {
try {
int count = s.mapToInt(p -> 1).reduce(0, Integer::sum);
fail("Should got FileSystemLoopException, but got " + count + "elements.");
} catch (UncheckedIOException uioe) {
IOException ioe = uioe.getCause();
if (ioe instanceof FileSystemLoopException) {
FileSystemLoopException fsle = (FileSystemLoopException) ioe;
boolean match = false;
for (Path cause: causes) {
if (fsle.getFile().equals(cause.toString())) {
match = true;
break;
}
}
assertTrue(match);
} else {
fail("Unexpected UncheckedIOException cause " + ioe.toString());
}
}
} catch(IOException ex) {
fail("Unexpected IOException " + ex);
}
}
private void validateFileSystemLoopException(Path start, Path... causes) {
try (Stream<Path> s = Files.walk(start, FileVisitOption.FOLLOW_LINKS)) {
try {
int count = s.mapToInt(p -> 1).reduce(0, Integer::sum);
fail("Should got FileSystemLoopException, but got " + count + "elements.");
} catch (UncheckedIOException uioe) {
IOException ioe = uioe.getCause();
if (ioe instanceof FileSystemLoopException) {
FileSystemLoopException fsle = (FileSystemLoopException) ioe;
boolean match = false;
for (Path cause: causes) {
if (fsle.getFile().equals(cause.toString())) {
match = true;
break;
}
}
assertTrue(match);
} else {
fail("Unexpected UncheckedIOException cause " + ioe.toString());
}
}
} catch(IOException ex) {
fail("Unexpected IOException " + ex);
}
}
private void validateFileSystemLoopException(Path start, Path... causes) {
try (Stream<Path> s = Files.walk(start, FileVisitOption.FOLLOW_LINKS)) {
try {
int count = s.mapToInt(p -> 1).reduce(0, Integer::sum);
fail("Should got FileSystemLoopException, but got " + count + "elements.");
} catch (UncheckedIOException uioe) {
IOException ioe = uioe.getCause();
if (ioe instanceof FileSystemLoopException) {
FileSystemLoopException fsle = (FileSystemLoopException) ioe;
boolean match = false;
for (Path cause: causes) {
if (fsle.getFile().equals(cause.toString())) {
match = true;
break;
}
}
assertTrue(match);
} else {
fail("Unexpected UncheckedIOException cause " + ioe.toString());
}
}
} catch(IOException ex) {
fail("Unexpected IOException " + ex);
}
}
private void validateFileSystemLoopException(Path start, Path... causes) {
try (Stream<Path> s = Files.walk(start, FileVisitOption.FOLLOW_LINKS)) {
try {
int count = s.mapToInt(p -> 1).reduce(0, Integer::sum);
fail("Should got FileSystemLoopException, but got " + count + "elements.");
} catch (UncheckedIOException uioe) {
IOException ioe = uioe.getCause();
if (ioe instanceof FileSystemLoopException) {
FileSystemLoopException fsle = (FileSystemLoopException) ioe;
boolean match = false;
for (Path cause: causes) {
if (fsle.getFile().equals(cause.toString())) {
match = true;
break;
}
}
assertTrue(match);
} else {
fail("Unexpected UncheckedIOException cause " + ioe.toString());
}
}
} catch(IOException ex) {
fail("Unexpected IOException " + ex);
}
}
private void validateFileSystemLoopException(Path start, Path... causes) {
try (Stream<Path> s = Files.walk(start, FileVisitOption.FOLLOW_LINKS)) {
try {
int count = s.mapToInt(p -> 1).reduce(0, Integer::sum);
fail("Should got FileSystemLoopException, but got " + count + "elements.");
} catch (UncheckedIOException uioe) {
IOException ioe = uioe.getCause();
if (ioe instanceof FileSystemLoopException) {
FileSystemLoopException fsle = (FileSystemLoopException) ioe;
boolean match = false;
for (Path cause: causes) {
if (fsle.getFile().equals(cause.toString())) {
match = true;
break;
}
}
assertTrue(match);
} else {
fail("Unexpected UncheckedIOException cause " + ioe.toString());
}
}
} catch(IOException ex) {
fail("Unexpected IOException " + ex);
}
}
private void validateFileSystemLoopException(Path start, Path... causes) {
try (Stream<Path> s = Files.walk(start, FileVisitOption.FOLLOW_LINKS)) {
try {
int count = s.mapToInt(p -> 1).reduce(0, Integer::sum);
fail("Should got FileSystemLoopException, but got " + count + "elements.");
} catch (UncheckedIOException uioe) {
IOException ioe = uioe.getCause();
if (ioe instanceof FileSystemLoopException) {
FileSystemLoopException fsle = (FileSystemLoopException) ioe;
boolean match = false;
for (Path cause: causes) {
if (fsle.getFile().equals(cause.toString())) {
match = true;
break;
}
}
assertTrue(match);
} else {
fail("Unexpected UncheckedIOException cause " + ioe.toString());
}
}
} catch(IOException ex) {
fail("Unexpected IOException " + ex);
}
}
private void validateFileSystemLoopException(Path start, Path... causes) {
try (Stream<Path> s = Files.walk(start, FileVisitOption.FOLLOW_LINKS)) {
try {
int count = s.mapToInt(p -> 1).reduce(0, Integer::sum);
fail("Should got FileSystemLoopException, but got " + count + "elements.");
} catch (UncheckedIOException uioe) {
IOException ioe = uioe.getCause();
if (ioe instanceof FileSystemLoopException) {
FileSystemLoopException fsle = (FileSystemLoopException) ioe;
boolean match = false;
for (Path cause: causes) {
if (fsle.getFile().equals(cause.toString())) {
match = true;
break;
}
}
assertTrue(match);
} else {
fail("Unexpected UncheckedIOException cause " + ioe.toString());
}
}
} catch(IOException ex) {
fail("Unexpected IOException " + ex);
}
}
private void validateFileSystemLoopException(Path start, Path... causes) {
try (Stream<Path> s = Files.walk(start, FileVisitOption.FOLLOW_LINKS)) {
try {
int count = s.mapToInt(p -> 1).reduce(0, Integer::sum);
fail("Should got FileSystemLoopException, but got " + count + "elements.");
} catch (UncheckedIOException uioe) {
IOException ioe = uioe.getCause();
if (ioe instanceof FileSystemLoopException) {
FileSystemLoopException fsle = (FileSystemLoopException) ioe;
boolean match = false;
for (Path cause: causes) {
if (fsle.getFile().equals(cause.toString())) {
match = true;
break;
}
}
assertTrue(match);
} else {
fail("Unexpected UncheckedIOException cause " + ioe.toString());
}
}
} catch(IOException ex) {
fail("Unexpected IOException " + ex);
}
}
/**
* {@inheritDoc}
*/
@Override
public FileVisitResult visitFileFailed(final Path file, final IOException ioe) {
if (ioe instanceof FileSystemLoopException) {
log.warn("Detected file system cycle visiting while visiting {}. Skipping.", file);
return FileVisitResult.SKIP_SUBTREE;
} else if (ioe instanceof AccessDeniedException) {
log.warn("Access denied for file {}. Skipping", file);
return FileVisitResult.SKIP_SUBTREE;
} else if (ioe instanceof NoSuchFileException) {
log.warn("File or directory disappeared while visiting {}. Skipping", file);
return FileVisitResult.SKIP_SUBTREE;
} else {
log.error("Got unknown error {} while visiting {}. Terminating visitor", ioe.getMessage(), file, ioe);
// TODO: Not sure if we should do this or skip subtree or just continue and ignore it?
return FileVisitResult.TERMINATE;
}
}
@Override
public FileVisitResult visitFileFailed(Object file, IOException ioe) throws IOException {
if (ioe instanceof FileSystemLoopException) {
System.err.println("Cycle was detected: " + (Path) file);
} else {
System.err.println("Error occured, unable to copy:" + (Path) file + " [" + ioe + "]");
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) {
if (exc instanceof FileSystemLoopException) {
LOGGER.error("File system loop/cycle detected: " + file, exc);
} else {
LOGGER.error("Error while copying resource: " + file, exc);
}
return CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) {
if (exc instanceof FileSystemLoopException) {
System.err.println("cycle detected: " + file);
} else {
System.err.format("Unable to copy: %s: %s%n", file, exc);
}
return CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(final Path file, final IOException exc) {
if (exc instanceof FileSystemLoopException) {
System.err.println("cycle detected: " + file);
} else {
System.err.format("Unable to copy: %s: %s%n", file, exc);
}
return CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) {
if (exc instanceof FileSystemLoopException) {
LOGGER.error("cycle detected: {}", file);
} else {
LOGGER.error("Unable to copy: {}", file, exc);
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
if (exc instanceof FileSystemLoopException) {
logger.error("cycle detected: " + file);
} else {
logger.error("Unable to copy: %s: %s%n", file, exc);
}
return CONTINUE;
}
@Override
public ExitCode handleException(IOException e) {
if (e instanceof FileSystemLoopException) {
return ExitCode.FATAL_GENERIC;
} else if (e.getMessage() != null
&& e.getMessage().startsWith("No space left on device")) {
return ExitCode.FATAL_DISK_FULL;
} else {
return ExitCode.FATAL_IO;
}
}
private void ensureNoLoops(Path p, BasicFileAttributes attrs) throws FileSystemLoopException {
if (!followLinks) {
return;
}
if (!attrs.isDirectory()) {
return;
}
if (willLoop(p, attrs)) {
throw new FileSystemLoopException(p.toString());
}
}
@Test
public void testFileSystemLoopException() {
LoggedErrors errors =
logException(new TestException(new FileSystemLoopException("It's a loop!")));
assertNull(errors.userVisible);
assertThat(
errors.userVisibleInternal,
Matchers.containsString(
"Loop detected in your directory, which may be caused by circular symlink. "
+ "You may consider running the command in a smaller directory."));
assertThat(
errors.userVisibleInternal,
Matchers.containsString("java.nio.file.FileSystemLoopException: It's a loop!"));
}
/** Returns the message (and optionally stack trace) for the root cause. */
public String getMessage(boolean suppressStackTraces) {
if (rootCause instanceof HumanReadableException) {
return ((HumanReadableException) rootCause).getHumanReadableErrorMessage();
}
if (rootCause instanceof InterruptedException
|| rootCause instanceof ClosedByInterruptException) {
return "Interrupted";
}
if (rootCause instanceof BuckIsDyingException) {
return "Failed because buck was already dying";
}
if (isNoSpaceOnDevice()) {
return rootCause.getMessage();
}
String message = "";
if (rootCause instanceof FileSystemLoopException) {
// TODO(cjhopman): Is this message helpful? What's a smaller directory?
message =
"Loop detected in your directory, which may be caused by circular symlink. "
+ "You may consider running the command in a smaller directory."
+ System.lineSeparator();
}
if (rootCause instanceof OutOfMemoryError) {
message =
"Buck ran out of memory, you may consider increasing heap size with java args "
+ "(see https://buck.build/files-and-dirs/buckjavaargs.html)"
+ System.lineSeparator();
}
if (suppressStackTraces) {
return String.format(
"%s%s: %s", message, rootCause.getClass().getName(), rootCause.getMessage());
}
return String.format("%s%s", message, Throwables.getStackTraceAsString(originalException));
}
@Test
public void testWithFileLoopException() {
assertThat(
registry.handleException(new FileSystemLoopException("Symlink found")),
is(ExitCode.FATAL_GENERIC));
}