yum.c
author James Bowes <jbowes@redhat.com>
Sun Jun 08 17:11:41 2008 -0400 (2008-06-08)
changeset 224 5803b6151d02
parent 212 e8f493d8ff9a
child 225 c51f49f38d18
permissions -rw-r--r--
Import summary and description into the repo files.

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