index.c 6.76 KB
Newer Older
1 2 3
/* index.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
#include <stdio.h>
14
#include <fcntl.h>
15
#include <unistd.h>
16
#include <sys/stat.h>
17 18 19

#include "apk_applet.h"
#include "apk_database.h"
Natanael Copa's avatar
Natanael Copa committed
20
#include "apk_print.h"
21 22 23 24 25

struct counts {
	int unsatisfied;
};

26
struct index_ctx {
27
	const char *index;
28
	const char *output;
29
	const char *description;
30
	apk_blob_t *rewrite_arch;
31
	time_t index_mtime;
32
	int method;
33 34
};

35
static int option_parse_applet(void *ctx, struct apk_db_options *dbopts, int optch, const char *optarg)
36 37 38 39
{
	struct index_ctx *ictx = (struct index_ctx *) ctx;

	switch (optch) {
40 41
	case 'x':
		ictx->index = optarg;
42
		break;
43 44 45
	case 'o':
		ictx->output = optarg;
		break;
46 47 48
	case 'd':
		ictx->description = optarg;
		break;
49 50 51
	case 0x10000:
		ictx->rewrite_arch = apk_blob_atomize(APK_BLOB_STR(optarg));
		break;
52
	default:
53
		return -ENOTSUP;
54 55 56 57
	}
	return 0;
}

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
static const struct apk_option options_applet[] = {
	{ 'o', "output", "Write the generated index to FILE",
	  required_argument, "FILE" },
	{ 'x', "index", "Read INDEX to speed up new index creation by reusing "
	  "the information from an old index",
	  required_argument, "INDEX" },
	{ 'd', "description", "Embed TEXT as description and version "
	  "information of the repository index",
	  required_argument, "TEXT" },
	{ 0x10000, "rewrite-arch", "Use ARCH as architecture for all packages",
	  required_argument, "ARCH" },
};

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

78 79
static int index_read_file(struct apk_database *db, struct index_ctx *ictx)
{
80
	struct apk_file_info fi;
81

82
	if (ictx->index == NULL)
83
		return 0;
84
	if (apk_fileinfo_get(AT_FDCWD, ictx->index, APK_CHECKSUM_NONE, &fi) < 0)
85
		return 0;
86

87
	ictx->index_mtime = fi.mtime;
88
	return apk_db_index_read_file(db, ictx->index, 0);
89 90
}

91 92 93 94 95
static int warn_if_no_providers(apk_hash_item item, void *ctx)
{
	struct counts *counts = (struct counts *) ctx;
	struct apk_name *name = (struct apk_name *) item;

96 97 98 99 100 101 102 103 104
	if (!name->is_dependency) return 0;
	if (name->providers->num) return 0;

	if (++counts->unsatisfied < 10) {
		apk_warning("No provider for dependency '%s'",
			    name->name);
	} else if (counts->unsatisfied == 10) {
		apk_warning("Too many unsatisfiable dependencies, "
			    "not reporting the rest.");
105 106 107 108 109
	}

	return 0;
}

110
static int index_main(void *ctx, struct apk_database *db, struct apk_string_array *args)
111
{
112
	struct counts counts = {0};
Timo Teräs's avatar
Timo Teräs committed
113
	struct apk_ostream *os;
114
	struct apk_file_info fi;
115
	int total, r, found, newpkgs = 0, errors = 0;
116
	struct index_ctx *ictx = (struct index_ctx *) ctx;
117
	struct apk_package *pkg;
118
	char **parg;
119

120
	if (isatty(STDOUT_FILENO) && ictx->output == NULL &&
121 122 123
	    !(apk_force & APK_FORCE_BINARY_STDOUT)) {
		apk_error("Will not write binary index to console. "
			  "Use --force-binary-stdout to override.");
124 125 126 127 128 129
		return -1;
	}

	if (ictx->method == 0)
		ictx->method = APK_SIGN_GENERATE;

130
	if ((r = index_read_file(db, ictx)) < 0) {
131 132
		apk_error("%s: %s", ictx->index, apk_error_str(r));
		return r;
133
	}
134

135
	foreach_array_item(parg, args) {
136
		if (apk_fileinfo_get(AT_FDCWD, *parg, APK_CHECKSUM_NONE, &fi) < 0) {
137
			apk_warning("File '%s' is unaccessible", *parg);
138 139 140 141 142
			continue;
		}

		found = FALSE;
		do {
143
			struct apk_provider *p;
144
			struct apk_name *name;
145 146 147 148 149 150 151 152
			char *fname, *fend;
			apk_blob_t bname, bver;

			/* Check if index is newer than package */
			if (ictx->index == NULL || ictx->index_mtime < fi.mtime)
				break;

			/* Check that it looks like a package name */
