summaryrefslogtreecommitdiff
path: root/src/import-yum.c
blob: fd78dc3dd3cf071baf07a8b3279611d29ca80cab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
 * Copyright (C) 2008  Kristian Høgsberg <krh@redhat.com>
 * Copyright (C) 2008  Red Hat, Inc
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#define _GNU_SOURCE

#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>

#include <expat.h>
#include <zlib.h>
#include "razor.h"

/* Import a yum filelist as a razor package set. */

enum {
	YUM_STATE_BEGIN,
	YUM_STATE_PACKAGE_NAME,
	YUM_STATE_PACKAGE_ARCH,
	YUM_STATE_SUMMARY,
	YUM_STATE_DESCRIPTION,
	YUM_STATE_URL,
	YUM_STATE_LICENSE,
	YUM_STATE_CHECKSUM,
	YUM_STATE_REQUIRES,
	YUM_STATE_PROVIDES,
	YUM_STATE_OBSOLETES,
	YUM_STATE_CONFLICTS,
	YUM_STATE_FILE
};

struct yum_context {
	XML_Parser primary_parser;
	XML_Parser filelists_parser;
	XML_Parser current_parser;

	struct razor_importer *importer;
	struct import_property_context *current_property_context;
	char name[256], arch[64], summary[512], description[4096];
	char url[256], license[64], buffer[512], *p;
	char pkgid[128];
	uint32_t property_type;
	int state;

	int total, current;
};

static uint32_t
yum_to_razor_relation (const char *flags)
{
	if (flags[0] == 'L') {
		if (flags[1] == 'T')
			return RAZOR_PROPERTY_LESS;
		else
			return RAZOR_PROPERTY_LESS | RAZOR_PROPERTY_EQUAL;
	} else if (flags[0] == 'G') {
		if (flags[1] == 'T')
			return RAZOR_PROPERTY_GREATER;
		else
			return RAZOR_PROPERTY_GREATER | RAZOR_PROPERTY_EQUAL;
	} else
		return RAZOR_PROPERTY_EQUAL;
}

static void
yum_primary_start_element(void *data, const char *name, const char **atts)
{
	struct yum_context *ctx = data;
	const char *n, *epoch, *version, *release;
	char buffer[128];
	uint32_t pre, relation, flags;
	int i;

	if (strcmp(name, "metadata") == 0) {
		for (i = 0; atts[i]; i += 2) {
			if (strcmp(atts[i], "packages") == 0)
				ctx->total = atoi(atts[i + 1]);
		}
	} else if (strcmp(name, "name") == 0) {
		ctx->state = YUM_STATE_PACKAGE_NAME;
		ctx->p = ctx->name;
	} else if (strcmp(name, "arch") == 0) {
		ctx->state = YUM_STATE_PACKAGE_ARCH;
		ctx->p = ctx->arch;
	} else if (strcmp(name, "version") == 0) {
		epoch = NULL;
		version = NULL;
		release = NULL;
		for (i = 0; atts[i]; i += 2) {
			if (strcmp(atts[i], "epoch") == 0)
				epoch = atts[i + 1];
			else if (strcmp(atts[i], "ver") == 0)
				version = atts[i + 1];
			else if (strcmp(atts[i], "rel") == 0)
				release = atts[i + 1];
		}
		if (version == NULL || release == NULL) {
			fprintf(stderr, "invalid version tag, "
				"missing version or  release attribute\n");
			return;
		}

		razor_build_evr(buffer, sizeof buffer, epoch, version, release);
		razor_importer_begin_package(ctx->importer,
					     ctx->name, buffer, ctx->arch);
	} else if (strcmp(name, "summary") == 0) {
		ctx->p = ctx->summary;
		ctx->state = YUM_STATE_SUMMARY;
	} else if (strcmp(name, "description") == 0) {
		ctx->p = ctx->description;
		ctx->state = YUM_STATE_DESCRIPTION;
	} else if (strcmp(name, "url") == 0) {
		ctx->p = ctx->url;
		ctx->state = YUM_STATE_URL;
	} else if (strcmp(name, "checksum") == 0) {
		ctx->p = ctx->pkgid;
		ctx->state = YUM_STATE_CHECKSUM;
	} else if (strcmp(name, "rpm:license") == 0) {
		ctx->p = ctx->license;
		ctx->state = YUM_STATE_LICENSE;
	} else if (strcmp(name, "rpm:requires") == 0) {
		ctx->state = YUM_STATE_REQUIRES;
		ctx->property_type = RAZOR_PROPERTY_REQUIRES;
	} else if (strcmp(name, "rpm:provides") == 0) {
		ctx->state = YUM_STATE_PROVIDES;
		ctx->property_type = RAZOR_PROPERTY_PROVIDES;
	} else if (strcmp(name, "rpm:obsoletes") == 0) {
		ctx->state = YUM_STATE_OBSOLETES;
		ctx->property_type = RAZOR_PROPERTY_OBSOLETES;
	} else if (strcmp(name, "rpm:conflicts") == 0) {
		ctx->state = YUM_STATE_CONFLICTS;
		ctx->property_type = RAZOR_PROPERTY_CONFLICTS;
	} else if (strcmp(name, "rpm:entry") == 0 &&
		   ctx->state != YUM_STATE_BEGIN) {
		n = NULL;
		epoch = NULL;
		version = NULL;
		release = NULL;
		relation = RAZOR_PROPERTY_EQUAL;
		pre = 0;
		for (i = 0; atts[i]; i += 2) {
			if (strcmp(atts[i], "name") == 0)
				n = atts[i + 1];
			else if (strcmp(atts[i], "epoch") == 0)
				epoch = atts[i + 1];
			else if (strcmp(atts[i], "ver") == 0)
				version = atts[i + 1];
			else if (strcmp(atts[i], "rel") == 0)
				release = atts[i + 1];
			else if (strcmp(atts[i], "flags") == 0)
				relation = yum_to_razor_relation(atts[i + 1]);
			else if (strcmp(atts[i], "pre") == 0)
				pre = 
					RAZOR_PROPERTY_PRE |
					RAZOR_PROPERTY_POST |
					RAZOR_PROPERTY_PREUN |
					RAZOR_PROPERTY_POSTUN;
		}

		if (n == NULL) {
			fprintf(stderr, "invalid rpm:entry, "
				"missing name or version attributes\n");
			return;
		}

		razor_build_evr(buffer, sizeof buffer, epoch, version, release);
		flags = ctx->property_type | relation | pre;
		razor_importer_add_property(ctx->importer, n, flags, buffer);
	}
}

static void
yum_primary_end_element (void *data, const char *name)
{
	struct yum_context *ctx = data;

	switch (ctx->state) {
	case YUM_STATE_PACKAGE_NAME:
	case YUM_STATE_PACKAGE_ARCH:
	case YUM_STATE_SUMMARY:
	case YUM_STATE_DESCRIPTION:
	case YUM_STATE_URL:
	case YUM_STATE_LICENSE:
	case YUM_STATE_CHECKSUM:
	case YUM_STATE_FILE:
		ctx->state = YUM_STATE_BEGIN;
		break;
	}

	if (strcmp(name, "package") == 0) {
		razor_importer_add_details(ctx->importer, ctx->summary,
					   ctx->description, ctx->url,
					   ctx->license);

		XML_StopParser(ctx->current_parser, XML_TRUE);
		ctx->current_parser = ctx->filelists_parser;

		printf("\rimporting %d/%d", ++ctx->current, ctx->total);
		fflush(stdout);
	}
}

static void
yum_character_data (void *data, const XML_Char *s, int len)
{
	struct yum_context *ctx = data;

	switch (ctx->state) {
	case YUM_STATE_PACKAGE_NAME:
	case YUM_STATE_PACKAGE_ARCH:
	case YUM_STATE_SUMMARY:
	case YUM_STATE_DESCRIPTION:
	case YUM_STATE_URL:
	case YUM_STATE_LICENSE:
	case YUM_STATE_CHECKSUM:
	case YUM_STATE_FILE:
		memcpy(ctx->p, s, len);
		ctx->p += len;
		*ctx->p = '\0';
		break;
	}
}

static void
yum_filelists_start_element(void *data, const char *name, const char **atts)
{
	struct yum_context *ctx = data;
	const char *pkg, *pkgid;
	int i;

	if (strcmp(name, "package") == 0) {
		pkg = NULL;
		pkgid = NULL;
		for (i = 0; atts[i]; i += 2) {
			if (strcmp(atts[i], "name") == 0)
				pkg = atts[i + 1];
			else if (strcmp(atts[i], "pkgid") == 0)
				pkgid = atts[i + 1];
		}
		if (strcmp(pkgid, ctx->pkgid) != 0)
			fprintf(stderr, "primary.xml and filelists.xml "
				"mismatch for %s: %s vs %s",
				pkg, pkgid, ctx->pkgid);
	} else if (strcmp(name, "file") == 0) {
		ctx->state = YUM_STATE_FILE;
		ctx->p = ctx->buffer;
	}
}


static void
yum_filelists_end_element (void *data, const char *name)
{
	struct yum_context *ctx = data;

	ctx->state = YUM_STATE_BEGIN;
	if (strcmp(name, "package") == 0) {
		XML_StopParser(ctx->current_parser, XML_TRUE);
		ctx->current_parser = ctx->primary_parser;
		razor_importer_finish_package(ctx->importer);
	} else if (strcmp(name, "file") == 0)
		razor_importer_add_file(ctx->importer, ctx->buffer);

}

#define XML_BUFFER_SIZE 4096

struct razor_set *
razor_set_create_from_yum(void)
{
	struct yum_context ctx;
	void *buf;
	int len, ret;
	gzFile primary, filelists;
	XML_ParsingStatus status;

	ctx.importer = razor_importer_create();
	ctx.state = YUM_STATE_BEGIN;

	ctx.primary_parser = XML_ParserCreate(NULL);
	XML_SetUserData(ctx.primary_parser, &ctx);
	XML_SetElementHandler(ctx.primary_parser,
			      yum_primary_start_element,
			      yum_primary_end_element);
	XML_SetCharacterDataHandler(ctx.primary_parser,
				    yum_character_data);

	ctx.filelists_parser = XML_ParserCreate(NULL);
	XML_SetUserData(ctx.filelists_parser, &ctx);
	XML_SetElementHandler(ctx.filelists_parser,
			      yum_filelists_start_element,
			      yum_filelists_end_element);
	XML_SetCharacterDataHandler(ctx.filelists_parser,
				    yum_character_data);

	primary = gzopen("primary.xml.gz", "rb");
	if (primary == NULL)
		return NULL;
	filelists = gzopen("filelists.xml.gz", "rb");
	if (filelists == NULL)
		return NULL;

	ctx.current_parser = ctx.primary_parser;

	ctx.current = 0;

	do {
		XML_GetParsingStatus(ctx.current_parser, &status);
		switch (status.parsing) {
		case XML_SUSPENDED:
			ret = XML_ResumeParser(ctx.current_parser);
			break;
		case XML_PARSING:
		case XML_INITIALIZED:
			buf = XML_GetBuffer(ctx.current_parser,
					    XML_BUFFER_SIZE);
			if (ctx.current_parser == ctx.primary_parser)
				len = gzread(primary, buf, XML_BUFFER_SIZE);
			else
				len = gzread(filelists, buf, XML_BUFFER_SIZE);
			if (len < 0) {
				fprintf(stderr,
					"couldn't read input: %s\n",
					strerror(errno));
				return NULL;
			}

			XML_ParseBuffer(ctx.current_parser, len, len == 0);
			break;
		case XML_FINISHED:
			break;
		}
	} while (status.parsing != XML_FINISHED);


	XML_ParserFree(ctx.primary_parser);
	XML_ParserFree(ctx.filelists_parser);

	gzclose(primary);
	gzclose(filelists);

	printf ("\nsaving\n");
	return razor_importer_finish(ctx.importer);
}