razor.c
author Kristian H?gsberg <krh@redhat.com>
Wed Sep 12 07:31:18 2007 -0400 (2007-09-12)
changeset 21 cfbf73037a39
parent 20 0aa6cf605ea0
child 22 083768007350
permissions -rw-r--r--
Implement package set validation.
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@0
    13
krh@0
    14
#include <expat.h>
krh@0
    15
#include "sha1.h"
krh@0
    16
krh@19
    17
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
krh@19
    18
krh@6
    19
struct array {
krh@6
    20
	void *data;
krh@6
    21
	int size, alloc;
krh@6
    22
};
krh@6
    23
krh@13
    24
static void
krh@13
    25
array_init(struct array *array)
krh@13
    26
{
krh@13
    27
	memset(array, 0, sizeof *array);
krh@13
    28
}
krh@13
    29
krh@13
    30
static void
krh@13
    31
array_release(struct array *array)
krh@13
    32
{
krh@13
    33
	free(array->data);
krh@13
    34
}
krh@13
    35
krh@6
    36
static void *
krh@6
    37
array_add(struct array *array, int size)
krh@6
    38
{
krh@6
    39
	int alloc;
krh@6
    40
	void *data, *p;
krh@6
    41
krh@6
    42
	if (array->alloc > 0)
krh@6
    43
		alloc = array->alloc;
krh@6
    44
	else
krh@10
    45
		alloc = 16;
krh@6
    46
krh@6
    47
	while (alloc < array->size + size)
krh@6
    48
		alloc *= 2;
krh@6
    49
krh@6
    50
	if (array->alloc < alloc) {
krh@6
    51
		data = realloc(array->data, alloc);
krh@6
    52
		if (data == NULL)
krh@6
    53
			return 0;
krh@6
    54
		array->data = data;
krh@6
    55
		array->alloc = alloc;
krh@6
    56
	}
krh@6
    57
krh@6
    58
	p = array->data + array->size;
krh@6
    59
	array->size += size;
krh@6
    60
krh@6
    61
	return p;
krh@6
    62
}
krh@6
    63
krh@0
    64
static int
krh@0
    65
write_to_fd(int fd, void *p, size_t size)
krh@0
    66
{
krh@0
    67
	int rest, len;
krh@0
    68
krh@0
    69
	rest = size;
krh@0
    70
	while (rest > 0) {
krh@0
    71
		len = write(fd, p, rest);
krh@0
    72
		if (len < 0)
krh@0
    73
			return -1;
krh@0
    74
		rest -= len;
krh@0
    75
	}
krh@0
    76
krh@0
    77
	return 0;
krh@0
    78
}
krh@0
    79
krh@0
    80
static int
krh@0
    81
write_to_file(const char *filename, void *p, size_t size)
krh@0
    82
{
krh@0
    83
	int fd, err;
krh@0
    84
krh@0
    85
	fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666);
krh@0
    86
	if (fd < 0)
krh@0
    87
		return -1;
krh@0
    88
	err = write_to_fd(fd, p, size);
krh@0
    89
	close(fd);
krh@0
    90
krh@0
    91
	return err;
krh@0
    92
}
krh@0
    93
krh@0
    94
static void *
krh@0
    95
zalloc(size_t size)
krh@0
    96
{
krh@0
    97
	void *p;
krh@0
    98
krh@0
    99
	p = malloc(size);
krh@0
   100
	memset(p, 0, size);
krh@0
   101
krh@0
   102
	return p;
krh@0
   103
}
krh@0
   104
krh@19
   105
struct razor_set_section {
krh@19
   106
	unsigned int type;
krh@19
   107
	unsigned int offset;
krh@19
   108
	unsigned int size;
krh@19
   109
};
krh@19
   110
krh@4
   111
struct razor_set_header {
krh@4
   112
	unsigned int magic;
krh@4
   113
	unsigned int version;
krh@19
   114
	struct razor_set_section sections[0];
krh@4
   115
};
krh@4
   116
krh@4
   117
#define RAZOR_MAGIC 0x7a7a7a7a
krh@4
   118
#define RAZOR_VERSION 1
krh@8
   119
krh@19
   120
#define RAZOR_PACKAGES 0
krh@19
   121
#define RAZOR_REQUIRES 1
krh@19
   122
#define RAZOR_PROVIDES 2
krh@19
   123
#define RAZOR_STRING_POOL 3
krh@19
   124
#define RAZOR_PROPERTY_POOL 4
krh@4
   125
krh@4
   126
struct razor_package {
krh@4
   127
	unsigned long name;
krh@4
   128
	unsigned long version;
krh@10
   129
	unsigned long requires;
krh@10
   130
	unsigned long provides;
krh@4
   131
};
krh@4
   132
krh@8
   133
struct razor_property {
krh@7
   134
	unsigned long name;
krh@7
   135
	unsigned long version;
krh@9
   136
	unsigned long packages;
krh@7
   137
};
krh@7
   138
krh@4
   139
struct razor_set {
krh@6
   140
	struct array buckets;
krh@6
   141
	struct array string_pool;
krh@10
   142
	struct array property_pool;
krh@7
   143
 	struct array packages;
krh@8
   144
 	struct array requires;
krh@7
   145
 	struct array provides;
krh@4
   146
	struct razor_set_header *header;
krh@4
   147
};
krh@4
   148
krh@19
   149
struct razor_set_section razor_sections[] = {
krh@19
   150
	{ RAZOR_PACKAGES,	offsetof(struct razor_set, packages) },
krh@19
   151
	{ RAZOR_REQUIRES,	offsetof(struct razor_set, requires) },
krh@19
   152
	{ RAZOR_PROVIDES,	offsetof(struct razor_set, provides) },
krh@19
   153
	{ RAZOR_STRING_POOL,	offsetof(struct razor_set, string_pool) },
krh@19
   154
	{ RAZOR_PROPERTY_POOL,	offsetof(struct razor_set, property_pool) },
krh@19
   155
};
krh@19
   156
krh@4
   157
struct razor_set *
krh@4
   158
razor_set_create(void)
krh@0
   159
{
krh@18
   160
	return zalloc(sizeof(struct razor_set));
krh@0
   161
}
krh@0
   162
krh@4
   163
struct razor_set *
krh@4
   164
razor_set_open(const char *filename)
krh@0
   165
{
krh@4
   166
	struct razor_set *set;
krh@19
   167
	struct razor_set_section *s;
krh@0
   168
	struct stat stat;
krh@19
   169
	struct array *array;
krh@19
   170
	int fd;
krh@0
   171
krh@4
   172
	set = zalloc(sizeof *set);
krh@0
   173
	fd = open(filename, O_RDONLY);
krh@0
   174
	if (fstat(fd, &stat) < 0)
krh@0
   175
		return NULL;
krh@4
   176
	set->header = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
krh@4
   177
	if (set->header == MAP_FAILED) {
krh@4
   178
		free(set);
krh@0
   179
		return NULL;
krh@0
   180
	}
krh@0
   181
krh@19
   182
	for (s = set->header->sections; ~s->type; s++) {
krh@19
   183
		if (s->type >= ARRAY_SIZE(razor_sections))
krh@19
   184
			continue;
krh@19
   185
		if (s->type != razor_sections[s->type].type)
krh@19
   186
			continue;
krh@19
   187
		array = (void *) set + razor_sections[s->type].offset;
krh@19
   188
		array->data = (void *) set->header + s->offset;
krh@19
   189
		array->size = s->size;
krh@19
   190
		array->alloc = s->size;
krh@0
   191
	}
krh@0
   192
	close(fd);
krh@0
   193
krh@4
   194
	return set;
krh@0
   195
}
krh@0
   196
