vpac / src / upgrade.v
106 lines · 94 sloc · 2.96 KB · 3609dfbd82867dc1034a443f8f9ced529ddbbedd
Raw
1module main
2
3fn upgrade(config ConfigUpgrade) ! {
4 // a --print preview asks nothing and takes no lock, as pacman's
5 mut ui := new_trans_ui(noconfirm: config.noconfirm || config.print_only)
6 ui.print_only = config.print_only
7 if !config.print_only {
8 check_ask_db_lock(mut ui)
9 }
10
11 if config.disable_timeout {
12 cfg_disable_timeout = true
13 }
14 mut handle := init_handle(ui)!
15 defer { handle.release() or { went_on('release the alpm handle', err) } }
16 defer { ui.stop() }
17 defer { trans_cleanup(handle) }
18 trans_options(config).apply(handle)!
19
20 mut s := ui.spinner(text: 'getting dbs and updating em')
21 s.start()
22 defer { stop_spinner(mut s) }
23
24 init_repos(mut handle)!
25 if config.sync_dbs {
26 update_repos(mut handle, false)!
27 } else {
28 check_sync_fresh(mut ui, mut handle)!
29 }
30
31 // a fresh one shows in either case: the set_text would land on the old
32 // spinner's frame when the prompt above paused it, and here the old one
33 // is finished with its work anyway
34 stop_spinner(mut s)
35 mut look := ui.spinner(text: 'looking for upgrades')
36 look.start()
37 defer { stop_spinner(mut look) }
38 mut flags := dep_check_flag(config.nodeps, config.nodeps_level) | print_flags(config.print_only)
39 if config.download_only {
40 flags |= .download_only
41 }
42 if config.dbonly {
43 flags |= .db_only
44 }
45 trans_begin(handle, flags)!
46 // pacman's -Suu: a local package newer than the sync one goes down to it
47 handle.sync_sysupgrade(config.downgrade || config.sysupgrade_level >= 2)!
48
49 trans_prepare_checked(handle)!
50
51 targets := handle.trans_get_add().pkgs()
52
53 end_frame(mut ui, mut look)
54
55 if targets.len == 0 {
56 // --print's output is the list alone
57 if !config.print_only {
58 report('nothing to upgrade.')
59 }
60 return
61 }
62
63 // --print stops here: the upgrade is listed with the archive names, as
64 // install's --print lists a transaction, and nothing is downloaded
65 if config.print_only {
66 if ipc_on() {
67 mut preview := trans_listing(handle, targets, .upgrade)
68 preview.preview = true
69 emit(preview)
70 return
71 }
72 for pkg in targets {
73 file := pkg.get_filename()
74 println(if file == '' { pkg.get_name() } else { file })
75 }
76 return
77 }
78
79 // what the upgrade removes on the way — a replaced or conflicting
80 // package — is listed before the question, as install lists its own
81 removals := handle.trans_get_remove().pkgs()
82 if removals.len > 0 {
83 ui.show_packages('${removals.len} packages to remove:', removals)
84 }
85 if !trans_confirm(mut ui, handle, targets, .upgrade) {
86 return
87 }
88 names := targets.map(it.get_name())
89
90 mut s2 := ui.spinner(
91 text: if config.download_only { 'downloading the upgrades' } else { 'upgrading system' }
92 )
93 s2.start()
94 defer { stop_spinner(mut s2) }
95 trans_commit(handle)!
96 stop_spinner(mut s2)
97 // a frontend learns what changed as from every transaction: a done event
98 if ipc_on() {
99 emit(DoneEvent{
100 action: if config.download_only { 'downloaded' } else { 'upgraded' }
101 packages: names
102 })
103 return
104 }
105 ui.say(if config.download_only { 'upgrades downloaded' } else { 'system upgraded' })
106}
107