| 1 | // removal policy follows pacman remove.c; ordering/state follow trans.c. |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | module engine |
| 4 | |
| 5 | pub enum PreparationEventKind { |
| 6 | check_dependencies_start |
| 7 | check_dependencies_done |
| 8 | resolve_dependencies_start |
| 9 | resolve_dependencies_done |
| 10 | conflicts_start |
| 11 | conflicts_done |
| 12 | optional_dependency_removed |
| 13 | } |
| 14 | |
| 15 | pub struct PreparationEvent { |
| 16 | pub: |
| 17 | kind PreparationEventKind |
| 18 | package &Package = unsafe { nil } |
| 19 | dependency Dependency |
| 20 | } |
| 21 | |
| 22 | pub type PreparationCallback = fn (voidptr, PreparationEvent) |
| 23 | |
| 24 | pub fn (mut handle Handle) set_preparation_callback(callback PreparationCallback, context voidptr) ! { |
| 25 | handle.check()! |
| 26 | handle.preparation_callback = callback |
| 27 | handle.preparation_context = context |
| 28 | } |
| 29 | |
| 30 | fn (handle &Handle) preparation_event(event PreparationEvent) { |
| 31 | if voidptr(handle.preparation_callback) != unsafe { nil } { |
| 32 | handle.preparation_callback(handle.preparation_context, event) |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | pub struct PreparationResult { |
| 37 | pub: |
| 38 | success bool |
| 39 | kind Errno |
| 40 | missing []MissingDependency |
| 41 | conflicts []PackageConflict |
| 42 | invalid_architectures []string |
| 43 | } |
| 44 | |
| 45 | pub fn (mut handle Handle) prepare_transaction() !PreparationResult { |
| 46 | mut trans := handle.current_transaction()! |
| 47 | if trans.state != .initialized { return handle.fail(.trans_not_initialized, '') } |
| 48 | // pacman leaves an empty transaction initialized rather than prepared. |
| 49 | if trans.remove.len == 0 && trans.add.len == 0 { return PreparationResult{ success: true } } |
| 50 | mut invalid_architectures := []string{} |
| 51 | architectures := handle.opts.lists[.architectures] |
| 52 | if architectures.len > 0 { |
| 53 | for pkg in trans.add { |
| 54 | dependency_metadata(pkg) |
| 55 | if (!pkg.has_architecture && pkg.architecture == '') || pkg.architecture == 'any' { |
| 56 | continue |
| 57 | } |
| 58 | if pkg.architecture !in architectures { |
| 59 | invalid_architectures << '${pkg.name}-${pkg.version}-${pkg.architecture}' |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | if invalid_architectures.len > 0 { |
| 64 | handle.fail(.pkg_invalid_arch, '') |
| 65 | return PreparationResult{ kind: .pkg_invalid_arch, invalid_architectures: invalid_architectures } |
| 66 | } |
| 67 | mut local := handle.local_database()! |
| 68 | installed := local.package_cache()! |
| 69 | result := if trans.add.len > 0 { |
| 70 | handle.prepare_install(mut trans, installed)! |
| 71 | } else { |
| 72 | handle.prepare_removal(mut trans, installed)! |
| 73 | } |
| 74 | if !result.success { return result } |
| 75 | if !trans.flags.has(.no_deps) { |
| 76 | trans.add = order_dependencies(trans.add, installed, trans.remove, false).packages |
| 77 | trans.remove = order_dependencies(trans.remove, installed, [], true).packages |
| 78 | } |
| 79 | trans.state = .prepared |
| 80 | return PreparationResult{ success: true } |
| 81 | } |
| 82 | |
| 83 | fn (mut handle Handle) prepare_removal(mut trans Transaction, installed []&Package) !PreparationResult { |
| 84 | if trans.flags.has(.recurse) && !trans.flags.has(.cascade) { |
| 85 | trans.remove = snapshot_removal_closure(installed, trans.remove, trans.flags.has(.recurse_all)) |
| 86 | } |
| 87 | if !trans.flags.has(.no_deps) { |
| 88 | handle.preparation_event(PreparationEvent{ kind: .check_dependencies_start }) |
| 89 | handle.log(.debug, 'looking for unsatisfied dependencies\n') |
| 90 | handle.check()! |
| 91 | mut missing := check_dependencies(installed, trans.remove, [], DependencyCheckOptions{ |
| 92 | reverse: true |
| 93 | ignore_versions: trans.flags.has(.no_depversion) |
| 94 | assumed: handle.opts.assumed |
| 95 | }) |
| 96 | for missing.len > 0 { |
| 97 | if trans.flags.has(.cascade) { |
| 98 | for miss in missing { |
| 99 | if _ := find_package(trans.remove, miss.target) { continue } |
| 100 | pkg := find_package(installed, miss.target) or { |
| 101 | handle.log(.error, localized_message('could not find %s in database -- skipping\n', miss.target)) |
| 102 | continue |
| 103 | } |
| 104 | handle.log(.debug, 'pulling ${pkg.name} in target list\n') |
| 105 | trans.remove << package_snapshot(pkg) |
| 106 | } |
| 107 | } else if trans.flags.has(.unneeded) { |
| 108 | for miss in missing { |
| 109 | for index, pkg in trans.remove { |
| 110 | if pkg.name == miss.causing_package { |
| 111 | trans.remove.delete(index) |
| 112 | handle.log(.warning, localized_message('removing %s from target list\n', pkg.name)) |
| 113 | break |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | } else { |
| 118 | handle.fail(.unsatisfied_deps, '') |
| 119 | return PreparationResult{ kind: .unsatisfied_deps, missing: missing } |
| 120 | } |
| 121 | handle.check()! |
| 122 | missing = check_dependencies(installed, trans.remove, [], DependencyCheckOptions{ |
| 123 | reverse: true |
| 124 | ignore_versions: trans.flags.has(.no_depversion) |
| 125 | assumed: handle.opts.assumed |
| 126 | }) |
| 127 | } |
| 128 | } |
| 129 | if trans.flags.has(.cascade) && trans.flags.has(.recurse) { |
| 130 | trans.remove = snapshot_removal_closure(installed, trans.remove, trans.flags.has(.recurse_all)) |
| 131 | } |
| 132 | if !trans.flags.has(.no_deps) { |
| 133 | for pkg in installed { |
| 134 | // remove.c reads optdepends even for packages elected for removal. |
| 135 | handle.check()! |
| 136 | dependency_metadata(pkg) |
| 137 | if _ := find_package(trans.remove, pkg.name) { continue } |
| 138 | for optional in pkg.optdepends { |
| 139 | if _ := find_satisfier(trans.remove, optional) { |
| 140 | handle.preparation_event(PreparationEvent{ kind: .optional_dependency_removed, package: pkg, dependency: optional }) |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | handle.preparation_event(PreparationEvent{ kind: .check_dependencies_done }) |
| 145 | } |
| 146 | return PreparationResult{ success: true } |
| 147 | } |
| 148 | |
| 149 | fn snapshot_removal_closure(installed []&Package, targets []&Package, include_explicit bool) []&Package { |
| 150 | selected := recursive_removals(installed, targets, include_explicit) |
| 151 | mut result := targets.clone() |
| 152 | for index in targets.len .. selected.len { result << package_snapshot(selected[index]) } |
| 153 | return result |
| 154 | } |
| 155 | |