module main import alpm // InfoLookup is how info and tree find their targets: the installed // packages first, then the sync dbs, which are only set up — and asked // about their freshness — by the first target that needs them struct InfoLookup { mut: installed &alpm.List dbs []&alpm.DB synced bool } // sync_dbs registers the sync dbs once for all targets fn (mut l InfoLookup) sync_dbs(mut ui TransUI, mut handle &alpm.Handle) ![]&alpm.DB { if !l.synced { l.dbs = init_repos(mut handle)! check_sync_fresh(mut ui, mut handle)! l.synced = true } return l.dbs } // find resolves one target: 'local/name' in the installed packages alone, // 'repo/name' in that sync db alone, a bare name in the installed packages // and then every sync db, like pacman's -Qi with its -Si behind it. a name // may be a dependency string or something provided. what misses gets the // nearest name of the same scope offered; an error is the target's own, // for the caller to report and go on from fn (mut l InfoLookup) find(mut ui TransUI, mut handle &alpm.Handle, raw string) !&alpm.Package { target := parse_target(raw) if target.repo in ['', 'local'] { if p := find_local_target(l.installed, target.name, true) { return p } } mut scoped := []&alpm.DB{} if target.repo != 'local' { scoped = scope_dbs(l.sync_dbs(mut ui, mut handle)!, target.repo)! if p := find_sync_target(handle, scoped, target.name, target.repo != '') { return p } } mut names := []string{} if target.repo in ['', 'local'] { names << pkg_names(l.installed.pkgs()) } names << sync_names(scoped) fixed := did_you_mean(mut ui, target.name, names)! if target.repo in ['', 'local'] { if p := l.installed.pkg_find(fixed) { return p } } return find_repo_pkg(scoped, fixed) or { error('unknown package: "${fixed}"') } } fn info(config ConfigInfo) ! { need_query(config.query, 'package')! fields := parse_fields(config.fields, pkg_field_names)! mut ui := new_trans_ui(noconfirm: config.noconfirm) mut handle := init_handle(ui)! defer { handle.release() or { went_on('release the alpm handle', err) } } defer { ui.stop() } // package archives on disk are shown directly, pacman's -Qp: loaded, not // installed. -p says every target is one; without it a single target // that is a file on disk is still taken for the archive it is if config.file || (config.query.len == 1 && is_local_pkg_file(config.query[0])) { info_files(handle, config, fields) return } mut lookup := InfoLookup{ installed: local_pkgcache(handle)! } mut printed := 0 for target in config.query { pkg := lookup.find(mut ui, mut handle, target) or { target_error(err.msg()) continue } if show_pkg(pkg, config, fields, printed == 0) { printed++ } } } // show_pkg is one target's answer: its changelog with -c, its fields // otherwise. it says whether anything was shown, which is what the blank // line before the next one goes by fn show_pkg(pkg &alpm.Package, config ConfigInfo, fields []string, first bool) bool { if config.changelog { return show_changelog(pkg, first) } show_info(info_fields(pkg, fields, config.fields != ''), first) return true } // show_info shows one package's fields: a table on a terminal, after a blank // line unless it is the first; an info event for a frontend fn show_info(fields []InfoField, first bool) { if ipc_on() { emit(InfoEvent{ fields: fields }) return } if !first { println('') } println(info_table(fields)) } // pkg_changelog reads a package's changelog whole, in chunks as libalpm // hands it out (an fread into the buffer, 0 at its end). none when the // package carries none: libalpm has one for an installed package and for a // package file, never for a sync db's entry fn pkg_changelog(pkg &alpm.Package) ?string { fp := pkg.changelog_open() if fp == unsafe { nil } { return none } mut text := []u8{} mut buf := []u8{len: 4096} for { n := int(pkg.changelog_read(buf.data, u32(buf.len), fp)) if n <= 0 { break } text << buf[..n] } pkg.changelog_close(fp) or { warn('could not close the changelog of ${pkg.get_name()}: ${err.msg()}') } return text.bytestr() } // show_changelog is pacman's -Qc: the heading, then the changelog as the // package carries it. a package without one is said and shows nothing, // which pacman does not count as a failure either fn show_changelog(pkg &alpm.Package, first bool) bool { text := pkg_changelog(pkg) or { warn('no changelog available for ${pkg.get_name()}') return false } if ipc_on() { emit(ChangelogEvent{ package: pkg.get_name() text: text }) return true } if !first { println('') } println(heading('changelog for ${pkg.get_name()}:')) println(text.trim_right('\n')) return true } // info_files shows package archives from disk, pacman's -Qp, each loaded // the way pacman's query loads one: whole, with no signature asked for, // since nothing of it is installed. one that does not load is its own // target's error, and the rest are still shown fn info_files(handle &alpm.Handle, config ConfigInfo, fields []string) { mut printed := 0 for path in config.query { pkg := handle.pkg_load(path, 1, 0) or { target_error('could not load ${path}: ${err.msg()}') continue } if show_pkg(pkg, config, fields, printed == 0) { printed++ } pkg.free_pkg() or { warn('could not free the package of ${path}: ${err.msg()}') } } } // which finds the installed package that provides a dependency fn which(config ConfigWhich) ! { need_query(config.query, 'dependency')! ui := new_trans_ui() mut handle := init_handle(ui)! defer { handle.release() or { went_on('release the alpm handle', err) } } defer { ui.stop() } mut pkgcache := local_pkgcache(handle)! mut printed := 0 for target in config.query { pkg := pkgcache.find_satisfier(local_target_name(target)) or { target_error('unknown package: "${target}"') continue } show_info(info_fields(pkg, pkg_field_names, false), printed == 0) printed++ } }