import.c
author Kristian H?gsberg <krh@redhat.com>
Tue Sep 18 15:02:04 2007 -0400 (2007-09-18)
changeset 27 5dbd81809d26
child 28 c8958f67afd8
permissions -rw-r--r--
Split importers to import.c.
krh@27
     1
#define _GNU_SOURCE
krh@27
     2
krh@27
     3
#include <string.h>
krh@27
     4
#include <stdio.h>
krh@27
     5
#include <sys/stat.h>
krh@27
     6
#include <sys/mman.h>
krh@27
     7
#include <unistd.h>
krh@27
     8
#include <fcntl.h>
krh@27
     9
#include <errno.h>
krh@27
    10
krh@27
    11
#include <expat.h>
krh@27
    12
#include "sha1.h"
krh@27
    13
#include "razor.h"
krh@27
    14
krh@27
    15
static void
krh@27
    16
parse_package(struct import_context *ctx, const char **atts, void *data)
krh@27
    17
{
krh@27
    18
	const char *name = NULL, *version = NULL;
krh@27
    19
	int i;
krh@27
    20
krh@27
    21
	for (i = 0; atts[i]; i += 2) {
krh@27
    22
		if (strcmp(atts[i], "name") == 0)
krh@27
    23
			name = atts[i + 1];
krh@27
    24
		else if (strcmp(atts[i], "version") == 0)
krh@27
    25
			version = atts[i + 1];
krh@27
    26
	}
krh@27
    27
krh@27
    28
	if (name == NULL || version == NULL) {
krh@27
    29
		fprintf(stderr, "invalid package tag, "
krh@27
    30
			"missing name or version attributes\n");
krh@27
    31
		return;
krh@27
    32
	}
krh@27
    33
krh@27
    34
	import_context_add_package(ctx, name, version);
krh@27
    35
}
krh@27
    36
krh@27
    37
static void
krh@27
    38
parse_property(struct import_context *ctx, const char **atts, void *data)
krh@27
    39
{
krh@27
    40
	const char *name = NULL, *version = NULL;
krh@27
    41
	int i;
krh@27
    42
krh@27
    43
	for (i = 0; atts[i]; i += 2) {
krh@27
    44
		if (strcmp(atts[i], "name") == 0)
krh@27
    45
			name = atts[i + 1];
krh@27
    46
		if (strcmp(atts[i], "version") == 0)
krh@27
    47
			version = atts[i + 1];
krh@27
    48
	}
krh@27
    49
	
krh@27
    50
	if (name == NULL) {
krh@27
    51
		fprintf(stderr, "invalid tag, missing name attribute\n");
krh@27
    52
		return;
krh@27
    53
	}
krh@27
    54
krh@27
    55
	import_context_add_property(ctx, data, name, version);
krh@27
    56
}
krh@27
    57
krh@27
    58
static void
krh@27
    59
start_element(void *data, const char *name, const char **atts)
krh@27
    60
{
krh@27
    61
	struct import_context *ctx = data;
krh@27
    62
krh@27
    63
	if (strcmp(name, "package") == 0)
krh@27
    64
		parse_package(ctx, atts, NULL);
krh@27
    65
	else if (strcmp(name, "requires") == 0)
krh@27
    66
		parse_property(ctx, atts, &ctx->requires);
krh@27
    67
	else if (strcmp(name, "provides") == 0)
krh@27
    68
		parse_property(ctx, atts, &ctx->provides);
krh@27
    69
}
krh@27
    70
krh@27
    71
static void
krh@27
    72
end_element (void *data, const char *name)
krh@27
    73
{
krh@27
    74
	struct import_context *ctx = data;
krh@27
    75
krh@27
    76
	if (strcmp(name, "package") == 0)
krh@27
    77
		import_context_finish_package(ctx);
krh@27
    78
}
krh@27
    79
krh@27
    80
static int
krh@27
    81
