nspkg / engine / transaction_commit.v
187 lines · 175 sloc · 5.96 KB · c01b0795d1db56c7bce2539b2c46b110637fdc1a
Raw
1// transaction lifecycle and extension ordering follow trans.c and remove.c.
2// SPDX-License-Identifier: GPL-2.0-or-later
3module engine
4
5pub enum CommitEventKind {
6 transaction_start
7 transaction_done
8 package_remove_start
9 package_remove_done
10 pacsave_created
11 scriptlet_info
12 hook_start
13 hook_done
14 hook_run_start
15 hook_run_done
16 file_conflicts_start
17 file_conflicts_done
18 keyring_start
19 keyring_done
20 key_download_start
21 key_download_done
22 integrity_start
23 integrity_done
24 load_start
25 load_done
26 package_install_start
27 package_install_done
28 pacnew_created
29 diskspace_start
30 diskspace_done
31}
32
33pub enum PackageOperation {
34 install = 1
35 upgrade
36 reinstall
37 downgrade
38 remove
39}
40
41pub enum CommitProgressKind {
42 remove
43 add
44 upgrade
45 downgrade
46 reinstall
47 file_conflicts
48 integrity
49 load
50 keyring
51 diskspace
52}
53
54pub struct CommitEvent {
55pub:
56 kind CommitEventKind
57 old_package &Package = unsafe { nil }
58 new_package &Package = unsafe { nil }
59 file string
60 line string
61 when HookWhen
62 name string
63 description string
64 has_description bool
65 position int
66 total int
67 operation PackageOperation
68 from_no_upgrade bool
69}
70
71pub struct CommitProgress {
72pub:
73 kind CommitProgressKind
74 package string
75 percent int
76 total int
77 current int
78}
79
80pub type CommitCallback = fn (voidptr, CommitEvent)
81pub type CommitProgressCallback = fn (voidptr, CommitProgress)
82
83pub fn (mut handle Handle) set_commit_callback(callback CommitCallback, context voidptr) ! {
84 handle.check()!
85 handle.commit_callback = callback
86 handle.commit_context = context
87}
88
89pub fn (mut handle Handle) set_commit_progress_callback(callback CommitProgressCallback, context voidptr) ! {
90 handle.check()!
91 handle.commit_progress_callback = callback
92 handle.commit_progress_context = context
93}
94
95fn (handle &Handle) commit_event(event CommitEvent) {
96 if voidptr(handle.commit_callback) != unsafe { nil } {
97 handle.commit_callback(handle.commit_context, event)
98 }
99}
100
101fn (handle &Handle) commit_progress(package string, percent int, total int, current int) {
102 handle.commit_progress_for(.remove, package, percent, total, current)
103}
104
105fn (handle &Handle) commit_progress_for(kind CommitProgressKind, package string, percent int, total int, current int) {
106 if voidptr(handle.commit_progress_callback) != unsafe { nil } {
107 handle.commit_progress_callback(handle.commit_progress_context, CommitProgress{ kind: kind, package: package, percent: percent, total: total, current: current })
108 }
109}
110
111pub fn (mut handle Handle) commit_transaction() ! {
112 mut trans := handle.current_transaction()!
113 if trans.state != .prepared { return handle.fail(.trans_not_prepared, '') }
114 if trans.flags.has(.nolock) || handle.lockfd < 0 { return handle.fail(.trans_not_locked, '') }
115 if trans.add.len == 0 && trans.remove.len == 0 { return }
116 if handle.execution_active { return handle.fail(.trans_not_null, 'transaction is executing') }
117 handle.execution_active = true
118 defer { handle.execution_active = false }
119 if trans.add.len > 0 {
120 loaded := handle.load_transaction_packages()!
121 if !loaded.success { return handle.fail(loaded.kind, '') }
122 if trans.flags.has(.download_only) { return }
123 handle.complete_file_targets(mut trans)!
124 result := handle.check_file_conflicts()!
125 if !result.success { return handle.fail(.file_conflicts, '') }
126 }
127 mut root := handle.open_root_filesystem()!
128 defer { root.close() }
129 if trans.add.len > 0 && handle.opts.integers[.check_space] != 0 && !trans.flags.has(.db_only) {
130 handle.commit_event(CommitEvent{ kind: .diskspace_start })
131 handle.check_install_space(mut root, trans)!
132 handle.commit_event(CommitEvent{ kind: .diskspace_done })
133 }
134 for pkg in trans.remove {
135 if !trans.flags.has(.db_only) {
136 if pkg.load_error != '' { return handle.fail(.db_invalid, pkg.load_error) }
137 for file in pkg.files {
138 validate_package_path(file.name) or { return handle.fail(.pkg_cant_remove, err.msg()) }
139 }
140 }
141 }
142 if !trans.flags.has(.no_hooks) {
143 handle.run_hooks(mut root, trans, .pre_transaction) or { return handle.fail(.trans_hook_failed, err.msg()) }
144 }
145 trans.state = .committing
146 handle.record_action('transaction started\n')
147 mut completed_transaction := false
148 defer { if !completed_transaction { handle.record_failed_transaction() } }
149 mut completed_packages := 0
150 handle.commit_event(CommitEvent{ kind: .transaction_start })
151 for index, pkg in trans.remove {
152 if trans.state == .interrupted { break }
153 handle.commit_event(CommitEvent{ kind: .package_remove_start, operation: .remove, old_package: pkg })
154 if !trans.flags.has(.no_scriptlet) && pkg.scriptlet {
155 handle.run_package_scriptlet(mut root, pkg, 'pre_remove', pkg.version, none) or { handle.diagnostics << 'pre_remove ${pkg.name}: ${err.msg()}' }
156 }
157 if !trans.flags.has(.db_only) {
158 handle.remove_package_files(mut root, pkg, unsafe { nil }, index + 1, trans.remove.len)!
159 }
160 handle.record_action('removed ${pkg.name} (${pkg.version})\n')
161 if !trans.flags.has(.no_scriptlet) && pkg.scriptlet {
162 handle.run_package_scriptlet(mut root, pkg, 'post_remove', pkg.version, none) or { handle.diagnostics << 'post_remove ${pkg.name}: ${err.msg()}' }
163 }
164 handle.commit_event(CommitEvent{ kind: .package_remove_done, operation: .remove, old_package: pkg })
165 handle.remove_installed_record(pkg)!
166 completed_packages++
167 }
168 if trans.add.len > 0 {
169 handle.execute_install_packages(mut root, mut trans) or {
170 handle.log(.error, localized_message('could not commit transaction\n'))
171 return err
172 }
173 } else if completed_packages == trans.remove.len {
174 handle.run_ldconfig(mut root) or { handle.diagnostics << 'ldconfig: ${err.msg()}' }
175 }
176 if trans.state == .interrupted {
177 handle.record_action('transaction interrupted\n')
178 } else {
179 handle.commit_event(CommitEvent{ kind: .transaction_done })
180 handle.record_action('transaction completed\n')
181 if !trans.flags.has(.no_hooks) {
182 handle.run_hooks(mut root, trans, .post_transaction) or { handle.diagnostics << 'post-transaction hooks: ${err.msg()}' }
183 }
184 }
185 trans.state = .committed
186 completed_transaction = true
187}
188