razor.c
changeset 27 5dbd81809d26
parent 25 09278d69f386
child 28 c8958f67afd8
     1.1 --- a/razor.c	Fri Sep 14 13:22:44 2007 -0400
     1.2 +++ b/razor.c	Tue Sep 18 15:02:04 2007 -0400
     1.3 @@ -11,15 +11,7 @@
     1.4  #include <fcntl.h>
     1.5  #include <errno.h>
     1.6  
     1.7 -#include <expat.h>
     1.8 -#include "sha1.h"
     1.9 -
    1.10 -#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
    1.11 -
    1.12 -struct array {
    1.13 -	void *data;
    1.14 -	int size, alloc;
    1.15 -};
    1.16 +#include "razor.h"
    1.17  
    1.18  static void
    1.19  array_init(struct array *array)
    1.20 @@ -77,20 +69,6 @@
    1.21  	return 0;
    1.22  }
    1.23  
    1.24 -static int
    1.25 -write_to_file(const char *filename, void *p, size_t size)
    1.26 -{
    1.27 -	int fd, err;
    1.28 -
    1.29 -	fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666);
    1.30 -	if (fd < 0)
    1.31 -		return -1;
    1.32 -	err = write_to_fd(fd, p, size);
    1.33 -	close(fd);
    1.34 -
    1.35 -	return err;
    1.36 -}
    1.37 -
    1.38  static void *
    1.39  zalloc(size_t size)
    1.40  {
    1.41 @@ -102,50 +80,6 @@
    1.42  	return p;
    1.43  }
    1.44  
    1.45 -struct razor_set_section {
    1.46 -	unsigned int type;
    1.47 -	unsigned int offset;
    1.48 -	unsigned int size;
    1.49 -};
    1.50 -
    1.51 -struct razor_set_header {
    1.52 -	unsigned int magic;
    1.53 -	unsigned int version;
    1.54 -	struct razor_set_section sections[0];
    1.55 -};
    1.56 -
    1.57 -#define RAZOR_MAGIC 0x7a7a7a7a
    1.58 -#define RAZOR_VERSION 1
    1.59 -
    1.60 -#define RAZOR_PACKAGES 0
    1.61 -#define RAZOR_REQUIRES 1
    1.62 -#define RAZOR_PROVIDES 2
    1.63 -#define RAZOR_STRING_POOL 3
    1.64 -#define RAZOR_PROPERTY_POOL 4
    1.65 -
    1.66 -struct razor_package {
    1.67 -	unsigned long name;
    1.68 -	unsigned long version;
    1.69 -	unsigned long requires;
    1.70 -	unsigned long provides;
    1.71 -};
    1.72 -
    1.73 -struct razor_property {
    1.74 -	unsigned long name;
    1.75 -	unsigned long version;
    1.76 -	unsigned long packages;
    1.77 -};
    1.78 -
    1.79 -struct razor_set {
    1.80 -	struct array buckets;
    1.81 -	struct array string_pool;
    1.82 -	struct array property_pool;
    1.83 - 	struct array packages;
    1.84 - 	struct array requires;
    1.85 - 	struct array provides;
    1.86 -	struct razor_set_header *header;
    1.87 -};
    1.88 -
    1.89  struct razor_set_section razor_sections[] = {
    1.90  	{ RAZOR_PACKAGES,	offsetof(struct razor_set, packages) },
    1.91  	{ RAZOR_REQUIRES,	offsetof(struct razor_set, requires) },
    1.92 @@ -386,21 +320,7 @@
    1.93  	return razor_set_insert(set, string);
    1.94  }
    1.95  
    1.96 -struct import_property_context {
    1.97 -	struct array *all;
    1.98 -	struct array package;
    1.99 -};
   1.100 -
   1.101 -struct import_context {
   1.102 -	struct razor_set *set;
   1.103 -	struct import_property_context requires;
   1.104 -	struct import_property_context provides;
   1.105 -	struct razor_package *package;
   1.106 -	unsigned long *requires_map;
   1.107 -	unsigned long *provides_map;
   1.108 -};
   1.109 -
   1.110 -static void
   1.111 +void
   1.112  import_context_add_package(struct import_context *ctx,
   1.113  			   const char *name, const char *version)
   1.114  {
   1.115 @@ -428,7 +348,7 @@
   1.116  	array_release(&ctx->provides.package);
   1.117  }
   1.118  
   1.119 -static void
   1.120 +void
   1.121  import_context_add_property(struct import_context *ctx,
   1.122  			    struct import_property_context *pctx,
   1.123  			    const char *name, const char *version)
   1.124 @@ -446,91 +366,7 @@
   1.125  	*r = p - (struct razor_property *) pctx->all->data;
   1.126  }
   1.127  
   1.128 -static void
   1.129 -parse_package(struct import_context *ctx, const char **atts, void *data)
   1.130 -{
   1.131 -	const char *name = NULL, *version = NULL;
   1.132 -	int i;
   1.133 -
   1.134 -	for (i = 0; atts[i]; i += 2) {
   1.135 -		if (strcmp(atts[i], "name") == 0)
   1.136 -			name = atts[i + 1];
   1.137 -		else if (strcmp(atts[i], "version") == 0)
   1.138 -			version = atts[i + 1];
   1.139 -	}
   1.140 -
   1.141 -	if (name == NULL || version == NULL) {
   1.142 -		fprintf(stderr, "invalid package tag, "
   1.143 -			"missing name or version attributes\n");
   1.144 -		return;
   1.145 -	}
   1.146 -
   1.147 -	import_context_add_package(ctx, name, version);
   1.148 -}
   1.149 -
   1.150 -static void
   1.151 -parse_property(struct import_context *ctx, const char **atts, void *data)
   1.152 -{
   1.153 -	const char *name = NULL, *version = NULL;
   1.154 -	int i;
   1.155 -
   1.156 -	for (i = 0; atts[i]; i += 2) {
   1.157 -		if (strcmp(atts[i], "name") == 0)
   1.158 -			name = atts[i + 1];
   1.159 -		if (strcmp(atts[i], "version") == 0)
   1.160 -			version = atts[i + 1];
   1.161 -	}
   1.162 -	
   1.163 -	if (name == NULL) {
   1.164 -		fprintf(stderr, "invalid tag, missing name attribute\n");
   1.165 -		return;
   1.166 -	}
   1.167 -
   1.168 -	import_context_add_property(ctx, data, name, version);
   1.169 -}
   1.170 -
   1.171 -static void
   1.172 -start_element(void *data, const char *name, const char **atts)
   1.173 -{
   1.174 -	struct import_context *ctx = data;
   1.175 -
   1.176 -	if (strcmp(name, "package") == 0)
   1.177 -		parse_package(ctx, atts, NULL);
   1.178 -	else if (strcmp(name, "requires") == 0)
   1.179 -		parse_property(ctx, atts, &ctx->requires);
   1.180 -	else if (strcmp(name, "provides") == 0)
   1.181 -		parse_property(ctx, atts, &ctx->provides);
   1.182 -}
   1.183 -
   1.184 -static void
   1.185 -end_element (void *data, const char *name)
   1.186 -{
   1.187 -	struct import_context *ctx = data;
   1.188 -
   1.189 -	if (strcmp(name, "package") == 0)
   1.190 -		import_context_finish_package(ctx);
   1.191 -}
   1.192 -
   1.193 -static char *
   1.194 -sha1_to_hex(const unsigned char *sha1)
   1.195 -{
   1.196 -	static int bufno;
   1.197 -	static char hexbuffer[4][50];
   1.198 -	static const char hex[] = "0123456789abcdef";
   1.199 -	char *buffer = hexbuffer[3 & ++bufno], *buf = buffer;
   1.200 -	int i;
   1.201 -
   1.202 -	for (i = 0; i < 20; i++) {
   1.203 -		unsigned int val = *sha1++;
   1.204 -		*buf++ = hex[val >> 4];
   1.205 -		*buf++ = hex[val & 0xf];
   1.206 -	}
   1.207 -	*buf = '\0';
   1.208 -
   1.209 -	return buffer;
   1.210 -}
   1.211 -
   1.212 -static void
   1.213 +void
   1.214  razor_prepare_import(struct import_context *ctx)
   1.215  {
   1.216  	memset(ctx, 0, sizeof *ctx);
   1.217 @@ -539,52 +375,6 @@
   1.218  	ctx->provides.all = &ctx->set->provides;
   1.219  }
   1.220  
   1.221 -static int
   1.222 -razor_import(struct import_context *ctx, const char *filename)
   1.223 -{
   1.224 -	SHA_CTX sha1;
   1.225 -	XML_Parser parser;
   1.226 -	int fd;
   1.227 -	void *p;
   1.228 -	struct stat stat;
   1.229 -	char buf[128];
   1.230 -	unsigned char hash[20];
   1.231 -
   1.232 -	fd = open(filename, O_RDONLY);
   1.233 -	if (fstat(fd, &stat) < 0)
   1.234 -		return -1;
   1.235 -	p = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
   1.236 -	if (p == MAP_FAILED)
   1.237 -		return -1;
   1.238 -
   1.239 -	parser = XML_ParserCreate(NULL);
   1.240 -	XML_SetUserData(parser, ctx);
   1.241 -	XML_SetElementHandler(parser, start_element, end_element);
   1.242 -	if (XML_Parse(parser, p, stat.st_size, 1) == XML_STATUS_ERROR) {
   1.243 -		fprintf(stderr,
   1.244 -			"%s at line %d, %s\n",
   1.245 -			XML_ErrorString(XML_GetErrorCode(parser)),
   1.246 -			XML_GetCurrentLineNumber(parser),
   1.247 -			filename);
   1.248 -		return 1;
   1.249 -	}
   1.250 -
   1.251 -	XML_ParserFree(parser);
   1.252 -
   1.253 -	SHA1_Init(&sha1);
   1.254 -	SHA1_Update(&sha1, p, stat.st_size);
   1.255 -	SHA1_Final(hash, &sha1);
   1.256 -
   1.257 -	close(fd);
   1.258 -
   1.259 -	snprintf(buf, sizeof buf, "set/%s", sha1_to_hex(hash));
   1.260 -	if (write_to_file(buf, p, stat.st_size) < 0)
   1.261 -		return -1;
   1.262 -	munmap(p, stat.st_size);
   1.263 -
   1.264 -	return 0;
   1.265 -}
   1.266 -
   1.267  typedef int (*compare_with_data_func_t)(const void *p1,
   1.268  					const void *p,
   1.269  					void *data);
   1.270 @@ -800,7 +590,7 @@
   1.271  	free(rmap);
   1.272  }
   1.273  
   1.274 -static struct razor_set *
   1.275 +struct razor_set *
   1.276  razor_finish_import(struct import_context *ctx)
   1.277  {
   1.278  	unsigned long *map;
   1.279 @@ -824,127 +614,6 @@
   1.280  	return ctx->set; 
   1.281  }
   1.282  
   1.283 -/* Import a yum filelist as a razor package set. */
   1.284 -
   1.285 -enum {
   1.286 -	YUM_STATE_BEGIN,
   1.287 -	YUM_STATE_PACKAGE_NAME
   1.288 -};
   1.289 -
   1.290 -struct yum_context {
   1.291 -	struct import_context ctx;
   1.292 -	struct import_property_context *current_property_context;
   1.293 -	char *name;
   1.294 -	int state;
   1.295 -};
   1.296 -
   1.297 -static void
   1.298 -yum_start_element(void *data, const char *name, const char **atts)
   1.299 -{
   1.300 -	struct yum_context *ctx = data;
   1.301 -	const char *n, *version;
   1.302 -	int i;
   1.303 -
   1.304 -	if (strcmp(name, "name") == 0) {
   1.305 -		ctx->state = YUM_STATE_PACKAGE_NAME;
   1.306 -	} else if (strcmp(name, "version") == 0) {
   1.307 -		version = NULL;
   1.308 -		for (i = 0; atts[i]; i += 2) {
   1.309 -			if (strcmp(atts[i], "ver") == 0)
   1.310 -				version = atts[i + 1];
   1.311 -		}
   1.312 -		import_context_add_package(&ctx->ctx, ctx->name, version);
   1.313 -	} else if (strcmp(name, "rpm:requires") == 0) {
   1.314 -		ctx->current_property_context = &ctx->ctx.requires;
   1.315 -	} else if (strcmp(name, "rpm:provides") == 0) {
   1.316 -		ctx->current_property_context = &ctx->ctx.provides;
   1.317 -	} else if (strcmp(name, "rpm:entry") == 0 &&
   1.318 -		   ctx->current_property_context != NULL) {
   1.319 -		n = NULL;
   1.320 -		version = NULL;
   1.321 -		for (i = 0; atts[i]; i += 2) {
   1.322 -			if (strcmp(atts[i], "name") == 0)
   1.323 -				n = atts[i + 1];
   1.324 -			else if (strcmp(atts[i], "ver") == 0)
   1.325 -				version = atts[i + 1];
   1.326 -		}
   1.327 -
   1.328 -		if (n == NULL) {
   1.329 -			fprintf(stderr, "invalid rpm:entry, "
   1.330 -				"missing name or version attributes\n");
   1.331 -			return;
   1.332 -		}
   1.333 -
   1.334 -		import_context_add_property(&ctx->ctx,
   1.335 -					    ctx->current_property_context,
   1.336 -					    n, version);
   1.337 -	}
   1.338 -}
   1.339 -
   1.340 -static void
   1.341 -yum_end_element (void *data, const char *name)
   1.342 -{
   1.343 -	struct yum_context *ctx = data;
   1.344 -
   1.345 -	if (strcmp(name, "package") == 0) {
   1.346 -		free(ctx->name);
   1.347 -		import_context_finish_package(&ctx->ctx);
   1.348 -	} else if (strcmp(name, "name") == 0) {
   1.349 -		ctx->state = 0;
   1.350 -	} else if (strcmp(name, "rpm:requires") == 0) {
   1.351 -		ctx->current_property_context = NULL;
   1.352 -	} else if (strcmp(name, "rpm:provides") == 0) {
   1.353 -		ctx->current_property_context = NULL;
   1.354 -	}
   1.355 -}
   1.356 -
   1.357 -static void
   1.358 -yum_character_data (void *data, const XML_Char *s, int len)
   1.359 -{
   1.360 -	struct yum_context *ctx = data;
   1.361 -
   1.362 -	if (ctx->state == YUM_STATE_PACKAGE_NAME)
   1.363 -		ctx->name = strndup(s, len);
   1.364 -}
   1.365 -
   1.366 -static struct razor_set *
   1.367 -razor_set_create_from_yum_filelist(int fd)
   1.368 -{
   1.369 -	struct yum_context ctx;
   1.370 -	XML_Parser parser;
   1.371 -	char buf[4096];
   1.372 -	int len;
   1.373 -
   1.374 -	razor_prepare_import(&ctx.ctx);
   1.375 -
   1.376 -	parser = XML_ParserCreate(NULL);
   1.377 -	XML_SetUserData(parser, &ctx);
   1.378 -	XML_SetElementHandler(parser, yum_start_element, yum_end_element);
   1.379 -	XML_SetCharacterDataHandler(parser, yum_character_data);
   1.380 -
   1.381 -	while (1) {
   1.382 -		len = read(fd, buf, sizeof buf);
   1.383 -		if (len < 0) {
   1.384 -			fprintf(stderr,
   1.385 -				"couldn't read input: %s\n", strerror(errno));
   1.386 -			return NULL;
   1.387 -		} else if (len == 0)
   1.388 -			break;
   1.389 -
   1.390 -		if (XML_Parse(parser, buf, len, 0) == XML_STATUS_ERROR) {
   1.391 -			fprintf(stderr,
   1.392 -				"%s at line %d\n",
   1.393 -				XML_ErrorString(XML_GetErrorCode(parser)),
   1.394 -				XML_GetCurrentLineNumber(parser));
   1.395 -			return NULL;
   1.396 -		}
   1.397 -	}
   1.398 -
   1.399 -	XML_ParserFree(parser);
   1.400 -
   1.401 -	return razor_finish_import(&ctx.ctx);
   1.402 -}
   1.403 -
   1.404  void
   1.405  razor_set_list(struct razor_set *set)
   1.406  {
   1.407 @@ -1188,12 +857,10 @@
   1.408  static const char rawhide_repo_filename[] = "rawhide.repo";
   1.409  
   1.410  int
   1.411 -main(int argc, char *argv[])
   1.412 +main(int argc, const char *argv[])
   1.413  {
   1.414 -	int i;
   1.415  	struct razor_set *set;
   1.416  	struct stat statbuf;
   1.417 -	struct import_context ctx;
   1.418  	char *repo;
   1.419  
   1.420  	repo = getenv("RAZOR_REPO");
   1.421 @@ -1208,17 +875,7 @@
   1.422  			exit(-1);
   1.423  		}
   1.424  			
   1.425 -		razor_prepare_import(&ctx);
   1.426 -
   1.427 -		for (i = 2; i < argc; i++) {
   1.428 -			if (razor_import(&ctx, argv[i]) < 0) {
   1.429 -				fprintf(stderr, "failed to import %s\n",
   1.430 -					argv[i]);
   1.431 -				exit(-1);
   1.432 -			}
   1.433 -		}
   1.434 -
   1.435 -		set = razor_finish_import(&ctx);
   1.436 +		set = razor_import_rzr_files(argc - 2, argv + 2);
   1.437  
   1.438  		printf("bucket allocation: %d\n", set->buckets.alloc);
   1.439  		printf("pool size: %d\n", set->string_pool.size);