src/import-yum.c
author Kristian H?gsberg <krh@redhat.com>
Thu Jun 19 00:32:24 2008 -0400 (2008-06-19)
changeset 245 0311b3fe72e3
child 247 63444a10fb8e
permissions -rw-r--r--
Make razor install work again after the razor_root changes.
rhughes@241
     1
/*
rhughes@241
     2
 * Copyright (C) 2008  Kristian Høgsberg <krh@redhat.com>
rhughes@241
     3
 * Copyright (C) 2008  Red Hat, Inc
rhughes@241
     4
 *
rhughes@241
     5
 * This program is free software; you can redistribute it and/or modify
rhughes@241
     6
 * it under the terms of the GNU General Public License as published by
rhughes@241
     7
 * the Free Software Foundation; either version 2 of the License, or
rhughes@241
     8
 * (at your option) any later version.
rhughes@241
     9
 *
rhughes@241
    10
 * This program is distributed in the hope that it will be useful,
rhughes@241
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
rhughes@241
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
rhughes@241
    13
 * GNU General Public License for more details.
rhughes@241
    14
 *
rhughes@241
    15
 * You should have received a copy of the GNU General Public License along
rhughes@241
    16
 * with this program; if not, write to the Free Software Foundation, Inc.,
rhughes@241
    17
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
rhughes@241
    18
 */
rhughes@241
    19
rhughes@241
    20
#define _GNU_SOURCE
rhughes@241
    21
rhughes@241
    22
#include <string.h>
rhughes@241
    23
#include <stdio.h>
rhughes@241
    24
#include <sys/stat.h>
rhughes@241
    25
#include <sys/mman.h>
rhughes@241
    26
#include <unistd.h>
rhughes@241
    27
#include <fcntl.h>
rhughes@241
    28
#include <errno.h>
rhughes@241
    29
rhughes@241
    30
#include <expat.h>
rhughes@241
    31
#include <zlib.h>
rhughes@241
    32
#include "razor.h"
rhughes@241
    33
rhughes@241
    34
/* Import a yum filelist as a razor package set. */
rhughes@241
    35
rhughes@241
    36
enum {
rhughes@241
    37
	YUM_STATE_BEGIN,
rhughes@241
    38
	YUM_STATE_PACKAGE_NAME,
rhughes@241
    39
	YUM_STATE_PACKAGE_ARCH,
rhughes@241
    40
	YUM_STATE_CHECKSUM,
rhughes@241
    41
	YUM_STATE_REQUIRES,
rhughes@241
    42
	YUM_STATE_PROVIDES,
rhughes@241
    43
	YUM_STATE_OBSOLETES,
rhughes@241
    44
	YUM_STATE_CONFLICTS,
rhughes@241
    45
	YUM_STATE_FILE
rhughes@241
    46
};
rhughes@241
    47
rhughes@241
    48
struct yum_context {
rhughes@241
    49
	XML_Parser primary_parser;
rhughes@241
    50
	XML_Parser filelists_parser;
rhughes@241
    51
	XML_Parser current_parser;
rhughes@241
    52
rhughes@241
    53
	struct razor_importer *importer;
rhughes@241
    54
	struct import_property_context *current_property_context;
rhughes@241
    55
	char name[256], arch[64], buffer[512], *p;
rhughes@241
    56
	char pkgid[128];
rhughes@241
    57
	int state;
rhughes@241
    58
};
rhughes@241
    59
rhughes@241
    60
static enum razor_version_relation
rhughes@241
    61
