Add support for binary checksum files

This commit is contained in:
Alexandre Archambault 2017-05-30 16:59:46 +02:00
parent a8c7ab3f03
commit 18be2e2dd2
6 changed files with 33 additions and 9 deletions

View File

@ -838,6 +838,18 @@ object Cache {
parseChecksumLine(lines) orElse parseChecksumAlternative(lines) parseChecksumLine(lines) orElse parseChecksumAlternative(lines)
} }
def parseRawChecksum(content: Array[Byte]): Option[BigInteger] =
if (content.length == 16 || content.length == 20)
Some(new BigInteger(content))
else {
val s = new String(content, UTF_8)
val lines = s
.lines
.toVector
parseChecksumLine(lines) orElse parseChecksumAlternative(lines)
}
// matches md5 or sha1 // matches md5 or sha1
private val checksumPattern = Pattern.compile("^[0-9a-f]{32}([0-9a-f]{8})?") private val checksumPattern = Pattern.compile("^[0-9a-f]{32}([0-9a-f]{8})?")
@ -870,9 +882,7 @@ object Cache {
val sumFile = localFile(sumUrl, cache, artifact.authentication.map(_.user)) val sumFile = localFile(sumUrl, cache, artifact.authentication.map(_.user))
Task { Task {
val sumOpt = parseChecksum( val sumOpt = parseRawChecksum(FileUtil.readAllBytes(sumFile))
new String(FileUtil.readAllBytes(sumFile), UTF_8)
)
sumOpt match { sumOpt match {
case None => case None =>

View File

@ -1,6 +1,7 @@
package coursier package coursier
import java.io._ import java.io._
import java.nio.charset.Charset
import scala.language.implicitConversions import scala.language.implicitConversions
@ -23,6 +24,8 @@ object Platform {
buffer.toByteArray buffer.toByteArray
} }
private lazy val UTF_8 = Charset.forName("UTF-8")
def readFully(is: => InputStream) = def readFully(is: => InputStream) =
Task { Task {
\/.fromTryCatchNonFatal { \/.fromTryCatchNonFatal {
@ -31,7 +34,7 @@ object Platform {
try readFullySync(is0) try readFullySync(is0)
finally is0.close() finally is0.close()
new String(b, "UTF-8") new String(b, UTF_8)
} .leftMap{ } .leftMap{
case e: java.io.FileNotFoundException if e.getMessage != null => case e: java.io.FileNotFoundException if e.getMessage != null =>
s"Not found: ${e.getMessage}" s"Not found: ${e.getMessage}"

View File

@ -223,9 +223,7 @@ object Assembly {
throw new Exception(s"SHA-1 file not found for ${a.url}") throw new Exception(s"SHA-1 file not found for ${a.url}")
} }
val sumOpt = Cache.parseChecksum( val sumOpt = Cache.parseRawChecksum(FileUtil.readAllBytes(f))
new String(FileUtil.readAllBytes(f), "UTF-8")
)
sumOpt match { sumOpt match {
case Some(sum) => case Some(sum) =>

Binary file not shown.

View File

@ -0,0 +1 @@
レ9」硤kK 2Uソ<55>`星リ

View File

@ -22,7 +22,7 @@ object ChecksumTests extends TestSuite {
assert(Cache.parseChecksum(other) == expected) assert(Cache.parseChecksum(other) == expected)
} }
* - { 'junk - {
// https://repo1.maven.org/maven2/org/apache/spark/spark-core_2.11/1.2.0/spark-core_2.11-1.2.0.pom.sha1 // https://repo1.maven.org/maven2/org/apache/spark/spark-core_2.11/1.2.0/spark-core_2.11-1.2.0.pom.sha1
// as of 2016-03-02 // as of 2016-03-02
val junkSha1 = val junkSha1 =
@ -34,7 +34,7 @@ object ChecksumTests extends TestSuite {
sha1ParseTest(cleanSha1, junkSha1) sha1ParseTest(cleanSha1, junkSha1)
} }
* - { 'singleLine - {
// https://repo1.maven.org/maven2/org/json/json/20080701/json-20080701.pom.sha1 // https://repo1.maven.org/maven2/org/json/json/20080701/json-20080701.pom.sha1
// as of 2016-03-05 // as of 2016-03-05
val dirtySha1 = val dirtySha1 =
@ -45,6 +45,18 @@ object ChecksumTests extends TestSuite {
sha1ParseTest(cleanSha1, dirtySha1) sha1ParseTest(cleanSha1, dirtySha1)
} }
'binarySha1 - {
val content = Platform.readFullySync(getClass.getResource("/empty.sha1").openStream())
val res = Cache.parseRawChecksum(content)
assert(res.nonEmpty)
}
'binaryMd5 - {
val content = Platform.readFullySync(getClass.getResource("/empty.md5").openStream())
val res = Cache.parseRawChecksum(content)
assert(res.nonEmpty)
}
} }
'artifact - { 'artifact - {