add.c 4.93 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 't':
		actx->virtpkg = optarg;
		break;
38
	default:
39
		return -ENOTSUP;
40 41 42 43
	}
	return 0;
}

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
static const struct apk_option options_applet[] = {
	{ 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" },
};

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

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

77
static int add_main(void *ctx, struct apk_database *db, struct apk_string_array *args)
78
{
79
	struct add_ctx *actx = (struct add_ctx *) ctx;
80
	struct apk_package *virtpkg = NULL;
81
	struct apk_dependency virtdep;
82
	struct apk_dependency_array *world = NULL;
83 84
	char **parg;
	int r = 0;
85 86

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

88
	if (actx->virtpkg) {
89 90 91
		apk_blob_t b = APK_BLOB_STR(actx->virtpkg);

		apk_blob_pull_dep(&b, db, &virtdep);
92 93
		if (APK_BLOB_IS_NULL(b) || virtdep.conflict ||
		    virtdep.result_mask != APK_DEPMASK_ANY ||
94 95 96 97 98
		    virtdep.version != &apk_null_blob) {
			apk_error("%s: bad package specifier");
			return -1;
		}

99 100 101
		if (virtdep.name->name[0] != '.' && non_repository_check(db))
			return -1;

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

116
	foreach_array_item(parg, args) {
117 118
		struct apk_dependency dep;

119
		if (strstr(*parg, ".apk") != NULL) {
120
			struct apk_package *pkg = NULL;
121
			struct apk_sign_ctx sctx;
122

123
			if (non_repository_check(db))
124
				return -1;
125

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

138
			apk_blob_pull_dep(&b, db, &dep);
139 140 141 142
			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)");
143
				return -1;
144
			}
Timo Teräs's avatar
Timo Teräs committed
145
		}
146

147
		if (virtpkg == NULL) {
148
			apk_deps_add(&world, &dep);
149
			apk_solver_set_name_flags(dep.name,
150
						  actx->solver_flags,
151
						  actx->solver_flags);
152 153 154
		} else {
			apk_deps_add(&virtpkg->depends, &dep);
		}
155
	}
156
	if (virtpkg) {
157
		apk_deps_add(&world, &virtdep);
158 159 160
		apk_solver_set_name_flags(virtdep.name,
					  actx->solver_flags,
					  actx->solver_flags);
161
	}
162

163
	r = apk_solver_commit(db, 0, world);
164
	apk_dependency_array_free(&world);
165

166
	return r;
167 168 169 170
}

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

APK_DEFINE_APPLET(apk_add);