Skip to content

Commit be940d6

Browse files
committed
Fix creation and deletion methods
1 parent bddcc77 commit be940d6

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

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

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

88
import scala.language.implicitConversions
99
import java.io.RandomAccessFile
10-
import java.nio.file.{FileAlreadyExistsException, Files}
10+
import java.nio.file.{DirectoryNotEmptyException, FileAlreadyExistsException, Files, NoSuchFileException}
1111
import java.net.{URI, URL}
1212

1313
import scala.util.Random.alphanumeric
@@ -206,25 +206,23 @@ class Path private[io] (val jpath: JPath) {
206206

207207
// creations
208208
def createDirectory(force: Boolean = true, failIfExists: Boolean = false): Directory = {
209-
val res = try {
210-
if (force) Files.createDirectories(jpath) else Files.createDirectory(jpath)
211-
true
212-
} catch {
213-
case _: FileAlreadyExistsException => false
214-
}
209+
val res = tryCreate(if (force) Files.createDirectories(jpath) else Files.createDirectory(jpath))
215210
if (!res && failIfExists && exists) fail("Directory '%s' already exists." format name)
216211
else if (isDirectory) toDirectory
217212
else new Directory(jpath)
218213
}
219214
def createFile(failIfExists: Boolean = false): File = {
220-
val res = try { Files.createFile(jpath); true } catch { case e: FileAlreadyExistsException => false}
215+
val res = tryCreate(Files.createFile(jpath))
221216
if (!res && failIfExists && exists) fail("File '%s' already exists." format name)
222217
else if (isFile) toFile
223218
else new File(jpath)
224219
}
225220

221+
private def tryCreate(create: => JPath): Boolean =
222+
try { create; true } catch { case _: FileAlreadyExistsException => false }
223+
226224
// deletions
227-
def delete(): Unit = Files.delete(jpath)
225+
def delete(): Unit = delete(jpath)
228226

229227
/** Deletes the path recursively. Returns false on failure.
230228
* Use with caution!
@@ -234,12 +232,12 @@ class Path private[io] (val jpath: JPath) {
234232
import scala.collection.JavaConverters._
235233
if (Files.isDirectory(p))
236234
Files.list(p).iterator().asScala.foreach(deleteRecursively)
237-
try {
238-
Files.delete(p)
239-
true
240-
} catch { case _: Throwable => false }
235+
delete(p)
241236
}
242237

238+
private def delete(path: JPath): Boolean =
239+
try { Files.delete(path); true } catch { case _: DirectoryNotEmptyException | _: NoSuchFileException => false }
240+
243241
def truncate() =
244242
isFile && {
245243
val raf = new RandomAccessFile(jfile, "rw")

0 commit comments

Comments
 (0)