razor.c
author Kristian H?gsberg <krh@redhat.com>
Fri Sep 07 11:56:04 2007 -0400 (2007-09-07)
changeset 13 6a715b310792
parent 12 71a410830f3d
child 14 d21d74d6e297
permissions -rw-r--r--
Clean up import logic and make it independent of XML parsing.
krh@0
     1
#include <stdlib.h>
krh@0
     2
#include <stdio.h>
krh@0
     3
#include <string.h>
krh@0
     4
#include <sys/types.h>
krh@0
     5
#include <sys/stat.h>
krh@0
     6
#include <sys/mman.h>
krh@0
     7
#include <unistd.h>
krh@0
     8
#include <fcntl.h>
krh@0
     9
krh@0
    10
#include <expat.h>
krh@0
    11
#include "sha1.h"
krh@0
    12
krh@6
    13
struct array {
krh@6
    14
	void *data;
krh@6
    15
	int size, alloc;
krh@6
    16
};
krh@6
    17
krh@13
    18
static void
krh@13
    19
array_init(struct array *array)
krh@13
    20
{
krh@13
    21
	memset(array, 0, sizeof *array);
krh@13
    22
}
krh@13
    23
krh@13
    24
static void
krh@13
    25
array_release(struct array *array)
krh@13
    26
{
krh@13
    27
	free(array->data);
krh@13
    28
}
krh@13
    29
krh@6
    30
static void *
krh@6
    31
array_add(struct array *array, int size)
krh@6
    32
{
krh@6
    33
	int alloc;
krh@6
    34
	void *data, *p;
krh@6
    35
krh@6
    36
	if (array->alloc > 0)
krh@6
    37
		alloc = array->alloc;
krh@6
    38
	else
krh@10
    39
		alloc = 16;
krh@6
    40
krh@6
    41
	while (alloc < array->size + size)
krh@6
    42
		alloc *= 2;
krh@6
    43
krh@6
    44
	if (array->alloc < alloc) {
krh@6
    45
		data = realloc(array->data, alloc);
krh@6
    46
		if (data == NULL)
krh@6
    47
			return 0;
krh@6
    48
		array->data = data;
krh@6
    49
		array->alloc = alloc;
krh@6
    50
	}
krh@6
    51
krh@6
    52
	p = array->data + array->size;
krh@6
    53
	array->size += size;
krh@6
    54
krh@6
    55
	return p;
krh@6
    56
}
krh@6
    57
krh@0
    58
static int
krh@0
    59
write_to_fd(int fd, void *p, size_t size)
krh@0
    60
{
krh@0
    61
	int rest, len;
krh@0
    62
krh@0
    63
	rest = size;
krh@0
    64
	while (rest > 0) {
krh@0
    65
		len = write(fd, p, rest);
krh@0
    66
		if (len < 0)
krh@0
    67
			return -1;
krh@0
    68
		rest -= len;
krh@0
    69
	}
krh@0
    70
krh@0
    71
	return 0;
krh@0
    72
}
krh@0
    73
krh@0
    74
static int
krh@0
    75
write_to_file(const char *filename, void *p, size_t size)
krh@0
    76
{
krh@0
    77
	int fd, err;
krh@0
    78
krh@0
    79
	fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666);
krh@0
    80
	if (fd < 0)
krh@0
    81
		return -1;
krh@0
    82
	err = write_to_fd(fd, p, size);
krh@0
    83
	close(fd);
krh@0
    84
krh@0
    85
	return err;
krh@0
    86
}
krh@0
    87
krh@0
    88
static void *
krh@0
    89
zalloc(size_t size)
krh@0
    90
{
krh@0
    91
	void *p;
krh@0
    92
krh@0
    93
	p = malloc(size);
krh@0
    94
	memset(p, 0, size);
krh@0
    95
krh@0
    96
	return p;
krh@0
    97
}
krh@0
    98
krh@4
    99
struct razor_set_header {
krh@4
   100
	unsigned int magic;
krh@4
   101
	unsigned int version;
krh@4
   102
	struct { unsigned int type, offset; } sections[0];
krh@4
   103
};
krh@4
   104
krh@4
   105
#define RAZOR_MAGIC 0x7a7a7a7a
krh@4
   106
#define RAZOR_VERSION 1
krh@8
   107
krh@4
   108
#define RAZOR_BUCKETS 1
krh@4
   109
#define RAZOR_STRINGS 2
krh@4
   110
#define RAZOR_PACKAGES 3
krh@8
   111
#define RAZOR_REQUIRES 4
krh@8
   112
#define RAZOR_PROVIDES 5
krh@10
   113
#define RAZOR_PROPERTIES 6
krh@4
   114
krh@4
   115
struct razor_package {
krh@4
   116
	unsigned long name;
krh@4
   117
	unsigned long version;
krh@10
   118
	unsigned long requires;
krh@10
   119
	unsigned long provides;
krh@4
   120
};
krh@4
   121
krh@8
   122
struct razor_property {
krh@7
   123
	unsigned long name;
krh@7
   124
	unsigned long version;
krh@9
   125
	unsigned long packages;
krh@7
   126
};
krh@7
   127
krh@4
   128
struct razor_set {
krh@6
   129
	struct array buckets;
krh@6
   130
	struct array string_pool;
krh@10
   131
	struct array property_pool;
krh@7
   132
 	struct array packages;
krh@8
   133
 	struct array requires;
krh@7
   134
 	struct array provides;
krh@4
   135
	struct razor_set_header *header;
krh@4
   136
};
krh@4
   137
