From 45a6a4856070ad529cc082afa144fcce3ee5b2ea Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Sat, 11 Nov 2017 17:51:19 +0100 Subject: [PATCH] Fix #3450: Partial revert of 7ff1cb6fbc6261a364481be597b20a17423f4d72 --- compiler/src/dotty/tools/io/Directory.scala | 6 ++++-- compiler/src/dotty/tools/io/PlainFile.scala | 13 +++++++------ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/compiler/src/dotty/tools/io/Directory.scala b/compiler/src/dotty/tools/io/Directory.scala index 4e670d50c39b..facccada7277 100644 --- a/compiler/src/dotty/tools/io/Directory.scala +++ b/compiler/src/dotty/tools/io/Directory.scala @@ -49,8 +49,10 @@ class Directory(jpath: JPath) extends Path(jpath) { /** An iterator over the contents of this directory. */ def list: Iterator[Path] = - if (isDirectory) Files.list(jpath).iterator.asScala.map(Path.apply) - else Iterator.empty + jpath.toFile.listFiles match { + case null => Iterator.empty + case xs => xs.iterator.map(x => Path(x.toPath)) + } def dirs: Iterator[Directory] = list collect { case x: Directory => x } def files: Iterator[File] = list collect { case x: File => x } diff --git a/compiler/src/dotty/tools/io/PlainFile.scala b/compiler/src/dotty/tools/io/PlainFile.scala index b7e3e171f557..2a381cb5745a 100644 --- a/compiler/src/dotty/tools/io/PlainFile.scala +++ b/compiler/src/dotty/tools/io/PlainFile.scala @@ -55,13 +55,14 @@ class PlainFile(val givenPath: Path) extends AbstractFile { /** Returns all abstract subfiles of this abstract directory. */ def iterator: Iterator[AbstractFile] = { - try { - import scala.collection.JavaConverters._ - val it = Files.newDirectoryStream(jpath).iterator() - it.asScala.map(p => new PlainFile(Path(p))) - } catch { - case _: NotDirectoryException => Iterator.empty + // Optimization: Assume that the file was not deleted and did not have permissions changed + // between the call to `list` and the iteration. This saves a call to `exists`. + def existsFast(path: Path) = path match { + case (_: Directory | _: io.File) => true + case _ => path.exists } + if (!isDirectory) Iterator.empty + else givenPath.toDirectory.list filter existsFast map (new PlainFile(_)) } /**