del.c 4.72 KB
Newer Older
1 2 3
/* del.c - Alpine Package Keeper (APK)
 *
 * Copyright (C) 2005-2008 Natanael Copa <n@tanael.org>
4
 * Copyright (C) 2008-2011 Timo Teräs <timo.teras@iki.fi>
5 6
 * All rights reserved.
 *
Timo Teräs's avatar
Timo Teräs committed
7
 * This program is free software; you can redistribute it and/or modify it
8 9 10 11 12 13 14
 * 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 <stdio.h>
#include "apk_applet.h"
#include "apk_database.h"
Natanael Copa's avatar
Natanael Copa committed
15
#include "apk_print.h"
Timo Teräs's avatar
Timo Teräs committed
16
#include "apk_solver.h"
17

18 19 20 21 22 23
struct del_ctx {
	int recursive_delete : 1;
	struct apk_dependency_array *world;
};

static int del_parse(void *pctx, struct apk_db_options *db,
24
		     int optch, int optindex, const char *optarg)
25
{
26 27
	struct del_ctx *ctx = (struct del_ctx *) pctx;

28 29
	switch (optch) {
	case 'r':
30 31
		ctx->recursive_delete = 1;
		break;
32 33 34 35 36 37
	default:
		return -1;
	}
	return 0;
}

38 39 40
static void foreach_installed_reverse_dependency(
	struct apk_package *pkg,
	void (*cb)(struct apk_package *rdepend, void *ctx), void *ctx)
41
{
42
	struct apk_name *name;
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
	int i, j, k;

	if (pkg == NULL)
		return;

	name = pkg->name;
	for (i = 0; i < pkg->name->rdepends->num; i++) {
		struct apk_name *name0 = pkg->name->rdepends->item[i];

		for (j = 0; j < name0->pkgs->num; j++) {
			struct apk_package *pkg0 = name0->pkgs->item[j];

			if (pkg0->ipkg == NULL)
				continue;

			for (k = 0; k < pkg0->depends->num; k++) {
				struct apk_dependency *dep = &pkg0->depends->item[k];
				if (dep->name == name &&
				    (dep->result_mask == APK_DEPMASK_CONFLICT ||
				     apk_dep_is_satisfied(dep, pkg)))
					break;
			}
			if (k >= pkg0->depends->num)
				continue;

			cb(pkg0, ctx);
		}
	}
}

struct not_deleted_ctx {
	struct apk_indent indent;
	struct apk_package *pkg;
	int header;
};

static void print_not_deleted_message(struct apk_package *pkg,
				      void *pctx)
{
	struct not_deleted_ctx *ctx = (struct not_deleted_ctx *) pctx;

	if (pkg == NULL)
		return;

	if (pkg->state_ptr == ctx->pkg)
		return;
	pkg->state_ptr = ctx->pkg;

	if (!ctx->header) {
		apk_message("World updated, but the following packages are not removed due to:");
		ctx->header = 1;
	}
	if (!ctx->indent.indent) {
		ctx->indent.x = printf("  %s:", ctx->pkg->name->name);
		ctx->indent.indent = ctx->indent.x + 1;
	}

	apk_print_indented(&ctx->indent, APK_BLOB_STR(pkg->name->name));
	foreach_installed_reverse_dependency(pkg, print_not_deleted_message, pctx);
}

static struct apk_package *name_to_pkg(struct apk_name *name)
{
	int i;

	for (i = 0; i < name->pkgs->num; i++) {
		if (name->pkgs->item[i]->ipkg != NULL)
			return name->pkgs->item[i];
	}

	return NULL;
}

static void delete_from_world(struct apk_package *pkg, void *pctx)
{
	struct del_ctx *ctx = (struct del_ctx *) pctx;

	apk_deps_del(&ctx->world, pkg->name);
	foreach_installed_reverse_dependency(pkg, delete_from_world, pctx);
}

static int del_main(void *pctx, struct apk_database *db, int argc, char **argv)
{
	struct del_ctx *ctx = (struct del_ctx *) pctx;
	struct apk_name **name;
	struct apk_changeset changeset = {};
	struct apk_package_array *solution = NULL;
	struct not_deleted_ctx ndctx = {};
131
	int i, r = 0;
132

133 134 135
	apk_dependency_array_copy(&ctx->world, db->world);

	name = alloca(argc * sizeof(struct apk_name*));
136
	for (i = 0; i < argc; i++) {
137 138 139 140 141 142 143 144
		name[i] = apk_db_get_name(db, APK_BLOB_STR(argv[i]));
		apk_deps_del(&ctx->world, name[i]);
		if (ctx->recursive_delete)
			foreach_installed_reverse_dependency(
					name_to_pkg(name[i]),
					delete_from_world, ctx);
	}

145
	r = apk_solver_solve(db, 0, ctx->world, &solution, &changeset);
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
	if (r == 0 || (apk_flags & APK_FORCE)) {
		/* check for non-deleted package names */
		for (i = 0; i < solution->num; i++) {
			struct apk_package *pkg = solution->item[i];
			pkg->name->state_ptr = pkg;
			pkg->state_int = 0;
		}
		for (i = 0; i < argc; i++) {
			ndctx.pkg = name[i]->state_ptr;
			ndctx.indent.indent = 0;
			foreach_installed_reverse_dependency(
					name[i]->state_ptr,
					print_not_deleted_message, &ndctx);
			if (ndctx.indent.indent)
				printf("\n");
		}
		if (ndctx.header)
			printf("\n");
		apk_solver_commit_changeset(db, &changeset, ctx->world);
		r = 0;
	} else {
		apk_solver_print_errors(db, solution, ctx->world, r);
168
	}
169 170
	apk_package_array_free(&solution);
	apk_dependency_array_free(&ctx->world);
171

172
	return r;
173 174
}

175 176 177 178 179
static struct apk_option del_options[] = {
	{ 'r', "rdepends",	"Recursively delete all top-level reverse "
				"dependencies too." },
};

180 181
static struct apk_applet apk_del = {
	.name = "del",
Timo Teräs's avatar
Timo Teräs committed
182 183
	.help = "Remove PACKAGEs from the main dependencies and uninstall them.",
	.arguments = "PACKAGE...",
184
	.open_flags = APK_OPENF_WRITE,
185
	.context_size = sizeof(struct del_ctx),
186 187 188
	.num_options = ARRAY_SIZE(del_options),
	.options = del_options,
	.parse = del_parse,
189 190 191 192 193
	.main = del_main,
};

APK_DEFINE_APPLET(apk_del);