nspkg / engine / transaction.v
198 lines · 178 sloc · 5.47 KB · c01b0795d1db56c7bce2539b2c46b110637fdc1a
Raw
1// transaction guards and lock ownership follow pacman trans.c and handle.c.
2// SPDX-License-Identifier: GPL-2.0-or-later
3module engine
4
5import os
6
7#include <fcntl.h>
8#include <unistd.h>
9#include <errno.h>
10
11fn C.open(&char, i32, ...int) i32
12fn C.close(i32) i32
13fn C.unlink(&char) i32
14
15@[flag]
16pub enum TransactionFlag {
17 no_deps = 0
18 no_save = 2
19 no_depversion = 3
20 cascade = 4
21 recurse = 5
22 db_only = 6
23 no_hooks = 7
24 all_deps = 8
25 download_only = 9
26 no_scriptlet = 10
27 no_conflicts = 11
28 needed = 13
29 all_explicit = 14
30 unneeded = 15
31 recurse_all = 16
32 nolock = 17
33}
34
35pub const transaction_flag_0 = TransactionFlag.zero()
36
37pub enum TransactionState {
38 idle
39 initialized
40 prepared
41 downloading
42 committing
43 committed
44 interrupted
45}
46
47struct Transaction {
48 flags TransactionFlag
49mut:
50 state TransactionState = .initialized
51 add []&Package
52 remove []&Package
53 unresolvable []&Package
54 skip_remove []string
55 file_conflicts []FileConflict
56 checking_files bool
57 loading_packages bool
58 invalid_packages []string
59}
60
61fn (mut handle Handle) current_transaction() !&Transaction {
62 handle.check()!
63 if handle.transaction == unsafe { nil } { return handle.fail(.trans_null, '') }
64 return handle.transaction
65}
66
67pub fn (mut handle Handle) start_transaction(flags TransactionFlag) ! {
68 handle.check()!
69 if handle.transaction_active || handle.retrieval_active || handle.download_active {
70 return handle.fail(.trans_not_null, '')
71 }
72 if !flags.has(.nolock) {
73 handle.lock_database()!
74 }
75 handle.transaction = &Transaction{ flags: flags }
76 handle.transaction_active = true
77}
78
79pub fn (mut handle Handle) transaction_flags() !TransactionFlag {
80 trans := handle.current_transaction()!
81 return trans.flags
82}
83
84pub fn (mut handle Handle) transaction_state() !TransactionState {
85 trans := handle.current_transaction()!
86 return trans.state
87}
88
89pub fn (mut handle Handle) transaction_remove() ![]&Package {
90 trans := handle.current_transaction()!
91 return trans.remove.clone()
92}
93
94pub fn (mut handle Handle) transaction_add() ![]&Package {
95 trans := handle.current_transaction()!
96 return trans.add.clone()
97}
98
99// only descriptor operations and scalar stores are allowed in the signal path.
100pub fn (mut handle Handle) unlock_async() int {
101 if voidptr(handle) == unsafe { nil } { return -1 }
102 if handle.lockfd < 0 { return 0 }
103 C.close(handle.lockfd)
104 handle.lockfd = -1
105 if C.unlink(unsafe { &char(handle.lockfile.str) }) != 0 {
106 handle.last_error = .system
107 handle.last_detail_valid = false
108 return -1
109 }
110 return 0
111}
112
113pub fn (mut handle Handle) unlock() ! {
114 if voidptr(handle) == unsafe { nil } { return AlpmError{ kind: .handle_null } }
115 if handle.released { return handle.fail(.handle_null, '') }
116 if handle.unlock_async() < 0 { return handle.error() }
117}
118
119pub fn (mut handle Handle) interrupt_transaction_async() int {
120 if voidptr(handle) == unsafe { nil } { return -1 }
121 if handle.released {
122 handle.last_error = .handle_null
123 handle.last_detail_valid = false
124 return -1
125 }
126 if handle.transaction == unsafe { nil } {
127 handle.last_error = .trans_null
128 handle.last_detail_valid = false
129 return -1
130 }
131 if handle.transaction.state != .committing && handle.transaction.state != .interrupted {
132 handle.last_error = .trans_type
133 handle.last_detail_valid = false
134 return -1
135 }
136 handle.transaction.state = .interrupted
137 return 0
138}
139
140pub fn (mut handle Handle) interrupt_transaction() ! {
141 if handle.interrupt_transaction_async() < 0 { return handle.error() }
142}
143
144pub fn (mut handle Handle) release_transaction() ! {
145 trans := handle.current_transaction()!
146 if handle.execution_active { return handle.fail(.trans_not_null, 'transaction is executing') }
147 if trans.state == .idle { return handle.fail(.trans_null, '') }
148 for pointer in [trans.add, trans.unresolvable] {
149 for pkg_pointer in pointer {
150 mut pkg := unsafe { pkg_pointer }
151 pkg.removes = []&Package{}
152 pkg.old_package = unsafe { nil }
153 }
154 }
155 handle.transaction = unsafe { nil }
156 handle.transaction_active = false
157 if !trans.flags.has(.nolock) && handle.unlock_async() < 0 {
158 handle.diagnostics << 'could not remove lock file ${handle.lockfile}: ${os.last_error().msg()}'
159 }
160}
161
162pub fn (mut handle Handle) remove_package(pkg &Package) ! {
163 handle.check()!
164 if pkg == unsafe { nil } || pkg.origin != .local_db || voidptr(pkg.owner) != voidptr(handle) {
165 return handle.fail(.wrong_args, 'removal target must belong to the local database of this handle')
166 }
167 mut trans := handle.current_transaction()!
168 if trans.state != .initialized { return handle.fail(.trans_not_initialized, '') }
169 if _ := find_package(trans.remove, pkg.name) { return }
170 trans.remove << package_snapshot(pkg)
171}
172
173fn package_snapshot(pointer &Package) &Package {
174 mut pkg := unsafe { pointer }
175 if pkg.origin == .local_db {
176 pkg.load_description() or { pkg.diagnostics << err.msg() }
177 pkg.load_files() or { pkg.diagnostics << err.msg() }
178 pkg.load_scriptlet() or { pkg.diagnostics << err.msg() }
179 }
180 return &Package{
181 ...pkg
182 licenses: pkg.licenses.clone()
183 groups: pkg.groups.clone()
184 depends: pkg.depends.clone()
185 optdepends: pkg.optdepends.clone()
186 makedepends: pkg.makedepends.clone()
187 checkdepends: pkg.checkdepends.clone()
188 conflicts: pkg.conflicts.clone()
189 provides: pkg.provides.clone()
190 replaces: pkg.replaces.clone()
191 files: pkg.files.clone()
192 backup: pkg.backup.clone()
193 xdata: pkg.xdata.clone()
194 diagnostics: pkg.diagnostics.clone()
195 removes: []&Package{}
196 old_package: unsafe { nil }
197 }
198}
199