krh@0
   197
void
krh@4
   198
razor_set_destroy(struct razor_set *set)
krh@0
   199
{
krh@0
   200
	unsigned int size;
krh@19
   201
	struct array *a;
krh@0
   202
	int i;
krh@0
   203
krh@4
   204
	if (set->header) {
krh@4
   205
		for (i = 0; set->header->sections[i].type; i++)
krh@0
   206
			;
krh@4
   207
		size = set->header->sections[i].type;
krh@4
   208
		munmap(set->header, size);
krh@17
   209
		free(set->buckets.data);
krh@0
   210
	} else {
krh@19
   211
		for (i = 0; i < ARRAY_SIZE(razor_sections); i++) {
krh@19
   212
			a = (void *) set + razor_sections[i].offset;
krh@19
   213
			free(a->data);
krh@19
   214
		}
krh@6
   215
		free(set->buckets.data);
krh@0
   216
	}
krh@0
   217
krh@4
   218
	free(set);
krh@0
   219
}
krh@0
   220
krh@0
   221
static int
krh@4
   222
razor_set_write(struct razor_set *set, const char *filename)
krh@0
   223
{
krh@0
   224
	char data[4096];
krh@4
   225
	struct razor_set_header *header = (struct razor_set_header *) data;
krh@19
   226
	struct array *a;
krh@17
   227
	unsigned long offset;
krh@17
   228
	int i, fd;
krh@0
   229
krh@0
   230
	memset(data, 0, sizeof data);
krh@4
   231
	header->magic = RAZOR_MAGIC;
krh@4
   232
	header->version = RAZOR_VERSION;
krh@17
   233
	offset = sizeof data;
krh@0
   234
krh@19
   235
	for (i = 0; i < ARRAY_SIZE(razor_sections); i++) {
krh@19
   236
		if (razor_sections[i].type != i)
krh@19
   237
			continue;
krh@19
   238
		a = (void *) set + razor_sections[i].offset;
krh@19
   239
		header->sections[i].type = i;
krh@17
   240
		header->sections[i].offset = offset;
krh@19
   241
		header->sections[i].size = a->size;
krh@19
   242
		offset += (a->size + 4095) & ~4095;
krh@17
   243
	}
krh@0
   244
krh@19
   245
	header->sections[i].type = ~0;
krh@17
   246
	header->sections[i].offset = 0;
krh@17
   247
	header->sections[i].size = 0;
krh@10
   248
krh@0
   249
	fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666);
krh@0
   250
	if (fd < 0)
krh@0
   251
		return -1;
krh@0
   252
krh@0
   253
	write_to_fd(fd, data, sizeof data);
krh@19
   254
	for (i = 0; i < ARRAY_SIZE(razor_sections); i++) {
krh@19
   255
		if (razor_sections[i].type != i)
krh@19
   256
			continue;
krh@19
   257
		a = (void *) set + razor_sections[i].offset;
krh@19
   258
		write_to_fd(fd, a->data, (a->size + 4095) & ~4095);
krh@19
   259
	}
krh@17
   260
krh@17
   261
	close(fd);
krh@0
   262
krh@0
   263
	return 0;
krh@0
   264
}
krh@0
   265
krh@0
   266
static unsigned int
krh@0
   267
hash_string(const char *key)
krh@0
   268
{
krh@0
   269
	const char *p;
krh@0
   270
	unsigned int hash = 0;
krh@0
   271
krh@0
   272
	for (p = key; *p; p++)
krh@9
   273
		hash = (hash * 617) ^ *p;
krh@0
   274
krh@0
   275
	return hash;
krh@0
   276
}
krh@0
   277
krh@0
   278
unsigned long
krh@4
   279
razor_set_lookup(struct razor_set *set, const char *key)
krh@0
   280
{
krh@6
   281
	unsigned int mask, start, i;
krh@6
   282
	unsigned long *b;
krh@6
   283
	char *pool;
krh@0
   284
krh@6
   285
	pool = set->string_pool.data;
krh@6
   286
	mask = set->buckets.alloc - 1;
krh@6
   287
	start = hash_string(key) * sizeof(unsigned long);
krh@0
   288
krh@6
   289
	for (i = 0; i < set->buckets.alloc; i += sizeof *b) {
krh@6
   290
		b = set->buckets.data + ((start + i) & mask);
krh@6
   291
krh@6
   292
		if (*b == 0)
krh@0
   293
			return 0;
krh@0
   294
krh@6
   295
		if (strcmp(key, &pool[*b]) == 0)
krh@6
   296
			return *b;
krh@6
   297
	}
krh@0
   298
krh@0
   299
	return 0;
krh@0
   300
}
krh@0
   301
krh@0
   302
static unsigned long
krh@4
   303
add_to_string_pool(struct razor_set *set, const char *key)
krh@0
   304
{
krh@6
   305
	int len;
krh@6
   306
	char *p;
krh@0
   307
krh@0
   308
	len = strlen(key) + 1;
krh@6
   309
	p = array_add(&set->string_pool, len);
krh@6
   310
	memcpy(p, key, len);
krh@0
   311
krh@6
   312
	return p - (char *) set->string_pool.data;
krh@0
   313
}
krh@0
   314
krh@10
   315
static unsigned long
krh@10
   316
add_to_property_pool(struct razor_set *set, struct array *properties)
krh@10
   317
{
krh@10
   318
	unsigned long  *p;
krh@10
   319
krh@10
   320
	p = array_add(properties, sizeof *p);
krh@18
   321
	*p = ~0ul;
krh@10
   322
	p = array_add(&set->property_pool, properties->size);
krh@10
   323
	memcpy(p, properties->data, properties->size);
krh@10
   324
krh@10
   325
	return p - (unsigned long *) set->property_pool.data;
krh@10
   326
}
krh@10
   327
krh@0
   328
static void
krh@4
   329
do_insert(struct razor_set *set, unsigned long value)
krh@0
   330
{
krh@6
   331
	unsigned int mask, start, i;
krh@6
   332
	unsigned long *b;
krh@0
   333
	const char *key;
krh@0
   334
krh@6
   335
	key = (char *) set->string_pool.data + value;
krh@6
   336
	mask = set->buckets.alloc - 1;
krh@6
   337
	start = hash_string(key) * sizeof(unsigned long);
krh@6
   338
krh@6
   339
	for (i = 0; i < set->buckets.alloc; i += sizeof *b) {
krh@6
   340
		b = set->buckets.data + ((start + i) & mask);
krh@6
   341
		if (*b == 0) {
krh@6
   342
			*b = value;
krh@0
   343
			break;
krh@0
   344
		}
krh@6
   345
	}
krh@0
   346
}
krh@0
   347
krh@0
   348
