module main import alpm import os fn files(config ConfigFiles) ! { need_query(config.query, 'package')! // no display: the ui is there for the did-you-mean question mut ui := new_trans_ui(noconfirm: config.noconfirm) defer { ui.stop() } mut handle := alpm.initialize(root_path, db_path) or { return err } defer { handle.release() or { went_on('release the alpm handle', err) } } mut pkgcache := local_pkgcache(handle)! for target in config.query { mut name := local_target_name(target) pkg := find_local_target(pkgcache, name, true) 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 } } paths := pkg_paths(pkg) if ipc_on() { emit(FilesEvent{ package: pkg.get_name() paths: paths }) continue } println(table_text(paths.map(row(name_style(pkg.get_name()), it)), '', ' ')) } } // pkg_paths are the files of an installed package, as paths under the root fn pkg_paths(pkg &alpm.Package) []string { return pkg.get_files().names().map('${root_path}${it}') } // OwnerQuery is one target of owner mode, the way pacman's query_fileowner // shapes it: rel is what the filelists are searched for, relative to the // root — a directory spelled with its trailing slash, as filelists spell // one — and dir says a directory is asked for struct OwnerQuery { rel string dir bool } // file_owners are the installed packages whose filelists hold q.rel, an // exact match as alpm_filelist_contains makes it. a file has one owner and // the walk stops there; a directory is shared, and every owner answers, as // in pacman fn file_owners(pkgs []&alpm.Package, q OwnerQuery) []&alpm.Package { mut out := []&alpm.Package{} for pkg in pkgs { if pkg.get_files().contains(q.rel) != none { out << pkg if !q.dir { break } } } return out } // owner_query makes a target into the path the filelists are searched for, // step for step as pacman's query_fileowner does: trailing slashes go, a // missing bare name is looked up in PATH, the directory the name sits in is // canonicalized while the name itself is not (a symlink is owned as the // link, the way a filelist lists it), and a directory takes its slash back fn owner_query(target string) !OwnerQuery { if target == '' { return error('empty path') } // a trailing slash would make lstat follow a directory symlink; it still // asks for a directory, unless what is there says otherwise. / stays mut p := target mut dir := false for p.len > 1 && p.ends_with('/') { p = p[..p.len - 1] dir = true } mut missing := false mut kind := os.FileType.unknown if st := os.lstat(p) { kind = st.get_filetype() } else { missing = true // not a path but a program name: the first PATH entry that holds it if !p.contains('/') { if found := search_path(p) { p = found missing = false kind = (os.lstat(p) or { os.Stat{} }).get_filetype() } } } // a directory that cannot be resolved (it does not exist) leaves the // path as named. pacman proceeds with it verbatim, which drops a relative // one as outside the root; it is made absolute here instead, so a file a // package lists under a since deleted directory still finds its owner mut path := lrealpath(p) or { os.abs_path(p) } // an existing path is a directory by what it is, a missing one by the // slash it was given dir = if missing { dir } else { kind == .directory } if dir && !path.ends_with('/') { path += '/' } // the root itself is the empty relative path: no package lists it, so it // gets the plain no-owner answer, not the outside-root one. the real // filespace '/' and a fake root '/x/' both end in their slash, which a // missing root (no directory to say so) can lack if path == root_path || path + '/' == root_path { return OwnerQuery{ dir: true } } if !path.starts_with(root_path) { return error('file is outside of root (${root_path}): ${path}') } return OwnerQuery{ rel: path[root_path.len..] dir: dir } } // search_path is pacman's own PATH search for a bare name: the first entry // where the name exists at all, as lstat sees it — a link or a file that is // not executable counts, as in pacman. an empty entry would be the working // directory, which the caller has looked in already fn search_path(name string) ?string { for entry in os.getenv('PATH').split(':') { if entry == '' { continue } candidate := '${entry.trim_right('/')}/${name}' if _ := os.lstat(candidate) { return candidate } } return none } // lrealpath is pacman's: the directory a path names its file in is resolved // (links, '.' and '..'), and the file itself is kept as named, so a link // stays the link. a path ending in '.' or '..' names a directory, which is // resolved whole. none when the directory does not resolve fn lrealpath(path string) ?string { base := path.all_after_last('/') if base in ['.', '..'] { return realpath(path) } slash := path.last_index('/') or { -1 } dname := match slash { -1 { '.' } 0 { '/' } else { path[..slash] } } resolved := realpath(dname)? return if resolved.ends_with('/') { resolved + base } else { '${resolved}/${base}' } } // realpath is realpath(3), none when it fails: os.real_path hands back its // input on failure, which here must tell a resolved path from an unresolved fn realpath(path string) ?string { res := C.realpath(&char(path.str), unsafe { nil }) if res == unsafe { nil } { return none } out := unsafe { cstring_to_vstring(res) } unsafe { C.free(res) } return out } fn owner(config ConfigOwner) ! { need_query(config.query, 'file')! mut handle := alpm.initialize(root_path, db_path) or { return err } defer { handle.release() or { went_on('release the alpm handle', err) } } mut pkgcache := local_pkgcache(handle)! installed := pkgcache.pkgs() for target in config.query { q := owner_query(target) or { target_error(err.msg()) continue } path := '${root_path}${q.rel}' owners := file_owners(installed, q) if owners.len == 0 { target_error('no package owns ${path}') continue } // a directory's owners are one event each, the path repeated if ipc_on() { for pkg in owners { emit(OwnerEvent{ path: path package: pkg.get_name() version: pkg.get_version() }) } continue } // pacman's -Qqo: the owners' names alone if config.quiet { for pkg in owners { println(pkg.get_name()) } continue } println(table_text(owners.map(row(name_style(path), name_style(it.get_name()), version_style(it.get_version()))), '', ' ')) } }