vpac / src / remote.v
454 lines · 429 sloc · 14.18 KB · 841315c100c81d9e6b14c4b55a25d4e3d12db762
Raw
1module main
2
3import nspkg
4import os
5
6// a target is a local package file if a file with that name exists,
7// like pacman's -U handling
8fn is_local_pkg_file(path string) bool {
9 return os.exists(path) && os.is_file(path)
10}
11
12// FileTarget is a package archive an install loads from disk: one named on
13// the command line, or one a url fetched into the cache
14struct FileTarget {
15 path string
16 fetched bool
17}
18
19// load_file_target loads an archive at the signature level pacman's -U
20// gives it: the local file level for a file named, the remote file level
21// for one fetched. the load is the only check a file package gets —
22// libalpm's integrity pass at commit skips one loaded from a file as valid
23// already — so a level of 0 here would let a bad signature beside the
24// archive install unread
25fn load_file_target(handle &nspkg.Handle, t FileTarget) !&nspkg.Package {
26 origin := if t.fetched { 'remote' } else { 'local' }
27 got := if t.fetched {
28 handle.option_get_remote_file_siglevel()
29 } else {
30 handle.option_get_local_file_siglevel()
31 }
32 level := got or { return error('could not read the ${origin} file signature level') }
33 return handle.pkg_load(t.path, 1, int(level)) or {
34 return error('failed to load ${t.path}: ${err.msg()}')
35 }
36}
37
38// find_repo_pkg looks a name up in the sync dbs, in order
39fn find_repo_pkg(dbs []&nspkg.DB, name string) ?&nspkg.Package {
40 for db in dbs {
41 if p := db.get_pkg(name) {
42 return p
43 }
44 }
45 return none
46}
47
48// add_pkg_once adds a package unless one with its name is already in the list
49fn add_pkg_once(mut pkgs []&nspkg.Package, pkg &nspkg.Package) {
50 if pkgs.any(it.get_name() == pkg.get_name()) {
51 return
52 }
53 pkgs << pkg
54}
55
56// expand_group adds the member packages of a sync group to the transaction:
57// members already installed at the sync version are skipped, and of the rest
58// the user picks, as pacman's -S asks about a group (ask is false for a
59// --print preview, and once another target failed: nothing installs then,
60// so there is nothing to choose). it answers whether the name was a group at
61// all, and how many members went in. a group with nothing left to install
62// is said, not passed over in silence: it is still a target that resolved
63fn expand_group(mut pkgs []&nspkg.Package, mut ui TransUI, handle &nspkg.Handle, dbs []&nspkg.DB, name string, ask bool) (bool, int) {
64 found, members := sync_group_members(dbs, name)
65 if !found {
66 return false, 0
67 }
68 mut news := []GroupMember{}
69 for m in members {
70 if local := handle.find_local_pkg(m.pkg.get_name()) {
71 if nspkg.pkg_vercmp(local.get_version(), m.pkg.get_version()) >= 0 {
72 continue
73 }
74 }
75 news << m
76 }
77 if news.len == 0 {
78 // --print's output is the list alone
79 if !ui.print_only {
80 ui.pause()
81 report('group ${name}: every member is installed and up to date')
82 }
83 return true, 0
84 }
85 chosen := select_members(mut ui, name, news, ask)
86 for m in chosen {
87 add_pkg_once(mut pkgs, m.pkg)
88 }
89 return true, chosen.len
90}
91
92// skip_up_to_date is --needed: a target installed at the version it would
93// install is left out, with pacman's warning. libalpm's needed flag does the
94// same inside add_pkg, warning through its log; here the targets are left
95// out before they reach the transaction, so its listing never shows them
96fn skip_up_to_date(handle &nspkg.Handle, pkgs []&nspkg.Package) []&nspkg.Package {
97 mut out := []&nspkg.Package{}
98 for pkg in pkgs {
99 if local := handle.find_local_pkg(pkg.get_name()) {
100 if nspkg.pkg_vercmp(local.get_version(), pkg.get_version()) == 0 {
101 warn('${pkg.get_name()}-${pkg.get_version()} is up to date, skipped')
102 continue
103 }
104 }
105 out << pkg
106 }
107 return out
108}
109
110// walk_upgrade_deps adds the outdated dependencies of the targets and
111// everything that hangs off them to the transaction, with libalpm's provider
112// questions answered silently on the way: these walks look the dependencies
113// up only to decide what to upgrade with, and the provider question that
114// counts comes again in the prepare. the walking window is this function's
115// whole body, so its defer is exactly as wide as the walks
116fn walk_upgrade_deps(mut ui TransUI, handle &nspkg.Handle, mut pkgs []&nspkg.Package, mut added map[string]bool) ! {
117 ui.walking = true
118 defer { ui.walking = false }
119 sync_dbs := handle.get_syncdbs()!
120 mut upgrades := []&nspkg.Package{}
121 upgrades << pkgs
122 for pkg in pkgs {
123 for dep in handle.outdated_deps(sync_dbs, pkg) {
124 upgrades << dep
125 }
126 }
127 for p in upgrades {
128 if p.get_name() !in added {
129 handle.add_pkg(p)!
130 added[p.get_name()] = true
131 }
132 }
133 // with nothing installed nothing can break, and the binding's
134 // broken_dependents takes libalpm's NULL for an empty local db as an
135 // error: it is asked only when some package is installed
136 if local_pkgcache(handle)! == unsafe { nil } {
137 return
138 }
139 for extra in handle.broken_dependents(upgrades, sync_dbs)! {
140 if extra.get_name() !in added {
141 handle.add_pkg(extra)!
142 added[extra.get_name()] = true
143 }
144 }
145}
146
147// install_reason is the reason --asdeps or --asexplicit asks for, if any
148fn install_reason(config ConfigInstall) ?nspkg.PackageReason {
149 if config.asdeps {
150 return .depend
151 }
152 if config.asexplicit {
153 return .explicit
154 }
155 return none
156}
157
158// mark_installed gives the installed targets the reason --asdeps or
159// --asexplicit asks for. libalpm's alldeps and allexplicit flags would reach
160// every package of the transaction, the outdated dependencies vpac upgrades
161// along with the targets too, so the reason is set on the targets alone once
162// they are in, under the db lock as pacman's -D does it
163fn mark_installed(handle &nspkg.Handle, names []string, reason nspkg.PackageReason) ! {
164 trans_begin(handle, nspkg.trans_flag_0)!
165 defer { trans_cleanup(handle) }
166 for name in names {
167 pkg := handle.find_local_pkg(name) or { continue }
168 if pkg.get_reason() != reason {
169 pkg.set_reason(reason)!
170 }
171 }
172}
173
174// install_pkgs prepares one transaction for all the packages. with the default
175// it also adds the outdated dependencies of each one, then lists everything the
176// transaction touches and asks before committing. it returns false when the user
177// declines
178fn install_pkgs(handle &nspkg.Handle, mut ui TransUI, mut pkgs []&nspkg.Package, config ConfigInstall) !bool {
179 mut s := ui.spinner(text: 'preparing transaction')
180 s.start()
181 defer { stop_spinner(mut s) }
182
183 mut flags := dep_check_flag(config.nodeps, config.nodeps_level) | print_flags(config.print_only)
184 if config.download_only {
185 flags |= .download_only
186 }
187 if config.dbonly {
188 flags |= .db_only
189 }
190 trans_begin(handle, flags)!
191 mut added := map[string]bool{}
192 for pkg in pkgs {
193 handle.add_pkg(pkg)!
194 added[pkg.get_name()] = true
195 }
196 if !config.no_upgrade_deps {
197 walk_upgrade_deps(mut ui, handle, mut pkgs, mut added)!
198 }
199
200 trans_prepare_checked(handle) or {
201 return error(prepare_error(handle, err.msg(), config.no_upgrade_deps))
202 }
203 // every result print below needs the render thread gone
204 end_frame(mut ui, mut s)
205 // --print stops here: the transaction is listed with the archive names,
206 // and nothing is downloaded or installed
207 if config.print_only {
208 if ipc_on() {
209 mut preview := trans_listing(handle, handle.trans_get_add().pkgs(), .install)
210 preview.preview = true
211 emit(preview)
212 return false
213 }
214 for pkg in handle.trans_get_add().pkgs() {
215 file := pkg.get_filename()
216 println(if file == '' { pkg.get_name() } else { file })
217 }
218 return false
219 }
220 // every transaction is listed and confirmed before it may go: the default
221 // pulls in upgrades beyond the targets, --no-upgrade-deps still installs
222 // the ones it names, and pacman's -S asks in both cases. packages the
223 // transaction removes, such as a conflicting one, are listed too
224 removals := handle.trans_get_remove().pkgs()
225 if removals.len > 0 {
226 ui.show_packages('${removals.len} packages to remove:', removals)
227 }
228 if !trans_confirm(mut ui, handle, handle.trans_get_add().pkgs(), .install) {
229 return false
230 }
231
232 mut commit := ui.spinner(
233 text: if config.download_only {
234 'downloading packages'
235 } else {
236 'installing packages'
237 }
238 )
239 commit.start()
240 defer { stop_spinner(mut commit) }
241 trans_commit(handle)!
242 stop_spinner(mut commit)
243 return true
244}
245
246// install installs the targets. URLs are downloaded first. local files and repo
247// packages go into one transaction, confirmed once. names in neither the repos
248// nor the pkgbuild repo are offered the closest repo name. pkgbuild packages are built one at a
249// time, after that transaction. a target nothing resolves is reported and
250// the rest are still looked up, so every bad one is named in one run; then
251// nothing installs, as pacman's -S stops after its target errors
252fn install(config ConfigInstall) ! {
253 need_query(config.query, 'package')!
254 if config.asdeps && config.asexplicit {
255 return error('--asdeps and --asexplicit contradict each other: pick one')
256 }
257 // a --print preview asks nothing and takes no lock, as pacman's
258 mut ui := new_trans_ui(noconfirm: config.noconfirm || config.print_only)
259 if !config.print_only {
260 check_ask_db_lock(mut ui)
261 }
262
263 // the timeout option is read when the handle is made, so it is set first
264 if config.disable_timeout {
265 cfg_disable_timeout = true
266 }
267 ui.print_only = config.print_only
268 ui.download_only = config.download_only
269 // one handle for the whole install: a url target is fetched through it,
270 // drawing on the same display and asking through the same prompter as
271 // the transaction's own downloads
272 mut handle := init_handle(ui)!
273 defer { handle.release() or { went_on('release the nspkg handle', err) } }
274 defer { ui.stop() }
275 defer { trans_cleanup(handle) }
276 trans_options(config).apply(handle)!
277
278 mut file_targets := []FileTarget{}
279 mut names := []string{}
280 for target in config.query {
281 if is_url(target) {
282 if config.print_only {
283 // a url target is printed as it is; nothing is fetched. the
284 // same message event a build plan goes out as carries this
285 // line on both a terminal and a frontend
286 report('would fetch ${target}')
287 continue
288 }
289 file_targets << FileTarget{
290 path: fetch_target_url(mut ui, handle, target)!
291 fetched: true
292 }
293 } else if is_local_pkg_file(target) {
294 file_targets << FileTarget{
295 path: target
296 }
297 } else {
298 names << target
299 }
300 }
301
302 mut s := ui.spinner(text: 'getting dbs and updating em')
303 s.start()
304 defer { stop_spinner(mut s) }
305
306 dbs := init_repos(mut handle)!
307 if names.len > 0 {
308 check_sync_fresh(mut ui, mut handle)!
309 }
310
311 mut pkgs := []&nspkg.Package{}
312 for f in file_targets {
313 add_pkg_once(mut pkgs, load_file_target(handle, f)!)
314 }
315 mut pkgbuild := []string{}
316 mut missed := 0
317 mut groups := map[string]bool{}
318 for name in names {
319 if config.pkgbuild {
320 pkgbuild << name
321 continue
322 }
323 // repo/name looks in that repo alone, for the package and the group
324 target := parse_target(name)
325 scoped := scope_dbs(dbs, target.repo) or {
326 target_error(err.msg())
327 missed++
328 continue
329 }
330 if p := find_sync_target(handle, scoped, target.name, target.repo != '') {
331 add_pkg_once(mut pkgs, p)
332 continue
333 }
334 // a group named twice is asked about once
335 if groups[name] {
336 continue
337 }
338 // a group installs the members picked of the ones that are not
339 // installed at the sync version already
340 found, _ := expand_group(mut pkgs, mut ui, handle, scoped, target.name, !config.print_only
341 && missed == 0)
342 if found {
343 groups[name] = true
344 continue
345 }
346 // a repo named is the whole search: the pkgbuild repos are no
347 // fallback for it
348 if target.repo != '' {
349 target_error('target not found in ${target.repo}: ${target.name}')
350 missed++
351 continue
352 }
353 pkgbuild << name
354 }
355 stop_spinner(mut s)
356 if config.needed {
357 pkgs = skip_up_to_date(handle, pkgs)
358 }
359
360 mut pkgbuild_builds := []string{}
361 if config.pkgbuild && pkgbuild_repos().len == 0 {
362 return error('no pkgbuild repo in the config: add (repo #:name "aur" #:type "pkgbuild" #:src "https://aur.archlinux.org")')
363 }
364 for name in pkgbuild {
365 if config.pkgbuild {
366 pkgbuild_builds << name
367 continue
368 }
369 // nothing in the repos: the pkgbuild repos are next, and they review
370 // before they build
371 in_the_repos := pkgbuild_has(name) or { return err }
372 if in_the_repos {
373 pkgbuild_builds << name
374 continue
375 }
376 // the pkgbuild db is not the only source: a typo of a package that is
377 // only installed gets its proposal too
378 fixed := did_you_mean(mut ui, name, all_names(handle, dbs)) or {
379 target_error('"${name}" is not in the repos or the pkgbuild repo')
380 missed++
381 continue
382 }
383 // the accepted suggestion goes the same way the typo would have: a
384 // pkgbuild package from its repo, a repo package into the
385 // transaction, and a name nothing carries is the miss it is
386 fixed_found := pkgbuild_has(fixed) or { return err }
387 if fixed_found {
388 pkgbuild_builds << fixed
389 continue
390 }
391 if p := find_repo_pkg(dbs, fixed) {
392 add_pkg_once(mut pkgs, p)
393 } else {
394 target_error('"${fixed}" is not in the repos or the pkgbuild repo')
395 missed++
396 }
397 }
398 // the bad targets are all named by now; pacman installs nothing then
399 if missed > 0 {
400 return
401 }
402
403 if config.download_only && pkgbuild_builds.len > 0 {
404 return error('download-only does not build pkgbuild packages')
405 }
406
407 if pkgs.len == 0 && pkgbuild_builds.len == 0 {
408 // --print's output is the list alone
409 if !config.print_only {
410 report('there is nothing to do')
411 }
412 return
413 }
414
415 if pkgs.len > 0 {
416 // the names are read before the commit, which may free the packages
417 targets := pkgs.map(it.get_name())
418 done := targets.map(name_style(it))
419 committed := install_pkgs(handle, mut ui, mut pkgs, config)!
420 // not the defer's work done twice: the pkgbuild installs below begin
421 // transactions of their own on this handle, so this one is closed
422 // here, and the defer has nothing left to close when it runs
423 trans_cleanup(handle)
424 if !committed {
425 // --print listed the repo part; the build plan is still listed
426 if config.print_only {
427 for name in pkgbuild_builds {
428 report('would build ${name}')
429 }
430 }
431 return
432 }
433 if !config.download_only {
434 if reason := install_reason(config) {
435 mark_installed(handle, targets, reason)!
436 }
437 }
438 print_done(mut ui, if config.download_only {
439 'downloaded:'
440 } else {
441 'installed:'
442 }, done)
443 }
444 // --print lists the build plan and stops
445 if config.print_only {
446 for name in pkgbuild_builds {
447 report('would build ${name}')
448 }
449 return
450 }
451 for target in pkgbuild_builds {
452 pkgbuild_install(mut ui, mut handle, config, target, '')!
453 }
454}
455