vpac / src / upgrade.v
281 lines · 260 sloc · 9.23 KB · 841315c100c81d9e6b14c4b55a25d4e3d12db762
Raw
1import nspkg
2
3fn upgrade(config ConfigUpgrade) ! {
4 // a build installs what it makes: download-only has nothing a pkgbuild
5 // upgrade serves, as install's says
6 if config.pkgbuild && config.download_only {
7 return error('download-only does not build pkgbuild packages')
8 }
9 // the pkgbuild pass asks about every candidate one by one, and a build
10 // runs what a PKGBUILD says: --noconfirm would skip that verification, so
11 // the refusal comes before any of the run's work, as install's does
12 if config.pkgbuild && config.noconfirm && !config.no_pkgbuild_confirm {
13 return error('the pkgbuild upgrades are asked about: drop --noconfirm, or pass --nopkgbuildconfirm')
14 }
15 // a --print preview asks nothing and takes no lock, as pacman's
16 mut ui := new_trans_ui(noconfirm: config.noconfirm || config.print_only)
17 ui.print_only = config.print_only
18 if !config.print_only {
19 check_ask_db_lock(mut ui)
20 }
21
22 if config.disable_timeout {
23 cfg_disable_timeout = true
24 }
25 mut handle := init_handle(ui)!
26 defer { handle.release() or { went_on('release the nspkg handle', err) } }
27 defer { ui.stop() }
28 defer { trans_cleanup(handle) }
29 trans_options(config).apply(handle)!
30
31 mut s := ui.spinner(text: 'getting dbs and updating em')
32 s.start()
33 defer { stop_spinner(mut s) }
34
35 init_repos(mut handle)!
36 if config.sync_dbs {
37 update_repos(mut handle, false)!
38 } else {
39 check_sync_fresh(mut ui, mut handle)!
40 }
41
42 // a fresh one shows in either case: the set_text would land on the old
43 // spinner's frame when the prompt above paused it, and here the old one
44 // is finished with its work anyway
45 stop_spinner(mut s)
46 mut look := ui.spinner(text: 'looking for upgrades')
47 look.start()
48 defer { stop_spinner(mut look) }
49 mut flags := dep_check_flag(config.nodeps, config.nodeps_level) | print_flags(config.print_only)
50 if config.download_only {
51 flags |= .download_only
52 }
53 if config.dbonly {
54 flags |= .db_only
55 }
56 trans_begin(handle, flags)!
57 // pacman's -Suu: a local package newer than the sync one goes down to it
58 handle.sync_sysupgrade(config.downgrade || config.sysupgrade_level >= 2)!
59
60 trans_prepare_checked(handle)!
61
62 targets := handle.trans_get_add().pkgs()
63
64 end_frame(mut ui, mut look)
65
66 // the sync part's names, the pkgbuild pass's added to them
67 mut upgraded := []string{}
68
69 if targets.len == 0 {
70 // --pkgbuild may still have work; without it, this is the end
71 if !config.print_only && !config.pkgbuild {
72 report('nothing to upgrade.')
73 return
74 }
75 } else {
76 // --print stops here: the upgrade is listed with the archive names, as
77 // install's --print lists a transaction, and nothing is downloaded
78 if config.print_only {
79 if ipc_on() {
80 mut preview := trans_listing(handle, targets, .upgrade)
81 preview.preview = true
82 emit(preview)
83 return
84 }
85 for pkg in targets {
86 file := pkg.get_filename()
87 println(if file == '' { pkg.get_name() } else { file })
88 }
89 // --pkgbuild lists what the pass would offer, as install's
90 // --print lists what it would build
91 if config.pkgbuild {
92 for u in pkgbuild_upgrade_candidates(mut ui, pkgbuild_repos(), foreign_pkgs(handle)!)! {
93 report('would upgrade ${u.info.name} from the pkgbuild repo (${u.repo}) (${u.local} -> ${u.info.version})')
94 }
95 }
96 return
97 }
98
99 // what the upgrade removes on the way — a replaced or conflicting
100 // package — is listed before the question, as install lists its own
101 removals := handle.trans_get_remove().pkgs()
102 if removals.len > 0 {
103 ui.show_packages('${removals.len} packages to remove:', removals)
104 }
105 if !trans_confirm(mut ui, handle, targets, .upgrade) {
106 return
107 }
108
109 mut s2 := ui.spinner(
110 text: if config.download_only { 'downloading the upgrades' } else { 'upgrading system' }
111 )
112 s2.start()
113 defer { stop_spinner(mut s2) }
114 trans_commit(handle)!
115 stop_spinner(mut s2)
116 upgraded = targets.map(it.get_name())
117 }
118
119 // --pkgbuild: the locally installed packages the repos do not carry are
120 // the pkgbuild repos' to upgrade, asked about one by one. a --print
121 // preview asks nothing and said its list already
122 if config.pkgbuild && !config.print_only {
123 upgraded << upgrade_pkgbuilds(mut ui, mut handle, config)!
124 }
125
126 if upgraded.len == 0 {
127 return
128 }
129
130 // a frontend learns what changed as from every transaction: a done event
131 if ipc_on() {
132 emit(DoneEvent{
133 action: if config.download_only { 'downloaded' } else { 'upgraded' }
134 packages: upgraded
135 })
136 return
137 }
138 ui.say(if config.download_only { 'upgrades downloaded' } else { 'system upgraded' })
139}
140
141// foreign_pkgs lists the locally installed packages the sync dbs do not
142// carry, the held and ignored ones left out: the sync upgrade just did its
143// part around them, and the pkgbuild repos are what could have the rest
144fn foreign_pkgs(handle &nspkg.Handle) ![]&nspkg.Package {
145 sync := handle.get_syncdbs()!
146 dbs := sync.dbs()
147 localdb := handle.get_localdb() or {
148 return error('could not open the local database: ${err.msg()}')
149 }
150 mut out := []&nspkg.Package{}
151 cache := pkgcache_of(handle, localdb)!
152 for pkg in cache.pkgs() {
153 if find_repo_pkg(dbs, pkg.get_name()) == none && handle.pkg_should_ignore(pkg) == 0 {
154 out << pkg
155 }
156 }
157 return out
158}
159
160// PkgbuildUpgrade is one locally installed package a pkgbuild repo would
161// upgrade: the record it would build from, the repo's name, and the version
162// installed now
163struct PkgbuildUpgrade {
164 info PackagePkgbuild
165 repo string
166 local string
167}
168
169// foreign_upgrades keeps the foreign packages a record would upgrade: the
170// record's version must come after the installed one — never a downgrade.
171// it answers the records, each with the version it would upgrade from
172fn foreign_upgrades(foreign []&nspkg.Package, found map[string]PackagePkgbuild) []PkgbuildUpgrade {
173 mut out := []PkgbuildUpgrade{}
174 for pkg in foreign {
175 rec := found[pkg.get_name()] or { continue }
176 if nspkg.pkg_vercmp(pkg.get_version(), rec.version) >= 0 {
177 continue
178 }
179 out << PkgbuildUpgrade{
180 info: rec
181 local: pkg.get_version()
182 }
183 }
184 return out
185}
186
187// pkgbuild_upgrade_candidates asks the pkgbuild repos after the foreign
188// names, batched per repo in the config's order — the first repo that holds
189// a name is the one it would build from, as pkgbuild_lookup tries them one
190// name at a time — and keeps the records that would upgrade
191fn pkgbuild_upgrade_candidates(mut ui TransUI, repos []CfgRepo, foreign []&nspkg.Package) ![]PkgbuildUpgrade {
192 mut left := foreign.map(it.get_name())
193 mut found := map[string]PackagePkgbuild{}
194 mut repo_of := map[string]string{}
195 mut s := ui.spinner(text: 'looking for pkgbuild upgrades')
196 s.start()
197 for repo in repos {
198 site := repo.servers[0].trim_right('/')
199 infos := get_pkgbuild_info(site, left) or {
200 stop_spinner(mut s)
201 return err
202 }
203 mut answered := []string{}
204 for info in infos {
205 found[info.name] = info
206 repo_of[info.name] = repo.name
207 answered << info.name
208 }
209 left = left.filter(it !in answered)
210 if left.len == 0 {
211 break
212 }
213 }
214 stop_spinner(mut s)
215 mut ups := []PkgbuildUpgrade{}
216 for u in foreign_upgrades(foreign, found) {
217 ups << PkgbuildUpgrade{
218 ...u
219 repo: repo_of[u.info.name]
220 }
221 }
222 return ups
223}
224
225// upgrade_pkgbuild_config is the install a pkgbuild upgrade runs as: the
226// questions are review_and_build's own, the gate worded by the pass with the
227// versions, and what the upgrade said of its transaction — the ignores, the
228// overwrites, the dependency checks, the timeout — holds for it
229fn upgrade_pkgbuild_config(config ConfigUpgrade) ConfigInstall {
230 return ConfigInstall{
231 pkgbuild: true
232 no_pkgbuild_confirm: config.no_pkgbuild_confirm
233 noconfirm: config.noconfirm
234 nodeps: config.nodeps
235 nodeps_level: config.nodeps_level
236 overwrite: config.overwrite
237 assume_installed: config.assume_installed
238 ignore: config.ignore
239 ignoregroup: config.ignoregroup
240 disable_timeout: config.disable_timeout
241 }
242}
243
244// upgrade_pkgbuilds offers the candidates one by one, each asked the way a
245// pkgbuild install asks — the upgrade as the gate, then the review, then the
246// build — and builds and installs the accepted ones. a declined package
247// stops the pass, as a declined build stops an install. it answers the names
248// that were upgraded
249fn upgrade_pkgbuilds(mut ui TransUI, mut handle &nspkg.Handle, config ConfigUpgrade) ![]string {
250 repos := pkgbuild_repos()
251 if repos.len == 0 {
252 warn('no pkgbuild repo in the config: the pkgbuild pass has nothing to query')
253 return []
254 }
255 candidates := pkgbuild_upgrade_candidates(mut ui, repos, foreign_pkgs(handle)!)!
256 if candidates.len == 0 {
257 return []
258 }
259 mut upgraded := []string{}
260 for u in candidates {
261 gate := 'upgrade ${u.info.name} from the pkgbuild repo (${u.repo}) (${u.local} -> ${u.info.version})?'
262 pkgbuild_install(mut ui, mut handle, upgrade_pkgbuild_config(config), u.info.name, gate) or {
263 return err
264 }
265 // the local db tells a built package from a declined one: a decline
266 // is a stop, the way a declined build stops an install
267 view, installed := local_now()!
268 built := if local := installed.pkg_find(u.info.name) {
269 nspkg.pkg_vercmp(local.get_version(), u.info.version) >= 0
270 } else {
271 false
272 }
273 view.release() or { went_on('release the nspkg handle', err) }
274 if !built {
275 advise('${u.info.name} was declined, the pass stops')
276 return upgraded
277 }
278 upgraded << u.info.name
279 }
280 return upgraded
281}
282