add.c 3.97 KB
Newer Older
1 2 3 4 5 6
/* add.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.
 *
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
 * 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.
 */

12
#include <errno.h>
13 14 15
#include <stdio.h>
#include "apk_applet.h"
#include "apk_database.h"
Natanael Copa's avatar
Natanael Copa committed
16
#include "apk_print.h"
Timo Teräs's avatar
Timo Teräs committed
17
#include "apk_solver.h"
18

19
struct add_ctx {
20
	const char *virtpkg;
Timo Teräs's avatar
Timo Teräs committed
21
	unsigned short solver_flags;
22 23
};

24 25
static int add_parse(void *ctx, struct apk_db_options *dbopts,
		     int optch, int optindex, const char *optarg)
26 27 28 29 30
{
	struct add_ctx *actx = (struct add_ctx *) ctx;

	switch (optch) {
	case 0x10000:
31
		dbopts->open_flags |= APK_OPENF_CREATE;
32
		break;
33
	case 'u':
Timo Teräs's avatar
Timo Teräs committed
34
		actx->solver_flags |= APK_SOLVERF_UPGRADE;
35
		break;
36 37 38
	case 't':
		actx->virtpkg = optarg;
		break;
39 40 41 42 43 44
	default:
		return -1;
	}
	return 0;
}

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
static int non_repository_check(struct apk_database *db)
{
	if (apk_flags & APK_FORCE)
		return 0;
	if (apk_db_cache_active(db))
		return 0;
	if (apk_db_permanent(db))
		return 0;

	apk_error("You tried to add a non-repository package to system, "
		  "but it would be lost on next reboot. Enable package caching "
		  "(apk cache --help) or use --force if you know what you are "
		  "doing.");
	return 1;
}

61
static int add_main(void *ctx, struct apk_database *db, int argc, char **argv)
62
{
63
	struct add_ctx *actx = (struct add_ctx *) ctx;
64
	struct apk_package *virtpkg = NULL;
65
	struct apk_dependency virtdep;
Timo Teräs's avatar
Timo Teräs committed
66 67 68 69
	struct apk_dependency_array *world = NULL;
	int i, r = 0;

	apk_dependency_array_copy(&world, db->world);
Timo Teräs's avatar
Timo Teräs committed
70

71
	if (actx->virtpkg) {
72
		if (non_repository_check(db))
73
			return -1;
74

75 76 77
		virtpkg = apk_pkg_new();
		if (virtpkg == NULL) {
			apk_error("Failed to allocate virtual meta package");
78
			return -1;
79
		}
80
		virtpkg->name = apk_db_get_name(db, APK_BLOB_STR(actx->virtpkg));
Timo Teräs's avatar
Timo Teräs committed
81
		apk_blob_checksum(APK_BLOB_STR(virtpkg->name->name),
82
				  apk_checksum_default(), &virtpkg->csum);
83
		virtpkg->version = apk_blob_atomize(APK_BLOB_STR("0"));
84
		virtpkg->description = strdup("virtual meta package");
85
		virtpkg->arch = apk_blob_atomize(APK_BLOB_STR("noarch"));
86 87
		apk_dep_from_pkg(&virtdep, db, virtpkg);
		virtpkg = apk_db_pkg_add(db, virtpkg);
Timo Teräs's avatar
Timo Teräs committed
88
	}
89

90
	for (i = 0; i < argc; i++) {
91 92 93
		struct apk_dependency dep;

		if (strstr(argv[i], ".apk") != NULL) {
94
			struct apk_package *pkg = NULL;
Timo Teräs's avatar
Timo Teräs committed
95
			struct apk_sign_ctx sctx;
96

97
			if (non_repository_check(db))
98
				return -1;
99

100
			apk_sign_ctx_init(&sctx, APK_SIGN_VERIFY_AND_GENERATE,
101 102
					  NULL, db->keys_fd);
			r = apk_pkg_read(db, argv[i], &sctx, &pkg);
Timo Teräs's avatar
Timo Teräs committed
103
			apk_sign_ctx_free(&sctx);
104 105
			if (r != 0) {
				apk_error("%s: %s", argv[i], apk_error_str(r));
106
				return -1;
107
			}
108
			apk_dep_from_pkg(&dep, db, pkg);
Timo Teräs's avatar
Timo Teräs committed
109
		} else {
110
			r = apk_dep_from_blob(&dep, db, APK_BLOB_STR(argv[i]));
Timo Teräs's avatar
Timo Teräs committed
111
			if (r != 0)
112
				return -1;
Timo Teräs's avatar
Timo Teräs committed
113
		}
114

115
		if (virtpkg)
116
			apk_deps_add(&virtpkg->depends, &dep);
Timo Teräs's avatar
Timo Teräs committed
117 118
		else
			apk_deps_add(&world, &dep);
119
	}
120
	if (virtpkg)
Timo Teräs's avatar
Timo Teräs committed
121
		apk_deps_add(&world, &virtdep);
122

Timo Teräs's avatar
Timo Teräs committed
123 124
	r = apk_solver_commit(db, actx->solver_flags, world);
	apk_dependency_array_free(&world);
125

126
	return r;
127 128
}

Timo Teräs's avatar
Timo Teräs committed
129 130 131 132 133 134 135 136
static struct apk_option add_options[] = {
	{ 0x10000,	"initdb",	"Initialize database" },
	{ 'u',		"upgrade",	"Prefer to upgrade package" },
	{ 't',		"virtual",
	  "Instead of adding all the packages to 'world', create a new virtual "
	  "package with the listed dependencies and add that to 'world'. The "
	  "actions of the command are easily reverted by deleting the virtual "
	  "package.", required_argument, "NAME" },
137 138
};

139 140
static struct apk_applet apk_add = {
	.name = "add",
Timo Teräs's avatar
Timo Teräs committed
141 142 143
	.help = "Add (or update) PACKAGEs to main dependencies and install "
		"them, while ensuring that all dependencies are met.",
	.arguments = "PACKAGE...",
144
	.open_flags = APK_OPENF_WRITE,
145 146 147 148
	.context_size = sizeof(struct add_ctx),
	.num_options = ARRAY_SIZE(add_options),
	.options = add_options,
	.parse = add_parse,
149 150 151 152
	.main = add_main,
};

APK_DEFINE_APPLET(apk_add);