module main import alpm import os // backups_of lists the backup entries of a package: the config files whose // expected md5 the db records fn backups_of(pkg &alpm.Package) []&alpm.Backup { mut out := []&alpm.Backup{} mut node := pkg.get_backup() for node != unsafe { nil } { if b := node.get_backup() { out << b } node = node.next } return out } // stat_denied: the stat failed not because the file is missing but because // this run may not look at it. the errno is compared by the names errno.h // gives it on this system, not by the strerror text, whose wording — and // language — is whatever the system says, nor by a number posix never fixed fn stat_denied(err IError) bool { return err.code() == C.EPERM || err.code() == C.EACCES } // VerifyLine is one line of a check's report, the file check's and the full // (mtree) check's alike. problem is whether it counts against the package; a // note does not: a modified backup file, a file this user may not read. the // lines are held until the spinner is gone, because an println during the // spin would be drawn under the running frame struct VerifyLine { problem bool text string } // verify_levels are how deep the checks go: the db level, -D counted (--db // one), and the file level, -k counted (--checksums one, --full two). there // are two of each, as in pacman, where -kkk is -kk and -Dkkk is -Dkk: more // letters are the deepest level, not a level of their own fn verify_levels(c ConfigVerify) (int, int) { db := c.db_level + if c.db { 1 } else { 0 } file := c.checksums_level + (if c.checksums { 1 } else { 0 }) + (if c.full { 2 } else { 0 }) return if db > 2 { 2 } else { db }, if file > 2 { 2 } else { file } } // verify checks the files of installed packages the way pacman's -Qk does: // every file in the db is on disk, of the type the db says, and with -k the // backed up config files are not modified. it reports only the packages with // problems, and returns how many there are: main exits 1 when any are fn verify(config ConfigVerify) !int { mut ui := new_trans_ui() // the configured handle: -kk needs the NoExtract globs mut handle := init_handle(ui)! defer { handle.release() or { went_on('release the alpm handle', err) } } defer { ui.stop() } mut pkgcache := local_pkgcache(handle)! // -D and -DD check the databases, pacman's -Dk and -Dkk, and no files db_level, level := verify_levels(config) if db_level > 0 { // a db error is reported as a failed target, which main counts verify_db(mut handle, db_level)! return 0 } mut pkgs := pkgcache.pkgs() if config.query.len > 0 { // a target is a name, a versioned dependency or something provided, // as pacman's -Qk resolves it; one that names nothing installed is // reported and counted, and the rest are checked pkgs = []&alpm.Package{} for target in config.query { pkg := find_local_target(pkgcache, local_target_name(target), true) or { target_error('package "${target}" was not found') continue } add_pkg_once(mut pkgs, pkg) } if pkgs.len == 0 { return error('no one of the named packages is installed') } } mut s := ui.spinner(text: 'checking packages') s.start() defer { stop_spinner(mut s) } mut lines := []VerifyLine{} mut bad := 0 for pkg in pkgs { // -kk: every file against the mtree, which covers the backup files too if level >= 2 { altered, found := verify_pkg_full(handle, pkg) lines << found if altered > 0 { bad++ } continue } mut problems := 0 // one name → expected md5 per package: the per-file check reads the // map instead of walking the backup list again for every file mut backups := map[string]string{} if level >= 1 { for b in backups_of(pkg) { backups[b.get_name()] = b.get_hash() } } for name in pkg.get_files().names() { path := '${root_path}${name}' // pacman's check_file_exists: lstat with the trailing slash // dropped (its llstat), so a link — broken or not — is there as // itself, and a directory the db lists that is now a link is // that link, not the directory it points at st := os.lstat(if name.ends_with('/') { path.trim_right('/') } else { path }) or { // a file NoExtract kept off the disk is not missing, and is not // said, as in pacman if handle.option_match_no_extract(name) { continue } // a stat denied by permissions is not the file missing, and it // would drown the run in noise; noted but not counted (upstream // pacman counts it). the errno comes along on the error, so // the reading does not lean on strerror's wording if stat_denied(err) { lines << VerifyLine{false, '${pkg.get_name()}: warning: ${path} (${err.msg()})'} } else { problems++ lines << VerifyLine{true, '${pkg.get_name()}: missing ${path}'} } continue } // the db records directories with a trailing slash; anything else // on disk where it says one, or a directory where it says a file, // is pacman's file type mismatch. a link where the db says a file // is the file a package may well ship as a link kind := st.get_filetype() wants_dir := name.ends_with('/') if (kind == .directory) != wants_dir { problems++ what := match kind { .directory { 'a directory' } .symbolic_link { 'a link' } else { 'a file' } } lines << VerifyLine{true, '${pkg.get_name()}: ${path} is ${what}, the db says ${if wants_dir { 'a directory' } else { 'a file' }}'} continue } // the checksums read regular files: a link's target is not the // package's file if kind == .symbolic_link { continue } if level >= 1 && !wants_dir { if hash := backups[name] { // a checksum that libalpm cannot calculate counts as a // problem, as pacman's "failed to calculate" does md5 := alpm.compute_md5sum(path) or { lines << VerifyLine{true, '${pkg.get_name()}: ${path} (failed to calculate MD5 checksum)'} problems++ continue } if md5 != hash { problems++ lines << VerifyLine{true, '${pkg.get_name()}: modified ${path}'} } } } } if problems > 0 { bad++ } } end_frame(mut ui, mut s) if ipc_on() { emit(VerifyEvent{ lines: lines bad: bad }) return bad } // now that the frame is stopped, the report prints plainly, one line // beside the next for l in lines { println(l.text) } if bad == 0 { println('no problems found') return 0 } noun := if bad == 1 { '1 package with problems' } else { '${bad} packages with problems' } println(noun) return bad }