// repository priority, replacements and reasons follow pacman sync.c. // SPDX-License-Identifier: GPL-2.0-or-later module engine fn replacement_matches(pkg &Package, local &Package) bool { for dep in pkg.replaces { if dep.name() == local.name && version_satisfies(local.version, dep) { return true } } return false } // Repositories usually have many packages and few replacements. Keep the // sparse list in its original order for the duration of a quiet upgrade plan. fn cached_replacers(db &Database, mut caches map[voidptr][]&Package) ?[]&Package { key := voidptr(db) if cached := caches[key] { return cached } mut candidates := []&Package{} for pkg in db.packages { if !pkg.desc_loaded { return none } if pkg.replaces.len > 0 { candidates << pkg } } caches[key] = candidates return candidates } fn (mut handle Handle) replacement_targets(mut trans Transaction, local_pointer &Package, mut db Database, mut caches map[voidptr][]&Package) ![]&Package { // Loading another repository can invoke a signature or retrieval callback // that edits previously visited packages. if caches.len > 0 && (!db.loaded || handle.has_log_callback() || voidptr(handle.dependency_questions.replace_package) != unsafe { nil }) { caches.clear() } db.ensure_package_cache_preserving_error()! mut local := unsafe { local_pointer } if handle.has_log_callback() { handle.log(.debug, 'searching for replacements for ${local.name} in ${db.name}\n') } else if voidptr(handle.dependency_questions.replace_package) == unsafe { nil } { if candidates := cached_replacers(db, mut caches) { if db.packages.len > 0 { handle.check()! } // Even without a question callback, ignore checks may read local // metadata or report its errors. for pointer in candidates { mut pkg := unsafe { pointer } if replacement_matches(pkg, local) && !handle.should_ignore(mut pkg)! { handle.should_ignore(mut local)! } } return [] } } mut result := []&Package{} mut skipped := false for mut pkg in db.packages { if pkg.desc_loaded && pkg.replaces.len == 0 { skipped = true continue } skipped = false handle.check()! dependency_metadata(pkg) if !replacement_matches(pkg, local) { continue } if handle.should_ignore(mut pkg)! || handle.should_ignore(mut local)! { if handle.has_log_callback() { handle.log(.warning, localized_message('ignoring package replacement (%s-%s => %s-%s)\n', local.name, local.version, pkg.name, pkg.version)) } continue } questions := handle.dependency_questions if voidptr(questions.replace_package) == unsafe { nil } || !questions.replace_package(questions.context, local, pkg, db) { continue } if pointer := find_package(trans.add, pkg.name) { mut existing := unsafe { pointer } if voidptr(existing.database) != voidptr(db) { if handle.has_log_callback() { handle.log(.warning, localized_message('cannot replace %s by %s\n', local.name, pkg.name)) } continue } if handle.has_log_callback() { handle.log(.debug, 'appending ${local.name} to the removes list of ${existing.name}\n') } existing.removes << local handle.check()! if local.reason == .explicit { existing.reason = .explicit } } else { handle.check()! pkg.reason = local.reason pkg.removes = [local] if handle.has_log_callback() { handle.log(.debug, 'adding package ${pkg.name}-${pkg.version} to the transaction targets\n') } result << pkg } } // A callback on the last replacement can leave errno set. A subsequent // empty package clears it just as an ordinary metadata visit would. if skipped { handle.check()! } return result } pub fn (mut handle Handle) plan_system_upgrade(enable_downgrade bool) ! { mut trans := handle.current_transaction()! if trans.state != .initialized { return handle.fail(.trans_not_initialized, '') } handle.log(.debug, 'checking for package upgrades\n') mut local := handle.local_database()! installed := local.package_cache()! mut caches := map[voidptr][]&Package{} for pointer in installed { mut old := unsafe { pointer } if _ := find_package(trans.remove, old.name) { if handle.has_log_callback() { handle.log(.debug, '${old.name} is marked for removal -- skipping\n') } continue } if _ := find_package(trans.add, old.name) { if handle.has_log_callback() { handle.log(.debug, '${old.name} is already in the target list -- skipping\n') } continue } for mut db in handle.sync_dbs { if db.usage & 8 == 0 { continue } replacers := handle.replacement_targets(mut trans, old, mut db, mut caches)! if replacers.len > 0 { trans.add << replacers break } mut candidate := db.by_name[old.name] or { continue } comparison := pkg_vercmp(candidate.version, old.version) if comparison > 0 { if handle.has_log_callback() { handle.log(.debug, "new version of '${old.name}' found (${old.version} => ${candidate.version})\n") } if handle.should_ignore(mut candidate)! || handle.should_ignore(mut old)! { if handle.has_log_callback() { handle.log(.warning, localized_message('%s: ignoring package upgrade (%s => %s)\n', old.name, old.version, candidate.version)) } } else { if handle.has_log_callback() { handle.log(.debug, 'adding package ${candidate.name}-${candidate.version} to the transaction targets\n') } trans.add << candidate } } else if comparison < 0 { if enable_downgrade { if handle.should_ignore(mut candidate)! || handle.should_ignore(mut old)! { if handle.has_log_callback() { handle.log(.warning, localized_message('%s: ignoring package downgrade (%s => %s)\n', old.name, old.version, candidate.version)) } } else { if handle.has_log_callback() { handle.log(.warning, localized_message('%s: downgrading from version %s to version %s\n', old.name, old.version, candidate.version)) } trans.add << candidate } } else { handle.check()! if handle.has_log_callback() { handle.log(.warning, localized_message('%s: local (%s) is newer than %s (%s)\n', old.name, old.version, db.name, candidate.version)) } } } break } } }