Skip to content

Commit

Permalink
Merge pull request #121 from j-keck/j-freebsd-fix
Browse files Browse the repository at this point in the history
align exception from file-timestamp functions from different impl.
  • Loading branch information
dwijnand authored Jan 23, 2018
2 parents 1dbb37f + 066be44 commit db7dbd0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
20 changes: 14 additions & 6 deletions io/src/main/scala/sbt/internal/io/Milli.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import java.io.File
import java.util.Date
import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.nio.file.Files
import java.nio.file.{ Files, NoSuchFileException }
import java.nio.file.{ Paths => JPaths }
import java.nio.file.attribute.FileTime

Expand Down Expand Up @@ -319,11 +319,19 @@ private abstract class MilliMilliseconds extends Milli {

private object JavaMilli extends MilliMilliseconds {
def getModifiedTime(filePath: String): Long =
Files.getLastModifiedTime(JPaths.get(filePath)).toMillis
def setModifiedTime(filePath: String, mtime: Long): Unit = {
Files.setLastModifiedTime(JPaths.get(filePath), FileTime.fromMillis(mtime))
()
}
mapNoSuchFileException(Files.getLastModifiedTime(JPaths.get(filePath)).toMillis)
def setModifiedTime(filePath: String, mtime: Long): Unit =
mapNoSuchFileException {
Files.setLastModifiedTime(JPaths.get(filePath), FileTime.fromMillis(mtime))
()
}

private def mapNoSuchFileException[A](f: => A): A =
try {
f
} catch {
case e: NoSuchFileException => throw new FileNotFoundException(e.getFile).initCause(e)
}
}

object Milli {
Expand Down
4 changes: 4 additions & 0 deletions io/src/test/scala/sbt/io/IOSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,8 @@ class IOSpec extends FlatSpec with Matchers {
val u = IO.toURI(file("src") / "main" / "scala")
assert(IO.toFile(u) == (file("src") / "main" / "scala"))
}

"getModifiedTimeOrZero" should "return 0L if the file doesn't exists" in {
assert(IO.getModifiedTimeOrZero(file("/not/existing/path")) == 0L)
}
}

0 comments on commit db7dbd0

Please sign in to comment.