module main import alpm // orphan_pkgs are the installed dependencies that nothing kept reaches through // its dependencies. the kept packages are the explicitly installed ones and the // ones an installed package lists as optional, as pacman keeps them. pacman's // -Qdt only finds the dependencies nothing requires, so a group that only // requires itself, such as a set of plugins left behind by their app, is missed. // with optional_too an optional dependency keeps nothing, pacman's -Qdtt: // what only an optdepends line still names is an orphan as well. // a dependency reaches every installed package that satisfies it, not the // first one alone: pacman's compute_requiredby counts a requirer for each // provider, so two providers of one virtual name are both kept fn orphan_pkgs(mut pkgcache alpm.List, optional_too bool) []&alpm.Package { installed := pkgcache.pkgs() // who could answer for a name: the package called it, and every one // that provides it, by its place in installed; the version is libalpm's to compare, below mut answers := map[string][]int{} for i, pkg in installed { answers[pkg.get_name()] << i for p in pkg.get_provides().depends() { if p.get_name() != pkg.get_name() { answers[p.get_name()] << i } } } mut reached := map[string]bool{} mut queue := []&alpm.Package{} for pkg in installed { if pkg.get_reason() != .depend || (!optional_too && pkg.compute_optional_for().len > 0) { reached[pkg.get_name()] = true queue << pkg } } for queue.len > 0 { pkg := queue.pop() deps := pkg.get_depends() for dep in deps.depends() { depstring := dep.compute_string() for i in answers[dep.get_name()] { cand := installed[i] if reached[cand.get_name()] { continue } // a list of the one candidate, for alpm_find_satisfier's // version and provision comparison one := &alpm.List(unsafe { nil }).add(voidptr(cand)) satisfied := one.find_satisfier(depstring) != none one.free() if satisfied { reached[cand.get_name()] = true queue << cand } } } } return installed.filter(it.get_reason() == .depend && !reached[it.get_name()]) } fn orphans(config ConfigOrphans) ! { 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)! mut listing := Listing{ fields: cfg_search_fields.clone() } for pkg in orphan_pkgs(mut pkgcache, config.level >= 2) { listing.add_pkg(pkg, 'local') } listing.print() }