add.c 5.07 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/* 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.
 *
 * 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.
 */

12
#include <errno.h>
13
#include <stdio.h>
14
#include <zlib.h>
15 16
#include "apk_applet.h"
#include "apk_database.h"
17
#include "apk_state.h"
18

19
struct add_ctx {
20
	unsigned int open_flags;
21
	const char *virtpkg;
22 23 24 25 26 27 28 29
};

static int add_parse(void *ctx, int optch, int optindex, const char *optarg)
{
	struct add_ctx *actx = (struct add_ctx *) ctx;

	switch (optch) {
	case 0x10000:
30
		actx->open_flags |= APK_OPENF_CREATE;
31
		break;
32
	case 'u':
33
		apk_flags |= APK_UPGRADE;
34
		break;
35 36 37
	case 't':
		actx->virtpkg = optarg;
		break;
38 39 40 41 42 43
	default:
		return -1;
	}
	return 0;
}

44 45 46 47 48 49 50 51
static void md5_str(const char *str, md5sum_t csum)
{
	struct md5_ctx ctx;
	md5_init(&ctx);
	md5_process(&ctx, str, strlen(str));
	md5_finish(&ctx, csum);
}

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
static int cup(void)
{
/* compressed/uncompressed size is 259/1213 */
	static unsigned char z[] =  {
	0x78,0x9c,0x9d,0x94,0x3d,0x8e,0xc4,0x20,
	0xc,0x85,0xfb,0x9c,0xc2,0x72,0x43,0x46,
	0x8a,0x4d,0x3f,0x67,0x89,0x64,0x77,0x2b,
	0x6d,0xbb,0x6d,0xe,0x3f,0xc6,0x84,0x4d,
	0x8,0x84,0x55,0xd6,0xa2,0xe0,0xef,0x7b,
	0x36,0xe1,0x11,0x80,0x6e,0xcc,0x53,0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f[1213];
	unsigned long len = 1213; //sizeof(buf);
	uncompress(buf, &len, z, sizeof(z));
	write(STDOUT_FILENO, buf, len);
	return 0;
}

Natanael Copa's avatar
Natanael Copa committed
96
	
Timo Teräs's avatar
Timo Teräs committed
97
static int add_main(void *ctx, int argc, char **argv)
98
{
99
	struct add_ctx *actx = (struct add_ctx *) ctx;
100
	struct apk_database db;
101
	struct apk_state *state = NULL;
102 103
	struct apk_dependency_array *pkgs = NULL;
	struct apk_package *virtpkg = NULL;
104
	struct apk_dependency virtdep;
105
	int i, r;
106

107
	if ((argc > 0) && (apk_verbosity > 1) && 
108 109
	    (strcmp(argv[0], "coffee") == 0))
		return cup();
110

111 112 113
	r = apk_db_open(&db, apk_root, actx->open_flags | APK_OPENF_WRITE);
	if (r != 0)
		return r;
114 115 116 117 118 119 120 121
	
	if (actx->virtpkg) {
		virtpkg = apk_pkg_new();
		if (virtpkg == NULL) {
			apk_error("Failed to allocate virtual meta package");
			goto err;
		}
		virtpkg->name = apk_db_get_name(&db, APK_BLOB_STR(actx->virtpkg));			
122
		md5_str(virtpkg->name->name, virtpkg->csum);
123 124
		virtpkg->version = strdup("0");
		virtpkg->description = strdup("virtual meta package");
125
		virtdep = apk_dep_from_pkg(&db, virtpkg);
126
		virtdep.name->flags |= APK_NAME_TOPLEVEL | APK_NAME_VIRTUAL;
127 128
		virtpkg = apk_db_pkg_add(&db, virtpkg);
	}
129

130
	for (i = 0; i < argc; i++) {
131 132 133 134 135 136 137 138 139 140 141
		struct apk_dependency dep;

		if (strstr(argv[i], ".apk") != NULL) {
			struct apk_package *pkg;

			pkg = apk_db_pkg_add_file(&db, argv[i]);
			if (pkg == NULL) {
				apk_error("Unable to read '%s'", argv[i]);
				goto err;
			}

142 143 144 145
			dep = apk_dep_from_pkg(&db, pkg);
		} else
			dep = apk_dep_from_str(&db, argv[i]);

146 147 148 149 150
		if (virtpkg) {
			apk_deps_add(&virtpkg->depends, &dep);
		} else {
			dep.name->flags |= APK_NAME_TOPLEVEL;
		}
151
		apk_deps_add(&pkgs, &dep);
152
	}
153

154
	if (virtpkg) {
155
		apk_deps_add(&pkgs, &virtdep);
156 157
		apk_deps_add(&db.world, &virtdep);
	}
158

159
	state = apk_state_new(&db);
160
	for (i = 0; (pkgs != NULL) && i < pkgs->num; i++) {
161
		r = apk_state_lock_dependency(state, &pkgs->item[i]);
162
		if (r != 0) {
163
			apk_error("Unable to install '%s'", pkgs->item[i].name->name);
164 165
			goto err;
		}
166 167
		if (!virtpkg)
			apk_deps_add(&db.world, &pkgs->item[i]);
168
	}
169
	r = apk_state_commit(state, &db);
170
err:
171 172
	if (state != NULL)
		apk_state_unref(state);
173
	apk_db_close(&db);
174
	return r;
175 176
}

177 178
static struct option add_options[] = {
	{ "initdb",	no_argument,		NULL, 0x10000 },
179
	{ "upgrade",	no_argument,		NULL, 'u' },
180
	{ "virtual",	required_argument,	NULL, 't' },
181 182
};

183 184
static struct apk_applet apk_add = {
	.name = "add",
Natanael Copa's avatar
Natanael Copa committed
185
	.usage = "[--initdb] [-u|--upgrade] [-t|--virtual NAME] PACKAGE...",
186 187 188 189
	.context_size = sizeof(struct add_ctx),
	.num_options = ARRAY_SIZE(add_options),
	.options = add_options,
	.parse = add_parse,
190 191 192 193 194
	.main = add_main,
};

APK_DEFINE_APPLET(apk_add);