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