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