2009-06-25 07:31:05 +00:00
|
|
|
/* upgrade.c - Alpine Package Keeper (APK)
|
|
|
|
*
|
|
|
|
* 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;
|
2011-09-09 13:31:11 +00:00
|
|
|
};
|
2009-06-25 07:31:05 +00:00
|
|
|
|
2009-08-06 11:25:03 +00:00
|
|
|
static int upgrade_parse(void *ctx, struct apk_db_options *dbopts,
|
|
|
|
int optch, int optindex, 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;
|
|
|
|
|
2009-06-25 08:09:40 +00:00
|
|
|
switch (optch) {
|
2011-09-14 08:07:45 +00:00
|
|
|
case 0x10000:
|
|
|
|
uctx->no_self_upgrade = 1;
|
|
|
|
break;
|
2009-06-25 08:09:40 +00:00
|
|
|
case 'a':
|
2011-09-09 13:31:11 +00:00
|
|
|
uctx->solver_flags |= APK_SOLVERF_AVAILABLE;
|
2009-06-25 08:09:40 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-09-14 07:38:26 +00:00
|
|
|
int apk_do_self_upgrade(struct apk_database *db, unsigned short solver_flags)
|
2011-01-01 13:48:10 +00:00
|
|
|
{
|
2011-09-14 07:38:26 +00:00
|
|
|
struct apk_name *name;
|
|
|
|
struct apk_changeset changeset = {};
|
|
|
|
struct apk_package_array *solution = NULL;
|
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"));
|
2011-09-16 14:10:50 +00:00
|
|
|
apk_solver_set_name_flags(name, solver_flags, solver_flags);
|
2011-09-14 07:38:26 +00:00
|
|
|
db->performing_self_update = 1;
|
|
|
|
|
2011-09-14 13:48:28 +00:00
|
|
|
r = apk_solver_solve(db, 0, db->world, &solution, &changeset);
|
2011-09-14 07:38:26 +00:00
|
|
|
if (r != 0) {
|
|
|
|
if (apk_flags & APK_FORCE)
|
|
|
|
r = 0;
|
|
|
|
else
|
|
|
|
apk_solver_print_errors(db, solution, db->world, r);
|
|
|
|
goto ret;
|
|
|
|
}
|
2011-01-01 13:48:10 +00:00
|
|
|
|
2011-09-14 07:38:26 +00:00
|
|
|
if (changeset.changes->num == 0)
|
|
|
|
goto ret;
|
2011-01-01 13:48:10 +00:00
|
|
|
|
|
|
|
if (apk_flags & APK_SIMULATE) {
|
|
|
|
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);
|
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_package_array_free(&solution);
|
|
|
|
apk_change_array_free(&changeset.changes);
|
|
|
|
db->performing_self_update = 0;
|
|
|
|
|
|
|
|
return r;
|
2011-01-01 13:48:10 +00:00
|
|
|
}
|
|
|
|
|
2009-08-06 11:25:03 +00:00
|
|
|
static int upgrade_main(void *ctx, struct apk_database *db, int argc, char **argv)
|
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;
|
2012-01-17 12:31:29 +00:00
|
|
|
struct apk_dependency_array *world = NULL;
|
|
|
|
int i, r;
|
2009-08-04 10:57:54 +00:00
|
|
|
|
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) {
|
|
|
|
r = apk_do_self_upgrade(db, solver_flags);
|
|
|
|
if (r != 0)
|
|
|
|
return r;
|
|
|
|
}
|
2011-03-19 12:21:58 +00:00
|
|
|
|
2012-01-17 12:31:29 +00:00
|
|
|
if (solver_flags & APK_SOLVERF_AVAILABLE) {
|
|
|
|
apk_dependency_array_copy(&world, db->world);
|
|
|
|
for (i = 0; i < world->num; i++) {
|
|
|
|
struct apk_dependency *dep = &world->item[i];
|
|
|
|
if (dep->result_mask == APK_DEPMASK_CHECKSUM) {
|
|
|
|
dep->result_mask = APK_DEPMASK_REQUIRE;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2009-06-25 12:14:07 +00:00
|
|
|
static struct apk_option upgrade_options[] = {
|
|
|
|
{ 'a', "available",
|
|
|
|
"Re-install or downgrade if currently installed package is not "
|
|
|
|
"currently available from any repository" },
|
2011-09-14 08:07:45 +00:00
|
|
|
{ 0x10000, "no-self-upgrade",
|
|
|
|
"Do not do early upgrade of 'apk-tools' package" },
|
2009-06-25 08:09:40 +00:00
|
|
|
};
|
|
|
|
|
2009-06-25 07:31:05 +00:00
|
|
|
static struct apk_applet apk_upgrade = {
|
|
|
|
.name = "upgrade",
|
2009-06-25 12:14:07 +00:00
|
|
|
.help = "Upgrade (or downgrade with -a) the currently installed "
|
|
|
|
"packages to versions available in repositories.",
|
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),
|
2009-06-25 08:09:40 +00:00
|
|
|
.num_options = ARRAY_SIZE(upgrade_options),
|
|
|
|
.options = upgrade_options,
|
|
|
|
.parse = upgrade_parse,
|
2009-06-25 07:31:05 +00:00
|
|
|
.main = upgrade_main,
|
|
|
|
};
|
|
|
|
|
|
|
|
APK_DEFINE_APPLET(apk_upgrade);
|
|
|
|
|