Skip to content

Fix checksum computation for Swift SDK bundles #7748

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Sources/Basics/Archiver/Archiver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,8 @@ extension Archiver {
self.validate(path: path, completion: { continuation.resume(with: $0) })
}
}

package func isFileSupported(_ lastPathComponent: String) -> Bool {
self.supportedExtensions.contains(where: { lastPathComponent.hasSuffix($0) })
}
}
13 changes: 3 additions & 10 deletions Sources/Commands/PackageCommands/ComputeChecksum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,10 @@ struct ComputeChecksum: SwiftCommand {
var path: AbsolutePath

func run(_ swiftCommandState: SwiftCommandState) throws {
let binaryArtifactsManager = try Workspace.BinaryArtifactsManager(
fileSystem: swiftCommandState.fileSystem,
authorizationProvider: swiftCommandState.getAuthorizationProvider(),
hostToolchain: swiftCommandState.getHostToolchain(),
checksumAlgorithm: SHA256(),
cachePath: .none,
customHTTPClient: .none,
customArchiver: .none,
delegate: .none
let checksum = try Workspace.BinaryArtifactsManager.checksum(
forBinaryArtifactAt: self.path,
fileSystem: swiftCommandState.fileSystem
)
let checksum = try binaryArtifactsManager.checksum(forBinaryArtifactAt: path)
print(checksum)
}
}
29 changes: 22 additions & 7 deletions Sources/Workspace/Workspace+BinaryArtifacts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import SPMBuildCore

import struct TSCBasic.ByteString
import protocol TSCBasic.HashAlgorithm

import struct TSCBasic.SHA256
import enum TSCUtility.Diagnostics

extension Workspace {
Expand Down Expand Up @@ -537,20 +537,35 @@ extension Workspace {
return result.get()
}

public func checksum(forBinaryArtifactAt path: AbsolutePath) throws -> String {
package static func checksum(
forBinaryArtifactAt path: AbsolutePath,
hashAlgorithm: HashAlgorithm = SHA256(),
archiver: (any Archiver)? = nil,
fileSystem: any FileSystem
) throws -> String {
let archiver = archiver ?? UniversalArchiver(fileSystem)
// Validate the path has a supported extension.
guard let pathExtension = path.extension, self.archiver.supportedExtensions.contains(pathExtension) else {
let supportedExtensionList = self.archiver.supportedExtensions.joined(separator: ", ")
guard let lastPathComponent = path.components.last, archiver.isFileSupported(lastPathComponent) else {
let supportedExtensionList = archiver.supportedExtensions.joined(separator: ", ")
throw StringError("unexpected file type; supported extensions are: \(supportedExtensionList)")
}

// Ensure that the path with the accepted extension is a file.
guard self.fileSystem.isFile(path) else {
guard fileSystem.isFile(path) else {
throw StringError("file not found at path: \(path.pathString)")
}

let contents = try self.fileSystem.readFileContents(path)
return self.checksumAlgorithm.hash(contents).hexadecimalRepresentation
let contents = try fileSystem.readFileContents(path)
return hashAlgorithm.hash(contents).hexadecimalRepresentation
}

public func checksum(forBinaryArtifactAt path: AbsolutePath) throws -> String {
try Self.checksum(
forBinaryArtifactAt: path,
hashAlgorithm: self.checksumAlgorithm,
archiver: self.archiver,
fileSystem: self.fileSystem
)
}

public func cancel(deadline: DispatchTime) throws {
Expand Down