krh@4
   138
struct razor_set *
krh@4
   139
razor_set_create(void)
krh@0
   140
{
krh@4
   141
	struct razor_set *set;
krh@6
   142
	char *p;
krh@0
   143
krh@6
   144
	set = zalloc(sizeof(struct razor_set));
krh@6
   145
	p = array_add(&set->string_pool, 1);
krh@6
   146
	*p = '\0';
krh@3
   147
krh@4
   148
	return set;
krh@0
   149
}
krh@0
   150
krh@4
   151
struct razor_set *
krh@4
   152
razor_set_open(const char *filename)
krh@0
   153
{
krh@4
   154
	struct razor_set *set;
krh@0
   155
	struct stat stat;
krh@0
   156
	unsigned int size, offset;
krh@0
   157
	int fd, i;
krh@0
   158
krh@4
   159
	set = zalloc(sizeof *set);
krh@0
   160
	fd = open(filename, O_RDONLY);
krh@0
   161
	if (fstat(fd, &stat) < 0)
krh@0
   162
		return NULL;
krh@4
   163
	set->header = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
krh@4
   164
	if (set->header == MAP_FAILED) {
krh@4
   165
		free(set);
krh@0
   166
		return NULL;
krh@0
   167
	}
krh@0
   168
krh@4
   169
	for (i = 0; i < set->header->sections[i].type; i++) {
krh@4
   170
		offset = set->header->sections[i].offset;
krh@4
   171
		size = set->header->sections[i + 1].offset - offset;
krh@0
   172
krh@4
   173
		switch (set->header->sections[i].type) {
krh@4
   174
		case RAZOR_BUCKETS:
krh@6
   175
			set->buckets.data = (void *) set->header + offset;
krh@6
   176
			set->buckets.size = size;
krh@6
   177
			set->buckets.alloc = size;
krh@0
   178
			break;
krh@4
   179
		case RAZOR_STRINGS:
krh@6
   180
			set->string_pool.data = (void *) set->header + offset;
krh@6
   181
			set->string_pool.size = size;
krh@6
   182
			set->string_pool.alloc = size;
krh@0
   183
			break;
krh@4
   184
		case RAZOR_PACKAGES:
krh@6
   185
			set->packages.data = (void *) set->header + offset;
krh@6
   186
			set->packages.size = size;
krh@6
   187
			set->packages.size = size;
krh@3
   188
			break;
krh@8
   189
		case RAZOR_REQUIRES:
krh@8
   190
			set->requires.data = (void *) set->header + offset;
krh@8
   191
			set->requires.size = size;
krh@8
   192
			set->requires.size = size;
krh@8
   193
			break;
krh@7
   194
		case RAZOR_PROVIDES:
krh@7
   195
			set->provides.data = (void *) set->header + offset;
krh@7
   196
			set->provides.size = size;
krh@7
   197
			set->provides.size = size;
krh@7
   198
			break;
krh@10
   199
		case RAZOR_PROPERTIES:
krh@10
   200
			set->property_pool.data = (void *) set->header + offset;
krh@10
   201
			set->property_pool.size = size;
krh@10
   202
			set->property_pool.size = size;
krh@10
   203
			break;
krh@0
   204
		}
krh@0
   205
	}
krh@0
   206
	close(fd);
krh@0
   207
krh@4
   208
	return set;
krh@0
   209
}
krh@0
   210
krh@0
   211
void
krh@4
   212
razor_set_destroy(struct razor_set *set)
krh@0
   213
{
krh@0
   214
	unsigned int size;
krh@0
   215
	int i;
krh@0
   216
krh@4
   217
	if (set->header) {
krh@4
   218
		for (i = 0; set->header->sections[i].type; i++)
krh@0
   219
			;
krh@4
   220
		size = set->header->sections[i].type;
krh@4
   221
		munmap(set->header, size);
krh@0
   222
	} else {
krh@6
   223
		free(set->buckets.data);
krh@6
   224
		free(set->string_pool.data);
krh@6
   225
		free(set->packages.data);
krh@9
   226
		free(set->requires.data);
krh@9
   227
		free(set->provides.data);
krh@10
   228
		free(set->property_pool.data);
krh@0
   229
	}
krh@0
   230
krh@4
   231
	free(set);
krh@0
   232
}
krh@0
   233
krh@0
   234
static int
krh@4
   235
