razor.c
author Kristian H?gsberg <krh@redhat.com>
Wed Sep 12 05:31:07 2007 -0400 (2007-09-12)
changeset 19 d2a716dd92bd
parent 18 b2bf852ca8d1
child 20 0aa6cf605ea0
permissions -rw-r--r--
Clean up writing/reading package sets a bit.
     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 static struct razor_set *qsort_set;
   605 
   606 static int
   607 compare_packages(const void *p1, const void *p2)
   608 {
   609 	const struct import_package *pkg1 = p1, *pkg2 = p2;
   610 	char *pool = qsort_set->string_pool.data;
   611 
   612 	return strcmp(&pool[pkg1->name], &pool[pkg2->name]);
   613 }
   614 
   615 static int
   616 compare_properties(const void *p1, const void *p2)
   617 {
   618 	const struct import_property *prop1 = p1, *prop2 = p2;
   619 	char *pool = qsort_set->string_pool.data;
   620 	int result;
   621 
   622 	result = strcmp(&pool[prop1->name], &pool[prop2->name]);
   623 	if (result == 0)
   624 		return strcmp(&pool[prop1->version], &pool[prop2->version]);
   625 	else
   626 		return result;
   627 }
   628 
   629 static unsigned long *
   630 uniqueify_properties(struct razor_set *set,
   631 		     struct array *in, struct array *out)
   632 {
   633 	struct import_property *ip, *end;
   634 	struct razor_property *rp, *rp_end;
   635 	struct array *pkgs, *p;
   636 	unsigned long *map, *r;
   637 	int i, count, unique;
   638 
   639 	count = in->size / sizeof(struct import_property);
   640 	qsort(in->data, count,
   641 	      sizeof(struct import_property), compare_properties);
   642 
   643 	rp = NULL;
   644 	end = in->data + in->size;
   645 	for (ip = in->data; ip < end; ip++) {
   646 		if (rp == NULL ||
   647 		    ip->name != rp->name || ip->version != rp->version) {
   648 			rp = array_add(out, sizeof *rp);
   649 			rp->name = ip->name;
   650 			rp->version = ip->version;
   651 		}
   652 		ip->unique_index = rp - (struct razor_property *) out->data;
   653 	}
   654 
   655 	map = malloc(count * sizeof (unsigned long));
   656 	ip = in->data;
   657 	for (i = 0; i < count; i++)
   658 		map[ip[i].index] = ip[i].unique_index;
   659 
   660 	unique = out->size / sizeof(*rp);
   661 	pkgs = zalloc(unique * sizeof *pkgs);
   662 	for (ip = in->data; ip < end; ip++) {
   663 		r = array_add(&pkgs[ip->unique_index], sizeof *r);
   664 		*r = ip->package;
   665 	}
   666 
   667 	rp_end = out->data + out->size;
   668 	for (rp = out->data, p = pkgs; rp < rp_end; rp++, p++)
   669 		rp->packages = add_to_property_pool(set, p);
   670 
   671 	free(pkgs);
   672 
   673 	return map;
   674 }
   675 
   676 static void
   677 remap_package_links(struct import_context *ctx)
   678 {
   679 	struct import_package *p, *end;
   680 	unsigned long *pool, *r;
   681 
   682 	pool = ctx->set->property_pool.data;
   683 	end = ctx->packages.data + ctx->packages.size;
   684 	for (p = ctx->packages.data; p < end; p++) {
   685 		for (r = &pool[p->requires]; ~*r; r++)
   686 			*r = ctx->requires_map[*r];
   687 		for (r = &pool[p->provides]; ~*r; r++)
   688 			*r = ctx->provides_map[*r];
   689 	}
   690 }
   691 
   692 static void
   693 remap_property_links(struct import_context *ctx)
   694 {
   695 	struct razor_property *p, *end;
   696 	struct import_package *ip;
   697 	unsigned long *map, *pool, *r;
   698 	int i, count;
   699 
   700 	pool = ctx->set->property_pool.data;
   701 	count = ctx->packages.size / sizeof(struct import_package);
   702 	map = malloc(count * sizeof *map);
   703 	ip = ctx->packages.data;
   704 	for (i = 0; i < count; i++)
   705 		map[ip[i].index] = i;
   706 
   707 	/* FIXME: This will break if we implement package list sharing
   708 	 * for all properties, since we'll remap those lists more than
   709 	 * once. We should just have a separate pool for property
   710 	 * lists and a separate pool for package lists and remap it as
   711 	 * a flat pool.  Right now, as property lists and package
   712 	 * lists are mixed, we can't do that. */
   713 
   714 	end = ctx->set->requires.data + ctx->set->requires.size;
   715 	for (p = ctx->set->requires.data; p < end; p++)
   716 		for (r = &pool[p->packages]; ~*r; r++)
   717 			*r = map[*r];
   718 
   719 	end = ctx->set->provides.data + ctx->set->provides.size;
   720 	for (p = ctx->set->provides.data; p < end; p++)
   721 		for (r = &pool[p->packages]; ~*r; r++)
   722 			*r = map[*r];
   723 
   724 	free(map);
   725 }
   726 
   727 static struct razor_set *
   728 razor_finish_import(struct import_context *ctx)
   729 {
   730 	struct import_package *ip;
   731 	struct razor_package *rp;
   732 	int i, count;
   733 
   734 	qsort_set = ctx->set;
   735 
   736 	ctx->requires_map =
   737 		uniqueify_properties(ctx->set, 
   738 				     &ctx->requires.all,
   739 				     &ctx->set->requires);
   740 	ctx->provides_map =
   741 		uniqueify_properties(ctx->set,
   742 				     &ctx->provides.all,
   743 				     &ctx->set->provides);
   744 
   745 	remap_package_links(ctx);
   746 
   747 	count = ctx->packages.size / sizeof(struct import_package);
   748 	qsort(ctx->packages.data, count,
   749 	      sizeof(struct import_package), compare_packages);
   750 
   751 	ip = ctx->packages.data;
   752 	for (i = 0; i < count; i++, ip++, rp++) {
   753 		rp = array_add(&ctx->set->packages, sizeof *rp);
   754 		rp->name = ip->name;
   755 		rp->version = ip->version;
   756 		rp->requires = ip->requires;
   757 		rp->provides = ip->provides;
   758 	}
   759 
   760 	remap_property_links(ctx);
   761 
   762 	free(ctx->requires.all.data);
   763 	free(ctx->provides.all.data);
   764 	free(ctx->requires_map);
   765 	free(ctx->provides_map);
   766 		
   767 	fprintf(stderr, "parsed %d requires, %d unique\n",
   768 		ctx->requires.all.size / sizeof(struct import_property),
   769 		ctx->set->requires.size / sizeof(struct razor_property));
   770 	fprintf(stderr, "parsed %d provides, %d unique\n",
   771 		ctx->provides.all.size / sizeof(struct import_property),
   772 		ctx->set->provides.size / sizeof(struct razor_property));
   773 
   774 	return ctx->set; 
   775 }
   776 
   777 /* Import a yum filelist as a razor package set. */
   778 
   779 enum {
   780 	YUM_STATE_BEGIN,
   781 	YUM_STATE_PACKAGE_NAME
   782 };
   783 
   784 struct yum_context {
   785 	struct import_context ctx;
   786 	struct import_property_context *current_property_context;
   787 	char *name;
   788 	int state;
   789 };
   790 
   791 static void
   792 yum_start_element(void *data, const char *name, const char **atts)
   793 {
   794 	struct yum_context *ctx = data;
   795 	const char *n, *version;
   796 	int i;
   797 
   798 	if (strcmp(name, "name") == 0) {
   799 		ctx->state = YUM_STATE_PACKAGE_NAME;
   800 	} else if (strcmp(name, "version") == 0) {
   801 		for (i = 0; atts[i]; i += 2) {
   802 			if (strcmp(atts[i], "ver") == 0)
   803 				version = atts[i + 1];
   804 		}
   805 		import_context_add_package(&ctx->ctx, ctx->name, version);
   806 	} else if (strcmp(name, "rpm:requires") == 0) {
   807 		ctx->current_property_context = &ctx->ctx.requires;
   808 	} else if (strcmp(name, "rpm:provides") == 0) {
   809 		ctx->current_property_context = &ctx->ctx.provides;
   810 	} else if (strcmp(name, "rpm:entry") == 0 &&
   811 		   ctx->current_property_context != NULL) {
   812 		n = NULL;
   813 		version = NULL;
   814 		for (i = 0; atts[i]; i += 2) {
   815 			if (strcmp(atts[i], "name") == 0)
   816 				n = atts[i + 1];
   817 			else if (strcmp(atts[i], "ver") == 0)
   818 				version = atts[i + 1];
   819 		}
   820 
   821 		if (n == NULL) {
   822 			fprintf(stderr, "invalid rpm:entry, "
   823 				"missing name or version attributes\n");
   824 			return;
   825 		}
   826 
   827 		import_context_add_property(&ctx->ctx,
   828 					    ctx->current_property_context,
   829 					    n, version);
   830 	}
   831 }
   832 
   833 static void
   834 yum_end_element (void *data, const char *name)
   835 {
   836 	struct yum_context *ctx = data;
   837 
   838 	if (strcmp(name, "package") == 0) {
   839 		free(ctx->name);
   840 		import_context_finish_package(&ctx->ctx);
   841 	} else if (strcmp(name, "name") == 0) {
   842 		ctx->state = 0;
   843 	} else if (strcmp(name, "rpm:requires") == 0) {
   844 		ctx->current_property_context = NULL;
   845 	} else if (strcmp(name, "rpm:provides") == 0) {
   846 		ctx->current_property_context = NULL;
   847 	}
   848 }
   849 
   850 static void
   851 yum_character_data (void *data, const XML_Char *s, int len)
   852 {
   853 	struct yum_context *ctx = data;
   854 
   855 	if (ctx->state == YUM_STATE_PACKAGE_NAME)
   856 		ctx->name = strndup(s, len);
   857 }
   858 
   859 static struct razor_set *
   860 razor_set_create_from_yum_filelist(int fd)
   861 {
   862 	struct yum_context ctx;
   863 	XML_Parser parser;
   864 	char buf[4096];
   865 	int len;
   866 
   867 	razor_prepare_import(&ctx.ctx);
   868 
   869 	parser = XML_ParserCreate(NULL);
   870 	XML_SetUserData(parser, &ctx);
   871 	XML_SetElementHandler(parser, yum_start_element, yum_end_element);
   872 	XML_SetCharacterDataHandler(parser, yum_character_data);
   873 
   874 	while (1) {
   875 		len = read(fd, buf, sizeof buf);
   876 		if (len < 0) {
   877 			fprintf(stderr,
   878 				"couldn't read input: %s\n", strerror(errno));
   879 			return NULL;
   880 		} else if (len == 0)
   881 			break;
   882 
   883 		if (XML_Parse(parser, buf, len, 0) == XML_STATUS_ERROR) {
   884 			fprintf(stderr,
   885 				"%s at line %d\n",
   886 				XML_ErrorString(XML_GetErrorCode(parser)),
   887 				XML_GetCurrentLineNumber(parser));
   888 			return NULL;
   889 		}
   890 	}
   891 
   892 	XML_ParserFree(parser);
   893 
   894 	return razor_finish_import(&ctx.ctx);
   895 }
   896 
   897 void
   898 razor_set_list(struct razor_set *set)
   899 {
   900 	struct razor_package *p, *end;
   901 	char *pool;
   902 
   903 	pool = set->string_pool.data;
   904 	end = set->packages.data + set->packages.size;
   905 	for (p = set->packages.data; p < end; p++)
   906 		printf("%s %s\n", &pool[p->name], &pool[p->version]);
   907 }
   908 
   909 struct razor_set *bsearch_set;
   910 
   911 static int
   912 compare_package_name(const void *key, const void *data)
   913 {
   914 	const struct razor_package *p = data;
   915 	char *pool;
   916 
   917 	pool = bsearch_set->string_pool.data;
   918 
   919 	return strcmp(key, &pool[p->name]);
   920 }
   921 
   922 struct razor_package *
   923 razor_set_get_package(struct razor_set *set, const char *package)
   924 {
   925 	bsearch_set = set;
   926 	return bsearch(package, set->packages.data,
   927 		       set->packages.size / sizeof(struct razor_package),
   928 		       sizeof(struct razor_package), compare_package_name);
   929 }
   930 
   931 static int
   932 compare_property_name(const void *key, const void *data)
   933 {
   934 	const struct razor_property *p = data;
   935 	char *pool;
   936 
   937 	pool = bsearch_set->string_pool.data;
   938 
   939 	return strcmp(key, &pool[p->name]);
   940 }
   941 
   942 struct razor_property *
   943 razor_set_get_property(struct razor_set *set,
   944 		       struct array *properties,
   945 		       const char *property)
   946 {
   947 	struct razor_property *p, *start;
   948 
   949 	bsearch_set = set;
   950 	p = bsearch(property, properties->data,
   951 		    properties->size / sizeof(struct razor_property),
   952 		    sizeof(struct razor_property), compare_property_name);
   953 
   954 	start = properties->data;
   955 	while (p > start && (p - 1)->name == p->name)
   956 		p--;
   957 
   958 	return p;
   959 }
   960 
   961 static void
   962 razor_set_list_all_properties(struct razor_set *set, struct array *properties)
   963 {
   964 	struct razor_property *p, *end;
   965 	char *pool;
   966 
   967 	pool = set->string_pool.data;
   968 	end = properties->data + properties->size;
   969 	for (p = properties->data; p < end; p++)
   970 		printf("%s %s\n", &pool[p->name], &pool[p->version]);
   971 }
   972 
   973 void
   974 razor_set_list_requires(struct razor_set *set, const char *name)
   975 {
   976 	struct razor_property *p, *requires;
   977 	struct razor_package *package;
   978 	unsigned long *r;
   979 	char *pool;
   980 
   981 	if (name) {
   982 		package = razor_set_get_package(set, name);
   983 		r = (unsigned long *) set->property_pool.data +
   984 			package->requires;
   985 		requires = set->requires.data;
   986 		pool = set->string_pool.data;
   987 		while (~*r) {
   988 			p = &requires[*r++];
   989 			printf("%s %s\n", &pool[p->name], &pool[p->version]);
   990 		}
   991 	} else
   992 		razor_set_list_all_properties(set, &set->requires);
   993 }
   994 
   995 void
   996 razor_set_list_provides(struct razor_set *set, const char *name)
   997 {
   998 	struct razor_property *p, *provides;
   999 	struct razor_package *package;
  1000 	unsigned long *r;
  1001 	char *pool;
  1002 
  1003 	if (name) {
  1004 		package = razor_set_get_package(set, name);
  1005 		r = (unsigned long *) set->property_pool.data +
  1006 			package->provides;
  1007 		provides = set->provides.data;
  1008 		pool = set->string_pool.data;
  1009 		while (~*r) {
  1010 			p = &provides[*r++];
  1011 			printf("%s %s\n", &pool[p->name], &pool[p->version]);
  1012 		}
  1013 	} else 
  1014 		razor_set_list_all_properties(set, &set->provides);
  1015 }
  1016 
  1017 void
  1018 razor_set_list_property_packages(struct razor_set *set,
  1019 				 struct array *properties,
  1020 				 const char *name)
  1021 {
  1022 	struct razor_property *property, *end;
  1023 	struct razor_package *p, *packages;
  1024 	unsigned long *r;
  1025 	char *pool;
  1026 
  1027 	if (name == NULL)
  1028 		return;
  1029 
  1030 	property = razor_set_get_property(set, properties, name);
  1031 	packages = set->packages.data;
  1032 	pool = set->string_pool.data;
  1033 	end = properties->data + properties->size;
  1034 	while (property < end && strcmp(name, &pool[property->name]) == 0) {
  1035 		r = (unsigned long *)
  1036 			set->property_pool.data + property->packages;
  1037 		while (~*r) {
  1038 			p = &packages[*r++];
  1039 			printf("%s %s\n",
  1040 			       &pool[p->name], &pool[p->version]);
  1041 		}
  1042 		property++;
  1043 	}
  1044 }
  1045 
  1046 void
  1047 razor_set_info(struct razor_set *set)
  1048 {
  1049 	unsigned int offset, size;
  1050 	int i;
  1051 
  1052 	for (i = 0; i < set->header->sections[i].type; i++) {
  1053 		offset = set->header->sections[i].offset;
  1054 		size = set->header->sections[i].size;
  1055 
  1056 		switch (set->header->sections[i].type) {
  1057 		case RAZOR_PACKAGES:
  1058 			printf("package section:\t%dkb\n", size / 1024);
  1059 			break;
  1060 		case RAZOR_REQUIRES:
  1061 			printf("requires section:\t%dkb\n", size / 1024);
  1062 			break;
  1063 		case RAZOR_PROVIDES:
  1064 			printf("provides section:\t%dkb\n", size / 1024);
  1065 			break;
  1066 		case RAZOR_STRING_POOL:
  1067 			printf("string pool:\t\t%dkb\n", size / 1024);
  1068 			break;
  1069 		case RAZOR_PROPERTY_POOL:
  1070 			printf("properties section:\t%dkb\n", size / 1024);
  1071 			break;
  1072 		}
  1073 	}
  1074 }
  1075 
  1076 static int
  1077 usage(void)
  1078 {
  1079 	printf("usage: razor [ import FILES | lookup <key> | "
  1080 	       "list | list-requires | list-provides | eat-yum | info ]\n");
  1081 	exit(1);
  1082 }
  1083 
  1084 static const char *repo_filename = "system.repo";
  1085 static const char rawhide_repo_filename[] = "rawhide.repo";
  1086 
  1087 int
  1088 main(int argc, char *argv[])
  1089 {
  1090 	int i;
  1091 	struct razor_set *set;
  1092 	struct stat statbuf;
  1093 	struct import_context ctx;
  1094 	char *repo;
  1095 
  1096 	repo = getenv("RAZOR_REPO");
  1097 	if (repo != NULL)
  1098 		repo_filename = repo;
  1099 
  1100 	if (argc < 2) {
  1101 		usage();
  1102 	} else if (strcmp(argv[1], "import") == 0) {
  1103 		if (stat("set", &statbuf) && mkdir("set", 0777)) {
  1104 			fprintf(stderr, "could not create directory 'set'\n");
  1105 			exit(-1);
  1106 		}
  1107 			
  1108 		razor_prepare_import(&ctx);
  1109 
  1110 		for (i = 2; i < argc; i++) {
  1111 			if (razor_import(&ctx, argv[i]) < 0) {
  1112 				fprintf(stderr, "failed to import %s\n",
  1113 					argv[i]);
  1114 				exit(-1);
  1115 			}
  1116 		}
  1117 
  1118 		set = razor_finish_import(&ctx);
  1119 
  1120 		printf("bucket allocation: %d\n", set->buckets.alloc);
  1121 		printf("pool size: %d\n", set->string_pool.size);
  1122 		printf("pool allocation: %d\n", set->string_pool.alloc);
  1123 		printf("packages: %d\n",
  1124 		       set->packages.size / sizeof(struct razor_package));
  1125 		printf("requires: %d\n",
  1126 		       set->requires.size / sizeof(struct razor_property));
  1127 		printf("provides: %d\n",
  1128 		       set->provides.size / sizeof(struct razor_property));
  1129 
  1130 		razor_set_write(set, repo_filename);
  1131 
  1132 		razor_set_destroy(set);
  1133 	} else if (strcmp(argv[1], "lookup") == 0) {
  1134 		set = razor_set_open(repo_filename);
  1135 		printf("%s is %lu\n", argv[2],
  1136 		       razor_set_lookup(set, argv[2]));
  1137 		razor_set_destroy(set);
  1138 	} else if (strcmp(argv[1], "list") == 0) {
  1139 		set = razor_set_open(repo_filename);
  1140 		razor_set_list(set);
  1141 		razor_set_destroy(set);
  1142 	} else if (strcmp(argv[1], "list-requires") == 0) {
  1143 		set = razor_set_open(repo_filename);
  1144 		razor_set_list_requires(set, argv[2]);
  1145 		razor_set_destroy(set);
  1146 	} else if (strcmp(argv[1], "list-provides") == 0) {
  1147 		set = razor_set_open(repo_filename);
  1148 		razor_set_list_provides(set, argv[2]);
  1149 		razor_set_destroy(set);
  1150 	} else if (strcmp(argv[1], "what-requires") == 0) {
  1151 		set = razor_set_open(repo_filename);
  1152 		razor_set_list_property_packages(set, &set->requires, argv[2]);
  1153 		razor_set_destroy(set);
  1154 	} else if (strcmp(argv[1], "what-provides") == 0) {
  1155 		set = razor_set_open(repo_filename);
  1156 		razor_set_list_property_packages(set, &set->provides, argv[2]);
  1157 		razor_set_destroy(set);
  1158 	} else if (strcmp(argv[1], "info") == 0) {
  1159 		set = razor_set_open(repo_filename);
  1160 		razor_set_info(set);
  1161 		razor_set_destroy(set);
  1162 	} else if (strcmp(argv[1], "eat-yum") == 0) {
  1163 		set = razor_set_create_from_yum_filelist(STDIN_FILENO);
  1164 		if (set == NULL)
  1165 			return 1;
  1166 		razor_set_write(set, rawhide_repo_filename);
  1167 		razor_set_destroy(set);
  1168 		printf("wrote %s\n", rawhide_repo_filename);
  1169 	} else {
  1170 		usage();
  1171 	}
  1172 
  1173 	return 0;
  1174 }