yum_to_razor_flags (const char *flags)
rhughes@241
    62
{
rhughes@241
    63
	/* FIXME? */
rhughes@241
    64
	if (!flags)
rhughes@241
    65
		return RAZOR_VERSION_EQUAL;
rhughes@241
    66
rhughes@241
    67
	if (flags[0] == 'L') {
rhughes@241
    68
		if (flags[1] == 'T')
rhughes@241
    69
			return RAZOR_VERSION_LESS;
rhughes@241
    70
		else
rhughes@241
    71
			return RAZOR_VERSION_LESS_OR_EQUAL;
rhughes@241
    72
	} else if (flags[0] == 'G') {
rhughes@241
    73
		if (flags[1] == 'T')
rhughes@241
    74
			return RAZOR_VERSION_GREATER;
rhughes@241
    75
		else
rhughes@241
    76
			return RAZOR_VERSION_GREATER_OR_EQUAL;
rhughes@241
    77
	} else
rhughes@241
    78
		return RAZOR_VERSION_EQUAL;
rhughes@241
    79
}
rhughes@241
    80
rhughes@241
    81
static void
rhughes@241
    82
yum_primary_start_element(void *data, const char *name, const char **atts)
rhughes@241
    83
{
rhughes@241
    84
	struct yum_context *ctx = data;
rhughes@241
    85
	const char *n, *epoch, *version, *release, *flags;
rhughes@241
    86
	char buffer[128];
rhughes@241
    87
	int i;
rhughes@241
    88
rhughes@241
    89
	if (strcmp(name, "name") == 0) {
rhughes@241
    90
		ctx->state = YUM_STATE_PACKAGE_NAME;
rhughes@241
    91
		ctx->p = ctx->name;
rhughes@241
    92
	} else if (strcmp(name, "arch") == 0) {
rhughes@241
    93
		ctx->state = YUM_STATE_PACKAGE_ARCH;
rhughes@241
    94
		ctx->p = ctx->arch;
rhughes@241
    95
	} else if (strcmp(name, "version") == 0) {
rhughes@241
    96
		epoch = NULL;
rhughes@241
    97
		version = NULL;
rhughes@241
    98
		release = NULL;
rhughes@241
    99
		for (i = 0; atts[i]; i += 2) {
rhughes@241
   100
			if (strcmp(atts[i], "epoch") == 0)
rhughes@241
   101
				epoch = atts[i + 1];
rhughes@241
   102
			else if (strcmp(atts[i], "ver") == 0)
rhughes@241
   103
				version = atts[i + 1];
rhughes@241
   104
			else if (strcmp(atts[i], "rel") == 0)
rhughes@241
   105
				release = atts[i + 1];
rhughes@241
   106
		}
rhughes@241
   107
		if (version == NULL || release == NULL) {
rhughes@241
   108
			fprintf(stderr, "invalid version tag, "
rhughes@241
   109
				"missing version or  release attribute\n");
rhughes@241
   110
			return;
rhughes@241
   111
		}
rhughes@241
   112
rhughes@241
   113
		razor_build_evr(buffer, sizeof buffer, epoch, version, release);
rhughes@241
   114
		razor_importer_begin_package(ctx->importer,
rhughes@241
   115
					     ctx->name, buffer, ctx->arch);
rhughes@241
   116
	} else if (strcmp(name, "checksum") == 0) {
rhughes@241
   117
		ctx->p = ctx->pkgid;
rhughes@241
   118
		ctx->state = YUM_STATE_CHECKSUM;
rhughes@241
   119
	} else if (strcmp(name, "rpm:requires") == 0) {
rhughes@241
   120
		ctx->state = YUM_STATE_REQUIRES;
rhughes@241
   121
	} else if (strcmp(name, "rpm:provides") == 0) {
rhughes@241
   122
		ctx->state = YUM_STATE_PROVIDES;
rhughes@241
   123
	} else if (strcmp(name, "rpm:obsoletes") == 0) {
rhughes@241
   124
		ctx->state = YUM_STATE_OBSOLETES;
rhughes@241
   125
	} else if (strcmp(name, "rpm:conflicts") == 0) {
rhughes@241
   126
		ctx->state = YUM_STATE_CONFLICTS;
rhughes@241
   127
	} else if (strcmp(name, "rpm:entry") == 0 &&
rhughes@241
   128
		   ctx->state != YUM_STATE_BEGIN) {
rhughes@241
   129
		n = NULL;
rhughes@241
   130
		epoch = NULL;
rhughes@241
   131
		version = NULL;
rhughes@241
   132
		release = NULL;
rhughes@241
   133
		flags = NULL;
rhughes@241
   134
		for (i = 0; atts[i]; i += 2) {
rhughes@241
   135
			if (strcmp(atts[i], "name") == 0)
rhughes@241
   136
				n = atts[i + 1];
rhughes@241
   137
			else if (strcmp(atts[i], "epoch") == 0)
rhughes@241
   138
				epoch = atts[i + 1];
rhughes@241
   139
			else if (strcmp(atts[i], "ver") == 0)
rhughes@241
   140
				version = atts[i + 1];
rhughes@241
   141
			else if (strcmp(atts[i], "rel") == 0)
rhughes@241
   142
				release = atts[i + 1];
rhughes@241
   143
			else if (strcmp(atts[i], "flags") == 0)
rhughes@241
   144
				flags = atts[i + 1];
rhughes@241
   145
		}
rhughes@241
   146
rhughes@241
   147
		if (n == NULL) {
rhughes@241
   148
			fprintf(stderr, "invalid rpm:entry, "
rhughes@241
   149
				"missing name or version attributes\n");
rhughes@241
   150
			return;
rhughes@241
   151
		}
rhughes@241
   152
rhughes@241
   153
		razor_build_evr(buffer, sizeof buffer, epoch, version, release);
rhughes@241
   154
		switch (ctx->state) {
rhughes@241
   155
		case YUM_STATE_REQUIRES:
rhughes@241
   156
			razor_importer_add_property(ctx->importer, n,
rhughes@241
   157
						    yum_to_razor_flags (flags),
rhughes@241
   158
						    buffer,
rhughes@241
   159
						    RAZOR_PROPERTY_REQUIRES);
rhughes@241
   160
			break;
rhughes@241
   161
		case YUM_STATE_PROVIDES:
rhughes@241
   162
			razor_importer_add_property(ctx->importer, n,
rhughes@241
   163
						    yum_to_razor_flags (flags),
rhughes@241
   164
						    buffer,
rhughes@241
   165
						    RAZOR_PROPERTY_PROVIDES);
rhughes@241
   166
			break;
rhughes@241
   167
		case YUM_STATE_OBSOLETES:
rhughes@241
   168
			razor_importer_add_property(ctx->importer, n,
rhughes@241
   169
						    yum_to_razor_flags (flags),
rhughes@241
   170
						    buffer,
rhughes@241
   171
						    RAZOR_PROPERTY_OBSOLETES);
rhughes@241
   172
			break;
rhughes@241
   173
		case YUM_STATE_CONFLICTS:
rhughes@241
   174
			razor_importer_add_property(ctx->importer, n,
rhughes@241
   175
						    yum_to_razor_flags (flags),
rhughes@241
   176
						    buffer,
rhughes@241
   177
						    RAZOR_PROPERTY_CONFLICTS);
rhughes@241
   178
			break;
rhughes@241
   179
		}
rhughes@241
   180
	}
rhughes@241
   181
}
rhughes@241
   182