razor_set_write(struct razor_set *set, const char *filename)
krh@0
   236
{
krh@0
   237
	char data[4096];
krh@4
   238
	struct razor_set_header *header = (struct razor_set_header *) data;
krh@8
   239
	int fd, pool_size, packages_size, requires_size, provides_size;
krh@10
   240
	int properties_size;
krh@3
   241
krh@3
   242
	/* Align these to pages sizes */
krh@6
   243
	pool_size = (set->string_pool.size + 4095) & ~4095;
krh@6
   244
	packages_size = (set->packages.size + 4095) & ~4095;
krh@8
   245
	requires_size = (set->requires.size + 4095) & ~4095;
krh@7
   246
	provides_size = (set->provides.size + 4095) & ~4095;
krh@10
   247
	properties_size = (set->property_pool.size + 4095) & ~4095;
krh@0
   248
krh@0
   249
	memset(data, 0, sizeof data);
krh@4
   250
	header->magic = RAZOR_MAGIC;
krh@4
   251
	header->version = RAZOR_VERSION;
krh@0
   252
krh@4
   253
	header->sections[0].type = RAZOR_BUCKETS;
krh@0
   254
	header->sections[0].offset = sizeof data;
krh@0
   255
krh@4
   256
	header->sections[1].type = RAZOR_STRINGS;
krh@6
   257
	header->sections[1].offset =
krh@6
   258
		header->sections[0].offset + set->buckets.alloc;
krh@0
   259
krh@4
   260
	header->sections[2].type = RAZOR_PACKAGES;
krh@6
   261
	header->sections[2].offset =
krh@6
   262
		header->sections[1].offset + pool_size;
krh@3
   263
krh@8
   264
	header->sections[3].type = RAZOR_REQUIRES;
krh@6
   265
	header->sections[3].offset =
krh@6
   266
		header->sections[2].offset + packages_size;
krh@0
   267
krh@8
   268
	header->sections[4].type = RAZOR_PROVIDES;
krh@7
   269
	header->sections[4].offset =
krh@8
   270
		header->sections[3].offset + requires_size;
krh@8
   271
krh@10
   272
	header->sections[5].type = RAZOR_PROPERTIES;
krh@8
   273
	header->sections[5].offset =
krh@8
   274
		header->sections[4].offset + provides_size;
krh@7
   275
krh@10
   276
	header->sections[6].type = 0;
krh@10
   277
	header->sections[6].offset =
krh@10
   278
		header->sections[5].offset + properties_size;
krh@10
   279
krh@0
   280
	fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666);
krh@0
   281
	if (fd < 0)
krh@0
   282
		return -1;
krh@0
   283
krh@0
   284
	write_to_fd(fd, data, sizeof data);
krh@6
   285
	write_to_fd(fd, set->buckets.data, set->buckets.alloc);
krh@6
   286
	write_to_fd(fd, set->string_pool.data, pool_size);
krh@6
   287
	write_to_fd(fd, set->packages.data, packages_size);
krh@8
   288
	write_to_fd(fd, set->requires.data, requires_size);
krh@7
   289
	write_to_fd(fd, set->provides.data, provides_size);
krh@10
   290
	write_to_fd(fd, set->property_pool.data, properties_size);
krh@0
   291
krh@0
   292
	return 0;
krh@0
   293
}
krh@0
   294
krh@0
   295
static unsigned int
krh@0
   296
hash_string(const char *key)
krh@0
   297
{
krh@0
   298
	const char *p;
krh@0
   299
	unsigned int hash = 0;
krh@0
   300
krh@0
   301
	for (p = key; *p; p++)
krh@9
   302
		hash = (hash * 617) ^ *p;
krh@0
   303
krh@0
   304
	return hash;
krh@0
   305
}
krh@0
   306
krh@0
   307
unsigned long
krh@4
   308
razor_set_lookup(struct razor_set *set, const char *key)
krh@0
   309
{
krh@6
   310
	unsigned int mask, start, i;
krh@6
   311
	unsigned long *b;
krh@6
   312
	char *pool;
krh@0
   313
krh@6
   314
	pool = set->string_pool.data;
krh@6
   315
	mask = set->buckets.alloc - 1;
krh@6
   316
	start = hash_string(key) * sizeof(unsigned long);
krh@0
   317
krh@6
   318
	for (i = 0; i < set->buckets.alloc; i += sizeof *b) {
krh@6
   319
		b = set->buckets.data + ((start + i) & mask);
krh@6
   320
krh@6
   321
		if (*b == 0)
krh@0
   322
			return 0;
krh@0
   323
krh@6
   324
		if (strcmp(key, &pool[*b]) == 0)
krh@6
   325
			return *b;
krh@6
   326
	}
krh@0
   327
krh@0
   328
	return 0;
krh@0
   329
}
krh@0
   330
krh@0
   331
static unsigned long
krh@4
   332
add_to_string_pool(struct razor_set *set, const char *key)
krh@0
   333
{
krh@6
   334
	int len;
krh@6
   335
	char *p;
krh@0
   336
krh@0
   337
	len = strlen(key) + 1;
krh@6
   338
	p = array_add(&set->string_pool, len);
krh@6
   339
	memcpy(p, key, len);
krh@0
   340
krh@6
   341
	return p - (char *) set->string_pool.data;
krh@0
   342
}
krh@0
   343
krh@10
   344
static unsigned long
krh@10
   345
add_to_property_pool(struct razor_set *set, struct array *properties)
krh@10
   346
{
krh@10
   347
	unsigned long  *p;
krh@10
   348
krh@10
   349
	p = array_add(properties, sizeof *p);
krh@10
   350
	*p = 0;
krh@10
   351
	p = array_add(&set->property_pool, properties->size);
krh@10
   352
	memcpy(p, properties->data, properties->size);
krh@10
   353
krh@10
   354
	return p - (unsigned long *) set->property_pool.data;
krh@10
   355
}
krh@10
   356
krh@0
   357
static void
krh@4
   358
do_insert(struct razor_set *set, unsigned long value)
krh@0
   359
{
krh@6
   360
	unsigned int mask, start, i;
krh@6
   361
	unsigned long *b;
krh@0
   362
	const char *key;
krh@0
   363
krh@6
   364
	key = (char *) set->string_pool.data + value;
krh@6
   365
	mask = set->buckets.alloc - 1;
krh@6
   366
	start = hash_string(key) * sizeof(unsigned long);
krh@6
   367
krh@6
   368
	for (i = 0; i < set->buckets.alloc; i += sizeof *b) {
krh@6
   369
		b = set->buckets.data + ((start + i) & mask);
krh@6
   370
		if (*b == 0) {
krh@6
   371
			*b = value;
krh@0
   372
			break;
krh@0
   373
		}
krh@6
   374
	}
krh@0
   375
}
krh@0
   376
krh@0
   377
unsigned long
krh@4
   378
razor_set_insert(struct razor_set *set, const char *key)
krh@0
   379
{
krh@6
   380
	unsigned long value, *buckets, *b, *end;
krh@6
   381
	int alloc;
krh@0
   382
krh@6
   383
	alloc = set->buckets.alloc;
krh@6
   384
	array_add(&set->buckets, 4 * sizeof *buckets);
krh@6
   385
	if (alloc != set->buckets.alloc) {
krh@6
   386
		end = set->buckets.data + alloc;
krh@6
   387
		memset(end, 0, set->buckets.alloc - alloc);
krh@6
   388
		for (b = set->buckets.data; b < end; b++) {
krh@6
   389
			value = *b;
krh@6
   390
			if (value != 0) {
krh@6
   391
				*b = 0;
krh@4
   392
				do_insert(set, value);
krh@6
   393
			}
krh@0
   394
		}
krh@0
   395
	}
krh@0
   396
krh@4
   397
	value = add_to_string_pool(set, key);
krh@4
   398
	do_insert (set, value);
krh@0
   399
krh@0
   400
	return value;
krh@0
   401
}
krh@0
   402
krh@0
   403
unsigned long
krh@4
   404
razor_set_tokenize(struct razor_set *set, const char *string)
krh@0
   405
{
krh@0
   406
	unsigned long token;
krh@0
   407
krh@13
   408
	if (string == NULL)
krh@13
   409
		return 0;
krh@13
   410
krh@4
   411
	token = razor_set_lookup(set, string);
krh@0
   412
	if (token != 0)
krh@0
   413
		return token;
krh@0
   414
krh@4
   415
	return razor_set_insert(set, string);
krh@0
   416
}
krh@0
   417
krh@13
   418
struct import_property_context {
krh@10
   419
	struct array all;
krh@10
   420
	struct array package;
krh@10
   421
};
krh@10
   422
krh@9
   423
struct import_context {
krh@4
   424
	struct razor_set *set;
krh@13
   425
	struct import_property_context requires;
krh@13
   426
	struct import_property_context provides;
krh@9
   427
	unsigned long package;
krh@10
   428
	unsigned long *requires_map;
krh@10
   429
	unsigned long *provides_map;
krh@10
   430
};
krh@10
   431
krh@10
   432
struct import_property {
krh@10
   433
	unsigned long name;
krh@10
   434
	unsigned long version;
krh@10
   435
	unsigned long package;
krh@10
   436
	unsigned long index;
krh@10
   437
	unsigned long unique_index;
krh@0
   438
};
krh@0
   439
krh@0
   440
static void
krh@13
   441
import_context_add_package(struct import_context *ctx,
krh@13
   442
			   const char *name, const char *version)
krh@13
   443
{
krh@13
   444
	struct razor_package *p;
krh@13
   445
krh@13
   446
	p = array_add(&ctx->set->packages, sizeof *p);
krh@13
   447
	p->name = razor_set_tokenize(ctx->set, name);
krh@13
   448
	p->version = razor_set_tokenize(ctx->set, version);
krh@13
   449
krh@13
   450
	ctx->package = p - (struct razor_package *) ctx->set->packages.data;
krh@13
   451
	array_init(&ctx->requires.package);
krh@13
   452
	array_init(&ctx->provides.package);
krh@13
   453
}
krh@13
   454
krh@13
   455
void
krh@13
   456
import_context_finish_package(struct import_context *ctx)
krh@13
   457
{
krh@13
   458
	struct razor_package *p;
krh@13
   459
krh@13
   460
	p = (struct razor_package *) ctx->set->packages.data + ctx->package;
krh@13
   461
	p->requires = add_to_property_pool(ctx->set, &ctx->requires.package);
krh@13
   462
	p->provides = add_to_property_pool(ctx->set, &ctx->provides.package);
krh@13
   463
krh@13
   464
	array_release(&ctx->requires.package);
krh@13
   465
	array_release(&ctx->provides.package);
krh@13
   466
}
krh@13
   467
krh@13
   468
static void
krh@13
   469
import_context_add_property(struct import_context *ctx,
krh@13
   470
			    struct import_property_context *pctx,
krh@13
   471
			    const char *name, const char *version)
krh@13
   472
{
krh@13
   473
	struct import_property *p;
krh@13
   474
	unsigned long *r;
krh@13
   475
krh@13
   476
	p = array_add(&pctx->all, sizeof *p);
krh@13
   477
	p->name = razor_set_tokenize(ctx->set, name);
krh@13
   478
	p->version = razor_set_tokenize(ctx->set, version);
krh@13
   479
	p->package = ctx->package;
krh@13
   480
	p->index = p - (struct import_property *) pctx->all.data;
krh@13
   481
krh@13
   482
	r = array_add(&pctx->package, sizeof *r);
krh@13
   483
	*r = p->index;
krh@13
   484
}
krh@13
   485
