razor.c
author Kristian H?gsberg <krh@redhat.com>
Thu Jan 17 23:28:37 2008 -0500 (2008-01-17)
changeset 101 9708f6d5db8c
parent 100 27aada326858
child 102 337a7a55e2c6
permissions -rw-r--r--
Add a package iterator for iterating through property owners.
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@27
   414
void
krh@30
   415
razor_importer_begin_package(struct razor_importer *importer,
krh@30
   416
			     const char *name, const char *version)
krh@13
   417
{
krh@25
   418
	struct razor_package *p;
krh@13
   419
krh@30
   420
	p = array_add(&importer->set->packages, sizeof *p);
krh@95
   421
	p->name = hashtable_tokenize(&importer->table, name);
krh@95
   422
	p->version = hashtable_tokenize(&importer->table, version);
krh@13
   423
krh@30
   424
	importer->package = p;
krh@66
   425
	array_init(&importer->properties);
krh@13
   426
}
krh@13
   427
krh@13
   428
void
krh@30
   429
razor_importer_finish_package(struct razor_importer *importer)
krh@13
   430
{
krh@25
   431
	struct razor_package *p;
krh@13
   432
krh@30
   433
	p = importer->package;
krh@66
   434
	p->properties = add_to_property_pool(&importer->set->property_pool,
krh@66
   435
					     &importer->properties);
krh@13
   436
krh@66
   437
	array_release(&importer->properties);
krh@13
   438
}
krh@13
   439
krh@66
   440
void
krh@30
   441
razor_importer_add_property(struct razor_importer *importer,
krh@66
   442
			    const char *name, const char *version,
krh@66
   443
			    enum razor_property_type type)
krh@13
   444
{
krh@25
   445
	struct razor_property *p;
krh@13
   446
	unsigned long *r;
krh@13
   447
krh@66
   448
	p = array_add(&importer->set->properties, sizeof *p);
krh@95
   449
	p->name = hashtable_tokenize(&importer->table, name) | (type << 30);
krh@95
   450
	p->version = hashtable_tokenize(&importer->table, version);
krh@30
   451
	p->packages = importer->package -
krh@30
   452
		(struct razor_package *) importer->set->packages.data;
krh@13
   453
krh@66
   454
	r = array_add(&importer->properties, sizeof *r);
krh@66
   455
	*r = p - (struct razor_property *) importer->set->properties.data;
krh@30
   456
}
krh@30
   457
krh@46
   458
void
krh@46
   459
razor_importer_add_file(struct razor_importer *importer, const char *name)
krh@46
   460
{
krh@52
   461
	struct import_entry *e;
krh@48
   462
krh@52
   463
	e = array_add(&importer->files, sizeof *e);
krh@52
   464
krh@52
   465
	e->package = importer->package -
krh@52
   466
		(struct razor_package *) importer->set->packages.data;
krh@52
   467
	e->name = strdup(name);
krh@46
   468
}
krh@46
   469
krh@30
   470
struct razor_importer *
krh@30
   471
razor_importer_new(void)
krh@30
   472
{
krh@30
   473
	struct razor_importer *importer;
krh@30
   474
krh@30
   475
	importer = zalloc(sizeof *importer);
krh@30
   476
	importer->set = razor_set_create();
krh@78
   477
	hashtable_init(&importer->table, &importer->set->string_pool);
krh@30
   478
krh@30
   479
	return importer;
krh@9
   480
}
krh@9
   481
krh@75
   482
/* Destroy an importer without creating the set. */
krh@75
   483
void
krh@75
   484
razor_importer_destroy(struct razor_importer *importer)
krh@75
   485
{
krh@75
   486
	/* FIXME: write this */
krh@75
   487
}
krh@75
   488
krh@75
   489
krh@22
   490
typedef int (*compare_with_data_func_t)(const void *p1,
krh@22
   491
					const void *p,
krh@22
   492
					void *data);
krh@22
   493
krh@25
   494
struct qsort_context {
krh@25
   495
	size_t size;
krh@25
   496
	compare_with_data_func_t compare;
krh@25
   497
	void *data;
krh@25
   498
};
krh@25
   499
krh@22
   500
static void
krh@22
   501
qsort_swap(void *p1, void *p2, size_t size)
krh@22
   502
{
krh@22
   503
	char buffer[size];
krh@22
   504
krh@22
   505
	memcpy(buffer, p1, size);
krh@22
   506
	memcpy(p1, p2, size);
krh@22
   507
	memcpy(p2, buffer, size);
krh@22
   508
}
krh@22
   509
krh@25
   510
static void
krh@25
   511
__qsort_with_data(void *base, size_t nelem, unsigned long *map,
krh@25
   512
		  struct qsort_context *ctx)
krh@22
   513
{
krh@22
   514
	void *p, *start, *end, *pivot;
krh@25
   515
	unsigned long *mp, *mstart, *mend, tmp;
krh@22
   516
	int left, right, result;
krh@25
   517
	size_t size = ctx->size;
krh@22
   518
krh@22
   519
	p = base;
krh@22
   520
	start = base;
krh@22
   521
	end = base + nelem * size;
krh@25
   522
	mp = map;
krh@25
   523
	mstart = map;
krh@25
   524
	mend = map + nelem;
krh@22
   525
	pivot = base + (random() % nelem) * size;
krh@25
   526
krh@22
   527
	while (p < end) {
krh@25
   528
		result = ctx->compare(p, pivot, ctx->data);
krh@22
   529
		if (result < 0) {
krh@22
   530
			qsort_swap(p, start, size);
krh@25
   531
			tmp = *mp;
krh@25
   532
			*mp = *mstart;
krh@25
   533
			*mstart = tmp;
krh@22
   534
			if (start == pivot)
krh@22
   535
				pivot = p;
krh@22
   536
			start += size;
krh@25
   537
			mstart++;
krh@22
   538
			p += size;
krh@29
   539
			mp++;
krh@22
   540
		} else if (result == 0) {
krh@22
   541
			p += size;
krh@25
   542
			mp++;
krh@22
   543
		} else {
krh@22
   544
 			end -= size;
krh@25
   545
			mend--;
krh@22
   546
			qsort_swap(p, end, size);
krh@25
   547
			tmp = *mp;
krh@29
   548
			*mp = *mend;
krh@29
   549
			*mend = tmp;
krh@22
   550
			if (end == pivot)
krh@22
   551
				pivot = p;
krh@22
   552
		}
krh@22
   553
	}
krh@22
   554
krh@22
   555
	left = (start - base) / size;
krh@22
   556
	right = (base + nelem * size - end) / size;
krh@22
   557
	if (left > 1)
krh@25
   558
		__qsort_with_data(base, left, map, ctx);
krh@22
   559
	if (right > 1)
krh@25
   560
		__qsort_with_data(end, right, mend, ctx);
krh@25
   561
}
krh@25
   562
krh@25
   563
unsigned long *
krh@25
   564
qsort_with_data(void *base, size_t nelem, size_t size,
krh@25
   565
		compare_with_data_func_t compare, void *data)
krh@25
   566
{
krh@25
   567
	struct qsort_context ctx;
krh@25
   568
	unsigned long *map;
krh@25
   569
	int i;
krh@25
   570
krh@75
   571
	if (nelem == 0)
krh@75
   572
		return NULL;
krh@75
   573
krh@25
   574
	ctx.size = size;
krh@25
   575
	ctx.compare = compare;
krh@25
   576
	ctx.data = data;
krh@25
   577
krh@25
   578
	map = malloc(nelem * sizeof (unsigned long));
krh@25
   579
	for (i = 0; i < nelem; i++)
krh@25
   580
		map[i] = i;
krh@25
   581
krh@25
   582
	__qsort_with_data(base, nelem, map, &ctx);
krh@25
   583
krh@25
   584
	return map;
krh@22
   585
}
krh@9
   586
krh@9
   587
static int
krh@35
   588
versioncmp(const char *s1, const char *s2)
krh@35
   589
{
krh@35
   590
	const char *p1, *p2;
krh@35
   591
	long n1, n2;
krh@35
   592
	int res;
krh@35
   593
krh@35
   594
	n1 = strtol(s1, (char **) &p1, 0);
krh@35
   595
	n2 = strtol(s2, (char **) &p2, 0);
krh@35
   596
krh@35
   597
	/* Epoch; if one but not the other has an epoch set, default
krh@35
   598
	 * the epoch-less version to 0. */
krh@35
   599
	res = (*p1 == ':') - (*p2 == ':');
krh@35
   600
	if (res < 0) {
krh@35
   601
		n1 = 0;
krh@35
   602
		p1 = s1;
krh@35
   603
		p2++;
krh@35
   604
	} else if (res > 0) {
krh@35
   605
		p1++;
krh@35
   606
		n2 = 0;
krh@35
   607
		p2 = s2;
krh@35
   608
	}
krh@35
   609
krh@35
   610
	if (n1 != n2)
krh@35
   611
		return n1 - n2;
krh@35
   612
	while (*p1 && *p2) {
krh@35
   613
		if (*p1 != *p2)
krh@35
   614
			return *p1 - *p2;
krh@35
   615
		p1++;
krh@35
   616
		p2++;
krh@35
   617
		if (isdigit(*p1) && isdigit(*p2))
krh@35
   618
			return versioncmp(p1, p2);
krh@35
   619
	}
krh@35
   620
krh@35
   621
	return *p1 - *p2;
krh@35
   622
}
krh@35
   623
