yum.c
author James Bowes <jbowes@redhat.com>
Sun Jun 08 17:11:41 2008 -0400 (2008-06-08)
changeset 224 5803b6151d02
parent 212 e8f493d8ff9a
child 225 c51f49f38d18
permissions -rw-r--r--
Import summary and description into the repo files.

Also add a 'info' command for displaying them.
     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 <sys/stat.h>
    25 #include <sys/mman.h>
    26 #include <unistd.h>
    27 #include <fcntl.h>
    28 #include <errno.h>
    29 
    30 #include <expat.h>
    31 #include <zlib.h>
    32 #include "razor.h"
    33 
    34 /* Import a yum filelist as a razor package set. */
    35 
    36 enum {
    37 	YUM_STATE_BEGIN,
    38 	YUM_STATE_PACKAGE_NAME,
    39 	YUM_STATE_PACKAGE_ARCH,
    40 	YUM_STATE_SUMMARY,
    41 	YUM_STATE_DESCRIPTION,
    42 	YUM_STATE_CHECKSUM,
    43 	YUM_STATE_REQUIRES,
    44 	YUM_STATE_PROVIDES,
    45 	YUM_STATE_OBSOLETES,
    46 	YUM_STATE_CONFLICTS,
    47 	YUM_STATE_FILE
    48 };
    49 
    50 struct yum_context {
    51 	XML_Parser primary_parser;
    52 	XML_Parser filelists_parser;
    53 	XML_Parser current_parser;
    54 
    55 	struct razor_importer *importer;
    56 	struct import_property_context *current_property_context;
    57 	char name[256], arch[64], summary[512], description[4096], buffer[512], *p;
    58 	char pkgid[128];
    59 	int state;
    60 };
    61 
    62 static enum razor_version_relation
    63 yum_to_razor_flags (const char *flags)
    64 {
    65 	/* FIXME? */
    66 	if (!flags)
    67 		return RAZOR_VERSION_EQUAL;
    68 
    69 	if (flags[0] == 'L') {
    70 		if (flags[1] == 'T')
    71 			return RAZOR_VERSION_LESS;
    72 		else
    73 			return RAZOR_VERSION_LESS_OR_EQUAL;
    74 	} else if (flags[0] == 'G') {
    75 		if (flags[1] == 'T')
    76 			return RAZOR_VERSION_GREATER;
    77 		else
    78 			return RAZOR_VERSION_GREATER_OR_EQUAL;
    79 	} else
    80 		return RAZOR_VERSION_EQUAL;
    81 }
    82 
    83 static void
    84 yum_primary_start_element(void *data, const char *name, const char **atts)
    85 {
    86 	struct yum_context *ctx = data;
    87 	const char *n, *epoch, *version, *release, *flags;
    88 	char buffer[128];
    89 	int i;
    90 
    91 	if (strcmp(name, "name") == 0) {
    92 		ctx->state = YUM_STATE_PACKAGE_NAME;
    93 		ctx->p = ctx->name;
    94 	} else if (strcmp(name, "arch") == 0) {
    95 		ctx->state = YUM_STATE_PACKAGE_ARCH;
    96 		ctx->p = ctx->arch;
    97 	} else if (strcmp(name, "version") == 0) {
    98 		epoch = NULL;
    99 		version = NULL;
   100 		release = NULL;
   101 		for (i = 0; atts[i]; i += 2) {
   102 			if (strcmp(atts[i], "epoch") == 0)
   103 				epoch = atts[i + 1];
   104 			else if (strcmp(atts[i], "ver") == 0)
   105 				version = atts[i + 1];
   106 			else if (strcmp(atts[i], "rel") == 0)
   107 				release = atts[i + 1];
   108 		}
   109 		if (version == NULL || release == NULL) {
   110 			fprintf(stderr, "invalid version tag, "
   111 				"missing version or  release attribute\n");
   112 			return;
   113 		}
   114 
   115 		razor_build_evr(buffer, sizeof buffer, epoch, version, release);
   116 		razor_importer_begin_package(ctx->importer,
   117 					     ctx->name, buffer, ctx->arch);
   118 	} else if (strcmp(name, "summary") == 0) {
   119 		ctx->p = ctx->summary;
   120 		ctx->state = YUM_STATE_SUMMARY;
   121 	} else if (strcmp(name, "description") == 0) {
   122 		ctx->p = ctx->description;
   123 		ctx->state = YUM_STATE_DESCRIPTION;
   124 	} else if (strcmp(name, "checksum") == 0) {
   125 		ctx->p = ctx->pkgid;
   126 		ctx->state = YUM_STATE_CHECKSUM;
   127 	} else if (strcmp(name, "rpm:requires") == 0) {
   128 		ctx->state = YUM_STATE_REQUIRES;
   129 	} else if (strcmp(name, "rpm:provides") == 0) {
   130 		ctx->state = YUM_STATE_PROVIDES;
   131 	} else if (strcmp(name, "rpm:obsoletes") == 0) {
   132 		ctx->state = YUM_STATE_OBSOLETES;
   133 	} else if (strcmp(name, "rpm:conflicts") == 0) {
   134 		ctx->state = YUM_STATE_CONFLICTS;
   135 	} else if (strcmp(name, "rpm:entry") == 0 &&
   136 		   ctx->state != YUM_STATE_BEGIN) {
   137 		n = NULL;
   138 		epoch = NULL;
   139 		version = NULL;
   140 		release = NULL;
   141 		flags = NULL;
   142 		for (i = 0; atts[i]; i += 2) {
   143 			if (strcmp(atts[i], "name") == 0)
   144 				n = atts[i + 1];
   145 			else if (strcmp(atts[i], "epoch") == 0)
   146 				epoch = atts[i + 1];
   147 			else if (strcmp(atts[i], "ver") == 0)
   148 				version = atts[i + 1];
   149 			else if (strcmp(atts[i], "rel") == 0)
   150 				release = atts[i + 1];
   151 			else if (strcmp(atts[i], "flags") == 0)
   152 				flags = atts[i + 1];
   153 		}
   154 
   155 		if (n == NULL) {
   156 			fprintf(stderr, "invalid rpm:entry, "
   157 				"missing name or version attributes\n");
   158 			return;
   159 		}
   160 
   161 		razor_build_evr(buffer, sizeof buffer, epoch, version, release);
   162 		switch (ctx->state) {
   163 		case YUM_STATE_REQUIRES:
   164 			razor_importer_add_property(ctx->importer, n,
   165 						    yum_to_razor_flags (flags),
   166 						    buffer,
   167 						    RAZOR_PROPERTY_REQUIRES);
   168 			break;
   169 		case YUM_STATE_PROVIDES:
   170 			razor_importer_add_property(ctx->importer, n,
   171 						    yum_to_razor_flags (flags),
   172 						    buffer,
   173 						    RAZOR_PROPERTY_PROVIDES);
   174 			break;
   175 		case YUM_STATE_OBSOLETES:
   176 			razor_importer_add_property(ctx->importer, n,
   177 						    yum_to_razor_flags (flags),
   178 						    buffer,
   179 						    RAZOR_PROPERTY_OBSOLETES);
   180 			break;
   181 		case YUM_STATE_CONFLICTS:
   182 			razor_importer_add_property(ctx->importer, n,
   183 						    yum_to_razor_flags (flags),
   184 						    buffer,
   185 						    RAZOR_PROPERTY_CONFLICTS);
   186 			break;
   187 		}
   188 	}
   189 }
   190 
   191 static void
   192 yum_primary_end_element (void *data, const char *name)
   193 {
   194 	struct yum_context *ctx = data;
   195 
   196 	switch (ctx->state) {
   197 	case YUM_STATE_PACKAGE_NAME:
   198 	case YUM_STATE_PACKAGE_ARCH:
   199 	case YUM_STATE_SUMMARY:
   200 	case YUM_STATE_DESCRIPTION:
   201 	case YUM_STATE_CHECKSUM:
   202 	case YUM_STATE_FILE:
   203 		ctx->state = YUM_STATE_BEGIN;
   204 		break;
   205 	}
   206 
   207 	if (strcmp(name, "package") == 0) {
   208 		razor_importer_add_details(ctx->importer, ctx->summary, ctx->description);
   209 
   210 		XML_StopParser(ctx->current_parser, XML_TRUE);
   211 		ctx->current_parser = ctx->filelists_parser;
   212 	}
   213 }
   214 
   215 static void
   216 yum_character_data (void *data, const XML_Char *s, int len)
   217 {
   218 	struct yum_context *ctx = data;
   219 
   220 	switch (ctx->state) {
   221 	case YUM_STATE_PACKAGE_NAME:
   222 	case YUM_STATE_PACKAGE_ARCH:
   223 	case YUM_STATE_SUMMARY:
   224 	case YUM_STATE_DESCRIPTION:
   225 	case YUM_STATE_CHECKSUM:
   226 	case YUM_STATE_FILE:
   227 		memcpy(ctx->p, s, len);
   228 		ctx->p += len;
   229 		*ctx->p = '\0';
   230 		break;
   231 	}
   232 }
   233 
   234 static void
   235 yum_filelists_start_element(void *data, const char *name, const char **atts)
   236 {
   237 	struct yum_context *ctx = data;
   238 	const char *pkg, *pkgid;
   239 	int i;
   240 
   241 	if (strcmp(name, "package") == 0) {
   242 		pkg = NULL;
   243 		pkgid = NULL;
   244 		for (i = 0; atts[i]; i += 2) {
   245 			if (strcmp(atts[i], "name") == 0)
   246 				pkg = atts[i + 1];
   247 			else if (strcmp(atts[i], "pkgid") == 0)
   248 				pkgid = atts[i + 1];
   249 		}
   250 		if (strcmp(pkgid, ctx->pkgid) != 0)
   251 			fprintf(stderr, "primary.xml and filelists.xml "
   252 				"mismatch for %s: %s vs %s",
   253 				pkg, pkgid, ctx->pkgid);
   254 	} else if (strcmp(name, "file") == 0) {
   255 		ctx->state = YUM_STATE_FILE;
   256 		ctx->p = ctx->buffer;
   257 	}
   258 }
   259 
   260 
   261 static void
   262 yum_filelists_end_element (void *data, const char *name)
   263 {
   264 	struct yum_context *ctx = data;
   265 
   266 	ctx->state = YUM_STATE_BEGIN;
   267 	if (strcmp(name, "package") == 0) {
   268 		XML_StopParser(ctx->current_parser, XML_TRUE);
   269 		ctx->current_parser = ctx->primary_parser;
   270 		razor_importer_finish_package(ctx->importer);
   271 	} else if (strcmp(name, "file") == 0)
   272 		razor_importer_add_file(ctx->importer, ctx->buffer);
   273 
   274 }
   275 
   276 #define XML_BUFFER_SIZE 4096
   277 
   278 struct razor_set *
   279 razor_set_create_from_yum(void)
   280 {
   281 	struct yum_context ctx;
   282 	void *buf;
   283 	int len, ret;
   284 	gzFile primary, filelists;
   285 	XML_ParsingStatus status;
   286 
   287 	ctx.importer = razor_importer_new();	
   288 	ctx.state = YUM_STATE_BEGIN;
   289 
   290 	ctx.primary_parser = XML_ParserCreate(NULL);
   291 	XML_SetUserData(ctx.primary_parser, &ctx);
   292 	XML_SetElementHandler(ctx.primary_parser,
   293 			      yum_primary_start_element,
   294 			      yum_primary_end_element);
   295 	XML_SetCharacterDataHandler(ctx.primary_parser,
   296 				    yum_character_data);
   297 
   298 	ctx.filelists_parser = XML_ParserCreate(NULL);
   299 	XML_SetUserData(ctx.filelists_parser, &ctx);
   300 	XML_SetElementHandler(ctx.filelists_parser,
   301 			      yum_filelists_start_element,
   302 			      yum_filelists_end_element);
   303 	XML_SetCharacterDataHandler(ctx.filelists_parser,
   304 				    yum_character_data);
   305 
   306 	primary = gzopen("primary.xml.gz", "rb");
   307 	if (primary == NULL)
   308 		return NULL;
   309 	filelists = gzopen("filelists.xml.gz", "rb");
   310 	if (filelists == NULL)
   311 		return NULL;
   312 
   313 	ctx.current_parser = ctx.primary_parser;
   314 
   315 	do {
   316 		XML_GetParsingStatus(ctx.current_parser, &status);
   317 		switch (status.parsing) {
   318 		case XML_SUSPENDED:
   319 			ret = XML_ResumeParser(ctx.current_parser);
   320 			break;
   321 		case XML_PARSING:
   322 		case XML_INITIALIZED:
   323 			buf = XML_GetBuffer(ctx.current_parser,
   324 					    XML_BUFFER_SIZE);
   325 			if (ctx.current_parser == ctx.primary_parser)
   326 				len = gzread(primary, buf, XML_BUFFER_SIZE);
   327 			else
   328 				len = gzread(filelists, buf, XML_BUFFER_SIZE);
   329 			if (len < 0) {
   330 				fprintf(stderr,
   331 					"couldn't read input: %s\n",
   332 					strerror(errno));
   333 				return NULL;
   334 			}
   335 
   336 			XML_ParseBuffer(ctx.current_parser, len, len == 0);
   337 			break;
   338 		case XML_FINISHED:
   339 			break;
   340 		}
   341 	} while (status.parsing != XML_FINISHED);
   342 
   343 
   344 	XML_ParserFree(ctx.primary_parser);
   345 	XML_ParserFree(ctx.filelists_parser);
   346 
   347 	gzclose(primary);
   348 	gzclose(filelists);
   349 
   350 	return razor_importer_finish(ctx.importer);
   351 }