| 1 |
/* |
| 2 |
* Copyright (C) 2025 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
import { InstallProgressCB, MissingPackages, PackageManager, ProgressCB, InstallProgressType, UpdateDetail, Update, ProgressData, History } from './packagemanager-abstract'; |
| 7 |
import * as PK from "packagekit.js"; |
| 8 |
|
| 9 |
const InstallProgressMap = { |
| 10 |
[PK.Enum.INFO_DOWNLOADING]: InstallProgressType.DOWNLOADING, |
| 11 |
[PK.Enum.INFO_UPDATING]: InstallProgressType.UPDATING, |
| 12 |
[PK.Enum.INFO_REMOVING]: InstallProgressType.REMOVING, |
| 13 |
[PK.Enum.INFO_INSTALLING]: InstallProgressType.INSTALLING, |
| 14 |
[PK.Enum.INFO_REINSTALLING]: InstallProgressType.REINSTALLING, |
| 15 |
[PK.Enum.INFO_DOWNGRADING]: InstallProgressType.DOWNGRADING, |
| 16 |
}; |
| 17 |
|
| 18 |
export class PackageKitManager implements PackageManager { |
| 19 |
name: string; |
| 20 |
|
| 21 |
constructor() { |
| 22 |
this.name = "packagekit"; |
| 23 |
} |
| 24 |
|
| 25 |
/* Support for installing missing packages. |
| 26 |
* |
| 27 |
* First call check_missing_packages to determine whether something |
| 28 |
* needs to be installed, then call install_missing_packages to |
| 29 |
* actually install them. |
| 30 |
* |
| 31 |
* check_missing_packages resolves to an object that can be passed to |
| 32 |
* install_missing_packages. It contains these fields: |
| 33 |
* |
| 34 |
* - missing_names: Packages that were requested, are currently not installed, |
| 35 |
* and can be installed. |
| 36 |
* |
| 37 |
* - missing_ids: The full PackageKit IDs corresponding to missing_names |
| 38 |
* |
| 39 |
* - unavailable_names: Packages that were requested, are currently not installed, |
| 40 |
* but can't be found in any repository. |
| 41 |
* |
| 42 |
* If unavailable_names is empty, a simulated installation of the missing packages |
| 43 |
* is done and the result also contains these fields: |
| 44 |
* |
| 45 |
* - extra_names: Packages that need to be installed as dependencies of |
| 46 |
* missing_names. |
| 47 |
* |
| 48 |
* - remove_names: Packages that need to be removed. |
| 49 |
* |
| 50 |
* - download_size: Bytes that need to be downloaded. |
| 51 |
*/ |
| 52 |
async check_missing_packages(pkgnames: string[], progress_cb?: ProgressCB): Promise<MissingPackages> { |
| 53 |
const install_ids: string[] = []; |
| 54 |
const installed_names = new Set(); |
| 55 |
const data: MissingPackages = { |
| 56 |
download_size: 0, |
| 57 |
missing_ids: [], |
| 58 |
missing_names: [], |
| 59 |
unavailable_names: [], |
| 60 |
extra_names: [], |
| 61 |
remove_names: [], |
| 62 |
}; |
| 63 |
|
| 64 |
if (pkgnames.length === 0) |
| 65 |
return data; |
| 66 |
|
| 67 |
await this.refresh(false, progress_cb); |
| 68 |
|
| 69 |
await PK.cancellableTransaction("Resolve", |
| 70 |
[PK.Enum.FILTER_ARCH | PK.Enum.FILTER_NOT_SOURCE | PK.Enum.FILTER_NEWEST, pkgnames], |
| 71 |
progress_cb, |
| 72 |
{ |
| 73 |
Package: (_info: number, package_id: string) => { |
| 74 |
const parts = package_id.split(";"); |
| 75 |
const repos = parts[3].split(":"); |
| 76 |
if (repos.indexOf("installed") >= 0) { |
| 77 |
installed_names.add(parts[0]); |
| 78 |
} else { |
| 79 |
data.missing_ids.push(package_id); |
| 80 |
data.missing_names.push(parts[0]); |
| 81 |
} |
| 82 |
}, |
| 83 |
}); |
| 84 |
pkgnames.forEach(name => { |
| 85 |
if (!installed_names.has(name) && data.missing_names.indexOf(name) == -1) |
| 86 |
data.unavailable_names.push(name); |
| 87 |
}); |
| 88 |
|
| 89 |
if (data.missing_ids.length > 0 && data.unavailable_names.length === 0) { |
| 90 |
await PK.cancellableTransaction("InstallPackages", |
| 91 |
[PK.Enum.TRANSACTION_FLAG_SIMULATE, data.missing_ids], |
| 92 |
progress_cb, |
| 93 |
{ |
| 94 |
Package: (info: number, package_id: string) => { |
| 95 |
const name = package_id.split(";")[0]; |
| 96 |
if (info == PK.Enum.INFO_REMOVING) { |
| 97 |
data.remove_names.push(name); |
| 98 |
} else if (info == PK.Enum.INFO_INSTALLING || |
| 99 |
info == PK.Enum.INFO_UPDATING) { |
| 100 |
install_ids.push(package_id); |
| 101 |
if (data.missing_names.indexOf(name) == -1) |
| 102 |
data.extra_names.push(name); |
| 103 |
} |
| 104 |
} |
| 105 |
}); |
| 106 |
data.missing_names.sort(); |
| 107 |
data.extra_names.sort(); |
| 108 |
data.remove_names.sort(); |
| 109 |
} |
| 110 |
|
| 111 |
if (install_ids.length > 0) { |
| 112 |
await PK.cancellableTransaction("GetDetails", |
| 113 |
[install_ids], |
| 114 |
progress_cb, |
| 115 |
{ |
| 116 |
Details: (details: { size: { v: number, t: string } }) => { |
| 117 |
if (details.size) |
| 118 |
data.download_size += details.size.v; |
| 119 |
} |
| 120 |
}); |
| 121 |
} |
| 122 |
|
| 123 |
return data; |
| 124 |
} |
| 125 |
|
| 126 |
/* Carry out what check_missing_packages has planned. |
| 127 |
* |
| 128 |
* In addition to the usual "waiting", "percentage", and "cancel" |
| 129 |
* fields, the object reported by progress_cb also includes "info" and |
| 130 |
* "package" from the "Package" signal. |
| 131 |
*/ |
| 132 |
async install_missing_packages(data: MissingPackages, progress_cb?: InstallProgressCB): Promise<void> { |
| 133 |
if (!data || data.missing_ids.length === 0) |
| 134 |
return; |
| 135 |
|
| 136 |
let last_progress: ProgressData | null = null; |
| 137 |
let last_info = 0; |
| 138 |
let last_name = ""; |
| 139 |
|
| 140 |
function report_progess() { |
| 141 |
if (progress_cb && last_progress !== null) |
| 142 |
progress_cb({ |
| 143 |
waiting: last_progress.waiting, |
| 144 |
percentage: last_progress.percentage, |
| 145 |
cancel: last_progress.cancel, |
| 146 |
info: InstallProgressMap[last_info], |
| 147 |
// Maps PackageKit state to our own PackageManager state temporary |
| 148 |
// until all pkg/lib/packagekit use cases are supported by the PackageManager abstraction. |
| 149 |
package: last_name |
| 150 |
}); |
| 151 |
} |
| 152 |
|
| 153 |
await PK.cancellableTransaction("InstallPackages", [0, data.missing_ids], |
| 154 |
(p: ProgressData) => { |
| 155 |
last_progress = p; |
| 156 |
report_progess(); |
| 157 |
}, |
| 158 |
{ |
| 159 |
Package: (info: number, id: string) => { |
| 160 |
last_info = info; |
| 161 |
last_name = id.split(";")[0]; |
| 162 |
report_progess(); |
| 163 |
} |
| 164 |
}); |
| 165 |
} |
| 166 |
|
| 167 |
async refresh(force: boolean, progress_cb?: ProgressCB): Promise<void> { |
| 168 |
return PK.refresh(force, progress_cb); |
| 169 |
} |
| 170 |
|
| 171 |
async is_installed(pkgnames: string[]): Promise<boolean> { |
| 172 |
const uninstalled = new Set(pkgnames); |
| 173 |
|
| 174 |
if (uninstalled.size === 0) |
| 175 |
return true; |
| 176 |
|
| 177 |
await PK.cancellableTransaction("Resolve", |
| 178 |
[PK.Enum.FILTER_ARCH | PK.Enum.FILTER_NOT_SOURCE | PK.Enum.FILTER_INSTALLED, pkgnames], |
| 179 |
null, |
| 180 |
{ |
| 181 |
Package: (_info: unknown, package_id: string) => { |
| 182 |
const parts = package_id.split(";"); |
| 183 |
uninstalled.delete(parts[0]); |
| 184 |
}, |
| 185 |
}); |
| 186 |
|
| 187 |
return uninstalled.size === 0; |
| 188 |
} |
| 189 |
|
| 190 |
async install_packages(pkgnames: string[], progress_cb?: ProgressCB): Promise<void> { |
| 191 |
const flags = PK.Enum.FILTER_ARCH | PK.Enum.FILTER_NOT_SOURCE | PK.Enum.FILTER_NEWEST; |
| 192 |
const ids: string[] = []; |
| 193 |
|
| 194 |
await PK.cancellableTransaction("Resolve", [flags | PK.Enum.FILTER_NOT_INSTALLED, Array.from(pkgnames)], null, |
| 195 |
{ |
| 196 |
Package: (_info: unknown, package_id: string) => ids.push(package_id), |
| 197 |
}); |
| 198 |
|
| 199 |
if (ids.length === 0) |
| 200 |
return Promise.reject(new PK.TransactionError("not-found", "Can't resolve package(s)")); |
| 201 |
else |
| 202 |
return PK.cancellableTransaction("InstallPackages", [0, ids], progress_cb) |
| 203 |
.catch(ex => { |
| 204 |
if (ex.code != PK.Enum.ERROR_ALREADY_INSTALLED) |
| 205 |
return Promise.reject(ex); |
| 206 |
}); |
| 207 |
} |
| 208 |
|
| 209 |
async remove_packages(pkgnames: string[], progress_cb?: ProgressCB): Promise<void> { |
| 210 |
const ids: string[] = []; |
| 211 |
|
| 212 |
await PK.cancellableTransaction("Resolve", [PK.Enum.FILTER_NOT_SOURCE | PK.Enum.FILTER_INSTALLED | PK.Enum.FILTER_NOT_SOURCE, pkgnames], null, |
| 213 |
{ |
| 214 |
Package: (_info: unknown, package_id: string) => ids.push(package_id), |
| 215 |
}); |
| 216 |
|
| 217 |
if (ids.length === 0) |
| 218 |
return Promise.resolve(); |
| 219 |
|
| 220 |
return PK.cancellableTransaction("RemovePackages", [0, ids, true, false], progress_cb); |
| 221 |
} |
| 222 |
|
| 223 |
async find_file_packages(files: string[], progress_cb?: ProgressCB): Promise<string[]> { |
| 224 |
const installed: string[] = []; |
| 225 |
await PK.cancellableTransaction("SearchFiles", |
| 226 |
[PK.Enum.FILTER_ARCH | PK.Enum.FILTER_NOT_SOURCE | PK.Enum.FILTER_INSTALLED, files], |
| 227 |
progress_cb, |
| 228 |
{ |
| 229 |
Package: (_info: unknown, package_id: string) => { |
| 230 |
const pkg = package_id.split(";")[0]; |
| 231 |
installed.push(pkg); |
| 232 |
}, |
| 233 |
}); |
| 234 |
|
| 235 |
return installed; |
| 236 |
} |
| 237 |
|
| 238 |
async get_updates<T extends boolean>(detail: T, progress_cb?: ProgressCB): Promise<T extends true ? UpdateDetail[] : Update[]> { |
| 239 |
const updates = await PK.get_updates(detail, progress_cb); |
| 240 |
return updates as unknown as T extends true ? UpdateDetail[] : Update[]; |
| 241 |
} |
| 242 |
|
| 243 |
async update_packages(updates: Update[] | UpdateDetail[], progress_cb?: ProgressCB, transaction_path?: string): Promise<void> { |
| 244 |
return PK.update_packages(updates, progress_cb, transaction_path); |
| 245 |
} |
| 246 |
|
| 247 |
async get_backend(): Promise<string> { |
| 248 |
const [result] = await PK.call("/org/freedesktop/PackageKit", |
| 249 |
"org.freedesktop.DBus.Properties", |
| 250 |
"Get", ["org.freedesktop.PackageKit", "BackendName"]) as [{ v: string, t: string }]; |
| 251 |
return result.v; |
| 252 |
} |
| 253 |
|
| 254 |
async get_last_refresh_time(): Promise<number> { |
| 255 |
const [seconds] = await PK.call("/org/freedesktop/PackageKit", "org.freedesktop.PackageKit", "GetTimeSinceAction", [PK.Enum.ROLE_REFRESH_CACHE]); |
| 256 |
return seconds; |
| 257 |
} |
| 258 |
|
| 259 |
async get_history(): Promise<History[]> { |
| 260 |
const history = [] as History[]; |
| 261 |
|
| 262 |
// would be nice to filter only for "update-packages" role, but can't here |
| 263 |
await PK.transaction("GetOldTransactions", [0], { |
| 264 |
Transaction: (_objPath: string, timeSpec: string, _succeeded: string, role: number, _duration: string, data: string) => { |
| 265 |
if (role !== PK.Enum.ROLE_UPDATE_PACKAGES) |
| 266 |
return; |
| 267 |
|
| 268 |
/** |
| 269 |
* data looks like: |
| 270 |
* downloading\tbash-completion;1:2.6-1.fc26;noarch;updates-testing |
| 271 |
* updating\tbash-completion;1:2.6-1.fc26;noarch;updates-testing |
| 272 |
* timeSpec will be one of: |
| 273 |
* 2026-01-29T12:57:49.112827-08 |
| 274 |
* 2026-01-29T19:27:49.112827-01:30 |
| 275 |
* 2026-01-29T20:57:49.112827Z |
| 276 |
* depending on timezone and PackageKit version |
| 277 |
*/ |
| 278 |
let timestamp = Date.parse(timeSpec); |
| 279 |
if (isNaN(timestamp)) { |
| 280 |
/* |
| 281 |
* Neither Firefox's nor Chromium's parsers handle the short offset |
| 282 |
* format (first one) as of 2026-01: |
| 283 |
* https://bugzilla.mozilla.org/show_bug.cgi?id=2013444 |
| 284 |
* https://issues.chromium.org/issues/479862357 |
| 285 |
* so we hack around it by adding the :00 |
| 286 |
*/ |
| 287 |
const shortoff_regex = /[-+]\d\d$/; |
| 288 |
if (shortoff_regex.exec(timeSpec)) { |
| 289 |
const fixed_timeSpec = timeSpec + ":00"; |
| 290 |
// console.debug(`Transaction has an invalid timeSpec=${timeSpec}, trying fixed=${fixed_timeSpec}`); |
| 291 |
timestamp = Date.parse(fixed_timeSpec); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
if (isNaN(timestamp)) { |
| 296 |
console.warn(`Transaction has an invalid timeSpec=${timeSpec}, skipping`); |
| 297 |
return; |
| 298 |
} |
| 299 |
|
| 300 |
const pkgs = { timestamp, packages: {} } as History; |
| 301 |
let empty = true; |
| 302 |
data.split("\n").forEach(line => { |
| 303 |
const fields = line.trim().split("\t"); |
| 304 |
if (fields.length >= 2) { |
| 305 |
const pkgId = fields[1].split(";"); |
| 306 |
pkgs.packages[pkgId[0]] = pkgId[1]; |
| 307 |
empty = false; |
| 308 |
} |
| 309 |
}); |
| 310 |
|
| 311 |
if (!empty) |
| 312 |
history.unshift(pkgs); // PK reports in time-ascending order, but we want the latest first |
| 313 |
}, |
| 314 |
}); |
| 315 |
|
| 316 |
return history; |
| 317 |
} |
| 318 |
|
| 319 |
/** |
| 320 |
* Check a list of packages whether they are available. |
| 321 |
* |
| 322 |
* @param {string[]} pkgnames - names of packages which should be available in the repositories |
| 323 |
* @return {Promise<boolean>} true if packages are available |
| 324 |
*/ |
| 325 |
async is_available(pkgnames: string[]): Promise<boolean> { |
| 326 |
const available = new Set(); |
| 327 |
|
| 328 |
if (pkgnames && pkgnames.length === 0) |
| 329 |
return true; |
| 330 |
|
| 331 |
await PK.cancellableTransaction("Resolve", |
| 332 |
[PK.Enum.FILTER_ARCH | PK.Enum.FILTER_NEWEST | PK.Enum.FILTER_NOT_INSTALLED, pkgnames], |
| 333 |
null, |
| 334 |
{ |
| 335 |
Package: (_info: unknown, package_id: string) => { |
| 336 |
const pkgname = package_id.split(";")[0]; |
| 337 |
available.add(pkgname); |
| 338 |
}, |
| 339 |
}); |
| 340 |
|
| 341 |
return available.size === new Set(pkgnames).size; |
| 342 |
} |
| 343 |
} |