razor.c
author Kristian H?gsberg <krh@redhat.com>
Tue Sep 18 14:16:35 2007 -0400 (2007-09-18)
changeset 26 b1f244b3ff22
parent 24 5963746558e7
child 27 5dbd81809d26
permissions -rw-r--r--
Add makefile rule to download and import yum data.
     1 #define _GNU_SOURCE
     2 
     3 #include <stdlib.h>
     4 #include <stddef.h>
     5 #include <stdio.h>
     6 #include <string.h>
     7 #include <sys/types.h>
     8 #include <sys/stat.h>
     9 #include <sys/mman.h>
    10 #include <unistd.h>
    11 #include <fcntl.h>
    12 #include <errno.h>
    13 
    14 #include <expat.h>
    15 #include "sha1.h"
    16 
    17 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
    18 
    19 struct array {
    20 	void *data;
    21 	int size, alloc;
    22 };
    23 
    24 static void
    25 array_init(struct array *array)
    26 {
    27 	memset(array, 0, sizeof *array);
    28 }
    29 
    30 static void
    31 array_release(struct array *array)
    32 {
    33 	free(array->data);
    34 }
    35 
    36 static void *
    37 array_add(struct array *array, int size)
    38 {
    39 	int alloc;
    40 	void *data, *p;
    41 
    42 	if (array->alloc > 0)
    43 		alloc = array->alloc;
    44 	else
    45 		alloc = 16;
    46 
    47 	while (alloc < array->size + size)
    48 		alloc *= 2;
    49 
    50 	if (array->alloc < alloc) {
    51 		data = realloc(array->data, alloc);
    52 		if (data == NULL)
    53 			return 0;
    54 		array->data = data;
    55 		array->alloc = alloc;
    56 	}
    57 
    58 	p = array->data + array->size;
    59 	array->size += size;
    60 
    61 	return p;
    62 }
    63 
    64 static int
    65 write_to_fd(int fd, void *p, size_t size)
    66 {
    67 	int rest, len;
    68 
    69 	rest = size;
    70 	while (rest > 0) {
    71 		len = write(fd, p, rest);
    72 		if (len < 0)
    73 			return -1;
    74 		rest -= len;
    75 	}
    76 
    77 	return 0;
    78 }
    79 
    80 static int
    81 write_to_file(const char *filename, void *p, size_t size)
    82 {
    83 	int fd, err;
    84 
    85 	fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666);
    86 	if (fd < 0)
    87 		return -1;
    88 	err = write_to_fd(fd, p, size);
    89 	close(fd);
    90 
    91 	return err;
    92 }
    93 
    94 static void *
    95 zalloc(size_t size)
    96 {
    97 	void *p;
    98 
    99 	p = malloc(size);
   100 	memset(p, 0, size);
   101 
   102 	return p;
   103 }
   104 
   105 struct razor_set_section {
   106 	unsigned int type;
   107 	unsigned int offset;
   108 	unsigned int size;
   109 };
   110 
   111 struct razor_set_header {
   112 	unsigned int magic;
   113 	unsigned int version;
   114 	struct razor_set_section sections[0];
   115 };
   116 
   117 #define RAZOR_MAGIC 0x7a7a7a7a
   118 #define RAZOR_VERSION 1
   119 
   120 #define RAZOR_PACKAGES 0
   121 #define RAZOR_REQUIRES 1
   122 #define RAZOR_PROVIDES 2
   123 #define RAZOR_STRING_POOL 3
   124 #define RAZOR_PROPERTY_POOL 4
   125 
   126 struct razor_package {
   127 	unsigned long name;
   128 	unsigned long version;
   129 	unsigned long requires;
   130 	unsigned long provides;
   131 };
   132 
   133 struct razor_property {
   134 	unsigned long name;
   135 	unsigned long version;
   136 	unsigned long packages;
   137 };
   138 
   139 struct razor_set {
   140 	struct array buckets;
   141 	struct array string_pool;
   142 	struct array property_pool;
   143  	struct array packages;
   144  	struct array requires;
   145  	struct array provides;
   146 	struct razor_set_header *header;
   147 };
   148 
   149 struct razor_set_section razor_sections[] = {
   150 	{ RAZOR_PACKAGES,	offsetof(struct razor_set, packages) },
   151 	{ RAZOR_REQUIRES,	offsetof(struct razor_set, requires) },
   152 	{ RAZOR_PROVIDES,	offsetof(struct razor_set, provides) },
   153 	{ RAZOR_STRING_POOL,	offsetof(struct razor_set, string_pool) },
   154 	{ RAZOR_PROPERTY_POOL,	offsetof(struct razor_set, property_pool) },
   155 };
   156 
   157 struct razor_set *
   158 razor_set_create(void)
   159 {
   160 	return zalloc(sizeof(struct razor_set));
   161 }
   162 
   163 struct razor_set *
   164 razor_set_open(const char *filename)
   165 {
   166 	struct razor_set *set;
   167 	struct razor_set_section *s;
   168 	struct stat stat;
   169 	struct array *array;
   170 	int fd;
   171 
   172 	set = zalloc(sizeof *set);
   173 	fd = open(filename, O_RDONLY);
   174 	if (fstat(fd, &stat) < 0)
   175 		return NULL;
   176 	set->header = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
   177 	if (set->header == MAP_FAILED) {
   178 		free(set);
   179 		return NULL;
   180 	}
   181 
   182 	for (s = set->header->sections; ~s->type; s++) {
   183 		if (s->type >= ARRAY_SIZE(razor_sections))
   184 			continue;
   185 		if (s->type != razor_sections[s->type].type)
   186 			continue;
   187 		array = (void *) set + razor_sections[s->type].offset;
   188 		array->data = (void *) set->header + s->offset;
   189 		array->size = s->size;
   190 		array->alloc = s->size;
   191 	}
   192 	close(fd);
   193 
   194 	return set;
   195 }
   196 
   197 void
   198 razor_set_destroy(struct razor_set *set)
   199 {
   200 	unsigned int size;
   201 	struct array *a;
   202 	int i;
   203 
   204 	if (set->header) {
   205 		for (i = 0; set->header->sections[i].type; i++)
   206 			;
   207 		size = set->header->sections[i].type;
   208 		munmap(set->header, size);
   209 		free(set->buckets.data);
   210 	} else {
   211 		for (i = 0; i < ARRAY_SIZE(razor_sections); i++) {
   212 			a = (void *) set + razor_sections[i].offset;
   213 			free(a->data);
   214 		}
   215 		free(set->buckets.data);
   216 	}
   217 
   218 	free(set);
   219 }
   220 
   221 static int
   222 razor_set_write(struct razor_set *set, const char *filename)
   223 {
   224 	char data[4096];
   225 	struct razor_set_header *header = (struct razor_set_header *) data;
   226 	struct array *a;
   227 	unsigned long offset;
   228 	int i, fd;
   229 
   230 	memset(data, 0, sizeof data);
   231 	header->magic = RAZOR_MAGIC;
   232 	header->version = RAZOR_VERSION;
   233 	offset = sizeof data;
   234 
   235 	for (i = 0; i < ARRAY_SIZE(razor_sections); i++) {
   236 		if (razor_sections[i].type != i)
   237 			continue;
   238 		a = (void *) set + razor_sections[i].offset;
   239 		header->sections[i].type = i;
   240 		header->sections[i].offset = offset;
   241 		header->sections[i].size = a->size;
   242 		offset += (a->size + 4095) & ~4095;
   243 	}
   244 
   245 	header->sections[i].type = ~0;
   246 	header->sections[i].offset = 0;
   247 	header->sections[i].size = 0;
   248 
   249 	fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666);
   250 	if (fd < 0)
   251 		return -1;
   252 
   253 	write_to_fd(fd, data, sizeof data);
   254 	for (i = 0; i < ARRAY_SIZE(razor_sections); i++) {
   255 		if (razor_sections[i].type != i)
   256 			continue;
   257 		a = (void *) set + razor_sections[i].offset;
   258 		write_to_fd(fd, a->data, (a->size + 4095) & ~4095);
   259 	}
   260 
   261 	close(fd);
   262 
   263 	return 0;
   264 }
   265 
   266 static unsigned int
   267 hash_string(const char *key)
   268 {
   269 	const char *p;
   270 	unsigned int hash = 0;
   271 
   272 	for (p = key; *p; p++)
   273 		hash = (hash * 617) ^ *p;
   274 
   275 	return hash;
   276 }
   277 
   278 unsigned long
   279 razor_set_lookup(struct razor_set *set, const char *key)
   280 {
   281 	unsigned int mask, start, i;
   282 	unsigned long *b;
   283 	char *pool;
   284 
   285 	pool = set->string_pool.data;
   286 	mask = set->buckets.alloc - 1;
   287 	start = hash_string(key) * sizeof(unsigned long);
   288 
   289 	for (i = 0; i < set->buckets.alloc; i += sizeof *b) {
   290 		b = set->buckets.data + ((start + i) & mask);
   291 
   292 		if (*b == 0)
   293 			return 0;
   294 
   295 		if (strcmp(key, &pool[*b]) == 0)
   296 			return *b;
   297 	}
   298 
   299 	return 0;
   300 }
   301 
   302 static unsigned long
   303 add_to_string_pool(struct razor_set *set, const char *key)
   304 {
   305 	int len;
   306 	char *p;
   307 
   308 	len = strlen(key) + 1;
   309 	p = array_add(&set->string_pool, len);
   310 	memcpy(p, key, len);
   311 
   312 	return p - (char *) set->string_pool.data;
   313 }
   314 
   315 static unsigned long
   316 add_to_property_pool(struct razor_set *set, struct array *properties)
   317 {
   318 	unsigned long  *p;
   319 
   320 	p = array_add(properties, sizeof *p);
   321 	*p = ~0ul;
   322 	p = array_add(&set->property_pool, properties->size);
   323 	memcpy(p, properties->data, properties->size);
   324 
   325 	return p - (unsigned long *) set->property_pool.data;
   326 }
   327 
   328 static void
   329 do_insert(struct razor_set *set, unsigned long value)
   330 {
   331 	unsigned int mask, start, i;
   332 	unsigned long *b;
   333 	const char *key;
   334 
   335 	key = (char *) set->string_pool.data + value;
   336 	mask = set->buckets.alloc - 1;
   337 	start = hash_string(key) * sizeof(unsigned long);
   338 
   339 	for (i = 0; i < set->buckets.alloc; i += sizeof *b) {
   340 		b = set->buckets.data + ((start + i) & mask);
   341 		if (*b == 0) {
   342 			*b = value;
   343 			break;
   344 		}
   345 	}
   346 }
   347 
   348 unsigned long
   349 razor_set_insert(struct razor_set *set, const char *key)
   350 {
   351 	unsigned long value, *buckets, *b, *end;
   352 	int alloc;
   353 
   354 	alloc = set->buckets.alloc;
   355 	array_add(&set->buckets, 4 * sizeof *buckets);
   356 	if (alloc != set->buckets.alloc) {
   357 		end = set->buckets.data + alloc;
   358 		memset(end, 0, set->buckets.alloc - alloc);
   359 		for (b = set->buckets.data; b < end; b++) {
   360 			value = *b;
   361 			if (value != 0) {
   362 				*b = 0;
   363 				do_insert(set, value);
   364 			}
   365 		}
   366 	}
   367 
   368 	value = add_to_string_pool(set, key);
   369 	do_insert (set, value);
   370 
   371 	return value;
   372 }
   373 
   374 unsigned long
   375 razor_set_tokenize(struct razor_set *set, const char *string)
   376 {
   377 	unsigned long token;
   378 
   379 	if (string == NULL)
   380 		return razor_set_tokenize(set, "");
   381 
   382 	token = razor_set_lookup(set, string);
   383 	if (token != 0)
   384 		return token;
   385 
   386 	return razor_set_insert(set, string);
   387 }
   388 
   389 struct import_property_context {
   390 	struct array *all;
   391 	struct array package;
   392 };
   393 
   394 struct import_context {
   395 	struct razor_set *set;
   396 	struct import_property_context requires;
   397 	struct import_property_context provides;
   398 	struct razor_package *package;
   399 	unsigned long *requires_map;
   400 	unsigned long *provides_map;
   401 };
   402 
   403 static void
   404 import_context_add_package(struct import_context *ctx,
   405 			   const char *name, const char *version)
   406 {
   407 	struct razor_package *p;
   408 
   409 	p = array_add(&ctx->set->packages, sizeof *p);
   410 	p->name = razor_set_tokenize(ctx->set, name);
   411 	p->version = razor_set_tokenize(ctx->set, version);
   412 
   413 	ctx->package = p;
   414 	array_init(&ctx->requires.package);
   415 	array_init(&ctx->provides.package);
   416 }
   417 
   418 void
   419 import_context_finish_package(struct import_context *ctx)
   420 {
   421 	struct razor_package *p;
   422 
   423 	p = ctx->package;
   424 	p->requires = add_to_property_pool(ctx->set, &ctx->requires.package);
   425 	p->provides = add_to_property_pool(ctx->set, &ctx->provides.package);
   426 
   427 	array_release(&ctx->requires.package);
   428 	array_release(&ctx->provides.package);
   429 }
   430 
   431 static void
   432 import_context_add_property(struct import_context *ctx,
   433 			    struct import_property_context *pctx,
   434 			    const char *name, const char *version)
   435 {
   436 	struct razor_property *p;
   437 	unsigned long *r;
   438 
   439 	p = array_add(pctx->all, sizeof *p);
   440 	p->name = razor_set_tokenize(ctx->set, name);
   441 	p->version = razor_set_tokenize(ctx->set, version);
   442 	p->packages = ctx->package -
   443 		(struct razor_package *) ctx->set->packages.data;
   444 
   445 	r = array_add(&pctx->package, sizeof *r);
   446 	*r = p - (struct razor_property *) pctx->all->data;
   447 }
   448 
   449 static void
   450 parse_package(struct import_context *ctx, const char **atts, void *data)
   451 {
   452 	const char *name = NULL, *version = NULL;
   453 	int i;
   454 
   455 	for (i = 0; atts[i]; i += 2) {
   456 		if (strcmp(atts[i], "name") == 0)
   457 			name = atts[i + 1];
   458 		else if (strcmp(atts[i], "version") == 0)
   459 			version = atts[i + 1];
   460 	}
   461 
   462 	if (name == NULL || version == NULL) {
   463 		fprintf(stderr, "invalid package tag, "
   464 			"missing name or version attributes\n");
   465 		return;
   466 	}
   467 
   468 	import_context_add_package(ctx, name, version);
   469 }
   470 
   471 static void
   472 parse_property(struct import_context *ctx, const char **atts, void *data)
   473 {
   474 	const char *name = NULL, *version = NULL;
   475 	int i;
   476 
   477 	for (i = 0; atts[i]; i += 2) {
   478 		if (strcmp(atts[i], "name") == 0)
   479 			name = atts[i + 1];
   480 		if (strcmp(atts[i], "version") == 0)
   481 			version = atts[i + 1];
   482 	}
   483 	
   484 	if (name == NULL) {
   485 		fprintf(stderr, "invalid tag, missing name attribute\n");
   486 		return;
   487 	}
   488 
   489 	import_context_add_property(ctx, data, name, version);
   490 }
   491 
   492 static void
   493 start_element(void *data, const char *name, const char **atts)
   494 {
   495 	struct import_context *ctx = data;
   496 
   497 	if (strcmp(name, "package") == 0)
   498 		parse_package(ctx, atts, NULL);
   499 	else if (strcmp(name, "requires") == 0)
   500 		parse_property(ctx, atts, &ctx->requires);
   501 	else if (strcmp(name, "provides") == 0)
   502 		parse_property(ctx, atts, &ctx->provides);
   503 }
   504 
   505 static void
   506 end_element (void *data, const char *name)
   507 {
   508 	struct import_context *ctx = data;
   509 
   510 	if (strcmp(name, "package") == 0)
   511 		import_context_finish_package(ctx);
   512 }
   513 
   514 static char *
   515 sha1_to_hex(const unsigned char *sha1)
   516 {
   517 	static int bufno;
   518 	static char hexbuffer[4][50];
   519 	static const char hex[] = "0123456789abcdef";
   520 	char *buffer = hexbuffer[3 & ++bufno], *buf = buffer;
   521 	int i;
   522 
   523 	for (i = 0; i < 20; i++) {
   524 		unsigned int val = *sha1++;
   525 		*buf++ = hex[val >> 4];
   526 		*buf++ = hex[val & 0xf];
   527 	}
   528 	*buf = '\0';
   529 
   530 	return buffer;
   531 }
   532 
   533 static void
   534 razor_prepare_import(struct import_context *ctx)
   535 {
   536 	memset(ctx, 0, sizeof *ctx);
   537 	ctx->set = razor_set_create();
   538 	ctx->requires.all = &ctx->set->requires;
   539 	ctx->provides.all = &ctx->set->provides;
   540 }
   541 
   542 static int
   543 razor_import(struct import_context *ctx, const char *filename)
   544 {
   545 	SHA_CTX sha1;
   546 	XML_Parser parser;
   547 	int fd;
   548 	void *p;
   549 	struct stat stat;
   550 	char buf[128];
   551 	unsigned char hash[20];
   552 
   553 	fd = open(filename, O_RDONLY);
   554 	if (fstat(fd, &stat) < 0)
   555 		return -1;
   556 	p = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
   557 	if (p == MAP_FAILED)
   558 		return -1;
   559 
   560 	parser = XML_ParserCreate(NULL);
   561 	XML_SetUserData(parser, ctx);
   562 	XML_SetElementHandler(parser, start_element, end_element);
   563 	if (XML_Parse(parser, p, stat.st_size, 1) == XML_STATUS_ERROR) {
   564 		fprintf(stderr,
   565 			"%s at line %d, %s\n",
   566 			XML_ErrorString(XML_GetErrorCode(parser)),
   567 			XML_GetCurrentLineNumber(parser),
   568 			filename);
   569 		return 1;
   570 	}
   571 
   572 	XML_ParserFree(parser);
   573 
   574 	SHA1_Init(&sha1);
   575 	SHA1_Update(&sha1, p, stat.st_size);
   576 	SHA1_Final(hash, &sha1);
   577 
   578 	close(fd);
   579 
   580 	snprintf(buf, sizeof buf, "set/%s", sha1_to_hex(hash));
   581 	if (write_to_file(buf, p, stat.st_size) < 0)
   582 		return -1;
   583 	munmap(p, stat.st_size);
   584 
   585 	return 0;
   586 }
   587 
   588 typedef int (*compare_with_data_func_t)(const void *p1,
   589 					const void *p,
   590 					void *data);
   591 
   592 struct qsort_context {
   593 	size_t size;
   594 	compare_with_data_func_t compare;
   595 	void *data;
   596 };
   597 
   598 static void
   599 qsort_swap(void *p1, void *p2, size_t size)
   600 {
   601 	char buffer[size];
   602 
   603 	memcpy(buffer, p1, size);
   604 	memcpy(p1, p2, size);
   605 	memcpy(p2, buffer, size);
   606 }
   607 
   608 static void
   609 __qsort_with_data(void *base, size_t nelem, unsigned long *map,
   610 		  struct qsort_context *ctx)
   611 {
   612 	void *p, *start, *end, *pivot;
   613 	unsigned long *mp, *mstart, *mend, tmp;
   614 	int left, right, result;
   615 	size_t size = ctx->size;
   616 
   617 	p = base;
   618 	start = base;
   619 	end = base + nelem * size;
   620 	mp = map;
   621 	mstart = map;
   622 	mend = map + nelem;
   623 	pivot = base + (random() % nelem) * size;
   624 
   625 	while (p < end) {
   626 		result = ctx->compare(p, pivot, ctx->data);
   627 		if (result < 0) {
   628 			qsort_swap(p, start, size);
   629 			tmp = *mp;
   630 			*mp = *mstart;
   631 			*mstart = tmp;
   632 			if (start == pivot)
   633 				pivot = p;
   634 			start += size;
   635 			mstart++;
   636 			p += size;
   637 		} else if (result == 0) {
   638 			p += size;
   639 			mp++;
   640 		} else {
   641  			end -= size;
   642 			mend--;
   643 			qsort_swap(p, end, size);
   644 			tmp = *mp;
   645 			*mp = *mstart;
   646 			*mstart = tmp;
   647 			if (end == pivot)
   648 				pivot = p;
   649 		}
   650 	}
   651 
   652 	left = (start - base) / size;
   653 	right = (base + nelem * size - end) / size;
   654 	if (left > 1)
   655 		__qsort_with_data(base, left, map, ctx);
   656 	if (right > 1)
   657 		__qsort_with_data(end, right, mend, ctx);
   658 }
   659 
   660 unsigned long *
   661 qsort_with_data(void *base, size_t nelem, size_t size,
   662 		compare_with_data_func_t compare, void *data)
   663 {
   664 	struct qsort_context ctx;
   665 	unsigned long *map;
   666 	int i;
   667 
   668 	ctx.size = size;
   669 	ctx.compare = compare;
   670 	ctx.data = data;
   671 
   672 	map = malloc(nelem * sizeof (unsigned long));
   673 	for (i = 0; i < nelem; i++)
   674 		map[i] = i;
   675 
   676 	__qsort_with_data(base, nelem, map, &ctx);
   677 
   678 	return map;
   679 }
   680 
   681 static int
   682 compare_packages(const void *p1, const void *p2, void *data)
   683 {
   684 	const struct razor_package *pkg1 = p1, *pkg2 = p2;
   685 	struct razor_set *set = data;
   686 	char *pool = set->string_pool.data;
   687 
   688 	if (pkg1->name == pkg2->name)
   689 		return 0;
   690 	else
   691 		return strcmp(&pool[pkg1->name], &pool[pkg2->name]);
   692 }
   693 
   694 static int
   695 compare_properties(const void *p1, const void *p2, void *data)
   696 {
   697 	const struct razor_property *prop1 = p1, *prop2 = p2;
   698 	struct razor_set *set = data;
   699 	char *pool = set->string_pool.data;
   700 
   701 	if (prop1->name == prop2->name)
   702 		return strcmp(&pool[prop1->version], &pool[prop2->version]);
   703 	else
   704 		return strcmp(&pool[prop1->name], &pool[prop2->name]);
   705 }
   706 
   707 static unsigned long *
   708 uniqueify_properties(struct razor_set *set, struct array *properties)
   709 {
   710 	struct razor_property *rp, *up, *rp_end;
   711 	struct array *pkgs, *p;
   712 	unsigned long *map, *rmap, *r;
   713 	int i, count, unique;
   714 
   715 	count = properties->size / sizeof(struct razor_property);
   716 	map = qsort_with_data(properties->data,
   717 			      count,
   718 			      sizeof(struct razor_property),
   719 			      compare_properties,
   720 			      set);
   721 
   722 	rp_end = properties->data + properties->size;
   723 	rmap = malloc(count * sizeof *map);
   724 	pkgs = zalloc(count * sizeof *pkgs);
   725 	for (rp = properties->data, up = rp, i = 0; rp < rp_end; rp++, i++) {
   726 		if (rp->name != up->name || rp->version != up->version) {
   727 			up++;
   728 			up->name = rp->name;
   729 			up->version = rp->version;
   730 		}
   731 
   732 		unique = up - (struct razor_property *) properties->data;
   733 		rmap[map[i]] = unique;
   734 		r = array_add(&pkgs[unique], sizeof *r);
   735 		*r = rp->packages;
   736 	}
   737 	free(map);
   738 
   739 	up++;
   740 	properties->size = (void *) up - properties->data;
   741 	rp_end = up;
   742 	for (rp = properties->data, p = pkgs; rp < rp_end; rp++, p++) {
   743 		rp->packages = add_to_property_pool(set, p);
   744 		array_release(p);
   745 	}
   746 
   747 	free(pkgs);
   748 
   749 	return rmap;
   750 }
   751 
   752 static void
   753 remap_package_links(struct import_context *ctx)
   754 {
   755 	struct razor_package *p, *end;
   756 	unsigned long *pool, *r;
   757 
   758 	pool = ctx->set->property_pool.data;
   759 	end = ctx->set->packages.data + ctx->set->packages.size;
   760 	for (p = ctx->set->packages.data; p < end; p++) {
   761 		for (r = &pool[p->requires]; ~*r; r++)
   762 			*r = ctx->requires_map[*r];
   763 		for (r = &pool[p->provides]; ~*r; r++)
   764 			*r = ctx->provides_map[*r];
   765 	}
   766 }
   767 
   768 static void
   769 remap_property_links(struct import_context *ctx, unsigned long *map)
   770 {
   771 	struct razor_property *p, *end;
   772 	struct razor_package *rp;
   773 	unsigned long *pool, *r, *rmap;
   774 	int i, count;
   775 
   776 	pool = ctx->set->property_pool.data;
   777 	count = ctx->set->packages.size / sizeof(struct razor_package);
   778 	rmap = malloc(count * sizeof *map);
   779 	rp = ctx->set->packages.data;
   780 	for (i = 0; i < count; i++)
   781 		rmap[map[i]] = i;
   782 
   783 	/* FIXME: This will break if we implement package list sharing
   784 	 * for all properties, since we'll remap those lists more than
   785 	 * once. We should just have a separate pool for property
   786 	 * lists and a separate pool for package lists and remap it as
   787 	 * a flat pool.  Right now, as property lists and package
   788 	 * lists are mixed, we can't do that. */
   789 
   790 	end = ctx->set->requires.data + ctx->set->requires.size;
   791 	for (p = ctx->set->requires.data; p < end; p++)
   792 		for (r = &pool[p->packages]; ~*r; r++)
   793 			*r = rmap[*r];
   794 
   795 	end = ctx->set->provides.data + ctx->set->provides.size;
   796 	for (p = ctx->set->provides.data; p < end; p++)
   797 		for (r = &pool[p->packages]; ~*r; r++)
   798 			*r = rmap[*r];
   799 
   800 	free(rmap);
   801 }
   802 
   803 static struct razor_set *
   804 razor_finish_import(struct import_context *ctx)
   805 {
   806 	unsigned long *map;
   807 	int count;
   808 
   809 	ctx->requires_map = uniqueify_properties(ctx->set, ctx->requires.all);
   810 	ctx->provides_map = uniqueify_properties(ctx->set, ctx->provides.all);
   811 	remap_package_links(ctx);
   812 	free(ctx->requires_map);
   813 	free(ctx->provides_map);
   814 
   815 	count = ctx->set->packages.size / sizeof(struct razor_package);
   816 	map = qsort_with_data(ctx->set->packages.data,
   817 			      count,
   818 			      sizeof(struct razor_package),
   819 			      compare_packages,
   820 			      ctx->set);
   821 	remap_property_links(ctx, map);
   822 	free(map);
   823 
   824 	return ctx->set; 
   825 }
   826 
   827 /* Import a yum filelist as a razor package set. */
   828 
   829 enum {
   830 	YUM_STATE_BEGIN,
   831 	YUM_STATE_PACKAGE_NAME
   832 };
   833 
   834 struct yum_context {
   835 	struct import_context ctx;
   836 	struct import_property_context *current_property_context;
   837 	char *name;
   838 	int state;
   839 };
   840 
   841 static void
   842 yum_start_element(void *data, const char *name, const char **atts)
   843 {
   844 	struct yum_context *ctx = data;
   845 	const char *n, *version;
   846 	int i;
   847 
   848 	if (strcmp(name, "name") == 0) {
   849 		ctx->state = YUM_STATE_PACKAGE_NAME;
   850 	} else if (strcmp(name, "version") == 0) {
   851 		version = NULL;
   852 		for (i = 0; atts[i]; i += 2) {
   853 			if (strcmp(atts[i], "ver") == 0)
   854 				version = atts[i + 1];
   855 		}
   856 		import_context_add_package(&ctx->ctx, ctx->name, version);
   857 	} else if (strcmp(name, "rpm:requires") == 0) {
   858 		ctx->current_property_context = &ctx->ctx.requires;
   859 	} else if (strcmp(name, "rpm:provides") == 0) {
   860 		ctx->current_property_context = &ctx->ctx.provides;
   861 	} else if (strcmp(name, "rpm:entry") == 0 &&
   862 		   ctx->current_property_context != NULL) {
   863 		n = NULL;
   864 		version = NULL;
   865 		for (i = 0; atts[i]; i += 2) {
   866 			if (strcmp(atts[i], "name") == 0)
   867 				n = atts[i + 1];
   868 			else if (strcmp(atts[i], "ver") == 0)
   869 				version = atts[i + 1];
   870 		}
   871 
   872 		if (n == NULL) {
   873 			fprintf(stderr, "invalid rpm:entry, "
   874 				"missing name or version attributes\n");
   875 			return;
   876 		}
   877 
   878 		import_context_add_property(&ctx->ctx,
   879 					    ctx->current_property_context,
   880 					    n, version);
   881 	}
   882 }
   883 
   884 static void
   885 yum_end_element (void *data, const char *name)
   886 {
   887 	struct yum_context *ctx = data;
   888 
   889 	if (strcmp(name, "package") == 0) {
   890 		free(ctx->name);
   891 		import_context_finish_package(&ctx->ctx);
   892 	} else if (strcmp(name, "name") == 0) {
   893 		ctx->state = 0;
   894 	} else if (strcmp(name, "rpm:requires") == 0) {
   895 		ctx->current_property_context = NULL;
   896 	} else if (strcmp(name, "rpm:provides") == 0) {
   897 		ctx->current_property_context = NULL;
   898 	}
   899 }
   900 
   901 static void
   902 yum_character_data (void *data, const XML_Char *s, int len)
   903 {
   904 	struct yum_context *ctx = data;
   905 
   906 	if (ctx->state == YUM_STATE_PACKAGE_NAME)
   907 		ctx->name = strndup(s, len);
   908 }
   909 
   910 static struct razor_set *
   911 razor_set_create_from_yum_filelist(int fd)
   912 {
   913 	struct yum_context ctx;
   914 	XML_Parser parser;
   915 	char buf[4096];
   916 	int len;
   917 
   918 	razor_prepare_import(&ctx.ctx);
   919 
   920 	parser = XML_ParserCreate(NULL);
   921 	XML_SetUserData(parser, &ctx);
   922 	XML_SetElementHandler(parser, yum_start_element, yum_end_element);
   923 	XML_SetCharacterDataHandler(parser, yum_character_data);
   924 
   925 	while (1) {
   926 		len = read(fd, buf, sizeof buf);
   927 		if (len < 0) {
   928 			fprintf(stderr,
   929 				"couldn't read input: %s\n", strerror(errno));
   930 			return NULL;
   931 		} else if (len == 0)
   932 			break;
   933 
   934 		if (XML_Parse(parser, buf, len, 0) == XML_STATUS_ERROR) {
   935 			fprintf(stderr,
   936 				"%s at line %d\n",
   937 				XML_ErrorString(XML_GetErrorCode(parser)),
   938 				XML_GetCurrentLineNumber(parser));
   939 			return NULL;
   940 		}
   941 	}
   942 
   943 	XML_ParserFree(parser);
   944 
   945 	return razor_finish_import(&ctx.ctx);
   946 }
   947 
   948 void
   949 razor_set_list(struct razor_set *set)
   950 {
   951 	struct razor_package *p, *end;
   952 	char *pool;
   953 
   954 	pool = set->string_pool.data;
   955 	end = set->packages.data + set->packages.size;
   956 	for (p = set->packages.data; p < end; p++)
   957 		printf("%s %s\n", &pool[p->name], &pool[p->version]);
   958 }
   959 
   960 struct razor_set *bsearch_set;
   961 
   962 static int
   963 compare_package_name(const void *key, const void *data)
   964 {
   965 	const struct razor_package *p = data;
   966 	char *pool;
   967 
   968 	pool = bsearch_set->string_pool.data;
   969 
   970 	return strcmp(key, &pool[p->name]);
   971 }
   972 
   973 struct razor_package *
   974 razor_set_get_package(struct razor_set *set, const char *package)
   975 {
   976 	bsearch_set = set;
   977 	return bsearch(package, set->packages.data,
   978 		       set->packages.size / sizeof(struct razor_package),
   979 		       sizeof(struct razor_package), compare_package_name);
   980 }
   981 
   982 static int
   983 compare_property_name(const void *key, const void *data)
   984 {
   985 	const struct razor_property *p = data;
   986 	char *pool;
   987 
   988 	pool = bsearch_set->string_pool.data;
   989 
   990 	return strcmp(key, &pool[p->name]);
   991 }
   992 
   993 struct razor_property *
   994 razor_set_get_property(struct razor_set *set,
   995 		       struct array *properties,
   996 		       const char *property)
   997 {
   998 	struct razor_property *p, *start;
   999 
  1000 	bsearch_set = set;
  1001 	p = bsearch(property, properties->data,
  1002 		    properties->size / sizeof(struct razor_property),
  1003 		    sizeof(struct razor_property), compare_property_name);
  1004 
  1005 	start = properties->data;
  1006 	while (p > start && (p - 1)->name == p->name)
  1007 		p--;
  1008 
  1009 	return p;
  1010 }
  1011 
  1012 static void
  1013 razor_set_list_all_properties(struct razor_set *set, struct array *properties)
  1014 {
  1015 	struct razor_property *p, *end;
  1016 	char *pool;
  1017 
  1018 	pool = set->string_pool.data;
  1019 	end = properties->data + properties->size;
  1020 	for (p = properties->data; p < end; p++)
  1021 		printf("%s %s\n", &pool[p->name], &pool[p->version]);
  1022 }
  1023 
  1024 void
  1025 razor_set_list_requires(struct razor_set *set, const char *name)
  1026 {
  1027 	struct razor_property *p, *requires;
  1028 	struct razor_package *package;
  1029 	unsigned long *r;
  1030 	char *pool;
  1031 
  1032 	if (name) {
  1033 		package = razor_set_get_package(set, name);
  1034 		r = (unsigned long *) set->property_pool.data +
  1035 			package->requires;
  1036 		requires = set->requires.data;
  1037 		pool = set->string_pool.data;
  1038 		while (~*r) {
  1039 			p = &requires[*r++];
  1040 			printf("%s %s\n", &pool[p->name], &pool[p->version]);
  1041 		}
  1042 	} else
  1043 		razor_set_list_all_properties(set, &set->requires);
  1044 }
  1045 
  1046 void
  1047 razor_set_list_provides(struct razor_set *set, const char *name)
  1048 {
  1049 	struct razor_property *p, *provides;
  1050 	struct razor_package *package;
  1051 	unsigned long *r;
  1052 	char *pool;
  1053 
  1054 	if (name) {
  1055 		package = razor_set_get_package(set, name);
  1056 		r = (unsigned long *) set->property_pool.data +
  1057 			package->provides;
  1058 		provides = set->provides.data;
  1059 		pool = set->string_pool.data;
  1060 		while (~*r) {
  1061 			p = &provides[*r++];
  1062 			printf("%s %s\n", &pool[p->name], &pool[p->version]);
  1063 		}
  1064 	} else 
  1065 		razor_set_list_all_properties(set, &set->provides);
  1066 }
  1067 
  1068 void
  1069 razor_set_list_property_packages(struct razor_set *set,
  1070 				 struct array *properties,
  1071 				 const char *name,
  1072 				 const char *version)
  1073 {
  1074 	struct razor_property *property, *end;
  1075 	struct razor_package *p, *packages;
  1076 	unsigned long *r;
  1077 	char *pool;
  1078 
  1079 	if (name == NULL)
  1080 		return;
  1081 
  1082 	property = razor_set_get_property(set, properties, name);
  1083 	packages = set->packages.data;
  1084 	pool = set->string_pool.data;
  1085 	end = properties->data + properties->size;
  1086 	while (property < end && strcmp(name, &pool[property->name]) == 0) {
  1087 		if (version && strcmp(version, &pool[property->version]) != 0)
  1088 			goto next;
  1089 		r = (unsigned long *)
  1090 			set->property_pool.data + property->packages;
  1091 		while (~*r) {
  1092 			p = &packages[*r++];
  1093 			printf("%s %s\n",
  1094 			       &pool[p->name], &pool[p->version]);
  1095 		}
  1096 	next:
  1097 		property++;
  1098 	}
  1099 }
  1100 
  1101 void
  1102 razor_set_validate(struct razor_set *set, struct array *unsatisfied)
  1103 {
  1104 	struct razor_property *r, *p, *rend, *pend;
  1105 	unsigned long *u;
  1106 	char *pool;
  1107 
  1108 	r = set->requires.data;
  1109 	p = set->provides.data;
  1110 	rend = set->requires.data + set->requires.size;
  1111 	pend = set->provides.data + set->provides.size;
  1112 	pool = set->string_pool.data;
  1113 	
  1114 	while (r < rend) {
  1115 		while (p < pend && strcmp(&pool[r->name], &pool[p->name]) > 0)
  1116 			p++;
  1117 		if (p == pend || strcmp(&pool[r->name], &pool[p->name]) != 0) {
  1118 			u = array_add(unsatisfied, sizeof *u);
  1119 			*u = r - (struct razor_property *) set->requires.data;
  1120 		}
  1121 		r++;
  1122 	}
  1123 }
  1124 
  1125 void
  1126 razor_set_list_unsatisfied(struct razor_set *set)
  1127 {
  1128 	struct array unsatisfied;
  1129 	struct razor_property *requires, *r;
  1130 	unsigned long *u, *end;
  1131 	char *pool;
  1132 
  1133 	array_init(&unsatisfied);
  1134 	razor_set_validate(set, &unsatisfied);
  1135 
  1136 	end = unsatisfied.data + unsatisfied.size;
  1137 	requires = set->requires.data;
  1138 	pool = set->string_pool.data;
  1139 
  1140 	for (u = unsatisfied.data; u < end; u++) {
  1141 		r = requires + *u;
  1142 		printf("%s %s not satisfied\n",
  1143 		       &pool[r->name], &pool[r->version]);
  1144 	}
  1145 
  1146 	array_release(&unsatisfied);
  1147 }
  1148 
  1149 void
  1150 razor_set_info(struct razor_set *set)
  1151 {
  1152 	unsigned int offset, size;
  1153 	int i;
  1154 
  1155 	for (i = 0; i < set->header->sections[i].type; i++) {
  1156 		offset = set->header->sections[i].offset;
  1157 		size = set->header->sections[i].size;
  1158 
  1159 		switch (set->header->sections[i].type) {
  1160 		case RAZOR_PACKAGES:
  1161 			printf("package section:\t%dkb\n", size / 1024);
  1162 			break;
  1163 		case RAZOR_REQUIRES:
  1164 			printf("requires section:\t%dkb\n", size / 1024);
  1165 			break;
  1166 		case RAZOR_PROVIDES:
  1167 			printf("provides section:\t%dkb\n", size / 1024);
  1168 			break;
  1169 		case RAZOR_STRING_POOL:
  1170 			printf("string pool:\t\t%dkb\n", size / 1024);
  1171 			break;
  1172 		case RAZOR_PROPERTY_POOL:
  1173 			printf("properties section:\t%dkb\n", size / 1024);
  1174 			break;
  1175 		}
  1176 	}
  1177 }
  1178 
  1179 static int
  1180 usage(void)
  1181 {
  1182 	printf("usage: razor [ import FILES | lookup <key> | "
  1183 	       "list | list-requires | list-provides | eat-yum | info ]\n");
  1184 	exit(1);
  1185 }
  1186 
  1187 static const char *repo_filename = "system.repo";
  1188 static const char rawhide_repo_filename[] = "rawhide.repo";
  1189 
  1190 int
  1191 main(int argc, char *argv[])
  1192 {
  1193 	int i;
  1194 	struct razor_set *set;
  1195 	struct stat statbuf;
  1196 	struct import_context ctx;
  1197 	char *repo;
  1198 
  1199 	repo = getenv("RAZOR_REPO");
  1200 	if (repo != NULL)
  1201 		repo_filename = repo;
  1202 
  1203 	if (argc < 2) {
  1204 		usage();
  1205 	} else if (strcmp(argv[1], "import") == 0) {
  1206 		if (stat("set", &statbuf) && mkdir("set", 0777)) {
  1207 			fprintf(stderr, "could not create directory 'set'\n");
  1208 			exit(-1);
  1209 		}
  1210 			
  1211 		razor_prepare_import(&ctx);
  1212 
  1213 		for (i = 2; i < argc; i++) {
  1214 			if (razor_import(&ctx, argv[i]) < 0) {
  1215 				fprintf(stderr, "failed to import %s\n",
  1216 					argv[i]);
  1217 				exit(-1);
  1218 			}
  1219 		}
  1220 
  1221 		set = razor_finish_import(&ctx);
  1222 
  1223 		printf("bucket allocation: %d\n", set->buckets.alloc);
  1224 		printf("pool size: %d\n", set->string_pool.size);
  1225 		printf("pool allocation: %d\n", set->string_pool.alloc);
  1226 		printf("packages: %d\n",
  1227 		       set->packages.size / sizeof(struct razor_package));
  1228 		printf("requires: %d\n",
  1229 		       set->requires.size / sizeof(struct razor_property));
  1230 		printf("provides: %d\n",
  1231 		       set->provides.size / sizeof(struct razor_property));
  1232 
  1233 		razor_set_write(set, repo_filename);
  1234 
  1235 		razor_set_destroy(set);
  1236 	} else if (strcmp(argv[1], "lookup") == 0) {
  1237 		set = razor_set_open(repo_filename);
  1238 		printf("%s is %lu\n", argv[2],
  1239 		       razor_set_lookup(set, argv[2]));
  1240 		razor_set_destroy(set);
  1241 	} else if (strcmp(argv[1], "list") == 0) {
  1242 		set = razor_set_open(repo_filename);
  1243 		razor_set_list(set);
  1244 		razor_set_destroy(set);
  1245 	} else if (strcmp(argv[1], "list-requires") == 0) {
  1246 		set = razor_set_open(repo_filename);
  1247 		razor_set_list_requires(set, argv[2]);
  1248 		razor_set_destroy(set);
  1249 	} else if (strcmp(argv[1], "list-provides") == 0) {
  1250 		set = razor_set_open(repo_filename);
  1251 		razor_set_list_provides(set, argv[2]);
  1252 		razor_set_destroy(set);
  1253 	} else if (strcmp(argv[1], "what-requires") == 0) {
  1254 		set = razor_set_open(repo_filename);
  1255 		razor_set_list_property_packages(set, &set->requires,
  1256 						 argv[2], argv[3]);
  1257 		razor_set_destroy(set);
  1258 	} else if (strcmp(argv[1], "what-provides") == 0) {
  1259 		set = razor_set_open(repo_filename);
  1260 		razor_set_list_property_packages(set, &set->provides,
  1261 						 argv[2], argv[3]);
  1262 		razor_set_destroy(set);
  1263 	} else if (strcmp(argv[1], "info") == 0) {
  1264 		set = razor_set_open(repo_filename);
  1265 		razor_set_info(set);
  1266 		razor_set_destroy(set);
  1267 	} else if (strcmp(argv[1], "eat-yum") == 0) {
  1268 		set = razor_set_create_from_yum_filelist(STDIN_FILENO);
  1269 		if (set == NULL)
  1270 			return 1;
  1271 		razor_set_write(set, rawhide_repo_filename);
  1272 		razor_set_destroy(set);
  1273 		printf("wrote %s\n", rawhide_repo_filename);
  1274 	} else if (strcmp(argv[1], "validate") == 0) {
  1275 		set = razor_set_open(repo_filename);
  1276 		if (set == NULL)
  1277 			return 1;
  1278 		razor_set_list_unsatisfied(set);
  1279 		razor_set_destroy(set);
  1280 	} else {
  1281 		usage();
  1282 	}
  1283 
  1284 	return 0;
  1285 }