krh@35
   624
static int
krh@22
   625
compare_packages(const void *p1, const void *p2, void *data)
krh@9
   626
{
krh@25
   627
	const struct razor_package *pkg1 = p1, *pkg2 = p2;
krh@22
   628
	struct razor_set *set = data;
krh@22
   629
	char *pool = set->string_pool.data;
krh@9
   630
krh@23
   631
	if (pkg1->name == pkg2->name)
krh@35
   632
		return versioncmp(&pool[pkg1->version], &pool[pkg2->version]);
krh@23
   633
	else
krh@23
   634
		return strcmp(&pool[pkg1->name], &pool[pkg2->name]);
krh@9
   635
}
krh@9
   636
krh@9
   637
static int
krh@22
   638
compare_properties(const void *p1, const void *p2, void *data)
krh@9
   639
{
krh@25
   640
	const struct razor_property *prop1 = p1, *prop2 = p2;
krh@22
   641
	struct razor_set *set = data;
krh@22
   642
	char *pool = set->string_pool.data;
krh@9
   643
krh@23
   644
	if (prop1->name == prop2->name)
krh@66
   645
		return versioncmp(&pool[prop1->version],
krh@66
   646
				  &pool[prop2->version]);
krh@66
   647
	else if ((prop1->name & RAZOR_ENTRY_MASK) == (prop2->name & RAZOR_ENTRY_MASK))
krh@66
   648
		return (prop1->name >> 30) - (prop2->name >> 30);
krh@12
   649
	else
krh@66
   650
		return strcmp(&pool[prop1->name & RAZOR_ENTRY_MASK],
krh@66
   651
			      &pool[prop2->name & RAZOR_ENTRY_MASK]);
krh@9
   652
}
krh@9
   653
krh@10
   654
static unsigned long *
krh@66
   655
uniqueify_properties(struct razor_set *set)
krh@9
   656
{
krh@25
   657
	struct razor_property *rp, *up, *rp_end;
krh@18
   658
	struct array *pkgs, *p;
krh@25
   659
	unsigned long *map, *rmap, *r;
krh@18
   660
	int i, count, unique;
krh@9
   661
krh@66
   662
	count = set->properties.size / sizeof(struct razor_property);
krh@66
   663
	map = qsort_with_data(set->properties.data,
krh@25
   664
			      count,
krh@25
   665
			      sizeof(struct razor_property),
krh@25
   666
			      compare_properties,
krh@25
   667
			      set);
krh@9
   668
krh@66
   669
	rp_end = set->properties.data + set->properties.size;
krh@25
   670
	rmap = malloc(count * sizeof *map);
krh@25
   671
	pkgs = zalloc(count * sizeof *pkgs);
krh@66
   672
	for (rp = set->properties.data, up = rp, i = 0; rp < rp_end; rp++, i++) {
krh@25
   673
		if (rp->name != up->name || rp->version != up->version) {
krh@25
   674
			up++;
krh@25
   675
			up->name = rp->name;
krh@25
   676
			up->version = rp->version;
krh@10
   677
		}
krh@25
   678
krh@66
   679
		unique = up - (struct razor_property *) set->properties.data;
krh@25
   680
		rmap[map[i]] = unique;
krh@25
   681
		r = array_add(&pkgs[unique], sizeof *r);
krh@25
   682
		*r = rp->packages;
krh@10
   683
	}
krh@25
   684
	free(map);
krh@9
   685
krh@25
   686
	up++;
krh@66
   687
	set->properties.size = (void *) up - set->properties.data;
krh@25
   688
	rp_end = up;
krh@66
   689
	for (rp = set->properties.data, p = pkgs; rp < rp_end; rp++, p++) {
krh@61
   690
		if (p->size / sizeof *r == 1) {
krh@61
   691
			r = p->data;
krh@61
   692
			rp->packages = *r | RAZOR_IMMEDIATE;
krh@61
   693
		} else {
krh@61
   694
			rp->packages =
krh@61
   695
				add_to_property_pool(&set->package_pool, p);
krh@61
   696
		}
krh@25
   697
		array_release(p);
krh@18
   698
	}
krh@18
   699
krh@18
   700
	free(pkgs);
krh@18
   701
krh@25
   702
	return rmap;
krh@10
   703
}
krh@10
   704
krh@10
   705
static void
krh@39
   706
remap_links(struct array *links, unsigned long *map)
krh@10
   707
{
krh@39
   708
	unsigned long *p, *end;
krh@10
   709
krh@39
   710
	end = links->data + links->size;
krh@39
   711
	for (p = links->data; p < end; p++)
krh@64
   712
		*p = map[*p & RAZOR_ENTRY_MASK] | (*p & ~RAZOR_ENTRY_MASK);
krh@9
   713
}
krh@9
   714
krh@48
   715
static int
krh@48
   716
compare_filenames(const void *p1, const void *p2, void *data)
krh@48
   717
{
krh@52
   718
	const struct import_entry *e1 = p1;
krh@52
   719
	const struct import_entry *e2 = p2;
krh@48
   720
krh@52
   721
	return strcmp(e1->name, e2->name);
krh@48
   722
}
krh@48
   723
krh@48
   724
static void
krh@52
   725
count_entries(struct import_directory *d)
krh@48
   726
{
krh@52
   727
	struct import_directory *p, *end;
krh@48
   728
krh@48
   729
	p = d->files.data;
krh@48
   730
	end = d->files.data + d->files.size;
krh@48
   731
	d->count = 0;
krh@48
   732
	while (p < end) {
krh@48
   733
		count_entries(p);
krh@48
   734
		d->count += p->count + 1;
krh@48
   735
		p++;
krh@48
   736
	}		
krh@48
   737
}
krh@48
   738
krh@48
   739
static void
krh@52
   740
serialize_files(struct razor_set *set,
krh@52
   741
		struct import_directory *d, struct array *array)
krh@48
   742
{
krh@52
   743
	struct import_directory *p, *end;
krh@56
   744
	struct razor_entry *e = NULL;
krh@60
   745
	unsigned long s, *r;
krh@48
   746
krh@48
   747
	p = d->files.data;
krh@48
   748
	end = d->files.data + d->files.size;
krh@48
   749
	s = array->size / sizeof *e + d->files.size / sizeof *p;
krh@48
   750
	while (p < end) {
krh@48
   751
		e = array_add(array, sizeof *e);
krh@48
   752
		e->name = p->name;
krh@56
   753
		e->start = p->count > 0 ? s : 0;
krh@48
   754
		s += p->count;
krh@60
   755
krh@64
   756
		if (p->packages.size == 0) {
krh@92
   757
			e->packages = ~0;
krh@64
   758
		} else if (p->packages.size / sizeof *r == 1) {
krh@60
   759
			r = p->packages.data;
krh@60
   760
			e->packages = *r | RAZOR_IMMEDIATE;
krh@60
   761
		} else {
krh@60
   762
			e->packages = add_to_property_pool(&set->package_pool,
krh@60
   763
							   &p->packages);
krh@60
   764
		}
krh@52
   765
		array_release(&p->packages);
krh@48
   766
		p++;
krh@48
   767
	}		
krh@56
   768
	if (e != NULL)
krh@56
   769
		e->name |= RAZOR_ENTRY_LAST;
krh@48
   770
krh@48
   771
	p = d->files.data;
krh@48
   772
	end = d->files.data + d->files.size;
krh@48
   773
	while (p < end) {
krh@52
   774
		serialize_files(set, p, array);
krh@48
   775
		p++;
krh@48
   776
	}
krh@48
   777
}
krh@48
   778
krh@48
   779
static void
krh@61
   780
remap_property_package_links(struct array *properties, unsigned long *rmap)
krh@61
   781
{
krh@61
   782
	struct razor_property *p, *end;
krh@61
   783
krh@61
   784
	end = properties->data + properties->size;
krh@61
   785
	for (p = properties->data; p < end; p++)
krh@61
   786
		if (p->packages & RAZOR_IMMEDIATE)
krh@61
   787
			p->packages = rmap[p->packages & RAZOR_ENTRY_MASK] |
krh@61
   788
				RAZOR_IMMEDIATE;
krh@61
   789
}
krh@61
   790
