module main import nspkg import os // a target is a local package file if a file with that name exists, // like pacman's -U handling fn is_local_pkg_file(path string) bool { return os.exists(path) && os.is_file(path) } // FileTarget is a package archive an install loads from disk: one named on // the command line, or one a url fetched into the cache struct FileTarget { path string fetched bool } // load_file_target loads an archive at the signature level pacman's -U // gives it: the local file level for a file named, the remote file level // for one fetched. the load is the only check a file package gets — // libalpm's integrity pass at commit skips one loaded from a file as valid // already — so a level of 0 here would let a bad signature beside the // archive install unread fn load_file_target(handle &nspkg.Handle, t FileTarget) !&nspkg.Package { origin := if t.fetched { 'remote' } else { 'local' } got := if t.fetched { handle.option_get_remote_file_siglevel() } else { handle.option_get_local_file_siglevel() } level := got or { return error('could not read the ${origin} file signature level') } return handle.pkg_load(t.path, 1, int(level)) or { return error('failed to load ${t.path}: ${err.msg()}') } } // find_repo_pkg looks a name up in the sync dbs, in order fn find_repo_pkg(dbs []&nspkg.DB, name string) ?&nspkg.Package { for db in dbs { if p := db.get_pkg(name) { return p } } return none } // add_pkg_once adds a package unless one with its name is already in the list fn add_pkg_once(mut pkgs []&nspkg.Package, pkg &nspkg.Package) { if pkgs.any(it.get_name() == pkg.get_name()) { return } pkgs << pkg } // expand_group adds the member packages of a sync group to the transaction: // members already installed at the sync version are skipped, and of the rest // the user picks, as pacman's -S asks about a group (ask is false for a // --print preview, and once another target failed: nothing installs then, // so there is nothing to choose). it answers whether the name was a group at // all, and how many members went in. a group with nothing left to install // is said, not passed over in silence: it is still a target that resolved fn expand_group(mut pkgs []&nspkg.Package, mut ui TransUI, handle &nspkg.Handle, dbs []&nspkg.DB, name string, ask bool) (bool, int) { found, members := sync_group_members(dbs, name) if !found { return false, 0 } mut news := []GroupMember{} for m in members { if local := handle.find_local_pkg(m.pkg.get_name()) { if nspkg.pkg_vercmp(local.get_version(), m.pkg.get_version()) >= 0 { continue } } news << m } if news.len == 0 { // --print's output is the list alone if !ui.print_only { ui.pause() report('group ${name}: every member is installed and up to date') } return true, 0 } chosen := select_members(mut ui, name, news, ask) for m in chosen { add_pkg_once(mut pkgs, m.pkg) } return true, chosen.len } // skip_up_to_date is --needed: a target installed at the version it would // install is left out, with pacman's warning. libalpm's needed flag does the // same inside add_pkg, warning through its log; here the targets are left // out before they reach the transaction, so its listing never shows them fn skip_up_to_date(handle &nspkg.Handle, pkgs []&nspkg.Package) []&nspkg.Package { mut out := []&nspkg.Package{} for pkg in pkgs { if local := handle.find_local_pkg(pkg.get_name()) { if nspkg.pkg_vercmp(local.get_version(), pkg.get_version()) == 0 { warn('${pkg.get_name()}-${pkg.get_version()} is up to date, skipped') continue } } out << pkg } return out } // walk_upgrade_deps adds the outdated dependencies of the targets and // everything that hangs off them to the transaction, with libalpm's provider // questions answered silently on the way: these walks look the dependencies // up only to decide what to upgrade with, and the provider question that // counts comes again in the prepare. the walking window is this function's // whole body, so its defer is exactly as wide as the walks fn walk_upgrade_deps(mut ui TransUI, handle &nspkg.Handle, mut pkgs []&nspkg.Package, mut added map[string]bool) ! { ui.walking = true defer { ui.walking = false } sync_dbs := handle.get_syncdbs()! mut upgrades := []&nspkg.Package{} upgrades << pkgs for pkg in pkgs { for dep in handle.outdated_deps(sync_dbs, pkg) { upgrades << dep } } for p in upgrades { if p.get_name() !in added { handle.add_pkg(p)! added[p.get_name()] = true } } // with nothing installed nothing can break, and the binding's // broken_dependents takes libalpm's NULL for an empty local db as an // error: it is asked only when some package is installed if local_pkgcache(handle)! == unsafe { nil } { return } for extra in handle.broken_dependents(upgrades, sync_dbs)! { if extra.get_name() !in added { handle.add_pkg(extra)! added[extra.get_name()] = true } } } // install_reason is the reason --asdeps or --asexplicit asks for, if any fn install_reason(config ConfigInstall) ?nspkg.PackageReason { if config.asdeps { return .depend } if config.asexplicit { return .explicit } return none } // mark_installed gives the installed targets the reason --asdeps or // --asexplicit asks for. libalpm's alldeps and allexplicit flags would reach // every package of the transaction, the outdated dependencies vpac upgrades // along with the targets too, so the reason is set on the targets alone once // they are in, under the db lock as pacman's -D does it fn mark_installed(handle &nspkg.Handle, names []string, reason nspkg.PackageReason) ! { trans_begin(handle, nspkg.trans_flag_0)! defer { trans_cleanup(handle) } for name in names { pkg := handle.find_local_pkg(name) or { continue } if pkg.get_reason() != reason { pkg.set_reason(reason)! } } } // install_pkgs prepares one transaction for all the packages. with the default // it also adds the outdated dependencies of each one, then lists everything the // transaction touches and asks before committing. it returns false when the user // declines fn install_pkgs(handle &nspkg.Handle, mut ui TransUI, mut pkgs []&nspkg.Package, config ConfigInstall) !bool { mut s := ui.spinner(text: 'preparing transaction') s.start() defer { stop_spinner(mut s) } 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)! mut added := map[string]bool{} for pkg in pkgs { handle.add_pkg(pkg)! added[pkg.get_name()] = true } if !config.no_upgrade_deps { walk_upgrade_deps(mut ui, handle, mut pkgs, mut added)! } trans_prepare_checked(handle) or { return error(prepare_error(handle, err.msg(), config.no_upgrade_deps)) } // every result print below needs the render thread gone end_frame(mut ui, mut s) // --print stops here: the transaction is listed with the archive names, // and nothing is downloaded or installed if config.print_only { if ipc_on() { mut preview := trans_listing(handle, handle.trans_get_add().pkgs(), .install) preview.preview = true emit(preview) return false } for pkg in handle.trans_get_add().pkgs() { file := pkg.get_filename() println(if file == '' { pkg.get_name() } else { file }) } return false } // every transaction is listed and confirmed before it may go: the default // pulls in upgrades beyond the targets, --no-upgrade-deps still installs // the ones it names, and pacman's -S asks in both cases. packages the // transaction removes, such as a conflicting one, are listed too 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, handle.trans_get_add().pkgs(), .install) { return false } mut commit := ui.spinner( text: if config.download_only { 'downloading packages' } else { 'installing packages' } ) commit.start() defer { stop_spinner(mut commit) } trans_commit(handle)! stop_spinner(mut commit) return true } // install installs the targets. URLs are downloaded first. local files and repo // packages go into one transaction, confirmed once. names in neither the repos // nor the pkgbuild repo are offered the closest repo name. pkgbuild packages are built one at a // time, after that transaction. a target nothing resolves is reported and // the rest are still looked up, so every bad one is named in one run; then // nothing installs, as pacman's -S stops after its target errors fn install(config ConfigInstall) ! { need_query(config.query, 'package')! if config.asdeps && config.asexplicit { return error('--asdeps and --asexplicit contradict each other: pick one') } // a --print preview asks nothing and takes no lock, as pacman's mut ui := new_trans_ui(noconfirm: config.noconfirm || config.print_only) if !config.print_only { check_ask_db_lock(mut ui) } // the timeout option is read when the handle is made, so it is set first if config.disable_timeout { cfg_disable_timeout = true } ui.print_only = config.print_only ui.download_only = config.download_only // one handle for the whole install: a url target is fetched through it, // drawing on the same display and asking through the same prompter as // the transaction's own downloads mut handle := init_handle(ui)! defer { handle.release() or { went_on('release the nspkg handle', err) } } defer { ui.stop() } defer { trans_cleanup(handle) } trans_options(config).apply(handle)! mut file_targets := []FileTarget{} mut names := []string{} for target in config.query { if is_url(target) { if config.print_only { // a url target is printed as it is; nothing is fetched. the // same message event a build plan goes out as carries this // line on both a terminal and a frontend report('would fetch ${target}') continue } file_targets << FileTarget{ path: fetch_target_url(mut ui, handle, target)! fetched: true } } else if is_local_pkg_file(target) { file_targets << FileTarget{ path: target } } else { names << target } } mut s := ui.spinner(text: 'getting dbs and updating em') s.start() defer { stop_spinner(mut s) } dbs := init_repos(mut handle)! if names.len > 0 { check_sync_fresh(mut ui, mut handle)! } mut pkgs := []&nspkg.Package{} for f in file_targets { add_pkg_once(mut pkgs, load_file_target(handle, f)!) } mut pkgbuild := []string{} mut missed := 0 mut groups := map[string]bool{} for name in names { if config.pkgbuild { pkgbuild << name continue } // repo/name looks in that repo alone, for the package and the group target := parse_target(name) scoped := scope_dbs(dbs, target.repo) or { target_error(err.msg()) missed++ continue } if p := find_sync_target(handle, scoped, target.name, target.repo != '') { add_pkg_once(mut pkgs, p) continue } // a group named twice is asked about once if groups[name] { continue } // a group installs the members picked of the ones that are not // installed at the sync version already found, _ := expand_group(mut pkgs, mut ui, handle, scoped, target.name, !config.print_only && missed == 0) if found { groups[name] = true continue } // a repo named is the whole search: the pkgbuild repos are no // fallback for it if target.repo != '' { target_error('target not found in ${target.repo}: ${target.name}') missed++ continue } pkgbuild << name } stop_spinner(mut s) if config.needed { pkgs = skip_up_to_date(handle, pkgs) } mut pkgbuild_builds := []string{} if config.pkgbuild && pkgbuild_repos().len == 0 { return error('no pkgbuild repo in the config: add (repo #:name "aur" #:type "pkgbuild" #:src "https://aur.archlinux.org")') } for name in pkgbuild { if config.pkgbuild { pkgbuild_builds << name continue } // nothing in the repos: the pkgbuild repos are next, and they review // before they build in_the_repos := pkgbuild_has(name) or { return err } if in_the_repos { pkgbuild_builds << name continue } // the pkgbuild db is not the only source: a typo of a package that is // only installed gets its proposal too fixed := did_you_mean(mut ui, name, all_names(handle, dbs)) or { target_error('"${name}" is not in the repos or the pkgbuild repo') missed++ continue } // the accepted suggestion goes the same way the typo would have: a // pkgbuild package from its repo, a repo package into the // transaction, and a name nothing carries is the miss it is fixed_found := pkgbuild_has(fixed) or { return err } if fixed_found { pkgbuild_builds << fixed continue } if p := find_repo_pkg(dbs, fixed) { add_pkg_once(mut pkgs, p) } else { target_error('"${fixed}" is not in the repos or the pkgbuild repo') missed++ } } // the bad targets are all named by now; pacman installs nothing then if missed > 0 { return } if config.download_only && pkgbuild_builds.len > 0 { return error('download-only does not build pkgbuild packages') } if pkgs.len == 0 && pkgbuild_builds.len == 0 { // --print's output is the list alone if !config.print_only { report('there is nothing to do') } return } if pkgs.len > 0 { // the names are read before the commit, which may free the packages targets := pkgs.map(it.get_name()) done := targets.map(name_style(it)) committed := install_pkgs(handle, mut ui, mut pkgs, config)! // not the defer's work done twice: the pkgbuild installs below begin // transactions of their own on this handle, so this one is closed // here, and the defer has nothing left to close when it runs trans_cleanup(handle) if !committed { // --print listed the repo part; the build plan is still listed if config.print_only { for name in pkgbuild_builds { report('would build ${name}') } } return } if !config.download_only { if reason := install_reason(config) { mark_installed(handle, targets, reason)! } } print_done(mut ui, if config.download_only { 'downloaded:' } else { 'installed:' }, done) } // --print lists the build plan and stops if config.print_only { for name in pkgbuild_builds { report('would build ${name}') } return } for target in pkgbuild_builds { pkgbuild_install(mut ui, mut handle, config, target, '')! } }