| 1 | module main |
| 2 | |
| 3 | import alpm |
| 4 | |
| 5 | // local_group_pkgs are the installed members of a group, pacman -R's |
| 6 | // fallback for a target that is no package: the local db's own group |
| 7 | fn local_group_pkgs(handle &alpm.Handle, name string) []&alpm.Package { |
| 8 | localdb := handle.get_localdb() or { return []&alpm.Package{} } |
| 9 | group := localdb.get_group(name) or { return []&alpm.Package{} } |
| 10 | return group.packages.pkgs() |
| 11 | } |
| 12 | |
| 13 | // recurse_flags is pacman's -s: once takes the dependencies the removal |
| 14 | // leaves unneeded, twice (-ss, or --recursive with -s) the ones that were |
| 15 | // installed explicitly too |
| 16 | fn recurse_flags(recursive bool, level int) alpm.TransFlag { |
| 17 | n := level + if recursive { 1 } else { 0 } |
| 18 | return if n >= 2 { |
| 19 | alpm.TransFlag.recurse | .recurse_all |
| 20 | } else if n == 1 { |
| 21 | alpm.TransFlag.recurse |
| 22 | } else { |
| 23 | alpm.trans_flag_0 |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | // remove removes the targets in one transaction, confirmed once |
| 28 | fn remove(config ConfigRemove) ! { |
| 29 | need_query(config.query, 'package')! |
| 30 | // a --print preview asks nothing and takes no lock, as pacman's |
| 31 | mut ui := new_trans_ui(noconfirm: config.noconfirm || config.print_only) |
| 32 | ui.print_only = config.print_only |
| 33 | if !config.print_only { |
| 34 | check_ask_db_lock(mut ui) |
| 35 | } |
| 36 | |
| 37 | mut handle := init_handle(ui)! |
| 38 | defer { handle.release() or { went_on('release the alpm handle', err) } } |
| 39 | defer { ui.stop() } |
| 40 | defer { trans_cleanup(handle) } |
| 41 | TransOptions{ |
| 42 | assume_installed: config.assume_installed |
| 43 | }.apply(handle)! |
| 44 | |
| 45 | mut s := ui.spinner(text: 'preparing removal') |
| 46 | s.start() |
| 47 | defer { stop_spinner(mut s) } |
| 48 | |
| 49 | mut pkgcache := local_pkgcache(handle)! |
| 50 | |
| 51 | // every target is found before the transaction starts, so an unknown |
| 52 | // name removes nothing: each bad one is reported and the rest still |
| 53 | // looked up, and then the run stops, as pacman's -R does. a target is a |
| 54 | // package, what satisfies its version constraint, or a group of the |
| 55 | // local db |
| 56 | mut pkgs := []&alpm.Package{} |
| 57 | mut missed := 0 |
| 58 | for target in config.query { |
| 59 | name := local_target_name(target) |
| 60 | if pkg := find_local_target(pkgcache, name, false) { |
| 61 | add_pkg_once(mut pkgs, pkg) |
| 62 | continue |
| 63 | } |
| 64 | members := local_group_pkgs(handle, name) |
| 65 | if members.len > 0 { |
| 66 | for pkg in members { |
| 67 | add_pkg_once(mut pkgs, pkg) |
| 68 | } |
| 69 | continue |
| 70 | } |
| 71 | stop_spinner(mut s) |
| 72 | fixed := did_you_mean(mut ui, name, pkg_names(pkgcache.pkgs())) or { |
| 73 | target_error(err.msg()) |
| 74 | missed++ |
| 75 | continue |
| 76 | } |
| 77 | pkg := pkgcache.pkg_find(fixed) or { |
| 78 | target_error('unknown package: "${fixed}"') |
| 79 | missed++ |
| 80 | continue |
| 81 | } |
| 82 | add_pkg_once(mut pkgs, pkg) |
| 83 | } |
| 84 | stop_spinner(mut s) |
| 85 | if missed > 0 { |
| 86 | return |
| 87 | } |
| 88 | |
| 89 | // the config can hold a package: refuse before anything is staged |
| 90 | check_hold(pkgs.map(it.get_name()))! |
| 91 | |
| 92 | // recursive takes the dependencies that the removal leaves unneeded too, as |
| 93 | // pacman's -Rs does. they are listed with the packages and confirmed once |
| 94 | mut flags := dep_check_flag(config.nodeps, config.nodeps_level) | print_flags(config.print_only) | recurse_flags(config.recursive, |
| 95 | config.recursive_level) |
| 96 | // pacman's -Ru: a target something still requires is left out by |
| 97 | // libalpm instead of failing the removal |
| 98 | if config.unneeded { |
| 99 | flags |= .unneeded |
| 100 | } |
| 101 | if config.cascade { |
| 102 | flags |= .cascade |
| 103 | } |
| 104 | if config.nosave { |
| 105 | flags |= .no_save |
| 106 | } |
| 107 | if config.dbonly { |
| 108 | flags |= .db_only |
| 109 | } |
| 110 | trans_begin(handle, flags)! |
| 111 | for pkg in pkgs { |
| 112 | handle.remove_pkg(pkg)! |
| 113 | } |
| 114 | |
| 115 | trans_prepare_checked(handle)! |
| 116 | |
| 117 | victims := handle.trans_get_remove().pkgs() |
| 118 | if victims.len == 0 { |
| 119 | // -u left every target where it is needed: pacman's answer, and no |
| 120 | // failure |
| 121 | if config.unneeded { |
| 122 | end_frame(mut ui, mut s) |
| 123 | if !config.print_only { |
| 124 | report('there is nothing to do') |
| 125 | } |
| 126 | return |
| 127 | } |
| 128 | return error('nothing to do') |
| 129 | } |
| 130 | |
| 131 | // --print stops here: what the removal takes is listed the way pacman's |
| 132 | // -Rp lists it, name-version, and nothing is removed |
| 133 | if config.print_only { |
| 134 | end_frame(mut ui, mut s) |
| 135 | if ipc_on() { |
| 136 | mut preview := trans_listing(handle, victims, .remove) |
| 137 | preview.preview = true |
| 138 | emit(preview) |
| 139 | return |
| 140 | } |
| 141 | for pkg in victims { |
| 142 | println('${pkg.get_name()}-${pkg.get_version()}') |
| 143 | } |
| 144 | return |
| 145 | } |
| 146 | |
| 147 | // the transaction is the truth of what goes: recursive and cascade pull |
| 148 | // packages in beyond the targets, and the hold list counts for them too |
| 149 | check_hold(victims.map(it.get_name()))! |
| 150 | |
| 151 | if !trans_confirm(mut ui, handle, victims, .remove) { |
| 152 | return |
| 153 | } |
| 154 | |
| 155 | // the names are read before the commit, which frees the removed packages |
| 156 | // with -u a target may have stayed: the ones that go are the ones named |
| 157 | going := victims.map(it.get_name()) |
| 158 | mut done := []string{} |
| 159 | for pkg in pkgs { |
| 160 | if pkg.get_name() in going { |
| 161 | done << name_style(pkg.get_name()) |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | mut s2 := ui.spinner(text: 'removing packages') |
| 166 | s2.start() |
| 167 | defer { stop_spinner(mut s2) } |
| 168 | trans_commit(handle)! |
| 169 | stop_spinner(mut s2) |
| 170 | |
| 171 | print_done(mut ui, 'removed:', done) |
| 172 | } |
| 173 | |