vpac / src / config.v
917 lines · 887 sloc · 29.98 KB · 841315c100c81d9e6b14c4b55a25d4e3d12db762
Raw
1@[has_globals]
2module main
3
4import os
5import vile.scheme
6import stutter
7import nspkg
8
9// config_path is the file read at startup, evaluated by Vile
10// embedded from V. the file is required, since the config carries parts of
11// vpac (the repos, for one); a setting no form of it names keeps the
12// default its __global is declared with below, which reset_config puts
13// back before every load
14pub const config_path = '/etc/vpac.d/config.vile'
15
16__global config_vile = true
17
18// the settings below were consts until the config existed. root_path and
19// db_path are joined onto file names directly, so they keep a trailing slash
20__global root_path = '/'
21__global db_path = '/var/lib/pacman/'
22__global arch = 'x86_64'
23__global parallel_downloads = 3
24// the rank form: how many mirrors to probe at once (0 = unset, so the rank
25// parallelism falls back to the network's parallel) and how long one probe
26// may take
27__global rank_parallel = 0
28__global rank_timeout = 10
29// the pkgbuild form: the command that builds and how deep the clone is.
30// where it builds is path #:build. the build command is the system's makepkg,
31// which ships in the pacman package with the libalpm vpac runs on; #:makepkg
32// names another
33// the build command is trusted text: it is spelled whole into a shell that
34// runs it in the cloned dir, first as the building user and through the
35// reviews of the pkgbuild for its content. vpac does not vet it — the config
36// is code the machine's user writes (like pacman.conf no more and no less),
37// and whoever controls a user's config already controls their shell
38__global pkgbuild_makepkg = 'makepkg -f'
39__global pkgbuild_depth = 1
40// the unprivileged user libalpm downloads as, like DownloadUser in pacman.conf
41__global download_user = 'alpm'
42// the configured directories and lists. the defaults are the values the
43// hardcoded vpac used
44__global cfg_cachedirs = ['/var/cache/pacman/pkg/']
45// pacman's default HookDir, beside the system one libalpm adds by itself
46__global cfg_hookdirs = ['/etc/pacman.d/hooks/']
47// where pkgbuild packages are cloned and built (path #:build): a relative
48// dir is taken under the invoking user's home, because makepkg runs as that user
49__global pkgbuild_dir = '.cache/vpac/pkgbuild'
50__global cfg_logfile = ''
51// the keyring signatures are checked against: pacman's GPGDir default, not
52// moved under a fake root (pacman keeps it as configuration). libalpm has
53// no default of its own, and without one gpgme falls back on root's own
54// ~/.gnupg, where no packager's key is
55__global cfg_gpgdir = '/etc/pacman.d/gnupg/'
56__global cfg_protocols = ['https']
57// the signature level every sync db is registered with and packages are
58// checked against. libalpm's freshly calloc'd handle verifies nothing, so
59// vpac defaults to pacman's shipped SigLevel: a package and a database
60// signature are both verified when they are present, and a mismatching one
61// fails. override with (network #:siglevel #(...))
62__global cfg_siglevel = nspkg.SigLevel.package | .package_optional | .database | .database_optional
63__global cfg_hold = []string{}
64__global cfg_ignore = []string{}
65// the rest of pacman.conf's [options]: groups ignored as cfg_ignore's packages
66// are, files an upgrade or an install leaves alone, and the download timeout
67__global cfg_ignore_groups = []string{}
68__global cfg_noupgrade = []string{}
69__global cfg_noextract = []string{}
70__global cfg_disable_timeout = false
71// pacman.conf's DisableSandboxFilesystem and DisableSandboxSyscalls: libalpm
72// sandboxes its downloads and extracts, and this turns both parts off
73__global cfg_disable_sandbox = false
74// pacman.conf's UseSyslog: libalpm logs to syslog too, (path #:syslog #t)
75__global cfg_syslog = false
76// cfg_repos holds the repos the config defines
77__global cfg_repos = []CfgRepo{}
78
79// CfgRepo is one registered repo: a name, a kind ('sync', or 'pkgbuild' for
80// a site to build from), and the urls to fetch it from, where a url keeps
81// the $repo and $arch placeholders
82struct CfgRepo {
83 name string
84 kind string
85 servers []string
86 // what the db is used for, as pacman.conf's Usage; every use by default
87 usage nspkg.DBUsage = nspkg.db_usage_all
88}
89
90// config forms accept scheme keywords as well as stutter's quoted symbols.
91fn config_keyword_args(form string, args []scheme.Value) !([]string, []scheme.Value) {
92 mut normalized := args.clone()
93 for i := 0; i < normalized.len; i += 2 {
94 if normalized[i].kind() == .keyword {
95 normalized[i] = scheme.symbol(normalized[i].keyword_name()!)!
96 }
97 }
98 return stutter.keyword_args(form, normalized)
99}
100
101// mirror files can extend a binding without requiring the config to seed it.
102fn prim_defined(mut vm scheme.VM, args []scheme.Value) !scheme.Value {
103 scheme.check_arity('defined?', args.len, 1, 1)!
104 name := args[0].symbol_name()!
105 value := vm.lookup(name) or {
106 if err.msg().starts_with('unbound variable ') { return scheme.boolean(false) }
107 return err
108 }
109 return scheme.boolean(value.kind() != .uninitialized)
110}
111
112// usage_of reads a repo's #:usage, pacman.conf's Usage: one or more of sync,
113// search, install, upgrade and all
114fn usage_of(v scheme.Value, what string) !nspkg.DBUsage {
115 names := match v.kind() {
116 .string { [v.as_string()!] }
117 else { stutter.strs_of(v, what)! }
118 }
119 if names.len == 0 {
120 return error('config: ${what} needs at least one of sync, search, install, upgrade, all')
121 }
122 mut usage := unsafe { nspkg.DBUsage(0) }
123 for name in names {
124 usage |= match name.to_lower() {
125 'sync' { nspkg.DBUsage.sync }
126 'search' { nspkg.DBUsage.search }
127 'install' { nspkg.DBUsage.install }
128 'upgrade' { nspkg.DBUsage.upgrade }
129 'all' { nspkg.db_usage_all }
130 else {
131 return error('config: ${what} takes sync, search, install, upgrade and all, not "${name}"')
132 }
133 }
134 }
135 return usage
136}
137
138// siglevel_names are the names (network #:siglevel) accepts, mapped onto
139// libalpm's SigLevel bits, plus what each one means
140const siglevel_names = {
141 'package': nspkg.SigLevel.package
142 'package_optional': nspkg.SigLevel.package_optional
143 'package_marginal_ok': nspkg.SigLevel.package_marginal_ok
144 'package_unknown_ok': nspkg.SigLevel.package_unknown_ok
145 'database': nspkg.SigLevel.database
146 'database_optional': nspkg.SigLevel.database_optional
147 'database_marginal_ok': nspkg.SigLevel.database_marginal_ok
148 'database_unknown_ok': nspkg.SigLevel.database_unknown_ok
149}
150
151// siglevel_of reads a SigLevel out of a config value: one name or an array
152// of names, e.g. (network #:siglevel #("package" "database_optional"))
153fn siglevel_of(v scheme.Value, what string) !nspkg.SigLevel {
154 names := match v.kind() {
155 .string { [v.as_string()!] }
156 else { stutter.strs_of(v, what)! }
157 }
158 // an empty one verifies nothing: libalpm would take the silent answer
159 // as handing everything over unsigned, which nobody ever means
160 if names.len == 0 {
161 return error('config: ${what} needs at least one of: ${siglevel_names.keys().join(', ')}')
162 }
163 mut level := unsafe { nspkg.SigLevel(0) }
164 for name in names {
165 key := name.to_lower()
166 bit := siglevel_names[key] or {
167 return error('config: ${what} must be one or more of: ${siglevel_names.keys().join(', ')}')
168 }
169 level |= bit
170 }
171 return level
172}
173
174// prim_path is the config's (path ...) form: root, db, log, gpg, cache, hook,
175// and #:syslog true for pacman.conf's UseSyslog
176fn prim_path(mut _vm scheme.VM, args []scheme.Value) !scheme.Value {
177 keys, values := config_keyword_args('path', args)!
178 mut i := 0
179 for i < keys.len {
180 match keys[i] {
181 'root' {
182 root_path = stutter.dir_path('path #:root', stutter.str_of(values[i], 'path #:root')!)!
183 }
184 'db' {
185 db_path = stutter.dir_path('path #:db', stutter.str_of(values[i], 'path #:db')!)!
186 }
187 'log' {
188 cfg_logfile = stutter.str_of(values[i], 'path #:log')!
189 }
190 'syslog' {
191 cfg_syslog = stutter.bool_of(values[i], 'path #:syslog')!
192 }
193 'gpg' {
194 cfg_gpgdir = stutter.dir_path('path #:gpg', stutter.str_of(values[i], 'path #:gpg')!)!
195 }
196 'cache' {
197 dirs := stutter.strs_of(values[i], 'path #:cache')!
198 cfg_cachedirs = []string{}
199 for dir in dirs {
200 cfg_cachedirs << stutter.dir_path('path #:cache', dir)!
201 }
202 }
203 'hook' {
204 dirs := stutter.strs_of(values[i], 'path #:hook')!
205 cfg_hookdirs = []string{}
206 for dir in dirs {
207 cfg_hookdirs << stutter.dir_path('path #:hook', dir)!
208 }
209 }
210 'build' {
211 d := stutter.str_of(values[i], 'path #:build')!
212 if d == '' {
213 return error('config: path #:build must not be empty')
214 }
215 pkgbuild_dir = d
216 }
217 else {
218 return error('config: path has no key #:${keys[i]}')
219 }
220 }
221 i++
222 }
223 return scheme.nil_value()
224}
225
226// prim_pkg is the config's (pkg ...) form: arch, hold, ignore, and pacman.conf's
227// IgnoreGroup, NoUpgrade and NoExtract as #:ignoregroup, #:noupgrade and
228// #:noextract, each a list, e.g. (pkg #:noextract #("usr/share/doc/*"))
229fn prim_pkg(mut _vm scheme.VM, args []scheme.Value) !scheme.Value {
230 keys, values := config_keyword_args('pkg', args)!
231 mut i := 0
232 for i < keys.len {
233 match keys[i] {
234 'arch' {
235 a := stutter.str_of(values[i], 'pkg #:arch')!
236 // it fills the $arch of every server url and is the one
237 // architecture libalpm installs for: one plain word
238 if a == '' || a.contains('/') || a.fields().len != 1 || a.fields()[0] != a {
239 return error('config: pkg #:arch must be one plain word, such as x86_64')
240 }
241 arch = a
242 }
243 'hold' {
244 cfg_hold = stutter.strs_of(values[i], 'pkg #:hold')!
245 }
246 'ignore' {
247 cfg_ignore = stutter.strs_of(values[i], 'pkg #:ignore')!
248 }
249 'ignoregroup' {
250 cfg_ignore_groups = stutter.strs_of(values[i], 'pkg #:ignoregroup')!
251 }
252 'noupgrade' {
253 cfg_noupgrade = stutter.strs_of(values[i], 'pkg #:noupgrade')!
254 }
255 'noextract' {
256 cfg_noextract = stutter.strs_of(values[i], 'pkg #:noextract')!
257 }
258 else {
259 return error('config: pkg has no key #:${keys[i]}')
260 }
261 }
262 i++
263 }
264 return scheme.nil_value()
265}
266
267// prim_network is the config's (network ...) form: parallel, protocols, user,
268// siglevel, #:disable-timeout true for pacman.conf's DisableDownloadTimeout, and
269// #:disable-sandbox true for its DisableSandbox
270fn prim_network(mut _vm scheme.VM, args []scheme.Value) !scheme.Value {
271 keys, values := config_keyword_args('network', args)!
272 mut i := 0
273 for i < keys.len {
274 match keys[i] {
275 'parallel' {
276 n := stutter.int_of(values[i], 'network #:parallel')!
277 if n < 1 {
278 return error('config: network #:parallel must be at least 1')
279 }
280 parallel_downloads = n
281 }
282 'protocols' {
283 protos := stutter.strs_of(values[i], 'network #:protocols')!
284 if protos.len == 0 {
285 return error('config: network #:protocols needs at least one, https or http')
286 }
287 // a scheme no mirror is listed under would filter every
288 // mirror out without a word
289 for proto in protos {
290 if proto !in ['https', 'http'] {
291 return error('config: network #:protocols takes https and http, not "${proto}"')
292 }
293 }
294 cfg_protocols = protos.clone()
295 }
296 'user' {
297 download_user = stutter.str_of(values[i], 'network #:user')!
298 }
299 'siglevel' {
300 cfg_siglevel = siglevel_of(values[i], 'network #:siglevel')!
301 }
302 'disable-timeout' {
303 cfg_disable_timeout = stutter.bool_of(values[i], 'network #:disable-timeout')!
304 }
305 'disable-sandbox' {
306 cfg_disable_sandbox = stutter.bool_of(values[i], 'network #:disable-sandbox')!
307 }
308 else {
309 return error('config: network has no key #:${keys[i]}')
310 }
311 }
312 i++
313 }
314 return scheme.nil_value()
315}
316
317// prim_rank is the config's (rank ...) form: parallel and timeout for the
318// mirror ranking. the parallelism falls back to the network's parallel when
319// the rank form does not set its own. #:top is how many mirrors are answered,
320// #:completion (0 to 1) and #:max-delay (seconds) what a mirror must report to
321// be probed, #:candidates how many get the speed test, #:retest how many of
322// the fastest are timed twice, #:dir where rank -w writes
323fn prim_rank(mut _vm scheme.VM, args []scheme.Value) !scheme.Value {
324 keys, values := config_keyword_args('rank', args)!
325 mut i := 0
326 for i < keys.len {
327 match keys[i] {
328 'parallel' {
329 n := stutter.int_of(values[i], 'rank #:parallel')!
330 if n < 1 {
331 return error('config: rank #:parallel must be at least 1')
332 }
333 rank_parallel = n
334 }
335 'timeout' {
336 n := stutter.int_of(values[i], 'rank #:timeout')!
337 if n < 1 {
338 return error('config: rank #:timeout must be at least 1')
339 }
340 rank_timeout = n
341 }
342 'top' {
343 rank_top = stutter.count_of(values[i], 'rank #:top', 1)!
344 }
345 'dir' {
346 d := stutter.str_of(values[i], 'rank #:dir')!
347 if !d.starts_with('/') {
348 return error('config: rank #:dir must be an absolute path')
349 }
350 rank_dir = d.trim_right('/')
351 }
352 'candidates' {
353 rank_speed_candidates = stutter.count_of(values[i], 'rank #:candidates', 1)!
354 }
355 'retest' {
356 rank_retest = stutter.count_of(values[i], 'rank #:retest', 0)!
357 }
358 'completion' {
359 rank_min_completion = stutter.ratio_of(values[i], 'rank #:completion')!
360 }
361 'max-delay' {
362 rank_max_delay = i64(stutter.count_of(values[i], 'rank #:max-delay', 0)!)
363 }
364 else {
365 return error('config: rank has no key #:${keys[i]}')
366 }
367 }
368 i++
369 }
370 return scheme.nil_value()
371}
372
373// prim_repo is the config's (repo ...) form: a repo's name, its type ('sync',
374// or 'pkgbuild', a site to build from) and the mirrors it is fetched
375// from, with $repo and $arch left as placeholders
376// #:usage is pacman.conf's Usage: a list of sync, search, install, upgrade
377// and all; every use when it is left out
378fn prim_repo(mut _vm scheme.VM, args []scheme.Value) !scheme.Value {
379 keys, values := config_keyword_args('repo', args)!
380 mut repo := CfgRepo{
381 kind: 'sync'
382 }
383 mut i := 0
384 for i < keys.len {
385 match keys[i] {
386 'name' {
387 rname := stutter.str_of(values[i], 'repo #:name')!
388 repo = CfgRepo{
389 ...repo
390 name: rname
391 }
392 }
393 'type' {
394 rkind := stutter.str_of(values[i], 'repo #:type')!
395 if rkind !in ['sync', 'pkgbuild'] {
396 return error("config: repo #:type is 'sync' or 'pkgbuild', not '${rkind}'")
397 }
398 repo = CfgRepo{
399 ...repo
400 kind: rkind
401 }
402 }
403 'src' {
404 rservers := stutter.strs_of(values[i], 'repo #:src')!
405 if rservers.len == 0 {
406 return error('config: repo #:src needs at least one url')
407 }
408 for server in rservers {
409 if server == '' {
410 return error('config: repo #:src has an empty url in it')
411 }
412 }
413 repo = CfgRepo{
414 ...repo
415 servers: rservers
416 }
417 }
418 'usage' {
419 rusage := usage_of(values[i], 'repo #:usage')!
420 repo = CfgRepo{
421 ...repo
422 usage: rusage
423 }
424 }
425 else {
426 return error('config: repo has no key #:${keys[i]}')
427 }
428 }
429 i++
430 }
431 if repo.name == '' {
432 return error('config: repo needs a #:name')
433 }
434 // libalpm refuses a second db of a name, and 'local' is its own db's:
435 // said here, by name, instead of late and from inside a mode
436 if repo.name == 'local' {
437 return error('config: a repo cannot be named local, the installed packages are')
438 }
439 if cfg_repos.any(it.name == repo.name) {
440 return error('config: repo ${repo.name} is defined twice')
441 }
442 if repo.kind == 'sync' && repo.servers.len == 0 {
443 return error('config: a sync repo takes at least one #:src')
444 }
445 if repo.kind == 'pkgbuild' && repo.servers.len != 1 {
446 return error('config: a pkgbuild repo takes one #:src, the site to build from')
447 }
448 cfg_repos << repo
449 return scheme.nil_value()
450}
451
452// pkgbuild_repos are the configured pkgbuild repos, in config order. the
453// --pkgbuild flags search and build from all of them, each under its own name
454fn pkgbuild_repos() []CfgRepo {
455 return cfg_repos.filter(it.kind == 'pkgbuild')
456}
457
458// prim_pkgbuild is the config's (pkgbuild ...) form: makepkg, depth, and
459// timeout, the seconds one lookup of a pkgbuild repo may take. where the
460// build happens is path #:build
461fn prim_pkgbuild(mut _vm scheme.VM, args []scheme.Value) !scheme.Value {
462 keys, values := config_keyword_args('pkgbuild', args)!
463 mut i := 0
464 for i < keys.len {
465 match keys[i] {
466 'makepkg' {
467 m := stutter.str_of(values[i], 'pkgbuild #:makepkg')!
468 if m == '' {
469 return error('config: pkgbuild #:makepkg must not be empty')
470 }
471 pkgbuild_makepkg = m
472 }
473 'depth' {
474 n := stutter.int_of(values[i], 'pkgbuild #:depth')!
475 if n < 1 {
476 return error('config: pkgbuild #:depth must be at least 1')
477 }
478 pkgbuild_depth = n
479 }
480 'timeout' {
481 pkgbuild_rpc_timeout = stutter.count_of(values[i], 'pkgbuild #:timeout', 1)!
482 }
483 else {
484 return error('config: pkgbuild has no key #:${keys[i]}')
485 }
486 }
487 i++
488 }
489 return scheme.nil_value()
490}
491
492// option_flags are the mode flags (options ...) can turn on for every run,
493// by the config's key: each names the field the modes' flag structs keep it
494// in, so a mode that has no such flag is simply not touched
495const option_flags = {
496 'noconfirm': 'noconfirm'
497 'nopkgbuildconfirm': 'no_pkgbuild_confirm'
498 'needed': 'needed'
499 'no-upgrade-deps': 'no_upgrade_deps'
500 'quiet': 'quiet'
501 'keep-current': 'keep_current'
502 'fuzzy': 'fuzzy'
503 'disable-timeout': 'disable_timeout'
504}
505
506// opt_flags holds the fields of option_flags the config turned on;
507// opt_color is options #:color, empty when the config names none
508__global opt_flags = map[string]bool{}
509__global opt_color = ''
510
511// prim_options is the config's (options ...) form: root, the command vpac
512// gets root with (privilege.v); color, the --color a run starts from;
513// verbose, the verbose log of a run's housekeeping failures (--verbose);
514// sync-age, the hours after which the sync dbs are called stale (0: never);
515// suggest, how close (0 to 1) a name must be to be offered for a typo; and
516// the mode flags of option_flags, true or false
517fn prim_options(mut _vm scheme.VM, args []scheme.Value) !scheme.Value {
518 keys, values := config_keyword_args('options', args)!
519 mut i := 0
520 for i < keys.len {
521 match keys[i] {
522 'root' {
523 r := stutter.str_of(values[i], 'options #:root')!
524 if r.trim_space() == '' {
525 return error('config: options #:root must not be empty')
526 }
527 root_provider = r
528 }
529 'color' {
530 c := stutter.str_of(values[i], 'options #:color')!
531 if c !in ['auto', 'always', 'never'] {
532 return error('config: options #:color takes auto, always or never, not "${c}"')
533 }
534 opt_color = c
535 }
536 'sync-age' {
537 // hours; 0 never warns
538 sync_max_age = i64(stutter.count_of(values[i], 'options #:sync-age', 0)!) * 3600
539 }
540 'suggest' {
541 suggest_thold = stutter.ratio_of(values[i], 'options #:suggest')!
542 }
543 'verbose' {
544 // the verbose log for every run, as --verbose for one
545 cfg_verbose = stutter.bool_of(values[i], 'options #:verbose')!
546 }
547 else {
548 field := option_flags[keys[i]] or {
549 return error('config: options has no key #:${keys[i]}')
550 }
551 // only what is on is held: a false takes back an earlier
552 // true (an --eval over the file) and leaves no entry behind
553 if stutter.bool_of(values[i], 'options #:${keys[i]}')! {
554 opt_flags[field] = true
555 } else {
556 opt_flags.delete(field)
557 }
558 }
559 }
560 i++
561 }
562 return scheme.nil_value()
563}
564
565// prim_search is the config's (search ...) form: fields, the columns of a
566// listing when --fields names none, and thold, the fuzzy threshold
567fn prim_search(mut _vm scheme.VM, args []scheme.Value) !scheme.Value {
568 keys, values := config_keyword_args('search', args)!
569 mut i := 0
570 for i < keys.len {
571 match keys[i] {
572 'fields' {
573 names := stutter.strs_of(values[i], 'search #:fields')!
574 if names.len == 0 {
575 return error('config: search #:fields needs at least one field')
576 }
577 cfg_search_fields = parse_fields(names.join(','), []string{}) or {
578 return error('config: search #:fields: ${err.msg()}')
579 }
580 }
581 'thold' {
582 cfg_search_thold = stutter.ratio_of(values[i], 'search #:thold')!
583 }
584 else {
585 return error('config: search has no key #:${keys[i]}')
586 }
587 }
588 i++
589 }
590 return scheme.nil_value()
591}
592
593// prim_clean is the config's (clean ...) form: keep, the versions of every
594// package the cache keeps when -k names none
595fn prim_clean(mut _vm scheme.VM, args []scheme.Value) !scheme.Value {
596 keys, values := config_keyword_args('clean', args)!
597 mut i := 0
598 for i < keys.len {
599 match keys[i] {
600 'keep' {
601 cfg_clean_keep = stutter.count_of(values[i], 'clean #:keep', 0)!
602 }
603 else {
604 return error('config: clean has no key #:${keys[i]}')
605 }
606 }
607 i++
608 }
609 return scheme.nil_value()
610}
611
612// prim_tree is the config's (tree ...) form: depth, how deep a tree is
613// drawn when -d names none
614fn prim_tree(mut _vm scheme.VM, args []scheme.Value) !scheme.Value {
615 keys, values := config_keyword_args('tree', args)!
616 mut i := 0
617 for i < keys.len {
618 match keys[i] {
619 'depth' {
620 cfg_tree_depth = stutter.count_of(values[i], 'tree #:depth', 0)!
621 }
622 else {
623 return error('config: tree has no key #:${keys[i]}')
624 }
625 }
626 i++
627 }
628 return scheme.nil_value()
629}
630
631// prim_color is the config's (color ...) form, the theme: each key is a role
632// of default_theme, each value one style or a list of them out of
633// style_names, e.g. (color #:version "cyan" #:warning #("bold" "magenta"))
634fn prim_color(mut _vm scheme.VM, args []scheme.Value) !scheme.Value {
635 keys, values := config_keyword_args('color', args)!
636 mut i := 0
637 for i < keys.len {
638 if keys[i] !in theme {
639 return error('config: color has no role #:${keys[i]}, the roles are: ${default_theme().keys().join(', ')}')
640 }
641 styles := stutter.strs_of(values[i], 'color #:${keys[i]}')!
642 for style in styles {
643 if style !in style_names {
644 return error('config: color #:${keys[i]} takes ${style_names.join(', ')}, not "${style}"')
645 }
646 }
647 theme[keys[i]] = styles.clone()
648 i++
649 }
650 return scheme.nil_value()
651}
652
653// prim_mirrorlist is the config's (mirrorlist ...) form: an os for the
654// ranker, new or one of its own redefined. #:name is what rank is asked for
655// (and what /etc/os-release is matched against), #:repo the repo whose db the
656// speed test pulls, and the mirrors come from one of #:status, a mirror
657// status json api as arch publishes, or #:list, a Server = mirrorlist at a
658// url or an absolute path
659fn prim_mirrorlist(mut _vm scheme.VM, args []scheme.Value) !scheme.Value {
660 keys, values := config_keyword_args('mirrorlist', args)!
661 mut name := ''
662 mut status := ''
663 mut list := ''
664 mut repo := ''
665 mut i := 0
666 for i < keys.len {
667 match keys[i] {
668 'name' { name = stutter.str_of(values[i], 'mirrorlist #:name')! }
669 'status' { status = stutter.str_of(values[i], 'mirrorlist #:status')! }
670 'list' { list = stutter.str_of(values[i], 'mirrorlist #:list')! }
671 'repo' { repo = stutter.str_of(values[i], 'mirrorlist #:repo')! }
672 else { return error('config: mirrorlist has no key #:${keys[i]}') }
673 }
674 i++
675 }
676 if name == '' || name.contains('/') || name.starts_with('.') || name.starts_with('-')
677 || name.fields().len != 1 || name.fields()[0] != name {
678 return error('config: mirrorlist needs a #:name, one plain word')
679 }
680 if repo == '' {
681 return error('config: mirrorlist ${name} needs a #:repo, the repo whose db is timed')
682 }
683 if (status == '') == (list == '') {
684 return error('config: mirrorlist ${name} takes one of #:status and #:list')
685 }
686 if status != '' && !status.starts_with('https://') && !status.starts_with('http://') {
687 return error('config: mirrorlist ${name} #:status must be an http or https url')
688 }
689 if list != '' && !list.starts_with('https://') && !list.starts_with('http://')
690 && !list.starts_with('/') {
691 return error('config: mirrorlist ${name} #:list must be a url or an absolute path')
692 }
693 rank_oses[name] = RankOs{
694 status_json: status
695 mirrorlist: list
696 repo: repo
697 }
698 return scheme.nil_value()
699}
700
701// prim_progress is the config's (progress ...) form, the bars: style
702// ('classic' or 'block'); the graphic's pieces start, end, fill, head and
703// empty; length in cells (0 fills the terminal); percent, eta, elapsed and
704// rate, true or false; delay, the milliseconds between redraws
705fn prim_progress(mut _vm scheme.VM, args []scheme.Value) !scheme.Value {
706 keys, values := config_keyword_args('progress', args)!
707 mut i := 0
708 for i < keys.len {
709 what := 'progress #:${keys[i]}'
710 match keys[i] {
711 'style' {
712 s := stutter.str_of(values[i], what)!
713 if s !in ['classic', 'block'] {
714 return error('config: ${what} is classic or block, not "${s}"')
715 }
716 cfg_progress.style = s
717 }
718 'start', 'end' {
719 cfg_progress.parts[keys[i]] = stutter.str_of(values[i], what)!
720 }
721 'fill', 'empty' {
722 cfg_progress.parts[keys[i]] = stutter.cell_of(values[i], what, false)!
723 }
724 'head' {
725 cfg_progress.parts[keys[i]] = stutter.cell_of(values[i], what, true)!
726 }
727 'length' {
728 cfg_progress.length = stutter.count_of(values[i], what, 0)!
729 }
730 'percent' {
731 cfg_progress.percent = stutter.bool_of(values[i], what)!
732 }
733 'eta' {
734 cfg_progress.eta = stutter.bool_of(values[i], what)!
735 }
736 'elapsed' {
737 cfg_progress.elapsed = stutter.bool_of(values[i], what)!
738 }
739 'rate' {
740 cfg_progress.rate = stutter.bool_of(values[i], what)!
741 }
742 'delay' {
743 cfg_progress.delay = stutter.count_of(values[i], what, 1)!
744 }
745 else {
746 return error('config: progress has no key #:${keys[i]}')
747 }
748 }
749 i++
750 }
751 return scheme.nil_value()
752}
753
754// prim_spinner is the config's (spinner ...) form: frames, a set by name
755// (spinner_sets) or a list of frames of one's own; interval, the
756// milliseconds a frame stays; done, what a finished step shows in place of
757// the animation; elapsed, whether the time taken is shown
758fn prim_spinner(mut _vm scheme.VM, args []scheme.Value) !scheme.Value {
759 keys, values := config_keyword_args('spinner', args)!
760 mut i := 0
761 for i < keys.len {
762 what := 'spinner #:${keys[i]}'
763 match keys[i] {
764 'frames' {
765 if values[i].kind() == .string {
766 name := values[i].as_string()!
767 cfg_progress.frames = spinner_frames_named(name) or {
768 return error('config: ${what} knows ${spinner_sets.join(', ')}, not "${name}"; a list gives frames of your own')
769 }
770 } else {
771 frames := stutter.strs_of(values[i], what)!
772 if frames.len == 0 {
773 return error('config: ${what} needs at least one frame')
774 }
775 cfg_progress.frames = frames.clone()
776 }
777 }
778 'interval' {
779 cfg_progress.interval = stutter.count_of(values[i], what, 1)!
780 }
781 'done' {
782 cfg_progress.done = stutter.str_of(values[i], what)!
783 }
784 'elapsed' {
785 cfg_progress.timed = stutter.bool_of(values[i], what)!
786 }
787 else {
788 return error('config: spinner has no key #:${keys[i]}')
789 }
790 }
791 i++
792 }
793 return scheme.nil_value()
794}
795
796// reset_config puts every config global back at its declared default, the
797// values the hardcoded vpac used. load_config runs it first, so a second
798// load in one process neither duplicates the repos nor keeps settings the
799// new file does not name; a failed load can leave values behind, and the
800// next load starts clean again
801fn reset_config() {
802 config_vile = true
803 root_path = '/'
804 root_provider = 'sudo'
805 opt_flags = map[string]bool{}
806 opt_color = ''
807 cfg_verbose = false
808 sync_max_age = i64(24 * 60 * 60)
809 suggest_thold = 0.85
810 pkgbuild_rpc_timeout = 10
811 rank_min_completion = 1.0
812 rank_max_delay = i64(86400)
813 rank_speed_candidates = 40
814 rank_retest = 5
815 rank_top = 10
816 cfg_search_fields = search_default_fields.clone()
817 cfg_search_thold = 0.9
818 cfg_clean_keep = 0
819 cfg_tree_depth = 0
820 theme = default_theme()
821 rank_oses = default_rank_oses()
822 rank_dir = '/etc/vpac.d/mirrors'
823 cfg_progress = ProgressCfg{}
824 db_path = '/var/lib/pacman/'
825 arch = 'x86_64'
826 parallel_downloads = 3
827 rank_parallel = 0
828 rank_timeout = 10
829 pkgbuild_makepkg = 'makepkg -f'
830 pkgbuild_depth = 1
831 download_user = 'alpm'
832 pkgbuild_dir = '.cache/vpac/pkgbuild'
833 cfg_cachedirs = ['/var/cache/pacman/pkg/']
834 cfg_hookdirs = ['/etc/pacman.d/hooks/']
835 cfg_logfile = ''
836 cfg_gpgdir = '/etc/pacman.d/gnupg/'
837 cfg_protocols = ['https']
838 cfg_siglevel = nspkg.SigLevel.package | .package_optional | .database | .database_optional
839 cfg_hold = []string{}
840 cfg_ignore = []string{}
841 cfg_ignore_groups = []string{}
842 cfg_noupgrade = []string{}
843 cfg_noextract = []string{}
844 cfg_disable_timeout = false
845 cfg_disable_sandbox = false
846 cfg_syslog = false
847 cfg_repos = []CfgRepo{}
848}
849
850// load_config reads the config file -- the one --config names, else
851// config_path -- and evaluates it in a machine that provides the config
852// forms. the file is required: vpac has no defaults to fall back to once its
853// repos, paths and mirror lists live in the config. the --eval snippets run
854// after it, in the same machine, so they can extend or override the file for
855// this run. a file that does not parse, or a form with the wrong keys, stops
856// vpac, because running on silently could act on the wrong database
857fn load_config(chosen string, evals []string) ! {
858 reset_config()
859 old_path := os.join_path(os.dir(config_path), 'config.scm')
860 path := if chosen != '' {
861 chosen
862 } else if !os.exists(config_path) && os.is_file(old_path) {
863 old_path
864 } else {
865 config_path
866 }
867 config_vile = os.file_ext(path) == '.vile'
868 mut reader := stutter.Config{
869 name: 'vpac'
870 syntax: if config_vile { .vile } else { .scheme }
871 path: path
872 default_path: config_path
873 required: true
874 max_steps: 100_000_000
875 primitives: {
876 'defined?': prim_defined
877 'path': prim_path
878 'pkg': prim_pkg
879 'network': prim_network
880 'rank': prim_rank
881 'pkgbuild': prim_pkgbuild
882 'options': prim_options
883 'search': prim_search
884 'clean': prim_clean
885 'tree': prim_tree
886 'color': prim_color
887 'mirrorlist': prim_mirrorlist
888 'progress': prim_progress
889 'spinner': prim_spinner
890 'repo': prim_repo
891 }
892 }.reader()!
893 if config_vile { bind_config_settings(mut reader.machine)! }
894 reader.load()!
895 for source in evals {
896 // Parenthesized command-line overrides have always been Scheme, including
897 // for Vile files. An assignment uses the same syntax as the file.
898 if config_vile && source.trim_space().starts_with('(') {
899 reader.machine.eval(source) or {
900 if scheme.is_control_transfer(err) { return err }
901 return error('<vpac --eval>: ${err.msg()}')
902 }
903 } else {
904 reader.eval(source, '<vpac --eval>')!
905 }
906 }
907}
908
909// check_hold refuses to remove a package the config holds, as pacman does for
910// HoldPkg. libalpm has no option for it, so it is a frontend check
911fn check_hold(names []string) ! {
912 for name in names {
913 if name in cfg_hold {
914 return error('${name} is held by the config: remove it from pkg #:hold first')
915 }
916 }
917}
918