del.c 3.82 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
struct not_deleted_ctx {
	struct apk_indent indent;
40 41
	struct apk_name *name;
	unsigned int matches;
42 43 44
	int header;
};

45 46 47
static void print_not_deleted_message(
	struct apk_package *pkg0, struct apk_dependency *dep0,
	struct apk_package *pkg, void *pctx)
48 49 50 51 52 53 54 55
{
	struct not_deleted_ctx *ctx = (struct not_deleted_ctx *) pctx;

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

60 61
	apk_print_indented(&ctx->indent, APK_BLOB_STR(pkg0->name->name));
	apk_pkg_foreach_reverse_dependency(pkg0, ctx->matches, print_not_deleted_message, pctx);
62 63
}

64 65 66
static void delete_from_world(
	struct apk_package *pkg0, struct apk_dependency *dep0,
	struct apk_package *pkg, void *pctx)
67 68 69
{
	struct del_ctx *ctx = (struct del_ctx *) pctx;

70
	apk_deps_del(&ctx->world, pkg0->name);
71 72 73 74 75

	if (ctx->recursive_delete)
		apk_pkg_foreach_reverse_dependency(
			pkg0, APK_FOREACH_INSTALLED | APK_DEP_SATISFIES,
			delete_from_world, pctx);
76 77 78 79 80
}

static int del_main(void *pctx, struct apk_database *db, int argc, char **argv)
{
	struct del_ctx *ctx = (struct del_ctx *) pctx;
81
	struct not_deleted_ctx ndctx = {};
82 83
	struct apk_name **name;
	struct apk_changeset changeset = {};
84 85
	struct apk_change *change;
	struct apk_provider *p;
86
	int i, r = 0;
87

88 89 90
	apk_dependency_array_copy(&ctx->world, db->world);

	name = alloca(argc * sizeof(struct apk_name*));
91
	for (i = 0; i < argc; i++) {
92
		name[i] = apk_db_get_name(db, APK_BLOB_STR(argv[i]));
93
		delete_from_world(apk_pkg_get_installed(name[i]), NULL, NULL, ctx);
94 95
	}

96
	r = apk_solver_solve(db, 0, ctx->world, &changeset);
97
	if (r == 0) {
98
		/* check for non-deleted package names */
99 100
		foreach_array_item(change, changeset.changes) {
			struct apk_package *pkg = change->new_pkg;
101 102
			if (pkg == NULL)
				continue;
103
			pkg->marked = 1;
104 105 106
		}
		for (i = 0; i < argc; i++) {
			ndctx.indent.indent = 0;
107 108 109 110 111 112 113
			ndctx.name = name[i];
			ndctx.matches = apk_foreach_genid() | APK_FOREACH_MARKED | APK_DEP_SATISFIES;
			foreach_array_item(p, name[i]->providers) {
				if (!p->pkg->marked)
					continue;
				print_not_deleted_message(p->pkg, NULL, NULL, &ndctx);
			}
114 115 116 117 118 119 120 121
			if (ndctx.indent.indent)
				printf("\n");
		}
		if (ndctx.header)
			printf("\n");
		apk_solver_commit_changeset(db, &changeset, ctx->world);
		r = 0;
	} else {
122
		apk_solver_print_errors(db, &changeset, ctx->world);
123
	}
124
	apk_dependency_array_free(&ctx->world);
125

126
	return r;
127 128
}

129 130 131 132 133
static struct apk_option del_options[] = {
	{ 'r', "rdepends",	"Recursively delete all top-level reverse "
				"dependencies too." },
};

134 135
static struct apk_applet apk_del = {
	.name = "del",
Timo Teräs's avatar
Timo Teräs committed
136 137
	.help = "Remove PACKAGEs from the main dependencies and uninstall them.",
	.arguments = "PACKAGE...",
138
	.open_flags = APK_OPENF_WRITE,
139
	.context_size = sizeof(struct del_ctx),
140 141 142
	.num_options = ARRAY_SIZE(del_options),
	.options = del_options,
	.parse = del_parse,
143 144 145 146 147
	.main = del_main,
};

APK_DEFINE_APPLET(apk_del);