unsigned long
krh@4
   349
razor_set_insert(struct razor_set *set, const char *key)
krh@0
   350
{
krh@6
   351
	unsigned long value, *buckets, *b, *end;
krh@6
   352
	int alloc;
krh@0
   353
krh@6
   354
	alloc = set->buckets.alloc;
krh@6
   355
	array_add(&set->buckets, 4 * sizeof *buckets);
krh@6
   356
	if (alloc != set->buckets.alloc) {
krh@6
   357
		end = set->buckets.data + alloc;
krh@6
   358
		memset(end, 0, set->buckets.alloc - alloc);
krh@6
   359
		for (b = set->buckets.data; b < end; b++) {
krh@6
   360
			value = *b;
krh@6
   361
			if (value != 0) {
krh@6
   362
				*b = 0;
krh@4
   363
				do_insert(set, value);
krh@6
   364
			}
krh@0
   365
		}
krh@0
   366
	}
krh@0
   367
krh@4
   368
	value = add_to_string_pool(set, key);
krh@4
   369
	do_insert (set, value);
krh@0
   370
krh@0
   371
	return value;
krh@0
   372
}
krh@0
   373
krh@0
   374
unsigned long
krh@4
   375
razor_set_tokenize(struct razor_set *set, const char *string)
krh@0
   376
{
krh@0
   377
	unsigned long token;
krh@0
   378
krh@13
   379
	if (string == NULL)
krh@18
   380
		return razor_set_tokenize(set, "");
krh@13
   381
krh@4
   382
	token = razor_set_lookup(set, string);
krh@0
   383
	if (token != 0)
krh@0
   384
		return token;
krh@0
   385
krh@4
   386
	return razor_set_insert(set, string);
krh@0
   387
}
krh@0
   388
krh@13
   389
struct import_property_context {
krh@10
   390
	struct array all;
krh@10
   391
	struct array package;
krh@10
   392
};
krh@10
   393
krh@9
   394
struct import_context {
krh@4
   395
	struct razor_set *set;
krh@13
   396
	struct import_property_context requires;
krh@13
   397
	struct import_property_context provides;
krh@18
   398
	struct array packages;
krh@18
   399
	struct import_package *package;
krh@10
   400
	unsigned long *requires_map;
krh@10
   401
	unsigned long *provides_map;
krh@10
   402
};
krh@10
   403
krh@18
   404
struct import_package {
krh@18
   405
	unsigned long name;
krh@18
   406
	unsigned long version;
krh@18
   407
	unsigned long requires;
krh@18
   408
	unsigned long provides;
krh@18
   409
	unsigned long index;
krh@18
   410
};
krh@18
   411
krh@10
   412
struct import_property {
krh@10
   413
	unsigned long name;
krh@10
   414
	unsigned long version;
krh@10
   415
	unsigned long package;
krh@10
   416
	unsigned long index;
krh@10
   417
	unsigned long unique_index;
krh@0
   418
};
krh@0
   419
krh@0
   420
static void
krh@13
   421
import_context_add_package(struct import_context *ctx,
krh@13
   422
			   const char *name, const char *version)
krh@13
   423
{
krh@18
   424
	struct import_package *p;
krh@13
   425
krh@18
   426
	p = array_add(&ctx->packages, sizeof *p);
krh@13
   427
	p->name = razor_set_tokenize(ctx->set, name);
krh@13
   428
	p->version = razor_set_tokenize(ctx->set, version);
krh@18
   429
	p->index = p - (struct import_package *) ctx->packages.data;
krh@13
   430
krh@18
   431
	ctx->package = p;
krh@13
   432
	array_init(&ctx->requires.package);
krh@13
   433
	array_init(&ctx->provides.package);
krh@13
   434
}
krh@13
   435
krh@13
   436
void
krh@13
   437
import_context_finish_package(struct import_context *ctx)
krh@13
   438
{
krh@18
   439
	struct import_package *p;
krh@13
   440
krh@18
   441
	p = ctx->package;
krh@13
   442
	p->requires = add_to_property_pool(ctx->set, &ctx->requires.package);
krh@13
   443
	p->provides = add_to_property_pool(ctx->set, &ctx->provides.package);
krh@13
   444
krh@13
   445
	array_release(&ctx->requires.package);
krh@13
   446
	array_release(&ctx->provides.package);
krh@13
   447
}
krh@13
   448
krh@13
   449
static void
krh@13
   450
import_context_add_property(struct import_context *ctx,
krh@13
   451
			    struct import_property_context *pctx,
krh@13
   452
			    const char *name, const char *version)
krh@13
   453
{
krh@13
   454
	struct import_property *p;
krh@13
   455
	unsigned long *r;
krh@13
   456
krh@13
   457
	p = array_add(&pctx->all, sizeof *p);
krh@13
   458
	p->name = razor_set_tokenize(ctx->set, name);
krh@13
   459
	p->version = razor_set_tokenize(ctx->set, version);
krh@18
   460
	p->package = ctx->package->index;
krh@13
   461
	p->index = p - (struct import_property *) pctx->all.data;
krh@13
   462
krh@13
   463
	r = array_add(&pctx->package, sizeof *r);
krh@13
   464
	*r = p->index;
krh@13
   465
}
krh@13
   466
krh@13
   467
static void
krh@9
   468
parse_package(struct import_context *ctx, const char **atts, void *data)
krh@3
   469
{
krh@13
   470
	const char *name = NULL, *version = NULL;
krh@3
   471
	int i;
krh@3
   472
krh@3
   473
	for (i = 0; atts[i]; i += 2) {
krh@3
   474
		if (strcmp(atts[i], "name") == 0)
krh@13
   475
			name = atts[i + 1];
krh@3
   476
		else if (strcmp(atts[i], "version") == 0)
krh@13
   477
			version = atts[i + 1];
krh@3
   478
	}
krh@3
   479
krh@13
   480
	if (name == NULL || version == NULL) {
krh@3
   481
		fprintf(stderr, "invalid package tag, "
krh@3
   482
			"missing name or version attributes\n");
krh@3
   483
		return;
krh@3
   484
	}
krh@3
   485
krh@13
   486
	import_context_add_package(ctx, name, version);
krh@3
   487
}
krh@3
   488
krh@3
   489
static void
krh@9
   490
parse_property(struct import_context *ctx, const char **atts, void *data)
krh@8
   491
{
krh@13
   492
	const char *name = NULL, *version = NULL;
krh@8
   493
	int i;
krh@8
   494
krh@8
   495
	for (i = 0; atts[i]; i += 2) {
krh@8
   496
		if (strcmp(atts[i], "name") == 0)
krh@13
   497
			name = atts[i + 1];
krh@9
   498
		if (strcmp(atts[i], "version") == 0)
krh@13
   499
			version = atts[i + 1];
krh@8
   500
	}
krh@8
   501
	
krh@13
   502
	if (name == NULL) {
krh@9
   503
		fprintf(stderr, "invalid tag, missing name attribute\n");
krh@8
   504
		return;
krh@8
   505
	}
krh@8
   506
krh@13
   507
	import_context_add_property(ctx, data, name, version);
krh@7
   508
}
krh@7
   509
krh@7
   510
static void
krh@0
   511
