vpac / src / modify.v
88 lines · 79 sloc · 2.7 KB · 3609dfbd82867dc1034a443f8f9ced529ddbbedd
Raw
1module main
2
3import alpm
4
5// mark sets the install reason of installed packages, pacman's -D --asdeps
6// and --asexplicit. every target is found first; one that names nothing is
7// reported and skipped, as pacman's -D and the other modes do, and the rest
8// are marked. the reasons are then written under the db lock, which pacman's
9// change_install_reason takes with an empty transaction too:
10// alpm_pkg_set_reason writes the db entry right away and locks nothing
11fn mark(config ConfigMark) ! {
12 need_query(config.query, 'package')!
13 mut ui := new_trans_ui(noconfirm: config.noconfirm)
14 check_ask_db_lock(mut ui)
15 mut handle := init_handle(ui)!
16 defer { handle.release() or { went_on('release the alpm handle', err) } }
17 defer { ui.stop() }
18 defer { trans_cleanup(handle) }
19
20 mut pkgcache := local_pkgcache(handle)!
21
22 reason := parse_reason(config.reason)!
23
24 mut pkgs := []&alpm.Package{}
25 for target in config.query {
26 mut name := local_target_name(target)
27 pkg := find_local_target(pkgcache, name, false) or {
28 name = did_you_mean(mut ui, name, pkg_names(pkgcache.pkgs())) or {
29 target_error(err.msg())
30 continue
31 }
32 pkgcache.pkg_find(name) or {
33 target_error('unknown package: "${name}"')
34 continue
35 }
36 }
37 add_pkg_once(mut pkgs, pkg)
38 }
39 if pkgs.len == 0 {
40 return
41 }
42
43 trans_begin(handle, alpm.trans_flag_0)!
44 // each reason is written to the db as it is set, so a failure leaves the
45 // ones before it changed: the error names them, so the run is truthful
46 // about what it did and did not do
47 mut set := []string{}
48 for pkg in pkgs {
49 if pkg.get_reason() != reason {
50 pkg.set_reason(reason) or {
51 already := if set.len > 0 { ' (already set: ${set.join(', ')})' } else { '' }
52 return error('could not set the install reason of ${pkg.get_name()}: ${err.msg()}${already}')
53 }
54 set << pkg.get_name()
55 }
56 }
57
58 names := pkgs.map(it.get_name())
59 if ipc_on() {
60 emit(DoneEvent{
61 action: 'marked'
62 reason: reason_key(reason)
63 packages: names
64 })
65 return
66 }
67 print_done(mut ui, '${reason_to_str(reason)}:', names.map(name_style(it)))
68}
69
70// reason_key is an install reason as a frontend reads it, and the word
71// mark takes for it (parse_reason); every reason named, as reason_to_str
72fn reason_key(reason alpm.PackageReason) string {
73 return match reason {
74 .explicit { 'explicit' }
75 .depend { 'depend' }
76 .unknown { 'unknown' }
77 }
78}
79
80// parse_reason reads mark's --reason: a reason's key, or a short form of it
81fn parse_reason(text string) !alpm.PackageReason {
82 return match text {
83 'e', 'ex', 'explicit' { alpm.PackageReason.explicit }
84 'd', 'dep', 'depend', 'dependency' { alpm.PackageReason.depend }
85 'u', 'un', 'unknown' { alpm.PackageReason.unknown }
86 else { error('unknown reason: "${text}"') }
87 }
88}
89