153
			fname = strrchr(*parg, '/');
154
			if (fname == NULL)
155
				fname = *parg;
156 157 158 159 160 161 162 163 164 165
			else
				fname++;
			fend = strstr(fname, ".apk");
			if (fend == NULL)
				break;
			if (apk_pkg_parse_name(APK_BLOB_PTR_PTR(fname, fend-1),
					       &bname, &bver) < 0)
				break;

			/* If we have it in the old index already? */
166
			name = apk_db_query_name(db, bname);
Timo Teräs's avatar
Timo Teräs committed
167
			if (name == NULL)
168 169
				break;

170 171
			foreach_array_item(p, name->providers) {
				pkg = p->pkg;
172 173
				if (pkg->name != name)
					continue;
174
				if (apk_blob_compare(bver, *pkg->version) != 0)
175 176 177
					continue;
				if (pkg->size != fi.size)
					continue;
178
				pkg->filename = strdup(*parg);
179 180
				if (ictx->rewrite_arch != NULL)
					pkg->arch = ictx->rewrite_arch;
181 182 183 184 185 186
				found = TRUE;
				break;
			}
		} while (0);

		if (!found) {
Timo Teräs's avatar
Timo Teräs committed
187
			struct apk_sign_ctx sctx;
188
			apk_sign_ctx_init(&sctx, ictx->method, NULL, db->keys_fd);
189
			r = apk_pkg_read(db, *parg, &sctx, &pkg);
190
			if (r < 0) {
191
				apk_error("%s: %s", *parg, apk_error_str(r));
192 193
				errors++;
			} else {
194
				newpkgs++;
195 196
				if (ictx->rewrite_arch != NULL)
					pkg->arch = ictx->rewrite_arch;
197
			}
Timo Teräs's avatar
Timo Teräs committed
198
			apk_sign_ctx_free(&sctx);
199
		}
200
	}
201 202
	if (errors)
		return -1;
Timo Teräs's avatar
Timo Teräs committed
203

204
	if (ictx->output != NULL)
205
		os = apk_ostream_to_file(AT_FDCWD, ictx->output, NULL, 0644);
206 207
	else
		os = apk_ostream_to_fd(STDOUT_FILENO);
208
	if (IS_ERR_OR_NULL(os)) return -1;
209

210
	if (ictx->method == APK_SIGN_GENERATE) {
211 212 213 214 215 216
		struct apk_ostream *counter;

		memset(&fi, 0, sizeof(fi));
		fi.mode = 0644 | S_IFREG;
		fi.name = "APKINDEX";
		counter = apk_ostream_counter(&fi.size);
217
		r = apk_db_index_write(db, counter);
218
		apk_ostream_close(counter);
219

220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
		if (r >= 0) {
			os = apk_ostream_gzip(os);
			if (ictx->description != NULL) {
				struct apk_file_info fi_desc;
				memset(&fi_desc, 0, sizeof(fi));
				fi_desc.mode = 0644 | S_IFREG;
				fi_desc.name = "DESCRIPTION";
				fi_desc.size = strlen(ictx->description);
				apk_tar_write_entry(os, &fi_desc, ictx->description);
			}

			apk_tar_write_entry(os, &fi, NULL);
			r = apk_db_index_write(db, os);
			apk_tar_write_padding(os, &fi);

			apk_tar_write_entry(os, NULL, NULL);
		}
237
	} else {
238
		r = apk_db_index_write(db, os);
239
	}
240
	apk_ostream_close(os);
Timo Teräs's avatar
Timo Teräs committed
241

242
	if (r < 0) {
243
		apk_error("Index generation failed: %s", apk_error_str(r));
244
		return r;
245 246
	}

247
	total = r;
248
	apk_hash_foreach(&db->available.names, warn_if_no_providers, &counts);
249 250 251

	if (counts.unsatisfied != 0)
		apk_warning("Total of %d unsatisfiable package "
252
			    "names. Your repository may be broken.",
253
			    counts.unsatisfied);
254 255
	apk_message("Index has %d packages (of which %d are new)",
		    total, newpkgs);
256 257 258 259 260 261

	return 0;
}

static struct apk_applet apk_index = {
	.name = "index",
262
	.help = "Create repository index file from FILEs",
Timo Teräs's avatar
Timo Teräs committed
263
	.arguments = "FILE...",
264
	.open_flags = APK_OPENF_READ | APK_OPENF_NO_STATE | APK_OPENF_NO_REPOS,
265
	.command_groups = APK_COMMAND_GROUP_REPO,
266
	.context_size = sizeof(struct index_ctx),
267
	.optgroups = { &optgroup_global, &optgroup_applet },
268 269 270 271 272
	.main = index_main,
};

APK_DEFINE_APPLET(apk_index);