start_element(void *data, const char *name, const char **atts)
krh@0
   512
{
krh@9
   513
	struct import_context *ctx = data;
krh@0
   514
krh@3
   515
	if (strcmp(name, "package") == 0)
krh@9
   516
		parse_package(ctx, atts, NULL);
krh@8
   517
	else if (strcmp(name, "requires") == 0)
krh@9
   518
		parse_property(ctx, atts, &ctx->requires);
krh@7
   519
	else if (strcmp(name, "provides") == 0)
krh@9
   520
		parse_property(ctx, atts, &ctx->provides);
krh@0
   521
}
krh@0
   522
krh@0
   523
static void
krh@0
   524
end_element (void *data, const char *name)
krh@0
   525
{
krh@9
   526
	struct import_context *ctx = data;
krh@4
   527
krh@13
   528
	if (strcmp(name, "package") == 0)
krh@13
   529
		import_context_finish_package(ctx);
krh@0
   530
}
krh@0
   531
krh@0
   532
static char *
krh@0
   533
sha1_to_hex(const unsigned char *sha1)
krh@0
   534
{
krh@0
   535
	static int bufno;
krh@0
   536
	static char hexbuffer[4][50];
krh@0
   537
	static const char hex[] = "0123456789abcdef";
krh@0
   538
	char *buffer = hexbuffer[3 & ++bufno], *buf = buffer;
krh@0
   539
	int i;
krh@0
   540
krh@0
   541
	for (i = 0; i < 20; i++) {
krh@0
   542
		unsigned int val = *sha1++;
krh@0
   543
		*buf++ = hex[val >> 4];
krh@0
   544
		*buf++ = hex[val & 0xf];
krh@0
   545
	}
krh@0
   546
	*buf = '\0';
krh@0
   547
krh@0
   548
	return buffer;
krh@0
   549
}
krh@0
   550
krh@9
   551
static void
krh@13
   552
razor_prepare_import(struct import_context *ctx)
krh@9
   553
{
krh@9
   554
	memset(ctx, 0, sizeof *ctx);
krh@13
   555
	ctx->set = razor_set_create();
krh@9
   556
}
krh@9
   557
krh@0
   558
static int
krh@13
   559
razor_import(struct import_context *ctx, const char *filename)
krh@0
   560
{
krh@0
   561
	SHA_CTX sha1;
krh@0
   562
	XML_Parser parser;
krh@0
   563
	int fd;
krh@0
   564
	void *p;
krh@0
   565
	struct stat stat;
krh@0
   566
	char buf[128];
krh@0
   567
	unsigned char hash[20];
krh@0
   568
krh@0
   569
	fd = open(filename, O_RDONLY);
krh@0
   570
	if (fstat(fd, &stat) < 0)
krh@0
   571
		return -1;
krh@0
   572
	p = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
krh@0
   573
	if (p == MAP_FAILED)
krh@0
   574
		return -1;
krh@0
   575
krh@0
   576
	parser = XML_ParserCreate(NULL);
krh@9
   577
	XML_SetUserData(parser, ctx);
krh@0
   578
	XML_SetElementHandler(parser, start_element, end_element);
krh@0
   579
	if (XML_Parse(parser, p, stat.st_size, 1) == XML_STATUS_ERROR) {
krh@0
   580
		fprintf(stderr,
krh@0
   581
			"%s at line %d, %s\n",
krh@0
   582
			XML_ErrorString(XML_GetErrorCode(parser)),
krh@0
   583
			XML_GetCurrentLineNumber(parser),
krh@0
   584
			filename);
krh@0
   585
		return 1;
krh@0
   586
	}
krh@0
   587
krh@0
   588
	XML_ParserFree(parser);
krh@0
   589
krh@0
   590
	SHA1_Init(&sha1);
krh@0
   591
	SHA1_Update(&sha1, p, stat.st_size);
krh@0
   592
	SHA1_Final(hash, &sha1);
krh@0
   593
krh@0
   594
	close(fd);
krh@0
   595
krh@0
   596
	snprintf(buf, sizeof buf, "set/%s", sha1_to_hex(hash));
krh@0
   597
	if (write_to_file(buf, p, stat.st_size) < 0)
krh@0
   598
		return -1;
krh@0
   599
	munmap(p, stat.st_size);
krh@0
   600
krh@0
   601
	return 0;
krh@0
   602
}
krh@0
   603
krh@9
   604
static struct razor_set *qsort_set;
krh@9
   605
krh@9
   606
static int
krh@9
   607
compare_packages(const void *p1, const void *p2)
krh@9
   608
{
krh@18
   609
	const struct import_package *pkg1 = p1, *pkg2 = p2;
krh@9
   610
	char *pool = qsort_set->string_pool.data;
krh@9
   611
krh@9
   612
	return strcmp(&pool[pkg1->name], &pool[pkg2->name]);
krh@9
   613
}
krh@9
   614
krh@9
   615
static int
krh@9
   616
compare_properties(const void *p1, const void *p2)
krh@9
   617
{
krh@10
   618
	const struct import_property *prop1 = p1, *prop2 = p2;
krh@9
   619
	char *pool = qsort_set->string_pool.data;
krh@12
   620
	int result;
krh@9
   621
krh@12
   622
	result = strcmp(&pool[prop1->name], &pool[prop2->name]);
krh@12
   623
	if (result == 0)
krh@12
   624
		return strcmp(&pool[prop1->version], &pool[prop2->version]);
krh@12
   625
	else
krh@12
   626
		return result;
krh@9
   627
}
krh@9
   628
krh@10
   629
static unsigned long *
krh@18
   630
uniqueify_properties(struct razor_set *set,
krh@18
   631
		     struct array *in, struct array *out)
krh@9
   632
{
krh@10
   633
	struct import_property *ip, *end;
krh@18
   634
	struct razor_property *rp, *rp_end;
krh@18
   635
	struct array *pkgs, *p;
krh@18
   636
	unsigned long *map, *r;
krh@18
   637
	int i, count, unique;
krh@9
   638
krh@10
   639
	count = in->size / sizeof(struct import_property);
krh@10
   640
	qsort(in->data, count,
krh@10
   641
	      sizeof(struct import_property), compare_properties);
krh@9
   642
krh@10
   643
	rp = NULL;
krh@9
   644
	end = in->data + in->size;
krh@10
   645
	for (ip = in->data; ip < end; ip++) {
krh@10
   646
		if (rp == NULL ||
krh@10
   647
		    ip->name != rp->name || ip->version != rp->version) {
krh@10
   648
			rp = array_add(out, sizeof *rp);
krh@10
   649
			rp->name = ip->name;
krh@10
   650
			rp->version = ip->version;
krh@10
   651
		}
krh@10
   652
		ip->unique_index = rp - (struct razor_property *) out->data;
krh@10
   653
	}
krh@9
   654
krh@10
   655
	map = malloc(count * sizeof (unsigned long));
krh@10
   656
	ip = in->data;
krh@10
   657
	for (i = 0; i < count; i++)
krh@10
   658
		map[ip[i].index] = ip[i].unique_index;
krh@10
   659
krh@18
   660
	unique = out->size / sizeof(*rp);
krh@18
   661
	pkgs = zalloc(unique * sizeof *pkgs);
krh@18
   662
	for (ip = in->data; ip < end; ip++) {
krh@18
   663
		r = array_add(&pkgs[ip->unique_index], sizeof *r);
krh@18
   664
		*r = ip->package;
krh@18
   665
	}
krh@18
   666
krh@18
   667
	rp_end = out->data + out->size;
krh@18
   668
	for (rp = out->data, p = pkgs; rp < rp_end; rp++, p++)
krh@18
   669
		rp->packages = add_to_property_pool(set, p);
krh@18
   670
krh@18
   671
	free(pkgs);
krh@18
   672
krh@10
   673
	return map;
krh@10
   674
}
krh@10
   675