krh@61
   791
static void
krh@48
   792
build_file_tree(struct razor_importer *importer)
krh@48
   793
{
krh@48
   794
	int count, i, length;
krh@52
   795
	struct import_entry *filenames;
krh@52
   796
	char *f, *end;
krh@52
   797
	unsigned long name, *r;
krh@48
   798
	char dirname[256];
krh@52
   799
	struct import_directory *d, root;
krh@48
   800
	struct razor_entry *e;
krh@48
   801
krh@52
   802
	count = importer->files.size / sizeof (struct import_entry);
krh@48
   803
	qsort_with_data(importer->files.data,
krh@48
   804
			count,
krh@52
   805
			sizeof (struct import_entry),
krh@48
   806
			compare_filenames,
krh@48
   807
			NULL);
krh@48
   808
krh@95
   809
	root.name = hashtable_tokenize(&importer->table, "");
krh@48
   810
	array_init(&root.files);
krh@52
   811
	array_init(&root.packages);
krh@48
   812
	root.last = NULL;
krh@48
   813
krh@48
   814
	filenames = importer->files.data;
krh@48
   815
	for (i = 0; i < count; i++) {
krh@52
   816
		f = filenames[i].name;
krh@48
   817
		if (*f != '/')
krh@48
   818
			continue;
krh@57
   819
		f++;
krh@48
   820
krh@48
   821
		d = &root;
krh@48
   822
		while (*f) {
krh@57
   823
			end = strchr(f, '/');
krh@48
   824
			if (end == NULL)
krh@48
   825
				end = f + strlen(f);
krh@57
   826
			length = end - f;
krh@57
   827
			memcpy(dirname, f, length);
krh@48
   828
			dirname[length] ='\0';
krh@95
   829
			name = hashtable_tokenize(&importer->table, dirname);
krh@48
   830
			if (d->last == NULL || d->last->name != name) {
krh@48
   831
				d->last = array_add(&d->files, sizeof *d);
krh@48
   832
				d->last->name = name;
krh@48
   833
				d->last->last = NULL;
krh@48
   834
				array_init(&d->last->files);
krh@52
   835
				array_init(&d->last->packages);
krh@48
   836
			}
krh@48
   837
			d = d->last;				
krh@57
   838
			f = end + 1;
krh@57
   839
			if (*end == '\0')
krh@57
   840
				break;
krh@48
   841
		}
krh@48
   842
krh@52
   843
		r = array_add(&d->packages, sizeof *r);
krh@52
   844
		*r = filenames[i].package;
krh@52
   845
		free(filenames[i].name);
krh@48
   846
	}
krh@48
   847
krh@48
   848
	count_entries(&root);
krh@56
   849
	array_init(&importer->set->files);
krh@48
   850
krh@56
   851
	e = array_add(&importer->set->files, sizeof *e);
krh@56
   852
	e->name = root.name | RAZOR_ENTRY_LAST;
krh@48
   853
	e->start = 1;
krh@92
   854
	e->packages = ~0;
krh@48
   855
krh@56
   856
	serialize_files(importer->set, &root, &importer->set->files);
krh@48
   857
krh@48
   858
	array_release(&importer->files);
krh@48
   859
}
krh@48
   860
krh@56
   861
static void
krh@60
   862
build_package_file_lists(struct razor_set *set, unsigned long *rmap)
krh@54
   863
{
krh@56
   864
	struct razor_package *p, *packages;
krh@56
   865
	struct array *pkgs;
krh@54
   866
	struct razor_entry *e, *end;
krh@56
   867
	unsigned long *r, *q;
krh@56
   868
	int i, count;
krh@54
   869
krh@56
   870
	count = set->packages.size / sizeof *p;
krh@56
   871
	pkgs = zalloc(count * sizeof *pkgs);
krh@54
   872
krh@56
   873
	end = set->files.data + set->files.size;
krh@92
   874
	for (e = set->files.data; e < end; e++) {
krh@92
   875
		if (e->packages == ~0) {
krh@92
   876
			continue;
krh@92
   877
		} else if (e->packages & RAZOR_IMMEDIATE) {
krh@60
   878
			e->packages = rmap[e->packages & RAZOR_ENTRY_MASK] |
krh@60
   879
				RAZOR_IMMEDIATE;
krh@60
   880
			r = &e->packages;
krh@60
   881
		} else {
krh@60
   882
			r = (unsigned long *) set->package_pool.data + e->packages;
krh@60
   883
		}
krh@60
   884
krh@64
   885
		while (1) {
krh@60
   886
			q = array_add(&pkgs[*r & RAZOR_ENTRY_MASK], sizeof *q);
krh@56
   887
			*q = e - (struct razor_entry *) set->files.data;
krh@60
   888
			if (*r++ & RAZOR_IMMEDIATE)
krh@60
   889
				break;
krh@54
   890
		}
krh@54
   891
	}
krh@54
   892
krh@56
   893
	packages = set->packages.data;
krh@56
   894
	for (i = 0; i < count; i++) {
krh@56
   895
		packages[i].files =
krh@56
   896
			add_to_property_pool(&set->file_pool, &pkgs[i]);
krh@56
   897
		array_release(&pkgs[i]);
krh@48
   898
	}
krh@56
   899
	free(pkgs);
krh@52
   900
}
krh@52
   901
krh@27
   902
struct razor_set *
krh@30
   903
razor_importer_finish(struct razor_importer *importer)
krh@9
   904
{
krh@30
   905
	struct razor_set *set;
krh@39
   906
	unsigned long *map, *rmap;
krh@39
   907
	int i, count;
krh@18
   908
krh@66
   909
	map = uniqueify_properties(importer->set);
krh@66
   910
	remap_links(&importer->set->property_pool, map);
krh@39
   911
	free(map);
krh@25
   912
krh@30
   913
	count = importer->set->packages.size / sizeof(struct razor_package);
krh@30
   914
	map = qsort_with_data(importer->set->packages.data,
krh@25
   915
			      count,
krh@25
   916
			      sizeof(struct razor_package),
krh@25
   917
			      compare_packages,
krh@30
   918
			      importer->set);
krh@39
   919
krh@39
   920
	rmap = malloc(count * sizeof *rmap);
krh@39
   921
	for (i = 0; i < count; i++)
krh@39
   922
		rmap[map[i]] = i;
krh@25
   923
	free(map);
krh@13
   924
krh@48
   925
	build_file_tree(importer);
krh@52
   926
	remap_links(&importer->set->package_pool, rmap);
krh@60
   927
	build_package_file_lists(importer->set, rmap);
krh@66
   928
	remap_property_package_links(&importer->set->properties, rmap);
krh@52
   929
	free(rmap);
krh@48
   930
krh@30
   931
	set = importer->set;
krh@78
   932
	hashtable_release(&importer->table);
krh@30
   933
	free(importer);
krh@30
   934
krh@30
   935
	return set;
krh@9
   936
}
krh@9
   937
krh@92
   938
struct razor_package_iterator {
krh@92
   939
	struct razor_set *set;
krh@92
   940
	struct razor_package *package, *end;
krh@101
   941
	unsigned long *index;
krh@101
   942
	int last;
krh@92
   943
};
krh@92
   944
krh@92
   945
struct razor_package_iterator *
krh@101
   946
razor_package_iterator_create_with_index(struct razor_set *set,
krh@101
   947
					 unsigned long *index)
krh@3
   948
{
krh@92
   949
	struct razor_package_iterator *pi;
krh@92
   950
krh@92
   951
	pi = zalloc(sizeof *pi);
krh@92
   952
	pi->set = set;
krh@101
   953
	pi->end = set->packages.data + set->packages.size;
krh@92
   954
	pi->package = set->packages.data;
krh@101
   955
	pi->index = index;
krh@92
   956
krh@92
   957
	return pi;
krh@92
   958
}
krh@92
   959
krh@101
   960
struct razor_package_iterator *
krh@101
   961
razor_package_iterator_create(struct razor_set *set)
krh@101
   962
{
krh@101
   963
	return razor_package_iterator_create_with_index(set, NULL);
krh@101
   964
}
krh@101
   965
krh@101
   966
struct razor_package_iterator *
krh@101
   967
razor_package_iterator_create_for_property(struct razor_set *set,
krh@101
   968
					   struct razor_property *property)
krh@101
   969
{
krh@101
   970
	unsigned long *index;
krh@101
   971
krh@101
   972
	if (property->packages & RAZOR_IMMEDIATE)
krh@101
   973
		index = &property->packages;
krh@101
   974
	else
krh@101
   975
		index = (unsigned long *)
krh@101
   976
			set->package_pool.data + property->packages;
krh@101
   977
krh@101
   978
	return razor_package_iterator_create_with_index(set, index);
krh@101
   979
}
krh@101
   980
