razor.c
author Kristian H?gsberg <krh@redhat.com>
Fri Sep 07 12:11:37 2007 -0400 (2007-09-07)
changeset 14 d21d74d6e297
parent 13 6a715b310792
child 15 228bd47c5dc4
permissions -rw-r--r--
Add a size field to the section headers to avoid sentinels.
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@14
   102
	struct { unsigned int type, offset, size; } 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@14
   171
		size = set->header->sections[i].size;
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@14
   255
	header->sections[0].size = set->buckets.alloc;
krh@0
   256
krh@4
   257
	header->sections[1].type = RAZOR_STRINGS;
krh@6
   258
	header->sections[1].offset =
krh@6
   259
		header->sections[0].offset + set->buckets.alloc;
krh@14
   260
	header->sections[1].size = set->string_pool.size;
krh@0
   261
krh@4
   262
	header->sections[2].type = RAZOR_PACKAGES;
krh@6
   263
	header->sections[2].offset =
krh@6
   264
		header->sections[1].offset + pool_size;
krh@14
   265
	header->sections[2].size = set->packages.size;
krh@3
   266
krh@8
   267
	header->sections[3].type = RAZOR_REQUIRES;
krh@6
   268
	header->sections[3].offset =
krh@6
   269
		header->sections[2].offset + packages_size;
krh@14
   270
	header->sections[3].size = set->requires.size;
krh@0
   271
krh@8
   272
	header->sections[4].type = RAZOR_PROVIDES;
krh@7
   273
	header->sections[4].offset =
krh@8
   274
		header->sections[3].offset + requires_size;
krh@14
   275
	header->sections[4].size = set->provides.size;
krh@8
   276
krh@10
   277
	header->sections[5].type = RAZOR_PROPERTIES;
krh@8
   278
	header->sections[5].offset =
krh@8
   279
		header->sections[4].offset + provides_size;
krh@14
   280
	header->sections[5].size = set->property_pool.size;
krh@7
   281
krh@10
   282
	header->sections[6].type = 0;
krh@10
   283
	header->sections[6].offset =
krh@10
   284
		header->sections[5].offset + properties_size;
krh@14
   285
	header->sections[6].size = 0;
krh@10
   286
krh@0
   287
	fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666);
krh@0
   288
	if (fd < 0)
krh@0
   289
		return -1;
krh@0
   290
krh@0
   291
	write_to_fd(fd, data, sizeof data);
krh@6
   292
	write_to_fd(fd, set->buckets.data, set->buckets.alloc);
krh@6
   293
	write_to_fd(fd, set->string_pool.data, pool_size);
krh@6
   294
	write_to_fd(fd, set->packages.data, packages_size);
krh@8
   295
	write_to_fd(fd, set->requires.data, requires_size);
krh@7
   296
	write_to_fd(fd, set->provides.data, provides_size);
krh@10
   297
	write_to_fd(fd, set->property_pool.data, properties_size);
krh@0
   298
krh@0
   299
	return 0;
krh@0
   300
}
krh@0
   301
krh@0
   302
static unsigned int
krh@0
   303
hash_string(const char *key)
krh@0
   304
{
krh@0
   305
	const char *p;
krh@0
   306
	unsigned int hash = 0;
krh@0
   307
krh@0
   308
	for (p = key; *p; p++)
krh@9
   309
		hash = (hash * 617) ^ *p;
krh@0
   310
krh@0
   311
	return hash;
krh@0
   312
}
krh@0
   313
krh@0
   314
unsigned long
krh@4
   315
razor_set_lookup(struct razor_set *set, const char *key)
krh@0
   316
{
krh@6
   317
	unsigned int mask, start, i;
krh@6
   318
	unsigned long *b;
krh@6
   319
	char *pool;
krh@0
   320
krh@6
   321
	pool = set->string_pool.data;
krh@6
   322
	mask = set->buckets.alloc - 1;
krh@6
   323
	start = hash_string(key) * sizeof(unsigned long);
krh@0
   324
krh@6
   325
	for (i = 0; i < set->buckets.alloc; i += sizeof *b) {
krh@6
   326
		b = set->buckets.data + ((start + i) & mask);
krh@6
   327
krh@6
   328
		if (*b == 0)
krh@0
   329
			return 0;
krh@0
   330
krh@6
   331
		if (strcmp(key, &pool[*b]) == 0)
krh@6
   332
			return *b;
krh@6
   333
	}
krh@0
   334
krh@0
   335
	return 0;
krh@0
   336
}
krh@0
   337
