@[has_globals] module main import nspkg import os import time // arch and the download user are configurable through the config // pkgcache_of is a db's package list. libalpm hands an empty db's list over // as NULL, the NULL it answers an error with too: alpm_db_get_pkgcache // clears the error first, so alpm_errno tells the two apart. an empty db is // an empty list here — a nil one, which every list reader takes as such // (pkgs, pkg_find, find_satisfier) — never a failure: a fresh root has no // package installed, and pacman finds nothing in it rather than failing fn pkgcache_of(handle &nspkg.Handle, db &nspkg.DB) !&nspkg.List { return db.get_pkgcache() or { e := handle.errno() if e != .ok { return error('could not read the packages of ${db.get_name()}: ${e.strerror()}') } unsafe { &nspkg.List(nil) } } } // local_pkgcache is the installed packages' list, as pkgcache_of reads one fn local_pkgcache(handle &nspkg.Handle) !&nspkg.List { localdb := handle.get_localdb() or { return error('could not open the local database: ${err.msg()}') } return pkgcache_of(handle, localdb) } // lock_path is a function because db_path is configurable fn lock_path() string { return db_path + 'db.lck' } fn check_db_lock() bool { return os.exists(lock_path()) } // check_ask_db_lock warns about a db lock left behind and offers to remove it. // it goes right before vpac takes the lock: a transaction or a db update. // reading the dbs takes no lock, as pacman's -Ss and -Si take none, so a // read-only command checks only when it is about to update them. only root // can remove the lock, so anyone else gets the warning alone. the warning // goes to stderr with the prompt it leads into fn check_ask_db_lock(mut ui TransUI) { if !check_db_lock() { return } // one path, spelled once: the warning, the advice and the removal read // the same path, and the removal says exactly what went wrong with it lpath := lock_path() warn('database lock already exists (${lpath})') advise("if you're sure a package manager is not already running, you can remove it") if os.geteuid() != 0 { advise('run as root to remove it') return } if ui.noyes('do you want to remove ${lpath}?') { os.rm(lpath) or { report('failed to remove ${lpath}: ${err.msg()}') } } } // init_repo registers one sync db under its name and mirrors; the arch comes // from the config, so the parameter is named a to keep the global visible // the handle parameter of the three functions here is spelled // `mut handle nspkg.Handle`, which is already the caller's pointer: spelled // `mut handle &nspkg.Handle` the same body hands libalpm a pointer one level // off, and alpm_db_update crashes on it (tried for the review's sake, and // caught by test_sync_stamp_keeps_an_old_server_time_fresh). leave it fn init_repo(mut handle nspkg.Handle, repo string, servers []string, a string, usage nspkg.DBUsage) !&nspkg.DB { // the siglevel is the configured one (pacman's default without config): // libalpm's own default is 0, which verifies nothing db := handle.register_syncdb(repo, int(cfg_siglevel))! for server in servers { db.add_server(server.replace('$repo', repo).replace('$arch', a))! } db.set_usage(usage)! return db } // a sync db counts as synced if its file is this recent, or if vpac updated // the dbs this recently. libalpm sets the file time from the mirror, so an // unchanged db keeps an old time, and the stamp covers that case // (options #:sync-age hours) sets it; 0 never calls a db stale __global sync_max_age = i64(24 * 60 * 60) // sync_stamp is a function because db_path is configurable fn sync_stamp() string { return db_path + 'sync/vpac.synced' } fn update_repos(mut handle nspkg.Handle, force bool) ! { // one db_update call: libalpm itself updates only the stale dbs, and // "up to date" (returned as 1) is an answer, not an error. whatever it // did, the dbs are known current now, so the stamp is written handle.db_update(handle.get_syncdbs()!, force)! // the stamp is what every later freshness check reads: when it cannot // be written, the dbs count stale again next time — said, not hidden os.write_file(sync_stamp(), '') or { warn('could not write the sync stamp ${sync_stamp()}: ${err.msg()}') } } // stale_sync_dbs lists the sync dbs not synced within sync_max_age. a sync // db listing that fails is said: an empty answer would read as 'fresh // enough', and it never was fn stale_sync_dbs(handle &nspkg.Handle) []string { dbs := handle.get_syncdbs() or { warn('could not list the sync databases: ${err.msg()}') return []string{} } stamp := if os.exists(sync_stamp()) { os.file_last_mod_unix(sync_stamp()) } else { i64(0) } now := time.now().unix() mut stale := []string{} for db in dbs.dbs() { name := db.get_name() file := '${db_path}sync/${name}.db' file_time := if os.exists(file) { os.file_last_mod_unix(file) } else { i64(0) } newest := if stamp > file_time { stamp } else { file_time } if sync_max_age > 0 && now - newest > sync_max_age { stale << name } } return stale } // check_sync_fresh asks whether to update the sync dbs when any are stale. // only root can update them, so anyone else gets the warning and carries on, // and so does a --print preview, which changes nothing. the warning goes to // stderr, so it stays out of piped results fn check_sync_fresh(mut ui TransUI, mut handle &nspkg.Handle) ! { stale := stale_sync_dbs(handle) if stale.len == 0 { return } ui.pause() warn('sync databases not synced in the last day: ${stale.join(', ')}') if ui.print_only { return } if os.geteuid() != 0 { advise('run as root to update them') return } if !ui.yesno('update them now?') { return } check_ask_db_lock(mut ui) update_repos(mut handle, false)! } // init_repos registers the sync dbs the config defines, in the order the // config lists them. the pkgbuild repo is not a sync db: it is built from, // not downloaded. urls keep the $repo and $arch placeholders for libalpm to // substitute fn init_repos(mut handle nspkg.Handle) ![]&nspkg.DB { mut dbs := []&nspkg.DB{} for repo in cfg_repos { if repo.kind == 'pkgbuild' { continue } dbs << init_repo(mut handle, repo.name, repo.servers, arch, repo.usage)! } return dbs }