razor.c
author Kristian H?gsberg <krh@redhat.com>
Mon Sep 03 23:13:19 2007 -0400 (2007-09-03)
changeset 3 917677cdceb3
parent 0 e15eb9ef9c28
child 4 c8e9dbcfaf02
permissions -rw-r--r--
Add a simple package list to the repo.
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@0
    13
static int
krh@0
    14
write_to_fd(int fd, void *p, size_t size)
krh@0
    15
{
krh@0
    16
	int rest, len;
krh@0
    17
krh@0
    18
	rest = size;
krh@0
    19
	while (rest > 0) {
krh@0
    20
		len = write(fd, p, rest);
krh@0
    21
		if (len < 0)
krh@0
    22
			return -1;
krh@0
    23
		rest -= len;
krh@0
    24
	}
krh@0
    25
krh@0
    26
	return 0;
krh@0
    27
}
krh@0
    28
krh@0
    29
static int
krh@0
    30
write_to_file(const char *filename, void *p, size_t size)
krh@0
    31
{
krh@0
    32
	int fd, err;
krh@0
    33
krh@0
    34
	fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666);
krh@0
    35
	if (fd < 0)
krh@0
    36
		return -1;
krh@0
    37
	err = write_to_fd(fd, p, size);
krh@0
    38
	close(fd);
krh@0
    39
krh@0
    40
	return err;
krh@0
    41
}
krh@0
    42
krh@0
    43
struct hashtable_header {
krh@0
    44
	unsigned int magic;
krh@0
    45
	unsigned int version;
krh@0
    46
	struct { unsigned int type, offset; } sections[0];
krh@0
    47
};
krh@0
    48
krh@0
    49
#define HASHTABLE_MAGIC 0x7a7a7a7a
krh@0
    50
#define HASHTABLE_VERSION 1
krh@0
    51
#define HASHTABLE_BUCKETS 1
krh@0
    52
#define HASHTABLE_STRINGS 2
krh@3
    53
#define HASHTABLE_PACKAGES 3
krh@3
    54
krh@3
    55
struct package {
krh@3
    56
	unsigned long name;
krh@3
    57
	unsigned long version;
krh@3
    58
};
krh@0
    59
krh@0
    60
struct hashtable {
krh@0
    61
	unsigned long *buckets;
krh@0
    62
	int bucket_count, bucket_alloc;
krh@0
    63
	char *string_pool;
krh@0
    64
	int pool_size, pool_alloc;
krh@0
    65
	struct hashtable_header *header;
krh@3
    66
krh@3
    67
	struct package *packages;
krh@3
    68
	int package_count, package_alloc;
krh@0
    69
};
krh@0
    70
krh@0
    71
static void *
krh@0
    72
zalloc(size_t size)
krh@0
    73
{
krh@0
    74
	void *p;
krh@0
    75
krh@0
    76
	p = malloc(size);
krh@0
    77
	memset(p, 0, size);
krh@0
    78
krh@0
    79
	return p;
krh@0
    80
}
krh@0
    81
krh@0
    82
struct hashtable *
krh@0
    83
hashtable_create(void)
krh@0
    84
{
krh@0
    85
	struct hashtable *ht;
krh@0
    86
krh@0
    87
	ht = zalloc(sizeof *ht);
krh@0
    88
	ht->buckets = zalloc(4096 * sizeof *ht->buckets);
krh@0
    89
	ht->bucket_count = 0;
krh@0
    90
	ht->bucket_alloc = 4096;
krh@0
    91
krh@0
    92
	ht->string_pool = zalloc(4096);
krh@0
    93
	ht->pool_size = 1;
krh@0
    94
	ht->pool_alloc = 4096;
krh@0
    95
krh@3
    96
	ht->packages = zalloc(4096 * sizeof *ht->packages);
krh@3
    97
	ht->package_count = 0;
krh@3
    98
	ht->package_alloc = 4096;
krh@3
    99
krh@0
   100
	return ht;
krh@0
   101
}
krh@0
   102
krh@0
   103
struct hashtable *
krh@0
   104
