razor.c
author Kristian H?gsberg <krh@redhat.com>
Thu Nov 08 22:45:27 2007 -0500 (2007-11-08)
changeset 77 3d14834c56ea
parent 69 35ca1ba469ec
child 78 89c06e68824a
permissions -rw-r--r--
First steps towards rpm installation.
     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 #include <ctype.h>
    14 #include <fnmatch.h>
    15 
    16 #include "razor.h"
    17 
    18 struct array {
    19 	void *data;
    20 	int size, alloc;
    21 };
    22 
    23 struct razor_set_section {
    24 	unsigned int type;
    25 	unsigned int offset;
    26 	unsigned int size;
    27 };
    28 
    29 struct razor_set_header {
    30 	unsigned int magic;
    31 	unsigned int version;
    32 	struct razor_set_section sections[0];
    33 };
    34 
    35 #define RAZOR_MAGIC 0x7a7a7a7a
    36 #define RAZOR_VERSION 1
    37 
    38 #define RAZOR_ENTRY_LAST	0x80000000ul
    39 #define RAZOR_IMMEDIATE		0x80000000ul
    40 #define RAZOR_ENTRY_MASK	0x00fffffful
    41 
    42 #define RAZOR_STRING_POOL	0
    43 #define RAZOR_PACKAGES		1
    44 #define RAZOR_PROPERTIES	2
    45 #define RAZOR_FILES		3
    46 #define RAZOR_PACKAGE_POOL	4
    47 #define RAZOR_PROPERTY_POOL	5
    48 #define RAZOR_FILE_POOL		6
    49 
    50 struct razor_package {
    51 	unsigned long name;
    52 	unsigned long version;
    53 	unsigned long properties;
    54 	unsigned long files;
    55 };
    56 
    57 struct razor_property {
    58 	unsigned long name;
    59 	unsigned long version;
    60 	unsigned long packages;
    61 };
    62 
    63 struct razor_entry {
    64 	unsigned long name;
    65 	unsigned long start;
    66 	unsigned long packages;
    67 };
    68 
    69 struct razor_set {
    70 	struct array string_pool;
    71  	struct array packages;
    72  	struct array properties;
    73  	struct array files;
    74 	struct array package_pool;
    75  	struct array property_pool;
    76  	struct array file_pool;
    77 	struct razor_set_header *header;
    78 };
    79 
    80 struct import_entry {
    81 	unsigned long package;
    82 	char *name;
    83 };
    84 
    85 struct import_directory {
    86 	unsigned long name, count;
    87 	struct array files;
    88 	struct array packages;
    89 	struct import_directory *last;
    90 };
    91 
    92 struct razor_importer {
    93 	struct razor_set *set;
    94 	struct array buckets;
    95 	struct razor_package *package;
    96 	struct array properties;
    97 	struct array files;
    98 };
    99 
   100 static void
   101 array_init(struct array *array)
   102 {
   103 	memset(array, 0, sizeof *array);
   104 }
   105 
   106 static void
   107 array_release(struct array *array)
   108 {
   109 	free(array->data);
   110 }
   111 
   112 static void *
   113 array_add(struct array *array, int size)
   114 {
   115 	int alloc;
   116 	void *data, *p;
   117 
   118 	if (array->alloc > 0)
   119 		alloc = array->alloc;
   120 	else
   121 		alloc = 16;
   122 
   123 	while (alloc < array->size + size)
   124 		alloc *= 2;
   125 
   126 	if (array->alloc < alloc) {
   127 		data = realloc(array->data, alloc);
   128 		if (data == NULL)
   129 			return 0;
   130 		array->data = data;
   131 		array->alloc = alloc;
   132 	}
   133 
   134 	p = array->data + array->size;
   135 	array->size += size;
   136 
   137 	return p;
   138 }
   139 
   140 static int
   141 write_to_fd(int fd, void *p, size_t size)
   142 {
   143 	int rest, len;
   144 
   145 	rest = size;
   146 	while (rest > 0) {
   147 		len = write(fd, p, rest);
   148 		if (len < 0)
   149 			return -1;
   150 		rest -= len;
   151 	}
   152 
   153 	return 0;
   154 }
   155 
   156 static void *
   157 zalloc(size_t size)
   158 {
   159 	void *p;
   160 
   161 	p = malloc(size);
   162 	memset(p, 0, size);
   163 
   164 	return p;
   165 }
   166 
   167 struct razor_set_section razor_sections[] = {
   168 	{ RAZOR_STRING_POOL,	offsetof(struct razor_set, string_pool) },
   169 	{ RAZOR_PACKAGES,	offsetof(struct razor_set, packages) },
   170 	{ RAZOR_PROPERTIES,	offsetof(struct razor_set, properties) },
   171 	{ RAZOR_FILES,		offsetof(struct razor_set, files) },
   172 	{ RAZOR_PACKAGE_POOL,	offsetof(struct razor_set, package_pool) },
   173 	{ RAZOR_PROPERTY_POOL,	offsetof(struct razor_set, property_pool) },
   174 	{ RAZOR_FILE_POOL,	offsetof(struct razor_set, file_pool) },
   175 };
   176 
   177 struct razor_set *
   178 razor_set_create(void)
   179 {
   180 	return zalloc(sizeof(struct razor_set));
   181 }
   182 
   183 struct razor_set *
   184 razor_set_open(const char *filename)
   185 {
   186 	struct razor_set *set;
   187 	struct razor_set_section *s;
   188 	struct stat stat;
   189 	struct array *array;
   190 	int fd;
   191 
   192 	set = zalloc(sizeof *set);
   193 	fd = open(filename, O_RDONLY);
   194 	if (fstat(fd, &stat) < 0)
   195 		return NULL;
   196 	set->header = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
   197 	if (set->header == MAP_FAILED) {
   198 		free(set);
   199 		return NULL;
   200 	}
   201 
   202 	for (s = set->header->sections; ~s->type; s++) {
   203 		if (s->type >= ARRAY_SIZE(razor_sections))
   204 			continue;
   205 		if (s->type != razor_sections[s->type].type)
   206 			continue;
   207 		array = (void *) set + razor_sections[s->type].offset;
   208 		array->data = (void *) set->header + s->offset;
   209 		array->size = s->size;
   210 		array->alloc = s->size;
   211 	}
   212 	close(fd);
   213 
   214 	return set;
   215 }
   216 
   217 void
   218 razor_set_destroy(struct razor_set *set)
   219 {
   220 	unsigned int size;
   221 	struct array *a;
   222 	int i;
   223 
   224 	if (set->header) {
   225 		for (i = 0; set->header->sections[i].type; i++)
   226 			;
   227 		size = set->header->sections[i].type;
   228 		munmap(set->header, size);
   229 	} else {
   230 		for (i = 0; i < ARRAY_SIZE(razor_sections); i++) {
   231 			a = (void *) set + razor_sections[i].offset;
   232 			free(a->data);
   233 		}
   234 	}
   235 
   236 	free(set);
   237 }
   238 
   239 int
   240 razor_set_write(struct razor_set *set, const char *filename)
   241 {
   242 	char data[4096];
   243 	struct razor_set_header *header = (struct razor_set_header *) data;
   244 	struct array *a;
   245 	unsigned long offset;
   246 	int i, fd;
   247 
   248 	memset(data, 0, sizeof data);
   249 	header->magic = RAZOR_MAGIC;
   250 	header->version = RAZOR_VERSION;
   251 	offset = sizeof data;
   252 
   253 	for (i = 0; i < ARRAY_SIZE(razor_sections); i++) {
   254 		if (razor_sections[i].type != i)
   255 			continue;
   256 		a = (void *) set + razor_sections[i].offset;
   257 		header->sections[i].type = i;
   258 		header->sections[i].offset = offset;
   259 		header->sections[i].size = a->size;
   260 		offset += (a->size + 4095) & ~4095;
   261 	}
   262 
   263 	header->sections[i].type = ~0;
   264 	header->sections[i].offset = 0;
   265 	header->sections[i].size = 0;
   266 
   267 	fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666);
   268 	if (fd < 0)
   269 		return -1;
   270 
   271 	write_to_fd(fd, data, sizeof data);
   272 	for (i = 0; i < ARRAY_SIZE(razor_sections); i++) {
   273 		if (razor_sections[i].type != i)
   274 			continue;
   275 		a = (void *) set + razor_sections[i].offset;
   276 		write_to_fd(fd, a->data, (a->size + 4095) & ~4095);
   277 	}
   278 
   279 	close(fd);
   280 
   281 	return 0;
   282 }
   283 
   284 static unsigned int
   285 hash_string(const char *key)
   286 {
   287 	const char *p;
   288 	unsigned int hash = 0;
   289 
   290 	for (p = key; *p; p++)
   291 		hash = (hash * 617) ^ *p;
   292 
   293 	return hash;
   294 }
   295 
   296 static unsigned long
   297 razor_importer_lookup(struct razor_importer *importer, const char *key)
   298 {
   299 	unsigned int mask, start, i;
   300 	unsigned long *b;
   301 	char *pool;
   302 
   303 	pool = importer->set->string_pool.data;
   304 	mask = importer->buckets.alloc - 1;
   305 	start = hash_string(key) * sizeof(unsigned long);
   306 
   307 	for (i = 0; i < importer->buckets.alloc; i += sizeof *b) {
   308 		b = importer->buckets.data + ((start + i) & mask);
   309 
   310 		if (*b == 0)
   311 			return 0;
   312 
   313 		if (strcmp(key, &pool[*b]) == 0)
   314 			return *b;
   315 	}
   316 
   317 	return 0;
   318 }
   319 
   320 static unsigned long
   321 add_to_string_pool(struct razor_set *set, const char *key)
   322 {
   323 	int len;
   324 	char *p;
   325 
   326 	len = strlen(key) + 1;
   327 	p = array_add(&set->string_pool, len);
   328 	memcpy(p, key, len);
   329 
   330 	return p - (char *) set->string_pool.data;
   331 }
   332 
   333 static unsigned long
   334 add_to_property_pool(struct array *pool, struct array *properties)
   335 {
   336 	unsigned long  *p;
   337 
   338 	p = array_add(pool, properties->size);
   339 	memcpy(p, properties->data, properties->size);
   340 	p[properties->size / sizeof *p - 1] |= RAZOR_IMMEDIATE;
   341 
   342 	return p - (unsigned long *) pool->data;
   343 }
   344 
   345 static void
   346 do_insert(struct razor_importer *importer, unsigned long value)
   347 {
   348 	unsigned int mask, start, i;
   349 	unsigned long *b;
   350 	const char *key;
   351 
   352 	key = (char *) importer->set->string_pool.data + value;
   353 	mask = importer->buckets.alloc - 1;
   354 	start = hash_string(key) * sizeof(unsigned long);
   355 
   356 	for (i = 0; i < importer->buckets.alloc; i += sizeof *b) {
   357 		b = importer->buckets.data + ((start + i) & mask);
   358 		if (*b == 0) {
   359 			*b = value;
   360 			break;
   361 		}
   362 	}
   363 }
   364 
   365 static unsigned long
   366 razor_importer_insert(struct razor_importer *importer, const char *key)
   367 {
   368 	unsigned long value, *buckets, *b, *end;
   369 	int alloc;
   370 
   371 	alloc = importer->buckets.alloc;
   372 	array_add(&importer->buckets, 4 * sizeof *buckets);
   373 	if (alloc != importer->buckets.alloc) {
   374 		end = importer->buckets.data + alloc;
   375 		memset(end, 0, importer->buckets.alloc - alloc);
   376 		for (b = importer->buckets.data; b < end; b++) {
   377 			value = *b;
   378 			if (value != 0) {
   379 				*b = 0;
   380 				do_insert(importer, value);
   381 			}
   382 		}
   383 	}
   384 
   385 	value = add_to_string_pool(importer->set, key);
   386 	do_insert (importer, value);
   387 
   388 	return value;
   389 }
   390 
   391 static unsigned long
   392 razor_importer_tokenize(struct razor_importer *importer, const char *string)
   393 {
   394 	unsigned long token;
   395 
   396 	if (string == NULL)
   397 		return razor_importer_tokenize(importer, "");
   398 
   399 	token = razor_importer_lookup(importer, string);
   400 	if (token != 0)
   401 		return token;
   402 
   403 	return razor_importer_insert(importer, string);
   404 }
   405 
   406 void
   407 razor_importer_begin_package(struct razor_importer *importer,
   408 			     const char *name, const char *version)
   409 {
   410 	struct razor_package *p;
   411 
   412 	p = array_add(&importer->set->packages, sizeof *p);
   413 	p->name = razor_importer_tokenize(importer, name);
   414 	p->version = razor_importer_tokenize(importer, version);
   415 
   416 	importer->package = p;
   417 	array_init(&importer->properties);
   418 }
   419 
   420 void
   421 razor_importer_finish_package(struct razor_importer *importer)
   422 {
   423 	struct razor_package *p;
   424 
   425 	p = importer->package;
   426 	p->properties = add_to_property_pool(&importer->set->property_pool,
   427 					     &importer->properties);
   428 
   429 	array_release(&importer->properties);
   430 }
   431 
   432 void
   433 razor_importer_add_property(struct razor_importer *importer,
   434 			    const char *name, const char *version,
   435 			    enum razor_property_type type)
   436 {
   437 	struct razor_property *p;
   438 	unsigned long *r;
   439 
   440 	p = array_add(&importer->set->properties, sizeof *p);
   441 	p->name = razor_importer_tokenize(importer, name) | (type << 30);
   442 	p->version = razor_importer_tokenize(importer, version);
   443 	p->packages = importer->package -
   444 		(struct razor_package *) importer->set->packages.data;
   445 
   446 	r = array_add(&importer->properties, sizeof *r);
   447 	*r = p - (struct razor_property *) importer->set->properties.data;
   448 }
   449 
   450 void
   451 razor_importer_add_file(struct razor_importer *importer, const char *name)
   452 {
   453 	struct import_entry *e;
   454 
   455 	e = array_add(&importer->files, sizeof *e);
   456 
   457 	e->package = importer->package -
   458 		(struct razor_package *) importer->set->packages.data;
   459 	e->name = strdup(name);
   460 }
   461 
   462 struct razor_importer *
   463 razor_importer_new(void)
   464 {
   465 	struct razor_importer *importer;
   466 
   467 	importer = zalloc(sizeof *importer);
   468 	importer->set = razor_set_create();
   469 
   470 	return importer;
   471 }
   472 
   473 /* Destroy an importer without creating the set. */
   474 void
   475 razor_importer_destroy(struct razor_importer *importer)
   476 {
   477 	/* FIXME: write this */
   478 }
   479 
   480 
   481 typedef int (*compare_with_data_func_t)(const void *p1,
   482 					const void *p,
   483 					void *data);
   484 
   485 struct qsort_context {
   486 	size_t size;
   487 	compare_with_data_func_t compare;
   488 	void *data;
   489 };
   490 
   491 static void
   492 qsort_swap(void *p1, void *p2, size_t size)
   493 {
   494 	char buffer[size];
   495 
   496 	memcpy(buffer, p1, size);
   497 	memcpy(p1, p2, size);
   498 	memcpy(p2, buffer, size);
   499 }
   500 
   501 static void
   502 __qsort_with_data(void *base, size_t nelem, unsigned long *map,
   503 		  struct qsort_context *ctx)
   504 {
   505 	void *p, *start, *end, *pivot;
   506 	unsigned long *mp, *mstart, *mend, tmp;
   507 	int left, right, result;
   508 	size_t size = ctx->size;
   509 
   510 	p = base;
   511 	start = base;
   512 	end = base + nelem * size;
   513 	mp = map;
   514 	mstart = map;
   515 	mend = map + nelem;
   516 	pivot = base + (random() % nelem) * size;
   517 
   518 	while (p < end) {
   519 		result = ctx->compare(p, pivot, ctx->data);
   520 		if (result < 0) {
   521 			qsort_swap(p, start, size);
   522 			tmp = *mp;
   523 			*mp = *mstart;
   524 			*mstart = tmp;
   525 			if (start == pivot)
   526 				pivot = p;
   527 			start += size;
   528 			mstart++;
   529 			p += size;
   530 			mp++;
   531 		} else if (result == 0) {
   532 			p += size;
   533 			mp++;
   534 		} else {
   535  			end -= size;
   536 			mend--;
   537 			qsort_swap(p, end, size);
   538 			tmp = *mp;
   539 			*mp = *mend;
   540 			*mend = tmp;
   541 			if (end == pivot)
   542 				pivot = p;
   543 		}
   544 	}
   545 
   546 	left = (start - base) / size;
   547 	right = (base + nelem * size - end) / size;
   548 	if (left > 1)
   549 		__qsort_with_data(base, left, map, ctx);
   550 	if (right > 1)
   551 		__qsort_with_data(end, right, mend, ctx);
   552 }
   553 
   554 unsigned long *
   555 qsort_with_data(void *base, size_t nelem, size_t size,
   556 		compare_with_data_func_t compare, void *data)
   557 {
   558 	struct qsort_context ctx;
   559 	unsigned long *map;
   560 	int i;
   561 
   562 	if (nelem == 0)
   563 		return NULL;
   564 
   565 	ctx.size = size;
   566 	ctx.compare = compare;
   567 	ctx.data = data;
   568 
   569 	map = malloc(nelem * sizeof (unsigned long));
   570 	for (i = 0; i < nelem; i++)
   571 		map[i] = i;
   572 
   573 	__qsort_with_data(base, nelem, map, &ctx);
   574 
   575 	return map;
   576 }
   577 
   578 static int
   579 versioncmp(const char *s1, const char *s2)
   580 {
   581 	const char *p1, *p2;
   582 	long n1, n2;
   583 	int res;
   584 
   585 	n1 = strtol(s1, (char **) &p1, 0);
   586 	n2 = strtol(s2, (char **) &p2, 0);
   587 
   588 	/* Epoch; if one but not the other has an epoch set, default
   589 	 * the epoch-less version to 0. */
   590 	res = (*p1 == ':') - (*p2 == ':');
   591 	if (res < 0) {
   592 		n1 = 0;
   593 		p1 = s1;
   594 		p2++;
   595 	} else if (res > 0) {
   596 		p1++;
   597 		n2 = 0;
   598 		p2 = s2;
   599 	}
   600 
   601 	if (n1 != n2)
   602 		return n1 - n2;
   603 	while (*p1 && *p2) {
   604 		if (*p1 != *p2)
   605 			return *p1 - *p2;
   606 		p1++;
   607 		p2++;
   608 		if (isdigit(*p1) && isdigit(*p2))
   609 			return versioncmp(p1, p2);
   610 	}
   611 
   612 	return *p1 - *p2;
   613 }
   614 
   615 
   616 static int
   617 compare_packages(const void *p1, const void *p2, void *data)
   618 {
   619 	const struct razor_package *pkg1 = p1, *pkg2 = p2;
   620 	struct razor_set *set = data;
   621 	char *pool = set->string_pool.data;
   622 
   623 	if (pkg1->name == pkg2->name)
   624 		return versioncmp(&pool[pkg1->version], &pool[pkg2->version]);
   625 	else
   626 		return strcmp(&pool[pkg1->name], &pool[pkg2->name]);
   627 }
   628 
   629 static int
   630 compare_properties(const void *p1, const void *p2, void *data)
   631 {
   632 	const struct razor_property *prop1 = p1, *prop2 = p2;
   633 	struct razor_set *set = data;
   634 	char *pool = set->string_pool.data;
   635 
   636 	if (prop1->name == prop2->name)
   637 		return versioncmp(&pool[prop1->version],
   638 				  &pool[prop2->version]);
   639 	else if ((prop1->name & RAZOR_ENTRY_MASK) == (prop2->name & RAZOR_ENTRY_MASK))
   640 		return (prop1->name >> 30) - (prop2->name >> 30);
   641 	else
   642 		return strcmp(&pool[prop1->name & RAZOR_ENTRY_MASK],
   643 			      &pool[prop2->name & RAZOR_ENTRY_MASK]);
   644 }
   645 
   646 static unsigned long *
   647 uniqueify_properties(struct razor_set *set)
   648 {
   649 	struct razor_property *rp, *up, *rp_end;
   650 	struct array *pkgs, *p;
   651 	unsigned long *map, *rmap, *r;
   652 	int i, count, unique;
   653 
   654 	count = set->properties.size / sizeof(struct razor_property);
   655 	map = qsort_with_data(set->properties.data,
   656 			      count,
   657 			      sizeof(struct razor_property),
   658 			      compare_properties,
   659 			      set);
   660 
   661 	rp_end = set->properties.data + set->properties.size;
   662 	rmap = malloc(count * sizeof *map);
   663 	pkgs = zalloc(count * sizeof *pkgs);
   664 	for (rp = set->properties.data, up = rp, i = 0; rp < rp_end; rp++, i++) {
   665 		if (rp->name != up->name || rp->version != up->version) {
   666 			up++;
   667 			up->name = rp->name;
   668 			up->version = rp->version;
   669 		}
   670 
   671 		unique = up - (struct razor_property *) set->properties.data;
   672 		rmap[map[i]] = unique;
   673 		r = array_add(&pkgs[unique], sizeof *r);
   674 		*r = rp->packages;
   675 	}
   676 	free(map);
   677 
   678 	up++;
   679 	set->properties.size = (void *) up - set->properties.data;
   680 	rp_end = up;
   681 	for (rp = set->properties.data, p = pkgs; rp < rp_end; rp++, p++) {
   682 		if (p->size / sizeof *r == 1) {
   683 			r = p->data;
   684 			rp->packages = *r | RAZOR_IMMEDIATE;
   685 		} else {
   686 			rp->packages =
   687 				add_to_property_pool(&set->package_pool, p);
   688 		}
   689 		array_release(p);
   690 	}
   691 
   692 	free(pkgs);
   693 
   694 	return rmap;
   695 }
   696 
   697 static void
   698 remap_links(struct array *links, unsigned long *map)
   699 {
   700 	unsigned long *p, *end;
   701 
   702 	end = links->data + links->size;
   703 	for (p = links->data; p < end; p++)
   704 		*p = map[*p & RAZOR_ENTRY_MASK] | (*p & ~RAZOR_ENTRY_MASK);
   705 }
   706 
   707 static int
   708 compare_filenames(const void *p1, const void *p2, void *data)
   709 {
   710 	const struct import_entry *e1 = p1;
   711 	const struct import_entry *e2 = p2;
   712 
   713 	return strcmp(e1->name, e2->name);
   714 }
   715 
   716 static void
   717 count_entries(struct import_directory *d)
   718 {
   719 	struct import_directory *p, *end;
   720 
   721 	p = d->files.data;
   722 	end = d->files.data + d->files.size;
   723 	d->count = 0;
   724 	while (p < end) {
   725 		count_entries(p);
   726 		d->count += p->count + 1;
   727 		p++;
   728 	}		
   729 }
   730 
   731 static void
   732 serialize_files(struct razor_set *set,
   733 		struct import_directory *d, struct array *array)
   734 {
   735 	struct import_directory *p, *end;
   736 	struct razor_entry *e = NULL;
   737 	unsigned long s, *r;
   738 
   739 	p = d->files.data;
   740 	end = d->files.data + d->files.size;
   741 	s = array->size / sizeof *e + d->files.size / sizeof *p;
   742 	while (p < end) {
   743 		e = array_add(array, sizeof *e);
   744 		e->name = p->name;
   745 		e->start = p->count > 0 ? s : 0;
   746 		s += p->count;
   747 
   748 		if (p->packages.size == 0) {
   749 			/* FIXME: We need to make sure this is handled
   750 			 * correctly as the empty list. */
   751 			e->packages = 0 | RAZOR_IMMEDIATE;
   752 		} else if (p->packages.size / sizeof *r == 1) {
   753 			r = p->packages.data;
   754 			e->packages = *r | RAZOR_IMMEDIATE;
   755 		} else {
   756 			e->packages = add_to_property_pool(&set->package_pool,
   757 							   &p->packages);
   758 		}
   759 		array_release(&p->packages);
   760 		p++;
   761 	}		
   762 	if (e != NULL)
   763 		e->name |= RAZOR_ENTRY_LAST;
   764 
   765 	p = d->files.data;
   766 	end = d->files.data + d->files.size;
   767 	while (p < end) {
   768 		serialize_files(set, p, array);
   769 		p++;
   770 	}
   771 }
   772 
   773 static void
   774 remap_property_package_links(struct array *properties, unsigned long *rmap)
   775 {
   776 	struct razor_property *p, *end;
   777 
   778 	end = properties->data + properties->size;
   779 	for (p = properties->data; p < end; p++)
   780 		if (p->packages & RAZOR_IMMEDIATE)
   781 			p->packages = rmap[p->packages & RAZOR_ENTRY_MASK] |
   782 				RAZOR_IMMEDIATE;
   783 }
   784 
   785 static void
   786 build_file_tree(struct razor_importer *importer)
   787 {
   788 	int count, i, length;
   789 	struct import_entry *filenames;
   790 	char *f, *end;
   791 	unsigned long name, *r;
   792 	char dirname[256];
   793 	struct import_directory *d, root;
   794 	struct razor_entry *e;
   795 
   796 	count = importer->files.size / sizeof (struct import_entry);
   797 	qsort_with_data(importer->files.data,
   798 			count,
   799 			sizeof (struct import_entry),
   800 			compare_filenames,
   801 			NULL);
   802 
   803 	root.name = razor_importer_tokenize(importer, "");
   804 	array_init(&root.files);
   805 	array_init(&root.packages);
   806 	root.last = NULL;
   807 
   808 	filenames = importer->files.data;
   809 	for (i = 0; i < count; i++) {
   810 		f = filenames[i].name;
   811 		if (*f != '/')
   812 			continue;
   813 		f++;
   814 
   815 		d = &root;
   816 		while (*f) {
   817 			end = strchr(f, '/');
   818 			if (end == NULL)
   819 				end = f + strlen(f);
   820 			length = end - f;
   821 			memcpy(dirname, f, length);
   822 			dirname[length] ='\0';
   823 			name = razor_importer_tokenize(importer, dirname);
   824 			if (d->last == NULL || d->last->name != name) {
   825 				d->last = array_add(&d->files, sizeof *d);
   826 				d->last->name = name;
   827 				d->last->last = NULL;
   828 				array_init(&d->last->files);
   829 				array_init(&d->last->packages);
   830 			}
   831 			d = d->last;				
   832 			f = end + 1;
   833 			if (*end == '\0')
   834 				break;
   835 		}
   836 
   837 		r = array_add(&d->packages, sizeof *r);
   838 		*r = filenames[i].package;
   839 		free(filenames[i].name);
   840 	}
   841 
   842 	count_entries(&root);
   843 	array_init(&importer->set->files);
   844 
   845 	e = array_add(&importer->set->files, sizeof *e);
   846 	e->name = root.name | RAZOR_ENTRY_LAST;
   847 	e->start = 1;
   848 	e->packages = 0;
   849 
   850 	serialize_files(importer->set, &root, &importer->set->files);
   851 
   852 	array_release(&importer->files);
   853 }
   854 
   855 static void
   856 build_package_file_lists(struct razor_set *set, unsigned long *rmap)
   857 {
   858 	struct razor_package *p, *packages;
   859 	struct array *pkgs;
   860 	struct razor_entry *e, *end;
   861 	unsigned long *r, *q;
   862 	int i, count;
   863 
   864 	count = set->packages.size / sizeof *p;
   865 	pkgs = zalloc(count * sizeof *pkgs);
   866 
   867 	e = set->files.data;
   868 	end = set->files.data + set->files.size;
   869 	while (e < end) {
   870 		if (e->packages & RAZOR_IMMEDIATE) {
   871 			e->packages = rmap[e->packages & RAZOR_ENTRY_MASK] |
   872 				RAZOR_IMMEDIATE;
   873 			r = &e->packages;
   874 		} else {
   875 			r = (unsigned long *) set->package_pool.data + e->packages;
   876 		}
   877 
   878 		while (1) {
   879 			q = array_add(&pkgs[*r & RAZOR_ENTRY_MASK], sizeof *q);
   880 			*q = e - (struct razor_entry *) set->files.data;
   881 			if (*r++ & RAZOR_IMMEDIATE)
   882 				break;
   883 		}
   884 		e++;
   885 	}
   886 
   887 	packages = set->packages.data;
   888 	for (i = 0; i < count; i++) {
   889 		packages[i].files =
   890 			add_to_property_pool(&set->file_pool, &pkgs[i]);
   891 		array_release(&pkgs[i]);
   892 	}
   893 	free(pkgs);
   894 }
   895 
   896 struct razor_set *
   897 razor_importer_finish(struct razor_importer *importer)
   898 {
   899 	struct razor_set *set;
   900 	unsigned long *map, *rmap;
   901 	int i, count;
   902 
   903 	map = uniqueify_properties(importer->set);
   904 	remap_links(&importer->set->property_pool, map);
   905 	free(map);
   906 
   907 	count = importer->set->packages.size / sizeof(struct razor_package);
   908 	map = qsort_with_data(importer->set->packages.data,
   909 			      count,
   910 			      sizeof(struct razor_package),
   911 			      compare_packages,
   912 			      importer->set);
   913 
   914 	rmap = malloc(count * sizeof *rmap);
   915 	for (i = 0; i < count; i++)
   916 		rmap[map[i]] = i;
   917 	free(map);
   918 
   919 	build_file_tree(importer);
   920 	remap_links(&importer->set->package_pool, rmap);
   921 	build_package_file_lists(importer->set, rmap);
   922 	remap_property_package_links(&importer->set->properties, rmap);
   923 	free(rmap);
   924 
   925 	set = importer->set;
   926 	array_release(&importer->buckets);
   927 	free(importer);
   928 
   929 	return set;
   930 }
   931 
   932 void
   933 razor_set_list(struct razor_set *set, const char *pattern)
   934 {
   935 	struct razor_package *p, *end;
   936 	int with_version = 0;
   937 	char *pool;
   938 
   939 	pool = set->string_pool.data;
   940 	end = set->packages.data + set->packages.size;
   941 	for (p = set->packages.data; p < end; p++) {
   942 		if (pattern && fnmatch(pattern, &pool[p->name], 0) != 0)
   943 		    continue;
   944 		if (with_version)
   945 			printf("%s-%s\n", &pool[p->name], &pool[p->version]);
   946 		else
   947 			printf("%s\n", &pool[p->name]);
   948 	}
   949 }
   950 
   951 struct razor_set *bsearch_set;
   952 
   953 static int
   954 compare_package_name(const void *key, const void *data)
   955 {
   956 	const struct razor_package *p = data;
   957 	char *pool;
   958 
   959 	pool = bsearch_set->string_pool.data;
   960 
   961 	return strcmp(key, &pool[p->name]);
   962 }
   963 
   964 struct razor_package *
   965 razor_set_get_package(struct razor_set *set, const char *package)
   966 {
   967 	bsearch_set = set;
   968 	return bsearch(package, set->packages.data,
   969 		       set->packages.size / sizeof(struct razor_package),
   970 		       sizeof(struct razor_package), compare_package_name);
   971 }
   972 
   973 static int
   974 compare_property_name(const void *key, const void *data)
   975 {
   976 	const struct razor_property *p = data;
   977 	char *pool;
   978 
   979 	pool = bsearch_set->string_pool.data;
   980 
   981 	return strcmp(key, &pool[p->name & RAZOR_ENTRY_MASK]);
   982 }
   983 
   984 struct razor_property *
   985 razor_set_get_property(struct razor_set *set, const char *property)
   986 {
   987 	struct razor_property *p, *start;
   988 
   989 	bsearch_set = set;
   990 	p = bsearch(property, set->properties.data,
   991 		    set->properties.size / sizeof(struct razor_property),
   992 		    sizeof(struct razor_property), compare_property_name);
   993 
   994 	start = set->properties.data;
   995 	while (p > start &&
   996 	       ((p - 1)->name & RAZOR_ENTRY_MASK) ==
   997 	       (p->name & RAZOR_ENTRY_MASK))
   998 		p--;
   999 
  1000 	return p;
  1001 }
  1002 
  1003 void
  1004 razor_set_list_properties(struct razor_set *set, const char *name,
  1005 			  enum razor_property_type type)
  1006 {
  1007 	struct razor_property *p, *properties, *end;
  1008 	struct razor_package *package;
  1009 	unsigned long *r;
  1010 	char *pool;
  1011 
  1012 	pool = set->string_pool.data;
  1013 
  1014 	if (name) {
  1015 		package = razor_set_get_package(set, name);
  1016 		r = (unsigned long *) set->property_pool.data +
  1017 			package->properties;
  1018 		properties = set->properties.data;
  1019 		while (1) {
  1020 			p = &properties[*r & RAZOR_ENTRY_MASK];
  1021 			if ((p->name >> 30) != type)
  1022 				goto next;
  1023 			if (pool[p->version] == '\0')
  1024 				printf("%s\n",
  1025 				       &pool[p->name & RAZOR_ENTRY_MASK]);
  1026 			else
  1027 				printf("%s-%s\n",
  1028 				       &pool[p->name & RAZOR_ENTRY_MASK],
  1029 				       &pool[p->version]);
  1030 		next:
  1031 			if (*r++ & RAZOR_IMMEDIATE)
  1032 				break;
  1033 		}
  1034 	} else {
  1035 		end = set->properties.data + set->properties.size;
  1036 		for (p = set->properties.data; p < end; p++) {
  1037 			if ((p->name >> 30) != type)
  1038 				continue;
  1039 			if (pool[p->version] == '\0')
  1040 				printf("%s\n",
  1041 				       &pool[p->name & RAZOR_ENTRY_MASK]);
  1042 			else
  1043 				printf("%s-%s\n",
  1044 				       &pool[p->name & RAZOR_ENTRY_MASK],
  1045 				       &pool[p->version]);
  1046 		}
  1047 	}
  1048 }
  1049 
  1050 void
  1051 razor_set_list_property_packages(struct razor_set *set,
  1052 				 const char *name,
  1053 				 const char *version,
  1054 				 enum razor_property_type type)
  1055 {
  1056 	struct razor_property *property, *end;
  1057 	struct razor_package *p, *packages;
  1058 	unsigned long *r;
  1059 	char *pool;
  1060 
  1061 	if (name == NULL)
  1062 		return;
  1063 
  1064 	property = razor_set_get_property(set, name);
  1065 	packages = set->packages.data;
  1066 	pool = set->string_pool.data;
  1067 	end = set->properties.data + set->properties.size;
  1068 	while (property < end &&
  1069 	       strcmp(name, &pool[property->name & RAZOR_ENTRY_MASK]) == 0) {
  1070 		if (version &&
  1071 		    versioncmp(version, &pool[property->version]) != 0)
  1072 			goto next;
  1073 		if (type != (property->name >> 30))
  1074 			goto next;
  1075 		
  1076 		if (property->packages & RAZOR_IMMEDIATE)
  1077 			r = &property->packages;
  1078 		else
  1079 			r = (unsigned long *)
  1080 				set->package_pool.data + property->packages;
  1081 		while (1) {
  1082 			p = &packages[*r & RAZOR_ENTRY_MASK];
  1083 			printf("%s-%s\n",
  1084 			       &pool[p->name & RAZOR_ENTRY_MASK],
  1085 			       &pool[p->version]);
  1086 			if (*r++ & RAZOR_IMMEDIATE)
  1087 				break;
  1088 		}
  1089 	next:
  1090 		property++;
  1091 	}
  1092 }
  1093 
  1094 static struct razor_entry *
  1095 find_entry(struct razor_set *set, struct razor_entry *dir, const char *pattern)
  1096 {
  1097 	struct razor_entry *e;
  1098 	const char *n, *pool = set->string_pool.data;
  1099 	int len;
  1100 
  1101 	e = (struct razor_entry *) set->files.data + dir->start;
  1102 	do {
  1103 		n = pool + (e->name & RAZOR_ENTRY_MASK);
  1104 		if (strcmp(pattern + 1, n) == 0)
  1105 			return e;
  1106 		len = strlen(n);
  1107 		if (e->start != 0 && strncmp(pattern + 1, n, len) == 0 &&
  1108 		    pattern[len + 1] == '/') {
  1109 			return find_entry(set, e, pattern + len + 1);
  1110 		}
  1111 	} while (((e++)->name & RAZOR_ENTRY_LAST) == 0);
  1112 
  1113 	return NULL;
  1114 }
  1115 
  1116 static void
  1117 list_dir(struct razor_set *set, struct razor_entry *dir,
  1118 	 const char *prefix, const char *pattern)
  1119 {
  1120 	struct razor_entry *e;
  1121 	const char *n, *pool = set->string_pool.data;
  1122 
  1123 	e = (struct razor_entry *) set->files.data + dir->start;
  1124 	do {
  1125 		n = pool + (e->name & RAZOR_ENTRY_MASK);
  1126 		if (pattern && pattern[0] && fnmatch(pattern, n, 0) != 0)
  1127 			continue;
  1128 		printf("%s/%s%s\n", prefix, n, e->start > 0 ? "/" : "");
  1129 	} while (((e++)->name & RAZOR_ENTRY_LAST) == 0);
  1130 }
  1131 
  1132 void
  1133 razor_set_list_files(struct razor_set *set, const char *pattern)
  1134 {
  1135 	struct razor_entry *e;
  1136 	char buffer[512], *p, *base;
  1137 
  1138 	if (pattern == NULL)
  1139 		pattern = "/";
  1140 
  1141 	strcpy(buffer, pattern);
  1142 	e = find_entry(set, set->files.data, buffer);
  1143 	if (e && e->start > 0) {
  1144 		base = NULL;
  1145 	} else {
  1146 		p = strrchr(buffer, '/');
  1147 		if (p) {
  1148 			*p = '\0';
  1149 			base = p + 1;
  1150 		} else {
  1151 			base = NULL;
  1152 		}
  1153 	}
  1154 	e = find_entry(set, set->files.data, buffer);
  1155 	if (e->start != 0)
  1156 		list_dir(set, e, buffer, base);
  1157 }
  1158 
  1159 void
  1160 razor_set_list_file_packages(struct razor_set *set, const char *filename)
  1161 {
  1162 	struct razor_entry *e;
  1163 	struct razor_package *packages, *p;
  1164 	const char *pool;
  1165 	unsigned long *r;
  1166 
  1167 	e = find_entry(set, set->files.data, filename);
  1168 	if (e == NULL)
  1169 		return;
  1170 	
  1171 	if (e->packages & RAZOR_IMMEDIATE)
  1172 		r = &e->packages;
  1173 	else
  1174 		r = (unsigned long *) set->package_pool.data + e->packages;
  1175 
  1176 	packages = set->packages.data;
  1177 	pool = set->string_pool.data;
  1178 	while (1) {
  1179 		p = &packages[*r & RAZOR_ENTRY_MASK];
  1180 		printf("%s-%s\n", &pool[p->name], &pool[p->version]);
  1181 		if (*r++ & RAZOR_IMMEDIATE)
  1182 			break;
  1183 	}
  1184 }
  1185 
  1186 static unsigned long *
  1187 list_package_files(struct razor_set *set, unsigned long *r,
  1188 		   struct razor_entry *dir, unsigned long end,
  1189 		   char *prefix)
  1190 {
  1191 	struct razor_entry *e, *f, *entries;
  1192 	unsigned long next;
  1193 	char *pool;
  1194 	int len;
  1195 	
  1196 	entries = (struct razor_entry *) set->files.data;
  1197 	pool = set->string_pool.data;
  1198 
  1199 	e = entries + dir->start;
  1200 	do {
  1201 		if (entries + *r == e) {
  1202 			printf("%s/%s\n", prefix,
  1203 			       pool + (e->name & RAZOR_ENTRY_MASK));
  1204 			r++;
  1205 			if (*r >= end)
  1206 				break;
  1207 		}
  1208 	} while (((e++)->name & RAZOR_ENTRY_LAST) == 0);
  1209 
  1210 	e = entries + dir->start;
  1211 	do {
  1212 		if (e->start == 0)
  1213 			continue;
  1214 
  1215 		if (e->name & RAZOR_ENTRY_LAST)
  1216 			next = end;
  1217 		else {
  1218 			f = e + 1; 
  1219 			while (f->start == 0 && !(f->name & RAZOR_ENTRY_LAST))
  1220 				f++;
  1221 			if (f->start == 0)
  1222 				next = end;
  1223 			else
  1224 				next = f->start;
  1225 		}
  1226 
  1227 		if (e->start <= *r && *r < next) {
  1228 			len = strlen(prefix);
  1229 			prefix[len] = '/';
  1230 			strcpy(prefix + len + 1,
  1231 			       pool + (e->name & RAZOR_ENTRY_MASK));
  1232 			r = list_package_files(set, r, e, next, prefix);
  1233 			prefix[len] = '\0';
  1234 			if (*r >= end)
  1235 				break;
  1236 		}
  1237 	} while (((e++)->name & RAZOR_ENTRY_LAST) == 0);
  1238 
  1239 	return r;
  1240 }
  1241 
  1242 void
  1243 razor_set_list_package_files(struct razor_set *set, const char *name)
  1244 {
  1245 	struct razor_package *package;
  1246 	unsigned long *r, end;
  1247 	char buffer[512];
  1248 
  1249 	package = razor_set_get_package(set, name);
  1250 
  1251 	r = (unsigned long *) set->file_pool.data + package->files;
  1252 	end = set->files.size / sizeof (struct razor_entry);
  1253 	buffer[0] = '\0';
  1254 	list_package_files(set, r, set->files.data, end, buffer);
  1255 }
  1256 
  1257 static void
  1258 razor_set_validate(struct razor_set *set, struct array *unsatisfied)
  1259 {
  1260 	struct razor_property *r, *p, *end;
  1261 	unsigned long *u;
  1262 	char *pool;
  1263 
  1264 	end = set->properties.data + set->properties.size;
  1265 	pool = set->string_pool.data;
  1266 	
  1267 	for (r = set->properties.data, p = r; r < end; r++) {
  1268 		if (r->name >> 30 != RAZOR_PROPERTY_REQUIRES)
  1269 			continue;
  1270 
  1271 		if ((r->name & RAZOR_ENTRY_MASK) != (p->name & RAZOR_ENTRY_MASK)) {
  1272 			p = r;
  1273 			while (p < end && p->name == r->name)
  1274 				p++;
  1275 		}
  1276 
  1277 		/* If there is more than one version of a provides,
  1278 		 * seek to the end for the highest version. */
  1279 		/* FIXME: This doesn't work if we have a series of
  1280 		 * requires a = 1, provides a = 1, requires a = 2,
  1281 		 * provides a = 2, as the kernel and kernel-devel
  1282 		 * does.*/
  1283 		while (p + 1 < end && p->name == (p + 1)->name)
  1284 			p++;
  1285 
  1286 		/* FIXME: We need to track property flags (<, <=, =
  1287 		 * etc) to properly determine if a requires is
  1288 		 * satisfied.  The current code doesn't track that the
  1289 		 * requires a = 1 isn't satisfied by a = 2 provides. */
  1290 
  1291 		if (p == end ||
  1292 		    (p->name >> 30) != RAZOR_PROPERTY_PROVIDES ||
  1293 		    (r->name & RAZOR_ENTRY_MASK) != (p->name & RAZOR_ENTRY_MASK) ||
  1294 		    versioncmp(&pool[r->version], &pool[p->version]) > 0) {
  1295 			/* FIXME: We ignore file requires for now. */
  1296 			if (pool[r->name & RAZOR_ENTRY_MASK] == '/')
  1297 				continue;
  1298 			u = array_add(unsatisfied, sizeof *u);
  1299 			*u = r - (struct razor_property *) set->properties.data;
  1300 		}
  1301 	}
  1302 }
  1303 
  1304 void
  1305 razor_set_list_unsatisfied(struct razor_set *set)
  1306 {
  1307 	struct array unsatisfied;
  1308 	struct razor_property *properties, *r;
  1309 	unsigned long *u, *end;
  1310 	char *pool;
  1311 
  1312 	array_init(&unsatisfied);
  1313 	razor_set_validate(set, &unsatisfied);
  1314 
  1315 	end = unsatisfied.data + unsatisfied.size;
  1316 	properties = set->properties.data;
  1317 	pool = set->string_pool.data;
  1318 
  1319 	for (u = unsatisfied.data; u < end; u++) {
  1320 		r = properties + *u;
  1321 		if (pool[r->version] == '\0')
  1322 			printf("%ss not satisfied\n",
  1323 			       &pool[r->name & RAZOR_ENTRY_MASK]);
  1324 		else
  1325 			printf("%s-%s not satisfied\n",
  1326 			       &pool[r->name & RAZOR_ENTRY_MASK],
  1327 			       &pool[r->version]);
  1328 	}
  1329 
  1330 	array_release(&unsatisfied);
  1331 }
  1332 
  1333 #define UPSTREAM_SOURCE 0x80000000ul
  1334 #define INDEX_MASK 0x00fffffful
  1335 
  1336 struct source {
  1337 	struct razor_set *set;
  1338 	unsigned long *property_map;
  1339 };
  1340 
  1341 static void
  1342 prepare_source(struct source *source, struct razor_set *set)
  1343 {
  1344 	int count;
  1345 	size_t size;
  1346 
  1347 	source->set = set;
  1348 
  1349 	count = set->properties.size / sizeof (struct razor_property);
  1350 	size = count * sizeof *source->property_map;
  1351 	source->property_map = zalloc(size);
  1352 }
  1353 
  1354 static void
  1355 add_package(struct razor_importer *importer,
  1356 	    struct razor_package *package, struct source *source,
  1357 	    unsigned long flags)
  1358 {
  1359 	char *pool;
  1360 	unsigned long *r;
  1361 	struct razor_package *p;
  1362 
  1363 	pool = source->set->string_pool.data;
  1364 	p = array_add(&importer->set->packages, sizeof *p);
  1365 	p->name = razor_importer_tokenize(importer, &pool[package->name]);
  1366 	p->name |= flags;
  1367 	p->version = razor_importer_tokenize(importer,
  1368 					     &pool[package->version]);
  1369 	p->properties = package->properties;
  1370 
  1371 	if (package->properties & RAZOR_IMMEDIATE)
  1372 		r = &package->properties;
  1373 	else
  1374 		r = (unsigned long *)
  1375 			source->set->property_pool.data + package->properties;
  1376 	while (1) {
  1377 		source->property_map[*r & RAZOR_ENTRY_MASK] = 1;
  1378 		if (*r++ & RAZOR_IMMEDIATE)
  1379 			break;
  1380 	}
  1381 }
  1382 
  1383 
  1384 /* Build the new package list sorted by merging the two package lists.
  1385  * Build new string pool as we go. (for now we just re-use that part of
  1386  * the importer). */
  1387 static void
  1388 merge_packages(struct razor_importer *importer,
  1389 	       struct source *source1, struct source *source2,
  1390 	       struct array *packages)
  1391 {
  1392 	struct razor_package *upstream_packages, *p, *s, *send;
  1393 	char *spool, *upool;
  1394 	unsigned long *u, *uend;
  1395 	int cmp;
  1396 
  1397 	upstream_packages = source2->set->packages.data;
  1398 
  1399 	u = packages->data;
  1400 	uend = packages->data + packages->size;
  1401 	upool = source2->set->string_pool.data;
  1402 
  1403 	s = source1->set->packages.data;
  1404 	send = source1->set->packages.data + source1->set->packages.size;
  1405 	spool = source1->set->string_pool.data;
  1406 
  1407 	while (s < send) {
  1408 		p = upstream_packages + *u;
  1409 
  1410 		if (u < uend)
  1411 			cmp = strcmp(&spool[s->name], &upool[p->name]);
  1412 		if (u >= uend || cmp < 0) {
  1413 			add_package(importer, s, source1, 0);
  1414 			s++;
  1415 		} else if (cmp == 0) {
  1416 			add_package(importer, p, source2, UPSTREAM_SOURCE);
  1417 			s++;
  1418 			u++;
  1419 		} else {
  1420 			add_package(importer, p, source2, UPSTREAM_SOURCE);
  1421 			u++;
  1422 		}
  1423 	}
  1424 }
  1425 
  1426 static unsigned long
  1427 add_property(struct razor_importer *importer,
  1428 	     const char *name, const char *version, int type)
  1429 {
  1430 	struct razor_property *p;
  1431 
  1432 	p = array_add(&importer->set->properties, sizeof *p);
  1433 	p->name = razor_importer_tokenize(importer, name) | (type << 30);
  1434 	p->version = razor_importer_tokenize(importer, version);
  1435 
  1436 	return p - (struct razor_property *) importer->set->properties.data;
  1437 }
  1438 
  1439 static void
  1440 merge_properties(struct razor_importer *importer,
  1441 		 struct razor_set *set1,
  1442 		 unsigned long *map1,
  1443 		 struct razor_set *set2,
  1444 		 unsigned long *map2)
  1445 {
  1446 	struct razor_property *p1, *p2;
  1447 	int i, j, cmp, count1, count2;
  1448 	char *pool1, *pool2;
  1449 
  1450 	i = 0;
  1451 	j = 0;
  1452 	pool1 = set1->string_pool.data;
  1453 	pool2 = set2->string_pool.data;
  1454 
  1455 	count1 = set1->properties.size / sizeof *p1;
  1456 	count2 = set2->properties.size / sizeof *p2;
  1457 	while (i < count1 || j < count2) {
  1458 		if (i < count1 && map1[i] == 0) {
  1459 			i++;
  1460 			continue;
  1461 		}
  1462 		if (j < count2 && map2[j] == 0) {
  1463 			j++;
  1464 			continue;
  1465 		}
  1466 		p1 = (struct razor_property *) set1->properties.data + i;
  1467 		p2 = (struct razor_property *) set2->properties.data + j;
  1468 		if (i < count1 && j < count2)
  1469 			cmp = strcmp(&pool1[p1->name & RAZOR_ENTRY_MASK],
  1470 				     &pool2[p2->name & RAZOR_ENTRY_MASK]);
  1471 		else if (i < count1)
  1472 			cmp = -1;
  1473 		else
  1474 			cmp = 1;
  1475 		if (cmp == 0)
  1476 			cmp = versioncmp(&pool1[p1->version],
  1477 					 &pool2[p2->version]);
  1478 		if (cmp < 0) {
  1479 			map1[i++] = add_property(importer,
  1480 						 &pool1[p1->name & RAZOR_ENTRY_MASK],
  1481 						 &pool1[p1->version],
  1482 						 (p1->name >> 30));
  1483 		} else if (cmp > 0) {
  1484 			map2[j++] = add_property(importer,
  1485 						 &pool2[p2->name & RAZOR_ENTRY_MASK],
  1486 						 &pool2[p2->version],
  1487 						 (p2->name >> 30));
  1488 		} else  {
  1489 			map1[i++] = map2[j++] = add_property(importer,
  1490 							     &pool1[p1->name & RAZOR_ENTRY_MASK],
  1491 							     &pool1[p1->version],
  1492 							     (p1->name >> 30));
  1493 		}
  1494 	}
  1495 }
  1496 
  1497 static unsigned long
  1498 emit_properties(struct array *source_pool, unsigned long index,
  1499 		unsigned long *map, struct array *pool)
  1500 {
  1501 	unsigned long r, *p, *q;
  1502 
  1503 	r = pool->size / sizeof *q;
  1504 	p = (unsigned long *) source_pool->data + index;
  1505 	while (1) {
  1506 		q = array_add(pool, sizeof *q);
  1507 		*q = map[*p & RAZOR_ENTRY_MASK] | (*p & ~RAZOR_ENTRY_MASK);
  1508 		if (*p++ & RAZOR_ENTRY_LAST)
  1509 			break;
  1510 	}
  1511 
  1512 	return r;
  1513 }
  1514 	
  1515 /* Rebuild property->packages maps.  We can't just remap these, as a
  1516  * property may have lost or gained a number of packages.  Allocate an
  1517  * array per property and loop through the packages and add them to
  1518  * the arrays for their properties. */
  1519 static void
  1520 rebuild_package_lists(struct razor_set *set)
  1521 {
  1522 	struct array *pkgs, *a;
  1523 	struct razor_package *pkg, *pkg_end;
  1524 	struct razor_property *prop, *prop_end;
  1525 	unsigned long *r, *q, *pool;
  1526 	int count;
  1527 
  1528 	count = set->properties.size / sizeof (struct razor_property);
  1529 	pkgs = zalloc(count * sizeof *pkgs);
  1530 	pkg_end = set->packages.data + set->packages.size;
  1531 	pool = set->property_pool.data;
  1532 
  1533 	for (pkg = set->packages.data; pkg < pkg_end; pkg++) {
  1534 		for (r = &pool[pkg->properties]; ; r++) {
  1535 			q = array_add(&pkgs[*r & RAZOR_ENTRY_MASK], sizeof *q);
  1536 			*q = pkg - (struct razor_package *) set->packages.data;
  1537 			if (*r & RAZOR_IMMEDIATE)
  1538 				break;
  1539 		}
  1540 	}
  1541 
  1542 	prop_end = set->properties.data + set->properties.size;
  1543 	a = pkgs;
  1544 	for (prop = set->properties.data; prop < prop_end; prop++, a++) {
  1545 		if (a->size / sizeof *r == 1) {
  1546 			r = a->data;
  1547 			prop->packages = *r | RAZOR_IMMEDIATE;
  1548 		} else {
  1549 			prop->packages =
  1550 				add_to_property_pool(&set->property_pool, a);
  1551 		}
  1552 		array_release(a);
  1553 	}
  1554 	free(pkgs);
  1555 }
  1556 
  1557 /* Add packages from 'upstream' to 'set'.  The packages to add are
  1558  * specified by the 'packages' array, which is a sorted list of
  1559  * package indexes.  Returns a newly allocated package set.  Does not
  1560  * enforce validity of the resulting package set.
  1561  *
  1562  * This looks more complicated than it is.  An easy way to merge two
  1563  * package sets would be to just use a razor_importer, but that
  1564  * requires resorting, and is thus O(n log n).  We can do this in a
  1565  * linear sweep, but it gets a little more complicated.
  1566  */
  1567 struct razor_set *
  1568 razor_set_add(struct razor_set *set, struct razor_set *upstream,
  1569 	      struct array *packages)
  1570 {
  1571 	struct razor_set *result;
  1572 	struct razor_importer *importer;
  1573 	struct razor_package *p, *pend;
  1574 	struct source source, upstream_source;
  1575 
  1576 	importer = razor_importer_new();
  1577 
  1578 	prepare_source(&upstream_source, upstream);
  1579 	prepare_source(&source, set);
  1580 
  1581 	merge_packages(importer, &source, &upstream_source, packages);
  1582 
  1583 	/* As we built the package list, we filled out a bitvector of
  1584 	 * the properties that are referenced by the packages in the
  1585 	 * new set.  Now we do a parallel loop through the properties
  1586 	 * and emit those marked in the bit vector to the new set.  In
  1587 	 * the process, we update the bit vector to actually map from
  1588 	 * indices in the old property list to indices in the new
  1589 	 * property list for both sets. */
  1590 
  1591 	merge_properties(importer,
  1592 			 set, source.property_map,
  1593 			 upstream, upstream_source.property_map);
  1594 
  1595 	/* Now we loop through the packages again and emit the
  1596 	 * property lists, remapped to point to the new properties. */
  1597 
  1598 	pend = importer->set->packages.data + importer->set->packages.size;
  1599 	for (p = importer->set->packages.data; p < pend; p++) {
  1600 		struct source *src;
  1601 
  1602 		if (p->name & UPSTREAM_SOURCE)
  1603 			src = &upstream_source;
  1604 		else
  1605 			src = &source;
  1606 
  1607 		p->properties = emit_properties(&src->set->property_pool,
  1608 						p->properties,
  1609 						src->property_map,
  1610 						&importer->set->property_pool);
  1611 		p->name &= INDEX_MASK;
  1612 	}
  1613 
  1614 	rebuild_package_lists(importer->set);
  1615 
  1616 	result = importer->set;
  1617 	array_release(&importer->buckets);
  1618 	free(importer);
  1619 
  1620 	return result;
  1621 }
  1622 
  1623 void
  1624 razor_set_satisfy(struct razor_set *set, struct array *unsatisfied,
  1625 		  struct razor_set *upstream, struct array *list)
  1626 {
  1627 	struct razor_property *requires, *r;
  1628 	struct razor_property *p, *pend;
  1629 	unsigned long *u, *end, *pkg, *package_pool;
  1630 	char *pool, *upool;
  1631 
  1632 	end = unsatisfied->data + unsatisfied->size;
  1633 	requires = set->properties.data;
  1634 	pool = set->string_pool.data;
  1635 
  1636 	p = upstream->properties.data;
  1637 	pend = upstream->properties.data + upstream->properties.size;
  1638 	upool = upstream->string_pool.data;
  1639 	package_pool = upstream->package_pool.data;
  1640 
  1641 	for (u = unsatisfied->data; u < end; u++) {
  1642 		r = requires + *u;
  1643 
  1644 		while (p < pend &&
  1645 		       strcmp(&pool[r->name & RAZOR_ENTRY_MASK],
  1646 			      &upool[p->name & RAZOR_ENTRY_MASK]) > 0 &&
  1647 		       (p->name >> 30) != RAZOR_PROPERTY_PROVIDES)
  1648 			p++;
  1649 		/* If there is more than one version of a provides,
  1650 		 * seek to the end for the highest version. */
  1651 		while (p + 1 < pend && p->name == (p + 1)->name)
  1652 			p++;
  1653 
  1654 		if (p == pend ||
  1655 		    strcmp(&pool[r->name & RAZOR_ENTRY_MASK],
  1656 			   &upool[p->name & RAZOR_ENTRY_MASK]) != 0 ||
  1657 		    versioncmp(&pool[r->version], &upool[p->version]) > 0) {
  1658 			/* Do we need to track unsatisfiable requires
  1659 			 * as we go, or should we just do a
  1660 			 * razor_set_validate() at the end? */
  1661 		} else {
  1662 			pkg = array_add(list, sizeof *pkg);
  1663 			/* We just pull in the first package that provides */
  1664 			if (p->packages & RAZOR_IMMEDIATE)
  1665 				*pkg = p->packages & RAZOR_ENTRY_MASK;
  1666 			else
  1667 				*pkg = package_pool[p->packages];
  1668 		}
  1669 	}	
  1670 }
  1671 
  1672 static void
  1673 find_packages(struct razor_set *set,
  1674 	      int count, const char **packages, struct array *list)
  1675 {
  1676 	struct razor_package *p;
  1677 	unsigned long *r;
  1678 	int i;
  1679 
  1680 	/* FIXME: Sort the packages. */
  1681 	for (i = 0; i < count; i++) {
  1682 		p = razor_set_get_package(set, packages[i]);
  1683 		r = array_add(list, sizeof *r);
  1684 		*r = p - (struct razor_package *) set->packages.data;
  1685 	}
  1686 }
  1687 
  1688 static void
  1689 find_all_packages(struct razor_set *set,
  1690 		  struct razor_set *upstream, struct array *list)
  1691 {
  1692 	struct razor_package *p, *u, *pend, *uend;
  1693 	unsigned long *r;
  1694 	char *pool, *upool;
  1695 
  1696 	pend = set->packages.data + set->packages.size;
  1697 	pool = set->string_pool.data;
  1698 	u = upstream->packages.data;
  1699 	uend = upstream->packages.data + upstream->packages.size;
  1700 	upool = upstream->string_pool.data;
  1701 
  1702 	for (p = set->packages.data; p < pend; p++) {
  1703 		while (u < uend && strcmp(&pool[p->name], &upool[u->name]) > 0)
  1704 			u++;
  1705 		if (strcmp(&pool[p->name], &upool[u->name]) == 0) {
  1706 			r = array_add(list, sizeof *r);
  1707 			*r = u - (struct razor_package *) upstream->packages.data;
  1708 		}
  1709 	}
  1710 }
  1711 
  1712 struct razor_set *
  1713 razor_set_update(struct razor_set *set, struct razor_set *upstream,
  1714 		 int count, const char **packages)
  1715 {
  1716 	struct razor_set *new;
  1717 	struct razor_package *upackages;
  1718 	struct array list, unsatisfied;
  1719 	char *pool;
  1720 	unsigned long *u, *end;
  1721 	int total = 0;
  1722 
  1723 	array_init(&list);
  1724 	if (count > 0)
  1725 		find_packages(upstream, count, packages, &list);
  1726 	else
  1727 		find_all_packages(set, upstream, &list);
  1728 
  1729 	end = list.data + list.size;
  1730 	upackages = upstream->packages.data;
  1731 	pool = upstream->string_pool.data;
  1732 	total += list.size / sizeof *u;
  1733 
  1734 	while (list.size > 0) {
  1735 		new = razor_set_add(set, upstream, &list);
  1736 		array_release(&list);
  1737 		razor_set_destroy(set);
  1738 		set = new;
  1739 
  1740 		array_init(&unsatisfied);
  1741 		razor_set_validate(new, &unsatisfied);
  1742 		array_init(&list);
  1743 		razor_set_satisfy(new, &unsatisfied, upstream, &list);
  1744 		array_release(&unsatisfied);
  1745 
  1746 		end = list.data + list.size;
  1747 		upackages = upstream->packages.data;
  1748 		pool = upstream->string_pool.data;
  1749 		total += list.size / sizeof *u;
  1750 	}
  1751 
  1752 	array_release(&list);
  1753 
  1754 	return set;
  1755 }
  1756 
  1757 /* The diff order matters.  We should sort the packages so that a
  1758  * REMOVE of a package comes before the INSTALL, and so that all
  1759  * requires for a package have been installed before the package.
  1760  **/
  1761 
  1762 void
  1763 razor_set_diff(struct razor_set *set, struct razor_set *upstream,
  1764 	       razor_package_callback_t callback, void *data)
  1765 {
  1766 	struct razor_package *p, *pend, *u, *uend;
  1767 	char *ppool, *upool;
  1768 	int res = 0;
  1769 
  1770 	p = set->packages.data;
  1771 	pend = set->packages.data + set->packages.size;
  1772 	ppool = set->string_pool.data;
  1773 
  1774 	u = upstream->packages.data;
  1775 	uend = upstream->packages.data + upstream->packages.size;
  1776 	upool = upstream->string_pool.data;
  1777 
  1778 	while (p < pend || u < uend) {
  1779 		if (p < pend && u < uend) {
  1780 			res = strcmp(&ppool[p->name], &upool[u->name]);
  1781 			if (res == 0)
  1782 				res = versioncmp(&ppool[p->version],
  1783 						 &upool[u->version]);
  1784 		}
  1785 
  1786 		if (u == uend || res < 0) {
  1787 			callback(&ppool[p->name], &ppool[p->version],
  1788 				 NULL, data);
  1789 			p++;
  1790 			continue;
  1791 		} else if (p == pend || res > 0) {
  1792 			callback(&upool[u->name], NULL, &upool[u->version],
  1793 				 data);
  1794 			u++;
  1795 			continue;
  1796 		} else {
  1797 			p++;
  1798 			u++;
  1799 		}
  1800 	}
  1801 }