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.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 as published
|
|
|
|
* by the Free Software Foundation. See http://www.gnu.org/ for details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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"
|
|
|
|
|
|
|
|
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;
|
2011-09-09 13:31:11 +00:00
|
|
|
};
|
2009-06-25 07:31:05 +00:00
|
|
|
|
2020-05-04 18:45:11 +00:00
|
|
|
enum {
|
|
|
|
OPT_UPGRADE_available,
|
|
|
|
OPT_UPGRADE_ignore,
|
|
|
|
OPT_UPGRADE_latest,
|
|
|
|
OPT_UPGRADE_no_self_upgrade,
|
|
|
|
OPT_UPGRADE_self_upgrade_only,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char option_desc[] =
|
|
|
|
APK_OPTAPPLET
|
|
|
|
APK_OPT2n("available", "a")
|
|
|
|
APK_OPT1n("ignore")
|
|
|
|
APK_OPT2n("latest", "l")
|
|
|
|
APK_OPT1n("no-self-upgrade")
|
|
|
|
APK_OPT1n("self-upgrade-only");
|
|
|
|
|
|
|
|
static int option_parse_applet(void *ctx, struct apk_db_options *dbopts, 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-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
|
|
|
{
|
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) {
|
2016-07-22 08:13:33 +00:00
|
|
|
apk_warning("Failed to perform initial self-upgrade, continuing with full upgrade.");
|
|
|
|
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
|
|
|
|
2016-07-22 08:13:33 +00:00
|
|
|
if (!self_upgrade_only && apk_flags & APK_SIMULATE) {
|
2011-01-01 13:48:10 +00:00
|
|
|
apk_warning("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
|
|
|
}
|
|
|
|
|
2011-04-09 13:05:49 +00:00
|
|
|
apk_message("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);
|
|
|
|
|
2011-04-09 13:05:49 +00:00
|
|
|
apk_message("Continuing the upgrade transaction with new apk-tools:");
|
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);
|
|
|
|
|
|
|
|
apk_error("PANIC! Failed to re-execute new apk-tools!");
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-06-18 10:01:51 +00:00
|
|
|
static int upgrade_main(void *ctx, struct apk_database *db, struct apk_string_array *args)
|
2009-06-25 07:31:05 +00:00
|
|
|
{
|
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;
|
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) {
|
2018-01-03 13:17:11 +00:00
|
|
|
apk_error("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;
|
2011-09-14 08:07:45 +00:00
|
|
|
if (!uctx->no_self_upgrade) {
|
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
|
|
|
|
2019-12-10 01:14:49 +00:00
|
|
|
if (uctx->ignore) {
|
|
|
|
char **pkg_name;
|
|
|
|
struct apk_name *name;
|
|
|
|
foreach_array_item(pkg_name, args) {
|
|
|
|
name = apk_db_get_name(db, APK_BLOB_STR(*pkg_name));
|
|
|
|
apk_solver_set_name_flags(name, solver_flags | APK_SOLVERF_IGNORE_UPGRADE, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-17 12:31:29 +00:00
|
|
|
if (solver_flags & APK_SOLVERF_AVAILABLE) {
|
|
|
|
apk_dependency_array_copy(&world, db->world);
|
2013-06-14 17:26:48 +00:00
|
|
|
foreach_array_item(dep, world) {
|
2012-01-17 12:31:29 +00:00
|
|
|
if (dep->result_mask == APK_DEPMASK_CHECKSUM) {
|
2012-02-29 06:53:43 +00:00
|
|
|
dep->result_mask = APK_DEPMASK_ANY;
|
2012-01-17 12:31:29 +00:00
|
|
|
dep->version = apk_blob_atomize(APK_BLOB_NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
world = db->world;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = apk_solver_commit(db, solver_flags, world);
|
|
|
|
|
|
|
|
if (solver_flags & APK_SOLVERF_AVAILABLE)
|
|
|
|
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);
|
|
|
|
|