krh@92
   981
int
krh@92
   982
razor_package_iterator_next(struct razor_package_iterator *pi,
krh@92
   983
			    struct razor_package **package,
krh@92
   984
			    const char **name, const char **version)
krh@92
   985
{
krh@6
   986
	char *pool;
krh@101
   987
	int valid;
krh@101
   988
	struct razor_package *p, *packages;
krh@3
   989
krh@101
   990
	if (pi->index) {
krh@101
   991
		packages = pi->set->packages.data;
krh@101
   992
		p = &packages[*pi->index & RAZOR_ENTRY_MASK];
krh@101
   993
		valid = !pi->last;
krh@101
   994
		pi->last = (*pi->index++ & RAZOR_IMMEDIATE) != 0;
krh@101
   995
	} else {
krh@101
   996
		p = pi->package++;
krh@101
   997
		valid = p < pi->end;
krh@101
   998
	}			
krh@92
   999
krh@101
  1000
	if (valid) {
krh@101
  1001
		pool = pi->set->string_pool.data;
krh@101
  1002
		*package = p;
krh@101
  1003
		*name = &pool[p->name & RAZOR_ENTRY_MASK];
krh@101
  1004
		*version = &pool[p->version];
krh@101
  1005
	} else {
krh@101
  1006
		*package = NULL;
krh@101
  1007
	}
krh@101
  1008
krh@101
  1009
	return valid;
krh@92
  1010
}
krh@92
  1011
krh@92
  1012
void
krh@92
  1013
razor_package_iterator_destroy(struct razor_package_iterator *pi)
krh@92
  1014
{
krh@92
  1015
	free(pi);
krh@3
  1016
}
krh@3
  1017
krh@10
  1018
struct razor_package *
krh@10
  1019
razor_set_get_package(struct razor_set *set, const char *package)
krh@10
  1020
{
krh@97
  1021
	struct razor_package_iterator *pi;
krh@97
  1022
	struct razor_package *p;
krh@97
  1023
	const char *name, *version;
krh@97
  1024
krh@97
  1025
	pi = razor_package_iterator_create(set);
krh@97
  1026
	while (razor_package_iterator_next(pi, &p, &name, &version)) {
krh@97
  1027
		if (strcmp(package, name) == 0)
krh@97
  1028
			break;
krh@97
  1029
	}
krh@97
  1030
	razor_package_iterator_destroy(pi);
krh@97
  1031
krh@97
  1032
	return p;
krh@10
  1033
}
krh@10
  1034
krh@92
  1035
struct razor_property_iterator {
krh@92
  1036
	struct razor_set *set;
krh@92
  1037
	struct razor_property *property, *end;
krh@92
  1038
	unsigned long *index;
krh@92
  1039
	int last;
krh@92
  1040
};
krh@92
  1041
krh@92
  1042
struct razor_property_iterator *
krh@92
  1043
razor_property_iterator_create(struct razor_set *set,
krh@92
  1044
			       struct razor_package *package)
krh@92
  1045
{
krh@92
  1046
	struct razor_property_iterator *pi;
krh@92
  1047
krh@92
  1048
	pi = zalloc(sizeof *pi);
krh@92
  1049
	pi->set = set;
krh@92
  1050
	pi->end = set->properties.data + set->properties.size;
krh@101
  1051
	pi->property = set->properties.data;
krh@98
  1052
krh@98
  1053
	if (package)
krh@92
  1054
		pi->index = (unsigned long *)
krh@92
  1055
			set->property_pool.data + package->properties;
krh@92
  1056
krh@92
  1057
	return pi;
krh@92
  1058
}
krh@92
  1059
krh@92
  1060
int
krh@92
  1061
razor_property_iterator_next(struct razor_property_iterator *pi,
krh@92
  1062
			     struct razor_property **property,
krh@92
  1063
			     const char **name, const char **version,
krh@92
  1064
			     enum razor_property_type *type)
krh@92
  1065
{
krh@92
  1066
	char *pool;
krh@92
  1067
	int valid;
krh@92
  1068
	struct razor_property *p, *properties;
krh@92
  1069
krh@92
  1070
	if (pi->index) {
krh@92
  1071
		properties = pi->set->properties.data;
krh@92
  1072
		p = &properties[*pi->index & RAZOR_ENTRY_MASK];
krh@92
  1073
		valid = !pi->last;
krh@92
  1074
		pi->last = (*pi->index++ & RAZOR_IMMEDIATE) != 0;
krh@92
  1075
	} else {
krh@92
  1076
		p = pi->property++;
krh@92
  1077
		valid = p < pi->end;
krh@92
  1078
	}			
krh@92
  1079
krh@96
  1080
	if (valid) {
krh@96
  1081
		pool = pi->set->string_pool.data;
krh@96
  1082
		*property = p;
krh@96
  1083
		*name = &pool[p->name & RAZOR_ENTRY_MASK];
krh@96
  1084
		*version = &pool[p->version];
krh@96
  1085
		*type = p->name >> 30;
krh@96
  1086
	} else {
krh@96
  1087
		*property = NULL;
krh@96
  1088
	}
krh@92
  1089
krh@92
  1090
	return valid;
krh@92
  1091
}
krh@92
  1092
krh@66
  1093
void
krh@92
  1094
razor_property_iterator_destroy(struct razor_property_iterator *pi)
krh@8
  1095
{
krh@92
  1096
	free(pi);
krh@10
  1097
}
krh@10
  1098
krh@56
  1099
static struct razor_entry *
krh@56
  1100
find_entry(struct razor_set *set, struct razor_entry *dir, const char *pattern)
krh@56
  1101
{
krh@56
  1102
	struct razor_entry *e;
krh@56
  1103
	const char *n, *pool = set->string_pool.data;
krh@56
  1104
	int len;
krh@56
  1105
krh@56
  1106
	e = (struct razor_entry *) set->files.data + dir->start;
krh@56
  1107
	do {
krh@56
  1108
		n = pool + (e->name & RAZOR_ENTRY_MASK);
krh@56
  1109
		if (strcmp(pattern + 1, n) == 0)
krh@56
  1110
			return e;
krh@57
  1111
		len = strlen(n);
krh@56
  1112
		if (e->start != 0 && strncmp(pattern + 1, n, len) == 0 &&
krh@56
  1113
		    pattern[len + 1] == '/') {
krh@56
  1114
			return find_entry(set, e, pattern + len + 1);
krh@56
  1115
		}
krh@56
  1116
	} while (((e++)->name & RAZOR_ENTRY_LAST) == 0);
krh@56
  1117
krh@56
  1118
	return NULL;
krh@56
  1119
}
krh@56
  1120
krh@56
  1121
static void
krh@56
  1122
list_dir(struct razor_set *set, struct razor_entry *dir,
krh@56
  1123
	 const char *prefix, const char *pattern)
krh@56
  1124
{
krh@56
  1125
	struct razor_entry *e;
krh@56
  1126
	const char *n, *pool = set->string_pool.data;
krh@56
  1127
krh@56
  1128
	e = (struct razor_entry *) set->files.data + dir->start;
krh@56
  1129
	do {
krh@56
  1130
		n = pool + (e->name & RAZOR_ENTRY_MASK);
krh@56
  1131
		if (pattern && pattern[0] && fnmatch(pattern, n, 0) != 0)
krh@56
  1132
			continue;
krh@56
  1133
		printf("%s/%s%s\n", prefix, n, e->start > 0 ? "/" : "");
krh@56
  1134
	} while (((e++)->name & RAZOR_ENTRY_LAST) == 0);
krh@56
  1135
}
krh@56
  1136
krh@56
  1137
void
krh@56
  1138
razor_set_list_files(struct razor_set *set, const char *pattern)
krh@56
  1139
{
krh@56
  1140
	struct razor_entry *e;
krh@56
  1141
	char buffer[512], *p, *base;
krh@56
  1142
krh@56
  1143
	if (pattern == NULL)
krh@56
  1144
		pattern = "/";
krh@56
  1145
krh@56
  1146
	strcpy(buffer, pattern);
krh@56
  1147
	e = find_entry(set, set->files.data, buffer);
krh@56
  1148
	if (e && e->start > 0) {
krh@56
  1149
		base = NULL;
krh@56
  1150
	} else {
krh@56
  1151
		p = strrchr(buffer, '/');
krh@56
  1152
		if (p) {
krh@56
  1153
			*p = '\0';
krh@56
  1154
			base = p + 1;
krh@56
  1155
		} else {
krh@56
  1156
			base = NULL;
krh@56
  1157
		}
krh@56
  1158
	}
krh@56
  1159
	e = find_entry(set, set->files.data, buffer);
krh@56
  1160
	if (e->start != 0)
krh@56
  1161
		list_dir(set, e, buffer, base);
krh@56
  1162
}
krh@56
  1163
