1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/import-yum.c Mon Jun 16 17:54:29 2008 -0400
1.3 @@ -0,0 +1,337 @@
1.4 +/*
1.5 + * Copyright (C) 2008 Kristian Høgsberg <krh@redhat.com>
1.6 + * Copyright (C) 2008 Red Hat, Inc
1.7 + *
1.8 + * This program is free software; you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation; either version 2 of the License, or
1.11 + * (at your option) any later version.
1.12 + *
1.13 + * This program is distributed in the hope that it will be useful,
1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.16 + * GNU General Public License for more details.
1.17 + *
1.18 + * You should have received a copy of the GNU General Public License along
1.19 + * with this program; if not, write to the Free Software Foundation, Inc.,
1.20 + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1.21 + */
1.22 +
1.23 +#define _GNU_SOURCE
1.24 +
1.25 +#include <string.h>
1.26 +#include <stdio.h>
1.27 +#include <sys/stat.h>
1.28 +#include <sys/mman.h>
1.29 +#include <unistd.h>
1.30 +#include <fcntl.h>
1.31 +#include <errno.h>
1.32 +
1.33 +#include <expat.h>
1.34 +#include <zlib.h>
1.35 +#include "razor.h"
1.36 +
1.37 +/* Import a yum filelist as a razor package set. */
1.38 +
1.39 +enum {
1.40 + YUM_STATE_BEGIN,
1.41 + YUM_STATE_PACKAGE_NAME,
1.42 + YUM_STATE_PACKAGE_ARCH,
1.43 + YUM_STATE_CHECKSUM,
1.44 + YUM_STATE_REQUIRES,
1.45 + YUM_STATE_PROVIDES,
1.46 + YUM_STATE_OBSOLETES,
1.47 + YUM_STATE_CONFLICTS,
1.48 + YUM_STATE_FILE
1.49 +};
1.50 +
1.51 +struct yum_context {
1.52 + XML_Parser primary_parser;
1.53 + XML_Parser filelists_parser;
1.54 + XML_Parser current_parser;
1.55 +
1.56 + struct razor_importer *importer;
1.57 + struct import_property_context *current_property_context;
1.58 + char name[256], arch[64], buffer[512], *p;
1.59 + char pkgid[128];
1.60 + int state;
1.61 +};
1.62 +
1.63 +static enum razor_version_relation
1.64 +yum_to_razor_flags (const char *flags)
1.65 +{
1.66 + /* FIXME? */
1.67 + if (!flags)
1.68 + return RAZOR_VERSION_EQUAL;
1.69 +
1.70 + if (flags[0] == 'L') {
1.71 + if (flags[1] == 'T')
1.72 + return RAZOR_VERSION_LESS;
1.73 + else
1.74 + return RAZOR_VERSION_LESS_OR_EQUAL;
1.75 + } else if (flags[0] == 'G') {
1.76 + if (flags[1] == 'T')
1.77 + return RAZOR_VERSION_GREATER;
1.78 + else
1.79 + return RAZOR_VERSION_GREATER_OR_EQUAL;
1.80 + } else
1.81 + return RAZOR_VERSION_EQUAL;
1.82 +}
1.83 +
1.84 +static void
1.85 +yum_primary_start_element(void *data, const char *name, const char **atts)
1.86 +{
1.87 + struct yum_context *ctx = data;
1.88 + const char *n, *epoch, *version, *release, *flags;
1.89 + char buffer[128];
1.90 + int i;
1.91 +
1.92 + if (strcmp(name, "name") == 0) {
1.93 + ctx->state = YUM_STATE_PACKAGE_NAME;
1.94 + ctx->p = ctx->name;
1.95 + } else if (strcmp(name, "arch") == 0) {
1.96 + ctx->state = YUM_STATE_PACKAGE_ARCH;
1.97 + ctx->p = ctx->arch;
1.98 + } else if (strcmp(name, "version") == 0) {
1.99 + epoch = NULL;
1.100 + version = NULL;
1.101 + release = NULL;
1.102 + for (i = 0; atts[i]; i += 2) {
1.103 + if (strcmp(atts[i], "epoch") == 0)
1.104 + epoch = atts[i + 1];
1.105 + else if (strcmp(atts[i], "ver") == 0)
1.106 + version = atts[i + 1];
1.107 + else if (strcmp(atts[i], "rel") == 0)
1.108 + release = atts[i + 1];
1.109 + }
1.110 + if (version == NULL || release == NULL) {
1.111 + fprintf(stderr, "invalid version tag, "
1.112 + "missing version or release attribute\n");
1.113 + return;
1.114 + }
1.115 +
1.116 + razor_build_evr(buffer, sizeof buffer, epoch, version, release);
1.117 + razor_importer_begin_package(ctx->importer,
1.118 + ctx->name, buffer, ctx->arch);
1.119 + } else if (strcmp(name, "checksum") == 0) {
1.120 + ctx->p = ctx->pkgid;
1.121 + ctx->state = YUM_STATE_CHECKSUM;
1.122 + } else if (strcmp(name, "rpm:requires") == 0) {
1.123 + ctx->state = YUM_STATE_REQUIRES;
1.124 + } else if (strcmp(name, "rpm:provides") == 0) {
1.125 + ctx->state = YUM_STATE_PROVIDES;
1.126 + } else if (strcmp(name, "rpm:obsoletes") == 0) {
1.127 + ctx->state = YUM_STATE_OBSOLETES;
1.128 + } else if (strcmp(name, "rpm:conflicts") == 0) {
1.129 + ctx->state = YUM_STATE_CONFLICTS;
1.130 + } else if (strcmp(name, "rpm:entry") == 0 &&
1.131 + ctx->state != YUM_STATE_BEGIN) {
1.132 + n = NULL;
1.133 + epoch = NULL;
1.134 + version = NULL;
1.135 + release = NULL;
1.136 + flags = NULL;
1.137 + for (i = 0; atts[i]; i += 2) {
1.138 + if (strcmp(atts[i], "name") == 0)
1.139 + n = atts[i + 1];
1.140 + else if (strcmp(atts[i], "epoch") == 0)
1.141 + epoch = atts[i + 1];
1.142 + else if (strcmp(atts[i], "ver") == 0)
1.143 + version = atts[i + 1];
1.144 + else if (strcmp(atts[i], "rel") == 0)
1.145 + release = atts[i + 1];
1.146 + else if (strcmp(atts[i], "flags") == 0)
1.147 + flags = atts[i + 1];
1.148 + }
1.149 +
1.150 + if (n == NULL) {
1.151 + fprintf(stderr, "invalid rpm:entry, "
1.152 + "missing name or version attributes\n");
1.153 + return;
1.154 + }
1.155 +
1.156 + razor_build_evr(buffer, sizeof buffer, epoch, version, release);
1.157 + switch (ctx->state) {
1.158 + case YUM_STATE_REQUIRES:
1.159 + razor_importer_add_property(ctx->importer, n,
1.160 + yum_to_razor_flags (flags),
1.161 + buffer,
1.162 + RAZOR_PROPERTY_REQUIRES);
1.163 + break;
1.164 + case YUM_STATE_PROVIDES:
1.165 + razor_importer_add_property(ctx->importer, n,
1.166 + yum_to_razor_flags (flags),
1.167 + buffer,
1.168 + RAZOR_PROPERTY_PROVIDES);
1.169 + break;
1.170 + case YUM_STATE_OBSOLETES:
1.171 + razor_importer_add_property(ctx->importer, n,
1.172 + yum_to_razor_flags (flags),
1.173 + buffer,
1.174 + RAZOR_PROPERTY_OBSOLETES);
1.175 + break;
1.176 + case YUM_STATE_CONFLICTS:
1.177 + razor_importer_add_property(ctx->importer, n,
1.178 + yum_to_razor_flags (flags),
1.179 + buffer,
1.180 + RAZOR_PROPERTY_CONFLICTS);
1.181 + break;
1.182 + }
1.183 + }
1.184 +}
1.185 +
1.186 +static void
1.187 +yum_primary_end_element (void *data, const char *name)
1.188 +{
1.189 + struct yum_context *ctx = data;
1.190 +
1.191 + switch (ctx->state) {
1.192 + case YUM_STATE_PACKAGE_NAME:
1.193 + case YUM_STATE_PACKAGE_ARCH:
1.194 + case YUM_STATE_CHECKSUM:
1.195 + case YUM_STATE_FILE:
1.196 + ctx->state = YUM_STATE_BEGIN;
1.197 + break;
1.198 + }
1.199 +
1.200 + if (strcmp(name, "package") == 0) {
1.201 + XML_StopParser(ctx->current_parser, XML_TRUE);
1.202 + ctx->current_parser = ctx->filelists_parser;
1.203 + }
1.204 +}
1.205 +
1.206 +static void
1.207 +yum_character_data (void *data, const XML_Char *s, int len)
1.208 +{
1.209 + struct yum_context *ctx = data;
1.210 +
1.211 + switch (ctx->state) {
1.212 + case YUM_STATE_PACKAGE_NAME:
1.213 + case YUM_STATE_PACKAGE_ARCH:
1.214 + case YUM_STATE_CHECKSUM:
1.215 + case YUM_STATE_FILE:
1.216 + memcpy(ctx->p, s, len);
1.217 + ctx->p += len;
1.218 + *ctx->p = '\0';
1.219 + break;
1.220 + }
1.221 +}
1.222 +
1.223 +static void
1.224 +yum_filelists_start_element(void *data, const char *name, const char **atts)
1.225 +{
1.226 + struct yum_context *ctx = data;
1.227 + const char *pkg, *pkgid;
1.228 + int i;
1.229 +
1.230 + if (strcmp(name, "package") == 0) {
1.231 + pkg = NULL;
1.232 + pkgid = NULL;
1.233 + for (i = 0; atts[i]; i += 2) {
1.234 + if (strcmp(atts[i], "name") == 0)
1.235 + pkg = atts[i + 1];
1.236 + else if (strcmp(atts[i], "pkgid") == 0)
1.237 + pkgid = atts[i + 1];
1.238 + }
1.239 + if (strcmp(pkgid, ctx->pkgid) != 0)
1.240 + fprintf(stderr, "primary.xml and filelists.xml "
1.241 + "mismatch for %s: %s vs %s",
1.242 + pkg, pkgid, ctx->pkgid);
1.243 + } else if (strcmp(name, "file") == 0) {
1.244 + ctx->state = YUM_STATE_FILE;
1.245 + ctx->p = ctx->buffer;
1.246 + }
1.247 +}
1.248 +
1.249 +
1.250 +static void
1.251 +yum_filelists_end_element (void *data, const char *name)
1.252 +{
1.253 + struct yum_context *ctx = data;
1.254 +
1.255 + ctx->state = YUM_STATE_BEGIN;
1.256 + if (strcmp(name, "package") == 0) {
1.257 + XML_StopParser(ctx->current_parser, XML_TRUE);
1.258 + ctx->current_parser = ctx->primary_parser;
1.259 + razor_importer_finish_package(ctx->importer);
1.260 + } else if (strcmp(name, "file") == 0)
1.261 + razor_importer_add_file(ctx->importer, ctx->buffer);
1.262 +
1.263 +}
1.264 +
1.265 +#define XML_BUFFER_SIZE 4096
1.266 +
1.267 +struct razor_set *
1.268 +razor_set_create_from_yum(void)
1.269 +{
1.270 + struct yum_context ctx;
1.271 + void *buf;
1.272 + int len, ret;
1.273 + gzFile primary, filelists;
1.274 + XML_ParsingStatus status;
1.275 +
1.276 + ctx.importer = razor_importer_new();
1.277 + ctx.state = YUM_STATE_BEGIN;
1.278 +
1.279 + ctx.primary_parser = XML_ParserCreate(NULL);
1.280 + XML_SetUserData(ctx.primary_parser, &ctx);
1.281 + XML_SetElementHandler(ctx.primary_parser,
1.282 + yum_primary_start_element,
1.283 + yum_primary_end_element);
1.284 + XML_SetCharacterDataHandler(ctx.primary_parser,
1.285 + yum_character_data);
1.286 +
1.287 + ctx.filelists_parser = XML_ParserCreate(NULL);
1.288 + XML_SetUserData(ctx.filelists_parser, &ctx);
1.289 + XML_SetElementHandler(ctx.filelists_parser,
1.290 + yum_filelists_start_element,
1.291 + yum_filelists_end_element);
1.292 + XML_SetCharacterDataHandler(ctx.filelists_parser,
1.293 + yum_character_data);
1.294 +
1.295 + primary = gzopen("primary.xml.gz", "rb");
1.296 + if (primary == NULL)
1.297 + return NULL;
1.298 + filelists = gzopen("filelists.xml.gz", "rb");
1.299 + if (filelists == NULL)
1.300 + return NULL;
1.301 +
1.302 + ctx.current_parser = ctx.primary_parser;
1.303 +
1.304 + do {
1.305 + XML_GetParsingStatus(ctx.current_parser, &status);
1.306 + switch (status.parsing) {
1.307 + case XML_SUSPENDED:
1.308 + ret = XML_ResumeParser(ctx.current_parser);
1.309 + break;
1.310 + case XML_PARSING:
1.311 + case XML_INITIALIZED:
1.312 + buf = XML_GetBuffer(ctx.current_parser,
1.313 + XML_BUFFER_SIZE);
1.314 + if (ctx.current_parser == ctx.primary_parser)
1.315 + len = gzread(primary, buf, XML_BUFFER_SIZE);
1.316 + else
1.317 + len = gzread(filelists, buf, XML_BUFFER_SIZE);
1.318 + if (len < 0) {
1.319 + fprintf(stderr,
1.320 + "couldn't read input: %s\n",
1.321 + strerror(errno));
1.322 + return NULL;
1.323 + }
1.324 +
1.325 + XML_ParseBuffer(ctx.current_parser, len, len == 0);
1.326 + break;
1.327 + case XML_FINISHED:
1.328 + break;
1.329 + }
1.330 + } while (status.parsing != XML_FINISHED);
1.331 +
1.332 +
1.333 + XML_ParserFree(ctx.primary_parser);
1.334 + XML_ParserFree(ctx.filelists_parser);
1.335 +
1.336 + gzclose(primary);
1.337 + gzclose(filelists);
1.338 +
1.339 + return razor_importer_finish(ctx.importer);
1.340 +}