razor.c
author Kristian H?gsberg <krh@redhat.com>
Thu Sep 13 10:54:13 2007 -0400 (2007-09-13)
changeset 24 5963746558e7
parent 23 8ffc32c648e2
child 25 09278d69f386
permissions -rw-r--r--
Fix warning.
     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 	if (pkg1->name == pkg2->name)
   664 		return 0;
   665 	else
   666 		return strcmp(&pool[pkg1->name], &pool[pkg2->name]);
   667 }
   668 
   669 static int
   670 compare_properties(const void *p1, const void *p2, void *data)
   671 {
   672 	const struct import_property *prop1 = p1, *prop2 = p2;
   673 	struct razor_set *set = data;
   674 	char *pool = set->string_pool.data;
   675 
   676 	if (prop1->name == prop2->name)
   677 		return strcmp(&pool[prop1->version], &pool[prop2->version]);
   678 	else
   679 		return strcmp(&pool[prop1->name], &pool[prop2->name]);
   680 }
   681 
   682 static unsigned long *
   683 uniqueify_properties(struct razor_set *set,
   684 		     struct array *in, struct array *out)
   685 {
   686 	struct import_property *ip, *end;
   687 	struct razor_property *rp, *rp_end;
   688 	struct array *pkgs, *p;
   689 	unsigned long *map, *r;
   690 	int i, count, unique;
   691 
   692 	count = in->size / sizeof(struct import_property);
   693 	qsort_with_data(in->data,
   694 			count,
   695 			sizeof(struct import_property),
   696 			compare_properties,
   697 			set);
   698 
   699 	rp = NULL;
   700 	end = in->data + in->size;
   701 	for (ip = in->data; ip < end; ip++) {
   702 		if (rp == NULL ||
   703 		    ip->name != rp->name || ip->version != rp->version) {
   704 			rp = array_add(out, sizeof *rp);
   705 			rp->name = ip->name;
   706 			rp->version = ip->version;
   707 		}
   708 		ip->unique_index = rp - (struct razor_property *) out->data;
   709 	}
   710 
   711 	map = malloc(count * sizeof (unsigned long));
   712 	ip = in->data;
   713 	for (i = 0; i < count; i++)
   714 		map[ip[i].index] = ip[i].unique_index;
   715 
   716 	unique = out->size / sizeof(*rp);
   717 	pkgs = zalloc(unique * sizeof *pkgs);
   718 	for (ip = in->data; ip < end; ip++) {
   719 		r = array_add(&pkgs[ip->unique_index], sizeof *r);
   720 		*r = ip->package;
   721 	}
   722 
   723 	rp_end = out->data + out->size;
   724 	for (rp = out->data, p = pkgs; rp < rp_end; rp++, p++)
   725 		rp->packages = add_to_property_pool(set, p);
   726 
   727 	free(pkgs);
   728 
   729 	return map;
   730 }
   731 
   732 static void
   733 remap_package_links(struct import_context *ctx)
   734 {
   735 	struct import_package *p, *end;
   736 	unsigned long *pool, *r;
   737 
   738 	pool = ctx->set->property_pool.data;
   739 	end = ctx->packages.data + ctx->packages.size;
   740 	for (p = ctx->packages.data; p < end; p++) {
   741 		for (r = &pool[p->requires]; ~*r; r++)
   742 			*r = ctx->requires_map[*r];
   743 		for (r = &pool[p->provides]; ~*r; r++)
   744 			*r = ctx->provides_map[*r];
   745 	}
   746 }
   747 
   748 static void
   749 remap_property_links(struct import_context *ctx)
   750 {
   751 	struct razor_property *p, *end;
   752 	struct import_package *ip;
   753 	unsigned long *map, *pool, *r;
   754 	int i, count;
   755 
   756 	pool = ctx->set->property_pool.data;
   757 	count = ctx->packages.size / sizeof(struct import_package);
   758 	map = malloc(count * sizeof *map);
   759 	ip = ctx->packages.data;
   760 	for (i = 0; i < count; i++)
   761 		map[ip[i].index] = i;
   762 
   763 	/* FIXME: This will break if we implement package list sharing
   764 	 * for all properties, since we'll remap those lists more than
   765 	 * once. We should just have a separate pool for property
   766 	 * lists and a separate pool for package lists and remap it as
   767 	 * a flat pool.  Right now, as property lists and package
   768 	 * lists are mixed, we can't do that. */
   769 
   770 	end = ctx->set->requires.data + ctx->set->requires.size;
   771 	for (p = ctx->set->requires.data; p < end; p++)
   772 		for (r = &pool[p->packages]; ~*r; r++)
   773 			*r = map[*r];
   774 
   775 	end = ctx->set->provides.data + ctx->set->provides.size;
   776 	for (p = ctx->set->provides.data; p < end; p++)
   777 		for (r = &pool[p->packages]; ~*r; r++)
   778 			*r = map[*r];
   779 
   780 	free(map);
   781 }
   782 
   783 static struct razor_set *
   784 razor_finish_import(struct import_context *ctx)
   785 {
   786 	struct import_package *ip;
   787 	struct razor_package *rp;
   788 	int i, count;
   789 
   790 	ctx->requires_map =
   791 		uniqueify_properties(ctx->set, 
   792 				     &ctx->requires.all,
   793 				     &ctx->set->requires);
   794 	ctx->provides_map =
   795 		uniqueify_properties(ctx->set,
   796 				     &ctx->provides.all,
   797 				     &ctx->set->provides);
   798 
   799 	remap_package_links(ctx);
   800 
   801 	count = ctx->packages.size / sizeof(struct import_package);
   802 	qsort_with_data(ctx->packages.data,
   803 			count,
   804 			sizeof(struct import_package),
   805 			compare_packages,
   806 			ctx->set);
   807 
   808 	ip = ctx->packages.data;
   809 	for (i = 0; i < count; i++, ip++, rp++) {
   810 		rp = array_add(&ctx->set->packages, sizeof *rp);
   811 		rp->name = ip->name;
   812 		rp->version = ip->version;
   813 		rp->requires = ip->requires;
   814 		rp->provides = ip->provides;
   815 	}
   816 
   817 	remap_property_links(ctx);
   818 
   819 	free(ctx->requires.all.data);
   820 	free(ctx->provides.all.data);
   821 	free(ctx->requires_map);
   822 	free(ctx->provides_map);
   823 		
   824 	fprintf(stderr, "parsed %d requires, %d unique\n",
   825 		ctx->requires.all.size / sizeof(struct import_property),
   826 		ctx->set->requires.size / sizeof(struct razor_property));
   827 	fprintf(stderr, "parsed %d provides, %d unique\n",
   828 		ctx->provides.all.size / sizeof(struct import_property),
   829 		ctx->set->provides.size / sizeof(struct razor_property));
   830 
   831 	return ctx->set; 
   832 }
   833 
   834 /* Import a yum filelist as a razor package set. */
   835 
   836 enum {
   837 	YUM_STATE_BEGIN,
   838 	YUM_STATE_PACKAGE_NAME
   839 };
   840 
   841 struct yum_context {
   842 	struct import_context ctx;
   843 	struct import_property_context *current_property_context;
   844 	char *name;
   845 	int state;
   846 };
   847 
   848 static void
   849 yum_start_element(void *data, const char *name, const char **atts)
   850 {
   851 	struct yum_context *ctx = data;
   852 	const char *n, *version;
   853 	int i;
   854 
   855 	if (strcmp(name, "name") == 0) {
   856 		ctx->state = YUM_STATE_PACKAGE_NAME;
   857 	} else if (strcmp(name, "version") == 0) {
   858 		version = NULL;
   859 		for (i = 0; atts[i]; i += 2) {
   860 			if (strcmp(atts[i], "ver") == 0)
   861 				version = atts[i + 1];
   862 		}
   863 		import_context_add_package(&ctx->ctx, ctx->name, version);
   864 	} else if (strcmp(name, "rpm:requires") == 0) {
   865 		ctx->current_property_context = &ctx->ctx.requires;
   866 	} else if (strcmp(name, "rpm:provides") == 0) {
   867 		ctx->current_property_context = &ctx->ctx.provides;
   868 	} else if (strcmp(name, "rpm:entry") == 0 &&
   869 		   ctx->current_property_context != NULL) {
   870 		n = NULL;
   871 		version = NULL;
   872 		for (i = 0; atts[i]; i += 2) {
   873 			if (strcmp(atts[i], "name") == 0)
   874 				n = atts[i + 1];
   875 			else if (strcmp(atts[i], "ver") == 0)
   876 				version = atts[i + 1];
   877 		}
   878 
   879 		if (n == NULL) {
   880 			fprintf(stderr, "invalid rpm:entry, "
   881 				"missing name or version attributes\n");
   882 			return;
   883 		}
   884 
   885 		import_context_add_property(&ctx->ctx,
   886 					    ctx->current_property_context,
   887 					    n, version);
   888 	}
   889 }
   890 
   891 static void
   892 yum_end_element (void *data, const char *name)
   893 {
   894 	struct yum_context *ctx = data;
   895 
   896 	if (strcmp(name, "package") == 0) {
   897 		free(ctx->name);
   898 		import_context_finish_package(&ctx->ctx);
   899 	} else if (strcmp(name, "name") == 0) {
   900 		ctx->state = 0;
   901 	} else if (strcmp(name, "rpm:requires") == 0) {
   902 		ctx->current_property_context = NULL;
   903 	} else if (strcmp(name, "rpm:provides") == 0) {
   904 		ctx->current_property_context = NULL;
   905 	}
   906 }
   907 
   908 static void
   909 yum_character_data (void *data, const XML_Char *s, int len)
   910 {
   911 	struct yum_context *ctx = data;
   912 
   913 	if (ctx->state == YUM_STATE_PACKAGE_NAME)
   914 		ctx->name = strndup(s, len);
   915 }
   916 
   917 static struct razor_set *
   918 razor_set_create_from_yum_filelist(int fd)
   919 {
   920 	struct yum_context ctx;
   921 	XML_Parser parser;
   922 	char buf[4096];
   923 	int len;
   924 
   925 	razor_prepare_import(&ctx.ctx);
   926 
   927 	parser = XML_ParserCreate(NULL);
   928 	XML_SetUserData(parser, &ctx);
   929 	XML_SetElementHandler(parser, yum_start_element, yum_end_element);
   930 	XML_SetCharacterDataHandler(parser, yum_character_data);
   931 
   932 	while (1) {
   933 		len = read(fd, buf, sizeof buf);
   934 		if (len < 0) {
   935 			fprintf(stderr,
   936 				"couldn't read input: %s\n", strerror(errno));
   937 			return NULL;
   938 		} else if (len == 0)
   939 			break;
   940 
   941 		if (XML_Parse(parser, buf, len, 0) == XML_STATUS_ERROR) {
   942 			fprintf(stderr,
   943 				"%s at line %d\n",
   944 				XML_ErrorString(XML_GetErrorCode(parser)),
   945 				XML_GetCurrentLineNumber(parser));
   946 			return NULL;
   947 		}
   948 	}
   949 
   950 	XML_ParserFree(parser);
   951 
   952 	return razor_finish_import(&ctx.ctx);
   953 }
   954 
   955 void
   956 razor_set_list(struct razor_set *set)
   957 {
   958 	struct razor_package *p, *end;
   959 	char *pool;
   960 
   961 	pool = set->string_pool.data;
   962 	end = set->packages.data + set->packages.size;
   963 	for (p = set->packages.data; p < end; p++)
   964 		printf("%s %s\n", &pool[p->name], &pool[p->version]);
   965 }
   966 
   967 struct razor_set *bsearch_set;
   968 
   969 static int
   970 compare_package_name(const void *key, const void *data)
   971 {
   972 	const struct razor_package *p = data;
   973 	char *pool;
   974 
   975 	pool = bsearch_set->string_pool.data;
   976 
   977 	return strcmp(key, &pool[p->name]);
   978 }
   979 
   980 struct razor_package *
   981 razor_set_get_package(struct razor_set *set, const char *package)
   982 {
   983 	bsearch_set = set;
   984 	return bsearch(package, set->packages.data,
   985 		       set->packages.size / sizeof(struct razor_package),
   986 		       sizeof(struct razor_package), compare_package_name);
   987 }
   988 
   989 static int
   990 compare_property_name(const void *key, const void *data)
   991 {
   992 	const struct razor_property *p = data;
   993 	char *pool;
   994 
   995 	pool = bsearch_set->string_pool.data;
   996 
   997 	return strcmp(key, &pool[p->name]);
   998 }
   999 
  1000 struct razor_property *
  1001 razor_set_get_property(struct razor_set *set,
  1002 		       struct array *properties,
  1003 		       const char *property)
  1004 {
  1005 	struct razor_property *p, *start;
  1006 
  1007 	bsearch_set = set;
  1008 	p = bsearch(property, properties->data,
  1009 		    properties->size / sizeof(struct razor_property),
  1010 		    sizeof(struct razor_property), compare_property_name);
  1011 
  1012 	start = properties->data;
  1013 	while (p > start && (p - 1)->name == p->name)
  1014 		p--;
  1015 
  1016 	return p;
  1017 }
  1018 
  1019 static void
  1020 razor_set_list_all_properties(struct razor_set *set, struct array *properties)
  1021 {
  1022 	struct razor_property *p, *end;
  1023 	char *pool;
  1024 
  1025 	pool = set->string_pool.data;
  1026 	end = properties->data + properties->size;
  1027 	for (p = properties->data; p < end; p++)
  1028 		printf("%s %s\n", &pool[p->name], &pool[p->version]);
  1029 }
  1030 
  1031 void
  1032 razor_set_list_requires(struct razor_set *set, const char *name)
  1033 {
  1034 	struct razor_property *p, *requires;
  1035 	struct razor_package *package;
  1036 	unsigned long *r;
  1037 	char *pool;
  1038 
  1039 	if (name) {
  1040 		package = razor_set_get_package(set, name);
  1041 		r = (unsigned long *) set->property_pool.data +
  1042 			package->requires;
  1043 		requires = set->requires.data;
  1044 		pool = set->string_pool.data;
  1045 		while (~*r) {
  1046 			p = &requires[*r++];
  1047 			printf("%s %s\n", &pool[p->name], &pool[p->version]);
  1048 		}
  1049 	} else
  1050 		razor_set_list_all_properties(set, &set->requires);
  1051 }
  1052 
  1053 void
  1054 razor_set_list_provides(struct razor_set *set, const char *name)
  1055 {
  1056 	struct razor_property *p, *provides;
  1057 	struct razor_package *package;
  1058 	unsigned long *r;
  1059 	char *pool;
  1060 
  1061 	if (name) {
  1062 		package = razor_set_get_package(set, name);
  1063 		r = (unsigned long *) set->property_pool.data +
  1064 			package->provides;
  1065 		provides = set->provides.data;
  1066 		pool = set->string_pool.data;
  1067 		while (~*r) {
  1068 			p = &provides[*r++];
  1069 			printf("%s %s\n", &pool[p->name], &pool[p->version]);
  1070 		}
  1071 	} else 
  1072 		razor_set_list_all_properties(set, &set->provides);
  1073 }
  1074 
  1075 void
  1076 razor_set_list_property_packages(struct razor_set *set,
  1077 				 struct array *properties,
  1078 				 const char *name,
  1079 				 const char *version)
  1080 {
  1081 	struct razor_property *property, *end;
  1082 	struct razor_package *p, *packages;
  1083 	unsigned long *r;
  1084 	char *pool;
  1085 
  1086 	if (name == NULL)
  1087 		return;
  1088 
  1089 	property = razor_set_get_property(set, properties, name);
  1090 	packages = set->packages.data;
  1091 	pool = set->string_pool.data;
  1092 	end = properties->data + properties->size;
  1093 	while (property < end && strcmp(name, &pool[property->name]) == 0) {
  1094 		if (version && strcmp(version, &pool[property->version]) != 0)
  1095 			goto next;
  1096 		r = (unsigned long *)
  1097 			set->property_pool.data + property->packages;
  1098 		while (~*r) {
  1099 			p = &packages[*r++];
  1100 			printf("%s %s\n",
  1101 			       &pool[p->name], &pool[p->version]);
  1102 		}
  1103 	next:
  1104 		property++;
  1105 	}
  1106 }
  1107 
  1108 void
  1109 razor_set_validate(struct razor_set *set, struct array *unsatisfied)
  1110 {
  1111 	struct razor_property *r, *p, *rend, *pend;
  1112 	unsigned long *u;
  1113 	char *pool;
  1114 
  1115 	r = set->requires.data;
  1116 	p = set->provides.data;
  1117 	rend = set->requires.data + set->requires.size;
  1118 	pend = set->provides.data + set->provides.size;
  1119 	pool = set->string_pool.data;
  1120 	
  1121 	while (r < rend) {
  1122 		while (p < pend && strcmp(&pool[r->name], &pool[p->name]) > 0)
  1123 			p++;
  1124 		if (p == pend || strcmp(&pool[r->name], &pool[p->name]) != 0) {
  1125 			u = array_add(unsatisfied, sizeof *u);
  1126 			*u = r - (struct razor_property *) set->requires.data;
  1127 		}
  1128 		r++;
  1129 	}
  1130 }
  1131 
  1132 void
  1133 razor_set_list_unsatisfied(struct razor_set *set)
  1134 {
  1135 	struct array unsatisfied;
  1136 	struct razor_property *requires, *r;
  1137 	unsigned long *u, *end;
  1138 	char *pool;
  1139 
  1140 	array_init(&unsatisfied);
  1141 	razor_set_validate(set, &unsatisfied);
  1142 
  1143 	end = unsatisfied.data + unsatisfied.size;
  1144 	requires = set->requires.data;
  1145 	pool = set->string_pool.data;
  1146 
  1147 	for (u = unsatisfied.data; u < end; u++) {
  1148 		r = requires + *u;
  1149 		printf("%s %s not satisfied\n",
  1150 		       &pool[r->name], &pool[r->version]);
  1151 	}
  1152 
  1153 	array_release(&unsatisfied);
  1154 }
  1155 
  1156 void
  1157 razor_set_info(struct razor_set *set)
  1158 {
  1159 	unsigned int offset, size;
  1160 	int i;
  1161 
  1162 	for (i = 0; i < set->header->sections[i].type; i++) {
  1163 		offset = set->header->sections[i].offset;
  1164 		size = set->header->sections[i].size;
  1165 
  1166 		switch (set->header->sections[i].type) {
  1167 		case RAZOR_PACKAGES:
  1168 			printf("package section:\t%dkb\n", size / 1024);
  1169 			break;
  1170 		case RAZOR_REQUIRES:
  1171 			printf("requires section:\t%dkb\n", size / 1024);
  1172 			break;
  1173 		case RAZOR_PROVIDES:
  1174 			printf("provides section:\t%dkb\n", size / 1024);
  1175 			break;
  1176 		case RAZOR_STRING_POOL:
  1177 			printf("string pool:\t\t%dkb\n", size / 1024);
  1178 			break;
  1179 		case RAZOR_PROPERTY_POOL:
  1180 			printf("properties section:\t%dkb\n", size / 1024);
  1181 			break;
  1182 		}
  1183 	}
  1184 }
  1185 
  1186 static int
  1187 usage(void)
  1188 {
  1189 	printf("usage: razor [ import FILES | lookup <key> | "
  1190 	       "list | list-requires | list-provides | eat-yum | info ]\n");
  1191 	exit(1);
  1192 }
  1193 
  1194 static const char *repo_filename = "system.repo";
  1195 static const char rawhide_repo_filename[] = "rawhide.repo";
  1196 
  1197 int
  1198 main(int argc, char *argv[])
  1199 {
  1200 	int i;
  1201 	struct razor_set *set;
  1202 	struct stat statbuf;
  1203 	struct import_context ctx;
  1204 	char *repo;
  1205 
  1206 	repo = getenv("RAZOR_REPO");
  1207 	if (repo != NULL)
  1208 		repo_filename = repo;
  1209 
  1210 	if (argc < 2) {
  1211 		usage();
  1212 	} else if (strcmp(argv[1], "import") == 0) {
  1213 		if (stat("set", &statbuf) && mkdir("set", 0777)) {
  1214 			fprintf(stderr, "could not create directory 'set'\n");
  1215 			exit(-1);
  1216 		}
  1217 			
  1218 		razor_prepare_import(&ctx);
  1219 
  1220 		for (i = 2; i < argc; i++) {
  1221 			if (razor_import(&ctx, argv[i]) < 0) {
  1222 				fprintf(stderr, "failed to import %s\n",
  1223 					argv[i]);
  1224 				exit(-1);
  1225 			}
  1226 		}
  1227 
  1228 		set = razor_finish_import(&ctx);
  1229 
  1230 		printf("bucket allocation: %d\n", set->buckets.alloc);
  1231 		printf("pool size: %d\n", set->string_pool.size);
  1232 		printf("pool allocation: %d\n", set->string_pool.alloc);
  1233 		printf("packages: %d\n",
  1234 		       set->packages.size / sizeof(struct razor_package));
  1235 		printf("requires: %d\n",
  1236 		       set->requires.size / sizeof(struct razor_property));
  1237 		printf("provides: %d\n",
  1238 		       set->provides.size / sizeof(struct razor_property));
  1239 
  1240 		razor_set_write(set, repo_filename);
  1241 
  1242 		razor_set_destroy(set);
  1243 	} else if (strcmp(argv[1], "lookup") == 0) {
  1244 		set = razor_set_open(repo_filename);
  1245 		printf("%s is %lu\n", argv[2],
  1246 		       razor_set_lookup(set, argv[2]));
  1247 		razor_set_destroy(set);
  1248 	} else if (strcmp(argv[1], "list") == 0) {
  1249 		set = razor_set_open(repo_filename);
  1250 		razor_set_list(set);
  1251 		razor_set_destroy(set);
  1252 	} else if (strcmp(argv[1], "list-requires") == 0) {
  1253 		set = razor_set_open(repo_filename);
  1254 		razor_set_list_requires(set, argv[2]);
  1255 		razor_set_destroy(set);
  1256 	} else if (strcmp(argv[1], "list-provides") == 0) {
  1257 		set = razor_set_open(repo_filename);
  1258 		razor_set_list_provides(set, argv[2]);
  1259 		razor_set_destroy(set);
  1260 	} else if (strcmp(argv[1], "what-requires") == 0) {
  1261 		set = razor_set_open(repo_filename);
  1262 		razor_set_list_property_packages(set, &set->requires,
  1263 						 argv[2], argv[3]);
  1264 		razor_set_destroy(set);
  1265 	} else if (strcmp(argv[1], "what-provides") == 0) {
  1266 		set = razor_set_open(repo_filename);
  1267 		razor_set_list_property_packages(set, &set->provides,
  1268 						 argv[2], argv[3]);
  1269 		razor_set_destroy(set);
  1270 	} else if (strcmp(argv[1], "info") == 0) {
  1271 		set = razor_set_open(repo_filename);
  1272 		razor_set_info(set);
  1273 		razor_set_destroy(set);
  1274 	} else if (strcmp(argv[1], "eat-yum") == 0) {
  1275 		set = razor_set_create_from_yum_filelist(STDIN_FILENO);
  1276 		if (set == NULL)
  1277 			return 1;
  1278 		razor_set_write(set, rawhide_repo_filename);
  1279 		razor_set_destroy(set);
  1280 		printf("wrote %s\n", rawhide_repo_filename);
  1281 	} else if (strcmp(argv[1], "validate") == 0) {
  1282 		set = razor_set_open(repo_filename);
  1283 		if (set == NULL)
  1284 			return 1;
  1285 		razor_set_list_unsatisfied(set);
  1286 		razor_set_destroy(set);
  1287 	} else {
  1288 		usage();
  1289 	}
  1290 
  1291 	return 0;
  1292 }