krh@10
   676
static void
krh@18
   677
remap_package_links(struct import_context *ctx)
krh@10
   678
{
krh@18
   679
	struct import_package *p, *end;
krh@10
   680
	unsigned long *pool, *r;
krh@10
   681
krh@10
   682
	pool = ctx->set->property_pool.data;
krh@18
   683
	end = ctx->packages.data + ctx->packages.size;
krh@18
   684
	for (p = ctx->packages.data; p < end; p++) {
krh@18
   685
		for (r = &pool[p->requires]; ~*r; r++)
krh@10
   686
			*r = ctx->requires_map[*r];
krh@18
   687
		for (r = &pool[p->provides]; ~*r; r++)
krh@10
   688
			*r = ctx->provides_map[*r];
krh@9
   689
	}
krh@18
   690
}
krh@10
   691
krh@18
   692
static void
krh@18
   693
remap_property_links(struct import_context *ctx)
krh@18
   694
{
krh@18
   695
	struct razor_property *p, *end;
krh@18
   696
	struct import_package *ip;
krh@18
   697
	unsigned long *map, *pool, *r;
krh@18
   698
	int i, count;
krh@18
   699
krh@18
   700
	pool = ctx->set->property_pool.data;
krh@18
   701
	count = ctx->packages.size / sizeof(struct import_package);
krh@18
   702
	map = malloc(count * sizeof *map);
krh@18
   703
	ip = ctx->packages.data;
krh@18
   704
	for (i = 0; i < count; i++)
krh@18
   705
		map[ip[i].index] = i;
krh@18
   706
krh@18
   707
	/* FIXME: This will break if we implement package list sharing
krh@18
   708
	 * for all properties, since we'll remap those lists more than
krh@18
   709
	 * once. We should just have a separate pool for property
krh@18
   710
	 * lists and a separate pool for package lists and remap it as
krh@18
   711
	 * a flat pool.  Right now, as property lists and package
krh@18
   712
	 * lists are mixed, we can't do that. */
krh@18
   713
krh@18
   714
	end = ctx->set->requires.data + ctx->set->requires.size;
krh@18
   715
	for (p = ctx->set->requires.data; p < end; p++)
krh@18
   716
		for (r = &pool[p->packages]; ~*r; r++)
krh@18
   717
			*r = map[*r];
krh@18
   718
krh@18
   719
	end = ctx->set->provides.data + ctx->set->provides.size;
krh@18
   720
	for (p = ctx->set->provides.data; p < end; p++)
krh@18
   721
		for (r = &pool[p->packages]; ~*r; r++)
krh@18
   722
			*r = map[*r];
krh@18
   723
krh@18
   724
	free(map);
krh@9
   725
}
krh@9
   726
krh@13
   727
static struct razor_set *
krh@13
   728
razor_finish_import(struct import_context *ctx)
krh@9
   729
{
krh@18
   730
	struct import_package *ip;
krh@18
   731
	struct razor_package *rp;
krh@18
   732
	int i, count;
krh@18
   733
krh@9
   734
	qsort_set = ctx->set;
krh@9
   735
krh@10
   736
	ctx->requires_map =
krh@18
   737
		uniqueify_properties(ctx->set, 
krh@18
   738
				     &ctx->requires.all,
krh@18
   739
				     &ctx->set->requires);
krh@10
   740
	ctx->provides_map =
krh@18
   741
		uniqueify_properties(ctx->set,
krh@18
   742
				     &ctx->provides.all,
krh@18
   743
				     &ctx->set->provides);
krh@9
   744
krh@18
   745
	remap_package_links(ctx);
krh@18
   746
krh@18
   747
	count = ctx->packages.size / sizeof(struct import_package);
krh@18
   748
	qsort(ctx->packages.data, count,
krh@18
   749
	      sizeof(struct import_package), compare_packages);
krh@18
   750
krh@18
   751
	ip = ctx->packages.data;
krh@18
   752
	for (i = 0; i < count; i++, ip++, rp++) {
krh@18
   753
		rp = array_add(&ctx->set->packages, sizeof *rp);
krh@18
   754
		rp->name = ip->name;
krh@18
   755
		rp->version = ip->version;
krh@18
   756
		rp->requires = ip->requires;
krh@18
   757
		rp->provides = ip->provides;
krh@18
   758
	}
krh@18
   759
krh@18
   760
	remap_property_links(ctx);
krh@10
   761
krh@10
   762
	free(ctx->requires.all.data);
krh@10
   763
	free(ctx->provides.all.data);
krh@10
   764
	free(ctx->requires_map);
krh@10
   765
	free(ctx->provides_map);
krh@13
   766
		
krh@9
   767
	fprintf(stderr, "parsed %d requires, %d unique\n",
krh@10
   768
		ctx->requires.all.size / sizeof(struct import_property),
krh@9
   769
		ctx->set->requires.size / sizeof(struct razor_property));
krh@9
   770
	fprintf(stderr, "parsed %d provides, %d unique\n",
krh@10
   771
		ctx->provides.all.size / sizeof(struct import_property),
krh@9
   772
		ctx->set->provides.size / sizeof(struct razor_property));
krh@13
   773
krh@13
   774
	return ctx->set; 
krh@9
   775
}
krh@9
   776
krh@15
   777
/* Import a yum filelist as a razor package set. */
krh@15
   778
krh@15
   779
enum {
krh@15
   780
	YUM_STATE_BEGIN,
krh@15
   781
	YUM_STATE_PACKAGE_NAME
krh@15
   782
};
krh@15
   783
krh@15
   784
struct yum_context {
krh@15
   785
	struct import_context ctx;
krh@15
   786
	struct import_property_context *current_property_context;
krh@15
   787
	char *name;
krh@15
   788
	int state;
krh@15
   789
};
krh@15
   790
krh@15
   791
static void
krh@15
   792
yum_start_element(void *data, const char *name, const char **atts)
krh@15
   793
{
krh@15
   794
	struct yum_context *ctx = data;
krh@15
   795
	const char *n, *version;
krh@15
   796
	int i;
krh@15
   797
krh@15
   798
	if (strcmp(name, "name") == 0) {
krh@15
   799
		ctx->state = YUM_STATE_PACKAGE_NAME;
krh@15
   800
	} else if (strcmp(name, "version") == 0) {
krh@15
   801
		for (i = 0; atts[i]; i += 2) {
krh@15
   802
			if (strcmp(atts[i], "ver") == 0)
krh@15
   803
				version = atts[i + 1];
krh@15
   804
		}
krh@15
   805
		import_context_add_package(&ctx->ctx, ctx->name, version);
krh@15
   806
	} else if (strcmp(name, "rpm:requires") == 0) {
krh@15
   807
		ctx->current_property_context = &ctx->ctx.requires;
krh@15
   808
	} else if (strcmp(name, "rpm:provides") == 0) {
krh@15
   809
		ctx->current_property_context = &ctx->ctx.provides;
krh@15
   810
	} else if (strcmp(name, "rpm:entry") == 0 &&
krh@15
   811
		   ctx->current_property_context != NULL) {
krh@15
   812
		n = NULL;
krh@15
   813
		version = NULL;
krh@15
   814
		for (i = 0; atts[i]; i += 2) {
krh@15
   815
			if (strcmp(atts[i], "name") == 0)
krh@15
   816
				n = atts[i + 1];
krh@15
   817
			else if (strcmp(atts[i], "ver") == 0)
krh@15
   818
				version = atts[i + 1];
krh@15
   819
		}
krh@15
   820
krh@15
   821
		if (n == NULL) {
krh@15
   822
			fprintf(stderr, "invalid rpm:entry, "
krh@15
   823
				"missing name or version attributes\n");
krh@15
   824
			return;
krh@15
   825
		}
krh@15
   826
krh@15
   827
		import_context_add_property(&ctx->ctx,
krh@15
   828
					    ctx->current_property_context,
krh@15
   829
					    n, version);
krh@15
   830
	}
krh@15
   831
}
krh@15
   832
krh@15
   833
static void
krh@15
   834
yum_end_element (void *data, const char *name)
krh@15
   835
{
krh@15
   836
	struct yum_context *ctx = data;
krh@15
   837
krh@15
   838
	if (strcmp(name, "package") == 0) {
krh@15
   839
		free(ctx->name);
krh@15
   840
		import_context_finish_package(&ctx->ctx);
krh@15
   841
	} else if (strcmp(name, "name") == 0) {
krh@15
   842
		ctx->state = 0;
krh@15
   843
	} else if (strcmp(name, "rpm:requires") == 0) {
krh@15
   844
		ctx->current_property_context = NULL;
krh@15
   845
	} else if (strcmp(name, "rpm:provides") == 0) {
krh@15
   846
		ctx->current_property_context = NULL;
krh@15
   847
	}
krh@15
   848
}
krh@15
   849
krh@15
   850
static void
krh@15
   851
yum_character_data (void *data, const XML_Char *s, int len)
krh@15
   852
{
krh@15
   853
	struct yum_context *ctx = data;
krh@15
   854
krh@15
   855
	if (ctx->state == YUM_STATE_PACKAGE_NAME)
krh@15
   856
		ctx->name = strndup(s, len);
krh@15
   857
}
krh@15
   858
krh@15
   859
static struct razor_set *
krh@15
   860
razor_set_create_from_yum_filelist(int fd)
krh@15
   861
{
krh@15
   862
	struct yum_context ctx;
krh@15
   863
	XML_Parser parser;
krh@15
   864
	char buf[4096];
krh@15
   865
	int len;
krh@15
   866
krh@15
   867
	razor_prepare_import(&ctx.ctx);
krh@15
   868
krh@15
   869
	parser = XML_ParserCreate(NULL);
krh@15
   870
	XML_SetUserData(parser, &ctx);
krh@15
   871
	XML_SetElementHandler(parser, yum_start_element, yum_end_element);
krh@15
   872
	XML_SetCharacterDataHandler(parser, yum_character_data);
krh@15
   873
krh@15
   874
	while (1) {
krh@15
   875
		len = read(fd, buf, sizeof buf);
krh@15
   876
		if (len < 0) {
krh@15
   877
			fprintf(stderr,
krh@15
   878
				"couldn't read input: %s\n", strerror(errno));
krh@15
   879
			return NULL;
krh@15
   880
		} else if (len == 0)
krh@15
   881
			break;
krh@15
   882
krh@15
   883
		if (XML_Parse(parser, buf, len, 0) == XML_STATUS_ERROR) {
krh@15
   884
			fprintf(stderr,
krh@15
   885
				"%s at line %d\n",
krh@15
   886
				XML_ErrorString(XML_GetErrorCode(parser)),
krh@15
   887
				XML_GetCurrentLineNumber(parser));
krh@15
   888
			return NULL;
krh@15
   889
		}
krh@15
   890
	}
krh@15
   891
krh@15
   892
	XML_ParserFree(parser);
krh@15
   893
krh@15
   894
	return razor_finish_import(&ctx.ctx);
krh@15
   895
}
krh@15
   896
krh@0
   897
void
krh@4
   898
razor_set_list(struct razor_set *set)
krh@3
   899
{
krh@6
   900
	struct razor_package *p, *end;
krh@6
   901
	char *pool;
krh@3
   902
krh@6
   903
	pool = set->string_pool.data;
krh@6
   904
	end = set->packages.data + set->packages.size;
krh@14
   905
	for (p = set->packages.data; p < end; p++)
krh@6
   906
		printf("%s %s\n", &pool[p->name], &pool[p->version]);
krh@3
   907
}
krh@3
   908
krh@16
   909
struct razor_set *bsearch_set;
krh@16
   910
krh@16
   911
static int
krh@16
   912
compare_package_name(const void *key, const void *data)
krh@16
   913
{
krh@16
   914
	const struct razor_package *p = data;
krh@16
   915
	char *pool;
krh@16
   916
krh@16
   917
	pool = bsearch_set->string_pool.data;
krh@16
   918
krh@16
   919
	return strcmp(key, &pool[p->name]);
krh@16
   920
}
krh@16
   921
krh@10
   922
struct razor_package *
krh@10
   923
razor_set_get_package(struct razor_set *set, const char *package)
krh@10
   924
{
krh@16
   925
	bsearch_set = set;
krh@16
   926
	return bsearch(package, set->packages.data,
krh@16
   927
		       set->packages.size / sizeof(struct razor_package),
krh@16
   928
		       sizeof(struct razor_package), compare_package_name);
krh@10
   929
}
krh@10
   930
krh@18
   931
static int
krh@18
   932
compare_property_name(const void *key, const void *data)
krh@18
   933
{
krh@18
   934
	const struct razor_property *p = data;
krh@18
   935
	char *pool;
krh@18
   936
krh@18
   937
	pool = bsearch_set->string_pool.data;
krh@18
   938
krh@18
   939
	return strcmp(key, &pool[p->name]);
krh@18
   940
}
krh@18
   941
krh@18
   942
struct razor_property *
krh@18
   943
razor_set_get_property(struct razor_set *set,
krh@18
   944
		       struct array *properties,
krh@18
   945
		       const char *property)
krh@18
   946
{
krh@18
   947
	struct razor_property *p, *start;
krh@18
   948
krh@18
   949
	bsearch_set = set;
krh@18
   950
	p = bsearch(property, properties->data,
krh@18
   951
		    properties->size / sizeof(struct razor_property),
krh@18
   952
		    sizeof(struct razor_property), compare_property_name);
krh@18
   953
krh@18
   954
	start = properties->data;
krh@18
   955
	while (p > start && (p - 1)->name == p->name)
krh@18
   956
		p--;
krh@18
   957
krh@18
   958
	return p;
krh@18
   959
}
krh@18
   960