hashtable_create_from_file(const char *filename)
krh@0
   105
{
krh@0
   106
	struct hashtable *ht;
krh@0
   107
	struct stat stat;
krh@0
   108
	unsigned int size, offset;
krh@0
   109
	int fd, i;
krh@0
   110
krh@0
   111
	ht = zalloc(sizeof *ht);
krh@0
   112
	fd = open(filename, O_RDONLY);
krh@0
   113
	if (fstat(fd, &stat) < 0)
krh@0
   114
		return NULL;
krh@0
   115
	ht->header = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
krh@0
   116
	if (ht->header == MAP_FAILED) {
krh@0
   117
		free(ht);
krh@0
   118
		return NULL;
krh@0
   119
	}
krh@0
   120
krh@0
   121
	for (i = 0; i < ht->header->sections[i].type; i++) {
krh@0
   122
		offset = ht->header->sections[i].offset;
krh@0
   123
		size = ht->header->sections[i + 1].offset - offset;
krh@0
   124
krh@0
   125
		switch (ht->header->sections[i].type) {
krh@0
   126
		case HASHTABLE_BUCKETS:
krh@0
   127
			ht->buckets = (void *) ht->header + offset;
krh@0
   128
			ht->bucket_count = size / sizeof *ht->buckets;
krh@0
   129
			ht->bucket_alloc = ht->bucket_count;
krh@0
   130
			break;
krh@0
   131
		case HASHTABLE_STRINGS:
krh@0
   132
			ht->string_pool = (void *) ht->header + offset;
krh@0
   133
			ht->pool_size = size;
krh@0
   134
			ht->pool_alloc = size;
krh@0
   135
			break;
krh@3
   136
		case HASHTABLE_PACKAGES:
krh@3
   137
			ht->packages = (void *) ht->header + offset;
krh@3
   138
			ht->package_count = size / sizeof *ht->packages;
krh@3
   139
			ht->package_alloc = size / sizeof *ht->packages;
krh@3
   140
			break;
krh@0
   141
		}
krh@0
   142
	}
krh@0
   143
	close(fd);
krh@0
   144
krh@0
   145
	return ht;
krh@0
   146
}
krh@0
   147
krh@0
   148
void
krh@0
   149
hashtable_destroy(struct hashtable *ht)
krh@0
   150
{
krh@0
   151
	unsigned int size;
krh@0
   152
	int i;
krh@0
   153
krh@0
   154
	if (ht->header) {
krh@0
   155
		for (i = 0; ht->header->sections[i].type; i++)
krh@0
   156
			;
krh@0
   157
		size = ht->header->sections[i].type;
krh@0
   158
		munmap(ht->header, size);
krh@0
   159
	} else {
krh@0
   160
		free(ht->buckets);
krh@0
   161
		free(ht->string_pool);
krh@0
   162
	}
krh@0
   163
krh@0
   164
	free(ht);
krh@0
   165
}
krh@0
   166
krh@0
   167
static int
krh@0
   168
hashtable_write(struct hashtable *ht, const char *filename)
krh@0
   169
{
krh@0
   170
	char data[4096];
krh@0
   171
	struct hashtable_header *header = (struct hashtable_header *) data;
krh@3
   172
	int fd, pool_size, package_size;
krh@3
   173
krh@3
   174
	/* Align these to pages sizes */
krh@3
   175
	pool_size = (ht->pool_size + 4095) & ~4095;
krh@3
   176
	package_size =
krh@3
   177
		(ht->package_alloc * sizeof *ht->packages + 4095) & ~4095;
krh@0
   178
krh@0
   179
	memset(data, 0, sizeof data);
krh@0
   180
	header->magic = HASHTABLE_MAGIC;
krh@0
   181
	header->version = HASHTABLE_VERSION;
krh@0
   182
krh@0
   183
	header->sections[0].type = HASHTABLE_BUCKETS;
krh@0
   184
	header->sections[0].offset = sizeof data;
krh@0
   185
krh@0
   186
	header->sections[1].type = HASHTABLE_STRINGS;
krh@3
   187
	header->sections[1].offset = header->sections[0].offset +
krh@3
   188
		ht->bucket_alloc * sizeof *ht->buckets;
krh@0
   189
krh@3
   190
	header->sections[2].type = HASHTABLE_PACKAGES;
krh@3
   191
	header->sections[2].offset = header->sections[1].offset + pool_size;
krh@3
   192
krh@3
   193
	header->sections[3].type = 0;
krh@3
   194
	header->sections[3].offset = header->sections[2].offset + package_size;
krh@0
   195
krh@0
   196
	fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666);
