// behavior referenced to pacman lib/libalpm/alpm.c and handle.c. // SPDX-License-Identifier: GPL-2.0-or-later module engine import os type DatabaseVerifier = fn (mut Handle, string, string, u32) ! pub struct Handle { pub: root string dbpath string lockfile string user u32 pub mut: diagnostics []string mut: action_log_fd i32 = -1 log_callback LogCallback = unsafe { nil } log_context voidptr binding_data voidptr opts Options local &Database = unsafe { nil } sync_dbs []&Database released bool transaction_active bool execution_active bool process_active bool transaction_questions TransactionQuestions commit_callback CommitCallback = unsafe { nil } commit_context voidptr commit_progress_callback CommitProgressCallback = unsafe { nil } commit_progress_context voidptr last_error Errno last_detail string last_detail_valid bool verify_database DatabaseVerifier = verify_database_signature signature_questions SignatureQuestions known_keys map[string]bool signature_initialized bool dependency_questions DependencyQuestions transaction &Transaction = unsafe { nil } lockfd int = -1 preparation_callback PreparationCallback = unsafe { nil } preparation_context voidptr curl_multi voidptr download_callback DownloadCallback = unsafe { nil } download_context voidptr download_active bool download_control C.v_alpm_download_control server_errors map[string]int fetch_callback FetchCallback = unsafe { nil } fetch_context voidptr retrieval_callback RetrievalCallback = unsafe { nil } retrieval_context voidptr retrieval_active bool sandbox_child bool } fn existing_directory(path string) !string { if path == '' || !os.is_dir(path) { return AlpmError{ kind: .not_a_dir, detail: path } } return directory_option(os.real_path(path)) } pub fn initialize(root string, dbpath string) !&Handle { rpath := existing_directory(root)! dpath := existing_directory(dbpath)! mut handle := &Handle{ root: rpath dbpath: dpath lockfile: dpath + 'db.lck' user: u32(C.getuid()) opts: Options{ lists: { .hook_dirs: [rpath + 'usr/share/libalpm/hooks/'] } strings: { .db_extension: '.db' } integers: { .parallel_downloads: i64(1) } } } handle.local = &Database{ name: 'local', path: dpath + 'local/', owner: handle, is_local: true } if !handle.local.get_valid()! { return handle.fail(.db_invalid, 'local') } initialize_locale() return handle } fn (mut handle Handle) require_live() ! { if voidptr(handle) == unsafe { nil } { return AlpmError{ kind: .handle_null } } if handle.released { return handle.fail(.handle_null, 'handle has been released') } } fn (mut handle Handle) check() ! { handle.require_live()! handle.last_error = .ok handle.last_detail = '' handle.last_detail_valid = false } fn (mut handle Handle) fail(kind Errno, detail string) IError { handle.last_error = kind handle.last_detail = detail handle.last_detail_valid = true return AlpmError{ kind: kind, detail: detail } } pub fn (handle &Handle) errno() Errno { return if handle == unsafe { nil } { .handle_null } else { handle.last_error } } pub fn (handle &Handle) error() IError { if handle == unsafe { nil } { return AlpmError{ kind: .handle_null } } return AlpmError{ kind: handle.last_error detail: if handle.last_detail_valid { handle.last_detail } else { '' } } } pub fn (mut handle Handle) release() ! { handle.check()! if handle.process_active { return handle.fail(.trans_not_null, 'package command is active') } if handle.transaction_active { return handle.fail(.trans_not_null, '') } if handle.download_active { return handle.fail(.trans_not_null, 'download is active') } if handle.retrieval_active { return handle.fail(.trans_not_null, 'retrieval is active') } $if nspkg_libcurl ? { if handle.curl_multi != unsafe { nil } { status := C.curl_multi_cleanup(handle.curl_multi) handle.curl_multi = unsafe { nil } if status != 0 { return handle.fail(.libcurl, curl_multi_error(status)) } } } if handle.local != unsafe { nil } { handle.local.detach() } for mut db in handle.sync_dbs { db.detach() } handle.close_action_log() handle.sync_dbs = []&Database{} handle.local = unsafe { nil } handle.released = true handle.binding_data = unsafe { nil } } pub fn (mut handle Handle) local_database() !&Database { handle.check()! if handle.local == unsafe { nil } { return handle.fail(.db_null, 'local') } return handle.local } pub fn (mut handle Handle) sync_databases() ![]&Database { handle.check()! return handle.sync_dbs.clone() } pub fn (mut handle Handle) register_syncdb(name string, siglevel u32) !&Database { handle.check()! treename := c_string_prefix(name) if treename == '' || treename.contains('/') { return handle.fail(.wrong_args, treename) } if handle.transaction_active { return handle.fail(.trans_not_null, '') } if treename == 'local' { return handle.fail(.db_not_null, treename) } for db in handle.sync_dbs { if db.name == treename { return handle.fail(.db_not_null, treename) } } handle.log(.debug, "registering sync database '${treename}'\n") mut db := &Database{ name: treename.clone() path: handle.dbpath + 'sync/' + treename + handle.opts.strings[.db_extension] owner: handle siglevel: siglevel } handle.log(.debug, 'database path for tree ${treename} set to ${db.path}\n') // registration returns the object even if its initial validation fails. db.get_valid() or { db.diagnostics << err.msg() } handle.sync_dbs << db return db } pub fn (mut handle Handle) unregister_syncdbs() ! { handle.check()! if handle.transaction_active { return handle.fail(.trans_not_null, '') } for mut db in handle.sync_dbs { db.detach() } handle.sync_dbs = []&Database{} }