krh@0
   338
static unsigned long
krh@4
   339
add_to_string_pool(struct razor_set *set, const char *key)
krh@0
   340
{
krh@6
   341
	int len;
krh@6
   342
	char *p;
krh@0
   343
krh@0
   344
	len = strlen(key) + 1;
krh@6
   345
	p = array_add(&set->string_pool, len);
krh@6
   346
	memcpy(p, key, len);
krh@0
   347
krh@6
   348
	return p - (char *) set->string_pool.data;
krh@0
   349
}
krh@0
   350
krh@10
   351
static unsigned long
krh@10
   352
add_to_property_pool(struct razor_set *set, struct array *properties)
krh@10
   353
{
krh@10
   354
	unsigned long  *p;
krh@10
   355
krh@10
   356
	p = array_add(properties, sizeof *p);
krh@10
   357
	*p = 0;
krh@10
   358
	p = array_add(&set->property_pool, properties->size);
krh@10
   359
	memcpy(p, properties->data, properties->size);
krh@10
   360
krh@10
   361
	return p - (unsigned long *) set->property_pool.data;
krh@10
   362
}
krh@10
   363
krh@0
   364
static void
krh@4
   365
do_insert(struct razor_set *set, unsigned long value)
krh@0
   366
{
krh@6
   367
	unsigned int mask, start, i;
krh@6
   368
	unsigned long *b;
krh@0
   369
	const char *key;
krh@0
   370
krh@6
   371
	key = (char *) set->string_pool.data + value;
krh@6
   372
	mask = set->buckets.alloc - 1;
krh@6
   373
	start = hash_string(key) * sizeof(unsigned long);
krh@6
   374
krh@6
   375
	for (i = 0; i < set->buckets.alloc; i += sizeof *b) {
krh@6
   376
		b = set->buckets.data + ((start + i) & mask);
krh@6
   377
		if (*b == 0) {
krh@6
   378
			*b = value;
krh@0
   379
			break;
krh@0
   380
		}
krh@6
   381
	}
krh@0
   382
}
krh@0
   383
krh@0
   384
unsigned long
krh@4
   385
razor_set_insert(struct razor_set *set, const char *key)
krh@0
   386
{
krh@6
   387
	unsigned long value, *buckets, *b, *end;
krh@6
   388
	int alloc;
krh@0
   389
krh@6
   390
	alloc = set->buckets.alloc;
krh@6
   391
	array_add(&set->buckets, 4 * sizeof *buckets);
krh@6
   392
	if (alloc != set->buckets.alloc) {
krh@6
   393
		end = set->buckets.data + alloc;
krh@6
   394
		memset(end, 0, set->buckets.alloc - alloc);
krh@6
   395
		for (b = set->buckets.data; b < end; b++) {
krh@6
   396
			value = *b;
krh@6
   397
			if (value != 0) {
krh@6
   398
				*b = 0;
krh@4
   399
				do_insert(set, value);
krh@6
   400
			}
krh@0
   401
		}
krh@0
   402
	}
krh@0
   403
krh@4
   404
	value = add_to_string_pool(set, key);
krh@4
   405
	do_insert (set, value);
krh@0
   406
krh@0
   407
	return value;
krh@0
   408
}
krh@0
   409
krh@0
   410
unsigned long
krh@4
   411
razor_set_tokenize(struct razor_set *set, const char *string)
krh@0
   412
{
krh@0
   413
	unsigned long token;
krh@0
   414
krh@13
   415
	if (string == NULL)
krh@13
   416
		return 0;
krh@13
   417
krh@4
   418
	token = razor_set_lookup(set, string);
krh@0
   419
	if (token != 0)
krh@0
   420
		return token;
krh@0
   421
krh@4
   422
	return razor_set_insert(set, string);
krh@0
   423
}
krh@0
   424
krh@13
   425
struct import_property_context {
krh@10
   426
	struct array all;
krh@10
   427
	struct array package;
krh@10
   428
};
krh@10
   429
krh@9
   430
struct import_context {
krh@4
   431
	struct razor_set *set;
krh@13
   432
	struct import_property_context requires;
krh@13
   433
	struct import_property_context provides;
krh@9
   434
	unsigned long package;
krh@10
   435
	unsigned long *requires_map;
krh@10
   436
	unsigned long *provides_map;
krh@10
   437
};
krh@10
   438
krh@10
   439
struct import_property {
krh@10
   440
	unsigned long name;
krh@10
   441
	unsigned long version;
krh@10
   442
	unsigned long package;
krh@10
   443
	unsigned long index;
krh@10
   444
	unsigned long unique_index;
krh@0
   445
};
krh@0
   446
krh@0
   447
static void
krh@13
   448
import_context_add_package(struct import_context *ctx,
krh@13
   449
			   const char *name, const char *version)
krh@13
   450
{
krh@13
   451
	struct razor_package *p;
krh@13
   452
krh@13
   453
	p = array_add(&ctx->set->packages, sizeof *p);
krh@13
   454
	p->name = razor_set_tokenize(ctx->set, name);
krh@13
   455
	p->version = razor_set_tokenize(ctx->set, version);
krh@13
   456
krh@13
   457
	ctx->package = p - (struct razor_package *) ctx->set->packages.data;
krh@13
   458
	array_init(&ctx->requires.package);
krh@13
   459
	array_init(&ctx->provides.package);
krh@13
   460
}
krh@13
   461
krh@13
   462
void
krh@13
   463
import_context_finish_package(struct import_context *ctx)
krh@13
   464
{
krh@13
   465
	struct razor_package *p;
krh@13
   466
krh@13
   467
	p = (struct razor_package *) ctx->set->packages.data + ctx->package;
krh@13
   468
	p->requires = add_to_property_pool(ctx->set, &ctx->requires.package);
krh@13
   469
	p->provides = add_to_property_pool(ctx->set, &ctx->provides.package);
krh@13
   470
krh@13
   471
	array_release(&ctx->requires.package);
krh@13
   472
	array_release(&ctx->provides.package);
krh@13
   473
}
krh@13
   474
krh@13
   475
static void
krh@13
   476
import_context_add_property(struct import_context *ctx,
krh@13
   477
			    struct import_property_context *pctx,
krh@13
   478
			    const char *name, const char *version)
krh@13
   479
{
krh@13
   480
	struct import_property *p;
krh@13
   481
	unsigned long *r;
krh@13
   482
krh@13
   483
	p = array_add(&pctx->all, sizeof *p);
krh@13
   484
	p->name = razor_set_tokenize(ctx->set, name);
krh@13
   485
	p->version = razor_set_tokenize(ctx->set, version);
krh@13
   486
	p->package = ctx->package;
krh@13
   487
	p->index = p - (struct import_property *) pctx->all.data;
krh@13
   488
krh@13
   489
	r = array_add(&pctx->package, sizeof *r);
krh@13
   490
	*r = p->index;
krh@13
   491
}
krh@13
   492
krh@13
   493
static void
krh@9
   494
parse_package(struct import_context *ctx, const char **atts, void *data)
krh@3
   495
{
krh@13
   496
	const char *name = NULL, *version = NULL;
krh@3
   497
	int i;
krh@3
   498
krh@3
   499
	for (i = 0; atts[i]; i += 2) {
krh@3
   500
		if (strcmp(atts[i], "name") == 0)
krh@13
   501
			name = atts[i + 1];
krh@3
   502
		else if (strcmp(atts[i], "version") == 0)
krh@13
   503
			version = atts[i + 1];
krh@3
   504
	}
krh@3
   505
krh@13
   506
	if (name == NULL || version == NULL) {
krh@3
   507
		fprintf(stderr, "invalid package tag, "
krh@3
   508
			"missing name or version attributes\n");
krh@3
   509
		return;
krh@3
   510
	}
krh@3
   511
krh@13
   512
	import_context_add_package(ctx, name, version);
krh@3
   513
}
krh@3
   514
krh@3
   515
static void
krh@9
   516
parse_property(struct import_context *ctx, const char **atts, void *data)
krh@8
   517
{
krh@13
   518
	const char *name = NULL, *version = NULL;
krh@8
   519
	int i;
krh@8
   520
krh@8
   521
	for (i = 0; atts[i]; i += 2) {
krh@8
   522
		if (strcmp(atts[i], "name") == 0)
krh@13
   523
			name = atts[i + 1];
krh@9
   524
		if (strcmp(atts[i], "version") == 0)
krh@13
   525
			version = atts[i + 1];
krh@8
   526
	}
krh@8
   527
	
krh@13
   528
	if (name == NULL) {
krh@9
   529
		fprintf(stderr, "invalid tag, missing name attribute\n");
krh@8
   530
		return;
krh@8
   531
	}
krh@8
   532
krh@13
   533
	import_context_add_property(ctx, data, name, version);
krh@7
   534
}
krh@7
   535
krh@7
   536
static void
krh@0
   537
start_element(void *data, const char *name, const char **atts)
krh@0
   538
{
krh@9
   539
	struct import_context *ctx = data;
krh@0
   540
krh@3
   541
	if (strcmp(name, "package") == 0)
krh@9
   542
		parse_package(ctx, atts, NULL);
krh@8
   543
	else if (strcmp(name, "requires") == 0)
krh@9
   544
		parse_property(ctx, atts, &ctx->requires);
krh@7
   545
	else if (strcmp(name, "provides") == 0)
krh@9
   546
		parse_property(ctx, atts, &ctx->provides);
krh@0
   547
}
krh@0
   548
krh@0
   549
static void
krh@0
   550
end_element (void *data, const char *name)
krh@0
   551
{
krh@9
   552
	struct import_context *ctx = data;
krh@4
   553
krh@13
   554
	if (strcmp(name, "package") == 0)
krh@13
   555
		import_context_finish_package(ctx);
krh@0
   556
}
krh@0
   557
krh@0
   558
static char *
krh@0
   559
sha1_to_hex(const unsigned char *sha1)
krh@0
   560
{
krh@0
   561
	static int bufno;
krh@0
   562
	static char hexbuffer[4][50];
krh@0
   563
	static const char hex[] = "0123456789abcdef";
krh@0
   564
	char *buffer = hexbuffer[3 & ++bufno], *buf = buffer;
krh@0
   565
	int i;
krh@0
   566
krh@0
   567
	for (i = 0; i < 20; i++) {
krh@0
   568
		unsigned int val = *sha1++;
krh@0
   569
		*buf++ = hex[val >> 4];
krh@0
   570
		*buf++ = hex[val & 0xf];
krh@0
   571
	}
krh@0
   572
	*buf = '\0';
krh@0
   573
krh@0
   574
	return buffer;
krh@0
   575
}
krh@0
   576
krh@9
   577
static void
krh@13
   578
razor_prepare_import(struct import_context *ctx)
krh@9
   579
{
krh@9
   580
	memset(ctx, 0, sizeof *ctx);
krh@13
   581
	ctx->set = razor_set_create();
krh@9
   582
}
krh@9
   583
krh@0
   584
static int
krh@13
   585