krh@56
  1164
void
krh@56
  1165
razor_set_list_file_packages(struct razor_set *set, const char *filename)
krh@56
  1166
{
krh@56
  1167
	struct razor_entry *e;
krh@56
  1168
	struct razor_package *packages, *p;
krh@56
  1169
	const char *pool;
krh@56
  1170
	unsigned long *r;
krh@56
  1171
krh@56
  1172
	e = find_entry(set, set->files.data, filename);
krh@56
  1173
	if (e == NULL)
krh@56
  1174
		return;
krh@56
  1175
	
krh@60
  1176
	if (e->packages & RAZOR_IMMEDIATE)
krh@60
  1177
		r = &e->packages;
krh@60
  1178
	else
krh@60
  1179
		r = (unsigned long *) set->package_pool.data + e->packages;
krh@60
  1180
krh@56
  1181
	packages = set->packages.data;
krh@56
  1182
	pool = set->string_pool.data;
krh@64
  1183
	while (1) {
krh@60
  1184
		p = &packages[*r & RAZOR_ENTRY_MASK];
krh@59
  1185
		printf("%s-%s\n", &pool[p->name], &pool[p->version]);
krh@60
  1186
		if (*r++ & RAZOR_IMMEDIATE)
krh@60
  1187
			break;
krh@56
  1188
	}
krh@56
  1189
}
krh@56
  1190
krh@56
  1191
static unsigned long *
krh@56
  1192
list_package_files(struct razor_set *set, unsigned long *r,
krh@56
  1193
		   struct razor_entry *dir, unsigned long end,
krh@56
  1194
		   char *prefix)
krh@56
  1195
{
krh@56
  1196
	struct razor_entry *e, *f, *entries;
krh@83
  1197
	unsigned long next, file;
krh@56
  1198
	char *pool;
krh@56
  1199
	int len;
krh@56
  1200
	
krh@56
  1201
	entries = (struct razor_entry *) set->files.data;
krh@56
  1202
	pool = set->string_pool.data;
krh@56
  1203
krh@56
  1204
	e = entries + dir->start;
krh@56
  1205
	do {
krh@83
  1206
		if (entries + (*r & RAZOR_ENTRY_MASK) == e) {
krh@56
  1207
			printf("%s/%s\n", prefix,
krh@56
  1208
			       pool + (e->name & RAZOR_ENTRY_MASK));
krh@83
  1209
			if (*r & RAZOR_ENTRY_LAST)
krh@83
  1210
				return NULL;
krh@56
  1211
			r++;
krh@83
  1212
			if ((*r & RAZOR_ENTRY_MASK) >= end)
krh@83
  1213
				return r;
krh@56
  1214
		}
krh@83
  1215
	} while (!((e++)->name & RAZOR_ENTRY_LAST));
krh@56
  1216
krh@56
  1217
	e = entries + dir->start;
krh@56
  1218
	do {
krh@56
  1219
		if (e->start == 0)
krh@56
  1220
			continue;
krh@56
  1221
krh@56
  1222
		if (e->name & RAZOR_ENTRY_LAST)
krh@56
  1223
			next = end;
krh@56
  1224
		else {
krh@56
  1225
			f = e + 1; 
krh@56
  1226
			while (f->start == 0 && !(f->name & RAZOR_ENTRY_LAST))
krh@56
  1227
				f++;
krh@56
  1228
			if (f->start == 0)
krh@56
  1229
				next = end;
krh@56
  1230
			else
krh@56
  1231
				next = f->start;
krh@56
  1232
		}
krh@56
  1233
krh@83
  1234
		file = *r & RAZOR_ENTRY_MASK;
krh@83
  1235
		if (e->start <= file && file < next) {
krh@56
  1236
			len = strlen(prefix);
krh@56
  1237
			prefix[len] = '/';
krh@56
  1238
			strcpy(prefix + len + 1,
krh@56
  1239
			       pool + (e->name & RAZOR_ENTRY_MASK));
krh@56
  1240
			r = list_package_files(set, r, e, next, prefix);
krh@56
  1241
			prefix[len] = '\0';
krh@56
  1242
		}
krh@83
  1243
	} while (!((e++)->name & RAZOR_ENTRY_LAST) && r != NULL);
krh@56
  1244
krh@56
  1245
	return r;
krh@56
  1246
}
krh@56
  1247
krh@56
  1248
void
krh@56
  1249
razor_set_list_package_files(struct razor_set *set, const char *name)
krh@56
  1250
{
krh@56
  1251
	struct razor_package *package;
krh@56
  1252
	unsigned long *r, end;
krh@56
  1253
	char buffer[512];
krh@56
  1254
krh@56
  1255
	package = razor_set_get_package(set, name);
krh@56
  1256
krh@56
  1257
	r = (unsigned long *) set->file_pool.data + package->files;
krh@56
  1258
	end = set->files.size / sizeof (struct razor_entry);
krh@56
  1259
	buffer[0] = '\0';
krh@56
  1260
	list_package_files(set, r, set->files.data, end, buffer);
krh@56
  1261
}
krh@56
  1262
krh@43
  1263
static void
krh@21
  1264
razor_set_validate(struct razor_set *set, struct array *unsatisfied)
krh@21
  1265
{
krh@66
  1266
	struct razor_property *r, *p, *end;
krh@21
  1267
	unsigned long *u;
krh@21
  1268
	char *pool;
krh@21
  1269
krh@66
  1270
	end = set->properties.data + set->properties.size;
krh@21
  1271
	pool = set->string_pool.data;
krh@21
  1272
	
krh@66
  1273
	for (r = set->properties.data, p = r; r < end; r++) {
krh@66
  1274
		if (r->name >> 30 != RAZOR_PROPERTY_REQUIRES)
krh@66
  1275
			continue;
krh@66
  1276
krh@66
  1277
		if ((r->name & RAZOR_ENTRY_MASK) != (p->name & RAZOR_ENTRY_MASK)) {
krh@66
  1278
			p = r;
krh@66
  1279
			while (p < end && p->name == r->name)
krh@66
  1280
				p++;
krh@66
  1281
		}
krh@37
  1282
krh@35
  1283
		/* If there is more than one version of a provides,
krh@35
  1284
		 * seek to the end for the highest version. */
krh@66
  1285
		/* FIXME: This doesn't work if we have a series of
krh@66
  1286
		 * requires a = 1, provides a = 1, requires a = 2,
krh@66
  1287
		 * provides a = 2, as the kernel and kernel-devel
krh@66
  1288
		 * does.*/
krh@66
  1289
		while (p + 1 < end && p->name == (p + 1)->name)
krh@35
  1290
			p++;
krh@37
  1291
krh@37
  1292
		/* FIXME: We need to track property flags (<, <=, =
krh@37
  1293
		 * etc) to properly determine if a requires is
krh@37
  1294
		 * satisfied.  The current code doesn't track that the
krh@37
  1295
		 * requires a = 1 isn't satisfied by a = 2 provides. */
krh@37
  1296
krh@66
  1297
		if (p == end ||
krh@66
  1298
		    (p->name >> 30) != RAZOR_PROPERTY_PROVIDES ||
krh@66
  1299
		    (r->name & RAZOR_ENTRY_MASK) != (p->name & RAZOR_ENTRY_MASK) ||
krh@35
  1300
		    versioncmp(&pool[r->version], &pool[p->version]) > 0) {
krh@35
  1301
			/* FIXME: We ignore file requires for now. */
krh@66
  1302
			if (pool[r->name & RAZOR_ENTRY_MASK] == '/')
krh@35
  1303
				continue;
krh@21
  1304
			u = array_add(unsatisfied, sizeof *u);
krh@66
  1305
			*u = r - (struct razor_property *) set->properties.data;
krh@21
  1306
		}
krh@21
  1307
	}
krh@21
  1308
}
krh@21
  1309
krh@21
  1310
void
krh@21
  1311
razor_set_list_unsatisfied(struct razor_set *set)
krh@21
  1312
{
krh@21
  1313
	struct array unsatisfied;
krh@66
  1314
	struct razor_property *properties, *r;
krh@21
  1315
	unsigned long *u, *end;
krh@21
  1316
	char *pool;
krh@21
  1317
krh@21
  1318
	array_init(&unsatisfied);
krh@21
  1319
	razor_set_validate(set, &unsatisfied);
krh@21
  1320
krh@21
  1321
	end = unsatisfied.data + unsatisfied.size;
krh@66
  1322
	properties = set->properties.data;
krh@21
  1323
	pool = set->string_pool.data;
krh@21
  1324
krh@21
  1325
	for (u = unsatisfied.data; u < end; u++) {
krh@66
  1326
		r = properties + *u;
krh@66
  1327
		if (pool[r->version] == '\0')
krh@66
  1328
			printf("%ss not satisfied\n",
krh@66
  1329
			       &pool[r->name & RAZOR_ENTRY_MASK]);
krh@66
  1330
		else
krh@66
  1331
			printf("%s-%s not satisfied\n",
krh@66
  1332
			       &pool[r->name & RAZOR_ENTRY_MASK],
krh@66
  1333
			       &pool[r->version]);
krh@21
  1334
	}
krh@21
  1335
krh@21
  1336
	array_release(&unsatisfied);
krh@21
  1337
}
krh@21
  1338