krh@10
   961
static void
krh@10
   962
razor_set_list_all_properties(struct razor_set *set, struct array *properties)
krh@8
   963
{
krh@8
   964
	struct razor_property *p, *end;
krh@8
   965
	char *pool;
krh@8
   966
krh@8
   967
	pool = set->string_pool.data;
krh@10
   968
	end = properties->data + properties->size;
krh@14
   969
	for (p = properties->data; p < end; p++)
krh@8
   970
		printf("%s %s\n", &pool[p->name], &pool[p->version]);
krh@8
   971
}
krh@8
   972
krh@8
   973
void
krh@10
   974
razor_set_list_requires(struct razor_set *set, const char *name)
krh@7
   975
{
krh@10
   976
	struct razor_property *p, *requires;
krh@10
   977
	struct razor_package *package;
krh@10
   978
	unsigned long *r;
krh@7
   979
	char *pool;
krh@7
   980
krh@10
   981
	if (name) {
krh@10
   982
		package = razor_set_get_package(set, name);
krh@10
   983
		r = (unsigned long *) set->property_pool.data +
krh@10
   984
			package->requires;
krh@10
   985
		requires = set->requires.data;
krh@10
   986
		pool = set->string_pool.data;
krh@18
   987
		while (~*r) {
krh@10
   988
			p = &requires[*r++];
krh@10
   989
			printf("%s %s\n", &pool[p->name], &pool[p->version]);
krh@10
   990
		}
krh@10
   991
	} else
krh@10
   992
		razor_set_list_all_properties(set, &set->requires);
krh@10
   993
}
krh@10
   994
krh@10
   995
void
krh@10
   996
razor_set_list_provides(struct razor_set *set, const char *name)
krh@10
   997
{
krh@10
   998
	struct razor_property *p, *provides;
krh@10
   999
	struct razor_package *package;
krh@10
  1000
	unsigned long *r;
krh@10
  1001
	char *pool;
krh@10
  1002
krh@10
  1003
	if (name) {
krh@10
  1004
		package = razor_set_get_package(set, name);
krh@10
  1005
		r = (unsigned long *) set->property_pool.data +
krh@10
  1006
			package->provides;
krh@10
  1007
		provides = set->provides.data;
krh@10
  1008
		pool = set->string_pool.data;
krh@18
  1009
		while (~*r) {
krh@10
  1010
			p = &provides[*r++];
krh@10
  1011
			printf("%s %s\n", &pool[p->name], &pool[p->version]);
krh@10
  1012
		}
krh@10
  1013
	} else 
krh@10
  1014
		razor_set_list_all_properties(set, &set->provides);
krh@7
  1015
}
krh@7
  1016
krh@7
  1017
void
krh@18
  1018
razor_set_list_property_packages(struct razor_set *set,
krh@18
  1019
				 struct array *properties,
krh@20
  1020
				 const char *name,
krh@20
  1021
				 const char *version)
krh@18
  1022
{
krh@18
  1023
	struct razor_property *property, *end;
krh@18
  1024
	struct razor_package *p, *packages;
krh@18
  1025
	unsigned long *r;
krh@18
  1026
	char *pool;
krh@18
  1027
krh@18
  1028
	if (name == NULL)
krh@18
  1029
		return;
krh@18
  1030
krh@18
  1031
	property = razor_set_get_property(set, properties, name);
krh@18
  1032
	packages = set->packages.data;
krh@18
  1033
	pool = set->string_pool.data;
krh@18
  1034
	end = properties->data + properties->size;
krh@18
  1035
	while (property < end && strcmp(name, &pool[property->name]) == 0) {
krh@20
  1036
		if (version && strcmp(version, &pool[property->version]) != 0)
krh@20
  1037
			goto next;
krh@18
  1038
		r = (unsigned long *)
krh@18
  1039
			set->property_pool.data + property->packages;
krh@18
  1040
		while (~*r) {
krh@18
  1041
			p = &packages[*r++];
krh@18
  1042
			printf("%s %s\n",
krh@18
  1043
			       &pool[p->name], &pool[p->version]);
krh@18
  1044
		}
krh@20
  1045
	next:
krh@18
  1046
		property++;
krh@18
  1047
	}
krh@18
  1048
}
krh@18
  1049
krh@18
  1050
void
krh@21
  1051
razor_set_validate(struct razor_set *set, struct array *unsatisfied)
krh@21
  1052
{
krh@21
  1053
	struct razor_property *r, *p, *rend, *pend;
krh@21
  1054
	unsigned long *u;
krh@21
  1055
	char *pool;
krh@21
  1056
krh@21
  1057
	r = set->requires.data;
krh@21
  1058
	p = set->provides.data;
krh@21
  1059
	rend = set->requires.data + set->requires.size;
krh@21
  1060
	pend = set->provides.data + set->provides.size;
krh@21
  1061
	pool = set->string_pool.data;
krh@21
  1062
	
krh@21
  1063
	while (r < rend) {
krh@21
  1064
		while (p < pend && strcmp(&pool[r->name], &pool[p->name]) > 0)
krh@21
  1065
			p++;
krh@21
  1066
		if (p == pend || strcmp(&pool[r->name], &pool[p->name]) != 0) {
krh@21
  1067
			u = array_add(unsatisfied, sizeof *u);
krh@21
  1068
			*u = r - (struct razor_property *) set->requires.data;
krh@21
  1069
		}
krh@21
  1070
		r++;
krh@21
  1071
	}
krh@21
  1072
}
krh@21
  1073
krh@21
  1074
void
krh@21
  1075
razor_set_list_unsatisfied(struct razor_set *set)
krh@21
  1076
{
krh@21
  1077
	struct array unsatisfied;
krh@21
  1078
	struct razor_property *requires, *r;
krh@21
  1079
	unsigned long *u, *end;
krh@21
  1080
	char *pool;
krh@21
  1081
krh@21
  1082
	array_init(&unsatisfied);
krh@21
  1083
	razor_set_validate(set, &unsatisfied);
krh@21
  1084
krh@21
  1085
	end = unsatisfied.data + unsatisfied.size;
krh@21
  1086
	requires = set->requires.data;
krh@21
  1087
	pool = set->string_pool.data;
krh@21
  1088
krh@21
  1089
	for (u = unsatisfied.data; u < end; u++) {
krh@21
  1090
		r = requires + *u;
krh@21
  1091
		printf("%s %s not satisfied\n",
krh@21
  1092
		       &pool[r->name], &pool[r->version]);
krh@21
  1093
	}
krh@21
  1094
krh@21
  1095
	array_release(&unsatisfied);
krh@21
  1096
}
krh@21
  1097
krh@21
  1098
void
krh@4
  1099