import_rzr_file(struct import_context *ctx, const char *filename)
krh@27
    82
{
krh@27
    83
	SHA_CTX sha1;
krh@27
    84
	XML_Parser parser;
krh@27
    85
	int fd;
krh@27
    86
	void *p;
krh@27
    87
	struct stat stat;
krh@27
    88
	unsigned char hash[20];
krh@27
    89
krh@27
    90
	fd = open(filename, O_RDONLY);
krh@27
    91
	if (fstat(fd, &stat) < 0)
krh@27
    92
		return -1;
krh@27
    93
	p = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
krh@27
    94
	if (p == MAP_FAILED)
krh@27
    95
		return -1;
krh@27
    96
krh@27
    97
	parser = XML_ParserCreate(NULL);
krh@27
    98
	XML_SetUserData(parser, ctx);
krh@27
    99
	XML_SetElementHandler(parser, start_element, end_element);
krh@27
   100
	if (XML_Parse(parser, p, stat.st_size, 1) == XML_STATUS_ERROR) {
krh@27
   101
		fprintf(stderr,
krh@27
   102
			"%s at line %d, %s\n",
krh@27
   103
			XML_ErrorString(XML_GetErrorCode(parser)),
krh@27
   104
			XML_GetCurrentLineNumber(parser),
krh@27
   105
			filename);
krh@27
   106
		return 1;
krh@27
   107
	}
krh@27
   108
krh@27
   109
	XML_ParserFree(parser);
krh@27
   110
krh@27
   111
	SHA1_Init(&sha1);
krh@27
   112
	SHA1_Update(&sha1, p, stat.st_size);
krh@27
   113
	SHA1_Final(hash, &sha1);
krh@27
   114
krh@27
   115
	close(fd);
krh@27
   116
krh@27
   117
	munmap(p, stat.st_size);
krh@27
   118
krh@27
   119
	return 0;
krh@27
   120
}
krh@27
   121
krh@27
   122
struct razor_set *
krh@27
   123
razor_import_rzr_files(int count, const char *files[])
krh@27
   124
{
krh@27
   125
	struct import_context ctx;
krh@27
   126
	int i;
krh@27
   127
krh@27
   128
	razor_prepare_import(&ctx);
krh@27
   129
krh@27
   130
	for (i = 0; i < count; i++) {
krh@27
   131
		if (import_rzr_file(&ctx, files[i]) < 0) {
krh@27
   132
			fprintf(stderr, "failed to import %s\n", files[i]);
krh@27
   133
			exit(-1);
krh@27
   134
		}
krh@27
   135
	}
krh@27
   136
krh@27
   137
	return razor_finish_import(&ctx);
krh@27
   138
}
krh@27
   139
krh@27
   140
/* Import a yum filelist as a razor package set. */
krh@27
   141
krh@27
   142
enum {
krh@27
   143
	YUM_STATE_BEGIN,
krh@27
   144
	YUM_STATE_PACKAGE_NAME
krh@27
   145
};
krh@27
   146
krh@27
   147
struct yum_context {
krh@27
   148
	struct import_context ctx;
krh@27
   149
	struct import_property_context *current_property_context;
krh@27
   150
	char *name;
krh@27
   151
	int state;
krh@27
   152
};
krh@27
   153
krh@27
   154
static void
krh@27
   155
yum_start_element(void *data, const char *name, const char **atts)
krh@27
   156
{
krh@27
   157
	struct yum_context *ctx = data;
krh@27
   158
	const char *n, *version;
krh@27
   159
	int i;
krh@27
   160
krh@27
   161
	if (strcmp(name, "name") == 0) {
krh@27
   162
		ctx->state = YUM_STATE_PACKAGE_NAME;
krh@27
   163
	} else if (strcmp(name, "version") == 0) {
krh@27
   164
		version = NULL;
krh@27
   165
		for (i = 0; atts[i]; i += 2) {
krh@27
   166
			if (strcmp(atts[i], "ver") == 0)
krh@27
   167
				version = atts[i + 1];
krh@27
   168
		}
krh@27
   169
		import_context_add_package(&ctx->ctx, ctx->name, version);
krh@27
   170
	} else if (strcmp(name, "rpm:requires") == 0) {
krh@27
   171
		ctx->current_property_context = &ctx->ctx.requires;
krh@27
   172
	} else if (strcmp(name, "rpm:provides") == 0) {
krh@27
   173
		ctx->current_property_context = &ctx->ctx.provides;
krh@27
   174
	} else if (strcmp(name, "rpm:entry") == 0 &&
krh@27
   175
		   ctx->current_property_context != NULL) {
krh@27
   176
		n = NULL;
krh@27
   177
		version = NULL;
krh@27
   178
		for (i = 0; atts[i]; i += 2) {
krh@27
   179
			if (strcmp(atts[i], "name") == 0)
krh@27
   180
				n = atts[i + 1];
krh@27
   181
			else if (strcmp(atts[i], "ver") == 0)
krh@27
   182
				version = atts[i + 1];
krh@27
   183
		}
krh@27
   184
krh@27
   185
		if (n == NULL) {
krh@27
   186
			fprintf(stderr, "invalid rpm:entry, "
krh@27
   187
				"missing name or version attributes\n");
krh@27
   188
			return;
krh@27
   189
		}
krh@27
   190
krh@27
   191
		import_context_add_property(&ctx->ctx,
krh@27
   192
					    ctx->current_property_context,
krh@27
   193
					    n, version);
krh@27
   194
	}
krh@27
   195
}
krh@27
   196