krh@41
  1339
#define UPSTREAM_SOURCE 0x80000000ul
krh@41
  1340
#define INDEX_MASK 0x00fffffful
krh@41
  1341
krh@41
  1342
struct source {
krh@41
  1343
	struct razor_set *set;
krh@66
  1344
	unsigned long *property_map;
krh@41
  1345
};
krh@41
  1346
krh@79
  1347
struct razor_merger {
krh@79
  1348
	struct razor_set *set;
krh@79
  1349
	struct hashtable table;
krh@79
  1350
	struct source source1;
krh@79
  1351
	struct source source2;
krh@79
  1352
};
krh@79
  1353
krh@79
  1354
static struct razor_merger *
krh@79
  1355
razor_merger_create(struct razor_set *set1, struct razor_set *set2)
krh@41
  1356
{
krh@79
  1357
	struct razor_merger *merger;
krh@41
  1358
	int count;
krh@41
  1359
	size_t size;
krh@41
  1360
krh@79
  1361
	merger = zalloc(sizeof *merger);
krh@79
  1362
	merger->set = razor_set_create();
krh@79
  1363
	hashtable_init(&merger->table, &merger->set->string_pool);
krh@41
  1364
krh@79
  1365
	count = set1->properties.size / sizeof (struct razor_property);
krh@79
  1366
	size = count * sizeof merger->source1.property_map[0];
krh@79
  1367
	merger->source1.property_map = zalloc(size);
krh@79
  1368
	merger->source1.set = set1;
krh@79
  1369
krh@79
  1370
	count = set2->properties.size / sizeof (struct razor_property);
krh@79
  1371
	size = count * sizeof merger->source2.property_map[0];
krh@79
  1372
	merger->source2.property_map = zalloc(size);
krh@79
  1373
	merger->source2.set = set2;
krh@79
  1374
krh@79
  1375
	return merger;
krh@41
  1376
}
krh@41
  1377
krh@33
  1378
static void
krh@79
  1379
add_package(struct razor_merger *merger,
krh@41
  1380
	    struct razor_package *package, struct source *source,
krh@41
  1381
	    unsigned long flags)
krh@33
  1382
{
krh@33
  1383
	char *pool;
krh@33
  1384
	unsigned long *r;
krh@41
  1385
	struct razor_package *p;
krh@33
  1386
krh@41
  1387
	pool = source->set->string_pool.data;
krh@79
  1388
	p = array_add(&merger->set->packages, sizeof *p);
krh@79
  1389
	p->name = hashtable_tokenize(&merger->table, &pool[package->name]);
krh@41
  1390
	p->name |= flags;
krh@79
  1391
	p->version = hashtable_tokenize(&merger->table,
krh@79
  1392
					&pool[package->version]);
krh@66
  1393
	p->properties = package->properties;
krh@33
  1394
krh@66
  1395
	if (package->properties & RAZOR_IMMEDIATE)
krh@66
  1396
		r = &package->properties;
krh@64
  1397
	else
krh@64
  1398
		r = (unsigned long *)
krh@66
  1399
			source->set->property_pool.data + package->properties;
krh@64
  1400
	while (1) {
krh@66
  1401
		source->property_map[*r & RAZOR_ENTRY_MASK] = 1;
krh@64
  1402
		if (*r++ & RAZOR_IMMEDIATE)
krh@64
  1403
			break;
krh@64
  1404
	}
krh@41
  1405
}
krh@41
  1406
krh@41
  1407
krh@41
  1408
/* Build the new package list sorted by merging the two package lists.
krh@79
  1409
 * Build new string pool as we go. */
krh@41
  1410
static void
krh@79
  1411
merge_packages(struct razor_merger *merger, struct array *packages)
krh@41
  1412
{
krh@41
  1413
	struct razor_package *upstream_packages, *p, *s, *send;
krh@79
  1414
	struct source *source1, *source2;
krh@41
  1415
	char *spool, *upool;
krh@41
  1416
	unsigned long *u, *uend;
krh@41
  1417
	int cmp;
krh@41
  1418
krh@79
  1419
	source1 = &merger->source1;
krh@79
  1420
	source2 = &merger->source2;
krh@41
  1421
	upstream_packages = source2->set->packages.data;
krh@41
  1422
krh@41
  1423
	u = packages->data;
krh@41
  1424
	uend = packages->data + packages->size;
krh@41
  1425
	upool = source2->set->string_pool.data;
krh@41
  1426
krh@41
  1427
	s = source1->set->packages.data;
krh@41
  1428
	send = source1->set->packages.data + source1->set->packages.size;
krh@41
  1429
	spool = source1->set->string_pool.data;
krh@41
  1430
krh@41
  1431
	while (s < send) {
krh@41
  1432
		p = upstream_packages + *u;
krh@41
  1433
krh@41
  1434
		if (u < uend)
krh@41
  1435
			cmp = strcmp(&spool[s->name], &upool[p->name]);
krh@41
  1436
		if (u >= uend || cmp < 0) {
krh@79
  1437
			add_package(merger, s, source1, 0);
krh@41
  1438
			s++;
krh@41
  1439
		} else if (cmp == 0) {
krh@79
  1440
			add_package(merger, p, source2, UPSTREAM_SOURCE);
krh@41
  1441
			s++;
krh@41
  1442
			u++;
krh@41
  1443
		} else {
krh@79
  1444
			add_package(merger, p, source2, UPSTREAM_SOURCE);
krh@41
  1445
			u++;
krh@41
  1446
		}
krh@41
  1447
	}
krh@41
  1448
}
krh@41
  1449
krh@41
  1450
static unsigned long
krh@79
  1451
add_property(struct razor_merger *merger,
krh@69
  1452
	     const char *name, const char *version, int type)
krh@41
  1453
{
krh@41
  1454
	struct razor_property *p;
krh@41
  1455
krh@79
  1456
	p = array_add(&merger->set->properties, sizeof *p);
krh@79
  1457
	p->name = hashtable_tokenize(&merger->table, name) | (type << 30);
krh@79
  1458
	p->version = hashtable_tokenize(&merger->table, version);
krh@41
  1459
krh@79
  1460
	return p - (struct razor_property *) merger->set->properties.data;
krh@41
  1461
}
krh@41
  1462
krh@41
  1463
static void
krh@79
  1464
merge_properties(struct razor_merger *merger)
krh@41
  1465
{
krh@41
  1466
	struct razor_property *p1, *p2;
krh@79
  1467
	struct razor_set *set1, *set2;
krh@79
  1468
	unsigned long *map1, *map2;
krh@41
  1469
	int i, j, cmp, count1, count2;
krh@41
  1470
	char *pool1, *pool2;
krh@41
  1471
krh@79
  1472
	set1 = merger->source1.set;
krh@79
  1473
	set2 = merger->source2.set;
krh@79
  1474
	map1 = merger->source1.property_map;
krh@79
  1475
	map2 = merger->source2.property_map;
krh@79
  1476
krh@41
  1477
	i = 0;
krh@41
  1478
	j = 0;
krh@41
  1479
	pool1 = set1->string_pool.data;
krh@41
  1480
	pool2 = set2->string_pool.data;
krh@41
  1481
krh@66
  1482
	count1 = set1->properties.size / sizeof *p1;
krh@66
  1483
	count2 = set2->properties.size / sizeof *p2;
krh@41
  1484
	while (i < count1 || j < count2) {
krh@41
  1485
		if (i < count1 && map1[i] == 0) {
krh@41
  1486
			i++;
krh@41
  1487
			continue;
krh@41
  1488
		}
krh@41
  1489
		if (j < count2 && map2[j] == 0) {
krh@41
  1490
			j++;
krh@41
  1491
			continue;
krh@41
  1492
		}
krh@66
  1493
		p1 = (struct razor_property *) set1->properties.data + i;
krh@66
  1494
		p2 = (struct razor_property *) set2->properties.data + j;
krh@41
  1495
		if (i < count1 && j < count2)
krh@66
  1496
			cmp = strcmp(&pool1[p1->name & RAZOR_ENTRY_MASK],
krh@66
  1497
				     &pool2[p2->name & RAZOR_ENTRY_MASK]);
krh@41
  1498
		else if (i < count1)
krh@41
  1499
			cmp = -1;
krh@41
  1500
		else
krh@41
  1501
			cmp = 1;
krh@41
  1502
		if (cmp == 0)
krh@41
  1503
			cmp = versioncmp(&pool1[p1->version],
krh@41
  1504
					 &pool2[p2->version]);
krh@41
  1505
		if (cmp < 0) {
krh@79
  1506
			map1[i++] = add_property(merger,
krh@66
  1507
						 &pool1[p1->name & RAZOR_ENTRY_MASK],
krh@69
  1508
						 &pool1[p1->version],
krh@69
  1509
						 (p1->name >> 30));
krh@41
  1510
		} else if (cmp > 0) {
krh@79
  1511
			map2[j++] = add_property(merger,
krh@66
  1512
						 &pool2[p2->name & RAZOR_ENTRY_MASK],
krh@69
  1513
						 &pool2[p2->version],
krh@69
  1514
						 (p2->name >> 30));
krh@41
  1515
		} else  {
krh@79
  1516
			map1[i++] = map2[j++] = add_property(merger,
krh@66
  1517
							     &pool1[p1->name & RAZOR_ENTRY_MASK],
krh@69
  1518
							     &pool1[p1->version],
krh@69
  1519
							     (p1->name >> 30));
krh@41
  1520
		}
krh@41
  1521
	}
krh@41
  1522
}
krh@41
  1523