rhughes@241
   183
static void
rhughes@241
   184
yum_primary_end_element (void *data, const char *name)
rhughes@241
   185
{
rhughes@241
   186
	struct yum_context *ctx = data;
rhughes@241
   187
rhughes@241
   188
	switch (ctx->state) {
rhughes@241
   189
	case YUM_STATE_PACKAGE_NAME:
rhughes@241
   190
	case YUM_STATE_PACKAGE_ARCH:
rhughes@241
   191
	case YUM_STATE_CHECKSUM:
rhughes@241
   192
	case YUM_STATE_FILE:
rhughes@241
   193
		ctx->state = YUM_STATE_BEGIN;
rhughes@241
   194
		break;
rhughes@241
   195
	}
rhughes@241
   196
rhughes@241
   197
	if (strcmp(name, "package") == 0) {
rhughes@241
   198
		XML_StopParser(ctx->current_parser, XML_TRUE);
rhughes@241
   199
		ctx->current_parser = ctx->filelists_parser;
rhughes@241
   200
	}
rhughes@241
   201
}
rhughes@241
   202
rhughes@241
   203
static void
rhughes@241
   204
yum_character_data (void *data, const XML_Char *s, int len)
rhughes@241
   205
{
rhughes@241
   206
	struct yum_context *ctx = data;
rhughes@241
   207
rhughes@241
   208
	switch (ctx->state) {
rhughes@241
   209
	case YUM_STATE_PACKAGE_NAME:
rhughes@241
   210
	case YUM_STATE_PACKAGE_ARCH:
rhughes@241
   211
	case YUM_STATE_CHECKSUM:
rhughes@241
   212
	case YUM_STATE_FILE:
rhughes@241
   213
		memcpy(ctx->p, s, len);
rhughes@241
   214
		ctx->p += len;
rhughes@241
   215
		*ctx->p = '\0';
rhughes@241
   216
		break;
rhughes@241
   217
	}
rhughes@241
   218
}
rhughes@241
   219
