| 1 | // package execution order, reasons and install timestamps follow add.c. |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | module engine |
| 4 | |
| 5 | import os |
| 6 | import strconv |
| 7 | import time |
| 8 | |
| 9 | fn C.archive_filter_bytes(&C.Archive, int) i64 |
| 10 | |
| 11 | fn installation_timestamp(value ?string, fallback i64) i64 { |
| 12 | text := value or { return fallback } |
| 13 | trimmed := text.trim_left(' \t\r\n\u000b\u000c') |
| 14 | if trimmed == '' { return fallback } |
| 15 | start := if trimmed[0] in [`+`, `-`] { 1 } else { 0 } |
| 16 | if start == trimmed.len || !trimmed[start..].bytes().all(it.is_digit()) { return fallback } |
| 17 | number := strconv.parse_uint(trimmed[start..], 10, 64) or { return fallback } |
| 18 | return i64(if trimmed[0] == `-` { u64(0) - number } else { number }) |
| 19 | } |
| 20 | |
| 21 | fn (mut handle Handle) complete_file_targets(mut trans Transaction) ! { |
| 22 | for index, pkg in trans.add { |
| 23 | if pkg.origin != .file || pkg.files_loaded { continue } |
| 24 | mut full := handle.read_package_for_handle(pkg.filename, true)! |
| 25 | if !transaction_package_matches(pkg, full) { |
| 26 | return handle.fail(.pkg_invalid, 'archive metadata changed: ${pkg.name}') |
| 27 | } |
| 28 | full.reason = pkg.reason |
| 29 | full.validation = pkg.validation |
| 30 | full.old_package = pkg.old_package |
| 31 | trans.add[index] = full |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | fn (mut db Database) invalidate_group_cache() { |
| 36 | db.groups_loaded = false |
| 37 | db.groups = []&Group{} |
| 38 | } |
| 39 | |
| 40 | fn (mut handle Handle) remove_installed_record(pkg &Package) ! { |
| 41 | handle.local.remove_local_package(pkg) or { |
| 42 | kind := handle.last_error |
| 43 | detail := handle.last_detail |
| 44 | handle.log(.error, localized_message('could not remove database entry %s-%s\n', pkg.name, pkg.version)) |
| 45 | // retain the database failure if the diagnostic callback invokes another API. |
| 46 | return handle.fail(kind, detail) |
| 47 | } |
| 48 | handle.local.by_name.delete(pkg.name) |
| 49 | for index, installed in handle.local.packages { |
| 50 | if installed.name == pkg.name { |
| 51 | handle.local.packages.delete(index) |
| 52 | handle.local.package_revision++ |
| 53 | break |
| 54 | } |
| 55 | } |
| 56 | handle.local.invalidate_group_cache() |
| 57 | } |
| 58 | |
| 59 | fn (mut handle Handle) cache_installed_package(pkg &Package) !&Package { |
| 60 | mut installed := package_snapshot(pkg) |
| 61 | installed.origin = .local_db |
| 62 | installed.database = handle.local |
| 63 | installed.owner = handle |
| 64 | installed.local_path = os.join_path(handle.local.path, local_entry_name(pkg)!) |
| 65 | handle.local.by_name[pkg.name] = installed |
| 66 | // Keep the cache ordered without sorting every installed package again. |
| 67 | mut low := 0 |
| 68 | mut high := handle.local.packages.len |
| 69 | for low < high { |
| 70 | middle := low + (high - low) / 2 |
| 71 | if handle.local.packages[middle].name < installed.name { |
| 72 | low = middle + 1 |
| 73 | } else { |
| 74 | high = middle |
| 75 | } |
| 76 | } |
| 77 | handle.local.packages.insert(low, installed) |
| 78 | handle.local.package_revision++ |
| 79 | handle.local.invalidate_group_cache() |
| 80 | return installed |
| 81 | } |
| 82 | |
| 83 | fn installation_operation(oldpkg &Package, pkg &Package) (PackageOperation, CommitProgressKind) { |
| 84 | if oldpkg == unsafe { nil } { return .install, .add } |
| 85 | comparison := pkg_vercmp(pkg.version, oldpkg.version) |
| 86 | if comparison < 0 { return .downgrade, .downgrade } |
| 87 | if comparison == 0 { return .reinstall, .reinstall } |
| 88 | return .upgrade, .upgrade |
| 89 | } |
| 90 | |
| 91 | fn (mut handle Handle) execute_install_package(mut root RootFilesystem, mut trans Transaction, mut pkg Package, current int) ! { |
| 92 | oldpkg := if pkg.name in handle.local.by_name { |
| 93 | pkg.old_package |
| 94 | } else { |
| 95 | unsafe { nil } |
| 96 | } |
| 97 | operation, progress := installation_operation(oldpkg, pkg) |
| 98 | if oldpkg != unsafe { nil } { pkg.reason = oldpkg.reason } |
| 99 | handle.commit_event(CommitEvent{ kind: .package_install_start, operation: operation, old_package: oldpkg, new_package: unsafe { pkg } }) |
| 100 | old_version := if oldpkg != unsafe { nil } { ?string(oldpkg.version) } else { ?string(none) } |
| 101 | // the source's scriptlet getter clears errno even when the phase is disabled. |
| 102 | handle.check()! |
| 103 | if !trans.flags.has(.no_scriptlet) && pkg.scriptlet { |
| 104 | phase := if oldpkg == unsafe { nil } { 'pre_install' } else { 'pre_upgrade' } |
| 105 | handle.run_package_scriptlet(mut root, pkg, phase, pkg.version, old_version) or { handle.diagnostics << '${phase} ${pkg.name}: ${err.msg()}' } |
| 106 | } |
| 107 | if trans.flags.has(.all_deps) { |
| 108 | pkg.reason = .depend |
| 109 | } else if trans.flags.has(.all_explicit) { |
| 110 | pkg.reason = .explicit |
| 111 | } |
| 112 | if oldpkg != unsafe { nil } { |
| 113 | if !trans.flags.has(.db_only) { handle.remove_package_files(mut root, oldpkg, pkg, 0, 0)! } |
| 114 | handle.remove_installed_record(oldpkg)! |
| 115 | } |
| 116 | handle.local.prepare_local_package(pkg) or { |
| 117 | handle.record_action('error: could not create database entry ${pkg.name}-${pkg.version}\n') |
| 118 | return handle.fail(.db_write, err.msg()) |
| 119 | } |
| 120 | mut recorded := false |
| 121 | defer { |
| 122 | if !recorded { |
| 123 | handle.local.remove_local_package(pkg) or { handle.diagnostics << 'cannot discard incomplete database entry ${pkg.name}: ${err.msg()}' } |
| 124 | } |
| 125 | } |
| 126 | mut reader := open_archive(pkg.filename) or { return handle.fail(.pkg_open, err.msg()) } |
| 127 | defer { reader.close() or { handle.diagnostics << err.msg() } } |
| 128 | if !trans.flags.has(.db_only) { |
| 129 | handle.commit_progress_for(progress, pkg.name, 0, trans.add.len, current) |
| 130 | } |
| 131 | for { |
| 132 | record := reader.next_record() or { return handle.fail(.libarchive, err.msg()) } |
| 133 | if record.eof { break } |
| 134 | if trans.flags.has(.db_only) { |
| 135 | if record.name.starts_with('.') { |
| 136 | handle.extract_database_record(mut root, mut reader, record, pkg) or { |
| 137 | handle.record_install_extraction_problem(pkg, oldpkg) |
| 138 | return err |
| 139 | } |
| 140 | } else { |
| 141 | reader.skip_data()! |
| 142 | } |
| 143 | } else { |
| 144 | position := C.archive_filter_bytes(reader.handle, -1) |
| 145 | percent := if pkg.size > 0 { int(f64(position) / f64(pkg.size) * 100) } else { 0 } |
| 146 | handle.commit_progress_for(progress, pkg.name, if percent > 100 { 100 } else { percent }, trans.add.len, current) |
| 147 | handle.extract_install_record(mut root, mut reader, record, mut pkg, oldpkg) or { |
| 148 | handle.record_install_extraction_problem(pkg, oldpkg) |
| 149 | return err |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | reader.close() or { return handle.fail(.libarchive, err.msg()) } |
| 154 | pkg.install_date = installation_timestamp(os.getenv_opt('SOURCE_DATE_EPOCH'), time.now().unix()) |
| 155 | handle.local.write_local_package(mut pkg, true, true) or { |
| 156 | handle.log(.error, localized_message('could not update database entry %s-%s\n', pkg.name, pkg.version)) |
| 157 | handle.record_action('error: could not update database entry ${pkg.name}-${pkg.version}\n') |
| 158 | return handle.fail(.db_write, err.msg()) |
| 159 | } |
| 160 | recorded = true |
| 161 | installed := handle.cache_installed_package(pkg)! |
| 162 | handle.commit_progress_for(progress, pkg.name, 100, trans.add.len, current) |
| 163 | match operation { |
| 164 | .install { handle.record_action('installed ${pkg.name} (${pkg.version})\n') } |
| 165 | .reinstall { handle.record_action('reinstalled ${pkg.name} (${pkg.version})\n') } |
| 166 | .upgrade { |
| 167 | handle.record_action('upgraded ${pkg.name} (${oldpkg.version} -> ${pkg.version})\n') |
| 168 | } |
| 169 | .downgrade { |
| 170 | handle.record_action('downgraded ${pkg.name} (${oldpkg.version} -> ${pkg.version})\n') |
| 171 | } |
| 172 | .remove {} |
| 173 | } |
| 174 | handle.check()! |
| 175 | if !trans.flags.has(.no_scriptlet) && pkg.scriptlet { |
| 176 | phase := if oldpkg == unsafe { nil } { 'post_install' } else { 'post_upgrade' } |
| 177 | handle.run_package_scriptlet(mut root, installed, phase, pkg.version, old_version) or { handle.diagnostics << '${phase} ${pkg.name}: ${err.msg()}' } |
| 178 | } |
| 179 | handle.commit_event(CommitEvent{ kind: .package_install_done, operation: operation, old_package: oldpkg, new_package: unsafe { pkg } }) |
| 180 | } |
| 181 | |
| 182 | fn (mut handle Handle) execute_install_packages(mut root RootFilesystem, mut trans Transaction) ! { |
| 183 | mut completed := 0 |
| 184 | for index, pointer in trans.add { |
| 185 | if trans.state == .interrupted { break } |
| 186 | mut pkg := unsafe { pointer } |
| 187 | handle.execute_install_package(mut root, mut trans, mut pkg, index + 1) or { |
| 188 | trans.state = .interrupted |
| 189 | return handle.fail(.trans_abort, '${pkg.name}: ${err.msg()}') |
| 190 | } |
| 191 | completed++ |
| 192 | } |
| 193 | if completed == trans.add.len { |
| 194 | handle.run_ldconfig(mut root) or { handle.diagnostics << 'ldconfig: ${err.msg()}' } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | fn (mut handle Handle) record_install_extraction_problem(pkg &Package, oldpkg &Package) { |
| 199 | operation := if oldpkg == unsafe { nil } { 'installing' } else { 'upgrading' } |
| 200 | text := 'problem occurred while ${operation} ${pkg.name}\n' |
| 201 | message := if oldpkg == unsafe { nil } { |
| 202 | 'problem occurred while installing %s\n' |
| 203 | } else { |
| 204 | 'problem occurred while upgrading %s\n' |
| 205 | } |
| 206 | handle.log(.error, localized_message(message, pkg.name)) |
| 207 | handle.record_action('error: ' + text) |
| 208 | } |
| 209 | |