krh@41
  1524
static unsigned long
krh@41
  1525
emit_properties(struct array *source_pool, unsigned long index,
krh@41
  1526
		unsigned long *map, struct array *pool)
krh@41
  1527
{
krh@41
  1528
	unsigned long r, *p, *q;
krh@41
  1529
krh@41
  1530
	r = pool->size / sizeof *q;
krh@41
  1531
	p = (unsigned long *) source_pool->data + index;
krh@64
  1532
	while (1) {
krh@41
  1533
		q = array_add(pool, sizeof *q);
krh@64
  1534
		*q = map[*p & RAZOR_ENTRY_MASK] | (*p & ~RAZOR_ENTRY_MASK);
krh@64
  1535
		if (*p++ & RAZOR_ENTRY_LAST)
krh@64
  1536
			break;
krh@33
  1537
	}
krh@33
  1538
krh@41
  1539
	return r;
krh@41
  1540
}
krh@41
  1541
	
krh@41
  1542
/* Rebuild property->packages maps.  We can't just remap these, as a
krh@41
  1543
 * property may have lost or gained a number of packages.  Allocate an
krh@41
  1544
 * array per property and loop through the packages and add them to
krh@41
  1545
 * the arrays for their properties. */
krh@41
  1546
static void
krh@41
  1547
rebuild_package_lists(struct razor_set *set)
krh@41
  1548
{
krh@66
  1549
	struct array *pkgs, *a;
krh@41
  1550
	struct razor_package *pkg, *pkg_end;
krh@41
  1551
	struct razor_property *prop, *prop_end;
krh@66
  1552
	unsigned long *r, *q, *pool;
krh@66
  1553
	int count;
krh@41
  1554
krh@66
  1555
	count = set->properties.size / sizeof (struct razor_property);
krh@66
  1556
	pkgs = zalloc(count * sizeof *pkgs);
krh@41
  1557
	pkg_end = set->packages.data + set->packages.size;
krh@66
  1558
	pool = set->property_pool.data;
krh@41
  1559
krh@41
  1560
	for (pkg = set->packages.data; pkg < pkg_end; pkg++) {
krh@66
  1561
		for (r = &pool[pkg->properties]; ; r++) {
krh@66
  1562
			q = array_add(&pkgs[*r & RAZOR_ENTRY_MASK], sizeof *q);
krh@41
  1563
			*q = pkg - (struct razor_package *) set->packages.data;
krh@64
  1564
			if (*r & RAZOR_IMMEDIATE)
krh@64
  1565
				break;
krh@41
  1566
		}
krh@33
  1567
	}
krh@33
  1568
krh@66
  1569
	prop_end = set->properties.data + set->properties.size;
krh@66
  1570
	a = pkgs;
krh@66
  1571
	for (prop = set->properties.data; prop < prop_end; prop++, a++) {
krh@61
  1572
		if (a->size / sizeof *r == 1) {
krh@61
  1573
			r = a->data;
krh@61
  1574
			prop->packages = *r | RAZOR_IMMEDIATE;
krh@61
  1575
		} else {
krh@61
  1576
			prop->packages =
krh@66
  1577
				add_to_property_pool(&set->property_pool, a);
krh@61
  1578
		}
krh@41
  1579
		array_release(a);
krh@41
  1580
	}
krh@66
  1581
	free(pkgs);
krh@33
  1582
}
krh@33
  1583
krh@79
  1584
struct razor_set *
krh@79
  1585
razor_merger_finish(struct razor_merger *merger)
krh@79
  1586
{
krh@79
  1587
	struct razor_set *result;
krh@79
  1588
krh@79
  1589
	result = merger->set;
krh@79
  1590
	hashtable_release(&merger->table);
krh@79
  1591
	free(merger);
krh@79
  1592
krh@79
  1593
	return result;
krh@79
  1594
}
krh@79
  1595
krh@33
  1596
/* Add packages from 'upstream' to 'set'.  The packages to add are
krh@33
  1597
 * specified by the 'packages' array, which is a sorted list of
krh@33
  1598
 * package indexes.  Returns a newly allocated package set.  Does not
krh@41
  1599
 * enforce validity of the resulting package set.
krh@41
  1600
 *
krh@41
  1601
 * This looks more complicated than it is.  An easy way to merge two
krh@41
  1602
 * package sets would be to just use a razor_importer, but that
krh@41
  1603
 * requires resorting, and is thus O(n log n).  We can do this in a
krh@41
  1604
 * linear sweep, but it gets a little more complicated.
krh@41
  1605
 */
krh@33
  1606
struct razor_set *
krh@33
  1607
razor_set_add(struct razor_set *set, struct razor_set *upstream,
krh@33
  1608
	      struct array *packages)
krh@33
  1609
{
krh@79
  1610
	struct razor_merger *merger;
krh@41
  1611
	struct razor_package *p, *pend;
krh@33
  1612
krh@79
  1613
	merger = razor_merger_create(set, upstream);
krh@33
  1614
krh@79
  1615
	merge_packages(merger, packages);
krh@41
  1616
krh@41
  1617
	/* As we built the package list, we filled out a bitvector of
krh@41
  1618
	 * the properties that are referenced by the packages in the
krh@41
  1619
	 * new set.  Now we do a parallel loop through the properties
krh@41
  1620
	 * and emit those marked in the bit vector to the new set.  In
krh@41
  1621
	 * the process, we update the bit vector to actually map from
krh@41
  1622
	 * indices in the old property list to indices in the new
krh@41
  1623
	 * property list for both sets. */
krh@41
  1624
krh@79
  1625
	merge_properties(merger);
krh@41
  1626
krh@41
  1627
	/* Now we loop through the packages again and emit the
krh@41
  1628
	 * property lists, remapped to point to the new properties. */
krh@41
  1629
krh@79
  1630
	pend = merger->set->packages.data + merger->set->packages.size;
krh@79
  1631
	for (p = merger->set->packages.data; p < pend; p++) {
krh@41
  1632
		struct source *src;
krh@41
  1633
krh@41
  1634
		if (p->name & UPSTREAM_SOURCE)
krh@79
  1635
			src = &merger->source2;
krh@41
  1636
		else
krh@79
  1637
			src = &merger->source1;
krh@41
  1638
krh@66
  1639
		p->properties = emit_properties(&src->set->property_pool,
krh@66
  1640
						p->properties,
krh@66
  1641
						src->property_map,
krh@79
  1642
						&merger->set->property_pool);
krh@41
  1643
		p->name &= INDEX_MASK;
krh@33
  1644
	}
krh@33
  1645
krh@79
  1646
	rebuild_package_lists(merger->set);
krh@41
  1647
krh@79
  1648
	return razor_merger_finish(merger);
krh@33
  1649
}
krh@33
  1650
krh@37
  1651
void
krh@37
  1652
razor_set_satisfy(struct razor_set *set, struct array *unsatisfied,
krh@37
  1653
		  struct razor_set *upstream, struct array *list)
