src/import-yum.c
author J. Ali Harlow <ali@juiblex.co.uk>
Thu Jul 07 11:04:10 2016 +0100 (2016-07-07)
changeset 478 8e4bf84a7bb8
parent 455 df914f383f5c
permissions -rw-r--r--
Port KTM driver to URI-based API
     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 "config.h"
    23 #include <string.h>
    24 #include <stdio.h>
    25 #include <stdint.h>
    26 #include <assert.h>
    27 #include <sys/stat.h>
    28 #include <unistd.h>
    29 #include <fcntl.h>
    30 #include <errno.h>
    31 
    32 #include <expat.h>
    33 #include <zlib.h>
    34 #include "razor.h"
    35 #include "import.h"
    36 
    37 /* Import a yum filelist as a razor package set. */
    38 
    39 enum {
    40 	YUM_STATE_BEGIN,
    41 	YUM_STATE_PACKAGE_NAME,
    42 	YUM_STATE_PACKAGE_ARCH,
    43 	YUM_STATE_SUMMARY,
    44 	YUM_STATE_DESCRIPTION,
    45 	YUM_STATE_URL,
    46 	YUM_STATE_LICENSE,
    47 	YUM_STATE_CHECKSUM,
    48 	YUM_STATE_REQUIRES,
    49 	YUM_STATE_PROVIDES,
    50 	YUM_STATE_OBSOLETES,
    51 	YUM_STATE_CONFLICTS,
    52 	YUM_STATE_FILE
    53 };
    54 
    55 struct yum_context {
    56 	XML_Parser primary_parser;
    57 	XML_Parser filelists_parser;
    58 	XML_Parser current_parser;
    59 
    60 	struct razor_importer *importer;
    61 	struct import_property_context *current_property_context;
    62 	char name[256], arch[64], summary[512], description[4096];
    63 	char url[256], license[64], buffer[512], *p;
    64 	char pkgid[128];
    65 	uint32_t property_type;
    66 	int state;
    67 
    68 	int total, current;
    69 };
    70 
    71 static uint32_t
    72 yum_to_razor_relation (const char *flags)
    73 {
    74 	if (flags[0] == 'L') {
    75 		if (flags[1] == 'T')
    76 			return RAZOR_PROPERTY_LESS;
    77 		else
    78 			return RAZOR_PROPERTY_LESS | RAZOR_PROPERTY_EQUAL;
    79 	} else if (flags[0] == 'G') {
    80 		if (flags[1] == 'T')
    81 			return RAZOR_PROPERTY_GREATER;
    82 		else
    83 			return RAZOR_PROPERTY_GREATER | RAZOR_PROPERTY_EQUAL;
    84 	} else
    85 		return RAZOR_PROPERTY_EQUAL;
    86 }
    87 
    88 static void
    89 yum_primary_start_element(void *data, const char *name, const char **atts)
    90 {
    91 	struct yum_context *ctx = data;
    92 	const char *n, *epoch, *version, *release;
    93 	char buffer[128];
    94 	uint32_t pre, relation, flags;
    95 	int i;
    96 
    97 	if (strcmp(name, "metadata") == 0) {
    98 		for (i = 0; atts[i]; i += 2) {
    99 			if (strcmp(atts[i], "packages") == 0)
   100 				ctx->total = atoi(atts[i + 1]);
   101 		}
   102 	} else if (strcmp(name, "name") == 0) {
   103 		ctx->state = YUM_STATE_PACKAGE_NAME;
   104 		ctx->p = ctx->name;
   105 	} else if (strcmp(name, "arch") == 0) {
   106 		ctx->state = YUM_STATE_PACKAGE_ARCH;
   107 		ctx->p = ctx->arch;
   108 	} else if (strcmp(name, "version") == 0) {
   109 		epoch = NULL;
   110 		version = NULL;
   111 		release = NULL;
   112 		for (i = 0; atts[i]; i += 2) {
   113 			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 		}
   120 		if (version == NULL || release == NULL) {
   121 			fprintf(stderr, "invalid version tag, "
   122 				"missing version or  release attribute\n");
   123 			return;
   124 		}
   125 
   126 		razor_build_evr(buffer, sizeof buffer, epoch, version, release);
   127 		razor_importer_begin_package(ctx->importer,
   128 					     ctx->name, buffer, ctx->arch);
   129 	} else if (strcmp(name, "summary") == 0) {
   130 		ctx->p = ctx->summary;
   131 		ctx->state = YUM_STATE_SUMMARY;
   132 	} else if (strcmp(name, "description") == 0) {
   133 		ctx->p = ctx->description;
   134 		ctx->state = YUM_STATE_DESCRIPTION;
   135 	} else if (strcmp(name, "url") == 0) {
   136 		ctx->p = ctx->url;
   137 		ctx->state = YUM_STATE_URL;
   138 	} else if (strcmp(name, "checksum") == 0) {
   139 		ctx->p = ctx->pkgid;
   140 		ctx->state = YUM_STATE_CHECKSUM;
   141 	} else if (strcmp(name, "rpm:license") == 0) {
   142 		ctx->p = ctx->license;
   143 		ctx->state = YUM_STATE_LICENSE;
   144 	} else if (strcmp(name, "rpm:requires") == 0) {
   145 		ctx->state = YUM_STATE_REQUIRES;
   146 		ctx->property_type = RAZOR_PROPERTY_REQUIRES;
   147 	} else if (strcmp(name, "rpm:provides") == 0) {
   148 		ctx->state = YUM_STATE_PROVIDES;
   149 		ctx->property_type = RAZOR_PROPERTY_PROVIDES;
   150 	} else if (strcmp(name, "rpm:obsoletes") == 0) {
   151 		ctx->state = YUM_STATE_OBSOLETES;
   152 		ctx->property_type = RAZOR_PROPERTY_OBSOLETES;
   153 	} else if (strcmp(name, "rpm:conflicts") == 0) {
   154 		ctx->state = YUM_STATE_CONFLICTS;
   155 		ctx->property_type = RAZOR_PROPERTY_CONFLICTS;
   156 	} else if (strcmp(name, "rpm:entry") == 0 &&
   157 		   ctx->state != YUM_STATE_BEGIN) {
   158 		n = NULL;
   159 		epoch = NULL;
   160 		version = NULL;
   161 		release = NULL;
   162 		relation = RAZOR_PROPERTY_EQUAL;
   163 		pre = 0;
   164 		for (i = 0; atts[i]; i += 2) {
   165 			if (strcmp(atts[i], "name") == 0)
   166 				n = atts[i + 1];
   167 			else if (strcmp(atts[i], "epoch") == 0)
   168 				epoch = atts[i + 1];
   169 			else if (strcmp(atts[i], "ver") == 0)
   170 				version = atts[i + 1];
   171 			else if (strcmp(atts[i], "rel") == 0)
   172 				release = atts[i + 1];
   173 			else if (strcmp(atts[i], "flags") == 0)
   174 				relation = yum_to_razor_relation(atts[i + 1]);
   175 			else if (strcmp(atts[i], "pre") == 0)
   176 				pre = RAZOR_PROPERTY_PRE;
   177 		}
   178 
   179 		if (n == NULL) {
   180 			fprintf(stderr, "invalid rpm:entry, "
   181 				"missing name or version attributes\n");
   182 			return;
   183 		}
   184 
   185 		razor_build_evr(buffer, sizeof buffer, epoch, version, release);
   186 		flags = ctx->property_type | relation | pre;
   187 		razor_importer_add_property(ctx->importer, n, flags, buffer);
   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_URL:
   202 	case YUM_STATE_LICENSE:
   203 	case YUM_STATE_CHECKSUM:
   204 	case YUM_STATE_FILE:
   205 		ctx->state = YUM_STATE_BEGIN;
   206 		break;
   207 	}
   208 
   209 	if (strcmp(name, "package") == 0) {
   210 		razor_importer_add_details(ctx->importer, ctx->summary,
   211 					   ctx->description, ctx->url,
   212 					   ctx->license);
   213 
   214 		XML_StopParser(ctx->current_parser, XML_TRUE);
   215 		ctx->current_parser = ctx->filelists_parser;
   216 
   217 		printf("\rimporting %d/%d", ++ctx->current, ctx->total);
   218 		fflush(stdout);
   219 	}
   220 }
   221 
   222 static void
   223 yum_character_data (void *data, const XML_Char *s, int len)
   224 {
   225 	struct yum_context *ctx = data;
   226 
   227 	switch (ctx->state) {
   228 	case YUM_STATE_PACKAGE_NAME:
   229 	case YUM_STATE_PACKAGE_ARCH:
   230 	case YUM_STATE_SUMMARY:
   231 	case YUM_STATE_DESCRIPTION:
   232 	case YUM_STATE_URL:
   233 	case YUM_STATE_LICENSE:
   234 	case YUM_STATE_CHECKSUM:
   235 	case YUM_STATE_FILE:
   236 		memcpy(ctx->p, s, len);
   237 		ctx->p += len;
   238 		*ctx->p = '\0';
   239 		break;
   240 	}
   241 }
   242 
   243 static void
   244 yum_filelists_start_element(void *data, const char *name, const char **atts)
   245 {
   246 	struct yum_context *ctx = data;
   247 	const char *pkg, *pkgid;
   248 	int i;
   249 
   250 	if (strcmp(name, "package") == 0) {
   251 		pkg = NULL;
   252 		pkgid = NULL;
   253 		for (i = 0; atts[i]; i += 2) {
   254 			if (strcmp(atts[i], "name") == 0)
   255 				pkg = atts[i + 1];
   256 			else if (strcmp(atts[i], "pkgid") == 0)
   257 				pkgid = atts[i + 1];
   258 		}
   259 		if (strcmp(pkgid, ctx->pkgid) != 0)
   260 			fprintf(stderr, "primary.xml and filelists.xml "
   261 				"mismatch for %s: %s vs %s",
   262 				pkg, pkgid, ctx->pkgid);
   263 	} else if (strcmp(name, "file") == 0) {
   264 		ctx->state = YUM_STATE_FILE;
   265 		ctx->p = ctx->buffer;
   266 	}
   267 }
   268 
   269 
   270 static void
   271 yum_filelists_end_element (void *data, const char *name)
   272 {
   273 	struct yum_context *ctx = data;
   274 
   275 	ctx->state = YUM_STATE_BEGIN;
   276 	if (strcmp(name, "package") == 0) {
   277 		XML_StopParser(ctx->current_parser, XML_TRUE);
   278 		ctx->current_parser = ctx->primary_parser;
   279 		razor_importer_finish_package(ctx->importer);
   280 	} else if (strcmp(name, "file") == 0)
   281 		razor_importer_add_file(ctx->importer, ctx->buffer);
   282 
   283 }
   284 
   285 #define XML_BUFFER_SIZE 4096
   286 
   287 struct razor_stream {
   288 	z_stream strm;
   289 	void *in;
   290 	size_t in_length;
   291 };
   292 
   293 static int razor_stream_open(struct razor_stream *rs, const char *uri,
   294 			     struct razor_error **error)
   295 {
   296 	rs->strm.zalloc = Z_NULL;
   297 	rs->strm.zfree = Z_NULL;
   298 	rs->strm.opaque = Z_NULL;
   299 	rs->strm.avail_in = 0;
   300 	rs->strm.next_in = Z_NULL;
   301 
   302 	if (inflateInit2(&rs->strm, 15 + 16) != Z_OK) {
   303 		razor_set_error(error, RAZOR_GENERAL_ERROR,
   304 				RAZOR_GENERAL_ERROR_FAILED, uri,
   305 				"Failed to initialize inflator");
   306 		return -1;
   307 	}
   308 
   309 	rs->in = razor_uri_get_contents(uri, &rs->in_length, 0, error);
   310 	if (!rs->in) {
   311 		(void)inflateEnd(&rs->strm);
   312 		return -1;
   313 	}
   314 
   315 	rs->strm.avail_in = rs->in_length;
   316 	rs->strm.next_in = rs->in;
   317 
   318 	return 0;
   319 }
   320 
   321 static ssize_t
   322 razor_stream_read(struct razor_stream *rs, unsigned char *buf, size_t len)
   323 {
   324 	int r;
   325 
   326 	rs->strm.avail_out = len;
   327 	rs->strm.next_out = buf;
   328 
   329 	r = inflate(&rs->strm, Z_NO_FLUSH);
   330 	assert(r != Z_STREAM_ERROR);  /* state not clobbered */
   331 	switch (r) {
   332 	case Z_NEED_DICT:
   333 	case Z_DATA_ERROR:
   334 	case Z_MEM_ERROR:
   335 		return -1;
   336 	}
   337 
   338 	return len - rs->strm.avail_out;
   339 }
   340 
   341 static void razor_stream_close(struct razor_stream *rs)
   342 {
   343 	(void)inflateEnd(&rs->strm);
   344 	(void)razor_uri_free_contents(rs->in, rs->in_length);
   345 }
   346 
   347 struct razor_set *
   348 razor_set_create_from_yum(const char *yum_uri)
   349 {
   350 	struct yum_context ctx={0};
   351 	char *uri;
   352 	void *buf;
   353 	ssize_t len;
   354 	XML_ParsingStatus status;
   355 	struct razor_error *error = NULL;
   356 	struct razor_stream primary, filelists;
   357 
   358 	ctx.importer = razor_importer_create();
   359 	ctx.state = YUM_STATE_BEGIN;
   360 
   361 	ctx.primary_parser = XML_ParserCreate(NULL);
   362 	XML_SetUserData(ctx.primary_parser, &ctx);
   363 	XML_SetElementHandler(ctx.primary_parser,
   364 			      yum_primary_start_element,
   365 			      yum_primary_end_element);
   366 	XML_SetCharacterDataHandler(ctx.primary_parser,
   367 				    yum_character_data);
   368 
   369 	ctx.filelists_parser = XML_ParserCreate(NULL);
   370 	XML_SetUserData(ctx.filelists_parser, &ctx);
   371 	XML_SetElementHandler(ctx.filelists_parser,
   372 			      yum_filelists_start_element,
   373 			      yum_filelists_end_element);
   374 	XML_SetCharacterDataHandler(ctx.filelists_parser,
   375 				    yum_character_data);
   376 
   377 	uri = razor_path_relative_to_uri(yum_uri, "repodata/primary.xml.gz",
   378 					 &error);
   379 	if (!uri) {
   380 		fprintf(stderr, "%s: %s\n", yum_uri,
   381 			razor_error_get_msg(error));
   382 		razor_error_free(error);
   383 		return NULL;
   384 	}
   385 	if (razor_stream_open(&primary, uri, &error)) {
   386 		fprintf(stderr, "%s: %s\n", uri, razor_error_get_msg(error));
   387 		free(uri);
   388 		return NULL;
   389 	}
   390 	free(uri);
   391 
   392 	uri = razor_path_relative_to_uri(yum_uri, "repodata/filelists.xml.gz",
   393 					 &error);
   394 	if (!uri) {
   395 		razor_stream_close(&primary);
   396 		fprintf(stderr, "%s: %s\n", yum_uri,
   397 			razor_error_get_msg(error));
   398 		razor_error_free(error);
   399 		return NULL;
   400 	}
   401 	if (razor_stream_open(&filelists, uri, &error)) {
   402 		razor_stream_close(&primary);
   403 		fprintf(stderr, "%s: %s\n", uri, razor_error_get_msg(error));
   404 		free(uri);
   405 		return NULL;
   406 	}
   407 	free(uri);
   408 
   409 	ctx.current_parser = ctx.primary_parser;
   410 
   411 	ctx.current = 0;
   412 
   413 	do {
   414 		XML_GetParsingStatus(ctx.current_parser, &status);
   415 		switch (status.parsing) {
   416 		case XML_SUSPENDED:
   417 			XML_ResumeParser(ctx.current_parser);
   418 			break;
   419 		case XML_PARSING:
   420 		case XML_INITIALIZED:
   421 			buf = XML_GetBuffer(ctx.current_parser,
   422 					    XML_BUFFER_SIZE);
   423 			if (ctx.current_parser == ctx.primary_parser)
   424 				len = razor_stream_read(&primary, buf,
   425 							XML_BUFFER_SIZE);
   426 			else
   427 				len = razor_stream_read(&filelists, buf,
   428 							XML_BUFFER_SIZE);
   429 			if (len < 0) {
   430 				fprintf(stderr,
   431 					"couldn't read input: %s\n",
   432 					strerror(errno));
   433 				return NULL;
   434 			}
   435 
   436 			XML_ParseBuffer(ctx.current_parser, len, len == 0);
   437 			break;
   438 		case XML_FINISHED:
   439 			break;
   440 		}
   441 	} while (status.parsing != XML_FINISHED);
   442 
   443 
   444 	XML_ParserFree(ctx.primary_parser);
   445 	XML_ParserFree(ctx.filelists_parser);
   446 
   447 	razor_stream_close(&primary);
   448 	razor_stream_close(&filelists);
   449 
   450 	printf ("\nsaving\n");
   451 	return razor_importer_finish(ctx.importer);
   452 }