krh@0
   197
	if (fd < 0)
krh@0
   198
		return -1;
krh@0
   199
krh@0
   200
	write_to_fd(fd, data, sizeof data);
krh@0
   201
	write_to_fd(fd, ht->buckets, ht->bucket_alloc * sizeof *ht->buckets);
krh@3
   202
	write_to_fd(fd, ht->string_pool, pool_size);
krh@3
   203
	write_to_fd(fd, ht->packages, package_size);
krh@0
   204
krh@0
   205
	return 0;
krh@0
   206
}
krh@0
   207
krh@0
   208
static unsigned int
krh@0
   209
hash_string(const char *key)
krh@0
   210
{
krh@0
   211
	const char *p;
krh@0
   212
	unsigned int hash = 0;
krh@0
   213
krh@0
   214
	for (p = key; *p; p++)
krh@0
   215
		hash = (hash << 2) ^ *p;
krh@0
   216
krh@0
   217
	return hash;
krh@0
   218
}
krh@0
   219
krh@0
   220
unsigned long
krh@0
   221
hashtable_lookup(struct hashtable *ht, const char *key)
krh@0
   222
{
krh@0
   223
	unsigned int start;
krh@0
   224
	unsigned int mask;
krh@0
   225
	unsigned long value;
krh@0
   226
	int i;
krh@0
   227
krh@0
   228
	mask = ht->bucket_alloc - 1;
krh@0
   229
	start = hash_string(key) & mask;
krh@0
   230
	i = start;
krh@0
   231
	do {
krh@0
   232
		value = ht->buckets[i];
krh@0
   233
krh@0
   234
		if (value == 0)
krh@0
   235
			return 0;
krh@0
   236
krh@0
   237
		if (strcmp(key, &ht->string_pool[value]) == 0)
krh@0
   238
			return value;
krh@0
   239
krh@0
   240
		i = (i + 1) & mask;
krh@0
   241
	} while (i != start);
krh@0
   242
krh@0
   243
	return 0;
krh@0
   244
}
krh@0
   245
krh@0
   246
static unsigned long
krh@0
   247
add_to_string_pool(struct hashtable *ht, const char *key)
krh@0
   248
{
krh@0
   249
	int len, alloc;
krh@0
   250
	char *pool;
krh@0
   251
	unsigned long value;
krh@0
   252
krh@0
   253
	len = strlen(key) + 1;
krh@0
   254
	alloc = ht->pool_alloc;
krh@0
   255
	while (alloc < ht->pool_size + len)
krh@0
   256
		alloc *= 2;
krh@0
   257
	if (ht->pool_alloc < alloc) {
krh@0
   258
		pool = realloc(ht->string_pool, alloc);
krh@0
   259
		if (pool == NULL)
krh@0
   260
			return 0;
krh@0
   261
		ht->string_pool = pool;
krh@0
   262
		ht->pool_alloc = alloc;
krh@0
   263
	}
krh@0
   264
krh@0
   265
	memcpy(ht->string_pool + ht->pool_size, key, len);
krh@0
   266
	value = ht->pool_size;
krh@0
   267
	ht->pool_size += len;
krh@0
   268
krh@0
   269
	return value;
krh@0
   270
}
krh@0
   271
krh@0
   272
static void
krh@0
   273
do_insert(struct hashtable *ht, unsigned long value)
krh@0
   274
{
krh@0
   275
	unsigned int mask;
krh@0
   276
	const char *key;
krh@0
   277
	int i, start;
krh@0
   278
krh@0
   279
	key = &ht->string_pool[value];
krh@0
   280
	mask = ht->bucket_alloc - 1;
krh@0
   281
	start = hash_string(key) & mask;
krh@0
   282
	i = start;
krh@0
   283
	do {
krh@0
   284
		if (ht->buckets[i] == 0) {
krh@0
   285
			ht->buckets[i] = value;
krh@0
   286
			break;
krh@0
   287
		}
krh@0
   288
		i = (i + 1) & mask;
krh@0
   289
	} while (i != start);
krh@0
   290
}
krh@0
   291
