| 1 | @[has_globals] |
| 2 | module main |
| 3 | |
| 4 | import nspkg |
| 5 | import os |
| 6 | import sync.stdatomic |
| 7 | import v.profiler |
| 8 | |
| 9 | fn C._exit(status int) |
| 10 | |
| 11 | // set while this process has an open transaction: txn_locked when it holds |
| 12 | // the db lock, txn_unlocked for a --print preview's, which takes none. it |
| 13 | // makes trans_cleanup release once, however many paths reach it |
| 14 | const txn_open = stdatomic.new_atomic(i64(0)) |
| 15 | |
| 16 | const txn_locked = i64(1) |
| 17 | const txn_unlocked = i64(2) |
| 18 | |
| 19 | // interrupt_handle is the handle a transaction runs on, published for the |
| 20 | // signal handler (as its address) from before nspkg takes the db lock |
| 21 | // until after it let go of it. the handler calls its async unlock, which |
| 22 | // removes the lock only while this handle holds it (its lockfd), as pacman's |
| 23 | // soft_interrupt_handler does: there is no moment where the lock is taken |
| 24 | // and the handler does not know it, and a lock held by someone else — or by |
| 25 | // nobody, a --print preview's transaction — is never touched |
| 26 | const interrupt_handle = stdatomic.new_atomic(u64(0)) |
| 27 | |
| 28 | // split_list reads a repeatable option the way pacman reads its lists: each |
| 29 | // value may hold several, comma separated |
| 30 | fn split_list(values []string) []string { |
| 31 | mut out := []string{} |
| 32 | for value in values { |
| 33 | for item in value.split(',') { |
| 34 | s := item.trim_space() |
| 35 | if s != '' { |
| 36 | out << s |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | return out |
| 41 | } |
| 42 | |
| 43 | // dep_check_flag is pacman's -d: once (or --nodeps) skips the version checks |
| 44 | // of the dependencies, twice (-dd, or --nodeps with -d) skips the |
| 45 | // dependency checks altogether |
| 46 | fn dep_check_flag(nodeps bool, level int) nspkg.TransFlag { |
| 47 | n := level + if nodeps { 1 } else { 0 } |
| 48 | return if n >= 2 { |
| 49 | nspkg.TransFlag.no_deps |
| 50 | } else if n == 1 { |
| 51 | nspkg.TransFlag.no_depversion |
| 52 | } else { |
| 53 | nspkg.trans_flag_0 |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | // TransOptions are the handle options a command line adds for one run, on |
| 58 | // top of the config's: pacman's --overwrite, --ignore, --ignoregroup and |
| 59 | // --assume-installed, every one a repeatable comma separated list |
| 60 | struct TransOptions { |
| 61 | overwrite []string |
| 62 | ignore []string |
| 63 | ignoregroup []string |
| 64 | assume_installed []string |
| 65 | } |
| 66 | |
| 67 | fn (o TransOptions) apply(handle &nspkg.Handle) ! { |
| 68 | for glob in split_list(o.overwrite) { |
| 69 | handle.option_add_overwrite_file(glob)! |
| 70 | } |
| 71 | for name in split_list(o.ignore) { |
| 72 | handle.option_add_ignore_pkg(name)! |
| 73 | } |
| 74 | for group in split_list(o.ignoregroup) { |
| 75 | handle.option_add_ignore_group(group)! |
| 76 | } |
| 77 | for spec in split_list(o.assume_installed) { |
| 78 | // libalpm parses any text into a dependency, '=1' into one with no |
| 79 | // name, which would assume nothing: a spec names its package |
| 80 | dep := nspkg.dep_from_string(spec) |
| 81 | if dep == unsafe { nil } || dep.get_name() == '' { |
| 82 | if dep != unsafe { nil } { |
| 83 | dep.free() |
| 84 | } |
| 85 | return error('--assume-installed: "${spec}" names no package, write it like name=version') |
| 86 | } |
| 87 | // libalpm keeps its own copy, as pacman relies on |
| 88 | handle.option_add_assumeinstalled(dep) or { |
| 89 | dep.free() |
| 90 | return err |
| 91 | } |
| 92 | dep.free() |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | // trans_begin initializes a transaction. pair it with |
| 97 | // `defer { trans_cleanup(handle) }` so every exit path releases it |
| 98 | fn trans_begin(handle &nspkg.Handle, flags nspkg.TransFlag) ! { |
| 99 | // published first: trans_init takes the lock, and a signal right after |
| 100 | // it must find the handle that holds it |
| 101 | publish_for_interrupt(handle) |
| 102 | handle.trans_init(flags) or { |
| 103 | withdraw_from_interrupt() |
| 104 | return err |
| 105 | } |
| 106 | mut open := txn_open |
| 107 | open.store(if flags.has(.nolock) { txn_unlocked } else { txn_locked }) |
| 108 | } |
| 109 | |
| 110 | // publish_for_interrupt makes handle the one the signal handler unlocks |
| 111 | fn publish_for_interrupt(handle &nspkg.Handle) { |
| 112 | mut published := interrupt_handle |
| 113 | published.store(u64(voidptr(handle))) |
| 114 | } |
| 115 | |
| 116 | // withdraw_from_interrupt leaves the signal handler no handle: the lock is |
| 117 | // gone, and the handle may be released next |
| 118 | fn withdraw_from_interrupt() { |
| 119 | mut published := interrupt_handle |
| 120 | published.store(0) |
| 121 | } |
| 122 | |
| 123 | // print_flags are what a --print preview adds to its transaction, as |
| 124 | // pacman's --print does: no db lock, so it needs no root and never waits on |
| 125 | // a running transaction, and no conflict checks, so a preview lists the |
| 126 | // packages even when a conflict question would come up |
| 127 | fn print_flags(print_only bool) nspkg.TransFlag { |
| 128 | return if print_only { |
| 129 | nspkg.TransFlag.nolock | .no_conflicts |
| 130 | } else { |
| 131 | nspkg.trans_flag_0 |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | // trans_cleanup releases an open transaction, which is what removes the db |
| 136 | // lock. like pacman's cleanup: label, it runs on success, decline and error. |
| 137 | // a release that does not go through is said (the lock and the transaction |
| 138 | // state would hang behind it); it still counts as closed, or the defers of |
| 139 | // a rollback would try again |
| 140 | fn trans_cleanup(handle &nspkg.Handle) { |
| 141 | mut open := txn_open |
| 142 | if open.load() != 0 { |
| 143 | handle.trans_release() or { |
| 144 | warn('failed to release the transaction: ${err.msg()}') |
| 145 | } |
| 146 | open.store(0) |
| 147 | } |
| 148 | withdraw_from_interrupt() |
| 149 | } |
| 150 | |
| 151 | // prepare_problem_lines words a failed prepare by what libalpm's errno says |
| 152 | // its problems are, as pacman's sync_prepare_execute tells them apart: a |
| 153 | // missing or broken dependency, a conflict between two packages, a package |
| 154 | // of the wrong architecture, and anything else under libalpm's own text |
| 155 | fn prepare_problem_lines(kind nspkg.Errno, problems []nspkg.DepProblem) []string { |
| 156 | mut lines := []string{} |
| 157 | match kind { |
| 158 | .unsatisfied_deps { |
| 159 | lines << 'could not satisfy dependencies:' |
| 160 | for p in problems { |
| 161 | lines << if p.causing == '' { |
| 162 | " unable to satisfy dependency '${p.depend}' required by ${p.target}" |
| 163 | } else { |
| 164 | " ${p.causing} breaks dependency '${p.depend}' required by ${p.target}" |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | .conflicting_deps { |
| 169 | lines << 'unresolvable package conflicts:' |
| 170 | for p in problems { |
| 171 | // the reason is only said when it is news: not one of the |
| 172 | // two names again |
| 173 | lines << if p.depend == '' || p.depend == p.target || p.depend == p.causing { |
| 174 | ' ${p.target} and ${p.causing} are in conflict' |
| 175 | } else { |
| 176 | ' ${p.target} and ${p.causing} are in conflict (${p.depend})' |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | .pkg_invalid_arch { |
| 181 | lines << '${kind.strerror()}:' |
| 182 | for p in problems { |
| 183 | lines << ' package ${p.target} does not have a valid architecture' |
| 184 | } |
| 185 | } |
| 186 | else { |
| 187 | lines << '${kind.strerror()}:' |
| 188 | for p in problems { |
| 189 | lines << ' ' + [p.target, p.depend, p.causing].filter(it != '').join(' ') |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | return lines |
| 194 | } |
| 195 | |
| 196 | // trans_prepare_checked prepares a transaction. when packages would break it, |
| 197 | // the error lists each problem the way pacman does |
| 198 | fn trans_prepare_checked(handle &nspkg.Handle) ! { |
| 199 | problems, failure := handle.trans_prepare_problems() |
| 200 | if err := failure { |
| 201 | if problems.len == 0 { |
| 202 | return err |
| 203 | } |
| 204 | // copying conflict package names can reset the handle's mutable errno. |
| 205 | kind := unsafe { nspkg.Errno(err.code()) } |
| 206 | return error_with_code(prepare_problem_lines(kind, problems).join('\n'), err.code()) |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | // runs the commit phase of a prepared transaction. the transaction must |
| 211 | // already have been begun and prepared. |
| 212 | fn trans_commit(handle &nspkg.Handle) ! { |
| 213 | handle.trans_commit()! |
| 214 | trans_cleanup(handle) |
| 215 | } |
| 216 | |
| 217 | // on_interrupt is pacman's soft_interrupt_handler: a killed run removes the |
| 218 | // db lock it holds, so it does not wedge the next run. unlock_async decides |
| 219 | // whether it holds one, from the handle's own state, and does only close and |
| 220 | // unlink — async-signal-safe, no allocation. the transaction is not |
| 221 | // interrupted cleanly (no alpm_trans_interrupt here: the run ends at once), |
| 222 | // but the lock is never left behind |
| 223 | fn on_interrupt(sig os.Signal) { |
| 224 | mut published := interrupt_handle |
| 225 | h := published.load() |
| 226 | if h != 0 { |
| 227 | handle := unsafe { &nspkg.Handle(voidptr(h)) } |
| 228 | handle.unlock_async() |
| 229 | } |
| 230 | C._exit(128 + int(sig)) |
| 231 | } |
| 232 | |
| 233 | // install_interrupt_handlers runs after load_config, at which point db_path |
| 234 | // is final: the handler gets the one path it may ever need, cut once here, |
| 235 | // and never allocates |
| 236 | fn install_interrupt_handlers() { |
| 237 | for sig in [os.Signal.int, .hup, .term, .abrt] { |
| 238 | os.signal_opt(sig, on_interrupt) or { went_on('install the handler of signal ${int(sig)}', err) } |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | enum TransAction { |
| 243 | remove |
| 244 | install |
| 245 | upgrade |
| 246 | autoremove |
| 247 | } |
| 248 | |
| 249 | // trans_listing collects what a prepared transaction does: every package, |
| 250 | // with the installed version an install or upgrade replaces, and the sizes |
| 251 | fn trans_listing(handle &nspkg.Handle, pkgs []&nspkg.Package, action TransAction) TransactionEvent { |
| 252 | mut out := []TransPkg{} |
| 253 | mut download := i64(0) |
| 254 | mut install_size := i64(0) |
| 255 | for pkg in pkgs { |
| 256 | name := pkg.get_name() |
| 257 | removing := action == .remove || action == .autoremove |
| 258 | mut old := '' |
| 259 | if !removing { |
| 260 | if installed := handle.find_local_pkg(name) { |
| 261 | old = installed.get_version() |
| 262 | } |
| 263 | } |
| 264 | out << TransPkg{ |
| 265 | name: name |
| 266 | version: pkg.get_version() |
| 267 | old_version: old |
| 268 | download_size: if removing { 0 } else { pkg.download_size() } |
| 269 | installed_size: pkg.get_isize() |
| 270 | } |
| 271 | download += out.last().download_size |
| 272 | install_size += out.last().installed_size |
| 273 | } |
| 274 | return TransactionEvent{ |
| 275 | action: action.str() |
| 276 | packages: out |
| 277 | download_size: download |
| 278 | installed_size: install_size |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | // print_trans_listing shows a transaction the way pkgin does, with the sizes |
| 283 | // for anything that downloads |
| 284 | fn print_trans_listing(t TransactionEvent) { |
| 285 | header := match t.action { |
| 286 | 'remove' { 'packages to delete' } |
| 287 | 'install' { 'packages to be installed' } |
| 288 | 'upgrade' { 'packages to be upgraded' } |
| 289 | else { 'packages to be autoremoved' } |
| 290 | } |
| 291 | removing := t.action in ['remove', 'autoremove'] |
| 292 | mut rows := []Row{} |
| 293 | for p in t.packages { |
| 294 | if removing { |
| 295 | rows << row(name_style(p.name), version_style(p.version)) |
| 296 | } else if p.old_version != '' { |
| 297 | // old version, arrow, new version: a new dependency has no old one |
| 298 | rows << row(name_style(p.name), old_style(p.old_version), '->', version_style(p.version)) |
| 299 | } else { |
| 300 | rows << row(name_style(p.name), '', '', version_style(p.version)) |
| 301 | } |
| 302 | } |
| 303 | println(heading('${t.packages.len} ${header}:')) |
| 304 | println(table_text(rows, ' ', ' ')) |
| 305 | if !removing { |
| 306 | sizes := [ |
| 307 | row('to download', profiler.format_bytes(u64(t.download_size))), |
| 308 | row('to install', profiler.format_bytes(u64(t.installed_size))), |
| 309 | ] |
| 310 | println(table_text(sizes, ' ', ' ')) |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | // trans_confirm lists a prepared transaction and asks 'proceed? [Y/n]', |
| 315 | // where enter is yes, as pacman's 'Proceed with installation?'. a frontend |
| 316 | // gets the listing as a transaction event before the question |
| 317 | fn trans_confirm(mut ui TransUI, handle &nspkg.Handle, pkgs []&nspkg.Package, action TransAction) bool { |
| 318 | ui.pause() |
| 319 | listing := trans_listing(handle, pkgs, action) |
| 320 | if ipc_on() { |
| 321 | emit(listing) |
| 322 | } else { |
| 323 | print_trans_listing(listing) |
| 324 | } |
| 325 | return ui.proceed('proceed?') |
| 326 | } |
| 327 | |