| 1 | // repository priority, replacements and reasons follow pacman sync.c. |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | module engine |
| 4 | |
| 5 | fn replacement_matches(pkg &Package, local &Package) bool { |
| 6 | for dep in pkg.replaces { |
| 7 | if dep.name() == local.name && version_satisfies(local.version, dep) { return true } |
| 8 | } |
| 9 | return false |
| 10 | } |
| 11 | |
| 12 | // Repositories usually have many packages and few replacements. Keep the |
| 13 | // sparse list in its original order for the duration of a quiet upgrade plan. |
| 14 | fn cached_replacers(db &Database, mut caches map[voidptr][]&Package) ?[]&Package { |
| 15 | key := voidptr(db) |
| 16 | if cached := caches[key] { return cached } |
| 17 | mut candidates := []&Package{} |
| 18 | for pkg in db.packages { |
| 19 | if !pkg.desc_loaded { return none } |
| 20 | if pkg.replaces.len > 0 { candidates << pkg } |
| 21 | } |
| 22 | caches[key] = candidates |
| 23 | return candidates |
| 24 | } |
| 25 | |
| 26 | fn (mut handle Handle) replacement_targets(mut trans Transaction, local_pointer &Package, mut db Database, mut caches map[voidptr][]&Package) ![]&Package { |
| 27 | // Loading another repository can invoke a signature or retrieval callback |
| 28 | // that edits previously visited packages. |
| 29 | if caches.len > 0 && (!db.loaded || handle.has_log_callback() |
| 30 | || voidptr(handle.dependency_questions.replace_package) != unsafe { nil }) { |
| 31 | caches.clear() |
| 32 | } |
| 33 | db.ensure_package_cache_preserving_error()! |
| 34 | mut local := unsafe { local_pointer } |
| 35 | if handle.has_log_callback() { |
| 36 | handle.log(.debug, 'searching for replacements for ${local.name} in ${db.name}\n') |
| 37 | } else if voidptr(handle.dependency_questions.replace_package) == unsafe { nil } { |
| 38 | if candidates := cached_replacers(db, mut caches) { |
| 39 | if db.packages.len > 0 { handle.check()! } |
| 40 | // Even without a question callback, ignore checks may read local |
| 41 | // metadata or report its errors. |
| 42 | for pointer in candidates { |
| 43 | mut pkg := unsafe { pointer } |
| 44 | if replacement_matches(pkg, local) && !handle.should_ignore(mut pkg)! { |
| 45 | handle.should_ignore(mut local)! |
| 46 | } |
| 47 | } |
| 48 | return [] |
| 49 | } |
| 50 | } |
| 51 | mut result := []&Package{} |
| 52 | mut skipped := false |
| 53 | for mut pkg in db.packages { |
| 54 | if pkg.desc_loaded && pkg.replaces.len == 0 { |
| 55 | skipped = true |
| 56 | continue |
| 57 | } |
| 58 | skipped = false |
| 59 | handle.check()! |
| 60 | dependency_metadata(pkg) |
| 61 | if !replacement_matches(pkg, local) { |
| 62 | continue |
| 63 | } |
| 64 | if handle.should_ignore(mut pkg)! || handle.should_ignore(mut local)! { |
| 65 | if handle.has_log_callback() { |
| 66 | handle.log(.warning, localized_message('ignoring package replacement (%s-%s => %s-%s)\n', local.name, local.version, pkg.name, pkg.version)) |
| 67 | } |
| 68 | continue |
| 69 | } |
| 70 | questions := handle.dependency_questions |
| 71 | if voidptr(questions.replace_package) == unsafe { nil } |
| 72 | || !questions.replace_package(questions.context, local, pkg, db) { |
| 73 | continue |
| 74 | } |
| 75 | if pointer := find_package(trans.add, pkg.name) { |
| 76 | mut existing := unsafe { pointer } |
| 77 | if voidptr(existing.database) != voidptr(db) { |
| 78 | if handle.has_log_callback() { |
| 79 | handle.log(.warning, localized_message('cannot replace %s by %s\n', local.name, pkg.name)) |
| 80 | } |
| 81 | continue |
| 82 | } |
| 83 | if handle.has_log_callback() { |
| 84 | handle.log(.debug, 'appending ${local.name} to the removes list of ${existing.name}\n') |
| 85 | } |
| 86 | existing.removes << local |
| 87 | handle.check()! |
| 88 | if local.reason == .explicit { existing.reason = .explicit } |
| 89 | } else { |
| 90 | handle.check()! |
| 91 | pkg.reason = local.reason |
| 92 | pkg.removes = [local] |
| 93 | if handle.has_log_callback() { |
| 94 | handle.log(.debug, 'adding package ${pkg.name}-${pkg.version} to the transaction targets\n') |
| 95 | } |
| 96 | result << pkg |
| 97 | } |
| 98 | } |
| 99 | // A callback on the last replacement can leave errno set. A subsequent |
| 100 | // empty package clears it just as an ordinary metadata visit would. |
| 101 | if skipped { handle.check()! } |
| 102 | return result |
| 103 | } |
| 104 | |
| 105 | pub fn (mut handle Handle) plan_system_upgrade(enable_downgrade bool) ! { |
| 106 | mut trans := handle.current_transaction()! |
| 107 | if trans.state != .initialized { return handle.fail(.trans_not_initialized, '') } |
| 108 | handle.log(.debug, 'checking for package upgrades\n') |
| 109 | mut local := handle.local_database()! |
| 110 | installed := local.package_cache()! |
| 111 | mut caches := map[voidptr][]&Package{} |
| 112 | for pointer in installed { |
| 113 | mut old := unsafe { pointer } |
| 114 | if _ := find_package(trans.remove, old.name) { |
| 115 | if handle.has_log_callback() { |
| 116 | handle.log(.debug, '${old.name} is marked for removal -- skipping\n') |
| 117 | } |
| 118 | continue |
| 119 | } |
| 120 | if _ := find_package(trans.add, old.name) { |
| 121 | if handle.has_log_callback() { |
| 122 | handle.log(.debug, '${old.name} is already in the target list -- skipping\n') |
| 123 | } |
| 124 | continue |
| 125 | } |
| 126 | for mut db in handle.sync_dbs { |
| 127 | if db.usage & 8 == 0 { continue } |
| 128 | replacers := handle.replacement_targets(mut trans, old, mut db, mut caches)! |
| 129 | if replacers.len > 0 { |
| 130 | trans.add << replacers |
| 131 | break |
| 132 | } |
| 133 | mut candidate := db.by_name[old.name] or { continue } |
| 134 | comparison := pkg_vercmp(candidate.version, old.version) |
| 135 | if comparison > 0 { |
| 136 | if handle.has_log_callback() { |
| 137 | handle.log(.debug, "new version of '${old.name}' found (${old.version} => ${candidate.version})\n") |
| 138 | } |
| 139 | if handle.should_ignore(mut candidate)! || handle.should_ignore(mut old)! { |
| 140 | if handle.has_log_callback() { |
| 141 | handle.log(.warning, localized_message('%s: ignoring package upgrade (%s => %s)\n', old.name, old.version, candidate.version)) |
| 142 | } |
| 143 | } else { |
| 144 | if handle.has_log_callback() { |
| 145 | handle.log(.debug, 'adding package ${candidate.name}-${candidate.version} to the transaction targets\n') |
| 146 | } |
| 147 | trans.add << candidate |
| 148 | } |
| 149 | } else if comparison < 0 { |
| 150 | if enable_downgrade { |
| 151 | if handle.should_ignore(mut candidate)! || handle.should_ignore(mut old)! { |
| 152 | if handle.has_log_callback() { |
| 153 | handle.log(.warning, localized_message('%s: ignoring package downgrade (%s => %s)\n', old.name, old.version, candidate.version)) |
| 154 | } |
| 155 | } else { |
| 156 | if handle.has_log_callback() { |
| 157 | handle.log(.warning, localized_message('%s: downgrading from version %s to version %s\n', old.name, old.version, candidate.version)) |
| 158 | } |
| 159 | trans.add << candidate |
| 160 | } |
| 161 | } else { |
| 162 | handle.check()! |
| 163 | if handle.has_log_callback() { |
| 164 | handle.log(.warning, localized_message('%s: local (%s) is newer than %s (%s)\n', old.name, old.version, db.name, candidate.version)) |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | break |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |