src/import-yum.c
author James Bowes <jbowes@redhat.com>
Fri Jun 20 19:04:47 2008 -0400 (2008-06-20)
changeset 258 29d5002bd17f
parent 247 63444a10fb8e
child 259 5b0601d184ed
permissions -rw-r--r--
Merge branch 'krh/master'

Conflicts:

librazor/razor.h
librazor/rpm.c
razor.c
src/main.c
     1 /*
     2  * Copyright (C) 2008  Kristian Høgsberg <krh@redhat.com>
     3  * Copyright (C) 2008  Red Hat, Inc
     4  *
     5  * This program is free software; you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation; either version 2 of the License, or
     8  * (at your option) any later version.
     9  *
    10  * This program is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  * GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License along
    16  * with this program; if not, write to the Free Software Foundation, Inc.,
    17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    18  */
    19 
    20 #define _GNU_SOURCE
    21 
    22 #include <string.h>
    23 #include <stdio.h>
    24 #include <stdint.h>
    25 #include <sys/stat.h>
    26 #include <sys/mman.h>
    27 #include <unistd.h>
    28 #include <fcntl.h>
    29 #include <errno.h>
    30 
    31 #include <expat.h>
    32 #include <zlib.h>
    33 #include "razor.h"
    34 
    35 /* Import a yum filelist as a razor package set. */
    36 
    37 enum {
    38 	YUM_STATE_BEGIN,
    39 	YUM_STATE_PACKAGE_NAME,
    40 	YUM_STATE_PACKAGE_ARCH,
    41 	YUM_STATE_SUMMARY,
    42 	YUM_STATE_DESCRIPTION,
    43 	YUM_STATE_URL,
    44 	YUM_STATE_LICENSE,
    45 	YUM_STATE_CHECKSUM,
    46 	YUM_STATE_REQUIRES,
    47 	YUM_STATE_PROVIDES,
    48 	YUM_STATE_OBSOLETES,
    49 	YUM_STATE_CONFLICTS,
    50 	YUM_STATE_FILE
    51 };
    52 
    53 struct yum_context {
    54 	XML_Parser primary_parser;
    55 	XML_Parser filelists_parser;
    56 	XML_Parser current_parser;
    57 
    58 	struct razor_importer *importer;
    59 	struct import_property_context *current_property_context;
    60 	char name[256], arch[64], summary[512], description[4096];
    61 	char url[256], license[64], buffer[512], *p;
    62 	char pkgid[128];
    63 	uint32_t property_type;
    64 	int state;
    65 };
    66 
    67 static uint32_t
    68 yum_to_razor_relation (const char *flags)
    69 {
    70 	if (flags[0] == 'L') {
    71 		if (flags[1] == 'T')
    72 			return RAZOR_PROPERTY_LESS;
    73 		else
    74 			return RAZOR_PROPERTY_LESS | RAZOR_PROPERTY_EQUAL;
    75 	} else if (flags[0] == 'G') {
    76 		if (flags[1] == 'T')
    77 			return RAZOR_PROPERTY_GREATER;
    78 		else
    79 			return RAZOR_PROPERTY_GREATER | RAZOR_PROPERTY_EQUAL;
    80 	} else
    81 		return RAZOR_PROPERTY_EQUAL;
    82 }
    83 
    84 static void
    85 yum_primary_start_element(void *data, const char *name, const char **atts)
    86 {
    87 	struct yum_context *ctx = data;
    88 	const char *n, *epoch, *version, *release;
    89 	char buffer[128];
    90 	uint32_t pre, relation, flags;
    91 	int i;
    92 
    93 	if (strcmp(name, "name") == 0) {
    94 		ctx->state = YUM_STATE_PACKAGE_NAME;
    95 		ctx->p = ctx->name;
    96 	} else if (strcmp(name, "arch") == 0) {
    97 		ctx->state = YUM_STATE_PACKAGE_ARCH;
    98 		ctx->p = ctx->arch;
    99 	} else if (strcmp(name, "version") == 0) {
   100 		epoch = NULL;
   101 		version = NULL;
   102 		release = NULL;
   103 		for (i = 0; atts[i]; i += 2) {
   104 			if (strcmp(atts[i], "epoch") == 0)
   105 				epoch = atts[i + 1];
   106 			else if (strcmp(atts[i], "ver") == 0)
   107 				version = atts[i + 1];
   108 			else if (strcmp(atts[i], "rel") == 0)
   109 				release = atts[i + 1];
   110 		}
   111 		if (version == NULL || release == NULL) {
   112 			fprintf(stderr, "invalid version tag, "
   113 				"missing version or  release attribute\n");
   114 			return;
   115 		}
   116 
   117 		razor_build_evr(buffer, sizeof buffer, epoch, version, release);
   118 		razor_importer_begin_package(ctx->importer,
   119 					     ctx->name, buffer, ctx->arch);
   120 	} else if (strcmp(name, "summary") == 0) {
   121 		ctx->p = ctx->summary;
   122 		ctx->state = YUM_STATE_SUMMARY;
   123 	} else if (strcmp(name, "description") == 0) {
   124 		ctx->p = ctx->description;
   125 		ctx->state = YUM_STATE_DESCRIPTION;
   126 	} else if (strcmp(name, "url") == 0) {
   127 		ctx->p = ctx->url;
   128 		ctx->state = YUM_STATE_URL;
   129 	} else if (strcmp(name, "checksum") == 0) {
   130 		ctx->p = ctx->pkgid;
   131 		ctx->state = YUM_STATE_CHECKSUM;
   132 	} else if (strcmp(name, "rpm:license") == 0) {
   133 		ctx->p = ctx->license;
   134 		ctx->state = YUM_STATE_LICENSE;
   135 	} else if (strcmp(name, "rpm:requires") == 0) {
   136 		ctx->state = YUM_STATE_REQUIRES;
   137 		ctx->property_type = RAZOR_PROPERTY_REQUIRES;
   138 	} else if (strcmp(name, "rpm:provides") == 0) {
   139 		ctx->state = YUM_STATE_PROVIDES;
   140 		ctx->property_type = RAZOR_PROPERTY_PROVIDES;
   141 	} else if (strcmp(name, "rpm:obsoletes") == 0) {
   142 		ctx->state = YUM_STATE_OBSOLETES;
   143 		ctx->property_type = RAZOR_PROPERTY_OBSOLETES;
   144 	} else if (strcmp(name, "rpm:conflicts") == 0) {
   145 		ctx->state = YUM_STATE_CONFLICTS;
   146 		ctx->property_type = RAZOR_PROPERTY_CONFLICTS;
   147 	} else if (strcmp(name, "rpm:entry") == 0 &&
   148 		   ctx->state != YUM_STATE_BEGIN) {
   149 		n = NULL;
   150 		epoch = NULL;
   151 		version = NULL;
   152 		release = NULL;
   153 		relation = RAZOR_PROPERTY_EQUAL;
   154 		pre = 0;
   155 		for (i = 0; atts[i]; i += 2) {
   156 			if (strcmp(atts[i], "name") == 0)
   157 				n = atts[i + 1];
   158 			else if (strcmp(atts[i], "epoch") == 0)
   159 				epoch = atts[i + 1];
   160 			else if (strcmp(atts[i], "ver") == 0)
   161 				version = atts[i + 1];
   162 			else if (strcmp(atts[i], "rel") == 0)
   163 				release = atts[i + 1];
   164 			else if (strcmp(atts[i], "flags") == 0)
   165 				relation = yum_to_razor_relation(atts[i + 1]);
   166 			else if (strcmp(atts[i], "pre") == 0)
   167 				pre = 
   168 					RAZOR_PROPERTY_PRE |
   169 					RAZOR_PROPERTY_POST |
   170 					RAZOR_PROPERTY_PREUN |
   171 					RAZOR_PROPERTY_POSTUN;
   172 		}
   173 
   174 		if (n == NULL) {
   175 			fprintf(stderr, "invalid rpm:entry, "
   176 				"missing name or version attributes\n");
   177 			return;
   178 		}
   179 
   180 		razor_build_evr(buffer, sizeof buffer, epoch, version, release);
   181 		flags = ctx->property_type | relation | pre;
   182 		razor_importer_add_property(ctx->importer, n, flags, buffer);
   183 	}
   184 }
   185 
   186 static void
   187 yum_primary_end_element (void *data, const char *name)
   188 {
   189 	struct yum_context *ctx = data;
   190 
   191 	switch (ctx->state) {
   192 	case YUM_STATE_PACKAGE_NAME:
   193 	case YUM_STATE_PACKAGE_ARCH:
   194 	case YUM_STATE_SUMMARY:
   195 	case YUM_STATE_DESCRIPTION:
   196 	case YUM_STATE_URL:
   197 	case YUM_STATE_LICENSE:
   198 	case YUM_STATE_CHECKSUM:
   199 	case YUM_STATE_FILE:
   200 		ctx->state = YUM_STATE_BEGIN;
   201 		break;
   202 	}
   203 
   204 	if (strcmp(name, "package") == 0) {
   205 		razor_importer_add_details(ctx->importer, ctx->summary,
   206 					   ctx->description, ctx->url,
   207 					   ctx->license);
   208 
   209 		XML_StopParser(ctx->current_parser, XML_TRUE);
   210 		ctx->current_parser = ctx->filelists_parser;
   211 	}
   212 }
   213 
   214 static void
   215 yum_character_data (void *data, const XML_Char *s, int len)
   216 {
   217 	struct yum_context *ctx = data;
   218 
   219 	switch (ctx->state) {
   220 	case YUM_STATE_PACKAGE_NAME:
   221 	case YUM_STATE_PACKAGE_ARCH:
   222 	case YUM_STATE_SUMMARY:
   223 	case YUM_STATE_DESCRIPTION:
   224 	case YUM_STATE_URL:
   225 	case YUM_STATE_LICENSE:
   226 	case YUM_STATE_CHECKSUM:
   227 	case YUM_STATE_FILE:
   228 		memcpy(ctx->p, s, len);
   229 		ctx->p += len;
   230 		*ctx->p = '\0';
   231 		break;
   232 	}
   233 }
   234 
   235 static void
   236 yum_filelists_start_element(void *data, const char *name, const char **atts)
   237 {
   238 	struct yum_context *ctx = data;
   239 	const char *pkg, *pkgid;
   240 	int i;
   241 
   242 	if (strcmp(name, "package") == 0) {
   243 		pkg = NULL;
   244 		pkgid = NULL;
   245 		for (i = 0; atts[i]; i += 2) {
   246 			if (strcmp(atts[i], "name") == 0)
   247 				pkg = atts[i + 1];
   248 			else if (strcmp(atts[i], "pkgid") == 0)
   249 				pkgid = atts[i + 1];
   250 		}
   251 		if (strcmp(pkgid, ctx->pkgid) != 0)
   252 			fprintf(stderr, "primary.xml and filelists.xml "
   253 				"mismatch for %s: %s vs %s",
   254 				pkg, pkgid, ctx->pkgid);
   255 	} else if (strcmp(name, "file") == 0) {
   256 		ctx->state = YUM_STATE_FILE;
   257 		ctx->p = ctx->buffer;
   258 	}
   259 }
   260 
   261 
   262 static void
   263 yum_filelists_end_element (void *data, const char *name)
   264 {
   265 	struct yum_context *ctx = data;
   266 
   267 	ctx->state = YUM_STATE_BEGIN;
   268 	if (strcmp(name, "package") == 0) {
   269 		XML_StopParser(ctx->current_parser, XML_TRUE);
   270 		ctx->current_parser = ctx->primary_parser;
   271 		razor_importer_finish_package(ctx->importer);
   272 	} else if (strcmp(name, "file") == 0)
   273 		razor_importer_add_file(ctx->importer, ctx->buffer);
   274 
   275 }
   276 
   277 #define XML_BUFFER_SIZE 4096
   278 
   279 struct razor_set *
   280 razor_set_create_from_yum(void)
   281 {
   282 	struct yum_context ctx;
   283 	void *buf;
   284 	int len, ret;
   285 	gzFile primary, filelists;
   286 	XML_ParsingStatus status;
   287 
   288 	ctx.importer = razor_importer_new();
   289 	ctx.state = YUM_STATE_BEGIN;
   290 
   291 	ctx.primary_parser = XML_ParserCreate(NULL);
   292 	XML_SetUserData(ctx.primary_parser, &ctx);
   293 	XML_SetElementHandler(ctx.primary_parser,
   294 			      yum_primary_start_element,
   295 			      yum_primary_end_element);
   296 	XML_SetCharacterDataHandler(ctx.primary_parser,
   297 				    yum_character_data);
   298 
   299 	ctx.filelists_parser = XML_ParserCreate(NULL);
   300 	XML_SetUserData(ctx.filelists_parser, &ctx);
   301 	XML_SetElementHandler(ctx.filelists_parser,
   302 			      yum_filelists_start_element,
   303 			      yum_filelists_end_element);
   304 	XML_SetCharacterDataHandler(ctx.filelists_parser,
   305 				    yum_character_data);
   306 
   307 	primary = gzopen("primary.xml.gz", "rb");
   308 	if (primary == NULL)
   309 		return NULL;
   310 	filelists = gzopen("filelists.xml.gz", "rb");
   311 	if (filelists == NULL)
   312 		return NULL;
   313 
   314 	ctx.current_parser = ctx.primary_parser;
   315 
   316 	do {
   317 		XML_GetParsingStatus(ctx.current_parser, &status);
   318 		switch (status.parsing) {
   319 		case XML_SUSPENDED:
   320 			ret = XML_ResumeParser(ctx.current_parser);
   321 			break;
   322 		case XML_PARSING:
   323 		case XML_INITIALIZED:
   324 			buf = XML_GetBuffer(ctx.current_parser,
   325 					    XML_BUFFER_SIZE);
   326 			if (ctx.current_parser == ctx.primary_parser)
   327 				len = gzread(primary, buf, XML_BUFFER_SIZE);
   328 			else
   329 				len = gzread(filelists, buf, XML_BUFFER_SIZE);
   330 			if (len < 0) {
   331 				fprintf(stderr,
   332 					"couldn't read input: %s\n",
   333 					strerror(errno));
   334 				return NULL;
   335 			}
   336 
   337 			XML_ParseBuffer(ctx.current_parser, len, len == 0);
   338 			break;
   339 		case XML_FINISHED:
   340 			break;
   341 		}
   342 	} while (status.parsing != XML_FINISHED);
   343 
   344 
   345 	XML_ParserFree(ctx.primary_parser);
   346 	XML_ParserFree(ctx.filelists_parser);
   347 
   348 	gzclose(primary);
   349 	gzclose(filelists);
   350 
   351 	return razor_importer_finish(ctx.importer);
   352 }