razor_import(struct import_context *ctx, const char *filename)
krh@0
   586
{
krh@0
   587
	SHA_CTX sha1;
krh@0
   588
	XML_Parser parser;
krh@0
   589
	int fd;
krh@0
   590
	void *p;
krh@0
   591
	struct stat stat;
krh@0
   592
	char buf[128];
krh@0
   593
	unsigned char hash[20];
krh@0
   594
krh@0
   595
	fd = open(filename, O_RDONLY);
krh@0
   596
	if (fstat(fd, &stat) < 0)
krh@0
   597
		return -1;
krh@0
   598
	p = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
krh@0
   599
	if (p == MAP_FAILED)
krh@0
   600
		return -1;
krh@0
   601
krh@0
   602
	parser = XML_ParserCreate(NULL);
krh@9
   603
	XML_SetUserData(parser, ctx);
krh@0
   604
	XML_SetElementHandler(parser, start_element, end_element);
krh@0
   605
	if (XML_Parse(parser, p, stat.st_size, 1) == XML_STATUS_ERROR) {
krh@0
   606
		fprintf(stderr,
krh@0
   607
			"%s at line %d, %s\n",
krh@0
   608
			XML_ErrorString(XML_GetErrorCode(parser)),
krh@0
   609
			XML_GetCurrentLineNumber(parser),
krh@0
   610
			filename);
krh@0
   611
		return 1;
krh@0
   612
	}
krh@0
   613
krh@0
   614
	XML_ParserFree(parser);
krh@0
   615
krh@0
   616
	SHA1_Init(&sha1);
krh@0
   617
	SHA1_Update(&sha1, p, stat.st_size);
krh@0
   618
	SHA1_Final(hash, &sha1);
krh@0
   619
krh@0
   620
	close(fd);
krh@0
   621
krh@0
   622
	snprintf(buf, sizeof buf, "set/%s", sha1_to_hex(hash));
krh@0
   623
	if (write_to_file(buf, p, stat.st_size) < 0)
krh@0
   624
		return -1;
krh@0
   625
	munmap(p, stat.st_size);
krh@0
   626
krh@0
   627
	return 0;
krh@0
   628
}
krh@0
   629
krh@9
   630
static struct razor_set *qsort_set;
krh@9
   631
krh@9
   632
static int
krh@9
   633
compare_packages(const void *p1, const void *p2)
krh@9
   634
{
krh@9
   635
	const struct razor_package *pkg1 = p1, *pkg2 = p2;
krh@9
   636
	char *pool = qsort_set->string_pool.data;
krh@9
   637
krh@9
   638
	return strcmp(&pool[pkg1->name], &pool[pkg2->name]);
krh@9
   639
}
krh@9
   640
krh@9
   641
static int
krh@9
   642
compare_properties(const void *p1, const void *p2)
krh@9
   643
{
krh@10
   644
	const struct import_property *prop1 = p1, *prop2 = p2;
krh@9
   645
	char *pool = qsort_set->string_pool.data;
krh@12
   646
	int result;
krh@9
   647
krh@12
   648
	result = strcmp(&pool[prop1->name], &pool[prop2->name]);
krh@12
   649
	if (result == 0)
krh@12
   650
		return strcmp(&pool[prop1->version], &pool[prop2->version]);
krh@12
   651
	else
krh@12
   652
		return result;
krh@9
   653
}
krh@9
   654
krh@10
   655
static unsigned long *
krh@9
   656
uniqueify_properties(struct array *in, struct array *out)
krh@9
   657
{
krh@10
   658
	struct import_property *ip, *end;
krh@10
   659
	struct razor_property *rp;
krh@10
   660
	unsigned long *map;
krh@10
   661
	int i, count;
krh@9
   662
krh@10
   663
	count = in->size / sizeof(struct import_property);
krh@10
   664
	qsort(in->data, count,
krh@10
   665
	      sizeof(struct import_property), compare_properties);
krh@9
   666
krh@10
   667
	rp = NULL;
krh@9
   668
	end = in->data + in->size;
krh@10
   669
	for (ip = in->data; ip < end; ip++) {
krh@10
   670
		if (rp == NULL ||
krh@10
   671
		    ip->name != rp->name || ip->version != rp->version) {
krh@10
   672
			rp = array_add(out, sizeof *rp);
krh@10
   673
			rp->name = ip->name;
krh@10
   674
			rp->version = ip->version;
krh@10
   675
		}
krh@10
   676
		ip->unique_index = rp - (struct razor_property *) out->data;
krh@10
   677
	}
krh@9
   678
krh@10
   679
	map = malloc(count * sizeof (unsigned long));
krh@10
   680
	ip = in->data;
krh@10
   681
	for (i = 0; i < count; i++)
krh@10
   682
		map[ip[i].index] = ip[i].unique_index;
krh@10
   683
krh@10
   684
	return map;
krh@10
   685
}
krh@10
   686
