2020-02-14 11:49:41 +00:00
|
|
|
/* app_upgrade.c - Alpine Package Keeper (APK)
|
2009-06-25 07:31:05 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2005-2008 Natanael Copa <n@tanael.org>
|
2011-09-13 08:53:01 +00:00
|
|
|
* Copyright (C) 2008-2011 Timo Teräs <timo.teras@iki.fi>
|
2009-06-25 07:31:05 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
2020-04-22 13:33:41 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only
|
2009-06-25 07:31:05 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
2011-09-14 08:07:45 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2009-06-25 07:31:05 +00:00
|
|
|
#include "apk_applet.h"
|
|
|
|
#include "apk_database.h"
|
2010-03-05 08:13:25 +00:00
|
|
|
#include "apk_print.h"
|
2011-09-09 13:31:11 +00:00
|
|
|
#include "apk_solver.h"
|
|
|
|
|
2020-10-05 15:52:51 +00:00
|
|
|
extern char **apk_argv;
|
|
|
|
|
2011-09-09 13:31:11 +00:00
|
|
|
struct upgrade_ctx {
|
|
|
|
unsigned short solver_flags;
|
2011-09-14 08:07:45 +00:00
|
|
|
int no_self_upgrade : 1;
|
2016-07-22 08:13:33 +00:00
|
|
|
int self_upgrade_only : 1;
|
2019-12-10 01:14:49 +00:00
|
|
|
int ignore : 1;
|
2020-09-28 14:34:01 +00:00
|
|
|
int prune : 1;
|
2020-07-15 00:16:38 +00:00
|
|
|
int errors;
|
2011-09-09 13:31:11 +00:00
|
|
|
};
|
2009-06-25 07:31:05 +00:00
|
|
|
|
2020-08-24 10:35:36 +00:00
|
|
|
#define UPGRADE_OPTIONS(OPT) \
|
|
|
|
OPT(OPT_UPGRADE_available, APK_OPT_SH("a") "available") \
|
|
|
|
OPT(OPT_UPGRADE_ignore, "ignore") \
|
|
|
|
OPT(OPT_UPGRADE_latest, APK_OPT_SH("l") "latest") \
|
|
|
|
OPT(OPT_UPGRADE_no_self_upgrade, "no-self-upgrade") \
|
2020-09-28 14:34:01 +00:00
|
|
|
OPT(OPT_UPGRADE_prune, "prune") \
|
2020-08-24 10:35:36 +00:00
|
|
|
OPT(OPT_UPGRADE_self_upgrade_only, "self-upgrade-only")
|
2020-05-04 18:45:11 +00:00
|
|
|
|
2020-08-24 10:35:36 +00:00
|
|
|
APK_OPT_APPLET(option_desc, UPGRADE_OPTIONS);
|
2020-05-04 18:45:11 +00:00
|
|
|
|
2020-10-05 15:52:51 +00:00
|
|
|
static int option_parse_applet(void *ctx, struct apk_ctx *ac, int opt, const char *optarg)
|
2009-06-25 08:09:40 +00:00
|
|
|
{
|
2011-09-09 13:31:11 +00:00
|
|
|
struct upgrade_ctx *uctx = (struct upgrade_ctx *) ctx;
|
|
|
|
|
2020-05-04 18:45:11 +00:00
|
|
|
switch (opt) {
|
|
|
|
case OPT_UPGRADE_no_self_upgrade:
|
2011-09-14 08:07:45 +00:00
|
|
|
uctx->no_self_upgrade = 1;
|
|
|
|
break;
|
2020-05-04 18:45:11 +00:00
|
|
|
case OPT_UPGRADE_self_upgrade_only:
|
2016-07-22 08:13:33 +00:00
|
|
|
uctx->self_upgrade_only = 1;
|
|
|
|
break;
|
2020-05-04 18:45:11 +00:00
|
|
|
case OPT_UPGRADE_ignore:
|
2019-12-10 01:14:49 +00:00
|
|
|
uctx->ignore = 1;
|
|
|
|
break;
|
2020-09-28 14:34:01 +00:00
|
|
|
case OPT_UPGRADE_prune:
|
|
|
|
uctx->prune = 1;
|
|
|
|
break;
|
2020-05-04 18:45:11 +00:00
|
|
|
case OPT_UPGRADE_available:
|
2011-09-09 13:31:11 +00:00
|
|
|
uctx->solver_flags |= APK_SOLVERF_AVAILABLE;
|
2009-06-25 08:09:40 +00:00
|
|
|
break;
|
2020-05-04 18:45:11 +00:00
|
|
|
case OPT_UPGRADE_latest:
|
2013-06-18 13:24:48 +00:00
|
|
|
uctx->solver_flags |= APK_SOLVERF_LATEST;
|
|
|
|
break;
|
2009-06-25 08:09:40 +00:00
|
|
|
default:
|
2019-06-05 06:32:29 +00:00
|
|
|
return -ENOTSUP;
|
2009-06-25 08:09:40 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-10-08 12:29:27 +00:00
|
|
|
static const struct apk_option_group optgroup_applet = {
|
2020-05-04 18:45:11 +00:00
|
|
|
.desc = option_desc,
|
2014-10-08 12:29:27 +00:00
|
|
|
.parse = option_parse_applet,
|
|
|
|
};
|
|
|
|
|
2016-07-22 08:13:33 +00:00
|
|
|
int apk_do_self_upgrade(struct apk_database *db, unsigned short solver_flags, unsigned int self_upgrade_only)
|
2011-01-01 13:48:10 +00:00
|
|
|
{
|
2020-10-05 15:52:51 +00:00
|
|
|
struct apk_out *out = &db->ctx->out;
|
2011-09-14 07:38:26 +00:00
|
|
|
struct apk_name *name;
|
2016-07-22 08:13:33 +00:00
|
|
|
struct apk_package *pkg;
|
|
|
|
struct apk_provider *p0;
|
2011-09-14 07:38:26 +00:00
|
|
|
struct apk_changeset changeset = {};
|
2011-03-19 12:21:58 +00:00
|
|
|
int r;
|
2011-01-01 13:48:10 +00:00
|
|
|
|
2011-09-14 07:38:26 +00:00
|
|
|
name = apk_db_get_name(db, APK_BLOB_STR("apk-tools"));
|
2016-07-22 08:13:33 +00:00
|
|
|
|
|
|
|
/* First check if new version is even available */
|
|
|
|
r = 0;
|
|
|
|
pkg = apk_pkg_get_installed(name);
|
|
|
|
if (!pkg) goto ret;
|
|
|
|
|
|
|
|
foreach_array_item(p0, name->providers) {
|
|
|
|
struct apk_package *pkg0 = p0->pkg;
|
|
|
|
if (pkg0->name != name || pkg0->repos == 0)
|
|
|
|
continue;
|
|
|
|
if (apk_version_compare_blob(*pkg0->version, *pkg->version) == APK_VERSION_GREATER) {
|
|
|
|
r = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r == 0) goto ret;
|
|
|
|
|
|
|
|
/* Create new commit upgrading apk-tools only with minimal other changes */
|
|
|
|
db->performing_self_upgrade = 1;
|
2013-07-02 08:05:03 +00:00
|
|
|
apk_solver_set_name_flags(name, solver_flags, 0);
|
2011-09-14 07:38:26 +00:00
|
|
|
|
2013-06-11 11:06:06 +00:00
|
|
|
r = apk_solver_solve(db, 0, db->world, &changeset);
|
2011-09-14 07:38:26 +00:00
|
|
|
if (r != 0) {
|
2020-10-05 15:52:51 +00:00
|
|
|
apk_warn(out, "Failed to perform initial self-upgrade, continuing with full upgrade.");
|
2016-07-22 08:13:33 +00:00
|
|
|
r = 0;
|
2011-09-14 07:38:26 +00:00
|
|
|
goto ret;
|
|
|
|
}
|
2011-01-01 13:48:10 +00:00
|
|
|
|
2013-06-12 12:25:16 +00:00
|
|
|
if (changeset.num_total_changes == 0)
|
2011-09-14 07:38:26 +00:00
|
|
|
goto ret;
|
2011-01-01 13:48:10 +00:00
|
|
|
|
2020-10-05 15:52:51 +00:00
|
|
|
if (!self_upgrade_only && db->ctx->flags & APK_SIMULATE) {
|
|
|
|
apk_warn(out, "This simulation is not reliable as apk-tools upgrade is available.");
|
2011-09-14 07:38:26 +00:00
|
|
|
goto ret;
|
2011-01-01 13:48:10 +00:00
|
|
|
}
|
|
|
|
|
2020-10-05 15:52:51 +00:00
|
|
|
apk_msg(out, "Upgrading critical system libraries and apk-tools:");
|
2011-09-14 07:38:26 +00:00
|
|
|
apk_solver_commit_changeset(db, &changeset, db->world);
|
2016-07-22 08:13:33 +00:00
|
|
|
if (self_upgrade_only) goto ret;
|
|
|
|
|
2011-01-01 13:48:10 +00:00
|
|
|
apk_db_close(db);
|
2020-10-05 15:52:51 +00:00
|
|
|
apk_msg(out, "Continuing the upgrade transaction with new apk-tools:");
|
2011-01-01 13:48:10 +00:00
|
|
|
|
2011-09-14 08:07:45 +00:00
|
|
|
for (r = 0; apk_argv[r] != NULL; r++)
|
|
|
|
;
|
|
|
|
apk_argv[r] = "--no-self-upgrade";
|
2011-01-01 13:48:10 +00:00
|
|
|
execvp(apk_argv[0], apk_argv);
|
|
|
|
|
2020-10-05 15:52:51 +00:00
|
|
|
apk_err(out, "PANIC! Failed to re-execute new apk-tools!");
|
2011-01-01 13:48:10 +00:00
|
|
|
exit(1);
|
2011-09-14 07:38:26 +00:00
|
|
|
|
|
|
|
ret:
|
|
|
|
apk_change_array_free(&changeset.changes);
|
2016-07-22 08:13:33 +00:00
|
|
|
db->performing_self_upgrade = 0;
|
2011-09-14 07:38:26 +00:00
|
|
|
return r;
|
2011-01-01 13:48:10 +00:00
|
|
|
}
|
|
|
|
|
2020-07-15 00:16:38 +00:00
|
|
|
static void set_upgrade_for_name(struct apk_database *db, const char *match, struct apk_name *name, void *pctx)
|
|
|
|
{
|
2020-10-05 15:52:51 +00:00
|
|
|
struct apk_out *out = &db->ctx->out;
|
2020-07-15 00:16:38 +00:00
|
|
|
struct upgrade_ctx *uctx = (struct upgrade_ctx *) pctx;
|
|
|
|
|
|
|
|
if (!name) {
|
2020-10-05 15:52:51 +00:00
|
|
|
apk_err(out, "Package '%s' not found", match);
|
2020-07-15 00:16:38 +00:00
|
|
|
uctx->errors++;
|
2020-08-24 12:26:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
apk_solver_set_name_flags(name, uctx->ignore ? APK_SOLVERF_INSTALLED : APK_SOLVERF_UPGRADE, 0);
|
2020-07-15 00:16:38 +00:00
|
|
|
}
|
|
|
|
|
2020-10-09 09:44:32 +00:00
|
|
|
static int upgrade_main(void *ctx, struct apk_ctx *ac, struct apk_string_array *args)
|
2009-06-25 07:31:05 +00:00
|
|
|
{
|
2020-10-09 09:44:32 +00:00
|
|
|
struct apk_out *out = &ac->out;
|
|
|
|
struct apk_database *db = ac->db;
|
2011-09-09 13:31:11 +00:00
|
|
|
struct upgrade_ctx *uctx = (struct upgrade_ctx *) ctx;
|
|
|
|
unsigned short solver_flags;
|
2013-06-14 17:26:48 +00:00
|
|
|
struct apk_dependency *dep;
|
2020-09-28 14:34:01 +00:00
|
|
|
struct apk_provider *p;
|
2012-01-17 12:31:29 +00:00
|
|
|
struct apk_dependency_array *world = NULL;
|
2016-07-22 08:13:33 +00:00
|
|
|
int r = 0;
|
2009-08-04 10:57:54 +00:00
|
|
|
|
2012-01-17 12:46:39 +00:00
|
|
|
if (apk_db_check_world(db, db->world) != 0) {
|
2020-10-05 15:52:51 +00:00
|
|
|
apk_err(out,
|
|
|
|
"Not continuing with upgrade due to missing repository tags. "
|
|
|
|
"Use --force-broken-world to override.");
|
2012-01-17 12:46:39 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-09-09 13:31:11 +00:00
|
|
|
solver_flags = APK_SOLVERF_UPGRADE | uctx->solver_flags;
|
2020-08-24 12:26:25 +00:00
|
|
|
if (!uctx->no_self_upgrade && !args->num) {
|
2016-07-22 08:13:33 +00:00
|
|
|
r = apk_do_self_upgrade(db, solver_flags, uctx->self_upgrade_only);
|
2011-09-14 08:07:45 +00:00
|
|
|
if (r != 0)
|
|
|
|
return r;
|
|
|
|
}
|
2016-07-22 08:13:33 +00:00
|
|
|
if (uctx->self_upgrade_only)
|
|
|
|
return 0;
|
2011-03-19 12:21:58 +00:00
|
|
|
|
2020-09-28 14:34:01 +00:00
|
|
|
if (uctx->prune || (solver_flags & APK_SOLVERF_AVAILABLE)) {
|
2012-01-17 12:31:29 +00:00
|
|
|
apk_dependency_array_copy(&world, db->world);
|
2020-09-28 14:34:01 +00:00
|
|
|
if (solver_flags & APK_SOLVERF_AVAILABLE) {
|
|
|
|
foreach_array_item(dep, world) {
|
|
|
|
if (dep->result_mask == APK_DEPMASK_CHECKSUM) {
|
|
|
|
dep->result_mask = APK_DEPMASK_ANY;
|
|
|
|
dep->version = &apk_atom_null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (uctx->prune) {
|
|
|
|
int i, j;
|
|
|
|
for (i = j = 0; i < world->num; i++) {
|
|
|
|
foreach_array_item(p, world->item[i].name->providers) {
|
|
|
|
if (p->pkg->repos & ~APK_REPOSITORY_CACHED) {
|
|
|
|
world->item[j++] = world->item[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-01-17 12:31:29 +00:00
|
|
|
}
|
2020-09-28 14:34:01 +00:00
|
|
|
apk_dependency_array_resize(&world, j);
|
2012-01-17 12:31:29 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
world = db->world;
|
|
|
|
}
|
|
|
|
|
2020-07-15 00:16:38 +00:00
|
|
|
if (args->num > 0) {
|
|
|
|
/* if specific packages are listed, we don't want to upgrade world. */
|
2020-08-24 12:26:25 +00:00
|
|
|
if (!uctx->ignore) solver_flags &= ~APK_SOLVERF_UPGRADE;
|
|
|
|
apk_name_foreach_matching(db, args, apk_foreach_genid(), set_upgrade_for_name, uctx);
|
|
|
|
if (uctx->errors) return uctx->errors;
|
2020-07-15 00:16:38 +00:00
|
|
|
}
|
|
|
|
|
2012-01-17 12:31:29 +00:00
|
|
|
r = apk_solver_commit(db, solver_flags, world);
|
|
|
|
|
2020-09-28 14:34:01 +00:00
|
|
|
if (world != db->world)
|
2012-01-17 12:31:29 +00:00
|
|
|
apk_dependency_array_free(&world);
|
|
|
|
|
|
|
|
return r;
|
2009-06-25 07:31:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct apk_applet apk_upgrade = {
|
|
|
|
.name = "upgrade",
|
2009-08-06 11:25:03 +00:00
|
|
|
.open_flags = APK_OPENF_WRITE,
|
2011-09-09 19:00:49 +00:00
|
|
|
.context_size = sizeof(struct upgrade_ctx),
|
2014-10-08 12:29:27 +00:00
|
|
|
.optgroups = { &optgroup_global, &optgroup_commit, &optgroup_applet },
|
2009-06-25 07:31:05 +00:00
|
|
|
.main = upgrade_main,
|
|
|
|
};
|
|
|
|
|
|
|
|
APK_DEFINE_APPLET(apk_upgrade);
|
|
|
|
|