Skip to content

Commit 3108499

Browse files
committed
Improve some implementations based on java Paths
1 parent d49469d commit 3108499

File tree

2 files changed

+27
-19
lines changed

2 files changed

+27
-19
lines changed

compiler/src/dotty/tools/io/Directory.scala

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,9 @@ class Directory(jpath: JPath) extends Path(jpath) {
4848

4949
/** An iterator over the contents of this directory.
5050
*/
51-
def list: Iterator[Path] = {
52-
try {
53-
Files.list(jpath).toArray[JPath](n => new Array(n)).iterator.map(Path.apply)
54-
} catch {
55-
case _: java.nio.file.NoSuchFileException => Iterator.empty
56-
}
57-
}
51+
def list: Iterator[Path] =
52+
if (isDirectory) Files.list(jpath).iterator.asScala.map(Path.apply)
53+
else Iterator.empty
5854

5955
def dirs: Iterator[Directory] = list collect { case x: Directory => x }
6056
def files: Iterator[File] = list collect { case x: File => x }

compiler/src/dotty/tools/io/Path.scala

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@ package dotty.tools.io
77

88
import scala.language.implicitConversions
99
import java.io.RandomAccessFile
10-
import java.nio.file.{DirectoryNotEmptyException, FileAlreadyExistsException, Files, NoSuchFileException, Paths}
10+
import java.nio.file._
1111
import java.net.{URI, URL}
12+
import java.nio.file.attribute.BasicFileAttributes
13+
import java.io.IOException
14+
15+
import scala.collection.JavaConverters._
1216

1317
import scala.util.Random.alphanumeric
1418

@@ -51,7 +55,7 @@ object Path {
5155
def onlyDirs(xs: List[Path]): List[Directory] = xs filter (_.isDirectory) map (_.toDirectory)
5256
def onlyFiles(xs: Iterator[Path]): Iterator[File] = xs filter (_.isFile) map (_.toFile)
5357

54-
def roots: List[Path] = java.io.File.listRoots().toList.map(r => Path.apply(r.toPath))
58+
def roots: List[Path] = FileSystems.getDefault.getRootDirectories.iterator().asScala.map(Path.apply).toList
5559

5660
def apply(path: String): Path = apply(Paths.get(path))
5761
def apply(jpath: JPath): Path = try {
@@ -221,22 +225,30 @@ class Path private[io] (val jpath: JPath) {
221225
try { create; true } catch { case _: FileAlreadyExistsException => false }
222226

223227
// deletions
224-
def delete(): Unit = delete(jpath)
228+
def delete(): Unit =
229+
try { Files.deleteIfExists(jpath) } catch { case _: DirectoryNotEmptyException => }
225230

226231
/** Deletes the path recursively. Returns false on failure.
227232
* Use with caution!
228233
*/
229-
def deleteRecursively(): Boolean = deleteRecursively(jpath)
230-
private def deleteRecursively(p: JPath): Boolean = {
231-
import scala.collection.JavaConverters._
232-
if (Files.isDirectory(p))
233-
Files.list(p).iterator().asScala.foreach(deleteRecursively)
234-
delete(p)
234+
def deleteRecursively(): Boolean = {
235+
if (!exists) false
236+
else {
237+
Files.walkFileTree(jpath, new SimpleFileVisitor[JPath]() {
238+
override def visitFile(file: JPath, attrs: BasicFileAttributes) = {
239+
Files.delete(file)
240+
FileVisitResult.CONTINUE
241+
}
242+
243+
override def postVisitDirectory(dir: JPath, exc: IOException) = {
244+
Files.delete(dir)
245+
FileVisitResult.CONTINUE
246+
}
247+
})
248+
true
249+
}
235250
}
236251

237-
private def delete(path: JPath): Boolean =
238-
try { Files.deleteIfExists(path); true } catch { case _: DirectoryNotEmptyException => false }
239-
240252
def truncate() =
241253
isFile && {
242254
val raf = new RandomAccessFile(jpath.toFile, "rw")

0 commit comments

Comments
 (0)