vpac / src / orphans.v
74 lines · 69 sloc · 2.5 KB · 3609dfbd82867dc1034a443f8f9ced529ddbbedd
Raw
1module main
2
3import alpm
4
5// orphan_pkgs are the installed dependencies that nothing kept reaches through
6// its dependencies. the kept packages are the explicitly installed ones and the
7// ones an installed package lists as optional, as pacman keeps them. pacman's
8// -Qdt only finds the dependencies nothing requires, so a group that only
9// requires itself, such as a set of plugins left behind by their app, is missed.
10// with optional_too an optional dependency keeps nothing, pacman's -Qdtt:
11// what only an optdepends line still names is an orphan as well.
12// a dependency reaches every installed package that satisfies it, not the
13// first one alone: pacman's compute_requiredby counts a requirer for each
14// provider, so two providers of one virtual name are both kept
15fn orphan_pkgs(mut pkgcache alpm.List, optional_too bool) []&alpm.Package {
16 installed := pkgcache.pkgs()
17 // who could answer for a name: the package called it, and every one
18 // that provides it, by its place in installed; the version is libalpm's to compare, below
19 mut answers := map[string][]int{}
20 for i, pkg in installed {
21 answers[pkg.get_name()] << i
22 for p in pkg.get_provides().depends() {
23 if p.get_name() != pkg.get_name() {
24 answers[p.get_name()] << i
25 }
26 }
27 }
28 mut reached := map[string]bool{}
29 mut queue := []&alpm.Package{}
30 for pkg in installed {
31 if pkg.get_reason() != .depend || (!optional_too && pkg.compute_optional_for().len > 0) {
32 reached[pkg.get_name()] = true
33 queue << pkg
34 }
35 }
36 for queue.len > 0 {
37 pkg := queue.pop()
38 deps := pkg.get_depends()
39 for dep in deps.depends() {
40 depstring := dep.compute_string()
41 for i in answers[dep.get_name()] {
42 cand := installed[i]
43 if reached[cand.get_name()] {
44 continue
45 }
46 // a list of the one candidate, for alpm_find_satisfier's
47 // version and provision comparison
48 one := &alpm.List(unsafe { nil }).add(voidptr(cand))
49 satisfied := one.find_satisfier(depstring) != none
50 one.free()
51 if satisfied {
52 reached[cand.get_name()] = true
53 queue << cand
54 }
55 }
56 }
57 }
58 return installed.filter(it.get_reason() == .depend && !reached[it.get_name()])
59}
60
61fn orphans(config ConfigOrphans) ! {
62 mut handle := alpm.initialize(root_path, db_path) or { return err }
63 defer { handle.release() or { went_on('release the alpm handle', err) } }
64
65 mut pkgcache := local_pkgcache(handle)!
66
67 mut listing := Listing{
68 fields: cfg_search_fields.clone()
69 }
70 for pkg in orphan_pkgs(mut pkgcache, config.level >= 2) {
71 listing.add_pkg(pkg, 'local')
72 }
73 listing.print()
74}
75