krh@27
   197
static void
krh@27
   198
yum_end_element (void *data, const char *name)
krh@27
   199
{
krh@27
   200
	struct yum_context *ctx = data;
krh@27
   201
krh@27
   202
	if (strcmp(name, "package") == 0) {
krh@27
   203
		free(ctx->name);
krh@27
   204
		import_context_finish_package(&ctx->ctx);
krh@27
   205
	} else if (strcmp(name, "name") == 0) {
krh@27
   206
		ctx->state = 0;
krh@27
   207
	} else if (strcmp(name, "rpm:requires") == 0) {
krh@27
   208
		ctx->current_property_context = NULL;
krh@27
   209
	} else if (strcmp(name, "rpm:provides") == 0) {
krh@27
   210
		ctx->current_property_context = NULL;
krh@27
   211
	}
krh@27
   212
}
krh@27
   213
krh@27
   214
static void
krh@27
   215
yum_character_data (void *data, const XML_Char *s, int len)
krh@27
   216
{
krh@27
   217
	struct yum_context *ctx = data;
krh@27
   218
krh@27
   219
	if (ctx->state == YUM_STATE_PACKAGE_NAME)
krh@27
   220
		ctx->name = strndup(s, len);
krh@27
   221
}
krh@27
   222
krh@27
   223
struct razor_set *
krh@27
   224
razor_set_create_from_yum_filelist(int fd)
krh@27
   225
{
krh@27
   226
	struct yum_context ctx;
krh@27
   227
	XML_Parser parser;
krh@27
   228
	char buf[4096];
krh@27
   229
	int len;
krh@27
   230
krh@27
   231
	razor_prepare_import(&ctx.ctx);
krh@27
   232
krh@27
   233
	parser = XML_ParserCreate(NULL);
krh@27
   234
	XML_SetUserData(parser, &ctx);
krh@27
   235
	XML_SetElementHandler(parser, yum_start_element, yum_end_element);
krh@27
   236
	XML_SetCharacterDataHandler(parser, yum_character_data);
krh@27
   237
krh@27
   238
	while (1) {
krh@27
   239
		len = read(fd, buf, sizeof buf);
krh@27
   240
		if (len < 0) {
krh@27
   241
			fprintf(stderr,
krh@27
   242
				"couldn't read input: %s\n", strerror(errno));
krh@27
   243
			return NULL;
krh@27
   244
		} else if (len == 0)
krh@27
   245
			break;
krh@27
   246
krh@27
   247
		if (XML_Parse(parser, buf, len, 0) == XML_STATUS_ERROR) {
krh@27
   248
			fprintf(stderr,
krh@27
   249
				"%s at line %d\n",
krh@27
   250
				XML_ErrorString(XML_GetErrorCode(parser)),
krh@27
   251
				XML_GetCurrentLineNumber(parser));
krh@27
   252
			return NULL;
krh@27
   253
		}
krh@27
   254
	}
krh@27
   255
krh@27
   256
	XML_ParserFree(parser);
krh@27
   257
krh@27
   258
	return razor_finish_import(&ctx.ctx);
krh@27
   259
}