krh@13
   486
static void
krh@9
   487
parse_package(struct import_context *ctx, const char **atts, void *data)
krh@3
   488
{
krh@13
   489
	const char *name = NULL, *version = NULL;
krh@3
   490
	int i;
krh@3
   491
krh@3
   492
	for (i = 0; atts[i]; i += 2) {
krh@3
   493
		if (strcmp(atts[i], "name") == 0)
krh@13
   494
			name = atts[i + 1];
krh@3
   495
		else if (strcmp(atts[i], "version") == 0)
krh@13
   496
			version = atts[i + 1];
krh@3
   497
	}
krh@3
   498
krh@13
   499
	if (name == NULL || version == NULL) {
krh@3
   500
		fprintf(stderr, "invalid package tag, "
krh@3
   501
			"missing name or version attributes\n");
krh@3
   502
		return;
krh@3
   503
	}
krh@3
   504
krh@13
   505
	import_context_add_package(ctx, name, version);
krh@3
   506
}
krh@3
   507
krh@3
   508
static void
krh@9
   509
parse_property(struct import_context *ctx, const char **atts, void *data)
krh@8
   510
{
krh@13
   511
	const char *name = NULL, *version = NULL;
krh@8
   512
	int i;
krh@8
   513
krh@8
   514
	for (i = 0; atts[i]; i += 2) {
krh@8
   515
		if (strcmp(atts[i], "name") == 0)
krh@13
   516
			name = atts[i + 1];
krh@9
   517
		if (strcmp(atts[i], "version") == 0)
krh@13
   518
			version = atts[i + 1];
krh@8
   519
	}
krh@8
   520
	
krh@13
   521
	if (name == NULL) {
krh@9
   522
		fprintf(stderr, "invalid tag, missing name attribute\n");
krh@8
   523
		return;
krh@8
   524
	}
krh@8
   525
krh@13
   526
	import_context_add_property(ctx, data, name, version);
krh@7
   527
}
krh@7
   528
krh@7
   529
static void
krh@0
   530
start_element(void *data, const char *name, const char **atts)
krh@0
   531
{
krh@9
   532
	struct import_context *ctx = data;
krh@0
   533
krh@3
   534
	if (strcmp(name, "package") == 0)
krh@9
   535
		parse_package(ctx, atts, NULL);
krh@8
   536
	else if (strcmp(name, "requires") == 0)
krh@9
   537
		parse_property(ctx, atts, &ctx->requires);
krh@7
   538
	else if (strcmp(name, "provides") == 0)
krh@9
   539
		parse_property(ctx, atts, &ctx->provides);
krh@0
   540
}
krh@0
   541
krh@0
   542
static void
krh@0
   543
end_element (void *data, const char *name)
krh@0
   544
{
krh@9
   545
	struct import_context *ctx = data;
krh@4
   546
krh@13
   547
	if (strcmp(name, "package") == 0)
krh@13
   548
		import_context_finish_package(ctx);
krh@0
   549
}
krh@0
   550
krh@0
   551
static char *
krh@0
   552
sha1_to_hex(const unsigned char *sha1)
krh@0
   553
{
krh@0
   554
	static int bufno;
krh@0
   555
	static char hexbuffer[4][50];
krh@0
   556
	static const char hex[] = "0123456789abcdef";
krh@0
   557
	char *buffer = hexbuffer[3 & ++bufno], *buf = buffer;
krh@0
   558
	int i;
krh@0
   559
krh@0
   560
	for (i = 0; i < 20; i++) {
krh@0
   561
		unsigned int val = *sha1++;
krh@0
   562
		*buf++ = hex[val >> 4];
krh@0
   563
		*buf++ = hex[val & 0xf];
krh@0
   564
	}
krh@0
   565
	*buf = '\0';
krh@0
   566
krh@0
   567
	return buffer;
krh@0
   568
}
krh@0
   569
krh@9
   570
static void
krh@13
   571
razor_prepare_import(struct import_context *ctx)
krh@9
   572
{
krh@9
   573
	memset(ctx, 0, sizeof *ctx);
krh@13
   574
	ctx->set = razor_set_create();
krh@9
   575
}
krh@9
   576
krh@0
   577
static int
krh@13
   578
razor_import(struct import_context *ctx, const char *filename)
krh@0
   579
{
krh@0
   580
	SHA_CTX sha1;
krh@0
   581
	XML_Parser parser;
krh@0
   582
	int fd;
krh@0
   583
	void *p;
krh@0
   584
	struct stat stat;
krh@0
   585
	char buf[128];
krh@0
   586
	unsigned char hash[20];
krh@0
   587
krh@0
   588
	fd = open(filename, O_RDONLY);
krh@0
   589
	if (fstat(fd, &stat) < 0)
krh@0
   590
		return -1;
krh@0
   591
	p = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
krh@0
   592
	if (p == MAP_FAILED)
krh@0
   593
		return -1;
krh@0
   594
krh@0
   595
	parser = XML_ParserCreate(NULL);
krh@9
   596
	XML_SetUserData(parser, ctx);
krh@0
   597
	XML_SetElementHandler(parser, start_element, end_element);
krh@0
   598
	if (XML_Parse(parser, p, stat.st_size, 1) == XML_STATUS_ERROR) {
krh@0
   599
		fprintf(stderr,
krh@0
   600
			"%s at line %d, %s\n",
krh@0
   601
			XML_ErrorString(XML_GetErrorCode(parser)),
krh@0
   602
			XML_GetCurrentLineNumber(parser),
krh@0
   603
			filename);
krh@0
   604
		return 1;
krh@0
   605
	}
krh@0
   606
krh@0
   607
	XML_ParserFree(parser);
krh@0
   608
krh@0
   609
	SHA1_Init(&sha1);
krh@0
   610
	SHA1_Update(&sha1, p, stat.st_size);
krh@0
   611
	SHA1_Final(hash, &sha1);
krh@0
   612
krh@0
   613
	close(fd);
krh@0
   614
krh@0
   615
	snprintf(buf, sizeof buf, "set/%s", sha1_to_hex(hash));
krh@0
   616
	if (write_to_file(buf, p, stat.st_size) < 0)
krh@0
   617
		return -1;
krh@0
   618
	munmap(p, stat.st_size);
krh@0
   619
krh@0
   620
	return 0;
krh@0
   621
}
krh@0
   622
