Allow FileInfo for non-existent files with the new timestamps

FileInfo is used to wrap information like last modified time on
files that may or may not exist. Arguably, that does not make
much sense: the non-existent files should not lead to modification
file information, hashes, and a persistent serialized version of
the resulting meaningless information. However, considering that
the FileInfo information is serialized and saved, it is necessary
to preserve compatibility at this stage. Therefore the modification
time is explicitly set to zero for those files that do not exist
when each FileInfo is built.
This commit is contained in:
Antonio Cunei 2017-12-08 23:50:40 +01:00
parent d03dfb3981
commit cd4346c5d7
1 changed files with 12 additions and 2 deletions

View File

@ -4,6 +4,7 @@
package sbt.util
import java.io.File
import java.io.FileNotFoundException
import scala.util.control.NonFatal
import sbt.io.Hash
import sjsonnew.{ Builder, JsonFormat, Unbuilder, deserializationError }
@ -50,6 +51,15 @@ object FilesInfo {
}
object FileInfo {
def getModifiedTimeOrZero(file: File) = { // returns 0L if file does not exist
try {
getModifiedTime(file)
} catch {
case _: FileNotFoundException => 0L
}
}
sealed trait Style {
type F <: FileInfo
@ -89,7 +99,7 @@ object FileInfo {
}
implicit def apply(file: File): HashModifiedFileInfo =
FileHashModified(file.getAbsoluteFile, Hash(file).toList, getModifiedTime(file))
FileHashModified(file.getAbsoluteFile, Hash(file).toList, getModifiedTimeOrZero(file))
}
object hash extends Style {
@ -146,7 +156,7 @@ object FileInfo {
}
implicit def apply(file: File): ModifiedFileInfo =
FileModified(file.getAbsoluteFile, getModifiedTime(file))
FileModified(file.getAbsoluteFile, getModifiedTimeOrZero(file))
}
object exists extends Style {