add.c 5.16 KB
Newer Older
1 2 3
/* add.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
 * 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"
17
#include "apk_solver.h"
18

19
struct add_ctx {
20
	const char *virtpkg;
21
	unsigned short solver_flags;
22 23
};

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

	switch (optch) {
	case 0x10000:
30
		dbopts->open_flags |= APK_OPENF_CREATE;
31
		break;
32
	case 'u':
33
		actx->solver_flags |= APK_SOLVERF_UPGRADE;
34
		break;
35 36 37
	case 'l':
		actx->solver_flags |= APK_SOLVERF_LATEST;
		break;
38 39 40
	case 't':
		actx->virtpkg = optarg;
		break;
41
	default:
42
		return -ENOTSUP;
43 44 45 46
	}
	return 0;
}

47 48 49
static const struct apk_option options_applet[] = {
	{ 0x10000,	"initdb",	"Initialize database" },
	{ 'u',		"upgrade",	"Prefer to upgrade package" },
50 51 52
        { 'l',		"latest",
	  "Select latest version of package (if it is not pinned), and "
	  "print error if it cannot be installed due to other dependencies" },
53 54 55 56 57 58 59 60 61 62 63 64 65 66
	{ '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" },
};

static const struct apk_option_group optgroup_applet = {
	.name = "Add",
	.options = options_applet,
	.num_options = ARRAY_SIZE(options_applet),
	.parse = option_parse_applet,
};

67 68
static int non_repository_check(struct apk_database *db)
{
69
	if (apk_force & APK_FORCE_NON_REPOSITORY)
70 71 72 73 74 75 76 77
		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 "
78 79
		  "(apk cache --help) or use --force-non-repository "
		  "if you know what you are doing.");
80 81 82
	return 1;
}

83
static int add_main(void *ctx, struct apk_database *db, struct apk_string_array *args)
84
{
85
	struct add_ctx *actx = (struct add_ctx *) ctx;
86
	struct apk_package *virtpkg = NULL;
87
	struct apk_dependency virtdep;
88
	struct apk_dependency_array *world = NULL;
89 90
	char **parg;
	int r = 0;
91 92

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

94
	if (actx->virtpkg) {
95 96 97
		apk_blob_t b = APK_BLOB_STR(actx->virtpkg);

		apk_blob_pull_dep(&b, db, &virtdep);
98 99
		if (APK_BLOB_IS_NULL(b) || virtdep.conflict ||
		    virtdep.result_mask != APK_DEPMASK_ANY ||
100 101 102 103 104
		    virtdep.version != &apk_null_blob) {
			apk_error("%s: bad package specifier");
			return -1;
		}

105 106 107
		if (virtdep.name->name[0] != '.' && non_repository_check(db))
			return -1;

108 109 110
		virtpkg = apk_pkg_new();
		if (virtpkg == NULL) {
			apk_error("Failed to allocate virtual meta package");
111
			return -1;
112
		}
113
		virtpkg->name = virtdep.name;
Timo Teräs's avatar
Timo Teräs committed
114
		apk_blob_checksum(APK_BLOB_STR(virtpkg->name->name),
115
				  apk_checksum_default(), &virtpkg->csum);
116
		virtpkg->version = apk_blob_atomize(APK_BLOB_STR("0"));
117
		virtpkg->description = strdup("virtual meta package");
118
		virtpkg->arch = apk_blob_atomize(APK_BLOB_STR("noarch"));
119
	}
120

121
	foreach_array_item(parg, args) {
122 123
		struct apk_dependency dep;

124
		if (strstr(*parg, ".apk") != NULL) {
125
			struct apk_package *pkg = NULL;
126
			struct apk_sign_ctx sctx;
127

128
			if (non_repository_check(db))
129
				return -1;
130

131
			apk_sign_ctx_init(&sctx, APK_SIGN_VERIFY_AND_GENERATE,
132
					  NULL, db->keys_fd);
133
			r = apk_pkg_read(db, *parg, &sctx, &pkg);
134
			apk_sign_ctx_free(&sctx);
135
			if (r != 0) {
136
				apk_error("%s: %s", *parg, apk_error_str(r));
137
				return -1;
138
			}
139
			apk_dep_from_pkg(&dep, db, pkg);
Timo Teräs's avatar
Timo Teräs committed
140
		} else {
141
			apk_blob_t b = APK_BLOB_STR(*parg);
142

143
			apk_blob_pull_dep(&b, db, &dep);
144 145 146 147
			if (APK_BLOB_IS_NULL(b) || b.len > 0 || (virtpkg != NULL && dep.repository_tag)) {
				apk_error("'%s' is not a valid %s dependency, format is %s",
					  *parg, virtpkg == NULL ? "world" : "child",
					  virtpkg == NULL ? "name(@tag)([<>~=]version)" : "name([<>~=]version)");
148
				return -1;
149
			}
Timo Teräs's avatar
Timo Teräs committed
150
		}
151

152
		if (virtpkg == NULL) {
153
			apk_deps_add(&world, &dep);
154
			apk_solver_set_name_flags(dep.name,
155
						  actx->solver_flags,
156
						  actx->solver_flags);
157 158 159
		} else {
			apk_deps_add(&virtpkg->depends, &dep);
		}
160
	}
161
	if (virtpkg) {
162
		virtpkg = apk_db_pkg_add(db, virtpkg);
163
		apk_deps_add(&world, &virtdep);
164 165 166
		apk_solver_set_name_flags(virtdep.name,
					  actx->solver_flags,
					  actx->solver_flags);
167
	}
168

169
	r = apk_solver_commit(db, 0, world);
170
	apk_dependency_array_free(&world);
171

172
	return r;
173 174 175 176
}

static struct apk_applet apk_add = {
	.name = "add",
177
	.help = "Add PACKAGEs to 'world' and install (or upgrade) "
178
		"them, while ensuring that all dependencies are met",
Timo Teräs's avatar
Timo Teräs committed
179
	.arguments = "PACKAGE...",
180
	.open_flags = APK_OPENF_WRITE,
181
	.command_groups = APK_COMMAND_GROUP_INSTALL,
182
	.context_size = sizeof(struct add_ctx),
183
	.optgroups = { &optgroup_global, &optgroup_commit, &optgroup_applet },
184 185 186 187
	.main = add_main,
};

APK_DEFINE_APPLET(apk_add);