razor.c
author Kristian H?gsberg <krh@redhat.com>
Thu Sep 13 10:44:03 2007 -0400 (2007-09-13)
changeset 22 083768007350
parent 21 cfbf73037a39
child 23 8ffc32c648e2
permissions -rw-r--r--
Add qsort_with_data that let's us pass user data to the compare callback.
     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 array packages;
   399 	struct import_package *package;
   400 	unsigned long *requires_map;
   401 	unsigned long *provides_map;
   402 };
   403 
   404 struct import_package {
   405 	unsigned long name;
   406 	unsigned long version;
   407 	unsigned long requires;
   408 	unsigned long provides;
   409 	unsigned long index;
   410 };
   411 
   412 struct import_property {
   413 	unsigned long name;
   414 	unsigned long version;
   415 	unsigned long package;
   416 	unsigned long index;
   417 	unsigned long unique_index;
   418 };
   419 
   420 static void
   421 import_context_add_package(struct import_context *ctx,
   422 			   const char *name, const char *version)
   423 {
   424 	struct import_package *p;
   425 
   426 	p = array_add(&ctx->packages, sizeof *p);
   427 	p->name = razor_set_tokenize(ctx->set, name);
   428 	p->version = razor_set_tokenize(ctx->set, version);
   429 	p->index = p - (struct import_package *) ctx->packages.data;
   430 
   431 	ctx->package = p;
   432 	array_init(&ctx->requires.package);
   433 	array_init(&ctx->provides.package);
   434 }
   435 
   436 void
   437 import_context_finish_package(struct import_context *ctx)
   438 {
   439 	struct import_package *p;
   440 
   441 	p = ctx->package;
   442 	p->requires = add_to_property_pool(ctx->set, &ctx->requires.package);
   443 	p->provides = add_to_property_pool(ctx->set, &ctx->provides.package);
   444 
   445 	array_release(&ctx->requires.package);
   446 	array_release(&ctx->provides.package);
   447 }
   448 
   449 static void
   450 import_context_add_property(struct import_context *ctx,
   451 			    struct import_property_context *pctx,
   452 			    const char *name, const char *version)
   453 {
   454 	struct import_property *p;
   455 	unsigned long *r;
   456 
   457 	p = array_add(&pctx->all, sizeof *p);
   458 	p->name = razor_set_tokenize(ctx->set, name);
   459 	p->version = razor_set_tokenize(ctx->set, version);
   460 	p->package = ctx->package->index;
   461 	p->index = p - (struct import_property *) pctx->all.data;
   462 
   463 	r = array_add(&pctx->package, sizeof *r);
   464 	*r = p->index;
   465 }
   466 
   467 static void
   468 parse_package(struct import_context *ctx, const char **atts, void *data)
   469 {
   470 	const char *name = NULL, *version = NULL;
   471 	int i;
   472 
   473 	for (i = 0; atts[i]; i += 2) {
   474 		if (strcmp(atts[i], "name") == 0)
   475 			name = atts[i + 1];
   476 		else if (strcmp(atts[i], "version") == 0)
   477 			version = atts[i + 1];
   478 	}
   479 
   480 	if (name == NULL || version == NULL) {
   481 		fprintf(stderr, "invalid package tag, "
   482 			"missing name or version attributes\n");
   483 		return;
   484 	}
   485 
   486 	import_context_add_package(ctx, name, version);
   487 }
   488 
   489 static void
   490 parse_property(struct import_context *ctx, const char **atts, void *data)
   491 {
   492 	const char *name = NULL, *version = NULL;
   493 	int i;
   494 
   495 	for (i = 0; atts[i]; i += 2) {
   496 		if (strcmp(atts[i], "name") == 0)
   497 			name = atts[i + 1];
   498 		if (strcmp(atts[i], "version") == 0)
   499 			version = atts[i + 1];
   500 	}
   501 	
   502 	if (name == NULL) {
   503 		fprintf(stderr, "invalid tag, missing name attribute\n");
   504 		return;
   505 	}
   506 
   507 	import_context_add_property(ctx, data, name, version);
   508 }
   509 
   510 static void
   511 start_element(void *data, const char *name, const char **atts)
   512 {
   513 	struct import_context *ctx = data;
   514 
   515 	if (strcmp(name, "package") == 0)
   516 		parse_package(ctx, atts, NULL);
   517 	else if (strcmp(name, "requires") == 0)
   518 		parse_property(ctx, atts, &ctx->requires);
   519 	else if (strcmp(name, "provides") == 0)
   520 		parse_property(ctx, atts, &ctx->provides);
   521 }
   522 
   523 static void
   524 end_element (void *data, const char *name)
   525 {
   526 	struct import_context *ctx = data;
   527 
   528 	if (strcmp(name, "package") == 0)
   529 		import_context_finish_package(ctx);
   530 }
   531 
   532 static char *
   533 sha1_to_hex(const unsigned char *sha1)
   534 {
   535 	static int bufno;
   536 	static char hexbuffer[4][50];
   537 	static const char hex[] = "0123456789abcdef";
   538 	char *buffer = hexbuffer[3 & ++bufno], *buf = buffer;
   539 	int i;
   540 
   541 	for (i = 0; i < 20; i++) {
   542 		unsigned int val = *sha1++;
   543 		*buf++ = hex[val >> 4];
   544 		*buf++ = hex[val & 0xf];
   545 	}
   546 	*buf = '\0';
   547 
   548 	return buffer;
   549 }
   550 
   551 static void
   552 razor_prepare_import(struct import_context *ctx)
   553 {
   554 	memset(ctx, 0, sizeof *ctx);
   555 	ctx->set = razor_set_create();
   556 }
   557 
   558 static int
   559 razor_import(struct import_context *ctx, const char *filename)
   560 {
   561 	SHA_CTX sha1;
   562 	XML_Parser parser;
   563 	int fd;
   564 	void *p;
   565 	struct stat stat;
   566 	char buf[128];
   567 	unsigned char hash[20];
   568 
   569 	fd = open(filename, O_RDONLY);
   570 	if (fstat(fd, &stat) < 0)
   571 		return -1;
   572 	p = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
   573 	if (p == MAP_FAILED)
   574 		return -1;
   575 
   576 	parser = XML_ParserCreate(NULL);
   577 	XML_SetUserData(parser, ctx);
   578 	XML_SetElementHandler(parser, start_element, end_element);
   579 	if (XML_Parse(parser, p, stat.st_size, 1) == XML_STATUS_ERROR) {
   580 		fprintf(stderr,
   581 			"%s at line %d, %s\n",
   582 			XML_ErrorString(XML_GetErrorCode(parser)),
   583 			XML_GetCurrentLineNumber(parser),
   584 			filename);
   585 		return 1;
   586 	}
   587 
   588 	XML_ParserFree(parser);
   589 
   590 	SHA1_Init(&sha1);
   591 	SHA1_Update(&sha1, p, stat.st_size);
   592 	SHA1_Final(hash, &sha1);
   593 
   594 	close(fd);
   595 
   596 	snprintf(buf, sizeof buf, "set/%s", sha1_to_hex(hash));
   597 	if (write_to_file(buf, p, stat.st_size) < 0)
   598 		return -1;
   599 	munmap(p, stat.st_size);
   600 
   601 	return 0;
   602 }
   603 
   604 typedef int (*compare_with_data_func_t)(const void *p1,
   605 					const void *p,
   606 					void *data);
   607 
   608 static void
   609 qsort_swap(void *p1, void *p2, size_t size)
   610 {
   611 	char buffer[size];
   612 
   613 	memcpy(buffer, p1, size);
   614 	memcpy(p1, p2, size);
   615 	memcpy(p2, buffer, size);
   616 }
   617 
   618 void
   619 qsort_with_data(void *base, size_t nelem, size_t size,
   620 		compare_with_data_func_t compare, void *data)
   621 {
   622 	void *p, *start, *end, *pivot;
   623 	int left, right, result;
   624 
   625 	p = base;
   626 	start = base;
   627 	end = base + nelem * size;
   628 	pivot = base + (random() % nelem) * size;
   629 	
   630 	while (p < end) {
   631 		result = compare(p, pivot, data);
   632 		if (result < 0) {
   633 			qsort_swap(p, start, size);
   634 			if (start == pivot)
   635 				pivot = p;
   636 			start += size;
   637 			p += size;
   638 		} else if (result == 0) {
   639 			p += size;
   640 		} else {
   641  			end -= size;
   642 			qsort_swap(p, end, size);
   643 			if (end == pivot)
   644 				pivot = p;
   645 		}
   646 	}
   647 
   648 	left = (start - base) / size;
   649 	right = (base + nelem * size - end) / size;
   650 	if (left > 1)
   651 		qsort_with_data(base, left, size, compare, data);
   652 	if (right > 1)
   653 		qsort_with_data(end, right, size, compare, data);
   654 }
   655 
   656 static int
   657 compare_packages(const void *p1, const void *p2, void *data)
   658 {
   659 	const struct import_package *pkg1 = p1, *pkg2 = p2;
   660 	struct razor_set *set = data;
   661 	char *pool = set->string_pool.data;
   662 
   663 	return strcmp(&pool[pkg1->name], &pool[pkg2->name]);
   664 }
   665 
   666 static int
   667 compare_properties(const void *p1, const void *p2, void *data)
   668 {
   669 	const struct import_property *prop1 = p1, *prop2 = p2;
   670 	struct razor_set *set = data;
   671 	char *pool = set->string_pool.data;
   672 	int result;
   673 
   674 	result = strcmp(&pool[prop1->name], &pool[prop2->name]);
   675 	if (result == 0)
   676 		return strcmp(&pool[prop1->version], &pool[prop2->version]);
   677 	else
   678 		return result;
   679 }
   680 
   681 static unsigned long *
   682 uniqueify_properties(struct razor_set *set,
   683 		     struct array *in, struct array *out)
   684 {
   685 	struct import_property *ip, *end;
   686 	struct razor_property *rp, *rp_end;
   687 	struct array *pkgs, *p;
   688 	unsigned long *map, *r;
   689 	int i, count, unique;
   690 
   691 	count = in->size / sizeof(struct import_property);
   692 	qsort_with_data(in->data,
   693 			count,
   694 			sizeof(struct import_property),
   695 			compare_properties,
   696 			set);
   697 
   698 	rp = NULL;
   699 	end = in->data + in->size;
   700 	for (ip = in->data; ip < end; ip++) {
   701 		if (rp == NULL ||
   702 		    ip->name != rp->name || ip->version != rp->version) {
   703 			rp = array_add(out, sizeof *rp);
   704 			rp->name = ip->name;
   705 			rp->version = ip->version;
   706 		}
   707 		ip->unique_index = rp - (struct razor_property *) out->data;
   708 	}
   709 
   710 	map = malloc(count * sizeof (unsigned long));
   711 	ip = in->data;
   712 	for (i = 0; i < count; i++)
   713 		map[ip[i].index] = ip[i].unique_index;
   714 
   715 	unique = out->size / sizeof(*rp);
   716 	pkgs = zalloc(unique * sizeof *pkgs);
   717 	for (ip = in->data; ip < end; ip++) {
   718 		r = array_add(&pkgs[ip->unique_index], sizeof *r);
   719 		*r = ip->package;
   720 	}
   721 
   722 	rp_end = out->data + out->size;
   723 	for (rp = out->data, p = pkgs; rp < rp_end; rp++, p++)
   724 		rp->packages = add_to_property_pool(set, p);
   725 
   726 	free(pkgs);
   727 
   728 	return map;
   729 }
   730 
   731 static void
   732 remap_package_links(struct import_context *ctx)
   733 {
   734 	struct import_package *p, *end;
   735 	unsigned long *pool, *r;
   736 
   737 	pool = ctx->set->property_pool.data;
   738 	end = ctx->packages.data + ctx->packages.size;
   739 	for (p = ctx->packages.data; p < end; p++) {
   740 		for (r = &pool[p->requires]; ~*r; r++)
   741 			*r = ctx->requires_map[*r];
   742 		for (r = &pool[p->provides]; ~*r; r++)
   743 			*r = ctx->provides_map[*r];
   744 	}
   745 }
   746 
   747 static void
   748 remap_property_links(struct import_context *ctx)
   749 {
   750 	struct razor_property *p, *end;
   751 	struct import_package *ip;
   752 	unsigned long *map, *pool, *r;
   753 	int i, count;
   754 
   755 	pool = ctx->set->property_pool.data;
   756 	count = ctx->packages.size / sizeof(struct import_package);
   757 	map = malloc(count * sizeof *map);
   758 	ip = ctx->packages.data;
   759 	for (i = 0; i < count; i++)
   760 		map[ip[i].index] = i;
   761 
   762 	/* FIXME: This will break if we implement package list sharing
   763 	 * for all properties, since we'll remap those lists more than
   764 	 * once. We should just have a separate pool for property
   765 	 * lists and a separate pool for package lists and remap it as
   766 	 * a flat pool.  Right now, as property lists and package
   767 	 * lists are mixed, we can't do that. */
   768 
   769 	end = ctx->set->requires.data + ctx->set->requires.size;
   770 	for (p = ctx->set->requires.data; p < end; p++)
   771 		for (r = &pool[p->packages]; ~*r; r++)
   772 			*r = map[*r];
   773 
   774 	end = ctx->set->provides.data + ctx->set->provides.size;
   775 	for (p = ctx->set->provides.data; p < end; p++)
   776 		for (r = &pool[p->packages]; ~*r; r++)
   777 			*r = map[*r];
   778 
   779 	free(map);
   780 }
   781 
   782 static struct razor_set *
   783 razor_finish_import(struct import_context *ctx)
   784 {
   785 	struct import_package *ip;
   786 	struct razor_package *rp;
   787 	int i, count;
   788 
   789 	ctx->requires_map =
   790 		uniqueify_properties(ctx->set, 
   791 				     &ctx->requires.all,
   792 				     &ctx->set->requires);
   793 	ctx->provides_map =
   794 		uniqueify_properties(ctx->set,
   795 				     &ctx->provides.all,
   796 				     &ctx->set->provides);
   797 
   798 	remap_package_links(ctx);
   799 
   800 	count = ctx->packages.size / sizeof(struct import_package);
   801 	qsort_with_data(ctx->packages.data,
   802 			count,
   803 			sizeof(struct import_package),
   804 			compare_packages,
   805 			ctx->set);
   806 
   807 	ip = ctx->packages.data;
   808 	for (i = 0; i < count; i++, ip++, rp++) {
   809 		rp = array_add(&ctx->set->packages, sizeof *rp);
   810 		rp->name = ip->name;
   811 		rp->version = ip->version;
   812 		rp->requires = ip->requires;
   813 		rp->provides = ip->provides;
   814 	}
   815 
   816 	remap_property_links(ctx);
   817 
   818 	free(ctx->requires.all.data);
   819 	free(ctx->provides.all.data);
   820 	free(ctx->requires_map);
   821 	free(ctx->provides_map);
   822 		
   823 	fprintf(stderr, "parsed %d requires, %d unique\n",
   824 		ctx->requires.all.size / sizeof(struct import_property),
   825 		ctx->set->requires.size / sizeof(struct razor_property));
   826 	fprintf(stderr, "parsed %d provides, %d unique\n",
   827 		ctx->provides.all.size / sizeof(struct import_property),
   828 		ctx->set->provides.size / sizeof(struct razor_property));
   829 
   830 	return ctx->set; 
   831 }
   832 
   833 /* Import a yum filelist as a razor package set. */
   834 
   835 enum {
   836 	YUM_STATE_BEGIN,
   837 	YUM_STATE_PACKAGE_NAME
   838 };
   839 
   840 struct yum_context {
   841 	struct import_context ctx;
   842 	struct import_property_context *current_property_context;
   843 	char *name;
   844 	int state;
   845 };
   846 
   847 static void
   848 yum_start_element(void *data, const char *name, const char **atts)
   849 {
   850 	struct yum_context *ctx = data;
   851 	const char *n, *version;
   852 	int i;
   853 
   854 	if (strcmp(name, "name") == 0) {
   855 		ctx->state = YUM_STATE_PACKAGE_NAME;
   856 	} else if (strcmp(name, "version") == 0) {
   857 		for (i = 0; atts[i]; i += 2) {
   858 			if (strcmp(atts[i], "ver") == 0)
   859 				version = atts[i + 1];
   860 		}
   861 		import_context_add_package(&ctx->ctx, ctx->name, version);
   862 	} else if (strcmp(name, "rpm:requires") == 0) {
   863 		ctx->current_property_context = &ctx->ctx.requires;
   864 	} else if (strcmp(name, "rpm:provides") == 0) {
   865 		ctx->current_property_context = &ctx->ctx.provides;
   866 	} else if (strcmp(name, "rpm:entry") == 0 &&
   867 		   ctx->current_property_context != NULL) {
   868 		n = NULL;
   869 		version = NULL;
   870 		for (i = 0; atts[i]; i += 2) {
   871 			if (strcmp(atts[i], "name") == 0)
   872 				n = atts[i + 1];
   873 			else if (strcmp(atts[i], "ver") == 0)
   874 				version = atts[i + 1];
   875 		}
   876 
   877 		if (n == NULL) {
   878 			fprintf(stderr, "invalid rpm:entry, "
   879 				"missing name or version attributes\n");
   880 			return;
   881 		}
   882 
   883 		import_context_add_property(&ctx->ctx,
   884 					    ctx->current_property_context,
   885 					    n, version);
   886 	}
   887 }
   888 
   889 static void
   890 yum_end_element (void *data, const char *name)
   891 {
   892 	struct yum_context *ctx = data;
   893 
   894 	if (strcmp(name, "package") == 0) {
   895 		free(ctx->name);
   896 		import_context_finish_package(&ctx->ctx);
   897 	} else if (strcmp(name, "name") == 0) {
   898 		ctx->state = 0;
   899 	} else if (strcmp(name, "rpm:requires") == 0) {
   900 		ctx->current_property_context = NULL;
   901 	} else if (strcmp(name, "rpm:provides") == 0) {
   902 		ctx->current_property_context = NULL;
   903 	}
   904 }
   905 
   906 static void
   907 yum_character_data (void *data, const XML_Char *s, int len)
   908 {
   909 	struct yum_context *ctx = data;
   910 
   911 	if (ctx->state == YUM_STATE_PACKAGE_NAME)
   912 		ctx->name = strndup(s, len);
   913 }
   914 
   915 static struct razor_set *
   916 razor_set_create_from_yum_filelist(int fd)
   917 {
   918 	struct yum_context ctx;
   919 	XML_Parser parser;
   920 	char buf[4096];
   921 	int len;
   922 
   923 	razor_prepare_import(&ctx.ctx);
   924 
   925 	parser = XML_ParserCreate(NULL);
   926 	XML_SetUserData(parser, &ctx);
   927 	XML_SetElementHandler(parser, yum_start_element, yum_end_element);
   928 	XML_SetCharacterDataHandler(parser, yum_character_data);
   929 
   930 	while (1) {
   931 		len = read(fd, buf, sizeof buf);
   932 		if (len < 0) {
   933 			fprintf(stderr,
   934 				"couldn't read input: %s\n", strerror(errno));
   935 			return NULL;
   936 		} else if (len == 0)
   937 			break;
   938 
   939 		if (XML_Parse(parser, buf, len, 0) == XML_STATUS_ERROR) {
   940 			fprintf(stderr,
   941 				"%s at line %d\n",
   942 				XML_ErrorString(XML_GetErrorCode(parser)),
   943 				XML_GetCurrentLineNumber(parser));
   944 			return NULL;
   945 		}
   946 	}
   947 
   948 	XML_ParserFree(parser);
   949 
   950 	return razor_finish_import(&ctx.ctx);
   951 }
   952 
   953 void
   954 razor_set_list(struct razor_set *set)
   955 {
   956 	struct razor_package *p, *end;
   957 	char *pool;
   958 
   959 	pool = set->string_pool.data;
   960 	end = set->packages.data + set->packages.size;
   961 	for (p = set->packages.data; p < end; p++)
   962 		printf("%s %s\n", &pool[p->name], &pool[p->version]);
   963 }
   964 
   965 struct razor_set *bsearch_set;
   966 
   967 static int
   968 compare_package_name(const void *key, const void *data)
   969 {
   970 	const struct razor_package *p = data;
   971 	char *pool;
   972 
   973 	pool = bsearch_set->string_pool.data;
   974 
   975 	return strcmp(key, &pool[p->name]);
   976 }
   977 
   978 struct razor_package *
   979 razor_set_get_package(struct razor_set *set, const char *package)
   980 {
   981 	bsearch_set = set;
   982 	return bsearch(package, set->packages.data,
   983 		       set->packages.size / sizeof(struct razor_package),
   984 		       sizeof(struct razor_package), compare_package_name);
   985 }
   986 
   987 static int
   988 compare_property_name(const void *key, const void *data)
   989 {
   990 	const struct razor_property *p = data;
   991 	char *pool;
   992 
   993 	pool = bsearch_set->string_pool.data;
   994 
   995 	return strcmp(key, &pool[p->name]);
   996 }
   997 
   998 struct razor_property *
   999 razor_set_get_property(struct razor_set *set,
  1000 		       struct array *properties,
  1001 		       const char *property)
  1002 {
  1003 	struct razor_property *p, *start;
  1004 
  1005 	bsearch_set = set;
  1006 	p = bsearch(property, properties->data,
  1007 		    properties->size / sizeof(struct razor_property),
  1008 		    sizeof(struct razor_property), compare_property_name);
  1009 
  1010 	start = properties->data;
  1011 	while (p > start && (p - 1)->name == p->name)
  1012 		p--;
  1013 
  1014 	return p;
  1015 }
  1016 
  1017 static void
  1018 razor_set_list_all_properties(struct razor_set *set, struct array *properties)
  1019 {
  1020 	struct razor_property *p, *end;
  1021 	char *pool;
  1022 
  1023 	pool = set->string_pool.data;
  1024 	end = properties->data + properties->size;
  1025 	for (p = properties->data; p < end; p++)
  1026 		printf("%s %s\n", &pool[p->name], &pool[p->version]);
  1027 }
  1028 
  1029 void
  1030 razor_set_list_requires(struct razor_set *set, const char *name)
  1031 {
  1032 	struct razor_property *p, *requires;
  1033 	struct razor_package *package;
  1034 	unsigned long *r;
  1035 	char *pool;
  1036 
  1037 	if (name) {
  1038 		package = razor_set_get_package(set, name);
  1039 		r = (unsigned long *) set->property_pool.data +
  1040 			package->requires;
  1041 		requires = set->requires.data;
  1042 		pool = set->string_pool.data;
  1043 		while (~*r) {
  1044 			p = &requires[*r++];
  1045 			printf("%s %s\n", &pool[p->name], &pool[p->version]);
  1046 		}
  1047 	} else
  1048 		razor_set_list_all_properties(set, &set->requires);
  1049 }
  1050 
  1051 void
  1052 razor_set_list_provides(struct razor_set *set, const char *name)
  1053 {
  1054 	struct razor_property *p, *provides;
  1055 	struct razor_package *package;
  1056 	unsigned long *r;
  1057 	char *pool;
  1058 
  1059 	if (name) {
  1060 		package = razor_set_get_package(set, name);
  1061 		r = (unsigned long *) set->property_pool.data +
  1062 			package->provides;
  1063 		provides = set->provides.data;
  1064 		pool = set->string_pool.data;
  1065 		while (~*r) {
  1066 			p = &provides[*r++];
  1067 			printf("%s %s\n", &pool[p->name], &pool[p->version]);
  1068 		}
  1069 	} else 
  1070 		razor_set_list_all_properties(set, &set->provides);
  1071 }
  1072 
  1073 void
  1074 razor_set_list_property_packages(struct razor_set *set,
  1075 				 struct array *properties,
  1076 				 const char *name,
  1077 				 const char *version)
  1078 {
  1079 	struct razor_property *property, *end;
  1080 	struct razor_package *p, *packages;
  1081 	unsigned long *r;
  1082 	char *pool;
  1083 
  1084 	if (name == NULL)
  1085 		return;
  1086 
  1087 	property = razor_set_get_property(set, properties, name);
  1088 	packages = set->packages.data;
  1089 	pool = set->string_pool.data;
  1090 	end = properties->data + properties->size;
  1091 	while (property < end && strcmp(name, &pool[property->name]) == 0) {
  1092 		if (version && strcmp(version, &pool[property->version]) != 0)
  1093 			goto next;
  1094 		r = (unsigned long *)
  1095 			set->property_pool.data + property->packages;
  1096 		while (~*r) {
  1097 			p = &packages[*r++];
  1098 			printf("%s %s\n",
  1099 			       &pool[p->name], &pool[p->version]);
  1100 		}
  1101 	next:
  1102 		property++;
  1103 	}
  1104 }
  1105 
  1106 void
  1107 razor_set_validate(struct razor_set *set, struct array *unsatisfied)
  1108 {
  1109 	struct razor_property *r, *p, *rend, *pend;
  1110 	unsigned long *u;
  1111 	char *pool;
  1112 
  1113 	r = set->requires.data;
  1114 	p = set->provides.data;
  1115 	rend = set->requires.data + set->requires.size;
  1116 	pend = set->provides.data + set->provides.size;
  1117 	pool = set->string_pool.data;
  1118 	
  1119 	while (r < rend) {
  1120 		while (p < pend && strcmp(&pool[r->name], &pool[p->name]) > 0)
  1121 			p++;
  1122 		if (p == pend || strcmp(&pool[r->name], &pool[p->name]) != 0) {
  1123 			u = array_add(unsatisfied, sizeof *u);
  1124 			*u = r - (struct razor_property *) set->requires.data;
  1125 		}
  1126 		r++;
  1127 	}
  1128 }
  1129 
  1130 void
  1131 razor_set_list_unsatisfied(struct razor_set *set)
  1132 {
  1133 	struct array unsatisfied;
  1134 	struct razor_property *requires, *r;
  1135 	unsigned long *u, *end;
  1136 	char *pool;
  1137 
  1138 	array_init(&unsatisfied);
  1139 	razor_set_validate(set, &unsatisfied);
  1140 
  1141 	end = unsatisfied.data + unsatisfied.size;
  1142 	requires = set->requires.data;
  1143 	pool = set->string_pool.data;
  1144 
  1145 	for (u = unsatisfied.data; u < end; u++) {
  1146 		r = requires + *u;
  1147 		printf("%s %s not satisfied\n",
  1148 		       &pool[r->name], &pool[r->version]);
  1149 	}
  1150 
  1151 	array_release(&unsatisfied);
  1152 }
  1153 
  1154 void
  1155 razor_set_info(struct razor_set *set)
  1156 {
  1157 	unsigned int offset, size;
  1158 	int i;
  1159 
  1160 	for (i = 0; i < set->header->sections[i].type; i++) {
  1161 		offset = set->header->sections[i].offset;
  1162 		size = set->header->sections[i].size;
  1163 
  1164 		switch (set->header->sections[i].type) {
  1165 		case RAZOR_PACKAGES:
  1166 			printf("package section:\t%dkb\n", size / 1024);
  1167 			break;
  1168 		case RAZOR_REQUIRES:
  1169 			printf("requires section:\t%dkb\n", size / 1024);
  1170 			break;
  1171 		case RAZOR_PROVIDES:
  1172 			printf("provides section:\t%dkb\n", size / 1024);
  1173 			break;
  1174 		case RAZOR_STRING_POOL:
  1175 			printf("string pool:\t\t%dkb\n", size / 1024);
  1176 			break;
  1177 		case RAZOR_PROPERTY_POOL:
  1178 			printf("properties section:\t%dkb\n", size / 1024);
  1179 			break;
  1180 		}
  1181 	}
  1182 }
  1183 
  1184 static int
  1185 usage(void)
  1186 {
  1187 	printf("usage: razor [ import FILES | lookup <key> | "
  1188 	       "list | list-requires | list-provides | eat-yum | info ]\n");
  1189 	exit(1);
  1190 }
  1191 
  1192 static const char *repo_filename = "system.repo";
  1193 static const char rawhide_repo_filename[] = "rawhide.repo";
  1194 
  1195 int
  1196 main(int argc, char *argv[])
  1197 {
  1198 	int i;
  1199 	struct razor_set *set;
  1200 	struct stat statbuf;
  1201 	struct import_context ctx;
  1202 	char *repo;
  1203 
  1204 	repo = getenv("RAZOR_REPO");
  1205 	if (repo != NULL)
  1206 		repo_filename = repo;
  1207 
  1208 	if (argc < 2) {
  1209 		usage();
  1210 	} else if (strcmp(argv[1], "import") == 0) {
  1211 		if (stat("set", &statbuf) && mkdir("set", 0777)) {
  1212 			fprintf(stderr, "could not create directory 'set'\n");
  1213 			exit(-1);
  1214 		}
  1215 			
  1216 		razor_prepare_import(&ctx);
  1217 
  1218 		for (i = 2; i < argc; i++) {
  1219 			if (razor_import(&ctx, argv[i]) < 0) {
  1220 				fprintf(stderr, "failed to import %s\n",
  1221 					argv[i]);
  1222 				exit(-1);
  1223 			}
  1224 		}
  1225 
  1226 		set = razor_finish_import(&ctx);
  1227 
  1228 		printf("bucket allocation: %d\n", set->buckets.alloc);
  1229 		printf("pool size: %d\n", set->string_pool.size);
  1230 		printf("pool allocation: %d\n", set->string_pool.alloc);
  1231 		printf("packages: %d\n",
  1232 		       set->packages.size / sizeof(struct razor_package));
  1233 		printf("requires: %d\n",
  1234 		       set->requires.size / sizeof(struct razor_property));
  1235 		printf("provides: %d\n",
  1236 		       set->provides.size / sizeof(struct razor_property));
  1237 
  1238 		razor_set_write(set, repo_filename);
  1239 
  1240 		razor_set_destroy(set);
  1241 	} else if (strcmp(argv[1], "lookup") == 0) {
  1242 		set = razor_set_open(repo_filename);
  1243 		printf("%s is %lu\n", argv[2],
  1244 		       razor_set_lookup(set, argv[2]));
  1245 		razor_set_destroy(set);
  1246 	} else if (strcmp(argv[1], "list") == 0) {
  1247 		set = razor_set_open(repo_filename);
  1248 		razor_set_list(set);
  1249 		razor_set_destroy(set);
  1250 	} else if (strcmp(argv[1], "list-requires") == 0) {
  1251 		set = razor_set_open(repo_filename);
  1252 		razor_set_list_requires(set, argv[2]);
  1253 		razor_set_destroy(set);
  1254 	} else if (strcmp(argv[1], "list-provides") == 0) {
  1255 		set = razor_set_open(repo_filename);
  1256 		razor_set_list_provides(set, argv[2]);
  1257 		razor_set_destroy(set);
  1258 	} else if (strcmp(argv[1], "what-requires") == 0) {
  1259 		set = razor_set_open(repo_filename);
  1260 		razor_set_list_property_packages(set, &set->requires,
  1261 						 argv[2], argv[3]);
  1262 		razor_set_destroy(set);
  1263 	} else if (strcmp(argv[1], "what-provides") == 0) {
  1264 		set = razor_set_open(repo_filename);
  1265 		razor_set_list_property_packages(set, &set->provides,
  1266 						 argv[2], argv[3]);
  1267 		razor_set_destroy(set);
  1268 	} else if (strcmp(argv[1], "info") == 0) {
  1269 		set = razor_set_open(repo_filename);
  1270 		razor_set_info(set);
  1271 		razor_set_destroy(set);
  1272 	} else if (strcmp(argv[1], "eat-yum") == 0) {
  1273 		set = razor_set_create_from_yum_filelist(STDIN_FILENO);
  1274 		if (set == NULL)
  1275 			return 1;
  1276 		razor_set_write(set, rawhide_repo_filename);
  1277 		razor_set_destroy(set);
  1278 		printf("wrote %s\n", rawhide_repo_filename);
  1279 	} else if (strcmp(argv[1], "validate") == 0) {
  1280 		set = razor_set_open(repo_filename);
  1281 		if (set == NULL)
  1282 			return 1;
  1283 		razor_set_list_unsatisfied(set);
  1284 		razor_set_destroy(set);
  1285 	} else {
  1286 		usage();
  1287 	}
  1288 
  1289 	return 0;
  1290 }