2020-02-14 11:49:41 +00:00
|
|
|
/* app_del.c - Alpine Package Keeper (APK)
|
2008-04-17 14:09:13 +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>
|
2008-04-17 14:09:13 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
2020-04-22 13:33:41 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only
|
2008-04-17 14:09:13 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#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"
|
2008-04-17 14:09:13 +00:00
|
|
|
|
2011-09-09 16:41:19 +00:00
|
|
|
struct del_ctx {
|
|
|
|
int recursive_delete : 1;
|
|
|
|
struct apk_dependency_array *world;
|
2014-12-08 06:30:58 +00:00
|
|
|
int errors;
|
2011-09-09 16:41:19 +00:00
|
|
|
};
|
|
|
|
|
2020-08-24 10:35:36 +00:00
|
|
|
#define DEL_OPTIONS(OPT) \
|
|
|
|
OPT(OPT_DEL_redepends, APK_OPT_SH("r") "rdepends")
|
2020-05-04 18:45:11 +00:00
|
|
|
|
2020-08-24 10:35:36 +00:00
|
|
|
APK_OPT_APPLET(option_desc, DEL_OPTIONS);
|
2020-05-04 18:45:11 +00:00
|
|
|
|
2020-10-05 15:52:51 +00:00
|
|
|
static int option_parse_applet(void *pctx, struct apk_ctx *ac, int opt, const char *optarg)
|
2009-08-04 07:57:33 +00:00
|
|
|
{
|
2011-09-09 16:41:19 +00:00
|
|
|
struct del_ctx *ctx = (struct del_ctx *) pctx;
|
|
|
|
|
2020-05-04 18:45:11 +00:00
|
|
|
switch (opt) {
|
|
|
|
case OPT_DEL_redepends:
|
2011-09-09 16:41:19 +00:00
|
|
|
ctx->recursive_delete = 1;
|
|
|
|
break;
|
2009-08-04 07:57:33 +00:00
|
|
|
default:
|
2014-10-08 12:29:27 +00:00
|
|
|
return -ENOTSUP;
|
2009-08-04 07:57:33 +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,
|
|
|
|
};
|
|
|
|
|
2011-09-09 16:41:19 +00:00
|
|
|
struct not_deleted_ctx {
|
2020-10-05 15:52:51 +00:00
|
|
|
struct apk_out *out;
|
2011-09-09 16:41:19 +00:00
|
|
|
struct apk_indent indent;
|
2013-06-15 17:39:05 +00:00
|
|
|
struct apk_name *name;
|
|
|
|
unsigned int matches;
|
2011-09-09 16:41:19 +00:00
|
|
|
int header;
|
|
|
|
};
|
|
|
|
|
2021-03-19 10:05:20 +00:00
|
|
|
static inline int name_in_world(struct apk_name *n)
|
|
|
|
{
|
|
|
|
return n->state_int == 1;
|
|
|
|
}
|
|
|
|
|
2013-06-19 11:49:44 +00:00
|
|
|
static void print_not_deleted_pkg(struct apk_package *pkg0, struct apk_dependency *dep0,
|
|
|
|
struct apk_package *pkg, void *pctx)
|
2011-09-09 16:41:19 +00:00
|
|
|
{
|
|
|
|
struct not_deleted_ctx *ctx = (struct not_deleted_ctx *) pctx;
|
2020-10-05 15:52:51 +00:00
|
|
|
struct apk_out *out = ctx->out;
|
2019-12-29 17:33:44 +00:00
|
|
|
struct apk_dependency *d;
|
|
|
|
struct apk_provider *p;
|
2011-09-09 16:41:19 +00:00
|
|
|
|
2021-03-19 10:05:20 +00:00
|
|
|
if (!ctx->header) {
|
|
|
|
apk_msg(out, "World updated, but the following packages are not removed due to:");
|
|
|
|
ctx->header = 1;
|
|
|
|
}
|
|
|
|
if (!ctx->indent.indent) {
|
|
|
|
ctx->indent.x = printf(" %s:", ctx->name->name);
|
|
|
|
ctx->indent.indent = ctx->indent.x + 1;
|
|
|
|
}
|
2019-12-29 17:33:44 +00:00
|
|
|
|
2021-03-19 10:05:20 +00:00
|
|
|
if (name_in_world(pkg0->name)) {
|
2019-12-29 17:33:44 +00:00
|
|
|
apk_print_indented(&ctx->indent, APK_BLOB_STR(pkg0->name->name));
|
2011-09-09 16:41:19 +00:00
|
|
|
}
|
2021-03-19 10:05:20 +00:00
|
|
|
foreach_array_item(d, pkg0->provides) {
|
|
|
|
if (!name_in_world(d->name)) continue;
|
|
|
|
apk_print_indented(&ctx->indent, APK_BLOB_STR(d->name->name));
|
|
|
|
}
|
2011-09-09 16:41:19 +00:00
|
|
|
|
2013-06-19 11:49:44 +00:00
|
|
|
apk_pkg_foreach_reverse_dependency(pkg0, ctx->matches, print_not_deleted_pkg, pctx);
|
2019-12-29 17:33:44 +00:00
|
|
|
foreach_array_item(d, pkg0->install_if) {
|
|
|
|
foreach_array_item(p, d->name->providers) {
|
|
|
|
if (!p->pkg->marked) continue;
|
|
|
|
if (apk_pkg_match_genid(p->pkg, ctx->matches)) continue;
|
|
|
|
print_not_deleted_pkg(p->pkg, NULL, NULL, pctx);
|
|
|
|
}
|
|
|
|
}
|
2011-09-09 16:41:19 +00:00
|
|
|
}
|
|
|
|
|
2013-06-19 11:49:44 +00:00
|
|
|
static void print_not_deleted_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;
|
2013-06-19 11:49:44 +00:00
|
|
|
struct not_deleted_ctx *ctx = (struct not_deleted_ctx *) pctx;
|
|
|
|
struct apk_provider *p;
|
|
|
|
|
2020-10-05 15:52:51 +00:00
|
|
|
ctx->indent = (struct apk_indent) { .out = out };
|
2013-06-19 11:49:44 +00:00
|
|
|
ctx->name = name;
|
|
|
|
ctx->matches = apk_foreach_genid() | APK_FOREACH_MARKED | APK_DEP_SATISFIES;
|
|
|
|
foreach_array_item(p, name->providers)
|
|
|
|
if (p->pkg->marked)
|
|
|
|
print_not_deleted_pkg(p->pkg, NULL, NULL, ctx);
|
|
|
|
if (ctx->indent.indent)
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void delete_pkg(struct apk_package *pkg0, struct apk_dependency *dep0,
|
|
|
|
struct apk_package *pkg, void *pctx)
|
2011-09-09 16:41:19 +00:00
|
|
|
{
|
|
|
|
struct del_ctx *ctx = (struct del_ctx *) pctx;
|
|
|
|
|
2013-06-13 17:48:38 +00:00
|
|
|
apk_deps_del(&ctx->world, pkg0->name);
|
2013-06-15 17:39:05 +00:00
|
|
|
if (ctx->recursive_delete)
|
|
|
|
apk_pkg_foreach_reverse_dependency(
|
|
|
|
pkg0, APK_FOREACH_INSTALLED | APK_DEP_SATISFIES,
|
2013-06-19 11:49:44 +00:00
|
|
|
delete_pkg, pctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void delete_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;
|
2014-05-19 05:49:55 +00:00
|
|
|
struct del_ctx *ctx = (struct del_ctx *) pctx;
|
2013-06-19 11:49:44 +00:00
|
|
|
struct apk_package *pkg;
|
|
|
|
|
2014-12-08 06:30:58 +00:00
|
|
|
if (!name) {
|
2020-10-05 15:52:51 +00:00
|
|
|
apk_err(out, "No such package: %s", match);
|
2014-12-08 06:30:58 +00:00
|
|
|
ctx->errors++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-19 11:49:44 +00:00
|
|
|
pkg = apk_pkg_get_installed(name);
|
|
|
|
if (pkg != NULL)
|
|
|
|
delete_pkg(pkg, NULL, NULL, pctx);
|
2014-05-19 05:49:55 +00:00
|
|
|
else
|
|
|
|
apk_deps_del(&ctx->world, name);
|
2011-09-09 16:41:19 +00:00
|
|
|
}
|
|
|
|
|
2020-10-09 09:44:32 +00:00
|
|
|
static int del_main(void *pctx, struct apk_ctx *ac, struct apk_string_array *args)
|
2011-09-09 16:41:19 +00:00
|
|
|
{
|
2020-10-09 09:44:32 +00:00
|
|
|
struct apk_database *db = ac->db;
|
2011-09-09 16:41:19 +00:00
|
|
|
struct del_ctx *ctx = (struct del_ctx *) pctx;
|
2020-10-05 15:52:51 +00:00
|
|
|
struct not_deleted_ctx ndctx = { .out = &db->ctx->out };
|
2011-09-09 16:41:19 +00:00
|
|
|
struct apk_changeset changeset = {};
|
2013-06-15 17:39:05 +00:00
|
|
|
struct apk_change *change;
|
2021-03-19 10:05:20 +00:00
|
|
|
struct apk_dependency *d;
|
2013-06-19 11:49:44 +00:00
|
|
|
int r = 0;
|
2008-04-17 14:09:13 +00:00
|
|
|
|
2011-09-09 16:41:19 +00:00
|
|
|
apk_dependency_array_copy(&ctx->world, db->world);
|
2013-06-19 11:49:44 +00:00
|
|
|
apk_name_foreach_matching(db, args, apk_foreach_genid(), delete_name, ctx);
|
2014-12-08 06:30:58 +00:00
|
|
|
if (ctx->errors) return ctx->errors;
|
|
|
|
|
2013-06-11 11:06:06 +00:00
|
|
|
r = apk_solver_solve(db, 0, ctx->world, &changeset);
|
2013-06-12 14:00:43 +00:00
|
|
|
if (r == 0) {
|
2011-09-09 16:41:19 +00:00
|
|
|
/* check for non-deleted package names */
|
2013-06-19 11:49:44 +00:00
|
|
|
foreach_array_item(change, changeset.changes)
|
|
|
|
if (change->new_pkg != NULL)
|
|
|
|
change->new_pkg->marked = 1;
|
2021-03-19 10:05:20 +00:00
|
|
|
foreach_array_item(d, ctx->world)
|
|
|
|
d->name->state_int = 1;
|
2013-07-08 12:18:10 +00:00
|
|
|
apk_name_foreach_matching(
|
|
|
|
db, args,
|
|
|
|
apk_foreach_genid() | APK_FOREACH_MARKED | APK_DEP_SATISFIES,
|
|
|
|
print_not_deleted_name, &ndctx);
|
2011-09-09 16:41:19 +00:00
|
|
|
if (ndctx.header)
|
|
|
|
printf("\n");
|
2013-06-19 11:49:44 +00:00
|
|
|
|
|
|
|
r = apk_solver_commit_changeset(db, &changeset, ctx->world);
|
2011-09-09 16:41:19 +00:00
|
|
|
} else {
|
2013-06-11 11:06:06 +00:00
|
|
|
apk_solver_print_errors(db, &changeset, ctx->world);
|
2009-04-16 12:37:53 +00:00
|
|
|
}
|
2018-01-03 07:30:12 +00:00
|
|
|
apk_change_array_free(&changeset.changes);
|
2011-09-09 16:41:19 +00:00
|
|
|
apk_dependency_array_free(&ctx->world);
|
2008-04-17 14:09:13 +00:00
|
|
|
|
2009-04-14 15:48:02 +00:00
|
|
|
return r;
|
2008-04-17 14:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct apk_applet apk_del = {
|
|
|
|
.name = "del",
|
2018-07-18 10:28:49 +00:00
|
|
|
.open_flags = APK_OPENF_WRITE | APK_OPENF_NO_AUTOUPDATE,
|
2011-09-09 16:41:19 +00:00
|
|
|
.context_size = sizeof(struct del_ctx),
|
2014-10-08 12:29:27 +00:00
|
|
|
.optgroups = { &optgroup_global, &optgroup_commit, &optgroup_applet },
|
2008-04-17 14:09:13 +00:00
|
|
|
.main = del_main,
|
|
|
|
};
|
|
|
|
|
|
|
|
APK_DEFINE_APPLET(apk_del);
|