// transaction guards and lock ownership follow pacman trans.c and handle.c. // SPDX-License-Identifier: GPL-2.0-or-later module engine import os #include #include #include fn C.open(&char, i32, ...int) i32 fn C.close(i32) i32 fn C.unlink(&char) i32 @[flag] pub enum TransactionFlag { no_deps = 0 no_save = 2 no_depversion = 3 cascade = 4 recurse = 5 db_only = 6 no_hooks = 7 all_deps = 8 download_only = 9 no_scriptlet = 10 no_conflicts = 11 needed = 13 all_explicit = 14 unneeded = 15 recurse_all = 16 nolock = 17 } pub const transaction_flag_0 = TransactionFlag.zero() pub enum TransactionState { idle initialized prepared downloading committing committed interrupted } struct Transaction { flags TransactionFlag mut: state TransactionState = .initialized add []&Package remove []&Package unresolvable []&Package skip_remove []string file_conflicts []FileConflict checking_files bool loading_packages bool invalid_packages []string } fn (mut handle Handle) current_transaction() !&Transaction { handle.check()! if handle.transaction == unsafe { nil } { return handle.fail(.trans_null, '') } return handle.transaction } pub fn (mut handle Handle) start_transaction(flags TransactionFlag) ! { handle.check()! if handle.transaction_active || handle.retrieval_active || handle.download_active { return handle.fail(.trans_not_null, '') } if !flags.has(.nolock) { handle.lock_database()! } handle.transaction = &Transaction{ flags: flags } handle.transaction_active = true } pub fn (mut handle Handle) transaction_flags() !TransactionFlag { trans := handle.current_transaction()! return trans.flags } pub fn (mut handle Handle) transaction_state() !TransactionState { trans := handle.current_transaction()! return trans.state } pub fn (mut handle Handle) transaction_remove() ![]&Package { trans := handle.current_transaction()! return trans.remove.clone() } pub fn (mut handle Handle) transaction_add() ![]&Package { trans := handle.current_transaction()! return trans.add.clone() } // only descriptor operations and scalar stores are allowed in the signal path. pub fn (mut handle Handle) unlock_async() int { if voidptr(handle) == unsafe { nil } { return -1 } if handle.lockfd < 0 { return 0 } C.close(handle.lockfd) handle.lockfd = -1 if C.unlink(unsafe { &char(handle.lockfile.str) }) != 0 { handle.last_error = .system handle.last_detail_valid = false return -1 } return 0 } pub fn (mut handle Handle) unlock() ! { if voidptr(handle) == unsafe { nil } { return AlpmError{ kind: .handle_null } } if handle.released { return handle.fail(.handle_null, '') } if handle.unlock_async() < 0 { return handle.error() } } pub fn (mut handle Handle) interrupt_transaction_async() int { if voidptr(handle) == unsafe { nil } { return -1 } if handle.released { handle.last_error = .handle_null handle.last_detail_valid = false return -1 } if handle.transaction == unsafe { nil } { handle.last_error = .trans_null handle.last_detail_valid = false return -1 } if handle.transaction.state != .committing && handle.transaction.state != .interrupted { handle.last_error = .trans_type handle.last_detail_valid = false return -1 } handle.transaction.state = .interrupted return 0 } pub fn (mut handle Handle) interrupt_transaction() ! { if handle.interrupt_transaction_async() < 0 { return handle.error() } } pub fn (mut handle Handle) release_transaction() ! { trans := handle.current_transaction()! if handle.execution_active { return handle.fail(.trans_not_null, 'transaction is executing') } if trans.state == .idle { return handle.fail(.trans_null, '') } for pointer in [trans.add, trans.unresolvable] { for pkg_pointer in pointer { mut pkg := unsafe { pkg_pointer } pkg.removes = []&Package{} pkg.old_package = unsafe { nil } } } handle.transaction = unsafe { nil } handle.transaction_active = false if !trans.flags.has(.nolock) && handle.unlock_async() < 0 { handle.diagnostics << 'could not remove lock file ${handle.lockfile}: ${os.last_error().msg()}' } } pub fn (mut handle Handle) remove_package(pkg &Package) ! { handle.check()! if pkg == unsafe { nil } || pkg.origin != .local_db || voidptr(pkg.owner) != voidptr(handle) { return handle.fail(.wrong_args, 'removal target must belong to the local database of this handle') } mut trans := handle.current_transaction()! if trans.state != .initialized { return handle.fail(.trans_not_initialized, '') } if _ := find_package(trans.remove, pkg.name) { return } trans.remove << package_snapshot(pkg) } fn package_snapshot(pointer &Package) &Package { mut pkg := unsafe { pointer } if pkg.origin == .local_db { pkg.load_description() or { pkg.diagnostics << err.msg() } pkg.load_files() or { pkg.diagnostics << err.msg() } pkg.load_scriptlet() or { pkg.diagnostics << err.msg() } } return &Package{ ...pkg licenses: pkg.licenses.clone() groups: pkg.groups.clone() depends: pkg.depends.clone() optdepends: pkg.optdepends.clone() makedepends: pkg.makedepends.clone() checkdepends: pkg.checkdepends.clone() conflicts: pkg.conflicts.clone() provides: pkg.provides.clone() replaces: pkg.replaces.clone() files: pkg.files.clone() backup: pkg.backup.clone() xdata: pkg.xdata.clone() diagnostics: pkg.diagnostics.clone() removes: []&Package{} old_package: unsafe { nil } } }