forked from sparkle-project/Sparkle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppcast.swift
164 lines (141 loc) · 6.73 KB
/
Appcast.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//
// Created by Kornel on 23/12/2016.
// Copyright © 2016 Sparkle Project. All rights reserved.
//
import Foundation
// Maximum number of delta updates (per OS).
let maxDeltas = 5
func makeError(code: SUError, _ description: String) -> NSError {
return NSError(domain: SUSparkleErrorDomain, code: Int(OSStatus(code.rawValue)), userInfo: [
NSLocalizedDescriptionKey: description,
])
}
func makeAppcast(archivesSourceDir: URL, keys: PrivateKeys, verbose: Bool) throws -> [String: [ArchiveItem]] {
let cacheDir = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0].appendingPathComponent("Sparkle_generate_appcast")
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 {
// 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 > maxDeltas {
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
}