vpac / src / db.v
167 lines · 153 sloc · 6.11 KB · 3609dfbd82867dc1034a443f8f9ced529ddbbedd
Raw
1@[has_globals]
2module main
3
4import alpm
5import os
6import time
7
8// arch and the download user are configurable through config.lip
9
10// pkgcache_of is a db's package list. libalpm hands an empty db's list over
11// as NULL, the NULL it answers an error with too: alpm_db_get_pkgcache
12// clears the error first, so alpm_errno tells the two apart. an empty db is
13// an empty list here — a nil one, which every list reader takes as such
14// (pkgs, pkg_find, find_satisfier) — never a failure: a fresh root has no
15// package installed, and pacman finds nothing in it rather than failing
16fn pkgcache_of(handle &alpm.Handle, db &alpm.DB) !&alpm.List {
17 return db.get_pkgcache() or {
18 e := handle.errno()
19 if e != .ok {
20 return error('could not read the packages of ${db.get_name()}: ${e.strerror()}')
21 }
22 unsafe { &alpm.List(nil) }
23 }
24}
25
26// local_pkgcache is the installed packages' list, as pkgcache_of reads one
27fn local_pkgcache(handle &alpm.Handle) !&alpm.List {
28 localdb := handle.get_localdb() or { return error('could not open the local database: ${err.msg()}') }
29 return pkgcache_of(handle, localdb)
30}
31
32// lock_path is a function because db_path is configurable
33fn lock_path() string {
34 return db_path + 'db.lck'
35}
36
37fn check_db_lock() bool {
38 return os.exists(lock_path())
39}
40
41// check_ask_db_lock warns about a db lock left behind and offers to remove it.
42// it goes right before vpac takes the lock: a transaction or a db update.
43// reading the dbs takes no lock, as pacman's -Ss and -Si take none, so a
44// read-only command checks only when it is about to update them. only root
45// can remove the lock, so anyone else gets the warning alone. the warning
46// goes to stderr with the prompt it leads into
47fn check_ask_db_lock(mut ui TransUI) {
48 if !check_db_lock() {
49 return
50 }
51 // one path, spelled once: the warning, the advice and the removal read
52 // the same path, and the removal says exactly what went wrong with it
53 lpath := lock_path()
54 warn('database lock already exists (${lpath})')
55 advise("if you're sure a package manager is not already running, you can remove it")
56 if os.geteuid() != 0 {
57 advise('run as root to remove it')
58 return
59 }
60 if ui.noyes('do you want to remove ${lpath}?') {
61 os.rm(lpath) or { report('failed to remove ${lpath}: ${err.msg()}') }
62 }
63}
64
65// init_repo registers one sync db under its name and mirrors; the arch comes
66// from the config, so the parameter is named a to keep the global visible
67// the handle parameter of the three functions here is spelled
68// `mut handle alpm.Handle`, which is already the caller's pointer: spelled
69// `mut handle &alpm.Handle` the same body hands libalpm a pointer one level
70// off, and alpm_db_update crashes on it (tried for the review's sake, and
71// caught by test_sync_stamp_keeps_an_old_server_time_fresh). leave it
72fn init_repo(mut handle alpm.Handle, repo string, servers []string, a string, usage alpm.DBUsage) !&alpm.DB {
73 // the siglevel is the configured one (pacman's default without config):
74 // libalpm's own default is 0, which verifies nothing
75 db := handle.register_syncdb(repo, int(cfg_siglevel))!
76 for server in servers {
77 db.add_server(server.replace('$repo', repo).replace('$arch', a))!
78 }
79 db.set_usage(usage)!
80 return db
81}
82
83// a sync db counts as synced if its file is this recent, or if vpac updated
84// the dbs this recently. libalpm sets the file time from the mirror, so an
85// unchanged db keeps an old time, and the stamp covers that case
86// (options :sync-age hours) sets it; 0 never calls a db stale
87__global sync_max_age = i64(24 * 60 * 60)
88
89// sync_stamp is a function because db_path is configurable
90fn sync_stamp() string {
91 return db_path + 'sync/vpac.synced'
92}
93
94fn update_repos(mut handle alpm.Handle, force bool) ! {
95 // one db_update call: libalpm itself updates only the stale dbs, and
96 // "up to date" (returned as 1) is an answer, not an error. whatever it
97 // did, the dbs are known current now, so the stamp is written
98 handle.db_update(handle.get_syncdbs()!, force)!
99 // the stamp is what every later freshness check reads: when it cannot
100 // be written, the dbs count stale again next time — said, not hidden
101 os.write_file(sync_stamp(), '') or {
102 warn('could not write the sync stamp ${sync_stamp()}: ${err.msg()}')
103 }
104}
105
106// stale_sync_dbs lists the sync dbs not synced within sync_max_age. a sync
107// db listing that fails is said: an empty answer would read as 'fresh
108// enough', and it never was
109fn stale_sync_dbs(handle &alpm.Handle) []string {
110 dbs := handle.get_syncdbs() or {
111 warn('could not list the sync databases: ${err.msg()}')
112 return []string{}
113 }
114 stamp := if os.exists(sync_stamp()) { os.file_last_mod_unix(sync_stamp()) } else { i64(0) }
115 now := time.now().unix()
116 mut stale := []string{}
117 for db in dbs.dbs() {
118 name := db.get_name()
119 file := '${db_path}sync/${name}.db'
120 file_time := if os.exists(file) { os.file_last_mod_unix(file) } else { i64(0) }
121 newest := if stamp > file_time { stamp } else { file_time }
122 if sync_max_age > 0 && now - newest > sync_max_age {
123 stale << name
124 }
125 }
126 return stale
127}
128
129// check_sync_fresh asks whether to update the sync dbs when any are stale.
130// only root can update them, so anyone else gets the warning and carries on,
131// and so does a --print preview, which changes nothing. the warning goes to
132// stderr, so it stays out of piped results
133fn check_sync_fresh(mut ui TransUI, mut handle &alpm.Handle) ! {
134 stale := stale_sync_dbs(handle)
135 if stale.len == 0 {
136 return
137 }
138 ui.pause()
139 warn('sync databases not synced in the last day: ${stale.join(', ')}')
140 if ui.print_only {
141 return
142 }
143 if os.geteuid() != 0 {
144 advise('run as root to update them')
145 return
146 }
147 if !ui.yesno('update them now?') {
148 return
149 }
150 check_ask_db_lock(mut ui)
151 update_repos(mut handle, false)!
152}
153
154// init_repos registers the sync dbs the config defines, in the order the
155// config lists them. the pkgbuild repo is not a sync db: it is built from,
156// not downloaded. urls keep the $repo and $arch placeholders for libalpm to
157// substitute
158fn init_repos(mut handle alpm.Handle) ![]&alpm.DB {
159 mut dbs := []&alpm.DB{}
160 for repo in cfg_repos {
161 if repo.kind == 'pkgbuild' {
162 continue
163 }
164 dbs << init_repo(mut handle, repo.name, repo.servers, arch, repo.usage)!
165 }
166 return dbs
167}
168