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

#include <stdio.h>
13
#include <fcntl.h>
14 15 16 17 18
#include <unistd.h>

#include "apk_applet.h"
#include "apk_database.h"

19 20
#define INDEX_OLD_FORMAT	0x10000

21 22 23 24
struct counts {
	int unsatisfied;
};

25
struct index_ctx {
26
	const char *index;
27
	const char *output;
28
	time_t index_mtime;
29
	int method;
30 31 32 33 34 35 36
};

static int index_parse(void *ctx, int optch, int optindex, const char *optarg)
{
	struct index_ctx *ictx = (struct index_ctx *) ctx;

	switch (optch) {
37 38
	case 'x':
		ictx->index = optarg;
39
		break;
40 41 42 43 44 45
	case 'o':
		ictx->output = optarg;
		break;
	case INDEX_OLD_FORMAT:
		ictx->method = APK_SIGN_GENERATE_V1;
		break;
46 47 48 49 50 51 52 53
	default:
		return -1;
	}
	return 0;
}

static int index_read_file(struct apk_database *db, struct index_ctx *ictx)
{
54
	struct apk_file_info fi;
55

56
	if (ictx->index == NULL)
57
		return 0;
58 59 60
	if (apk_file_get_info(ictx->index, APK_CHECKSUM_NONE, &fi) < 0)
		return -1;
	ictx->index_mtime = fi.mtime;
61 62

	return apk_db_index_read_file(db, ictx->index, 0);
63 64
}

65 66 67 68 69 70
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;

	if (name->pkgs == NULL) {
71
		if (++counts->unsatisfied < 10) {
72 73
			apk_warning("No provider for dependency '%s'",
				    name->name);
74
		} else if (counts->unsatisfied == 10) {
75 76
			apk_warning("Too many unsatisfiable dependencies, "
				    "not reporting the rest.");
77
		}
78 79 80 81 82
	}

	return 0;
}

Timo Teräs's avatar
Timo Teräs committed
83
static int index_main(void *ctx, int argc, char **argv)
84 85
{
	struct apk_database db;
86
	struct counts counts = {0};
Timo Teräs's avatar
Timo Teräs committed
87
	struct apk_ostream *os;
88 89
	struct apk_file_info fi;
	int total, i, j, found, newpkgs = 0;
90
	struct index_ctx *ictx = (struct index_ctx *) ctx;
91

92 93 94 95 96 97 98 99 100 101
	if (isatty(STDOUT_FILENO) && ictx->output == NULL &&
	    !(apk_flags & APK_FORCE)) {
		apk_error("Will not write binary index to console "
			  "without --force");
		return -1;
	}

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

102
	apk_db_open(&db, NULL, APK_OPENF_READ);
103 104 105 106 107
	if (index_read_file(&db, ictx) < 0) {
		apk_db_close(&db);
		apk_error("The index is corrupt, or of unknown format.");
		return -1;
	}
108 109

	for (i = 0; i < argc; i++) {
110 111 112 113 114 115 116
		if (apk_file_get_info(argv[i], APK_CHECKSUM_NONE, &fi) < 0) {
			apk_warning("File '%s' is unaccessible", argv[i]);
			continue;
		}

		found = FALSE;
		do {
117
			struct apk_name *name;
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
			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 */
			fname = strrchr(argv[i], '/');
			if (fname == NULL)
				fname = argv[i];
			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? */
			name = apk_db_query_name(&db, bname);
140
			if (name == NULL || name->pkgs == NULL)
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
				break;

			for (j = 0; j < name->pkgs->num; j++) {
				struct apk_package *pkg = name->pkgs->item[j];
				if (apk_blob_compare(bver, APK_BLOB_STR(pkg->version)) != 0)
					continue;
				if (pkg->size != fi.size)
					continue;
				pkg->filename = strdup(argv[i]);
				found = TRUE;
				break;
			}
		} while (0);

		if (!found) {
Timo Teräs's avatar
Timo Teräs committed
156
			struct apk_sign_ctx sctx;
157
			apk_sign_ctx_init(&sctx, ictx->method, NULL);
Timo Teräs's avatar
Timo Teräs committed
158
			if (apk_pkg_read(&db, argv[i], &sctx) != NULL)
159
				newpkgs++;
Timo Teräs's avatar
Timo Teräs committed
160
			apk_sign_ctx_free(&sctx);
161
		}
162
	}
Timo Teräs's avatar
Timo Teräs committed
163

164 165 166
	if (ictx->method == APK_SIGN_GENERATE) {
		memset(&fi, 0, sizeof(fi));
		fi.name = "APKINDEX";
167
		fi.mode = 0644 | S_IFREG;
168 169 170 171 172 173
		os = apk_ostream_counter(&fi.size);
		apk_db_index_write(&db, os);
		os->close(os);
	}

	if (ictx->output != NULL)
174
		os = apk_ostream_to_file(ictx->output, 0644);
175 176 177 178 179 180
	else
		os = apk_ostream_to_fd(STDOUT_FILENO);
	if (ictx->method == APK_SIGN_GENERATE) {
		os = apk_ostream_gzip(os);
		apk_tar_write_entry(os, &fi, NULL);
	}
181
	total = apk_db_index_write(&db, os);
182 183 184 185
	if (ictx->method == APK_SIGN_GENERATE) {
		apk_tar_write_padding(os, &fi);
		apk_tar_write_entry(os, NULL, NULL);
	}
Timo Teräs's avatar
Timo Teräs committed
186 187
	os->close(os);

188
	apk_hash_foreach(&db.available.names, warn_if_no_providers, &counts);
189
	apk_db_close(&db);
190 191 192 193 194

	if (counts.unsatisfied != 0)
		apk_warning("Total of %d unsatisfiable package "
			    "names. Your repository maybe broken.",
			    counts.unsatisfied);
195 196
	apk_message("Index has %d packages (of which %d are new)",
		    total, newpkgs);
197 198 199 200

	return 0;
}

Timo Teräs's avatar
Timo Teräs committed
201
static struct apk_option index_options[] = {
202 203
	{ 'o', "output", "Write the generated index to FILE",
	  required_argument, "FILE" },
204 205 206
	{ 'x', "index", "Read INDEX to speed up new index creation by reusing "
	  "the information from an old index",
	  required_argument, "INDEX" },
207 208
	{ INDEX_OLD_FORMAT, "old-format",
	  "Specify to create old style index files" }
209 210
};

211 212
static struct apk_applet apk_index = {
	.name = "index",
Timo Teräs's avatar
Timo Teräs committed
213 214
	.help = "Create repository index file from FILEs.",
	.arguments = "FILE...",
215 216 217 218
	.context_size = sizeof(struct index_ctx),
	.num_options = ARRAY_SIZE(index_options),
	.options = index_options,
	.parse = index_parse,
219 220 221 222 223
	.main = index_main,
};

APK_DEFINE_APPLET(apk_index);