Files
Mayur Pawashe 34fcb8784b Support appcast selection features in generate_appcast (#1914)
Add generate_appcast options for appcast selection features

We can now supply for new update items:

* Link
* Channel name
* Last major version / minimum autoupdate version
* Phased rollout interval
* Last critical update version
* Originating informational update versions

We can also specify the maximum number of delta items to generate.

Lastly we can specify which versions to create new items for now. This allows applying flags for only specific versions, or for exceptional cases allows adding old versions that do not meet the most recent updates threshold (maxNewVersionsInFeed) -- think for updates for a prior OS or for a prior major version.

We do not override the minimum system version element on already existing items now, if the element already exists. Apps don't strictly need to specify LSMinimumSystemVersion and this allows users specifying it themselves in the feed.

The help page for generate_appcast was expanded with how the archive directory looks like, how release notes work, that old entries are kept in tact, and how extracted archives are cached..

We now write out the number of new updates written and the number of existing items updated, to make the generated result be less confusing.

Bug fixes:

* The scheme now builds for Debug so I can properly debug generate_appcast from within Xcode (distribution builds are still in Release)
* Newly generated items were appended at the end of the appcast and not necessarily inserted in the correct order (on top). As a workaround when we process existing items, we remove them, and re-add them.
* Fixed an off-by-one error for maximum number of delta files we can generate.

Fixes #1903
2021-08-08 09:07:27 -07:00

166 lines
6.8 KiB
Swift

//
// Created by Kornel on 23/12/2016.
// Copyright © 2016 Sparkle Project. All rights reserved.
//
import Foundation
func makeError(code: SUError, _ description: String) -> NSError {
return NSError(domain: SUSparkleErrorDomain, code: Int(OSStatus(code.rawValue)), userInfo: [
NSLocalizedDescriptionKey: description,
])
}
func makeAppcast(archivesSourceDir: URL, cacheDirectory cacheDir: URL, keys: PrivateKeys, versions: Set<String>?, maximumDeltas: Int, verbose: Bool) throws -> [String: [ArchiveItem]] {
let comparator = SUStandardVersionComparator()
let allUpdates = (try unarchiveUpdates(archivesSourceDir: archivesSourceDir, archivesDestDir: cacheDir, verbose: verbose))
.sorted(by: {
.orderedDescending == comparator.compareVersion($0.version, toVersion: $1.version)
})
if allUpdates.count == 0 {
throw makeError(code: .noUpdateError, "No usable archives found in \(archivesSourceDir.path)")
}
var updatesByAppcast: [String: [ArchiveItem]] = [:]
let group = DispatchGroup()
for update in allUpdates {
group.enter()
DispatchQueue.global().async {
if let privateDSAKey = keys.privateDSAKey {
do {
update.dsaSignature = try dsaSignature(path: update.archivePath, privateDSAKey: privateDSAKey)
} catch {
print(update, error)
}
} else if update.supportsDSA {
print("Note: did not sign with legacy DSA \(update.archivePath.path) because private DSA key file was not specified")
}
if let publicEdKey = update.publicEdKey {
if let privateEdKey = keys.privateEdKey, let expectedPublicKey = keys.publicEdKey {
if publicEdKey == expectedPublicKey {
do {
update.edSignature = try edSignature(path: update.archivePath, publicEdKey: publicEdKey, privateEdKey: privateEdKey)
} catch {
print(update, error)
}
} else {
print("Warning: SUPublicEDKey in the app \(update.archivePath.path) does not match key EdDSA in the Keychain. Run generate_keys and update Info.plist to match")
}
} else {
print("Warning: could not sign \(update.archivePath.path) due to lack of private EdDSA key")
}
}
group.leave()
}
let appcastFile = update.feedURL?.lastPathComponent ?? "appcast.xml"
if updatesByAppcast[appcastFile] == nil {
updatesByAppcast[appcastFile] = []
}
updatesByAppcast[appcastFile]!.append(update)
}
for (_, updates) in updatesByAppcast {
var latestUpdatePerOS: [String: ArchiveItem] = [:]
for update in updates {
// If the new versions are specified, ignore everything else
if let versions = versions, !versions.contains(update.version) {
continue
}
// items are ordered starting latest first
let os = update.minimumSystemVersion
if latestUpdatePerOS[os] == nil {
latestUpdatePerOS[os] = update
}
}
for (_, latestItem) in latestUpdatePerOS {
var numDeltas = 0
let appBaseName = latestItem.appPath.deletingPathExtension().lastPathComponent
for item in updates {
if numDeltas >= maximumDeltas {
break
}
// No downgrades
if .orderedAscending != comparator.compareVersion(item.version, toVersion: latestItem.version) {
continue
}
// Old version will not be able to verify the new version
if !item.supportsDSA && item.publicEdKey == nil {
continue
}
let deltaBaseName = appBaseName + latestItem.version + "-" + item.version
let deltaPath = archivesSourceDir.appendingPathComponent(deltaBaseName).appendingPathExtension("delta")
var delta: DeltaUpdate
let ignoreMarkerPath = cacheDir.appendingPathComponent(deltaPath.lastPathComponent).appendingPathExtension(".ignore")
let fm = FileManager.default
if fm.fileExists(atPath: ignoreMarkerPath.path) {
continue
}
if !fm.fileExists(atPath: deltaPath.path) {
do {
delta = try DeltaUpdate.create(from: item, to: latestItem, archivePath: deltaPath)
} catch {
print("Could not create delta update", deltaPath.path, error)
continue
}
} else {
delta = DeltaUpdate(fromVersion: item.version, archivePath: deltaPath)
}
numDeltas += 1
// Require delta to be a bit smaller
if delta.fileSize / 7 > latestItem.fileSize / 8 {
markDeltaAsIgnored(delta: delta, markerPath: ignoreMarkerPath)
continue
}
group.enter()
DispatchQueue.global().async {
if item.supportsDSA, let privateDSAKey = keys.privateDSAKey {
do {
delta.dsaSignature = try dsaSignature(path: deltaPath, privateDSAKey: privateDSAKey)
} catch {
print(delta.archivePath.lastPathComponent, error)
}
}
if let publicEdKey = item.publicEdKey, let privateEdKey = keys.privateEdKey {
do {
delta.edSignature = try edSignature(path: deltaPath, publicEdKey: publicEdKey, privateEdKey: privateEdKey)
} catch {
print(delta.archivePath.lastPathComponent, error)
}
}
if delta.dsaSignature != nil || delta.edSignature != nil {
latestItem.deltas.append(delta)
} else {
markDeltaAsIgnored(delta: delta, markerPath: ignoreMarkerPath)
print("Delta \(delta.archivePath.path) ignored, because it could not be signed")
}
group.leave()
}
}
}
}
group.wait()
return updatesByAppcast
}
func markDeltaAsIgnored(delta: DeltaUpdate, markerPath: URL) {
_ = try? FileManager.default.removeItem(at: delta.archivePath)
_ = try? Data.init().write(to: markerPath); // 0-sized file
}