import alpm fn upgrade(config ConfigUpgrade) ! { // a build installs what it makes: download-only has nothing a pkgbuild // upgrade serves, as install's says if config.pkgbuild && config.download_only { return error('download-only does not build pkgbuild packages') } // the pkgbuild pass asks about every candidate one by one, and a build // runs what a PKGBUILD says: --noconfirm would skip that verification, so // the refusal comes before any of the run's work, as install's does if config.pkgbuild && config.noconfirm && !config.no_pkgbuild_confirm { return error('the pkgbuild upgrades are asked about: drop --noconfirm, or pass --nopkgbuildconfirm') } // a --print preview asks nothing and takes no lock, as pacman's mut ui := new_trans_ui(noconfirm: config.noconfirm || config.print_only) ui.print_only = config.print_only if !config.print_only { check_ask_db_lock(mut ui) } if config.disable_timeout { cfg_disable_timeout = true } mut handle := init_handle(ui)! defer { handle.release() or { went_on('release the alpm handle', err) } } defer { ui.stop() } defer { trans_cleanup(handle) } trans_options(config).apply(handle)! mut s := ui.spinner(text: 'getting dbs and updating em') s.start() defer { stop_spinner(mut s) } init_repos(mut handle)! if config.sync_dbs { update_repos(mut handle, false)! } else { check_sync_fresh(mut ui, mut handle)! } // a fresh one shows in either case: the set_text would land on the old // spinner's frame when the prompt above paused it, and here the old one // is finished with its work anyway stop_spinner(mut s) mut look := ui.spinner(text: 'looking for upgrades') look.start() defer { stop_spinner(mut look) } mut flags := dep_check_flag(config.nodeps, config.nodeps_level) | print_flags(config.print_only) if config.download_only { flags |= .download_only } if config.dbonly { flags |= .db_only } trans_begin(handle, flags)! // pacman's -Suu: a local package newer than the sync one goes down to it handle.sync_sysupgrade(config.downgrade || config.sysupgrade_level >= 2)! trans_prepare_checked(handle)! targets := handle.trans_get_add().pkgs() end_frame(mut ui, mut look) // the sync part's names, the pkgbuild pass's added to them mut upgraded := []string{} if targets.len == 0 { // --pkgbuild may still have work; without it, this is the end if !config.print_only && !config.pkgbuild { report('nothing to upgrade.') return } } else { // --print stops here: the upgrade is listed with the archive names, as // install's --print lists a transaction, and nothing is downloaded if config.print_only { if ipc_on() { mut preview := trans_listing(handle, targets, .upgrade) preview.preview = true emit(preview) return } for pkg in targets { file := pkg.get_filename() println(if file == '' { pkg.get_name() } else { file }) } // --pkgbuild lists what the pass would offer, as install's // --print lists what it would build if config.pkgbuild { for u in pkgbuild_upgrade_candidates(mut ui, pkgbuild_repos(), foreign_pkgs(handle)!)! { report('would upgrade ${u.info.name} from the pkgbuild repo (${u.repo}) (${u.local} -> ${u.info.version})') } } return } // what the upgrade removes on the way — a replaced or conflicting // package — is listed before the question, as install lists its own removals := handle.trans_get_remove().pkgs() if removals.len > 0 { ui.show_packages('${removals.len} packages to remove:', removals) } if !trans_confirm(mut ui, handle, targets, .upgrade) { return } mut s2 := ui.spinner( text: if config.download_only { 'downloading the upgrades' } else { 'upgrading system' } ) s2.start() defer { stop_spinner(mut s2) } trans_commit(handle)! stop_spinner(mut s2) upgraded = targets.map(it.get_name()) } // --pkgbuild: the locally installed packages the repos do not carry are // the pkgbuild repos' to upgrade, asked about one by one. a --print // preview asks nothing and said its list already if config.pkgbuild && !config.print_only { upgraded << upgrade_pkgbuilds(mut ui, mut handle, config)! } if upgraded.len == 0 { return } // a frontend learns what changed as from every transaction: a done event if ipc_on() { emit(DoneEvent{ action: if config.download_only { 'downloaded' } else { 'upgraded' } packages: upgraded }) return } ui.say(if config.download_only { 'upgrades downloaded' } else { 'system upgraded' }) } // foreign_pkgs lists the locally installed packages the sync dbs do not // carry, the held and ignored ones left out: the sync upgrade just did its // part around them, and the pkgbuild repos are what could have the rest fn foreign_pkgs(handle &alpm.Handle) ![]&alpm.Package { sync := handle.get_syncdbs()! dbs := sync.dbs() localdb := handle.get_localdb() or { return error('could not open the local database: ${err.msg()}') } mut out := []&alpm.Package{} cache := pkgcache_of(handle, localdb)! for pkg in cache.pkgs() { if find_repo_pkg(dbs, pkg.get_name()) == none && handle.pkg_should_ignore(pkg) == 0 { out << pkg } } return out } // PkgbuildUpgrade is one locally installed package a pkgbuild repo would // upgrade: the record it would build from, the repo's name, and the version // installed now struct PkgbuildUpgrade { info PackagePkgbuild repo string local string } // foreign_upgrades keeps the foreign packages a record would upgrade: the // record's version must come after the installed one — never a downgrade. // it answers the records, each with the version it would upgrade from fn foreign_upgrades(foreign []&alpm.Package, found map[string]PackagePkgbuild) []PkgbuildUpgrade { mut out := []PkgbuildUpgrade{} for pkg in foreign { rec := found[pkg.get_name()] or { continue } if alpm.pkg_vercmp(pkg.get_version(), rec.version) >= 0 { continue } out << PkgbuildUpgrade{ info: rec local: pkg.get_version() } } return out } // pkgbuild_upgrade_candidates asks the pkgbuild repos after the foreign // names, batched per repo in the config's order — the first repo that holds // a name is the one it would build from, as pkgbuild_lookup tries them one // name at a time — and keeps the records that would upgrade fn pkgbuild_upgrade_candidates(mut ui TransUI, repos []CfgRepo, foreign []&alpm.Package) ![]PkgbuildUpgrade { mut left := foreign.map(it.get_name()) mut found := map[string]PackagePkgbuild{} mut repo_of := map[string]string{} mut s := ui.spinner(text: 'looking for pkgbuild upgrades') s.start() for repo in repos { site := repo.servers[0].trim_right('/') infos := get_pkgbuild_info(site, left) or { stop_spinner(mut s) return err } mut answered := []string{} for info in infos { found[info.name] = info repo_of[info.name] = repo.name answered << info.name } left = left.filter(it !in answered) if left.len == 0 { break } } stop_spinner(mut s) mut ups := []PkgbuildUpgrade{} for u in foreign_upgrades(foreign, found) { ups << PkgbuildUpgrade{ ...u repo: repo_of[u.info.name] } } return ups } // upgrade_pkgbuild_config is the install a pkgbuild upgrade runs as: the // questions are review_and_build's own, the gate worded by the pass with the // versions, and what the upgrade said of its transaction — the ignores, the // overwrites, the dependency checks, the timeout — holds for it fn upgrade_pkgbuild_config(config ConfigUpgrade) ConfigInstall { return ConfigInstall{ pkgbuild: true no_pkgbuild_confirm: config.no_pkgbuild_confirm noconfirm: config.noconfirm nodeps: config.nodeps nodeps_level: config.nodeps_level overwrite: config.overwrite assume_installed: config.assume_installed ignore: config.ignore ignoregroup: config.ignoregroup disable_timeout: config.disable_timeout } } // upgrade_pkgbuilds offers the candidates one by one, each asked the way a // pkgbuild install asks — the upgrade as the gate, then the review, then the // build — and builds and installs the accepted ones. a declined package // stops the pass, as a declined build stops an install. it answers the names // that were upgraded fn upgrade_pkgbuilds(mut ui TransUI, mut handle &alpm.Handle, config ConfigUpgrade) ![]string { repos := pkgbuild_repos() if repos.len == 0 { warn('no pkgbuild repo in the config: the pkgbuild pass has nothing to query') return [] } candidates := pkgbuild_upgrade_candidates(mut ui, repos, foreign_pkgs(handle)!)! if candidates.len == 0 { return [] } mut upgraded := []string{} for u in candidates { gate := 'upgrade ${u.info.name} from the pkgbuild repo (${u.repo}) (${u.local} -> ${u.info.version})?' pkgbuild_install(mut ui, mut handle, upgrade_pkgbuild_config(config), u.info.name, gate) or { return err } // the local db tells a built package from a declined one: a decline // is a stop, the way a declined build stops an install view, installed := local_now()! built := if local := installed.pkg_find(u.info.name) { alpm.pkg_vercmp(local.get_version(), u.info.version) >= 0 } else { false } view.release() or { went_on('release the alpm handle', err) } if !built { advise('${u.info.name} was declined, the pass stops') return upgraded } upgraded << u.info.name } return upgraded }