razor_set_info(struct razor_set *set)
krh@3
  1100
{
krh@3
  1101
	unsigned int offset, size;
krh@3
  1102
	int i;
krh@3
  1103
krh@4
  1104
	for (i = 0; i < set->header->sections[i].type; i++) {
krh@4
  1105
		offset = set->header->sections[i].offset;
krh@18
  1106
		size = set->header->sections[i].size;
krh@3
  1107
krh@4
  1108
		switch (set->header->sections[i].type) {
krh@4
  1109
		case RAZOR_PACKAGES:
krh@3
  1110
			printf("package section:\t%dkb\n", size / 1024);
krh@3
  1111
			break;
krh@8
  1112
		case RAZOR_REQUIRES:
krh@8
  1113
			printf("requires section:\t%dkb\n", size / 1024);
krh@8
  1114
			break;
krh@7
  1115
		case RAZOR_PROVIDES:
krh@7
  1116
			printf("provides section:\t%dkb\n", size / 1024);
krh@7
  1117
			break;
krh@19
  1118
		case RAZOR_STRING_POOL:
krh@19
  1119
			printf("string pool:\t\t%dkb\n", size / 1024);
krh@19
  1120
			break;
krh@19
  1121
		case RAZOR_PROPERTY_POOL:
krh@18
  1122
			printf("properties section:\t%dkb\n", size / 1024);
krh@18
  1123
			break;
krh@3
  1124
		}
krh@3
  1125
	}
krh@0
  1126
}
krh@0
  1127
krh@0
  1128
static int
krh@0
  1129
usage(void)
krh@0
  1130
{
krh@7
  1131
	printf("usage: razor [ import FILES | lookup <key> | "
krh@15
  1132
	       "list | list-requires | list-provides | eat-yum | info ]\n");
krh@0
  1133
	exit(1);
krh@0
  1134
}
krh@0
  1135
krh@15
  1136
static const char *repo_filename = "system.repo";
krh@15
  1137
static const char rawhide_repo_filename[] = "rawhide.repo";
krh@0
  1138
krh@0
  1139
int
krh@0
  1140
main(int argc, char *argv[])
krh@0
  1141
{
krh@0
  1142
	int i;
krh@4
  1143
	struct razor_set *set;
krh@0
  1144
	struct stat statbuf;
krh@9
  1145
	struct import_context ctx;
krh@15
  1146
	char *repo;
krh@15
  1147
krh@15
  1148
	repo = getenv("RAZOR_REPO");
krh@15
  1149
	if (repo != NULL)
krh@15
  1150
		repo_filename = repo;
krh@0
  1151
krh@3
  1152
	if (argc < 2) {
krh@0
  1153
		usage();
krh@0
  1154
	} else if (strcmp(argv[1], "import") == 0) {
krh@0
  1155
		if (stat("set", &statbuf) && mkdir("set", 0777)) {
krh@0
  1156
			fprintf(stderr, "could not create directory 'set'\n");
krh@0
  1157
			exit(-1);
krh@0
  1158
		}
krh@0
  1159
			
krh@13
  1160
		razor_prepare_import(&ctx);
krh@9
  1161
krh@0
  1162
		for (i = 2; i < argc; i++) {
krh@13
  1163
			if (razor_import(&ctx, argv[i]) < 0) {
krh@0
  1164
				fprintf(stderr, "failed to import %s\n",
krh@0
  1165
					argv[i]);
krh@0
  1166
				exit(-1);
krh@0
  1167
			}
krh@0
  1168
		}
krh@0
  1169
krh@13
  1170
		set = razor_finish_import(&ctx);
krh@6
  1171
krh@6
  1172
		printf("bucket allocation: %d\n", set->buckets.alloc);
krh@6
  1173
		printf("pool size: %d\n", set->string_pool.size);
krh@6
  1174
		printf("pool allocation: %d\n", set->string_pool.alloc);
krh@7
  1175
		printf("packages: %d\n",
krh@7
  1176
		       set->packages.size / sizeof(struct razor_package));
krh@8
  1177
		printf("requires: %d\n",
krh@8
  1178
		       set->requires.size / sizeof(struct razor_property));
krh@7
  1179
		printf("provides: %d\n",
krh@8
  1180
		       set->provides.size / sizeof(struct razor_property));
krh@0
  1181
krh@4
  1182
		razor_set_write(set, repo_filename);
krh@0
  1183
krh@4
  1184
		razor_set_destroy(set);
krh@0
  1185
	} else if (strcmp(argv[1], "lookup") == 0) {
krh@4
  1186
		set = razor_set_open(repo_filename);
krh@0
  1187
		printf("%s is %lu\n", argv[2],
krh@4
  1188
		       razor_set_lookup(set, argv[2]));
krh@4
  1189
		razor_set_destroy(set);
krh@3
  1190
	} else if (strcmp(argv[1], "list") == 0) {
krh@4
  1191
		set = razor_set_open(repo_filename);
krh@4
  1192
		razor_set_list(set);
krh@4
  1193
		razor_set_destroy(set);
krh@8
  1194
	} else if (strcmp(argv[1], "list-requires") == 0) {
krh@8
  1195
		set = razor_set_open(repo_filename);
krh@10
  1196
		razor_set_list_requires(set, argv[2]);
krh@8
  1197
		razor_set_destroy(set);
krh@7
  1198
	} else if (strcmp(argv[1], "list-provides") == 0) {
krh@7
  1199
		set = razor_set_open(repo_filename);
krh@10
  1200
		razor_set_list_provides(set, argv[2]);
krh@7
  1201
		razor_set_destroy(set);
krh@18
  1202
	} else if (strcmp(argv[1], "what-requires") == 0) {
krh@18
  1203
		set = razor_set_open(repo_filename);
krh@20
  1204
		razor_set_list_property_packages(set, &set->requires,
krh@20
  1205
						 argv[2], argv[3]);
krh@18
  1206
		razor_set_destroy(set);
krh@18
  1207
	} else if (strcmp(argv[1], "what-provides") == 0) {
krh@18
  1208
		set = razor_set_open(repo_filename);
krh@20
  1209
		razor_set_list_property_packages(set, &set->provides,
krh@20
  1210
						 argv[2], argv[3]);
krh@18
  1211
		razor_set_destroy(set);
krh@3
  1212
	} else if (strcmp(argv[1], "info") == 0) {
krh@4
  1213
		set = razor_set_open(repo_filename);
krh@4
  1214
		razor_set_info(set);
krh@4
  1215
		razor_set_destroy(set);
krh@15
  1216
	} else if (strcmp(argv[1], "eat-yum") == 0) {
krh@15
  1217
		set = razor_set_create_from_yum_filelist(STDIN_FILENO);
krh@15
  1218
		if (set == NULL)
krh@15
  1219
			return 1;
krh@15
  1220
		razor_set_write(set, rawhide_repo_filename);
krh@15
  1221
		razor_set_destroy(set);
krh@18
  1222
		printf("wrote %s\n", rawhide_repo_filename);
krh@21
  1223
	} else if (strcmp(argv[1], "validate") == 0) {
krh@21
  1224
		set = razor_set_open(repo_filename);
krh@21
  1225
		if (set == NULL)
krh@21
  1226
			return 1;
krh@21
  1227
		razor_set_list_unsatisfied(set);
krh@21
  1228
		razor_set_destroy(set);
krh@0
  1229
	} else {
krh@0
  1230
		usage();
krh@0
  1231
	}
krh@0
  1232
krh@0
  1233
	return 0;
krh@0
  1234
}