krh@10
   687
static void
krh@10
   688
sort_packages(struct import_context *ctx)
krh@10
   689
{
krh@10
   690
	struct razor_package *p, *end;
krh@10
   691
	unsigned long *pool, *r;
krh@10
   692
krh@10
   693
	pool = ctx->set->property_pool.data;
krh@10
   694
	end = ctx->set->packages.data + ctx->set->packages.size;
krh@10
   695
	for (p = ctx->set->packages.data; p < end; p++) {
krh@10
   696
		for (r = &pool[p->requires]; *r; r++)
krh@10
   697
			*r = ctx->requires_map[*r];
krh@10
   698
		for (r = &pool[p->provides]; *r; r++)
krh@10
   699
			*r = ctx->provides_map[*r];
krh@9
   700
	}
krh@10
   701
krh@10
   702
	qsort(ctx->set->packages.data,
krh@10
   703
	      ctx->set->packages.size / sizeof(struct razor_package),
krh@10
   704
	      sizeof(struct razor_package), compare_packages);
krh@9
   705
}
krh@9
   706
krh@13
   707
static struct razor_set *
krh@13
   708
razor_finish_import(struct import_context *ctx)
krh@9
   709
{
krh@9
   710
	qsort_set = ctx->set;
krh@9
   711
krh@10
   712
	ctx->requires_map =
krh@10
   713
		uniqueify_properties(&ctx->requires.all, &ctx->set->requires);
krh@10
   714
	ctx->provides_map =
krh@10
   715
		uniqueify_properties(&ctx->provides.all, &ctx->set->provides);
krh@9
   716
krh@10
   717
	sort_packages(ctx);
krh@10
   718
krh@10
   719
	free(ctx->requires.all.data);
krh@10
   720
	free(ctx->provides.all.data);
krh@10
   721
	free(ctx->requires_map);
krh@10
   722
	free(ctx->provides_map);
krh@13
   723
		
krh@9
   724
	fprintf(stderr, "parsed %d requires, %d unique\n",
krh@10
   725
		ctx->requires.all.size / sizeof(struct import_property),
krh@9
   726
		ctx->set->requires.size / sizeof(struct razor_property));
krh@9
   727
	fprintf(stderr, "parsed %d provides, %d unique\n",
krh@10
   728
		ctx->provides.all.size / sizeof(struct import_property),
krh@9
   729
		ctx->set->provides.size / sizeof(struct razor_property));
krh@13
   730
krh@13
   731
	return ctx->set; 
krh@9
   732
}
krh@9
   733
krh@0
   734
void
krh@4
   735
razor_set_list(struct razor_set *set)
krh@3
   736
{
krh@6
   737
	struct razor_package *p, *end;
krh@6
   738
	char *pool;
krh@3
   739
krh@6
   740
	pool = set->string_pool.data;
krh@6
   741
	end = set->packages.data + set->packages.size;
krh@14
   742
	for (p = set->packages.data; p < end; p++)
krh@6
   743
		printf("%s %s\n", &pool[p->name], &pool[p->version]);
krh@3
   744
}
krh@3
   745
krh@10
   746
struct razor_package *
krh@10
   747
razor_set_get_package(struct razor_set *set, const char *package)
krh@10
   748
{
krh@10
   749
	unsigned long name;
krh@10
   750
	struct razor_package *p, *end;
krh@10
   751
krh@10
   752
	name = razor_set_lookup(set, package);
krh@10
   753
	end = set->packages.data + set->packages.size;
krh@14
   754
	for (p = set->packages.data; p < end; p++)
krh@10
   755
		if (p->name == name)
krh@10
   756
			return p;
krh@10
   757
krh@10
   758
	return NULL;
krh@10
   759
}
krh@10
   760
krh@10
   761