krh@0
   292
unsigned long
krh@0
   293
hashtable_insert(struct hashtable *ht, const char *key)
krh@0
   294
{
krh@0
   295
	unsigned long value, *buckets, *old_buckets;
krh@0
   296
	int i, alloc, old_alloc;
krh@0
   297
krh@0
   298
	alloc = ht->bucket_alloc;
krh@0
   299
	while (alloc < 4 * ht->bucket_count)
krh@0
   300
		alloc *= 2;
krh@0
   301
krh@0
   302
	if (alloc != ht->bucket_alloc) {
krh@0
   303
		buckets = zalloc(alloc * sizeof *ht->buckets);
krh@0
   304
		if (buckets == NULL)
krh@0
   305
			return 0;
krh@0
   306
		old_buckets = ht->buckets;
krh@0
   307
		ht->buckets = buckets;
krh@0
   308
		old_alloc = ht->bucket_alloc;
krh@0
   309
		ht->bucket_alloc = alloc;
krh@0
   310
		
krh@0
   311
		for (i = 0; i < old_alloc; i++) {
krh@0
   312
			value = old_buckets[i];
krh@0
   313
			if (value != 0)
krh@0
   314
				do_insert(ht, value);
krh@0
   315
		}
krh@0
   316
		free(old_buckets);
krh@0
   317
	}
krh@0
   318
krh@0
   319
	value = add_to_string_pool(ht, key);
krh@0
   320
	do_insert (ht, value);
krh@0
   321
	ht->bucket_count++;
krh@0
   322
krh@0
   323
	return value;
krh@0
   324
}
krh@0
   325
krh@3
   326
static unsigned long
krh@3
   327
hashtable_add_package(struct hashtable *ht,
krh@3
   328
		      unsigned long name, unsigned long version)
krh@3
   329
{
krh@3
   330
	struct package *packages;
krh@3
   331
	int alloc;
krh@3
   332
krh@3
   333
	alloc = ht->package_alloc;
krh@3
   334
	while (alloc < ht->package_count + 1)
krh@3
   335
		alloc *= 2;
krh@3
   336
	if (ht->package_alloc < alloc) {
krh@3
   337
		packages = realloc(ht->packages, alloc * sizeof ht->packages);
krh@3
   338
		if (packages == NULL)
krh@3
   339
			return 0;
krh@3
   340
		ht->packages = packages;
krh@3
   341
		ht->package_alloc = alloc;
krh@3
   342
	}
krh@3
   343
krh@3
   344
	ht->packages[ht->package_count].name = name;
krh@3
   345
	ht->packages[ht->package_count].version = version;
krh@3
   346
	ht->package_count++;
krh@3
   347
krh@3
   348
	return 0;
krh@3
   349
}
krh@3
   350
krh@3
   351
krh@0
   352
struct razor_context {
krh@0
   353
	struct hashtable *global_ht;
krh@0
   354
};
krh@0
   355
krh@0
   356
struct razor_context *
krh@0
   357
razor_context_create (void)
krh@0
   358
{
krh@0
   359
	struct razor_context *ctx;
krh@0
   360
krh@0
   361
	ctx = malloc(sizeof *ctx);
krh@0
   362
	ctx->global_ht = hashtable_create();
krh@0
   363
krh@0
   364
	return ctx;
krh@0
   365
}
krh@0
   366
krh@0
   367
struct razor_context *
krh@0
   368
razor_context_create_from_file (const char *filename)
krh@0
   369
{
krh@0
   370
	struct razor_context *ctx;
krh@0
   371
krh@0
   372
	ctx = malloc(sizeof *ctx);
krh@0
   373
	ctx->global_ht = hashtable_create_from_file(filename);
krh@0
   374
krh@0
   375
	return ctx;
krh@0
   376
}
krh@0
   377
krh@0
   378
unsigned long
krh@0
   379
