import.c
author Kristian H?gsberg <krh@redhat.com>
Wed Oct 31 22:41:27 2007 -0400 (2007-10-31)
changeset 66 bb4ca4a47f66
parent 62 3e42af5305ac
child 67 cfe57117efee
permissions -rw-r--r--
Store requires and provides in the same list.
     1 #define _GNU_SOURCE
     2 
     3 #include <string.h>
     4 #include <stdio.h>
     5 #include <sys/stat.h>
     6 #include <sys/mman.h>
     7 #include <unistd.h>
     8 #include <fcntl.h>
     9 #include <errno.h>
    10 
    11 #include <expat.h>
    12 #include <rpm/rpmlib.h>
    13 #include <rpm/rpmdb.h>
    14 #include "sha1.h"
    15 #include "razor.h"
    16 
    17 static void
    18 parse_package(struct razor_importer *importer, const char **atts, void *data)
    19 {
    20 	const char *name = NULL, *version = NULL;
    21 	int i;
    22 
    23 	for (i = 0; atts[i]; i += 2) {
    24 		if (strcmp(atts[i], "name") == 0)
    25 			name = atts[i + 1];
    26 		else if (strcmp(atts[i], "version") == 0)
    27 			version = atts[i + 1];
    28 	}
    29 
    30 	if (name == NULL || version == NULL) {
    31 		fprintf(stderr, "invalid package tag, "
    32 			"missing name or version attributes\n");
    33 		return;
    34 	}
    35 
    36 	razor_importer_begin_package(importer, name, version);
    37 }
    38 
    39 enum {
    40 	RZR_REQUIRES, RZR_PROVIDES
    41 };
    42 
    43 static void
    44 parse_property(struct razor_importer *importer, const char **atts, void *data)
    45 {
    46 	const char *name = NULL, *version = NULL;
    47 	int i;
    48 
    49 	for (i = 0; atts[i]; i += 2) {
    50 		if (strcmp(atts[i], "name") == 0)
    51 			name = atts[i + 1];
    52 		if (strcmp(atts[i], "version") == 0)
    53 			version = atts[i + 1];
    54 	}
    55 	
    56 	if (name == NULL) {
    57 		fprintf(stderr, "invalid tag, missing name attribute\n");
    58 		return;
    59 	}
    60 
    61 	switch ((int) data) {
    62 	case RZR_REQUIRES:
    63 		razor_importer_add_property(importer, name, version,
    64 					    RAZOR_PROPERTY_REQUIRES);
    65 		break;
    66 	case RZR_PROVIDES:
    67 		razor_importer_add_property(importer, name, version,
    68 					    RAZOR_PROPERTY_PROVIDES);
    69 		break;
    70 	}
    71 }
    72 
    73 static void
    74 start_element(void *data, const char *name, const char **atts)
    75 {
    76 	struct razor_importer *importer = data;
    77 
    78 	if (strcmp(name, "package") == 0)
    79 		parse_package(importer, atts, NULL);
    80 	else if (strcmp(name, "requires") == 0)
    81 		parse_property(importer, atts, (void *) RZR_REQUIRES);
    82 	else if (strcmp(name, "provides") == 0)
    83 		parse_property(importer, atts, (void*) RZR_PROVIDES);
    84 }
    85 
    86 static void
    87 end_element (void *data, const char *name)
    88 {
    89 	struct razor_importer *importer = data;
    90 
    91 	if (strcmp(name, "package") == 0)
    92 		razor_importer_finish_package(importer);
    93 }
    94 
    95 static int
    96 import_rzr_file(struct razor_importer *importer, const char *filename)
    97 {
    98 	SHA_CTX sha1;
    99 	XML_Parser parser;
   100 	int fd;
   101 	void *p;
   102 	struct stat stat;
   103 	unsigned char hash[20];
   104 
   105 	fd = open(filename, O_RDONLY);
   106 	if (fstat(fd, &stat) < 0)
   107 		return -1;
   108 	p = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
   109 	if (p == MAP_FAILED)
   110 		return -1;
   111 
   112 	parser = XML_ParserCreate(NULL);
   113 	XML_SetUserData(parser, importer);
   114 	XML_SetElementHandler(parser, start_element, end_element);
   115 	if (XML_Parse(parser, p, stat.st_size, 1) == XML_STATUS_ERROR) {
   116 		fprintf(stderr,
   117 			"%s at line %ld, %s\n",
   118 			XML_ErrorString(XML_GetErrorCode(parser)),
   119 			XML_GetCurrentLineNumber(parser),
   120 			filename);
   121 		return 1;
   122 	}
   123 
   124 	XML_ParserFree(parser);
   125 
   126 	SHA1_Init(&sha1);
   127 	SHA1_Update(&sha1, p, stat.st_size);
   128 	SHA1_Final(hash, &sha1);
   129 
   130 	close(fd);
   131 
   132 	munmap(p, stat.st_size);
   133 
   134 	return 0;
   135 }
   136 
   137 struct razor_set *
   138 razor_import_rzr_files(int count, const char *files[])
   139 {
   140 	struct razor_importer *importer;
   141 	int i;
   142 
   143 	importer = razor_importer_new();
   144 
   145 	for (i = 0; i < count; i++) {
   146 		if (import_rzr_file(importer, files[i]) < 0) {
   147 			fprintf(stderr, "failed to import %s\n", files[i]);
   148 			exit(-1);
   149 		}
   150 	}
   151 
   152 	return razor_importer_finish(importer);
   153 }
   154 
   155 /* Import a yum filelist as a razor package set. */
   156 
   157 enum {
   158 	YUM_STATE_BEGIN,
   159 	YUM_STATE_PACKAGE_NAME,
   160 	YUM_STATE_REQUIRES,
   161 	YUM_STATE_PROVIDES,
   162 	YUM_STATE_FILE
   163 };
   164 
   165 struct yum_context {
   166 	struct razor_importer *importer;
   167 	struct import_property_context *current_property_context;
   168 	char name[256], buffer[512], *p;
   169 	int state;
   170 };
   171 
   172 static void
   173 yum_start_element(void *data, const char *name, const char **atts)
   174 {
   175 	struct yum_context *ctx = data;
   176 	const char *n, *version, *release;
   177 	char buffer[128];
   178 	int i;
   179 
   180 	if (strcmp(name, "name") == 0) {
   181 		ctx->state = YUM_STATE_PACKAGE_NAME;
   182 		ctx->p = ctx->name;
   183 	} else if (strcmp(name, "version") == 0) {
   184 		version = NULL;
   185 		release = NULL;
   186 		for (i = 0; atts[i]; i += 2) {
   187 			if (strcmp(atts[i], "ver") == 0)
   188 				version = atts[i + 1];
   189 			else if (strcmp(atts[i], "rel") == 0)
   190 				release = atts[i + 1];
   191 		}
   192 		if (version == NULL || release == NULL) {
   193 			fprintf(stderr, "invalid version tag, "
   194 				"missing version or  release attribute\n");
   195 			return;
   196 		}
   197 
   198 		snprintf(buffer, sizeof buffer, "%s-%s", version, release);
   199 		razor_importer_begin_package(ctx->importer, ctx->name, buffer);
   200 	} else if (strcmp(name, "rpm:requires") == 0) {
   201 		ctx->state = YUM_STATE_REQUIRES;
   202 	} else if (strcmp(name, "rpm:provides") == 0) {
   203 		ctx->state = YUM_STATE_PROVIDES;
   204 	} else if (strcmp(name, "rpm:entry") == 0 &&
   205 		   ctx->state != YUM_STATE_BEGIN) {
   206 		n = NULL;
   207 		version = NULL;
   208 		release = NULL;
   209 		for (i = 0; atts[i]; i += 2) {
   210 			if (strcmp(atts[i], "name") == 0)
   211 				n = atts[i + 1];
   212 			else if (strcmp(atts[i], "ver") == 0)
   213 				version = atts[i + 1];
   214 			else if (strcmp(atts[i], "rel") == 0)
   215 				release = atts[i + 1];
   216 		}
   217 
   218 		if (n == NULL) {
   219 			fprintf(stderr, "invalid rpm:entry, "
   220 				"missing name or version attributes\n");
   221 			return;
   222 		}
   223 
   224 		if (version && release)
   225 			snprintf(buffer, sizeof buffer,
   226 				 "%s-%s", version, release);
   227 		else if (version)
   228 			strcpy(buffer, version);
   229 		else
   230 			buffer[0] = '\0';
   231 			
   232 		switch (ctx->state) {
   233 		case YUM_STATE_REQUIRES:
   234 			razor_importer_add_property(ctx->importer, n, buffer,
   235 						    RAZOR_PROPERTY_REQUIRES);
   236 			break;
   237 		case YUM_STATE_PROVIDES:
   238 			razor_importer_add_property(ctx->importer, n, buffer,
   239 						    RAZOR_PROPERTY_PROVIDES);
   240 			break;
   241 		}
   242 	} else if (strcmp(name, "file") == 0) {
   243 		ctx->state = YUM_STATE_FILE;
   244 		ctx->p = ctx->buffer;
   245 	}
   246 }
   247 
   248 static void
   249 yum_end_element (void *data, const char *name)
   250 {
   251 	struct yum_context *ctx = data;
   252 
   253 	ctx->state = YUM_STATE_BEGIN;
   254 	if (strcmp(name, "package") == 0)
   255 		razor_importer_finish_package(ctx->importer);
   256 	else if (strcmp(name, "file") == 0)
   257 		razor_importer_add_file(ctx->importer, ctx->buffer);
   258 }
   259 
   260 static void
   261 yum_character_data (void *data, const XML_Char *s, int len)
   262 {
   263 	struct yum_context *ctx = data;
   264 
   265 	switch (ctx->state) {
   266 	case YUM_STATE_PACKAGE_NAME:
   267 	case YUM_STATE_FILE:
   268 		memcpy(ctx->p, s, len);
   269 		ctx->p += len;
   270 		*ctx->p = '\0';
   271 		break;
   272 	}
   273 }
   274 
   275 struct razor_set *
   276 razor_set_create_from_yum_filelist(int fd)
   277 {
   278 	struct yum_context ctx;
   279 	XML_Parser parser;
   280 	char buf[4096];
   281 	int len;
   282 
   283 	ctx.importer = razor_importer_new();	
   284 	ctx.state = YUM_STATE_BEGIN;
   285 
   286 	parser = XML_ParserCreate(NULL);
   287 	XML_SetUserData(parser, &ctx);
   288 	XML_SetElementHandler(parser, yum_start_element, yum_end_element);
   289 	XML_SetCharacterDataHandler(parser, yum_character_data);
   290 
   291 	while (1) {
   292 		len = read(fd, buf, sizeof buf);
   293 		if (len < 0) {
   294 			fprintf(stderr,
   295 				"couldn't read input: %s\n", strerror(errno));
   296 			return NULL;
   297 		} else if (len == 0)
   298 			break;
   299 
   300 		if (XML_Parse(parser, buf, len, 0) == XML_STATUS_ERROR) {
   301 			fprintf(stderr,
   302 				"%s at line %ld\n",
   303 				XML_ErrorString(XML_GetErrorCode(parser)),
   304 				XML_GetCurrentLineNumber(parser));
   305 			return NULL;
   306 		}
   307 	}
   308 
   309 	XML_ParserFree(parser);
   310 
   311 	return razor_importer_finish(ctx.importer);
   312 }
   313 
   314 union rpm_entry {
   315 	void *p;
   316 	char *string;
   317 	char **list;
   318 	unsigned int *flags;
   319 };
   320 
   321 struct razor_set *
   322 razor_set_create_from_rpmdb(void)
   323 {
   324 	struct razor_importer *importer;
   325 	rpmdbMatchIterator iter;
   326 	Header h;
   327 	int_32 type, count, i;
   328 	union rpm_entry name, version, release;
   329 	union rpm_entry property_names, property_versions, property_flags;
   330 	union rpm_entry basenames, dirnames, dirindexes;
   331 	char filename[PATH_MAX];
   332 	rpmdb db;
   333 
   334 	rpmReadConfigFiles(NULL, NULL);
   335 
   336 	if (rpmdbOpen("", &db, O_RDONLY, 0644) != 0) {
   337 		fprintf(stderr, "cannot open rpm database\n");
   338 		exit(1);
   339 	}
   340 
   341 	importer = razor_importer_new();
   342 
   343 	iter = rpmdbInitIterator(db, 0, NULL, 0);
   344 	while (h = rpmdbNextIterator(iter), h != NULL) {
   345 		headerGetEntry(h, RPMTAG_NAME, &type, &name.p, &count);
   346 		headerGetEntry(h, RPMTAG_VERSION, &type, &version.p, &count);
   347 		headerGetEntry(h, RPMTAG_RELEASE, &type, &release.p, &count);
   348 		snprintf(filename, sizeof filename, "%s-%s",
   349 			 version.string, release.string);
   350 		razor_importer_begin_package(importer, name.string, filename);
   351 
   352 		headerGetEntry(h, RPMTAG_REQUIRENAME, &type,
   353 			       &property_names.p, &count);
   354 		headerGetEntry(h, RPMTAG_REQUIREVERSION, &type,
   355 			       &property_versions.p, &count);
   356 		headerGetEntry(h, RPMTAG_REQUIREFLAGS, &type,
   357 			       &property_flags.p, &count);
   358 		for (i = 0; i < count; i++)
   359 			razor_importer_add_property(importer,
   360 						    property_names.list[i],
   361 						    property_versions.list[i],
   362 						    RAZOR_PROPERTY_REQUIRES);
   363 
   364 		headerGetEntry(h, RPMTAG_PROVIDENAME, &type,
   365 			       &property_names.p, &count);
   366 		headerGetEntry(h, RPMTAG_PROVIDEVERSION, &type,
   367 			       &property_versions.p, &count);
   368 		for (i = 0; i < count; i++)
   369 			razor_importer_add_property(importer,
   370 						    property_names.list[i],
   371 						    property_versions.list[i],
   372 						    RAZOR_PROPERTY_PROVIDES);
   373 
   374 		headerGetEntry(h, RPMTAG_BASENAMES, &type,
   375 			       &basenames.p, &count);
   376 		headerGetEntry(h, RPMTAG_DIRNAMES, &type,
   377 			       &dirnames.p, &count);
   378 		headerGetEntry(h, RPMTAG_DIRINDEXES, &type,
   379 			       &dirindexes.p, &count);
   380 		for (i = 0; i < count; i++) {
   381 			snprintf(filename, sizeof filename, "%s%s",
   382 				 dirnames.list[dirindexes.flags[i]],
   383 				 basenames.list[i]);
   384 			razor_importer_add_file(importer, filename);
   385 		}
   386 
   387 		razor_importer_finish_package(importer);
   388 	}
   389 
   390 	rpmdbClose(db);
   391 
   392 	return razor_importer_finish(importer);
   393 }