| 1 | // repository preference and provider questions follow pacman lib/libalpm/deps.c. |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | module engine |
| 4 | |
| 5 | pub type IgnorePackageQuestion = fn (voidptr, &Package) bool |
| 6 | pub type ProviderQuestion = fn (voidptr, Dependency, []&Package) int |
| 7 | pub type SkipUnresolvableQuestion = fn (voidptr, []&Package) bool |
| 8 | pub type RemoveConflictQuestion = fn (voidptr, PackageConflict) bool |
| 9 | pub type ReplacePackageQuestion = fn (voidptr, &Package, &Package, &Database) bool |
| 10 | |
| 11 | pub struct DependencyQuestions { |
| 12 | pub: |
| 13 | context voidptr |
| 14 | install_ignored IgnorePackageQuestion = unsafe { nil } |
| 15 | select_provider ProviderQuestion = unsafe { nil } |
| 16 | skip_unresolvable SkipUnresolvableQuestion = unsafe { nil } |
| 17 | remove_conflict RemoveConflictQuestion = unsafe { nil } |
| 18 | replace_package ReplacePackageQuestion = unsafe { nil } |
| 19 | } |
| 20 | |
| 21 | pub fn (mut handle Handle) set_dependency_questions(questions DependencyQuestions) ! { |
| 22 | handle.check()! |
| 23 | handle.dependency_questions = questions |
| 24 | } |
| 25 | |
| 26 | fn (mut handle Handle) allow_dependency(pointer &Package, prompt bool) bool { |
| 27 | mut pkg := unsafe { pointer } |
| 28 | ignored := handle.should_ignore(mut pkg) or { return false } |
| 29 | if !ignored { return true } |
| 30 | if !prompt { |
| 31 | if handle.has_log_callback() { |
| 32 | handle.log(.warning, localized_message('ignoring package %s-%s\n', pkg.name, pkg.version)) |
| 33 | } |
| 34 | return false |
| 35 | } |
| 36 | questions := handle.dependency_questions |
| 37 | return prompt && voidptr(questions.install_ignored) != unsafe { nil } |
| 38 | && questions.install_ignored(questions.context, pkg) |
| 39 | } |
| 40 | |
| 41 | fn (mut handle Handle) resolve_database_dependency(databases []&Database, dependency Dependency, excluding []&Package, prompt bool) ?&Package { |
| 42 | mut ignored := false |
| 43 | for pointer in databases { |
| 44 | mut db := unsafe { pointer } |
| 45 | if db.usage & 12 == 0 { continue } |
| 46 | db.ensure_package_cache_preserving_error() or { continue } |
| 47 | pkg := db.by_name[dependency.name()] or { continue } |
| 48 | if !version_satisfies(pkg.version, dependency) { continue } |
| 49 | if _ := find_package(excluding, pkg.name) { continue } |
| 50 | if !handle.allow_dependency(pkg, prompt) { |
| 51 | ignored = true |
| 52 | continue |
| 53 | } |
| 54 | return pkg |
| 55 | } |
| 56 | mut providers := []&Package{} |
| 57 | for pointer in databases { |
| 58 | mut db := unsafe { pointer } |
| 59 | if db.usage & 12 == 0 { continue } |
| 60 | db.ensure_package_cache_preserving_error() or { continue } |
| 61 | for pkg in db.packages { |
| 62 | if pkg.name == dependency.name() { continue } |
| 63 | dependency_metadata(pkg) |
| 64 | if !pkg.provides.any(provision_satisfies(it, dependency)) { continue } |
| 65 | if _ := find_package(excluding, pkg.name) { continue } |
| 66 | if !handle.allow_dependency(pkg, prompt) { |
| 67 | ignored = true |
| 68 | continue |
| 69 | } |
| 70 | if handle.has_log_callback() { |
| 71 | handle.log(.debug, 'provider found (${pkg.name} provides ${dependency.name()})\n') |
| 72 | } |
| 73 | mut local := handle.local |
| 74 | local.ensure_package_cache_preserving_error() or { return none } |
| 75 | if _ := local.by_name[pkg.name] { return pkg } |
| 76 | providers << pkg |
| 77 | } |
| 78 | } |
| 79 | if providers.len > 0 { |
| 80 | questions := handle.dependency_questions |
| 81 | index := if providers.len > 1 && voidptr(questions.select_provider) != unsafe { nil } { |
| 82 | questions.select_provider(questions.context, dependency, providers.clone()) |
| 83 | } else { |
| 84 | 0 |
| 85 | } |
| 86 | if index >= 0 && index < providers.len { return providers[index] } |
| 87 | } |
| 88 | handle.fail(if ignored { .pkg_ignored } else { .pkg_not_found }, dependency.compute_string()) |
| 89 | return none |
| 90 | } |
| 91 | |
| 92 | pub fn (mut handle Handle) find_database_satisfier(databases []&Database, specification string) ?&Package { |
| 93 | handle.check() or { return none } |
| 94 | if databases.len == 0 { |
| 95 | handle.fail(.wrong_args, 'no databases supplied') |
| 96 | return none |
| 97 | } |
| 98 | return handle.resolve_database_dependency(databases, dep_from_string(specification), [], true) |
| 99 | } |
| 100 | |
| 101 | pub struct DependencyResolution { |
| 102 | pub: |
| 103 | packages []&Package |
| 104 | missing []MissingDependency |
| 105 | success bool |
| 106 | } |
| 107 | |
| 108 | // additions are rolled back per package when its dependencies cannot resolve. |
| 109 | pub fn (mut handle Handle) resolve_dependencies(installed []&Package, pkg &Package, preferred []&Package, packages []&Package, remove []&Package, ignore_versions bool) !DependencyResolution { |
| 110 | handle.require_live()! |
| 111 | mut resolved := packages.clone() |
| 112 | result := handle.resolve_dependencies_into(installed, pkg, preferred, mut resolved, remove, ignore_versions)! |
| 113 | return DependencyResolution{ packages: resolved, missing: result.missing, success: result.success } |
| 114 | } |
| 115 | |
| 116 | // Each branch observes removals after its start callback. Lazy metadata reads |
| 117 | // can call back again, so preserve the original partition before loading them. |
| 118 | fn resolution_missing(installed []&Package, remove []&Package, pkg &Package, assumed []Dependency, ignore_versions bool) []MissingDependency { |
| 119 | if pkg.desc_loaded && pkg.depends.len == 0 { return [] } |
| 120 | if !pkg.desc_loaded || installed.any(!it.desc_loaded) { |
| 121 | return check_dependencies(installed, remove, [pkg], DependencyCheckOptions{ |
| 122 | ignore_versions: ignore_versions |
| 123 | assumed: assumed |
| 124 | }) |
| 125 | } |
| 126 | mut removed := &DependencyIndex(unsafe { nil }) |
| 127 | if remove.len > 0 { removed = dependency_index(remove, [], false) } |
| 128 | mut missing := []MissingDependency{} |
| 129 | dependencies: for original in pkg.depends { |
| 130 | dep := check_dependency_version(original, ignore_versions) |
| 131 | if package_satisfies_dependency(pkg, dep) { continue } |
| 132 | for candidate in installed { |
| 133 | if candidate.name != pkg.name && (removed == unsafe { nil } || !removed.has_name(candidate.name)) |
| 134 | && package_satisfies_dependency(candidate, dep) { |
| 135 | continue dependencies |
| 136 | } |
| 137 | } |
| 138 | if !assumed_satisfies(assumed, dep) { |
| 139 | missing << MissingDependency{ target: pkg.name, dependency: dep } |
| 140 | } |
| 141 | } |
| 142 | return missing |
| 143 | } |
| 144 | |
| 145 | // Each branch appends to one buffer. A failed branch truncates its additions, |
| 146 | // retaining successful siblings without copying the entire closure per edge. |
| 147 | fn (mut handle Handle) resolve_dependencies_into(installed []&Package, pkg &Package, preferred []&Package, mut resolved []&Package, remove []&Package, ignore_versions bool) !DependencyResolution { |
| 148 | handle.require_live()! |
| 149 | if _ := find_package(resolved, pkg.name) { |
| 150 | return DependencyResolution{ success: true } |
| 151 | } |
| 152 | checkpoint := resolved.len |
| 153 | resolved << pkg |
| 154 | handle.log(.debug, 'started resolving dependencies\n') |
| 155 | handle.check()! |
| 156 | mut failures := []MissingDependency{} |
| 157 | missing := resolution_missing(installed, remove, pkg, handle.opts.assumed, ignore_versions) |
| 158 | for miss in missing { |
| 159 | if _ := find_satisfier(resolved, miss.dependency) { continue } |
| 160 | candidate := find_satisfier(preferred, miss.dependency) or { |
| 161 | handle.resolve_database_dependency(handle.sync_dbs, miss.dependency, resolved, false) or { unsafe { nil } } |
| 162 | } |
| 163 | mut candidate_failures := []MissingDependency{} |
| 164 | if candidate != unsafe { nil } { |
| 165 | child := handle.resolve_dependencies_into(installed, candidate, preferred, mut resolved, remove, ignore_versions)! |
| 166 | if child.success { |
| 167 | if handle.has_log_callback() { |
| 168 | handle.log(.debug, 'pulling dependency ${candidate.name} (needed by ${pkg.name})\n') |
| 169 | } |
| 170 | continue |
| 171 | } |
| 172 | candidate_failures = child.missing |
| 173 | } |
| 174 | mut local := handle.local_database()! |
| 175 | if _ := handle.resolve_database_dependency([local], miss.dependency, remove, false) { |
| 176 | continue |
| 177 | } |
| 178 | failures << candidate_failures |
| 179 | failures << miss |
| 180 | handle.fail(.unsatisfied_deps, pkg.name) |
| 181 | if handle.has_log_callback() { |
| 182 | handle.log(.warning, localized_message('cannot resolve "%s", a dependency of "%s"\n', miss.dependency.compute_string(), pkg.name)) |
| 183 | } |
| 184 | } |
| 185 | handle.log(.debug, 'finished resolving dependencies\n') |
| 186 | if failures.len > 0 { |
| 187 | resolved.trim(checkpoint) |
| 188 | return DependencyResolution{ missing: failures } |
| 189 | } |
| 190 | return DependencyResolution{ success: true } |
| 191 | } |
| 192 | |
| 193 | pub fn find_new_version(pkg &Package, databases []&Database) ?&Package { |
| 194 | for pointer in databases { |
| 195 | mut db := unsafe { pointer } |
| 196 | db.ensure_package_cache() or { continue } |
| 197 | candidate := db.by_name[pkg.name] or { continue } |
| 198 | // the first repository carrying the name wins, even if another is newer. |
| 199 | return if pkg_vercmp(candidate.version, pkg.version) > 0 { candidate } else { none } |
| 200 | } |
| 201 | return none |
| 202 | } |
| 203 | |