| 1 | module main |
| 2 | |
| 3 | import nspkg |
| 4 | import os |
| 5 | |
| 6 | // backups_of lists the backup entries of a package: the config files whose |
| 7 | // expected md5 the db records |
| 8 | fn backups_of(pkg &nspkg.Package) []&nspkg.Backup { |
| 9 | mut out := []&nspkg.Backup{} |
| 10 | mut node := pkg.get_backup() |
| 11 | for node != unsafe { nil } { |
| 12 | if b := node.get_backup() { |
| 13 | out << b |
| 14 | } |
| 15 | node = node.next |
| 16 | } |
| 17 | return out |
| 18 | } |
| 19 | |
| 20 | // stat_denied: the stat failed not because the file is missing but because |
| 21 | // this run may not look at it. the errno is compared by the names errno.h |
| 22 | // gives it on this system, not by the strerror text, whose wording — and |
| 23 | // language — is whatever the system says, nor by a number posix never fixed |
| 24 | fn stat_denied(err IError) bool { |
| 25 | return err.code() == C.EPERM || err.code() == C.EACCES |
| 26 | } |
| 27 | |
| 28 | // VerifyLine is one line of a check's report, the file check's and the full |
| 29 | // (mtree) check's alike. problem is whether it counts against the package; a |
| 30 | // note does not: a modified backup file, a file this user may not read. the |
| 31 | // lines are held until the spinner is gone, because an println during the |
| 32 | // spin would be drawn under the running frame |
| 33 | struct VerifyLine { |
| 34 | problem bool |
| 35 | text string |
| 36 | } |
| 37 | |
| 38 | // verify_levels are how deep the checks go: the db level, -D counted (--db |
| 39 | // one), and the file level, -k counted (--checksums one, --full two). there |
| 40 | // are two of each, as in pacman, where -kkk is -kk and -Dkkk is -Dkk: more |
| 41 | // letters are the deepest level, not a level of their own |
| 42 | fn verify_levels(c ConfigVerify) (int, int) { |
| 43 | db := c.db_level + if c.db { 1 } else { 0 } |
| 44 | file := c.checksums_level + (if c.checksums { 1 } else { 0 }) + (if c.full { 2 } else { 0 }) |
| 45 | return if db > 2 { 2 } else { db }, if file > 2 { 2 } else { file } |
| 46 | } |
| 47 | |
| 48 | // verify checks the files of installed packages the way pacman's -Qk does: |
| 49 | // every file in the db is on disk, of the type the db says, and with -k the |
| 50 | // backed up config files are not modified. it reports only the packages with |
| 51 | // problems, and returns how many there are: main exits 1 when any are |
| 52 | fn verify(config ConfigVerify) !int { |
| 53 | mut ui := new_trans_ui() |
| 54 | // the configured handle: -kk needs the NoExtract globs |
| 55 | mut handle := init_handle(ui)! |
| 56 | defer { handle.release() or { went_on('release the nspkg handle', err) } } |
| 57 | defer { ui.stop() } |
| 58 | |
| 59 | mut pkgcache := local_pkgcache(handle)! |
| 60 | |
| 61 | // -D and -DD check the databases, pacman's -Dk and -Dkk, and no files |
| 62 | db_level, level := verify_levels(config) |
| 63 | if db_level > 0 { |
| 64 | // a db error is reported as a failed target, which main counts |
| 65 | verify_db(mut handle, db_level)! |
| 66 | return 0 |
| 67 | } |
| 68 | |
| 69 | mut pkgs := pkgcache.pkgs() |
| 70 | if config.query.len > 0 { |
| 71 | // a target is a name, a versioned dependency or something provided, |
| 72 | // as pacman's -Qk resolves it; one that names nothing installed is |
| 73 | // reported and counted, and the rest are checked |
| 74 | pkgs = []&nspkg.Package{} |
| 75 | for target in config.query { |
| 76 | pkg := find_local_target(pkgcache, local_target_name(target), true) or { |
| 77 | target_error('package "${target}" was not found') |
| 78 | continue |
| 79 | } |
| 80 | add_pkg_once(mut pkgs, pkg) |
| 81 | } |
| 82 | if pkgs.len == 0 { |
| 83 | return error('no one of the named packages is installed') |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | mut s := ui.spinner(text: 'checking packages') |
| 88 | s.start() |
| 89 | defer { stop_spinner(mut s) } |
| 90 | |
| 91 | mut lines := []VerifyLine{} |
| 92 | mut bad := 0 |
| 93 | for pkg in pkgs { |
| 94 | // -kk: every file against the mtree, which covers the backup files too |
| 95 | if level >= 2 { |
| 96 | altered, found := verify_pkg_full(handle, pkg) |
| 97 | lines << found |
| 98 | if altered > 0 { |
| 99 | bad++ |
| 100 | } |
| 101 | continue |
| 102 | } |
| 103 | mut problems := 0 |
| 104 | // one name → expected md5 per package: the per-file check reads the |
| 105 | // map instead of walking the backup list again for every file |
| 106 | mut backups := map[string]string{} |
| 107 | if level >= 1 { |
| 108 | for b in backups_of(pkg) { |
| 109 | backups[b.get_name()] = b.get_hash() |
| 110 | } |
| 111 | } |
| 112 | for name in pkg.get_files().names() { |
| 113 | path := '${root_path}${name}' |
| 114 | // pacman's check_file_exists: lstat with the trailing slash |
| 115 | // dropped (its llstat), so a link — broken or not — is there as |
| 116 | // itself, and a directory the db lists that is now a link is |
| 117 | // that link, not the directory it points at |
| 118 | st := os.lstat(if name.ends_with('/') { path.trim_right('/') } else { path }) or { |
| 119 | // a file NoExtract kept off the disk is not missing, and is not |
| 120 | // said, as in pacman |
| 121 | if handle.option_match_no_extract(name) { |
| 122 | continue |
| 123 | } |
| 124 | // a stat denied by permissions is not the file missing, and it |
| 125 | // would drown the run in noise; noted but not counted (upstream |
| 126 | // pacman counts it). the errno comes along on the error, so |
| 127 | // the reading does not lean on strerror's wording |
| 128 | if stat_denied(err) { |
| 129 | lines << VerifyLine{false, '${pkg.get_name()}: warning: ${path} (${err.msg()})'} |
| 130 | } else { |
| 131 | problems++ |
| 132 | lines << VerifyLine{true, '${pkg.get_name()}: missing ${path}'} |
| 133 | } |
| 134 | continue |
| 135 | } |
| 136 | // the db records directories with a trailing slash; anything else |
| 137 | // on disk where it says one, or a directory where it says a file, |
| 138 | // is pacman's file type mismatch. a link where the db says a file |
| 139 | // is the file a package may well ship as a link |
| 140 | kind := st.get_filetype() |
| 141 | wants_dir := name.ends_with('/') |
| 142 | if (kind == .directory) != wants_dir { |
| 143 | problems++ |
| 144 | what := match kind { |
| 145 | .directory { 'a directory' } |
| 146 | .symbolic_link { 'a link' } |
| 147 | else { 'a file' } |
| 148 | } |
| 149 | lines << VerifyLine{true, '${pkg.get_name()}: ${path} is ${what}, the db says ${if wants_dir { |
| 150 | 'a directory' |
| 151 | } else { |
| 152 | 'a file' |
| 153 | }}'} |
| 154 | continue |
| 155 | } |
| 156 | // the checksums read regular files: a link's target is not the |
| 157 | // package's file |
| 158 | if kind == .symbolic_link { |
| 159 | continue |
| 160 | } |
| 161 | if level >= 1 && !wants_dir { |
| 162 | if hash := backups[name] { |
| 163 | // a checksum that libalpm cannot calculate counts as a |
| 164 | // problem, as pacman's "failed to calculate" does |
| 165 | md5 := nspkg.compute_md5sum(path) or { |
| 166 | lines << VerifyLine{true, '${pkg.get_name()}: ${path} (failed to calculate MD5 checksum)'} |
| 167 | problems++ |
| 168 | continue |
| 169 | } |
| 170 | if md5 != hash { |
| 171 | problems++ |
| 172 | lines << VerifyLine{true, '${pkg.get_name()}: modified ${path}'} |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | if problems > 0 { |
| 178 | bad++ |
| 179 | } |
| 180 | } |
| 181 | end_frame(mut ui, mut s) |
| 182 | if ipc_on() { |
| 183 | emit(VerifyEvent{ |
| 184 | lines: lines |
| 185 | bad: bad |
| 186 | }) |
| 187 | return bad |
| 188 | } |
| 189 | // now that the frame is stopped, the report prints plainly, one line |
| 190 | // beside the next |
| 191 | for l in lines { |
| 192 | println(l.text) |
| 193 | } |
| 194 | if bad == 0 { |
| 195 | println('no problems found') |
| 196 | return 0 |
| 197 | } |
| 198 | noun := if bad == 1 { '1 package with problems' } else { '${bad} packages with problems' } |
| 199 | println(noun) |
| 200 | return bad |
| 201 | } |
| 202 | |