| 1 | // behavior referenced to pacman lib/libalpm/alpm.c and handle.c. |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | module engine |
| 4 | |
| 5 | import os |
| 6 | |
| 7 | type DatabaseVerifier = fn (mut Handle, string, string, u32) ! |
| 8 | |
| 9 | pub struct Handle { |
| 10 | pub: |
| 11 | root string |
| 12 | dbpath string |
| 13 | lockfile string |
| 14 | user u32 |
| 15 | pub mut: |
| 16 | diagnostics []string |
| 17 | mut: |
| 18 | action_log_fd i32 = -1 |
| 19 | log_callback LogCallback = unsafe { nil } |
| 20 | log_context voidptr |
| 21 | binding_data voidptr |
| 22 | opts Options |
| 23 | local &Database = unsafe { nil } |
| 24 | sync_dbs []&Database |
| 25 | released bool |
| 26 | transaction_active bool |
| 27 | execution_active bool |
| 28 | process_active bool |
| 29 | transaction_questions TransactionQuestions |
| 30 | commit_callback CommitCallback = unsafe { nil } |
| 31 | commit_context voidptr |
| 32 | commit_progress_callback CommitProgressCallback = unsafe { nil } |
| 33 | commit_progress_context voidptr |
| 34 | last_error Errno |
| 35 | last_detail string |
| 36 | last_detail_valid bool |
| 37 | verify_database DatabaseVerifier = verify_database_signature |
| 38 | signature_questions SignatureQuestions |
| 39 | known_keys map[string]bool |
| 40 | signature_initialized bool |
| 41 | dependency_questions DependencyQuestions |
| 42 | transaction &Transaction = unsafe { nil } |
| 43 | lockfd int = -1 |
| 44 | preparation_callback PreparationCallback = unsafe { nil } |
| 45 | preparation_context voidptr |
| 46 | curl_multi voidptr |
| 47 | download_callback DownloadCallback = unsafe { nil } |
| 48 | download_context voidptr |
| 49 | download_active bool |
| 50 | download_control C.v_alpm_download_control |
| 51 | server_errors map[string]int |
| 52 | fetch_callback FetchCallback = unsafe { nil } |
| 53 | fetch_context voidptr |
| 54 | retrieval_callback RetrievalCallback = unsafe { nil } |
| 55 | retrieval_context voidptr |
| 56 | retrieval_active bool |
| 57 | sandbox_child bool |
| 58 | } |
| 59 | |
| 60 | fn existing_directory(path string) !string { |
| 61 | if path == '' || !os.is_dir(path) { return AlpmError{ kind: .not_a_dir, detail: path } } |
| 62 | return directory_option(os.real_path(path)) |
| 63 | } |
| 64 | |
| 65 | pub fn initialize(root string, dbpath string) !&Handle { |
| 66 | rpath := existing_directory(root)! |
| 67 | dpath := existing_directory(dbpath)! |
| 68 | mut handle := &Handle{ |
| 69 | root: rpath |
| 70 | dbpath: dpath |
| 71 | lockfile: dpath + 'db.lck' |
| 72 | user: u32(C.getuid()) |
| 73 | opts: Options{ |
| 74 | lists: { |
| 75 | .hook_dirs: [rpath + 'usr/share/libalpm/hooks/'] |
| 76 | } |
| 77 | strings: { |
| 78 | .db_extension: '.db' |
| 79 | } |
| 80 | integers: { |
| 81 | .parallel_downloads: i64(1) |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | handle.local = &Database{ name: 'local', path: dpath + 'local/', owner: handle, is_local: true } |
| 86 | if !handle.local.get_valid()! { return handle.fail(.db_invalid, 'local') } |
| 87 | initialize_locale() |
| 88 | return handle |
| 89 | } |
| 90 | |
| 91 | fn (mut handle Handle) require_live() ! { |
| 92 | if voidptr(handle) == unsafe { nil } { return AlpmError{ kind: .handle_null } } |
| 93 | if handle.released { return handle.fail(.handle_null, 'handle has been released') } |
| 94 | } |
| 95 | |
| 96 | fn (mut handle Handle) check() ! { |
| 97 | handle.require_live()! |
| 98 | handle.last_error = .ok |
| 99 | handle.last_detail = '' |
| 100 | handle.last_detail_valid = false |
| 101 | } |
| 102 | |
| 103 | fn (mut handle Handle) fail(kind Errno, detail string) IError { |
| 104 | handle.last_error = kind |
| 105 | handle.last_detail = detail |
| 106 | handle.last_detail_valid = true |
| 107 | return AlpmError{ kind: kind, detail: detail } |
| 108 | } |
| 109 | |
| 110 | pub fn (handle &Handle) errno() Errno { |
| 111 | return if handle == unsafe { nil } { .handle_null } else { handle.last_error } |
| 112 | } |
| 113 | |
| 114 | pub fn (handle &Handle) error() IError { |
| 115 | if handle == unsafe { nil } { return AlpmError{ kind: .handle_null } } |
| 116 | return AlpmError{ |
| 117 | kind: handle.last_error |
| 118 | detail: if handle.last_detail_valid { |
| 119 | handle.last_detail |
| 120 | } else { |
| 121 | '' |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | pub fn (mut handle Handle) release() ! { |
| 127 | handle.check()! |
| 128 | if handle.process_active { return handle.fail(.trans_not_null, 'package command is active') } |
| 129 | if handle.transaction_active { return handle.fail(.trans_not_null, '') } |
| 130 | if handle.download_active { return handle.fail(.trans_not_null, 'download is active') } |
| 131 | if handle.retrieval_active { return handle.fail(.trans_not_null, 'retrieval is active') } |
| 132 | $if nspkg_libcurl ? { |
| 133 | if handle.curl_multi != unsafe { nil } { |
| 134 | status := C.curl_multi_cleanup(handle.curl_multi) |
| 135 | handle.curl_multi = unsafe { nil } |
| 136 | if status != 0 { return handle.fail(.libcurl, curl_multi_error(status)) } |
| 137 | } |
| 138 | } |
| 139 | if handle.local != unsafe { nil } { handle.local.detach() } |
| 140 | for mut db in handle.sync_dbs { db.detach() } |
| 141 | handle.close_action_log() |
| 142 | handle.sync_dbs = []&Database{} |
| 143 | handle.local = unsafe { nil } |
| 144 | handle.released = true |
| 145 | handle.binding_data = unsafe { nil } |
| 146 | } |
| 147 | |
| 148 | pub fn (mut handle Handle) local_database() !&Database { |
| 149 | handle.check()! |
| 150 | if handle.local == unsafe { nil } { return handle.fail(.db_null, 'local') } |
| 151 | return handle.local |
| 152 | } |
| 153 | |
| 154 | pub fn (mut handle Handle) sync_databases() ![]&Database { |
| 155 | handle.check()! |
| 156 | return handle.sync_dbs.clone() |
| 157 | } |
| 158 | |
| 159 | pub fn (mut handle Handle) register_syncdb(name string, siglevel u32) !&Database { |
| 160 | handle.check()! |
| 161 | treename := c_string_prefix(name) |
| 162 | if treename == '' || treename.contains('/') { return handle.fail(.wrong_args, treename) } |
| 163 | if handle.transaction_active { return handle.fail(.trans_not_null, '') } |
| 164 | if treename == 'local' { return handle.fail(.db_not_null, treename) } |
| 165 | for db in handle.sync_dbs { |
| 166 | if db.name == treename { return handle.fail(.db_not_null, treename) } |
| 167 | } |
| 168 | handle.log(.debug, "registering sync database '${treename}'\n") |
| 169 | mut db := &Database{ |
| 170 | name: treename.clone() |
| 171 | path: handle.dbpath + 'sync/' + treename + handle.opts.strings[.db_extension] |
| 172 | owner: handle |
| 173 | siglevel: siglevel |
| 174 | } |
| 175 | handle.log(.debug, 'database path for tree ${treename} set to ${db.path}\n') |
| 176 | // registration returns the object even if its initial validation fails. |
| 177 | db.get_valid() or { db.diagnostics << err.msg() } |
| 178 | handle.sync_dbs << db |
| 179 | return db |
| 180 | } |
| 181 | |
| 182 | pub fn (mut handle Handle) unregister_syncdbs() ! { |
| 183 | handle.check()! |
| 184 | if handle.transaction_active { return handle.fail(.trans_not_null, '') } |
| 185 | for mut db in handle.sync_dbs { db.detach() } |
| 186 | handle.sync_dbs = []&Database{} |
| 187 | } |
| 188 | |