razor_context_tokenize(struct razor_context *ctx, const char *string)
krh@0
   380
{
krh@0
   381
	unsigned long token;
krh@0
   382
krh@0
   383
	token = hashtable_lookup(ctx->global_ht, string);
krh@0
   384
	if (token != 0)
krh@0
   385
		return token;
krh@0
   386
krh@0
   387
	return hashtable_insert(ctx->global_ht, string);
krh@0
   388
}
krh@0
   389
krh@3
   390
static struct hashtable *qsort_ht;
krh@3
   391
krh@3
   392
static int
krh@3
   393
compare_packages(const void *p1, const void *p2)
krh@3
   394
{
krh@3
   395
	const struct package *pkg1 = p1, *pkg2 = p2;
krh@3
   396
krh@3
   397
	return strcmp(&qsort_ht->string_pool[pkg1->name],
krh@3
   398
		      &qsort_ht->string_pool[pkg2->name]);
krh@3
   399
}
krh@3
   400
krh@3
   401
static void
krh@3
   402
razor_context_sort(struct razor_context *ctx)
krh@3
   403
{
krh@3
   404
	struct hashtable *ht = ctx->global_ht;
krh@3
   405
krh@3
   406
	qsort_ht = ht;
krh@3
   407
	qsort(ht->packages, ht->package_count, sizeof *ht->packages,
krh@3
   408
	      compare_packages);
krh@3
   409
}
krh@3
   410
krh@0
   411
struct razor_set {
krh@0
   412
	struct razor_context *ctx;
krh@0
   413
};
krh@0
   414
krh@0
   415
struct parsing_context {
krh@0
   416
	struct razor_context *ctx;
krh@0
   417
};
krh@0
   418
krh@0
   419
static void
krh@3
   420
parse_package(struct parsing_context *ctx, const char **atts)
krh@3
   421
{
krh@3
   422
	unsigned long name, version;
krh@3
   423
	int i;
krh@3
   424
krh@3
   425
	for (i = 0; atts[i]; i += 2) {
krh@3
   426
		if (strcmp(atts[i], "name") == 0)
krh@3
   427
			name = razor_context_tokenize(ctx->ctx, atts[i + 1]);
krh@3
   428
		else if (strcmp(atts[i], "version") == 0)
krh@3
   429
			version = razor_context_tokenize(ctx->ctx, atts[i + 1]);
krh@3
   430
	}
krh@3
   431
krh@3
   432
	if (name == 0 || version == 0) {
krh@3
   433
		fprintf(stderr, "invalid package tag, "
krh@3
   434
			"missing name or version attributes\n");
krh@3
   435
		return;
krh@3
   436
	}
krh@3
   437
krh@3
   438
	hashtable_add_package(ctx->ctx->global_ht, name, version);
krh@3
   439
}
krh@3
   440
krh@3
   441
static void
krh@0
   442
start_element(void *data, const char *name, const char **atts)
krh@0
   443
{
krh@0
   444
	struct parsing_context *ctx = data;
krh@0
   445
	int i;
krh@0
   446
krh@3
   447
	if (strcmp(name, "package") == 0)
krh@3
   448
		parse_package(ctx, atts);
krh@3
   449
krh@0
   450
	for (i = 0; atts[i]; i += 2)
krh@0
   451
		razor_context_tokenize(ctx->ctx, atts[i + 1]);
krh@0
   452
}
krh@0
   453
krh@0
   454
static void
krh@0
   455
end_element (void *data, const char *name)
krh@0
   456
{
krh@0
   457
}
krh@0
   458
krh@0
   459
static char *
krh@0
   460
sha1_to_hex(const unsigned char *sha1)
krh@0
   461
{
krh@0
   462
	static int bufno;
krh@0
   463
	static char hexbuffer[4][50];
krh@0
   464
	static const char hex[] = "0123456789abcdef";
krh@0
   465
	char *buffer = hexbuffer[3 & ++bufno], *buf = buffer;
krh@0
   466
	int i;
krh@0
   467
krh@0
   468
	for (i = 0; i < 20; i++) {
krh@0
   469
		unsigned int val = *sha1++;
krh@0
   470
		*buf++ = hex[val >> 4];
krh@0
   471
		*buf++ = hex[val & 0xf];
krh@0
   472
	}
krh@0
   473
	*buf = '\0';
krh@0
   474
krh@0
   475
	return buffer;
krh@0
   476
}
krh@0
   477