static void
krh@10
   762
razor_set_list_all_properties(struct razor_set *set, struct array *properties)
krh@8
   763
{
krh@8
   764
	struct razor_property *p, *end;
krh@8
   765
	char *pool;
krh@8
   766
krh@8
   767
	pool = set->string_pool.data;
krh@10
   768
	end = properties->data + properties->size;
krh@14
   769
	for (p = properties->data; p < end; p++)
krh@8
   770
		printf("%s %s\n", &pool[p->name], &pool[p->version]);
krh@8
   771
}
krh@8
   772
krh@8
   773
void
krh@10
   774
razor_set_list_requires(struct razor_set *set, const char *name)
krh@7
   775
{
krh@10
   776
	struct razor_property *p, *requires;
krh@10
   777
	struct razor_package *package;
krh@10
   778
	unsigned long *r;
krh@7
   779
	char *pool;
krh@7
   780
krh@10
   781
	if (name) {
krh@10
   782
		package = razor_set_get_package(set, name);
krh@10
   783
		r = (unsigned long *) set->property_pool.data +
krh@10
   784
			package->requires;
krh@10
   785
		requires = set->requires.data;
krh@10
   786
		pool = set->string_pool.data;
krh@10
   787
		while (*r) {
krh@10
   788
			p = &requires[*r++];
krh@10
   789
			printf("%s %s\n", &pool[p->name], &pool[p->version]);
krh@10
   790
		}
krh@10
   791
	} else
krh@10
   792
		razor_set_list_all_properties(set, &set->requires);
krh@10
   793
}
krh@10
   794
krh@10
   795
void
krh@10
   796
razor_set_list_provides(struct razor_set *set, const char *name)
krh@10
   797
{
krh@10
   798
	struct razor_property *p, *provides;
krh@10
   799
	struct razor_package *package;
krh@10
   800
	unsigned long *r;
krh@10
   801
	char *pool;
krh@10
   802
krh@10
   803
	if (name) {
krh@10
   804
		package = razor_set_get_package(set, name);
krh@10
   805
		r = (unsigned long *) set->property_pool.data +
krh@10
   806
			package->provides;
krh@10
   807
		provides = set->provides.data;
krh@10
   808
		pool = set->string_pool.data;
krh@10
   809
		while (*r) {
krh@10
   810
			p = &provides[*r++];
krh@10
   811
			printf("%s %s\n", &pool[p->name], &pool[p->version]);
krh@10
   812
		}
krh@10
   813
	} else 
krh@10
   814
		razor_set_list_all_properties(set, &set->provides);
krh@7
   815
}
krh@7
   816
krh@7
   817
void
krh@4
   818
razor_set_info(struct razor_set *set)
krh@3
   819
{
krh@3
   820
	unsigned int offset, size;
krh@3
   821
	int i;
krh@3
   822
krh@4
   823
	for (i = 0; i < set->header->sections[i].type; i++) {
krh@4
   824
		offset = set->header->sections[i].offset;
krh@4
   825
		size = set->header->sections[i + 1].offset - offset;
krh@3
   826
krh@4
   827
		switch (set->header->sections[i].type) {
krh@4
   828
		case RAZOR_BUCKETS:
krh@3
   829
			printf("bucket section:\t\t%dkb\n", size / 1024);
krh@3
   830
			break;
krh@4
   831
		case RAZOR_STRINGS:
krh@3
   832
			printf("string pool:\t\t%dkb\n", size / 1024);
krh@3
   833
			break;
krh@4
   834
		case RAZOR_PACKAGES:
krh@3
   835
			printf("package section:\t%dkb\n", size / 1024);
krh@3
   836
			break;
krh@8
   837
		case RAZOR_REQUIRES:
krh@8
   838
			printf("requires section:\t%dkb\n", size / 1024);
krh@8
   839
			break;
krh@7
   840
		case RAZOR_PROVIDES:
krh@7
   841
			printf("provides section:\t%dkb\n", size / 1024);
krh@7
   842
			break;
krh@3
   843
		}
krh@3
   844
	}
krh@0
   845
}
krh@0
   846