krh@9
   623
static struct razor_set *qsort_set;
krh@9
   624
krh@9
   625
static int
krh@9
   626
compare_packages(const void *p1, const void *p2)
krh@9
   627
{
krh@9
   628
	const struct razor_package *pkg1 = p1, *pkg2 = p2;
krh@9
   629
	char *pool = qsort_set->string_pool.data;
krh@9
   630
krh@9
   631
	return strcmp(&pool[pkg1->name], &pool[pkg2->name]);
krh@9
   632
}
krh@9
   633
krh@9
   634
static int
krh@9
   635
compare_properties(const void *p1, const void *p2)
krh@9
   636
{
krh@10
   637
	const struct import_property *prop1 = p1, *prop2 = p2;
krh@9
   638
	char *pool = qsort_set->string_pool.data;
krh@12
   639
	int result;
krh@9
   640
krh@12
   641
	result = strcmp(&pool[prop1->name], &pool[prop2->name]);
krh@12
   642
	if (result == 0)
krh@12
   643
		return strcmp(&pool[prop1->version], &pool[prop2->version]);
krh@12
   644
	else
krh@12
   645
		return result;
krh@9
   646
}
krh@9
   647
krh@10
   648
static unsigned long *
krh@9
   649
uniqueify_properties(struct array *in, struct array *out)
krh@9
   650
{
krh@10
   651
	struct import_property *ip, *end;
krh@10
   652
	struct razor_property *rp;
krh@10
   653
	unsigned long *map;
krh@10
   654
	int i, count;
krh@9
   655
krh@10
   656
	count = in->size / sizeof(struct import_property);
krh@10
   657
	qsort(in->data, count,
krh@10
   658
	      sizeof(struct import_property), compare_properties);
krh@9
   659
krh@10
   660
	rp = NULL;
krh@9
   661
	end = in->data + in->size;
krh@10
   662
	for (ip = in->data; ip < end; ip++) {
krh@10
   663
		if (rp == NULL ||
krh@10
   664
		    ip->name != rp->name || ip->version != rp->version) {
krh@10
   665
			rp = array_add(out, sizeof *rp);
krh@10
   666
			rp->name = ip->name;
krh@10
   667
			rp->version = ip->version;
krh@10
   668
		}
krh@10
   669
		ip->unique_index = rp - (struct razor_property *) out->data;
krh@10
   670
	}
krh@9
   671
krh@10
   672
	map = malloc(count * sizeof (unsigned long));
krh@10
   673
	ip = in->data;
krh@10
   674
	for (i = 0; i < count; i++)
krh@10
   675
		map[ip[i].index] = ip[i].unique_index;
krh@10
   676
krh@10
   677
	return map;
krh@10
   678
}
krh@10
   679
krh@10
   680
static void
krh@10
   681
sort_packages(struct import_context *ctx)
krh@10
   682
{
krh@10
   683
	struct razor_package *p, *end;
krh@10
   684
	unsigned long *pool, *r;
krh@10
   685
krh@10
   686
	pool = ctx->set->property_pool.data;
krh@10
   687
	end = ctx->set->packages.data + ctx->set->packages.size;
krh@10
   688
	for (p = ctx->set->packages.data; p < end; p++) {
krh@10
   689
		for (r = &pool[p->requires]; *r; r++)
krh@10
   690
			*r = ctx->requires_map[*r];
krh@10
   691
		for (r = &pool[p->provides]; *r; r++)
krh@10
   692
			*r = ctx->provides_map[*r];
krh@9
   693
	}
krh@10
   694
krh@10
   695
	qsort(ctx->set->packages.data,
krh@10
   696
	      ctx->set->packages.size / sizeof(struct razor_package),
krh@10
   697
	      sizeof(struct razor_package), compare_packages);
krh@9
   698
}
krh@9
   699
krh@13
   700
static struct razor_set *
krh@13
   701
