vpac / src / db_check.v
157 lines · 148 sloc · 4.68 KB · 3609dfbd82867dc1034a443f8f9ced529ddbbedd
Raw
1module main
2
3import alpm
4import os
5
6// pacman's -Dk and -Dkk, as vpac v -D and -DD. every check returns the
7// errors it found, as pacman words them
8
9// db_local_files: every entry of the local db has its description and its
10// file list. the other checks read both, so they wait for this one
11fn db_local_files(dbpath string) ![]string {
12 local := '${dbpath}local'
13 mut out := []string{}
14 mut entries := os.ls(local) or { return error('could not open the local database ${local}: ${err.msg()}') }
15 entries.sort()
16 for entry in entries {
17 if entry == 'ALPM_DB_VERSION' {
18 continue
19 }
20 if !os.exists(os.join_path(local, entry, 'desc')) {
21 out << "'${entry}': description file is missing"
22 }
23 if !os.exists(os.join_path(local, entry, 'files')) {
24 out << "'${entry}': file list is missing"
25 }
26 }
27 return out
28}
29
30// db_missing_deps: every dependency of the packages, satisfied among the
31// packages themselves
32fn db_missing_deps(handle &alpm.Handle, pkgs &alpm.List) []string {
33 mut out := []string{}
34 mut head := handle.checkdeps(unsafe { nil }, unsafe { nil }, pkgs, 0)
35 defer {
36 // the misses and the depends inside them are libalpm's to free;
37 // inner first, then the list, as pacman's check_db_missing_deps does
38 head.free_inner(fn (p voidptr) {
39 unsafe {
40 miss := &alpm.DepMissing(p)
41 miss.free()
42 }
43 })
44 head.free()
45 }
46 // node walks to nil; the head is what lives
47 mut node := head
48 for node != unsafe { nil } {
49 miss := unsafe { &alpm.DepMissing(node.data) }
50 target := unsafe { cstring_to_vstring(miss.target) }
51 out << "missing '${miss.depend.compute_string()}' dependency for '${target}'"
52 node = node.next
53 }
54 return out
55}
56
57// db_package_conflicts: no two installed packages conflict
58fn db_package_conflicts(handle &alpm.Handle, pkgs &alpm.List) []string {
59 mut out := []string{}
60 mut head := handle.checkconflicts(pkgs)
61 defer {
62 // the conflicts are libalpm's to free, as pacman's
63 // check_db_local_package_conflicts does: inner, then the list
64 head.free_inner(fn (p voidptr) {
65 unsafe {
66 conf := &alpm.Conflict(p)
67 conf.free()
68 }
69 })
70 head.free()
71 }
72 // node walks to nil; the head is what lives
73 mut node := head
74 for node != unsafe { nil } {
75 c := unsafe { &alpm.Conflict(node.data) }
76 out << "'${c.package1.get_name()}' conflicts with '${c.package2.get_name()}'"
77 node = node.next
78 }
79 return out
80}
81
82// db_filelist_conflicts: no file, directories aside, belongs to two
83// packages. every pair of owners is reported, in file order
84fn db_filelist_conflicts(pkgs []&alpm.Package) []string {
85 // the owners of every file seen so far, so a third package that shares
86 // one clashes with both of the ones before it
87 mut owners := map[string][]string{}
88 mut clashes := map[string][]string{}
89 for pkg in pkgs {
90 for name in pkg.get_files().names() {
91 if name.ends_with('/') {
92 continue
93 }
94 for other in owners[name] {
95 clashes[name] << "file owned by '${other}' and '${pkg.get_name()}': '${name}'"
96 }
97 owners[name] << pkg.get_name()
98 }
99 }
100 mut names := clashes.keys()
101 names.sort()
102 mut out := []string{}
103 for name in names {
104 out << clashes[name]
105 }
106 return out
107}
108
109// check_db_local is -Dk: the local db's files, then its packages'
110// dependencies, conflicts and shared files. the checks accumulate: each one
111// speaks for itself, and none hides the later ones behind an early return
112fn check_db_local(handle &alpm.Handle, dbpath string) ![]string {
113 mut out := db_local_files(dbpath)!
114 mut pkgcache := local_pkgcache(handle)!
115 out << db_missing_deps(handle, pkgcache)
116 out << db_package_conflicts(handle, pkgcache)
117 out << db_filelist_conflicts(pkgcache.pkgs())
118 return out
119}
120
121// check_db_sync is -Dkk: every sync package's dependencies, satisfied
122// across all the sync dbs. a cache that cannot be read has the db say so;
123// the check does not quietly pretend that db has no packages
124fn check_db_sync(handle &alpm.Handle) ![]string {
125 mut all := &alpm.List(unsafe { nil })
126 mut syncdbs := handle.get_syncdbs()!
127 for db in syncdbs.dbs() {
128 // a cache that cannot be read is named, so its unchecked packages
129 // are not read as an empty db; an empty db is one, and checks fine
130 mut cache := pkgcache_of(handle, db) or {
131 warn('${err.msg()}: its packages are not checked')
132 continue
133 }
134 for pkg in cache.pkgs() {
135 all = all.add(pkg)
136 }
137 }
138 defer { all.free() }
139 return db_missing_deps(handle, all)
140}
141
142// verify_db is vpac v -D (level 1) and -DD (level 2). each error is a failed
143// target, so vpac exits 1 when there is any
144fn verify_db(mut handle &alpm.Handle, level int) ! {
145 errors := if level >= 2 {
146 init_repos(mut handle)!
147 check_db_sync(handle)!
148 } else {
149 check_db_local(handle, db_path)!
150 }
151 for e in errors {
152 target_error(e)
153 }
154 if errors.len == 0 {
155 report('no database errors found')
156 }
157}
158