vpac / src / filter.v
48 lines · 45 sloc · 1.45 KB · 3609dfbd82867dc1034a443f8f9ced529ddbbedd
Raw
1module main
2
3import alpm
4
5// filter_sync_dbs loads the sync dbs when a filter compares the installed
6// packages with them. the other filters need no sync data
7fn filter_sync_dbs(mut ui TransUI, mut handle &alpm.Handle, config ConfigSearch) ![]&alpm.DB {
8 if !(config.foreign || config.native || config.upgrades) {
9 return []&alpm.DB{}
10 }
11 dbs := init_repos(mut handle)!
12 check_sync_fresh(mut ui, mut handle)!
13 return dbs
14}
15
16// pkg_matches_filter is true when an installed package passes every filter
17// given, the way pacman's -Q filters combine
18fn pkg_matches_filter(pkg &alpm.Package, config ConfigSearch, sync_dbs []&alpm.DB) bool {
19 reason := pkg.get_reason()
20 if config.deps && reason != .depend {
21 return false
22 }
23 if config.explicit && reason != .explicit {
24 return false
25 }
26 // pacman's -Qt: required by nothing, and optional for nothing either;
27 // twice (-Qtt) what is only optionally required passes too
28 if config.unrequired && (pkg.compute_required_by().len > 0
29 || (config.unrequired_level < 2 && pkg.compute_optional_for().len > 0)) {
30 return false
31 }
32 if config.foreign || config.native || config.upgrades {
33 sync_pkg := find_repo_pkg(sync_dbs, pkg.get_name())
34 if config.foreign && sync_pkg != none {
35 return false
36 }
37 if config.native && sync_pkg == none {
38 return false
39 }
40 if config.upgrades {
41 newer := sync_pkg or { return false }
42 if alpm.pkg_vercmp(pkg.get_version(), newer.get_version()) >= 0 {
43 return false
44 }
45 }
46 }
47 return true
48}
49