Split importers to import.c.
authorKristian H?gsberg <krh@redhat.com>
Tue Sep 18 15:02:04 2007 -0400 (2007-09-18)
changeset 275dbd81809d26
parent 26 b1f244b3ff22
child 28 c8958f67afd8
Split importers to import.c.
Makefile
import.c
razor.c
razor.h
     1.1 --- a/Makefile	Tue Sep 18 14:16:35 2007 -0400
     1.2 +++ b/Makefile	Tue Sep 18 15:02:04 2007 -0400
     1.3 @@ -1,7 +1,7 @@
     1.4  CFLAGS = -Wall -g -O2
     1.5  LDLIBS = -lexpat -g
     1.6  
     1.7 -razor : razor.o sha1.o
     1.8 +razor : razor.o import.o sha1.o
     1.9  
    1.10  import : razor primary.xml.gz
    1.11  	zcat primary.xml.gz | ./razor eat-yum
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/import.c	Tue Sep 18 15:02:04 2007 -0400
     2.3 @@ -0,0 +1,259 @@
     2.4 +#define _GNU_SOURCE
     2.5 +
     2.6 +#include <string.h>
     2.7 +#include <stdio.h>
     2.8 +#include <sys/stat.h>
     2.9 +#include <sys/mman.h>
    2.10 +#include <unistd.h>
    2.11 +#include <fcntl.h>
    2.12 +#include <errno.h>
    2.13 +
    2.14 +#include <expat.h>
    2.15 +#include "sha1.h"
    2.16 +#include "razor.h"
    2.17 +
    2.18 +static void
    2.19 +parse_package(struct import_context *ctx, const char **atts, void *data)
    2.20 +{
    2.21 +	const char *name = NULL, *version = NULL;
    2.22 +	int i;
    2.23 +
    2.24 +	for (i = 0; atts[i]; i += 2) {
    2.25 +		if (strcmp(atts[i], "name") == 0)
    2.26 +			name = atts[i + 1];
    2.27 +		else if (strcmp(atts[i], "version") == 0)
    2.28 +			version = atts[i + 1];
    2.29 +	}
    2.30 +
    2.31 +	if (name == NULL || version == NULL) {
    2.32 +		fprintf(stderr, "invalid package tag, "
    2.33 +			"missing name or version attributes\n");
    2.34 +		return;
    2.35 +	}
    2.36 +
    2.37 +	import_context_add_package(ctx, name, version);
    2.38 +}
    2.39 +
    2.40 +static void
    2.41 +parse_property(struct import_context *ctx, const char **atts, void *data)
    2.42 +{
    2.43 +	const char *name = NULL, *version = NULL;
    2.44 +	int i;
    2.45 +
    2.46 +	for (i = 0; atts[i]; i += 2) {
    2.47 +		if (strcmp(atts[i], "name") == 0)
    2.48 +			name = atts[i + 1];
    2.49 +		if (strcmp(atts[i], "version") == 0)
    2.50 +			version = atts[i + 1];
    2.51 +	}
    2.52 +	
    2.53 +	if (name == NULL) {
    2.54 +		fprintf(stderr, "invalid tag, missing name attribute\n");
    2.55 +		return;
    2.56 +	}
    2.57 +
    2.58 +	import_context_add_property(ctx, data, name, version);
    2.59 +}
    2.60 +
    2.61 +static void
    2.62 +start_element(void *data, const char *name, const char **atts)
    2.63 +{
    2.64 +	struct import_context *ctx = data;
    2.65 +
    2.66 +	if (strcmp(name, "package") == 0)
    2.67 +		parse_package(ctx, atts, NULL);
    2.68 +	else if (strcmp(name, "requires") == 0)
    2.69 +		parse_property(ctx, atts, &ctx->requires);
    2.70 +	else if (strcmp(name, "provides") == 0)
    2.71 +		parse_property(ctx, atts, &ctx->provides);
    2.72 +}
    2.73 +
    2.74 +static void
    2.75 +end_element (void *data, const char *name)
    2.76 +{
    2.77 +	struct import_context *ctx = data;
    2.78 +
    2.79 +	if (strcmp(name, "package") == 0)
    2.80 +		import_context_finish_package(ctx);
    2.81 +}
    2.82 +
    2.83 +static int
    2.84 +import_rzr_file(struct import_context *ctx, const char *filename)
    2.85 +{
    2.86 +	SHA_CTX sha1;
    2.87 +	XML_Parser parser;
    2.88 +	int fd;
    2.89 +	void *p;
    2.90 +	struct stat stat;
    2.91 +	unsigned char hash[20];
    2.92 +
    2.93 +	fd = open(filename, O_RDONLY);
    2.94 +	if (fstat(fd, &stat) < 0)
    2.95 +		return -1;
    2.96 +	p = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
    2.97 +	if (p == MAP_FAILED)
    2.98 +		return -1;
    2.99 +
   2.100 +	parser = XML_ParserCreate(NULL);
   2.101 +	XML_SetUserData(parser, ctx);
   2.102 +	XML_SetElementHandler(parser, start_element, end_element);
   2.103 +	if (XML_Parse(parser, p, stat.st_size, 1) == XML_STATUS_ERROR) {
   2.104 +		fprintf(stderr,
   2.105 +			"%s at line %d, %s\n",
   2.106 +			XML_ErrorString(XML_GetErrorCode(parser)),
   2.107 +			XML_GetCurrentLineNumber(parser),
   2.108 +			filename);
   2.109 +		return 1;
   2.110 +	}
   2.111 +
   2.112 +	XML_ParserFree(parser);
   2.113 +
   2.114 +	SHA1_Init(&sha1);
   2.115 +	SHA1_Update(&sha1, p, stat.st_size);
   2.116 +	SHA1_Final(hash, &sha1);
   2.117 +
   2.118 +	close(fd);
   2.119 +
   2.120 +	munmap(p, stat.st_size);
   2.121 +
   2.122 +	return 0;
   2.123 +}
   2.124 +
   2.125 +struct razor_set *
   2.126 +razor_import_rzr_files(int count, const char *files[])
   2.127 +{
   2.128 +	struct import_context ctx;
   2.129 +	int i;
   2.130 +
   2.131 +	razor_prepare_import(&ctx);
   2.132 +
   2.133 +	for (i = 0; i < count; i++) {
   2.134 +		if (import_rzr_file(&ctx, files[i]) < 0) {
   2.135 +			fprintf(stderr, "failed to import %s\n", files[i]);
   2.136 +			exit(-1);
   2.137 +		}
   2.138 +	}
   2.139 +
   2.140 +	return razor_finish_import(&ctx);
   2.141 +}
   2.142 +
   2.143 +/* Import a yum filelist as a razor package set. */
   2.144 +
   2.145 +enum {
   2.146 +	YUM_STATE_BEGIN,
   2.147 +	YUM_STATE_PACKAGE_NAME
   2.148 +};
   2.149 +
   2.150 +struct yum_context {
   2.151 +	struct import_context ctx;
   2.152 +	struct import_property_context *current_property_context;
   2.153 +	char *name;
   2.154 +	int state;
   2.155 +};
   2.156 +
   2.157 +static void
   2.158 +yum_start_element(void *data, const char *name, const char **atts)
   2.159 +{
   2.160 +	struct yum_context *ctx = data;
   2.161 +	const char *n, *version;
   2.162 +	int i;
   2.163 +
   2.164 +	if (strcmp(name, "name") == 0) {
   2.165 +		ctx->state = YUM_STATE_PACKAGE_NAME;
   2.166 +	} else if (strcmp(name, "version") == 0) {
   2.167 +		version = NULL;
   2.168 +		for (i = 0; atts[i]; i += 2) {
   2.169 +			if (strcmp(atts[i], "ver") == 0)
   2.170 +				version = atts[i + 1];
   2.171 +		}
   2.172 +		import_context_add_package(&ctx->ctx, ctx->name, version);
   2.173 +	} else if (strcmp(name, "rpm:requires") == 0) {
   2.174 +		ctx->current_property_context = &ctx->ctx.requires;
   2.175 +	} else if (strcmp(name, "rpm:provides") == 0) {
   2.176 +		ctx->current_property_context = &ctx->ctx.provides;
   2.177 +	} else if (strcmp(name, "rpm:entry") == 0 &&
   2.178 +		   ctx->current_property_context != NULL) {
   2.179 +		n = NULL;
   2.180 +		version = NULL;
   2.181 +		for (i = 0; atts[i]; i += 2) {
   2.182 +			if (strcmp(atts[i], "name") == 0)
   2.183 +				n = atts[i + 1];
   2.184 +			else if (strcmp(atts[i], "ver") == 0)
   2.185 +				version = atts[i + 1];
   2.186 +		}
   2.187 +
   2.188 +		if (n == NULL) {
   2.189 +			fprintf(stderr, "invalid rpm:entry, "
   2.190 +				"missing name or version attributes\n");
   2.191 +			return;
   2.192 +		}
   2.193 +
   2.194 +		import_context_add_property(&ctx->ctx,
   2.195 +					    ctx->current_property_context,
   2.196 +					    n, version);
   2.197 +	}
   2.198 +}
   2.199 +
   2.200 +static void
   2.201 +yum_end_element (void *data, const char *name)
   2.202 +{
   2.203 +	struct yum_context *ctx = data;
   2.204 +
   2.205 +	if (strcmp(name, "package") == 0) {
   2.206 +		free(ctx->name);
   2.207 +		import_context_finish_package(&ctx->ctx);
   2.208 +	} else if (strcmp(name, "name") == 0) {
   2.209 +		ctx->state = 0;
   2.210 +	} else if (strcmp(name, "rpm:requires") == 0) {
   2.211 +		ctx->current_property_context = NULL;
   2.212 +	} else if (strcmp(name, "rpm:provides") == 0) {
   2.213 +		ctx->current_property_context = NULL;
   2.214 +	}
   2.215 +}
   2.216 +
   2.217 +static void
   2.218 +yum_character_data (void *data, const XML_Char *s, int len)
   2.219 +{
   2.220 +	struct yum_context *ctx = data;
   2.221 +
   2.222 +	if (ctx->state == YUM_STATE_PACKAGE_NAME)
   2.223 +		ctx->name = strndup(s, len);
   2.224 +}
   2.225 +
   2.226 +struct razor_set *
   2.227 +razor_set_create_from_yum_filelist(int fd)
   2.228 +{
   2.229 +	struct yum_context ctx;
   2.230 +	XML_Parser parser;
   2.231 +	char buf[4096];
   2.232 +	int len;
   2.233 +
   2.234 +	razor_prepare_import(&ctx.ctx);
   2.235 +
   2.236 +	parser = XML_ParserCreate(NULL);
   2.237 +	XML_SetUserData(parser, &ctx);
   2.238 +	XML_SetElementHandler(parser, yum_start_element, yum_end_element);
   2.239 +	XML_SetCharacterDataHandler(parser, yum_character_data);
   2.240 +
   2.241 +	while (1) {
   2.242 +		len = read(fd, buf, sizeof buf);
   2.243 +		if (len < 0) {
   2.244 +			fprintf(stderr,
   2.245 +				"couldn't read input: %s\n", strerror(errno));
   2.246 +			return NULL;
   2.247 +		} else if (len == 0)
   2.248 +			break;
   2.249 +
   2.250 +		if (XML_Parse(parser, buf, len, 0) == XML_STATUS_ERROR) {
   2.251 +			fprintf(stderr,
   2.252 +				"%s at line %d\n",
   2.253 +				XML_ErrorString(XML_GetErrorCode(parser)),
   2.254 +				XML_GetCurrentLineNumber(parser));
   2.255 +			return NULL;
   2.256 +		}
   2.257 +	}
   2.258 +
   2.259 +	XML_ParserFree(parser);
   2.260 +
   2.261 +	return razor_finish_import(&ctx.ctx);
   2.262 +}
     3.1 --- a/razor.c	Tue Sep 18 14:16:35 2007 -0400
     3.2 +++ b/razor.c	Tue Sep 18 15:02:04 2007 -0400
     3.3 @@ -11,15 +11,7 @@
     3.4  #include <fcntl.h>
     3.5  #include <errno.h>
     3.6  
     3.7 -#include <expat.h>
     3.8 -#include "sha1.h"
     3.9 -
    3.10 -#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
    3.11 -
    3.12 -struct array {
    3.13 -	void *data;
    3.14 -	int size, alloc;
    3.15 -};
    3.16 +#include "razor.h"
    3.17  
    3.18  static void
    3.19  array_init(struct array *array)
    3.20 @@ -77,20 +69,6 @@
    3.21  	return 0;
    3.22  }
    3.23  
    3.24 -static int
    3.25 -write_to_file(const char *filename, void *p, size_t size)
    3.26 -{
    3.27 -	int fd, err;
    3.28 -
    3.29 -	fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666);
    3.30 -	if (fd < 0)
    3.31 -		return -1;
    3.32 -	err = write_to_fd(fd, p, size);
    3.33 -	close(fd);
    3.34 -
    3.35 -	return err;
    3.36 -}
    3.37 -
    3.38  static void *
    3.39  zalloc(size_t size)
    3.40  {
    3.41 @@ -102,50 +80,6 @@
    3.42  	return p;
    3.43  }
    3.44  
    3.45 -struct razor_set_section {
    3.46 -	unsigned int type;
    3.47 -	unsigned int offset;
    3.48 -	unsigned int size;
    3.49 -};
    3.50 -
    3.51 -struct razor_set_header {
    3.52 -	unsigned int magic;
    3.53 -	unsigned int version;
    3.54 -	struct razor_set_section sections[0];
    3.55 -};
    3.56 -
    3.57 -#define RAZOR_MAGIC 0x7a7a7a7a
    3.58 -#define RAZOR_VERSION 1
    3.59 -
    3.60 -#define RAZOR_PACKAGES 0
    3.61 -#define RAZOR_REQUIRES 1
    3.62 -#define RAZOR_PROVIDES 2
    3.63 -#define RAZOR_STRING_POOL 3
    3.64 -#define RAZOR_PROPERTY_POOL 4
    3.65 -
    3.66 -struct razor_package {
    3.67 -	unsigned long name;
    3.68 -	unsigned long version;
    3.69 -	unsigned long requires;
    3.70 -	unsigned long provides;
    3.71 -};
    3.72 -
    3.73 -struct razor_property {
    3.74 -	unsigned long name;
    3.75 -	unsigned long version;
    3.76 -	unsigned long packages;
    3.77 -};
    3.78 -
    3.79 -struct razor_set {
    3.80 -	struct array buckets;
    3.81 -	struct array string_pool;
    3.82 -	struct array property_pool;
    3.83 - 	struct array packages;
    3.84 - 	struct array requires;
    3.85 - 	struct array provides;
    3.86 -	struct razor_set_header *header;
    3.87 -};
    3.88 -
    3.89  struct razor_set_section razor_sections[] = {
    3.90  	{ RAZOR_PACKAGES,	offsetof(struct razor_set, packages) },
    3.91  	{ RAZOR_REQUIRES,	offsetof(struct razor_set, requires) },
    3.92 @@ -386,21 +320,7 @@
    3.93  	return razor_set_insert(set, string);
    3.94  }
    3.95  
    3.96 -struct import_property_context {
    3.97 -	struct array *all;
    3.98 -	struct array package;
    3.99 -};
   3.100 -
   3.101 -struct import_context {
   3.102 -	struct razor_set *set;
   3.103 -	struct import_property_context requires;
   3.104 -	struct import_property_context provides;
   3.105 -	struct razor_package *package;
   3.106 -	unsigned long *requires_map;
   3.107 -	unsigned long *provides_map;
   3.108 -};
   3.109 -
   3.110 -static void
   3.111 +void
   3.112  import_context_add_package(struct import_context *ctx,
   3.113  			   const char *name, const char *version)
   3.114  {
   3.115 @@ -428,7 +348,7 @@
   3.116  	array_release(&ctx->provides.package);
   3.117  }
   3.118  
   3.119 -static void
   3.120 +void
   3.121  import_context_add_property(struct import_context *ctx,
   3.122  			    struct import_property_context *pctx,
   3.123  			    const char *name, const char *version)
   3.124 @@ -446,91 +366,7 @@
   3.125  	*r = p - (struct razor_property *) pctx->all->data;
   3.126  }
   3.127  
   3.128 -static void
   3.129 -parse_package(struct import_context *ctx, const char **atts, void *data)
   3.130 -{
   3.131 -	const char *name = NULL, *version = NULL;
   3.132 -	int i;
   3.133 -
   3.134 -	for (i = 0; atts[i]; i += 2) {
   3.135 -		if (strcmp(atts[i], "name") == 0)
   3.136 -			name = atts[i + 1];
   3.137 -		else if (strcmp(atts[i], "version") == 0)
   3.138 -			version = atts[i + 1];
   3.139 -	}
   3.140 -
   3.141 -	if (name == NULL || version == NULL) {
   3.142 -		fprintf(stderr, "invalid package tag, "
   3.143 -			"missing name or version attributes\n");
   3.144 -		return;
   3.145 -	}
   3.146 -
   3.147 -	import_context_add_package(ctx, name, version);
   3.148 -}
   3.149 -
   3.150 -static void
   3.151 -parse_property(struct import_context *ctx, const char **atts, void *data)
   3.152 -{
   3.153 -	const char *name = NULL, *version = NULL;
   3.154 -	int i;
   3.155 -
   3.156 -	for (i = 0; atts[i]; i += 2) {
   3.157 -		if (strcmp(atts[i], "name") == 0)
   3.158 -			name = atts[i + 1];
   3.159 -		if (strcmp(atts[i], "version") == 0)
   3.160 -			version = atts[i + 1];
   3.161 -	}
   3.162 -	
   3.163 -	if (name == NULL) {
   3.164 -		fprintf(stderr, "invalid tag, missing name attribute\n");
   3.165 -		return;
   3.166 -	}
   3.167 -
   3.168 -	import_context_add_property(ctx, data, name, version);
   3.169 -}
   3.170 -
   3.171 -static void
   3.172 -start_element(void *data, const char *name, const char **atts)
   3.173 -{
   3.174 -	struct import_context *ctx = data;
   3.175 -
   3.176 -	if (strcmp(name, "package") == 0)
   3.177 -		parse_package(ctx, atts, NULL);
   3.178 -	else if (strcmp(name, "requires") == 0)
   3.179 -		parse_property(ctx, atts, &ctx->requires);
   3.180 -	else if (strcmp(name, "provides") == 0)
   3.181 -		parse_property(ctx, atts, &ctx->provides);
   3.182 -}
   3.183 -
   3.184 -static void
   3.185 -end_element (void *data, const char *name)
   3.186 -{
   3.187 -	struct import_context *ctx = data;
   3.188 -
   3.189 -	if (strcmp(name, "package") == 0)
   3.190 -		import_context_finish_package(ctx);
   3.191 -}
   3.192 -
   3.193 -static char *
   3.194 -sha1_to_hex(const unsigned char *sha1)
   3.195 -{
   3.196 -	static int bufno;
   3.197 -	static char hexbuffer[4][50];
   3.198 -	static const char hex[] = "0123456789abcdef";
   3.199 -	char *buffer = hexbuffer[3 & ++bufno], *buf = buffer;
   3.200 -	int i;
   3.201 -
   3.202 -	for (i = 0; i < 20; i++) {
   3.203 -		unsigned int val = *sha1++;
   3.204 -		*buf++ = hex[val >> 4];
   3.205 -		*buf++ = hex[val & 0xf];
   3.206 -	}
   3.207 -	*buf = '\0';
   3.208 -
   3.209 -	return buffer;
   3.210 -}
   3.211 -
   3.212 -static void
   3.213 +void
   3.214  razor_prepare_import(struct import_context *ctx)
   3.215  {
   3.216  	memset(ctx, 0, sizeof *ctx);
   3.217 @@ -539,52 +375,6 @@
   3.218  	ctx->provides.all = &ctx->set->provides;
   3.219  }
   3.220  
   3.221 -static int
   3.222 -razor_import(struct import_context *ctx, const char *filename)
   3.223 -{
   3.224 -	SHA_CTX sha1;
   3.225 -	XML_Parser parser;
   3.226 -	int fd;
   3.227 -	void *p;
   3.228 -	struct stat stat;
   3.229 -	char buf[128];
   3.230 -	unsigned char hash[20];
   3.231 -
   3.232 -	fd = open(filename, O_RDONLY);
   3.233 -	if (fstat(fd, &stat) < 0)
   3.234 -		return -1;
   3.235 -	p = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
   3.236 -	if (p == MAP_FAILED)
   3.237 -		return -1;
   3.238 -
   3.239 -	parser = XML_ParserCreate(NULL);
   3.240 -	XML_SetUserData(parser, ctx);
   3.241 -	XML_SetElementHandler(parser, start_element, end_element);
   3.242 -	if (XML_Parse(parser, p, stat.st_size, 1) == XML_STATUS_ERROR) {
   3.243 -		fprintf(stderr,
   3.244 -			"%s at line %d, %s\n",
   3.245 -			XML_ErrorString(XML_GetErrorCode(parser)),
   3.246 -			XML_GetCurrentLineNumber(parser),
   3.247 -			filename);
   3.248 -		return 1;
   3.249 -	}
   3.250 -
   3.251 -	XML_ParserFree(parser);
   3.252 -
   3.253 -	SHA1_Init(&sha1);
   3.254 -	SHA1_Update(&sha1, p, stat.st_size);
   3.255 -	SHA1_Final(hash, &sha1);
   3.256 -
   3.257 -	close(fd);
   3.258 -
   3.259 -	snprintf(buf, sizeof buf, "set/%s", sha1_to_hex(hash));
   3.260 -	if (write_to_file(buf, p, stat.st_size) < 0)
   3.261 -		return -1;
   3.262 -	munmap(p, stat.st_size);
   3.263 -
   3.264 -	return 0;
   3.265 -}
   3.266 -
   3.267  typedef int (*compare_with_data_func_t)(const void *p1,
   3.268  					const void *p,
   3.269  					void *data);
   3.270 @@ -800,7 +590,7 @@
   3.271  	free(rmap);
   3.272  }
   3.273  
   3.274 -static struct razor_set *
   3.275 +struct razor_set *
   3.276  razor_finish_import(struct import_context *ctx)
   3.277  {
   3.278  	unsigned long *map;
   3.279 @@ -824,127 +614,6 @@
   3.280  	return ctx->set; 
   3.281  }
   3.282  
   3.283 -/* Import a yum filelist as a razor package set. */
   3.284 -
   3.285 -enum {
   3.286 -	YUM_STATE_BEGIN,
   3.287 -	YUM_STATE_PACKAGE_NAME
   3.288 -};
   3.289 -
   3.290 -struct yum_context {
   3.291 -	struct import_context ctx;
   3.292 -	struct import_property_context *current_property_context;
   3.293 -	char *name;
   3.294 -	int state;
   3.295 -};
   3.296 -
   3.297 -static void
   3.298 -yum_start_element(void *data, const char *name, const char **atts)
   3.299 -{
   3.300 -	struct yum_context *ctx = data;
   3.301 -	const char *n, *version;
   3.302 -	int i;
   3.303 -
   3.304 -	if (strcmp(name, "name") == 0) {
   3.305 -		ctx->state = YUM_STATE_PACKAGE_NAME;
   3.306 -	} else if (strcmp(name, "version") == 0) {
   3.307 -		version = NULL;
   3.308 -		for (i = 0; atts[i]; i += 2) {
   3.309 -			if (strcmp(atts[i], "ver") == 0)
   3.310 -				version = atts[i + 1];
   3.311 -		}
   3.312 -		import_context_add_package(&ctx->ctx, ctx->name, version);
   3.313 -	} else if (strcmp(name, "rpm:requires") == 0) {
   3.314 -		ctx->current_property_context = &ctx->ctx.requires;
   3.315 -	} else if (strcmp(name, "rpm:provides") == 0) {
   3.316 -		ctx->current_property_context = &ctx->ctx.provides;
   3.317 -	} else if (strcmp(name, "rpm:entry") == 0 &&
   3.318 -		   ctx->current_property_context != NULL) {
   3.319 -		n = NULL;
   3.320 -		version = NULL;
   3.321 -		for (i = 0; atts[i]; i += 2) {
   3.322 -			if (strcmp(atts[i], "name") == 0)
   3.323 -				n = atts[i + 1];
   3.324 -			else if (strcmp(atts[i], "ver") == 0)
   3.325 -				version = atts[i + 1];
   3.326 -		}
   3.327 -
   3.328 -		if (n == NULL) {
   3.329 -			fprintf(stderr, "invalid rpm:entry, "
   3.330 -				"missing name or version attributes\n");
   3.331 -			return;
   3.332 -		}
   3.333 -
   3.334 -		import_context_add_property(&ctx->ctx,
   3.335 -					    ctx->current_property_context,
   3.336 -					    n, version);
   3.337 -	}
   3.338 -}
   3.339 -
   3.340 -static void
   3.341 -yum_end_element (void *data, const char *name)
   3.342 -{
   3.343 -	struct yum_context *ctx = data;
   3.344 -
   3.345 -	if (strcmp(name, "package") == 0) {
   3.346 -		free(ctx->name);
   3.347 -		import_context_finish_package(&ctx->ctx);
   3.348 -	} else if (strcmp(name, "name") == 0) {
   3.349 -		ctx->state = 0;
   3.350 -	} else if (strcmp(name, "rpm:requires") == 0) {
   3.351 -		ctx->current_property_context = NULL;
   3.352 -	} else if (strcmp(name, "rpm:provides") == 0) {
   3.353 -		ctx->current_property_context = NULL;
   3.354 -	}
   3.355 -}
   3.356 -
   3.357 -static void
   3.358 -yum_character_data (void *data, const XML_Char *s, int len)
   3.359 -{
   3.360 -	struct yum_context *ctx = data;
   3.361 -
   3.362 -	if (ctx->state == YUM_STATE_PACKAGE_NAME)
   3.363 -		ctx->name = strndup(s, len);
   3.364 -}
   3.365 -
   3.366 -static struct razor_set *
   3.367 -razor_set_create_from_yum_filelist(int fd)
   3.368 -{
   3.369 -	struct yum_context ctx;
   3.370 -	XML_Parser parser;
   3.371 -	char buf[4096];
   3.372 -	int len;
   3.373 -
   3.374 -	razor_prepare_import(&ctx.ctx);
   3.375 -
   3.376 -	parser = XML_ParserCreate(NULL);
   3.377 -	XML_SetUserData(parser, &ctx);
   3.378 -	XML_SetElementHandler(parser, yum_start_element, yum_end_element);
   3.379 -	XML_SetCharacterDataHandler(parser, yum_character_data);
   3.380 -
   3.381 -	while (1) {
   3.382 -		len = read(fd, buf, sizeof buf);
   3.383 -		if (len < 0) {
   3.384 -			fprintf(stderr,
   3.385 -				"couldn't read input: %s\n", strerror(errno));
   3.386 -			return NULL;
   3.387 -		} else if (len == 0)
   3.388 -			break;
   3.389 -
   3.390 -		if (XML_Parse(parser, buf, len, 0) == XML_STATUS_ERROR) {
   3.391 -			fprintf(stderr,
   3.392 -				"%s at line %d\n",
   3.393 -				XML_ErrorString(XML_GetErrorCode(parser)),
   3.394 -				XML_GetCurrentLineNumber(parser));
   3.395 -			return NULL;
   3.396 -		}
   3.397 -	}
   3.398 -
   3.399 -	XML_ParserFree(parser);
   3.400 -
   3.401 -	return razor_finish_import(&ctx.ctx);
   3.402 -}
   3.403 -
   3.404  void
   3.405  razor_set_list(struct razor_set *set)
   3.406  {
   3.407 @@ -1188,12 +857,10 @@
   3.408  static const char rawhide_repo_filename[] = "rawhide.repo";
   3.409  
   3.410  int
   3.411 -main(int argc, char *argv[])
   3.412 +main(int argc, const char *argv[])
   3.413  {
   3.414 -	int i;
   3.415  	struct razor_set *set;
   3.416  	struct stat statbuf;
   3.417 -	struct import_context ctx;
   3.418  	char *repo;
   3.419  
   3.420  	repo = getenv("RAZOR_REPO");
   3.421 @@ -1208,17 +875,7 @@
   3.422  			exit(-1);
   3.423  		}
   3.424  			
   3.425 -		razor_prepare_import(&ctx);
   3.426 -
   3.427 -		for (i = 2; i < argc; i++) {
   3.428 -			if (razor_import(&ctx, argv[i]) < 0) {
   3.429 -				fprintf(stderr, "failed to import %s\n",
   3.430 -					argv[i]);
   3.431 -				exit(-1);
   3.432 -			}
   3.433 -		}
   3.434 -
   3.435 -		set = razor_finish_import(&ctx);
   3.436 +		set = razor_import_rzr_files(argc - 2, argv + 2);
   3.437  
   3.438  		printf("bucket allocation: %d\n", set->buckets.alloc);
   3.439  		printf("pool size: %d\n", set->string_pool.size);
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/razor.h	Tue Sep 18 15:02:04 2007 -0400
     4.3 @@ -0,0 +1,83 @@
     4.4 +#ifndef _RAZOR_H_
     4.5 +#define _RAZOR_H_
     4.6 +
     4.7 +#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
     4.8 +
     4.9 +struct array {
    4.10 +	void *data;
    4.11 +	int size, alloc;
    4.12 +};
    4.13 +
    4.14 +struct razor_set_section {
    4.15 +	unsigned int type;
    4.16 +	unsigned int offset;
    4.17 +	unsigned int size;
    4.18 +};
    4.19 +
    4.20 +struct razor_set_header {
    4.21 +	unsigned int magic;
    4.22 +	unsigned int version;
    4.23 +	struct razor_set_section sections[0];
    4.24 +};
    4.25 +
    4.26 +#define RAZOR_MAGIC 0x7a7a7a7a
    4.27 +#define RAZOR_VERSION 1
    4.28 +
    4.29 +#define RAZOR_PACKAGES 0
    4.30 +#define RAZOR_REQUIRES 1
    4.31 +#define RAZOR_PROVIDES 2
    4.32 +#define RAZOR_STRING_POOL 3
    4.33 +#define RAZOR_PROPERTY_POOL 4
    4.34 +
    4.35 +struct razor_package {
    4.36 +	unsigned long name;
    4.37 +	unsigned long version;
    4.38 +	unsigned long requires;
    4.39 +	unsigned long provides;
    4.40 +};
    4.41 +
    4.42 +struct razor_property {
    4.43 +	unsigned long name;
    4.44 +	unsigned long version;
    4.45 +	unsigned long packages;
    4.46 +};
    4.47 +
    4.48 +struct razor_set {
    4.49 +	struct array buckets;
    4.50 +	struct array string_pool;
    4.51 +	struct array property_pool;
    4.52 + 	struct array packages;
    4.53 + 	struct array requires;
    4.54 + 	struct array provides;
    4.55 +	struct razor_set_header *header;
    4.56 +};
    4.57 +
    4.58 +struct import_property_context {
    4.59 +	struct array *all;
    4.60 +	struct array package;
    4.61 +};
    4.62 +
    4.63 +struct import_context {
    4.64 +	struct razor_set *set;
    4.65 +	struct import_property_context requires;
    4.66 +	struct import_property_context provides;
    4.67 +	struct razor_package *package;
    4.68 +	unsigned long *requires_map;
    4.69 +	unsigned long *provides_map;
    4.70 +};
    4.71 +
    4.72 +void import_context_add_package(struct import_context *ctx,
    4.73 +				const char *name, const char *version);
    4.74 +void import_context_add_property(struct import_context *ctx,
    4.75 +				 struct import_property_context *pctx,
    4.76 +				 const char *name, const char *version);
    4.77 +void import_context_finish_package(struct import_context *ctx);
    4.78 +
    4.79 +unsigned long razor_set_tokenize(struct razor_set *set, const char *string);
    4.80 +void razor_prepare_import(struct import_context *ctx);
    4.81 +struct razor_set *razor_finish_import(struct import_context *ctx);
    4.82 +
    4.83 +struct razor_set *razor_import_rzr_files(int count, const char **files);
    4.84 +struct razor_set *razor_set_create_from_yum_filelist(int fd);
    4.85 +
    4.86 +#endif /* _RAZOR_H_ */