rhughes@241
   220
static void
rhughes@241
   221
yum_filelists_start_element(void *data, const char *name, const char **atts)
rhughes@241
   222
{
rhughes@241
   223
	struct yum_context *ctx = data;
rhughes@241
   224
	const char *pkg, *pkgid;
rhughes@241
   225
	int i;
rhughes@241
   226
rhughes@241
   227
	if (strcmp(name, "package") == 0) {
rhughes@241
   228
		pkg = NULL;
rhughes@241
   229
		pkgid = NULL;
rhughes@241
   230
		for (i = 0; atts[i]; i += 2) {
rhughes@241
   231
			if (strcmp(atts[i], "name") == 0)
rhughes@241
   232
				pkg = atts[i + 1];
rhughes@241
   233
			else if (strcmp(atts[i], "pkgid") == 0)
rhughes@241
   234
				pkgid = atts[i + 1];
rhughes@241
   235
		}
rhughes@241
   236
		if (strcmp(pkgid, ctx->pkgid) != 0)
rhughes@241
   237
			fprintf(stderr, "primary.xml and filelists.xml "
rhughes@241
   238
				"mismatch for %s: %s vs %s",
rhughes@241
   239
				pkg, pkgid, ctx->pkgid);
rhughes@241
   240
	} else if (strcmp(name, "file") == 0) {
rhughes@241
   241
		ctx->state = YUM_STATE_FILE;
rhughes@241
   242
		ctx->p = ctx->buffer;
rhughes@241
   243
	}
rhughes@241
   244
}
rhughes@241
   245
rhughes@241
   246
rhughes@241
   247
static void
rhughes@241
   248
yum_filelists_end_element (void *data, const char *name)
rhughes@241
   249
{
rhughes@241
   250
	struct yum_context *ctx = data;
rhughes@241
   251
rhughes@241
   252
	ctx->state = YUM_STATE_BEGIN;
rhughes@241
   253
	if (strcmp(name, "package") == 0) {
rhughes@241
   254
		XML_StopParser(ctx->current_parser, XML_TRUE);
rhughes@241
   255
		ctx->current_parser = ctx->primary_parser;
rhughes@241
   256
		razor_importer_finish_package(ctx->importer);
rhughes@241
   257
	} else if (strcmp(name, "file") == 0)
rhughes@241
   258
		razor_importer_add_file(ctx->importer, ctx->buffer);
rhughes@241
   259
rhughes@241
   260
}
rhughes@241
   261
rhughes@241
   262
#define XML_BUFFER_SIZE 4096
rhughes@241
   263
rhughes@241
   264
struct razor_set *
rhughes@241
   265