razor_finish_import(struct import_context *ctx)
krh@9
   702
{
krh@13
   703
	struct razor_package *pkg;
krh@13
   704
	struct razor_property *prop;
krh@13
   705
krh@9
   706
	qsort_set = ctx->set;
krh@9
   707
krh@10
   708
	ctx->requires_map =
krh@10
   709
		uniqueify_properties(&ctx->requires.all, &ctx->set->requires);
krh@10
   710
	ctx->provides_map =
krh@10
   711
		uniqueify_properties(&ctx->provides.all, &ctx->set->provides);
krh@9
   712
krh@10
   713
	sort_packages(ctx);
krh@10
   714
krh@10
   715
	free(ctx->requires.all.data);
krh@10
   716
	free(ctx->provides.all.data);
krh@10
   717
	free(ctx->requires_map);
krh@10
   718
	free(ctx->provides_map);
krh@9
   719
krh@13
   720
	/* FIXME: We add sentinel package/props here, but we should
krh@13
   721
	 * probably just have a size field in the header section. */
krh@13
   722
	pkg = array_add(&ctx->set->packages, sizeof *pkg);
krh@13
   723
	pkg->name = 0;
krh@13
   724
	prop = array_add(&ctx->set->requires, sizeof *prop);
krh@13
   725
	prop->name = 0;
krh@13
   726
	prop = array_add(&ctx->set->provides, sizeof *prop);
krh@13
   727
	prop->name = 0;
krh@13
   728
		
krh@9
   729
	fprintf(stderr, "parsed %d requires, %d unique\n",
krh@10
   730
		ctx->requires.all.size / sizeof(struct import_property),
krh@9
   731
		ctx->set->requires.size / sizeof(struct razor_property));
krh@9
   732
	fprintf(stderr, "parsed %d provides, %d unique\n",
krh@10
   733
		ctx->provides.all.size / sizeof(struct import_property),
krh@9
   734
		ctx->set->provides.size / sizeof(struct razor_property));
krh@13
   735
krh@13
   736
	return ctx->set; 
krh@9
   737
}
krh@9
   738
krh@0
   739
void
krh@4
   740
razor_set_list(struct razor_set *set)
krh@3
   741
{
krh@6
   742
	struct razor_package *p, *end;
krh@6
   743
	char *pool;
krh@3
   744
krh@6
   745
	pool = set->string_pool.data;
krh@6
   746
	end = set->packages.data + set->packages.size;
krh@6
   747
	for (p = set->packages.data; p < end && p->name; p++)
krh@6
   748
		printf("%s %s\n", &pool[p->name], &pool[p->version]);
krh@3
   749
}
krh@3
   750
krh@10
   751
struct razor_package *
krh@10
   752
razor_set_get_package(struct razor_set *set, const char *package)
krh@10
   753
{
krh@10
   754
	unsigned long name;
krh@10
   755
	struct razor_package *p, *end;
krh@10
   756
krh@10
   757
	name = razor_set_lookup(set, package);
krh@10
   758
	end = set->packages.data + set->packages.size;
krh@10
   759
	for (p = set->packages.data; p < end && p->name; p++)
krh@10
   760
		if (p->name == name)
krh@10
   761
			return p;
krh@10
   762
krh@10
   763
	return NULL;
krh@10
   764
}
krh@10
   765
krh@10
   766
static void
krh@10
   767
razor_set_list_all_properties(struct razor_set *set, struct array *properties)
krh@8
   768
{
krh@8
   769
	struct razor_property *p, *end;
krh@8
   770
	char *pool;
krh@8
   771
krh@8
   772
	pool = set->string_pool.data;
krh@10
   773
	end = properties->data + properties->size;
krh@10
   774
	for (p = properties->data; p < end && p->name; p++)
krh@8
   775
		printf("%s %s\n", &pool[p->name], &pool[p->version]);
krh@8
   776
}
krh@8
   777
krh@8
   778
void
krh@10
   779
razor_set_list_requires(struct razor_set *set, const char *name)
krh@7
   780
{
krh@10
   781
	struct razor_property *p, *requires;
krh@10
   782
	struct razor_package *package;
krh@10
   783
	unsigned long *r;
krh@7
   784
	char *pool;
krh@7
   785
krh@10
   786
	if (name) {
krh@10
   787
		package = razor_set_get_package(set, name);
krh@10
   788
		r = (unsigned long *) set->property_pool.data +
krh@10
   789
			package->requires;
krh@10
   790
		requires = set->requires.data;
krh@10
   791
		pool = set->string_pool.data;
krh@10
   792
		while (*r) {
krh@10
   793
			p = &requires[*r++];
krh@10
   794
			printf("%s %s\n", &pool[p->name], &pool[p->version]);
krh@10
   795
		}
krh@10
   796
	} else
krh@10
   797
		razor_set_list_all_properties(set, &set->requires);
krh@10
   798
}
krh@10
   799
krh@10
   800
void
krh@10
   801
razor_set_list_provides(struct razor_set *set, const char *name)
krh@10
   802
{
krh@10
   803
	struct razor_property *p, *provides;
krh@10
   804
	struct razor_package *package;
krh@10
   805
	unsigned long *r;
krh@10
   806
	char *pool;
krh@10
   807
krh@10
   808
	if (name) {
krh@10
   809
		package = razor_set_get_package(set, name);
krh@10
   810
		r = (unsigned long *) set->property_pool.data +
krh@10
   811
			package->provides;
krh@10
   812
		provides = set->provides.data;
krh@10
   813
		pool = set->string_pool.data;
krh@10
   814
		while (*r) {
krh@10
   815
			p = &provides[*r++];
krh@10
   816
			printf("%s %s\n", &pool[p->name], &pool[p->version]);
krh@10
   817
		}
krh@10
   818
	} else 
krh@10
   819
		razor_set_list_all_properties(set, &set->provides);
krh@7
   820
}
krh@7
   821
krh@7
   822
void
krh@4
   823
