Add import feature for yum primary.xml files.
authorKristian H?gsberg <krh@redhat.com>
Fri Sep 07 13:48:42 2007 -0400 (2007-09-07)
changeset 15228bd47c5dc4
parent 14 d21d74d6e297
child 16 78383b7bc4fa
Add import feature for yum primary.xml files.
razor.c
     1.1 --- a/razor.c	Fri Sep 07 12:11:37 2007 -0400
     1.2 +++ b/razor.c	Fri Sep 07 13:48:42 2007 -0400
     1.3 @@ -1,3 +1,5 @@
     1.4 +#define _GNU_SOURCE
     1.5 +
     1.6  #include <stdlib.h>
     1.7  #include <stdio.h>
     1.8  #include <string.h>
     1.9 @@ -6,6 +8,7 @@
    1.10  #include <sys/mman.h>
    1.11  #include <unistd.h>
    1.12  #include <fcntl.h>
    1.13 +#include <errno.h>
    1.14  
    1.15  #include <expat.h>
    1.16  #include "sha1.h"
    1.17 @@ -731,6 +734,126 @@
    1.18  	return ctx->set; 
    1.19  }
    1.20  
    1.21 +/* Import a yum filelist as a razor package set. */
    1.22 +
    1.23 +enum {
    1.24 +	YUM_STATE_BEGIN,
    1.25 +	YUM_STATE_PACKAGE_NAME
    1.26 +};
    1.27 +
    1.28 +struct yum_context {
    1.29 +	struct import_context ctx;
    1.30 +	struct import_property_context *current_property_context;
    1.31 +	char *name;
    1.32 +	int state;
    1.33 +};
    1.34 +
    1.35 +static void
    1.36 +yum_start_element(void *data, const char *name, const char **atts)
    1.37 +{
    1.38 +	struct yum_context *ctx = data;
    1.39 +	const char *n, *version;
    1.40 +	int i;
    1.41 +
    1.42 +	if (strcmp(name, "name") == 0) {
    1.43 +		ctx->state = YUM_STATE_PACKAGE_NAME;
    1.44 +	} else if (strcmp(name, "version") == 0) {
    1.45 +		for (i = 0; atts[i]; i += 2) {
    1.46 +			if (strcmp(atts[i], "ver") == 0)
    1.47 +				version = atts[i + 1];
    1.48 +		}
    1.49 +		import_context_add_package(&ctx->ctx, ctx->name, version);
    1.50 +	} else if (strcmp(name, "rpm:requires") == 0) {
    1.51 +		ctx->current_property_context = &ctx->ctx.requires;
    1.52 +	} else if (strcmp(name, "rpm:provides") == 0) {
    1.53 +		ctx->current_property_context = &ctx->ctx.provides;
    1.54 +	} else if (strcmp(name, "rpm:entry") == 0 &&
    1.55 +		   ctx->current_property_context != NULL) {
    1.56 +		n = NULL;
    1.57 +		version = NULL;
    1.58 +		for (i = 0; atts[i]; i += 2) {
    1.59 +			if (strcmp(atts[i], "name") == 0)
    1.60 +				n = atts[i + 1];
    1.61 +			else if (strcmp(atts[i], "ver") == 0)
    1.62 +				version = atts[i + 1];
    1.63 +		}
    1.64 +
    1.65 +		if (n == NULL) {
    1.66 +			fprintf(stderr, "invalid rpm:entry, "
    1.67 +				"missing name or version attributes\n");
    1.68 +			return;
    1.69 +		}
    1.70 +
    1.71 +		import_context_add_property(&ctx->ctx,
    1.72 +					    ctx->current_property_context,
    1.73 +					    n, version);
    1.74 +	}
    1.75 +}
    1.76 +
    1.77 +static void
    1.78 +yum_end_element (void *data, const char *name)
    1.79 +{
    1.80 +	struct yum_context *ctx = data;
    1.81 +
    1.82 +	if (strcmp(name, "package") == 0) {
    1.83 +		free(ctx->name);
    1.84 +		import_context_finish_package(&ctx->ctx);
    1.85 +	} else if (strcmp(name, "name") == 0) {
    1.86 +		ctx->state = 0;
    1.87 +	} else if (strcmp(name, "rpm:requires") == 0) {
    1.88 +		ctx->current_property_context = NULL;
    1.89 +	} else if (strcmp(name, "rpm:provides") == 0) {
    1.90 +		ctx->current_property_context = NULL;
    1.91 +	}
    1.92 +}
    1.93 +
    1.94 +static void
    1.95 +yum_character_data (void *data, const XML_Char *s, int len)
    1.96 +{
    1.97 +	struct yum_context *ctx = data;
    1.98 +
    1.99 +	if (ctx->state == YUM_STATE_PACKAGE_NAME)
   1.100 +		ctx->name = strndup(s, len);
   1.101 +}
   1.102 +
   1.103 +static struct razor_set *
   1.104 +razor_set_create_from_yum_filelist(int fd)
   1.105 +{
   1.106 +	struct yum_context ctx;
   1.107 +	XML_Parser parser;
   1.108 +	char buf[4096];
   1.109 +	int len;
   1.110 +
   1.111 +	razor_prepare_import(&ctx.ctx);
   1.112 +
   1.113 +	parser = XML_ParserCreate(NULL);
   1.114 +	XML_SetUserData(parser, &ctx);
   1.115 +	XML_SetElementHandler(parser, yum_start_element, yum_end_element);
   1.116 +	XML_SetCharacterDataHandler(parser, yum_character_data);
   1.117 +
   1.118 +	while (1) {
   1.119 +		len = read(fd, buf, sizeof buf);
   1.120 +		if (len < 0) {
   1.121 +			fprintf(stderr,
   1.122 +				"couldn't read input: %s\n", strerror(errno));
   1.123 +			return NULL;
   1.124 +		} else if (len == 0)
   1.125 +			break;
   1.126 +
   1.127 +		if (XML_Parse(parser, buf, len, 0) == XML_STATUS_ERROR) {
   1.128 +			fprintf(stderr,
   1.129 +				"%s at line %d\n",
   1.130 +				XML_ErrorString(XML_GetErrorCode(parser)),
   1.131 +				XML_GetCurrentLineNumber(parser));
   1.132 +			return NULL;
   1.133 +		}
   1.134 +	}
   1.135 +
   1.136 +	XML_ParserFree(parser);
   1.137 +
   1.138 +	return razor_finish_import(&ctx.ctx);
   1.139 +}
   1.140 +
   1.141  void
   1.142  razor_set_list(struct razor_set *set)
   1.143  {
   1.144 @@ -848,11 +971,12 @@
   1.145  usage(void)
   1.146  {
   1.147  	printf("usage: razor [ import FILES | lookup <key> | "
   1.148 -	       "list | list-requires | list-provides | info ]\n");
   1.149 +	       "list | list-requires | list-provides | eat-yum | info ]\n");
   1.150  	exit(1);
   1.151  }
   1.152  
   1.153 -static const char repo_filename[] = "system.repo";
   1.154 +static const char *repo_filename = "system.repo";
   1.155 +static const char rawhide_repo_filename[] = "rawhide.repo";
   1.156  
   1.157  int
   1.158  main(int argc, char *argv[])
   1.159 @@ -861,6 +985,11 @@
   1.160  	struct razor_set *set;
   1.161  	struct stat statbuf;
   1.162  	struct import_context ctx;
   1.163 +	char *repo;
   1.164 +
   1.165 +	repo = getenv("RAZOR_REPO");
   1.166 +	if (repo != NULL)
   1.167 +		repo_filename = repo;
   1.168  
   1.169  	if (argc < 2) {
   1.170  		usage();
   1.171 @@ -916,6 +1045,12 @@
   1.172  		set = razor_set_open(repo_filename);
   1.173  		razor_set_info(set);
   1.174  		razor_set_destroy(set);
   1.175 +	} else if (strcmp(argv[1], "eat-yum") == 0) {
   1.176 +		set = razor_set_create_from_yum_filelist(STDIN_FILENO);
   1.177 +		if (set == NULL)
   1.178 +			return 1;
   1.179 +		razor_set_write(set, rawhide_repo_filename);
   1.180 +		razor_set_destroy(set);
   1.181  	} else {
   1.182  		usage();
   1.183  	}