// download, keyring, integrity and archive replacement order follow sync.c. // SPDX-License-Identifier: GPL-2.0-or-later module engine pub type CorruptedPackageQuestion = fn (voidptr, string, Errno) bool pub struct TransactionQuestions { pub: context voidptr corrupted_package CorruptedPackageQuestion = unsafe { nil } } pub struct TransactionLoadResult { pub: success bool kind Errno invalid_packages []string } pub fn (mut handle Handle) set_transaction_questions(questions TransactionQuestions) ! { handle.check()! handle.transaction_questions = questions } fn (mut handle Handle) prompt_corrupted_package(path string, reason Errno) { questions := handle.transaction_questions if voidptr(questions.corrupted_package) != unsafe { nil } && questions.corrupted_package(questions.context, path, reason) { handle.remove_temporary_file(path) handle.remove_temporary_file(path + '.sig') } } fn (mut handle Handle) download_transaction_packages(mut trans Transaction) ! { defer { for mut pkg in trans.add { pkg.download_bytes = 0 pkg.download_size_loaded = false } } directory := handle.cache_directory()! trans.state = .downloading mut requests := []DownloadRequest{} mut sizes := []i64{} mut total_size := i64(0) for pointer in trans.add { if pointer.origin == .file { continue } mut pkg := unsafe { pointer } mut db := pkg.owner_database()! level := db.effective_siglevel()! if db.servers.len == 0 { return handle.fail(.server_none, db.name) } if !pkg.has_filename && pkg.filename == '' { return handle.fail(.pkg_invalid_name, pkg.name) } if !safe_download_name(pkg.filename) { return handle.fail(.pkg_invalid_name, pkg.filename) } cached := handle.find_cached_file(pkg.filename)! mut needed := pkg.download_bytes != 0 || !cached.found if !needed && level & 1 != 0 { needed = !handle.find_cached_file(pkg.filename + '.sig')!.found } if !needed { continue } if pkg.download_bytes < 0 || pkg.download_bytes > max_i64 - total_size { return handle.fail(.retrieve_prepare, 'transaction download size overflow') } total_size += pkg.download_bytes sizes << pkg.download_bytes 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 } } if requests.len == 0 { return } if handle.opts.integers[.check_space] != 0 { handle.check_download_space(directory, sizes)! } handle.retrieval_event(RetrievalEvent{ kind: .pkg_retrieve_start, packages: requests.len, total_size: total_size }) handle.download_files(requests, directory) or { problem := err handle.retrieval_event(RetrievalEvent{ kind: .pkg_retrieve_failed }) return problem } handle.retrieval_event(RetrievalEvent{ kind: .pkg_retrieve_done }) } struct TransactionKey { uid string fingerprint string } fn (mut handle Handle) transaction_signature(pkg &Package) ![]u8 { if pkg.has_base64_sig { return decode_signature(pkg.base64_sig) } cached := handle.find_cached_file(pkg.filename)! if !cached.found { return []u8{} } return read_signature_file(cached.path + '.sig') } fn (mut handle Handle) check_transaction_keyring(trans &Transaction) ! { handle.commit_event(CommitEvent{ kind: .keyring_start }) mut missing := []TransactionKey{} mut seen := map[string]bool{} for index, pkg in trans.add { handle.commit_progress_for(.keyring, '', index * 100 / trans.add.len, trans.add.len, index) if pkg.origin == .file { continue } mut db := pkg.owner_database()! if db.effective_siglevel()! & 1 == 0 { continue } data := handle.transaction_signature(pkg) or { continue } keys := extract_key_ids(data) or { continue } for fingerprint in keys { if fingerprint in seen { continue } known := handle.key_in_keychain(fingerprint) or { handle.diagnostics << err.msg() continue } if known { continue } seen[fingerprint] = true missing << TransactionKey{ uid: pkg.packager, fingerprint: fingerprint } } } handle.commit_progress_for(.keyring, '', 100, trans.add.len, trans.add.len) handle.commit_event(CommitEvent{ kind: .keyring_done }) if missing.len == 0 { return } handle.commit_event(CommitEvent{ kind: .key_download_start }) mut failed := false for key in missing { if !handle.import_key(key.uid, key.fingerprint) or { handle.diagnostics << err.msg() false } { failed = true } } handle.commit_event(CommitEvent{ kind: .key_download_done }) if failed { return handle.fail(.pkg_invalid_sig, 'required key missing from keyring') } } struct TransactionInvalidPackage { path string kind Errno } fn (mut handle Handle) check_transaction_integrity(mut trans Transaction, total_bytes u64) !TransactionLoadResult { handle.commit_event(CommitEvent{ kind: .integrity_start }) mut current_bytes := u64(0) mut failures := []TransactionInvalidPackage{} for index, pointer in trans.add { handle.commit_progress_for(.integrity, '', int(f64(current_bytes) / f64(total_bytes) * 100), trans.add.len, index) if pointer.origin == .file { continue } mut pkg := unsafe { pointer } current_bytes += u64(pkg.size) cached := handle.find_cached_file(pkg.filename)! if !cached.found { return handle.fail(.pkg_not_found, pkg.filename) } mut db := pkg.owner_database()! level := db.effective_siglevel()! pkg.validation = handle.validate_package(cached.path, pkg, level) or { failures << TransactionInvalidPackage{ path: cached.path, kind: unsafe { Errno(err.code()) } } continue } } handle.commit_progress_for(.integrity, '', 100, trans.add.len, trans.add.len) handle.commit_event(CommitEvent{ kind: .integrity_done }) if failures.len > 0 { for failure in failures { if failure.kind in [.pkg_invalid_sig, .pkg_invalid_checksum] { handle.prompt_corrupted_package(failure.path, failure.kind) } } kind := failures.last().kind handle.fail(kind, '') return TransactionLoadResult{ kind: kind } } return TransactionLoadResult{ success: true } } fn transaction_dependency_key(dep Dependency) string { // Length-prefix the name so field contents cannot make distinct pairs collide. name := dep.name() return '${int(dep.mod)}:${name.len}:${name}${dep.version()}' } fn transaction_dependency_fields_equal(first []Dependency, second []Dependency) bool { if first.len != second.len { return false } // Repositories and archives commonly store the fields in the same order. mut ordered := true for index, dep in first { other := second[index] if dep.name() != other.name() || dep.version() != other.version() || dep.mod != other.mod { ordered = false break } } if ordered { return true } mut counts := map[string]int{} for dep in first { key := transaction_dependency_key(dep) counts[key]++ } for dep in second { key := transaction_dependency_key(dep) count := counts[key] if count == 0 { return false } counts[key] = count - 1 } return true } fn transaction_package_matches(expected &Package, actual &Package) bool { if expected.name != actual.name || expected.version != actual.version || expected.isize != actual.isize { return false } 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) { return false } if expected.groups.len != actual.groups.len { return false } if expected.groups == actual.groups { return true } mut first := expected.groups.clone() mut second := actual.groups.clone() first.sort() second.sort() return first == second } fn (mut handle Handle) load_transaction_archives(mut trans Transaction, total_bytes u64) !TransactionLoadResult { handle.commit_event(CommitEvent{ kind: .load_start }) mut current_bytes := u64(0) mut invalid_paths := []string{} mut failure := Errno.pkg_invalid for index, pointer in trans.add { handle.commit_progress_for(.load, '', int(f64(current_bytes) / f64(total_bytes) * 100), trans.add.len, index) if pointer.origin == .file { continue } mut pkg := unsafe { pointer } current_bytes += u64(pkg.size) cached := handle.find_cached_file(pkg.filename)! if !cached.found { return handle.fail(.pkg_not_found, pkg.filename) } mut loaded := handle.read_package_for_handle(cached.path, true) or { failure = unsafe { Errno(err.code()) } handle.diagnostics << 'cannot load ${pkg.filename}: ${err.msg()}' trans.invalid_packages << pkg.filename invalid_paths << cached.path continue } if !transaction_package_matches(pkg, loaded) { handle.diagnostics << 'archive metadata does not match repository: ${pkg.filename}' trans.invalid_packages << pkg.filename invalid_paths << cached.path continue } loaded.owner = handle loaded.reason = pkg.reason loaded.validation = pkg.validation loaded.old_package = pkg.old_package pkg.old_package = unsafe { nil } trans.add[index] = loaded } handle.commit_progress_for(.load, '', 100, trans.add.len, trans.add.len) handle.commit_event(CommitEvent{ kind: .load_done }) if trans.invalid_packages.len > 0 { for path in invalid_paths { handle.prompt_corrupted_package(path, .pkg_invalid) } handle.fail(failure, '') return TransactionLoadResult{ kind: failure, invalid_packages: trans.invalid_packages.clone() } } return TransactionLoadResult{ success: true } } pub fn (mut handle Handle) load_transaction_packages() !TransactionLoadResult { mut trans := handle.current_transaction()! if trans.state != .prepared { return handle.fail(.trans_not_prepared, '') } if trans.loading_packages { return handle.fail(.trans_not_null, 'transaction archive loading is active') } if trans.add.len == 0 { return TransactionLoadResult{ success: true } } trans.loading_packages = true was_executing := handle.execution_active handle.execution_active = true defer { trans.loading_packages = false handle.execution_active = was_executing } trans.invalid_packages.clear() handle.download_transaction_packages(mut trans)! handle.check_transaction_keyring(trans)! mut total_bytes := u64(0) for pkg in trans.add { if pkg.origin != .file { if pkg.size < 0 || u64(pkg.size) > ~u64(0) - total_bytes { return handle.fail(.pkg_invalid, 'transaction package size overflow') } total_bytes += u64(pkg.size) } } if total_bytes == 0 { total_bytes = 1 } integrity := handle.check_transaction_integrity(mut trans, total_bytes)! if !integrity.success { return integrity } if trans.flags.has(.download_only) { return TransactionLoadResult{ success: true } } return handle.load_transaction_archives(mut trans, total_bytes) } pub fn (mut handle Handle) transaction_invalid_packages() ![]string { trans := handle.current_transaction()! return trans.invalid_packages.clone() }