@@ -664,6 +664,7 @@ public final class ManifestLoader: ManifestLoaderProtocol {
664
664
let toolsVersion : ToolsVersion
665
665
let env : [ String : String ]
666
666
let swiftpmVersion : String
667
+ let sha256Checksum : String
667
668
668
669
init ( packageIdentity: PackageIdentity ,
669
670
manifestPath: AbsolutePath ,
@@ -672,37 +673,38 @@ public final class ManifestLoader: ManifestLoaderProtocol {
672
673
swiftpmVersion: String ,
673
674
fileSystem: FileSystem
674
675
) throws {
676
+ let manifestContents = try fileSystem. readFileContents ( manifestPath) . contents
677
+ let sha256Checksum = try Self . computeSHA256Checksum ( packageIdentity: packageIdentity, manifestContents: manifestContents, toolsVersion: toolsVersion, env: env, swiftpmVersion: swiftpmVersion)
678
+
675
679
self . packageIdentity = packageIdentity
676
680
self . manifestPath = manifestPath
677
- self . manifestContents = try fileSystem . readFileContents ( manifestPath ) . contents
681
+ self . manifestContents = manifestContents
678
682
self . toolsVersion = toolsVersion
679
683
self . env = env
680
684
self . swiftpmVersion = swiftpmVersion
685
+ self . sha256Checksum = sha256Checksum
681
686
}
682
687
683
- // TODO: is there a way to avoid the dual hashing?
684
688
func hash( into hasher: inout Hasher ) {
685
- hasher. combine ( self . packageIdentity)
686
- hasher. combine ( self . manifestContents)
687
- hasher. combine ( self . toolsVersion. description)
688
- for key in self . env. keys. sorted ( by: > ) {
689
- hasher. combine ( key)
690
- hasher. combine ( env [ key] !) // forced unwrap safe
691
- }
692
- hasher. combine ( self . swiftpmVersion)
689
+ hasher. combine ( self . sha256Checksum)
693
690
}
694
691
695
- // TODO: is there a way to avoid the dual hashing?
696
- func computeHash( ) throws -> ByteString {
692
+ private static func computeSHA256Checksum(
693
+ packageIdentity: PackageIdentity ,
694
+ manifestContents: [ UInt8 ] ,
695
+ toolsVersion: ToolsVersion ,
696
+ env: [ String : String ] ,
697
+ swiftpmVersion: String
698
+ ) throws -> String {
697
699
let stream = BufferedOutputByteStream ( )
698
- stream <<< self . packageIdentity
699
- stream <<< self . manifestContents
700
- stream <<< self . toolsVersion. description
701
- for key in self . env. keys. sorted ( by: > ) {
702
- stream <<< key <<< env [ key] ! // forced unwrap safe
700
+ stream <<< packageIdentity
701
+ stream <<< manifestContents
702
+ stream <<< toolsVersion. description
703
+ for key in env. keys. sorted ( by: > ) {
704
+ stream <<< key <<< env [ key] ! // forced unwrap safe
703
705
}
704
- stream <<< self . swiftpmVersion
705
- return SHA256 ( ) . hash ( stream. bytes)
706
+ stream <<< swiftpmVersion
707
+ return stream. bytes. sha256Checksum
706
708
}
707
709
}
708
710
@@ -849,7 +851,7 @@ public final class ManifestLoader: ManifestLoaderProtocol {
849
851
if compilerResult. exitStatus != . terminated( code: 0 ) {
850
852
return
851
853
}
852
-
854
+
853
855
// Pass an open file descriptor of a file to which the JSON representation of the manifest will be written.
854
856
let jsonOutputFile = tmpDir. appending ( component: " \( packageIdentity) -output.json " )
855
857
guard let jsonOutputFileDesc = fopen ( jsonOutputFile. pathString, " w " ) else {
@@ -1135,12 +1137,11 @@ private final class SQLiteManifestCache: Closable {
1135
1137
}
1136
1138
1137
1139
func put( key: ManifestLoader . ManifestCacheKey , manifest: ManifestLoader . ManifestParseResult ) throws {
1138
- let query = " INSERT OR IGNORE INTO MANIFEST_CACHE VALUES (?, ?); "
1140
+ let query = " INSERT OR IGNORE INTO MANIFEST_CACHE VALUES (?, ?); "
1139
1141
try self . executeStatement ( query) { statement -> Void in
1140
- let keyHash = try key. computeHash ( )
1141
1142
let data = try self . jsonEncoder. encode ( manifest)
1142
1143
let bindings : [ SQLite . SQLiteValue ] = [
1143
- . string( keyHash . hexadecimalRepresentation ) ,
1144
+ . string( key . sha256Checksum ) ,
1144
1145
. blob( data) ,
1145
1146
]
1146
1147
try statement. bind ( bindings)
@@ -1151,8 +1152,7 @@ private final class SQLiteManifestCache: Closable {
1151
1152
func get( key: ManifestLoader . ManifestCacheKey ) throws -> ManifestLoader . ManifestParseResult ? {
1152
1153
let query = " SELECT value FROM MANIFEST_CACHE WHERE key == ? LIMIT 1; "
1153
1154
return try self . executeStatement ( query) { statement -> ManifestLoader . ManifestParseResult ? in
1154
- let keyHash = try key. computeHash ( )
1155
- try statement. bind ( [ . string( keyHash. hexadecimalRepresentation) ] )
1155
+ try statement. bind ( [ . string( key. sha256Checksum) ] )
1156
1156
let data = try statement. step ( ) ? . blob ( at: 0 )
1157
1157
return try data. flatMap {
1158
1158
try self . jsonDecoder. decode ( ManifestLoader . ManifestParseResult. self, from: $0)
0 commit comments