razor_set_create_from_yum(void)
rhughes@241
   266
{
rhughes@241
   267
	struct yum_context ctx;
rhughes@241
   268
	void *buf;
rhughes@241
   269
	int len, ret;
rhughes@241
   270
	gzFile primary, filelists;
rhughes@241
   271
	XML_ParsingStatus status;
rhughes@241
   272
rhughes@241
   273
	ctx.importer = razor_importer_new();
rhughes@241
   274
	ctx.state = YUM_STATE_BEGIN;
rhughes@241
   275
rhughes@241
   276
	ctx.primary_parser = XML_ParserCreate(NULL);
rhughes@241
   277
	XML_SetUserData(ctx.primary_parser, &ctx);
rhughes@241
   278
	XML_SetElementHandler(ctx.primary_parser,
rhughes@241
   279
			      yum_primary_start_element,
rhughes@241
   280
			      yum_primary_end_element);
rhughes@241
   281
	XML_SetCharacterDataHandler(ctx.primary_parser,
rhughes@241
   282
				    yum_character_data);
rhughes@241
   283
rhughes@241
   284
	ctx.filelists_parser = XML_ParserCreate(NULL);
rhughes@241
   285
	XML_SetUserData(ctx.filelists_parser, &ctx);
rhughes@241
   286
	XML_SetElementHandler(ctx.filelists_parser,
rhughes@241
   287
			      yum_filelists_start_element,
rhughes@241
   288
			      yum_filelists_end_element);
rhughes@241
   289
	XML_SetCharacterDataHandler(ctx.filelists_parser,
rhughes@241
   290
				    yum_character_data);
rhughes@241
   291
rhughes@241
   292
	primary = gzopen("primary.xml.gz", "rb");
rhughes@241
   293
	if (primary == NULL)
rhughes@241
   294
		return NULL;
rhughes@241
   295
	filelists = gzopen("filelists.xml.gz", "rb");
rhughes@241
   296
	if (filelists == NULL)
rhughes@241
   297
		return NULL;
rhughes@241
   298
rhughes@241
   299
	ctx.current_parser = ctx.primary_parser;
rhughes@241
   300
rhughes@241
   301
	do {
rhughes@241
   302
		XML_GetParsingStatus(ctx.current_parser, &status);
rhughes@241
   303
		switch (status.parsing) {
rhughes@241
   304
		case XML_SUSPENDED:
rhughes@241
   305
			ret = XML_ResumeParser(ctx.current_parser);
rhughes@241
   306
			break;
rhughes@241
   307
		case XML_PARSING:
rhughes@241
   308
		case XML_INITIALIZED:
rhughes@241
   309
			buf = XML_GetBuffer(ctx.current_parser,
rhughes@241
   310
					    XML_BUFFER_SIZE);
rhughes@241
   311
			if (ctx.current_parser == ctx.primary_parser)
rhughes@241
   312
				len = gzread(primary, buf, XML_BUFFER_SIZE);
rhughes@241
   313
			else
rhughes@241
   314
				len = gzread(filelists, buf, XML_BUFFER_SIZE);
rhughes@241
   315
			if (len < 0) {
rhughes@241
   316
				fprintf(stderr,
rhughes@241
   317
					"couldn't read input: %s\n",
rhughes@241
   318
					strerror(errno));
rhughes@241
   319
				return NULL;
rhughes@241
   320
			}
rhughes@241
   321
rhughes@241
   322
			XML_ParseBuffer(ctx.current_parser, len, len == 0);
rhughes@241
   323
			break;
rhughes@241
   324
		case XML_FINISHED:
rhughes@241
   325
			break;
rhughes@241
   326
		}
rhughes@241
   327
	} while (status.parsing != XML_FINISHED);
rhughes@241
   328
rhughes@241
   329
rhughes@241
   330
	XML_ParserFree(ctx.primary_parser);
rhughes@241
   331
	XML_ParserFree(ctx.filelists_parser);
rhughes@241
   332
rhughes@241
   333
	gzclose(primary);
rhughes@241
   334
	gzclose(filelists);
rhughes@241
   335
rhughes@241
   336
	return razor_importer_finish(ctx.importer);
rhughes@241
   337
}