krh@0
   478
static int
krh@0
   479
razor_context_read_file(struct razor_context *ctx, const char *filename)
krh@0
   480
{
krh@0
   481
	SHA_CTX sha1;
krh@0
   482
	XML_Parser parser;
krh@0
   483
	struct parsing_context pctx;
krh@0
   484
	int fd;
krh@0
   485
	void *p;
krh@0
   486
	struct stat stat;
krh@0
   487
	char buf[128];
krh@0
   488
	unsigned char hash[20];
krh@0
   489
krh@0
   490
	fd = open(filename, O_RDONLY);
krh@0
   491
	if (fstat(fd, &stat) < 0)
krh@0
   492
		return -1;
krh@0
   493
	p = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
krh@0
   494
	if (p == MAP_FAILED)
krh@0
   495
		return -1;
krh@0
   496
krh@0
   497
	parser = XML_ParserCreate(NULL);
krh@0
   498
	pctx.ctx = ctx;
krh@0
   499
	XML_SetUserData(parser, &pctx);
krh@0
   500
	XML_SetElementHandler(parser, start_element, end_element);
krh@0
   501
	if (XML_Parse(parser, p, stat.st_size, 1) == XML_STATUS_ERROR) {
krh@0
   502
		fprintf(stderr,
krh@0
   503
			"%s at line %d, %s\n",
krh@0
   504
			XML_ErrorString(XML_GetErrorCode(parser)),
krh@0
   505
			XML_GetCurrentLineNumber(parser),
krh@0
   506
			filename);
krh@0
   507
		return 1;
krh@0
   508
	}
krh@0
   509
krh@0
   510
	XML_ParserFree(parser);
krh@0
   511
krh@0
   512
	SHA1_Init(&sha1);
krh@0
   513
	SHA1_Update(&sha1, p, stat.st_size);
krh@0
   514
	SHA1_Final(hash, &sha1);
krh@0
   515
krh@0
   516
	close(fd);
krh@0
   517
krh@0
   518
	snprintf(buf, sizeof buf, "set/%s", sha1_to_hex(hash));
krh@0
   519
	if (write_to_file(buf, p, stat.st_size) < 0)
krh@0
   520
		return -1;
krh@0
   521
	munmap(p, stat.st_size);
krh@0
   522
krh@0
   523
	return 0;
krh@0
   524
}
krh@0
   525
krh@0
   526
int
krh@0
   527
razor_context_write(struct razor_context *ctx, const char *filename)
krh@0
   528
{
krh@0
   529
	return hashtable_write(ctx->global_ht, filename);
krh@0
   530
}
krh@0
   531
krh@0
   532
void
krh@3
   533
razor_context_list_packages(struct razor_context *ctx)
krh@3
   534
{
krh@3
   535
	int i;
krh@3
   536
	struct hashtable *ht = ctx->global_ht;
krh@3
   537
	struct package *p;
krh@3
   538
krh@3
   539
	p = ht->packages;
krh@3
   540
	for (i = 0; i < ht->package_count && p->name; i++, p++) {
krh@3
   541
		printf("%s %s\n",
krh@3
   542
		       &ht->string_pool[p->name],
krh@3
   543
		       &ht->string_pool[p->version]);
krh@3
   544
	}
krh@3
   545
}
krh@3
   546
krh@3
   547
void
krh@3
   548
razor_context_info(struct razor_context *ctx)
krh@3
   549
{
krh@3
   550
	struct hashtable *ht = ctx->global_ht;
krh@3
   551
	unsigned int offset, size;
krh@3
   552
	int i;
krh@3
   553
krh@3
   554
	for (i = 0; i < ht->header->sections[i].type; i++) {
krh@3
   555
		offset = ht->header->sections[i].offset;
krh@3
   556
		size = ht->header->sections[i + 1].offset - offset;
krh@3
   557
krh@3
   558
		switch (ht->header->sections[i].type) {
krh@3
   559
		case HASHTABLE_BUCKETS:
krh@3
   560
			printf("bucket section:\t\t%dkb\n", size / 1024);
krh@3
   561
			break;
krh@3
   562
		case HASHTABLE_STRINGS:
krh@3
   563
			printf("string pool:\t\t%dkb\n", size / 1024);
krh@3
   564
			break;
krh@3
   565
		case HASHTABLE_PACKAGES:
krh@3
   566
			printf("package section:\t%dkb\n", size / 1024);
krh@3
   567
			break;
krh@3
   568
		}
krh@3
   569
	}
krh@3
   570
krh@3
   571
}
krh@3
   572
