librazor/importer.c
changeset 248 057933050c42
child 249 061a5b815727
child 258 29d5002bd17f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/librazor/importer.c	Fri Jun 20 15:10:34 2008 -0400
     1.3 @@ -0,0 +1,488 @@
     1.4 +/*
     1.5 + * Copyright (C) 2008  Kristian Høgsberg <krh@redhat.com>
     1.6 + * Copyright (C) 2008  Red Hat, Inc
     1.7 + *
     1.8 + * This program is free software; you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation; either version 2 of the License, or
    1.11 + * (at your option) any later version.
    1.12 + *
    1.13 + * This program is distributed in the hope that it will be useful,
    1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.16 + * GNU General Public License for more details.
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License along
    1.19 + * with this program; if not, write to the Free Software Foundation, Inc.,
    1.20 + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + */
    1.22 +
    1.23 +#define _GNU_SOURCE
    1.24 +
    1.25 +#include <string.h>
    1.26 +#include "razor-internal.h"
    1.27 +#include "razor.h"
    1.28 +
    1.29 +void
    1.30 +razor_importer_begin_package(struct razor_importer *importer,
    1.31 +			     const char *name,
    1.32 +			     const char *version,
    1.33 +			     const char *arch)
    1.34 +{
    1.35 +	struct razor_package *p;
    1.36 +
    1.37 +	p = array_add(&importer->set->packages, sizeof *p);
    1.38 +	p->name = hashtable_tokenize(&importer->table, name);
    1.39 +	p->flags = 0;
    1.40 +	p->version = hashtable_tokenize(&importer->table, version);
    1.41 +	p->arch = hashtable_tokenize(&importer->table, arch);
    1.42 +
    1.43 +	importer->package = p;
    1.44 +	array_init(&importer->properties);
    1.45 +}
    1.46 +
    1.47 +
    1.48 +void
    1.49 +razor_importer_finish_package(struct razor_importer *importer)
    1.50 +{
    1.51 +	list_set_array(&importer->package->properties,
    1.52 +		       &importer->set->property_pool,
    1.53 +		       &importer->properties,
    1.54 +		       1);
    1.55 +
    1.56 +	array_release(&importer->properties);
    1.57 +}
    1.58 +
    1.59 +void
    1.60 +razor_importer_add_property(struct razor_importer *importer,
    1.61 +			    const char *name,
    1.62 +			    uint32_t flags,
    1.63 +			    const char *version)
    1.64 +{
    1.65 +	struct razor_property *p;
    1.66 +	uint32_t *r;
    1.67 +
    1.68 +	p = array_add(&importer->set->properties, sizeof *p);
    1.69 +	p->name = hashtable_tokenize(&importer->table, name);
    1.70 +	p->flags = flags;
    1.71 +	p->version = hashtable_tokenize(&importer->table, version);
    1.72 +	list_set_ptr(&p->packages, importer->package -
    1.73 +		     (struct razor_package *) importer->set->packages.data);
    1.74 +
    1.75 +	r = array_add(&importer->properties, sizeof *r);
    1.76 +	*r = p - (struct razor_property *) importer->set->properties.data;
    1.77 +
    1.78 +	if (((flags & RAZOR_PROPERTY_TYPE_MASK) == RAZOR_PROPERTY_REQUIRES) &&
    1.79 +	    *name == '/') {
    1.80 +		r = array_add(&importer->file_requires, sizeof *r);
    1.81 +		*r = p->name;
    1.82 +	}
    1.83 +}
    1.84 +
    1.85 +void
    1.86 +razor_importer_add_file(struct razor_importer *importer, const char *name)
    1.87 +{
    1.88 +	struct import_entry *e;
    1.89 +
    1.90 +	e = array_add(&importer->files, sizeof *e);
    1.91 +
    1.92 +	e->package = importer->package -
    1.93 +		(struct razor_package *) importer->set->packages.data;
    1.94 +	e->name = strdup(name);
    1.95 +}
    1.96 +
    1.97 +struct razor_importer *
    1.98 +razor_importer_new(void)
    1.99 +{
   1.100 +	struct razor_importer *importer;
   1.101 +
   1.102 +	importer = zalloc(sizeof *importer);
   1.103 +	importer->set = razor_set_create();
   1.104 +	hashtable_init(&importer->table, &importer->set->string_pool);
   1.105 +
   1.106 +	return importer;
   1.107 +}
   1.108 +
   1.109 +/* Destroy an importer without creating the set. */
   1.110 +void
   1.111 +razor_importer_destroy(struct razor_importer *importer)
   1.112 +{
   1.113 +	/* FIXME: write this */
   1.114 +}
   1.115 +
   1.116 +static int
   1.117 +compare_packages(const void *p1, const void *p2, void *data)
   1.118 +{
   1.119 +	const struct razor_package *pkg1 = p1, *pkg2 = p2;
   1.120 +	struct razor_set *set = data;
   1.121 +	char *pool = set->string_pool.data;
   1.122 +
   1.123 +	/* FIXME: what if the flags are different? */
   1.124 +	if (pkg1->name == pkg2->name)
   1.125 +		return razor_versioncmp(&pool[pkg1->version], &pool[pkg2->version]);
   1.126 +	else
   1.127 +		return strcmp(&pool[pkg1->name], &pool[pkg2->name]);
   1.128 +}
   1.129 +
   1.130 +static int
   1.131 +compare_properties(const void *p1, const void *p2, void *data)
   1.132 +{
   1.133 +	const struct razor_property *prop1 = p1, *prop2 = p2;
   1.134 +	struct razor_set *set = data;
   1.135 +	char *pool = set->string_pool.data;
   1.136 +
   1.137 +	if (prop1->name != prop2->name)
   1.138 +		return strcmp(&pool[prop1->name], &pool[prop2->name]);
   1.139 +	else if (prop1->flags != prop2->flags)
   1.140 +		return prop1->flags - prop2->flags;
   1.141 +	else
   1.142 +		return razor_versioncmp(&pool[prop1->version], &pool[prop2->version]);
   1.143 +}
   1.144 +
   1.145 +static uint32_t *
   1.146 +uniqueify_properties(struct razor_set *set)
   1.147 +{
   1.148 +	struct razor_property *rp, *up, *rp_end;
   1.149 +	struct array *pkgs, *p;
   1.150 +	struct list_head *r;
   1.151 +	uint32_t *map, *rmap;
   1.152 +	int i, count, unique;
   1.153 +
   1.154 +	count = set->properties.size / sizeof(struct razor_property);
   1.155 +	map = razor_qsort_with_data(set->properties.data,
   1.156 +				    count,
   1.157 +				    sizeof(struct razor_property),
   1.158 +				    compare_properties,
   1.159 +				    set);
   1.160 +
   1.161 +	rp_end = set->properties.data + set->properties.size;
   1.162 +	rmap = malloc(count * sizeof *map);
   1.163 +	pkgs = zalloc(count * sizeof *pkgs);
   1.164 +	for (rp = set->properties.data, up = rp, i = 0; rp < rp_end; rp++, i++) {
   1.165 +		if (rp->name != up->name ||
   1.166 +		    rp->flags != up->flags ||
   1.167 +		    rp->version != up->version) {
   1.168 +			up++;
   1.169 +			up->name = rp->name;
   1.170 +			up->flags = rp->flags;
   1.171 +			up->version = rp->version;
   1.172 +		}
   1.173 +
   1.174 +		unique = up - (struct razor_property *) set->properties.data;
   1.175 +		rmap[map[i]] = unique;
   1.176 +		r = array_add(&pkgs[unique], sizeof *r);
   1.177 +		*r = rp->packages;
   1.178 +	}
   1.179 +	free(map);
   1.180 +
   1.181 +	if (up != rp)
   1.182 +		up++;
   1.183 +	set->properties.size = (void *) up - set->properties.data;
   1.184 +	rp_end = up;
   1.185 +	for (rp = set->properties.data, p = pkgs; rp < rp_end; rp++, p++) {
   1.186 +		list_set_array(&rp->packages, &set->package_pool, p, 0);
   1.187 +		array_release(p);
   1.188 +	}
   1.189 +
   1.190 +	free(pkgs);
   1.191 +
   1.192 +	return rmap;
   1.193 +}
   1.194 +
   1.195 +static int
   1.196 +compare_filenames(const void *p1, const void *p2, void *data)
   1.197 +{
   1.198 +	const struct import_entry *e1 = p1;
   1.199 +	const struct import_entry *e2 = p2;
   1.200 +	const char *n1 = e1->name;
   1.201 +	const char *n2 = e2->name;
   1.202 +
   1.203 +	/* Need to make sure that the contents of a directory
   1.204 +	 * are sorted immediately after it. So "foo/bar" has to
   1.205 +	 * sort before "foo.conf"
   1.206 +	 *
   1.207 +	 * FIXME: this is about 60% slower than strcmp
   1.208 +	 */
   1.209 +	while (*n1 && *n2) {
   1.210 +		if (*n1 < *n2)
   1.211 +			return *n2 == '/' ? 1 : -1;
   1.212 +		else if (*n1 > *n2)
   1.213 +			return *n1 == '/' ? -1 : 1;
   1.214 +		n1++;
   1.215 +		n2++;
   1.216 +	}
   1.217 +	if (*n1)
   1.218 +		return 1;
   1.219 +	else if (*n2)
   1.220 +		return -1;
   1.221 +	else
   1.222 +		return 0;
   1.223 +}
   1.224 +
   1.225 +static void
   1.226 +count_entries(struct import_directory *d)
   1.227 +{
   1.228 +	struct import_directory *p, *end;
   1.229 +
   1.230 +	p = d->files.data;
   1.231 +	end = d->files.data + d->files.size;
   1.232 +	d->count = 0;
   1.233 +	while (p < end) {
   1.234 +		count_entries(p);
   1.235 +		d->count += p->count + 1;
   1.236 +		p++;
   1.237 +	}
   1.238 +}
   1.239 +
   1.240 +static void
   1.241 +serialize_files(struct razor_set *set,
   1.242 +		struct import_directory *d, struct array *array)
   1.243 +{
   1.244 +	struct import_directory *p, *end;
   1.245 +	struct razor_entry *e = NULL;
   1.246 +	uint32_t s;
   1.247 +
   1.248 +	p = d->files.data;
   1.249 +	end = d->files.data + d->files.size;
   1.250 +	s = array->size / sizeof *e + d->files.size / sizeof *p;
   1.251 +	while (p < end) {
   1.252 +		e = array_add(array, sizeof *e);
   1.253 +		e->name = p->name;
   1.254 +		e->flags = 0;
   1.255 +		e->start = p->count > 0 ? s : 0;
   1.256 +		s += p->count;
   1.257 +
   1.258 +		list_set_array(&e->packages, &set->package_pool, &p->packages, 0);
   1.259 +		array_release(&p->packages);
   1.260 +		p++;
   1.261 +	}
   1.262 +	if (e != NULL)
   1.263 +		e->flags |= RAZOR_ENTRY_LAST;
   1.264 +
   1.265 +	p = d->files.data;
   1.266 +	end = d->files.data + d->files.size;
   1.267 +	while (p < end) {
   1.268 +		serialize_files(set, p, array);
   1.269 +		p++;
   1.270 +	}
   1.271 +}
   1.272 +
   1.273 +static void
   1.274 +remap_property_package_links(struct array *properties, uint32_t *rmap)
   1.275 +{
   1.276 +	struct razor_property *p, *end;
   1.277 +
   1.278 +	end = properties->data + properties->size;
   1.279 +	for (p = properties->data; p < end; p++)
   1.280 +		list_remap_head(&p->packages, rmap);
   1.281 +}
   1.282 +
   1.283 +static void
   1.284 +build_file_tree(struct razor_importer *importer)
   1.285 +{
   1.286 +	int count, i, length;
   1.287 +	struct import_entry *filenames;
   1.288 +	char *f, *end;
   1.289 +	uint32_t name, *r;
   1.290 +	char dirname[256];
   1.291 +	struct import_directory *d, root;
   1.292 +	struct razor_entry *e;
   1.293 +
   1.294 +	count = importer->files.size / sizeof (struct import_entry);
   1.295 +	razor_qsort_with_data(importer->files.data,
   1.296 +			      count,
   1.297 +			      sizeof (struct import_entry),
   1.298 +			      compare_filenames,
   1.299 +			      NULL);
   1.300 +
   1.301 +	root.name = hashtable_tokenize(&importer->table, "");
   1.302 +	array_init(&root.files);
   1.303 +	array_init(&root.packages);
   1.304 +	root.last = NULL;
   1.305 +
   1.306 +	filenames = importer->files.data;
   1.307 +	for (i = 0; i < count; i++) {
   1.308 +		f = filenames[i].name;
   1.309 +		if (*f != '/')
   1.310 +			continue;
   1.311 +		f++;
   1.312 +
   1.313 +		d = &root;
   1.314 +		while (*f) {
   1.315 +			end = strchr(f, '/');
   1.316 +			if (end == NULL)
   1.317 +				end = f + strlen(f);
   1.318 +			length = end - f;
   1.319 +			memcpy(dirname, f, length);
   1.320 +			dirname[length] ='\0';
   1.321 +			name = hashtable_tokenize(&importer->table, dirname);
   1.322 +			if (d->last == NULL || d->last->name != name) {
   1.323 +				d->last = array_add(&d->files, sizeof *d);
   1.324 +				d->last->name = name;
   1.325 +				d->last->last = NULL;
   1.326 +				array_init(&d->last->files);
   1.327 +				array_init(&d->last->packages);
   1.328 +			}
   1.329 +			d = d->last;
   1.330 +			f = end + 1;
   1.331 +			if (*end == '\0')
   1.332 +				break;
   1.333 +		}
   1.334 +
   1.335 +		r = array_add(&d->packages, sizeof *r);
   1.336 +		*r = filenames[i].package;
   1.337 +		free(filenames[i].name);
   1.338 +	}
   1.339 +
   1.340 +	count_entries(&root);
   1.341 +	e = importer->set->files.data;
   1.342 +	e->name = root.name;
   1.343 +	e->flags = RAZOR_ENTRY_LAST;
   1.344 +	e->start = importer->files.size ? 1 : 0;
   1.345 +	list_set_empty(&e->packages);
   1.346 +
   1.347 +	serialize_files(importer->set, &root, &importer->set->files);
   1.348 +
   1.349 +	array_release(&importer->files);
   1.350 +}
   1.351 +
   1.352 +static void
   1.353 +list_to_array(struct list *list, struct array *array)
   1.354 +{
   1.355 +	uint32_t *item;
   1.356 +
   1.357 +	while (list) {
   1.358 +		 item = array_add(array, sizeof *item);
   1.359 +		 *item = list->data;
   1.360 +		 list = list_next(list);
   1.361 +	}
   1.362 +}
   1.363 +
   1.364 +static int
   1.365 +compare_file_requires(const void *p1, const void *p2, void *data)
   1.366 +{
   1.367 +	uint32_t *f1 = (void *)p1, *f2 = (void *)p2;
   1.368 +	const char *pool = data;
   1.369 +
   1.370 +	return strcmp(&pool[*f1], &pool[*f2]);
   1.371 +}
   1.372 +
   1.373 +static void
   1.374 +find_file_provides(struct razor_importer *importer)
   1.375 +{
   1.376 +	struct razor_property *prop;
   1.377 +	struct razor_entry *top, *entry;
   1.378 +	struct razor_package *packages;
   1.379 +	struct array pkgprops;
   1.380 +	struct list *pkg;
   1.381 +	uint32_t *req, *req_start, *req_end;
   1.382 +	uint32_t *map, *newprop;
   1.383 +	char *pool;
   1.384 +
   1.385 +	pool = importer->set->string_pool.data;
   1.386 +	packages = importer->set->packages.data;
   1.387 +	top = importer->set->files.data;
   1.388 +
   1.389 +	req = req_start = importer->file_requires.data;
   1.390 +	req_end = importer->file_requires.data + importer->file_requires.size;
   1.391 +	map = razor_qsort_with_data(req, req_end - req, sizeof *req,
   1.392 +				    compare_file_requires, pool);
   1.393 +	free(map);
   1.394 +
   1.395 +	for (req = req_start; req < req_end; req++) {
   1.396 +		if (req > req_start && req[0] == req[-1])
   1.397 +			continue;
   1.398 +		entry = razor_set_find_entry(importer->set, top, &pool[*req]);
   1.399 +		if (!entry)
   1.400 +			continue;
   1.401 +
   1.402 +		for (pkg = list_first(&entry->packages, &importer->set->package_pool); pkg; pkg = list_next(pkg)) {
   1.403 +			prop = array_add(&importer->set->properties, sizeof *prop);
   1.404 +			prop->name = *req;
   1.405 +			prop->flags =
   1.406 +				RAZOR_PROPERTY_PROVIDES | RAZOR_PROPERTY_EQUAL;
   1.407 +			prop->version = hashtable_tokenize(&importer->table, "");
   1.408 +			list_set_ptr(&prop->packages, pkg->data);
   1.409 +
   1.410 +			/* Update property list of pkg */
   1.411 +			array_init(&pkgprops);
   1.412 +			list_to_array(list_first(&packages[pkg->data].properties, &importer->set->property_pool), &pkgprops);
   1.413 +			newprop = array_add(&pkgprops, sizeof *newprop);
   1.414 +			*newprop = prop - (struct razor_property *)importer->set->properties.data;
   1.415 +			list_set_array(&packages[pkg->data].properties, &importer->set->property_pool, &pkgprops, 1);
   1.416 +			array_release(&pkgprops);
   1.417 +		}
   1.418 +	}
   1.419 +
   1.420 +	array_release(&importer->file_requires);
   1.421 +}
   1.422 +
   1.423 +static void
   1.424 +build_package_file_lists(struct razor_set *set, uint32_t *rmap)
   1.425 +{
   1.426 +	struct razor_package *p, *packages;
   1.427 +	struct array *pkgs;
   1.428 +	struct razor_entry *e, *end;
   1.429 +	struct list *r;
   1.430 +	uint32_t *q;
   1.431 +	int i, count;
   1.432 +
   1.433 +	count = set->packages.size / sizeof *p;
   1.434 +	pkgs = zalloc(count * sizeof *pkgs);
   1.435 +
   1.436 +	end = set->files.data + set->files.size;
   1.437 +	for (e = set->files.data; e < end; e++) {
   1.438 +		list_remap_head(&e->packages, rmap);
   1.439 +		r = list_first(&e->packages, &set->package_pool);
   1.440 +		while (r) {
   1.441 +			q = array_add(&pkgs[r->data], sizeof *q);
   1.442 +			*q = e - (struct razor_entry *) set->files.data;
   1.443 +			r = list_next(r);
   1.444 +		}
   1.445 +	}
   1.446 +
   1.447 +	packages = set->packages.data;
   1.448 +	for (i = 0; i < count; i++) {
   1.449 +		list_set_array(&packages[i].files, &set->file_pool, &pkgs[i], 0);
   1.450 +		array_release(&pkgs[i]);
   1.451 +	}
   1.452 +	free(pkgs);
   1.453 +}
   1.454 +
   1.455 +struct razor_set *
   1.456 +razor_importer_finish(struct razor_importer *importer)
   1.457 +{
   1.458 +	struct razor_set *set;
   1.459 +	uint32_t *map, *rmap;
   1.460 +	int i, count;
   1.461 +
   1.462 +	build_file_tree(importer);
   1.463 +	find_file_provides(importer);
   1.464 +
   1.465 +	map = uniqueify_properties(importer->set);
   1.466 +	list_remap_pool(&importer->set->property_pool, map);
   1.467 +	free(map);
   1.468 +
   1.469 +	count = importer->set->packages.size / sizeof(struct razor_package);
   1.470 +	map = razor_qsort_with_data(importer->set->packages.data,
   1.471 +				    count,
   1.472 +				    sizeof(struct razor_package),
   1.473 +				    compare_packages,
   1.474 +				    importer->set);
   1.475 +
   1.476 +	rmap = malloc(count * sizeof *rmap);
   1.477 +	for (i = 0; i < count; i++)
   1.478 +		rmap[map[i]] = i;
   1.479 +	free(map);
   1.480 +
   1.481 +	list_remap_pool(&importer->set->package_pool, rmap);
   1.482 +	build_package_file_lists(importer->set, rmap);
   1.483 +	remap_property_package_links(&importer->set->properties, rmap);
   1.484 +	free(rmap);
   1.485 +
   1.486 +	set = importer->set;
   1.487 +	hashtable_release(&importer->table);
   1.488 +	free(importer);
   1.489 +
   1.490 +	return set;
   1.491 +}