Skip to content

Commit

Permalink
Merge pull request #171 from eed3si9n/wip/delete2
Browse files Browse the repository at this point in the history
Fixes file deletion error on Windows 2
  • Loading branch information
eed3si9n authored Jun 25, 2018
2 parents 7cdc5f0 + 57870f2 commit c7a83d4
Showing 1 changed file with 7 additions and 37 deletions.
44 changes: 7 additions & 37 deletions io/src/main/scala/sbt/io/IO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,14 @@ import java.io.{
BufferedWriter,
File,
InputStream,
IOException,
OutputStream,
PrintWriter
}
import java.io.{ ObjectInputStream, ObjectStreamClass, FileNotFoundException }
import java.net.{ URI, URISyntaxException, URL }
import java.nio.charset.Charset
import java.nio.file.{
FileSystems,
Files,
Path => NioPath,
SimpleFileVisitor,
FileVisitResult,
NoSuchFileException
}
import java.nio.file.attribute.{ PosixFilePermissions, BasicFileAttributes }
import java.nio.file.FileSystems
import java.nio.file.attribute.PosixFilePermissions
import java.util.Properties
import java.util.jar.{ Attributes, JarEntry, JarOutputStream, Manifest }
import java.util.zip.{ CRC32, ZipEntry, ZipInputStream, ZipOutputStream }
Expand Down Expand Up @@ -462,34 +454,12 @@ object IO {

/** Deletes `file`, recursively if it is a directory. */
def delete(file: File): Unit = {
object deleter extends SimpleFileVisitor[NioPath] {
def deletePath(path: NioPath) =
translate("Error deleting " + path.toFile + ": ") {
try {
Files.delete(path)
} catch {
case _: NoSuchFileException => // ignore missing files
}
}

override def visitFile(filePath: NioPath, attr: BasicFileAttributes): FileVisitResult = {
deletePath(filePath)
FileVisitResult.CONTINUE
}

override def postVisitDirectory(dirPath: NioPath, e: IOException): FileVisitResult = {
if (e eq null) {
deletePath(dirPath)
FileVisitResult.CONTINUE
} else throw e // directory iteration failed
}
}
translate("Error during a recursive delete of path " + file + ": ") {
try {
Files.walkFileTree(file.toPath, deleter)
translate("Error deleting file " + file + ": ") {
val deleted = file.delete()
if (!deleted && file.isDirectory) {
delete(listFiles(file))
file.delete
()
} catch {
case _: NoSuchFileException => // ignore missing file or dir
}
}
}
Expand Down

0 comments on commit c7a83d4

Please sign in to comment.