krh@3
   573
void
krh@0
   574
razor_context_destroy(struct razor_context *ctx)
krh@0
   575
{
krh@0
   576
	hashtable_destroy(ctx->global_ht);
krh@0
   577
	free(ctx);
krh@0
   578
}
krh@0
   579
krh@0
   580
static int
krh@0
   581
usage(void)
krh@0
   582
{
krh@3
   583
	printf("usage: razor [ import FILES | lookup <key> | list | info ]\n");
krh@0
   584
	exit(1);
krh@0
   585
}
krh@0
   586
krh@0
   587
static const char repo_filename[] = "system.repo";
krh@0
   588
krh@0
   589
int
krh@0
   590
main(int argc, char *argv[])
krh@0
   591
{
krh@0
   592
	int i;
krh@0
   593
	struct razor_context *ctx;
krh@0
   594
	struct stat statbuf;
krh@0
   595
krh@3
   596
	if (argc < 2) {
krh@0
   597
		usage();
krh@0
   598
	} else if (strcmp(argv[1], "import") == 0) {
krh@0
   599
		if (stat("set", &statbuf) && mkdir("set", 0777)) {
krh@0
   600
			fprintf(stderr, "could not create directory 'set'\n");
krh@0
   601
			exit(-1);
krh@0
   602
		}
krh@0
   603
			
krh@0
   604
		ctx = razor_context_create();
krh@0
   605
krh@0
   606
		for (i = 2; i < argc; i++) {
krh@0
   607
			if (razor_context_read_file(ctx, argv[i]) < 0) {
krh@0
   608
				fprintf(stderr, "failed to import %s\n",
krh@0
   609
					argv[i]);
krh@0
   610
				exit(-1);
krh@0
   611
			}
krh@0
   612
		}
krh@0
   613
krh@3
   614
		razor_context_sort(ctx);
krh@3
   615
krh@0
   616
		printf("number of buckets: %d\n",
krh@0
   617
		       ctx->global_ht->bucket_count);
krh@0
   618
		printf("bucket allocation: %d\n",
krh@0
   619
		       ctx->global_ht->bucket_alloc);
krh@0
   620
		printf("pool size: %d\n", ctx->global_ht->pool_size);
krh@0
   621
		printf("pool allocation: %d\n", ctx->global_ht->pool_alloc);
krh@0
   622
krh@0
   623
		razor_context_write(ctx, repo_filename);
krh@0
   624
krh@0
   625
		razor_context_destroy(ctx);
krh@0
   626
	} else if (strcmp(argv[1], "lookup") == 0) {
krh@0
   627
		ctx = razor_context_create_from_file(repo_filename);
krh@0
   628
		printf("%s is %lu\n", argv[2],
krh@0
   629
		       hashtable_lookup(ctx->global_ht, argv[2]));
krh@0
   630
		razor_context_destroy(ctx);
krh@3
   631
	} else if (strcmp(argv[1], "list") == 0) {
krh@3
   632
		ctx = razor_context_create_from_file(repo_filename);
krh@3
   633
		razor_context_list_packages(ctx);
krh@3
   634
		razor_context_destroy(ctx);
krh@3
   635
	} else if (strcmp(argv[1], "info") == 0) {
krh@3
   636
		ctx = razor_context_create_from_file(repo_filename);
krh@3
   637
		razor_context_info(ctx);
krh@3
   638
		razor_context_destroy(ctx);
krh@0
   639
	} else {
krh@0
   640
		usage();
krh@0
   641
	}
krh@0
   642
krh@0
   643
	return 0;
krh@0
   644
}