krh@37
  1654
{
krh@37
  1655
	struct razor_property *requires, *r;
krh@37
  1656
	struct razor_property *p, *pend;
krh@39
  1657
	unsigned long *u, *end, *pkg, *package_pool;
krh@37
  1658
	char *pool, *upool;
krh@37
  1659
krh@37
  1660
	end = unsatisfied->data + unsatisfied->size;
krh@66
  1661
	requires = set->properties.data;
krh@37
  1662
	pool = set->string_pool.data;
krh@37
  1663
krh@66
  1664
	p = upstream->properties.data;
krh@66
  1665
	pend = upstream->properties.data + upstream->properties.size;
krh@37
  1666
	upool = upstream->string_pool.data;
krh@39
  1667
	package_pool = upstream->package_pool.data;
krh@37
  1668
krh@37
  1669
	for (u = unsatisfied->data; u < end; u++) {
krh@37
  1670
		r = requires + *u;
krh@37
  1671
krh@66
  1672
		while (p < pend &&
krh@66
  1673
		       strcmp(&pool[r->name & RAZOR_ENTRY_MASK],
krh@66
  1674
			      &upool[p->name & RAZOR_ENTRY_MASK]) > 0 &&
krh@66
  1675
		       (p->name >> 30) != RAZOR_PROPERTY_PROVIDES)
krh@37
  1676
			p++;
krh@37
  1677
		/* If there is more than one version of a provides,
krh@37
  1678
		 * seek to the end for the highest version. */
krh@37
  1679
		while (p + 1 < pend && p->name == (p + 1)->name)
krh@37
  1680
			p++;
krh@37
  1681
krh@37
  1682
		if (p == pend ||
krh@66
  1683
		    strcmp(&pool[r->name & RAZOR_ENTRY_MASK],
krh@66
  1684
			   &upool[p->name & RAZOR_ENTRY_MASK]) != 0 ||
krh@37
  1685
		    versioncmp(&pool[r->version], &upool[p->version]) > 0) {
krh@37
  1686
			/* Do we need to track unsatisfiable requires
krh@37
  1687
			 * as we go, or should we just do a
krh@37
  1688
			 * razor_set_validate() at the end? */
krh@37
  1689
		} else {
krh@37
  1690
			pkg = array_add(list, sizeof *pkg);
krh@37
  1691
			/* We just pull in the first package that provides */
krh@61
  1692
			if (p->packages & RAZOR_IMMEDIATE)
krh@61
  1693
				*pkg = p->packages & RAZOR_ENTRY_MASK;
krh@61
  1694
			else
krh@61
  1695
				*pkg = package_pool[p->packages];
krh@37
  1696
		}
krh@37
  1697
	}	
krh@37
  1698
}
krh@37
  1699
krh@38
  1700
static void
krh@38
  1701
find_packages(struct razor_set *set,
krh@38
  1702
	      int count, const char **packages, struct array *list)
krh@38
  1703
{
krh@38
  1704
	struct razor_package *p;
krh@38
  1705
	unsigned long *r;
krh@38
  1706
	int i;
krh@38
  1707
krh@38
  1708
	/* FIXME: Sort the packages. */
krh@38
  1709
	for (i = 0; i < count; i++) {
krh@38
  1710
		p = razor_set_get_package(set, packages[i]);
krh@38
  1711
		r = array_add(list, sizeof *r);
krh@38
  1712
		*r = p - (struct razor_package *) set->packages.data;
krh@38
  1713
	}
krh@38
  1714
}
krh@38
  1715
krh@38
  1716
static void
krh@38
  1717
find_all_packages(struct razor_set *set,
krh@38
  1718
		  struct razor_set *upstream, struct array *list)
krh@38
  1719
{
krh@38
  1720
	struct razor_package *p, *u, *pend, *uend;
krh@38
  1721
	unsigned long *r;
krh@38
  1722
	char *pool, *upool;
krh@38
  1723
krh@38
  1724
	pend = set->packages.data + set->packages.size;
krh@38
  1725
	pool = set->string_pool.data;
krh@38
  1726
	u = upstream->packages.data;
krh@38
  1727
	uend = upstream->packages.data + upstream->packages.size;
krh@38
  1728
	upool = upstream->string_pool.data;
krh@38
  1729
krh@38
  1730
	for (p = set->packages.data; p < pend; p++) {
krh@38
  1731
		while (u < uend && strcmp(&pool[p->name], &upool[u->name]) > 0)
krh@38
  1732
			u++;
krh@38
  1733
		if (strcmp(&pool[p->name], &upool[u->name]) == 0) {
krh@38
  1734
			r = array_add(list, sizeof *r);
krh@38
  1735
			*r = u - (struct razor_package *) upstream->packages.data;
krh@38
  1736
		}
krh@38
  1737
	}
krh@38
  1738
}
krh@38
  1739
krh@33
  1740
struct razor_set *
krh@33
  1741
razor_set_update(struct razor_set *set, struct razor_set *upstream,
krh@33
  1742
		 int count, const char **packages)
krh@33
  1743
{
krh@37
  1744
	struct razor_set *new;
krh@43
  1745
	struct razor_package *upackages;
krh@37
  1746
	struct array list, unsatisfied;
krh@37
  1747
	char *pool;
krh@38
  1748
	unsigned long *u, *end;
krh@38
  1749
	int total = 0;
krh@33
  1750
krh@33
  1751
	array_init(&list);
krh@38
  1752
	if (count > 0)
krh@38
  1753
		find_packages(upstream, count, packages, &list);
krh@38
  1754
	else
krh@38
  1755
		find_all_packages(set, upstream, &list);
krh@33
  1756
krh@37
  1757
	end = list.data + list.size;
krh@37
  1758
	upackages = upstream->packages.data;
krh@37
  1759
	pool = upstream->string_pool.data;
krh@38
  1760
	total += list.size / sizeof *u;
krh@37
  1761
krh@37
  1762
	while (list.size > 0) {
krh@37
  1763
		new = razor_set_add(set, upstream, &list);
krh@37
  1764
		array_release(&list);
krh@37
  1765
		razor_set_destroy(set);
krh@37
  1766
		set = new;
krh@37
  1767
krh@37
  1768
		array_init(&unsatisfied);
krh@37
  1769
		razor_set_validate(new, &unsatisfied);
krh@37
  1770
		array_init(&list);
krh@37
  1771
		razor_set_satisfy(new, &unsatisfied, upstream, &list);
krh@37
  1772
		array_release(&unsatisfied);
krh@37
  1773
krh@37
  1774
		end = list.data + list.size;
krh@37
  1775
		upackages = upstream->packages.data;
krh@37
  1776
		pool = upstream->string_pool.data;
krh@38
  1777
		total += list.size / sizeof *u;
krh@37
  1778
	}
krh@37
  1779
krh@37
  1780
	array_release(&list);
krh@37
  1781
krh@37
  1782
	return set;
krh@33
  1783
}
krh@33
  1784
krh@44
  1785
/* The diff order matters.  We should sort the packages so that a
krh@44
  1786
 * REMOVE of a package comes before the INSTALL, and so that all
krh@44
  1787
 * requires for a package have been installed before the package.
krh@44
  1788
 **/
krh@44
  1789
krh@44
  1790
void
krh@44
  1791
razor_set_diff(struct razor_set *set, struct razor_set *upstream,
krh@44
  1792
	       razor_package_callback_t callback, void *data)
krh@44
  1793
{
krh@44
  1794
	struct razor_package *p, *pend, *u, *uend;
krh@44
  1795
	char *ppool, *upool;
krh@44
  1796
	int res = 0;
krh@44
  1797
krh@44
  1798
	p = set->packages.data;
krh@44
  1799
	pend = set->packages.data + set->packages.size;
krh@44
  1800
	ppool = set->string_pool.data;
krh@44
  1801
krh@44
  1802
	u = upstream->packages.data;
krh@44
  1803
	uend = upstream->packages.data + upstream->packages.size;
krh@44
  1804
	upool = upstream->string_pool.data;
krh@44
  1805
krh@44
  1806
	while (p < pend || u < uend) {
krh@44
  1807
		if (p < pend && u < uend) {
krh@44
  1808
			res = strcmp(&ppool[p->name], &upool[u->name]);
krh@44
  1809
			if (res == 0)
krh@44
  1810
				res = versioncmp(&ppool[p->version],
krh@44
  1811
						 &upool[u->version]);
krh@44
  1812
		}
krh@44
  1813
krh@44
  1814
		if (u == uend || res < 0) {
krh@44
  1815
			callback(&ppool[p->name], &ppool[p->version],
krh@44
  1816
				 NULL, data);
krh@44
  1817
			p++;
krh@44
  1818
			continue;
krh@44
  1819
		} else if (p == pend || res > 0) {
krh@44
  1820
			callback(&upool[u->name], NULL, &upool[u->version],
krh@44
  1821
				 data);
krh@44
  1822
			u++;
krh@44
  1823
			continue;
krh@44
  1824
		} else {
krh@44
  1825
			p++;
krh@44
  1826
			u++;
krh@44
  1827
		}
krh@44
  1828
	}
krh@44
  1829
}