module main import alpm // local_group_pkgs are the installed members of a group, pacman -R's // fallback for a target that is no package: the local db's own group fn local_group_pkgs(handle &alpm.Handle, name string) []&alpm.Package { localdb := handle.get_localdb() or { return []&alpm.Package{} } group := localdb.get_group(name) or { return []&alpm.Package{} } return group.packages.pkgs() } // recurse_flags is pacman's -s: once takes the dependencies the removal // leaves unneeded, twice (-ss, or --recursive with -s) the ones that were // installed explicitly too fn recurse_flags(recursive bool, level int) alpm.TransFlag { n := level + if recursive { 1 } else { 0 } return if n >= 2 { alpm.TransFlag.recurse | .recurse_all } else if n == 1 { alpm.TransFlag.recurse } else { alpm.trans_flag_0 } } // remove removes the targets in one transaction, confirmed once fn remove(config ConfigRemove) ! { need_query(config.query, 'package')! // 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) } mut handle := init_handle(ui)! defer { handle.release() or { went_on('release the alpm handle', err) } } defer { ui.stop() } defer { trans_cleanup(handle) } TransOptions{ assume_installed: config.assume_installed }.apply(handle)! mut s := ui.spinner(text: 'preparing removal') s.start() defer { stop_spinner(mut s) } mut pkgcache := local_pkgcache(handle)! // every target is found before the transaction starts, so an unknown // name removes nothing: each bad one is reported and the rest still // looked up, and then the run stops, as pacman's -R does. a target is a // package, what satisfies its version constraint, or a group of the // local db mut pkgs := []&alpm.Package{} mut missed := 0 for target in config.query { name := local_target_name(target) if pkg := find_local_target(pkgcache, name, false) { add_pkg_once(mut pkgs, pkg) continue } members := local_group_pkgs(handle, name) if members.len > 0 { for pkg in members { add_pkg_once(mut pkgs, pkg) } continue } stop_spinner(mut s) fixed := did_you_mean(mut ui, name, pkg_names(pkgcache.pkgs())) or { target_error(err.msg()) missed++ continue } pkg := pkgcache.pkg_find(fixed) or { target_error('unknown package: "${fixed}"') missed++ continue } add_pkg_once(mut pkgs, pkg) } stop_spinner(mut s) if missed > 0 { return } // the config can hold a package: refuse before anything is staged check_hold(pkgs.map(it.get_name()))! // recursive takes the dependencies that the removal leaves unneeded too, as // pacman's -Rs does. they are listed with the packages and confirmed once mut flags := dep_check_flag(config.nodeps, config.nodeps_level) | print_flags(config.print_only) | recurse_flags(config.recursive, config.recursive_level) // pacman's -Ru: a target something still requires is left out by // libalpm instead of failing the removal if config.unneeded { flags |= .unneeded } if config.cascade { flags |= .cascade } if config.nosave { flags |= .no_save } if config.dbonly { flags |= .db_only } trans_begin(handle, flags)! for pkg in pkgs { handle.remove_pkg(pkg)! } trans_prepare_checked(handle)! victims := handle.trans_get_remove().pkgs() if victims.len == 0 { // -u left every target where it is needed: pacman's answer, and no // failure if config.unneeded { end_frame(mut ui, mut s) if !config.print_only { report('there is nothing to do') } return } return error('nothing to do') } // --print stops here: what the removal takes is listed the way pacman's // -Rp lists it, name-version, and nothing is removed if config.print_only { end_frame(mut ui, mut s) if ipc_on() { mut preview := trans_listing(handle, victims, .remove) preview.preview = true emit(preview) return } for pkg in victims { println('${pkg.get_name()}-${pkg.get_version()}') } return } // the transaction is the truth of what goes: recursive and cascade pull // packages in beyond the targets, and the hold list counts for them too check_hold(victims.map(it.get_name()))! if !trans_confirm(mut ui, handle, victims, .remove) { return } // the names are read before the commit, which frees the removed packages // with -u a target may have stayed: the ones that go are the ones named going := victims.map(it.get_name()) mut done := []string{} for pkg in pkgs { if pkg.get_name() in going { done << name_style(pkg.get_name()) } } mut s2 := ui.spinner(text: 'removing packages') s2.start() defer { stop_spinner(mut s2) } trans_commit(handle)! stop_spinner(mut s2) print_done(mut ui, 'removed:', done) }