nspkg / engine / transaction_load.v
294 lines · 277 sloc · 10.75 KB · c01b0795d1db56c7bce2539b2c46b110637fdc1a
Raw
1// download, keyring, integrity and archive replacement order follow sync.c.
2// SPDX-License-Identifier: GPL-2.0-or-later
3module engine
4
5pub type CorruptedPackageQuestion = fn (voidptr, string, Errno) bool
6
7pub struct TransactionQuestions {
8pub:
9 context voidptr
10 corrupted_package CorruptedPackageQuestion = unsafe { nil }
11}
12
13pub struct TransactionLoadResult {
14pub:
15 success bool
16 kind Errno
17 invalid_packages []string
18}
19
20pub fn (mut handle Handle) set_transaction_questions(questions TransactionQuestions) ! {
21 handle.check()!
22 handle.transaction_questions = questions
23}
24
25fn (mut handle Handle) prompt_corrupted_package(path string, reason Errno) {
26 questions := handle.transaction_questions
27 if voidptr(questions.corrupted_package) != unsafe { nil } && questions.corrupted_package(questions.context, path, reason) {
28 handle.remove_temporary_file(path)
29 handle.remove_temporary_file(path + '.sig')
30 }
31}
32
33fn (mut handle Handle) download_transaction_packages(mut trans Transaction) ! {
34 defer {
35 for mut pkg in trans.add {
36 pkg.download_bytes = 0
37 pkg.download_size_loaded = false
38 }
39 }
40 directory := handle.cache_directory()!
41 trans.state = .downloading
42 mut requests := []DownloadRequest{}
43 mut sizes := []i64{}
44 mut total_size := i64(0)
45 for pointer in trans.add {
46 if pointer.origin == .file { continue }
47 mut pkg := unsafe { pointer }
48 mut db := pkg.owner_database()!
49 level := db.effective_siglevel()!
50 if db.servers.len == 0 { return handle.fail(.server_none, db.name) }
51 if !pkg.has_filename && pkg.filename == '' {
52 return handle.fail(.pkg_invalid_name, pkg.name)
53 }
54 if !safe_download_name(pkg.filename) { return handle.fail(.pkg_invalid_name, pkg.filename) }
55 cached := handle.find_cached_file(pkg.filename)!
56 mut needed := pkg.download_bytes != 0 || !cached.found
57 if !needed && level & 1 != 0 {
58 needed = !handle.find_cached_file(pkg.filename + '.sig')!.found
59 }
60 if !needed { continue }
61 if pkg.download_bytes < 0 || pkg.download_bytes > max_i64 - total_size {
62 return handle.fail(.retrieve_prepare, 'transaction download size overflow')
63 }
64 total_size += pkg.download_bytes
65 sizes << pkg.download_bytes
66 requests << DownloadRequest{ filepath: pkg.filename, filename: pkg.filename, servers: db.servers.clone(), cache_servers: db.cache_servers.clone(), allow_resume: true, max_size: pkg.size, download_signature: level & 1 != 0, signature_optional: level & 2 != 0 }
67 }
68 if requests.len == 0 { return }
69 if handle.opts.integers[.check_space] != 0 { handle.check_download_space(directory, sizes)! }
70 handle.retrieval_event(RetrievalEvent{ kind: .pkg_retrieve_start, packages: requests.len, total_size: total_size })
71 handle.download_files(requests, directory) or {
72 problem := err
73 handle.retrieval_event(RetrievalEvent{ kind: .pkg_retrieve_failed })
74 return problem
75 }
76 handle.retrieval_event(RetrievalEvent{ kind: .pkg_retrieve_done })
77}
78
79struct TransactionKey {
80 uid string
81 fingerprint string
82}
83
84fn (mut handle Handle) transaction_signature(pkg &Package) ![]u8 {
85 if pkg.has_base64_sig { return decode_signature(pkg.base64_sig) }
86 cached := handle.find_cached_file(pkg.filename)!
87 if !cached.found { return []u8{} }
88 return read_signature_file(cached.path + '.sig')
89}
90
91fn (mut handle Handle) check_transaction_keyring(trans &Transaction) ! {
92 handle.commit_event(CommitEvent{ kind: .keyring_start })
93 mut missing := []TransactionKey{}
94 mut seen := map[string]bool{}
95 for index, pkg in trans.add {
96 handle.commit_progress_for(.keyring, '', index * 100 / trans.add.len, trans.add.len, index)
97 if pkg.origin == .file { continue }
98 mut db := pkg.owner_database()!
99 if db.effective_siglevel()! & 1 == 0 { continue }
100 data := handle.transaction_signature(pkg) or { continue }
101 keys := extract_key_ids(data) or { continue }
102 for fingerprint in keys {
103 if fingerprint in seen { continue }
104 known := handle.key_in_keychain(fingerprint) or {
105 handle.diagnostics << err.msg()
106 continue
107 }
108 if known { continue }
109 seen[fingerprint] = true
110 missing << TransactionKey{ uid: pkg.packager, fingerprint: fingerprint }
111 }
112 }
113 handle.commit_progress_for(.keyring, '', 100, trans.add.len, trans.add.len)
114 handle.commit_event(CommitEvent{ kind: .keyring_done })
115 if missing.len == 0 { return }
116 handle.commit_event(CommitEvent{ kind: .key_download_start })
117 mut failed := false
118 for key in missing {
119 if !handle.import_key(key.uid, key.fingerprint) or {
120 handle.diagnostics << err.msg()
121 false
122 } {
123 failed = true
124 }
125 }
126 handle.commit_event(CommitEvent{ kind: .key_download_done })
127 if failed { return handle.fail(.pkg_invalid_sig, 'required key missing from keyring') }
128}
129
130struct TransactionInvalidPackage {
131 path string
132 kind Errno
133}
134
135fn (mut handle Handle) check_transaction_integrity(mut trans Transaction, total_bytes u64) !TransactionLoadResult {
136 handle.commit_event(CommitEvent{ kind: .integrity_start })
137 mut current_bytes := u64(0)
138 mut failures := []TransactionInvalidPackage{}
139 for index, pointer in trans.add {
140 handle.commit_progress_for(.integrity, '', int(f64(current_bytes) / f64(total_bytes) * 100), trans.add.len, index)
141 if pointer.origin == .file { continue }
142 mut pkg := unsafe { pointer }
143 current_bytes += u64(pkg.size)
144 cached := handle.find_cached_file(pkg.filename)!
145 if !cached.found { return handle.fail(.pkg_not_found, pkg.filename) }
146 mut db := pkg.owner_database()!
147 level := db.effective_siglevel()!
148 pkg.validation = handle.validate_package(cached.path, pkg, level) or {
149 failures << TransactionInvalidPackage{ path: cached.path, kind: unsafe { Errno(err.code()) } }
150 continue
151 }
152 }
153 handle.commit_progress_for(.integrity, '', 100, trans.add.len, trans.add.len)
154 handle.commit_event(CommitEvent{ kind: .integrity_done })
155 if failures.len > 0 {
156 for failure in failures {
157 if failure.kind in [.pkg_invalid_sig, .pkg_invalid_checksum] {
158 handle.prompt_corrupted_package(failure.path, failure.kind)
159 }
160 }
161 kind := failures.last().kind
162 handle.fail(kind, '')
163 return TransactionLoadResult{ kind: kind }
164 }
165 return TransactionLoadResult{ success: true }
166}
167
168fn transaction_dependency_key(dep Dependency) string {
169 // Length-prefix the name so field contents cannot make distinct pairs collide.
170 name := dep.name()
171 return '${int(dep.mod)}:${name.len}:${name}${dep.version()}'
172}
173
174fn transaction_dependency_fields_equal(first []Dependency, second []Dependency) bool {
175 if first.len != second.len { return false }
176 // Repositories and archives commonly store the fields in the same order.
177 mut ordered := true
178 for index, dep in first {
179 other := second[index]
180 if dep.name() != other.name() || dep.version() != other.version() || dep.mod != other.mod {
181 ordered = false
182 break
183 }
184 }
185 if ordered { return true }
186 mut counts := map[string]int{}
187 for dep in first {
188 key := transaction_dependency_key(dep)
189 counts[key]++
190 }
191 for dep in second {
192 key := transaction_dependency_key(dep)
193 count := counts[key]
194 if count == 0 { return false }
195 counts[key] = count - 1
196 }
197 return true
198}
199
200fn transaction_package_matches(expected &Package, actual &Package) bool {
201 if expected.name != actual.name || expected.version != actual.version || expected.isize != actual.isize {
202 return false
203 }
204 if !transaction_dependency_fields_equal(expected.depends, actual.depends) || !transaction_dependency_fields_equal(expected.conflicts, actual.conflicts) || !transaction_dependency_fields_equal(expected.replaces, actual.replaces) || !transaction_dependency_fields_equal(expected.provides, actual.provides) {
205 return false
206 }
207 if expected.groups.len != actual.groups.len { return false }
208 if expected.groups == actual.groups { return true }
209 mut first := expected.groups.clone()
210 mut second := actual.groups.clone()
211 first.sort()
212 second.sort()
213 return first == second
214}
215
216fn (mut handle Handle) load_transaction_archives(mut trans Transaction, total_bytes u64) !TransactionLoadResult {
217 handle.commit_event(CommitEvent{ kind: .load_start })
218 mut current_bytes := u64(0)
219 mut invalid_paths := []string{}
220 mut failure := Errno.pkg_invalid
221 for index, pointer in trans.add {
222 handle.commit_progress_for(.load, '', int(f64(current_bytes) / f64(total_bytes) * 100), trans.add.len, index)
223 if pointer.origin == .file { continue }
224 mut pkg := unsafe { pointer }
225 current_bytes += u64(pkg.size)
226 cached := handle.find_cached_file(pkg.filename)!
227 if !cached.found { return handle.fail(.pkg_not_found, pkg.filename) }
228 mut loaded := handle.read_package_for_handle(cached.path, true) or {
229 failure = unsafe { Errno(err.code()) }
230 handle.diagnostics << 'cannot load ${pkg.filename}: ${err.msg()}'
231 trans.invalid_packages << pkg.filename
232 invalid_paths << cached.path
233 continue
234 }
235 if !transaction_package_matches(pkg, loaded) {
236 handle.diagnostics << 'archive metadata does not match repository: ${pkg.filename}'
237 trans.invalid_packages << pkg.filename
238 invalid_paths << cached.path
239 continue
240 }
241 loaded.owner = handle
242 loaded.reason = pkg.reason
243 loaded.validation = pkg.validation
244 loaded.old_package = pkg.old_package
245 pkg.old_package = unsafe { nil }
246 trans.add[index] = loaded
247 }
248 handle.commit_progress_for(.load, '', 100, trans.add.len, trans.add.len)
249 handle.commit_event(CommitEvent{ kind: .load_done })
250 if trans.invalid_packages.len > 0 {
251 for path in invalid_paths { handle.prompt_corrupted_package(path, .pkg_invalid) }
252 handle.fail(failure, '')
253 return TransactionLoadResult{ kind: failure, invalid_packages: trans.invalid_packages.clone() }
254 }
255 return TransactionLoadResult{ success: true }
256}
257
258pub fn (mut handle Handle) load_transaction_packages() !TransactionLoadResult {
259 mut trans := handle.current_transaction()!
260 if trans.state != .prepared { return handle.fail(.trans_not_prepared, '') }
261 if trans.loading_packages {
262 return handle.fail(.trans_not_null, 'transaction archive loading is active')
263 }
264 if trans.add.len == 0 { return TransactionLoadResult{ success: true } }
265 trans.loading_packages = true
266 was_executing := handle.execution_active
267 handle.execution_active = true
268 defer {
269 trans.loading_packages = false
270 handle.execution_active = was_executing
271 }
272 trans.invalid_packages.clear()
273 handle.download_transaction_packages(mut trans)!
274 handle.check_transaction_keyring(trans)!
275 mut total_bytes := u64(0)
276 for pkg in trans.add {
277 if pkg.origin != .file {
278 if pkg.size < 0 || u64(pkg.size) > ~u64(0) - total_bytes {
279 return handle.fail(.pkg_invalid, 'transaction package size overflow')
280 }
281 total_bytes += u64(pkg.size)
282 }
283 }
284 if total_bytes == 0 { total_bytes = 1 }
285 integrity := handle.check_transaction_integrity(mut trans, total_bytes)!
286 if !integrity.success { return integrity }
287 if trans.flags.has(.download_only) { return TransactionLoadResult{ success: true } }
288 return handle.load_transaction_archives(mut trans, total_bytes)
289}
290
291pub fn (mut handle Handle) transaction_invalid_packages() ![]string {
292 trans := handle.current_transaction()!
293 return trans.invalid_packages.clone()
294}
295