module main import alpm import os // pacman's -Dk and -Dkk, as vpac v -D and -DD. every check returns the // errors it found, as pacman words them // db_local_files: every entry of the local db has its description and its // file list. the other checks read both, so they wait for this one fn db_local_files(dbpath string) ![]string { local := '${dbpath}local' mut out := []string{} mut entries := os.ls(local) or { return error('could not open the local database ${local}: ${err.msg()}') } entries.sort() for entry in entries { if entry == 'ALPM_DB_VERSION' { continue } if !os.exists(os.join_path(local, entry, 'desc')) { out << "'${entry}': description file is missing" } if !os.exists(os.join_path(local, entry, 'files')) { out << "'${entry}': file list is missing" } } return out } // db_missing_deps: every dependency of the packages, satisfied among the // packages themselves fn db_missing_deps(handle &alpm.Handle, pkgs &alpm.List) []string { mut out := []string{} mut head := handle.checkdeps(unsafe { nil }, unsafe { nil }, pkgs, 0) defer { // the misses and the depends inside them are libalpm's to free; // inner first, then the list, as pacman's check_db_missing_deps does head.free_inner(fn (p voidptr) { unsafe { miss := &alpm.DepMissing(p) miss.free() } }) head.free() } // node walks to nil; the head is what lives mut node := head for node != unsafe { nil } { miss := unsafe { &alpm.DepMissing(node.data) } target := unsafe { cstring_to_vstring(miss.target) } out << "missing '${miss.depend.compute_string()}' dependency for '${target}'" node = node.next } return out } // db_package_conflicts: no two installed packages conflict fn db_package_conflicts(handle &alpm.Handle, pkgs &alpm.List) []string { mut out := []string{} mut head := handle.checkconflicts(pkgs) defer { // the conflicts are libalpm's to free, as pacman's // check_db_local_package_conflicts does: inner, then the list head.free_inner(fn (p voidptr) { unsafe { conf := &alpm.Conflict(p) conf.free() } }) head.free() } // node walks to nil; the head is what lives mut node := head for node != unsafe { nil } { c := unsafe { &alpm.Conflict(node.data) } out << "'${c.package1.get_name()}' conflicts with '${c.package2.get_name()}'" node = node.next } return out } // db_filelist_conflicts: no file, directories aside, belongs to two // packages. every pair of owners is reported, in file order fn db_filelist_conflicts(pkgs []&alpm.Package) []string { // the owners of every file seen so far, so a third package that shares // one clashes with both of the ones before it mut owners := map[string][]string{} mut clashes := map[string][]string{} for pkg in pkgs { for name in pkg.get_files().names() { if name.ends_with('/') { continue } for other in owners[name] { clashes[name] << "file owned by '${other}' and '${pkg.get_name()}': '${name}'" } owners[name] << pkg.get_name() } } mut names := clashes.keys() names.sort() mut out := []string{} for name in names { out << clashes[name] } return out } // check_db_local is -Dk: the local db's files, then its packages' // dependencies, conflicts and shared files. the checks accumulate: each one // speaks for itself, and none hides the later ones behind an early return fn check_db_local(handle &alpm.Handle, dbpath string) ![]string { mut out := db_local_files(dbpath)! mut pkgcache := local_pkgcache(handle)! out << db_missing_deps(handle, pkgcache) out << db_package_conflicts(handle, pkgcache) out << db_filelist_conflicts(pkgcache.pkgs()) return out } // check_db_sync is -Dkk: every sync package's dependencies, satisfied // across all the sync dbs. a cache that cannot be read has the db say so; // the check does not quietly pretend that db has no packages fn check_db_sync(handle &alpm.Handle) ![]string { mut all := &alpm.List(unsafe { nil }) mut syncdbs := handle.get_syncdbs()! for db in syncdbs.dbs() { // a cache that cannot be read is named, so its unchecked packages // are not read as an empty db; an empty db is one, and checks fine mut cache := pkgcache_of(handle, db) or { warn('${err.msg()}: its packages are not checked') continue } for pkg in cache.pkgs() { all = all.add(pkg) } } defer { all.free() } return db_missing_deps(handle, all) } // verify_db is vpac v -D (level 1) and -DD (level 2). each error is a failed // target, so vpac exits 1 when there is any fn verify_db(mut handle &alpm.Handle, level int) ! { errors := if level >= 2 { init_repos(mut handle)! check_db_sync(handle)! } else { check_db_local(handle, db_path)! } for e in errors { target_error(e) } if errors.len == 0 { report('no database errors found') } }