razor_set_info(struct razor_set *set)
krh@3
   824
{
krh@3
   825
	unsigned int offset, size;
krh@3
   826
	int i;
krh@3
   827
krh@4
   828
	for (i = 0; i < set->header->sections[i].type; i++) {
krh@4
   829
		offset = set->header->sections[i].offset;
krh@4
   830
		size = set->header->sections[i + 1].offset - offset;
krh@3
   831
krh@4
   832
		switch (set->header->sections[i].type) {
krh@4
   833
		case RAZOR_BUCKETS:
krh@3
   834
			printf("bucket section:\t\t%dkb\n", size / 1024);
krh@3
   835
			break;
krh@4
   836
		case RAZOR_STRINGS:
krh@3
   837
			printf("string pool:\t\t%dkb\n", size / 1024);
krh@3
   838
			break;
krh@4
   839
		case RAZOR_PACKAGES:
krh@3
   840
			printf("package section:\t%dkb\n", size / 1024);
krh@3
   841
			break;
krh@8
   842
		case RAZOR_REQUIRES:
krh@8
   843
			printf("requires section:\t%dkb\n", size / 1024);
krh@8
   844
			break;
krh@7
   845
		case RAZOR_PROVIDES:
krh@7
   846
			printf("provides section:\t%dkb\n", size / 1024);
krh@7
   847
			break;
krh@3
   848
		}
krh@3
   849
	}
krh@0
   850
}
krh@0
   851
krh@0
   852
static int
krh@0
   853
usage(void)
krh@0
   854
{
krh@7
   855
	printf("usage: razor [ import FILES | lookup <key> | "
krh@8
   856
	       "list | list-requires | list-provides | info ]\n");
krh@0
   857
	exit(1);
krh@0
   858
}
krh@0
   859
krh@0
   860
static const char repo_filename[] = "system.repo";
krh@0
   861
krh@0
   862
int
krh@0
   863
main(int argc, char *argv[])
krh@0
   864
{
krh@0
   865
	int i;
krh@4
   866
	struct razor_set *set;
krh@0
   867
	struct stat statbuf;
krh@9
   868
	struct import_context ctx;
krh@0
   869
krh@3
   870
	if (argc < 2) {
krh@0
   871
		usage();
krh@0
   872
	} else if (strcmp(argv[1], "import") == 0) {
krh@0
   873
		if (stat("set", &statbuf) && mkdir("set", 0777)) {
krh@0
   874
			fprintf(stderr, "could not create directory 'set'\n");
krh@0
   875
			exit(-1);
krh@0
   876
		}
krh@0
   877
			
krh@13
   878
		razor_prepare_import(&ctx);
krh@9
   879
krh@0
   880
		for (i = 2; i < argc; i++) {
krh@13
   881
			if (razor_import(&ctx, argv[i]) < 0) {
krh@0
   882
				fprintf(stderr, "failed to import %s\n",
krh@0
   883
					argv[i]);
krh@0
   884
				exit(-1);
krh@0
   885
			}
krh@0
   886
		}
krh@0
   887
krh@13
   888
		set = razor_finish_import(&ctx);
krh@6
   889
krh@6
   890
		printf("bucket allocation: %d\n", set->buckets.alloc);
krh@6
   891
		printf("pool size: %d\n", set->string_pool.size);
krh@6
   892
		printf("pool allocation: %d\n", set->string_pool.alloc);
krh@7
   893
		printf("packages: %d\n",
krh@7
   894
		       set->packages.size / sizeof(struct razor_package));
krh@8
   895
		printf("requires: %d\n",
krh@8
   896
		       set->requires.size / sizeof(struct razor_property));
krh@7
   897
		printf("provides: %d\n",
krh@8
   898
		       set->provides.size / sizeof(struct razor_property));
krh@0
   899
krh@4
   900
		razor_set_write(set, repo_filename);
krh@0
   901
krh@4
   902
		razor_set_destroy(set);
krh@0
   903
	} else if (strcmp(argv[1], "lookup") == 0) {
krh@4
   904
		set = razor_set_open(repo_filename);
krh@0
   905
		printf("%s is %lu\n", argv[2],
krh@4
   906
		       razor_set_lookup(set, argv[2]));
krh@4
   907
		razor_set_destroy(set);
krh@3
   908
	} else if (strcmp(argv[1], "list") == 0) {
krh@4
   909
		set = razor_set_open(repo_filename);
krh@4
   910
		razor_set_list(set);
krh@4
   911
		razor_set_destroy(set);
krh@8
   912
	} else if (strcmp(argv[1], "list-requires") == 0) {
krh@8
   913
		set = razor_set_open(repo_filename);
krh@10
   914
		razor_set_list_requires(set, argv[2]);
krh@8
   915
		razor_set_destroy(set);
krh@7
   916
	} else if (strcmp(argv[1], "list-provides") == 0) {
krh@7
   917
		set = razor_set_open(repo_filename);
krh@10
   918
		razor_set_list_provides(set, argv[2]);
krh@7
   919
		razor_set_destroy(set);
krh@3
   920
	} else if (strcmp(argv[1], "info") == 0) {
krh@4
   921
		set = razor_set_open(repo_filename);
krh@4
   922
		razor_set_info(set);
krh@4
   923
		razor_set_destroy(set);
krh@0
   924
	} else {
krh@0
   925
		usage();
krh@0
   926
	}
krh@0
   927
krh@0
   928
	return 0;
krh@0
   929
}