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