del.c 1.48 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/* del.c - Alpine Package Keeper (APK)
 *
 * Copyright (C) 2005-2008 Natanael Copa <n@tanael.org>
 * Copyright (C) 2008 Timo Teräs <timo.teras@iki.fi>
 * 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 <stdio.h>
#include "apk_applet.h"
14
#include "apk_state.h"
15 16
#include "apk_database.h"

Timo Teräs's avatar
Timo Teräs committed
17
static int del_main(void *ctx, int argc, char **argv)
18 19
{
	struct apk_database db;
20 21
	struct apk_state *state;
	struct apk_name *name;
22
	int i, r;
23

24
	if (apk_db_open(&db, apk_root, APK_OPENF_WRITE) < 0)
25
		return -1;
26 27 28 29 30

	if (db.world == NULL)
		goto out;

	for (i = 0; i < argc; i++) {
31
		name = apk_db_get_name(&db, APK_BLOB_STR(argv[i]));
32
		name->flags &= ~APK_NAME_TOPLEVEL;
33
		apk_deps_del(&db.world, name);
34
	}
35

36 37 38 39 40
	state = apk_state_new(&db);
	for (i = 0; i < argc; i++) {
		struct apk_dependency dep;

		name = apk_db_get_name(&db, APK_BLOB_STR(argv[i]));
41 42 43 44 45 46 47 48 49 50 51 52 53 54
		dep = (struct apk_dependency) {
			.name = name,
			.result_mask = APK_DEPMASK_CONFLICT,
		};

		r = apk_state_lock_dependency(state, &dep);
		if (r != 0) {
			apk_error("Unable to remove '%s'", name->name);
			goto err;
		}
	}
	r = apk_state_commit(state, &db);
err:
	apk_state_unref(state);
55
out:
56
	apk_db_close(&db);
57

58
	return r;
59 60 61 62 63 64 65 66 67 68
}

static struct apk_applet apk_del = {
	.name = "del",
	.usage = "apkname...",
	.main = del_main,
};

APK_DEFINE_APPLET(apk_del);