krh@0
   847
static int
krh@0
   848
usage(void)
krh@0
   849
{
krh@7
   850
	printf("usage: razor [ import FILES | lookup <key> | "
krh@8
   851
	       "list | list-requires | list-provides | info ]\n");
krh@0
   852
	exit(1);
krh@0
   853
}
krh@0
   854
krh@0
   855
static const char repo_filename[] = "system.repo";
krh@0
   856
krh@0
   857
int
krh@0
   858
main(int argc, char *argv[])
krh@0
   859
{
krh@0
   860
	int i;
krh@4
   861
	struct razor_set *set;
krh@0
   862
	struct stat statbuf;
krh@9
   863
	struct import_context ctx;
krh@0
   864
krh@3
   865
	if (argc < 2) {
krh@0
   866
		usage();
krh@0
   867
	} else if (strcmp(argv[1], "import") == 0) {
krh@0
   868
		if (stat("set", &statbuf) && mkdir("set", 0777)) {
krh@0
   869
			fprintf(stderr, "could not create directory 'set'\n");
krh@0
   870
			exit(-1);
krh@0
   871
		}
krh@0
   872
			
krh@13
   873
		razor_prepare_import(&ctx);
krh@9
   874
krh@0
   875
		for (i = 2; i < argc; i++) {
krh@13
   876
			if (razor_import(&ctx, argv[i]) < 0) {
krh@0
   877
				fprintf(stderr, "failed to import %s\n",
krh@0
   878
					argv[i]);
krh@0
   879
				exit(-1);
krh@0
   880
			}
krh@0
   881
		}
krh@0
   882
krh@13
   883
		set = razor_finish_import(&ctx);
krh@6
   884
krh@6
   885
		printf("bucket allocation: %d\n", set->buckets.alloc);
krh@6
   886
		printf("pool size: %d\n", set->string_pool.size);
krh@6
   887
		printf("pool allocation: %d\n", set->string_pool.alloc);
krh@7
   888
		printf("packages: %d\n",
krh@7
   889
		       set->packages.size / sizeof(struct razor_package));
krh@8
   890
		printf("requires: %d\n",
krh@8
   891
		       set->requires.size / sizeof(struct razor_property));
krh@7
   892
		printf("provides: %d\n",
krh@8
   893
		       set->provides.size / sizeof(struct razor_property));
krh@0
   894
krh@4
   895
		razor_set_write(set, repo_filename);
krh@0
   896
krh@4
   897
		razor_set_destroy(set);
krh@0
   898
	} else if (strcmp(argv[1], "lookup") == 0) {
krh@4
   899
		set = razor_set_open(repo_filename);
krh@0
   900
		printf("%s is %lu\n", argv[2],
krh@4
   901
		       razor_set_lookup(set, argv[2]));
krh@4
   902
		razor_set_destroy(set);
krh@3
   903
	} else if (strcmp(argv[1], "list") == 0) {
krh@4
   904
		set = razor_set_open(repo_filename);
krh@4
   905
		razor_set_list(set);
krh@4
   906
		razor_set_destroy(set);
krh@8
   907
	} else if (strcmp(argv[1], "list-requires") == 0) {
krh@8
   908
		set = razor_set_open(repo_filename);
krh@10
   909
		razor_set_list_requires(set, argv[2]);
krh@8
   910
		razor_set_destroy(set);
krh@7
   911
	} else if (strcmp(argv[1], "list-provides") == 0) {
krh@7
   912
		set = razor_set_open(repo_filename);
krh@10
   913
		razor_set_list_provides(set, argv[2]);
krh@7
   914
		razor_set_destroy(set);
krh@3
   915
	} else if (strcmp(argv[1], "info") == 0) {
krh@4
   916
		set = razor_set_open(repo_filename);
krh@4
   917
		razor_set_info(set);
krh@4
   918
		razor_set_destroy(set);
krh@0
   919
	} else {
krh@0
   920
		usage();
krh@0
   921
	}
krh@0
   922
krh@0
   923
	return 0;
krh@0
   924
}