module main import nspkg // mark sets the install reason of installed packages, pacman's -D --asdeps // and --asexplicit. every target is found first; one that names nothing is // reported and skipped, as pacman's -D and the other modes do, and the rest // are marked. the reasons are then written under the db lock, which pacman's // change_install_reason takes with an empty transaction too: // alpm_pkg_set_reason writes the db entry right away and locks nothing fn mark(config ConfigMark) ! { need_query(config.query, 'package')! mut ui := new_trans_ui(noconfirm: config.noconfirm) check_ask_db_lock(mut ui) mut handle := init_handle(ui)! defer { handle.release() or { went_on('release the nspkg handle', err) } } defer { ui.stop() } defer { trans_cleanup(handle) } mut pkgcache := local_pkgcache(handle)! reason := parse_reason(config.reason)! mut pkgs := []&nspkg.Package{} for target in config.query { mut name := local_target_name(target) pkg := find_local_target(pkgcache, name, false) or { name = did_you_mean(mut ui, name, pkg_names(pkgcache.pkgs())) or { target_error(err.msg()) continue } pkgcache.pkg_find(name) or { target_error('unknown package: "${name}"') continue } } add_pkg_once(mut pkgs, pkg) } if pkgs.len == 0 { return } trans_begin(handle, nspkg.trans_flag_0)! // each reason is written to the db as it is set, so a failure leaves the // ones before it changed: the error names them, so the run is truthful // about what it did and did not do mut set := []string{} for pkg in pkgs { if pkg.get_reason() != reason { pkg.set_reason(reason) or { already := if set.len > 0 { ' (already set: ${set.join(', ')})' } else { '' } return error('could not set the install reason of ${pkg.get_name()}: ${err.msg()}${already}') } set << pkg.get_name() } } names := pkgs.map(it.get_name()) if ipc_on() { emit(DoneEvent{ action: 'marked' reason: reason_key(reason) packages: names }) return } print_done(mut ui, '${reason_to_str(reason)}:', names.map(name_style(it))) } // reason_key is an install reason as a frontend reads it, and the word // mark takes for it (parse_reason); every reason named, as reason_to_str fn reason_key(reason nspkg.PackageReason) string { return match reason { .explicit { 'explicit' } .depend { 'depend' } .unknown { 'unknown' } } } // parse_reason reads mark's --reason: a reason's key, or a short form of it fn parse_reason(text string) !nspkg.PackageReason { return match text { 'e', 'ex', 'explicit' { nspkg.PackageReason.explicit } 'd', 'dep', 'depend', 'dependency' { nspkg.PackageReason.depend } 'u', 'un', 'unknown' { nspkg.PackageReason.unknown } else { error('unknown reason: "${text}"') } } }