rpm.c
author Kristian H?gsberg <krh@redhat.com>
Mon Apr 07 01:03:07 2008 -0400 (2008-04-07)
changeset 199 9ce6d4e1b067
parent 198 d26bdf77569d
child 202 e8594c82dffc
permissions -rw-r--r--
Reduce harmless warnings from run_script().
     1 #include <stdio.h>
     2 #include <stddef.h>
     3 #include <string.h>
     4 #include <errno.h>
     5 #include <sys/stat.h>
     6 #include <sys/mman.h>
     7 #include <sys/types.h>
     8 #include <sys/wait.h>
     9 #include <fcntl.h>
    10 #include <unistd.h>
    11 #include <arpa/inet.h>
    12 #include <rpm/rpmlib.h>
    13 #include <rpm/rpmdb.h>
    14 #include <zlib.h>
    15 
    16 #include "razor.h"
    17 #include "razor-internal.h"
    18 
    19 #define	RPM_LEAD_SIZE 96
    20 
    21 struct rpm_header {
    22 	unsigned char magic[4];
    23 	unsigned char reserved[4];
    24 	int nindex;
    25 	int hsize;
    26 };
    27 
    28 struct rpm_header_index {
    29 	int tag;
    30 	int type;
    31 	int offset;
    32 	int count;
    33 };
    34 
    35 struct razor_rpm {
    36 	struct rpm_header *signature;
    37 	struct rpm_header *header;
    38 	const char **dirs;
    39 	const char *pool;
    40 	void *map;
    41 	size_t size;
    42 	void *payload;
    43 };
    44 
    45 static struct rpm_header_index *
    46 razor_rpm_get_header(struct razor_rpm *rpm, unsigned int tag)
    47 {
    48 	struct rpm_header_index *index, *end;
    49 
    50 	index = (struct rpm_header_index *) (rpm->header + 1);
    51 	end = index + ntohl(rpm->header->nindex);
    52 	while (index < end) {
    53 		if (ntohl(index->tag) == tag)
    54 			return index;
    55 		index++;
    56 	}
    57 
    58 	return NULL;
    59 }
    60 
    61 static const void *
    62 razor_rpm_get_indirect(struct razor_rpm *rpm,
    63 		       unsigned int tag, unsigned int *count)
    64 {
    65 	struct rpm_header_index *index;
    66 
    67 	index = razor_rpm_get_header(rpm, tag);
    68 	if (index != NULL) {
    69 		if (count)
    70 			*count = ntohl(index->count);
    71 
    72 		return rpm->pool + ntohl(index->offset);
    73 	}
    74 
    75 	return NULL;
    76 }
    77 
    78 static enum razor_version_relation
    79 rpm_to_razor_flags (uint_32 flags)
    80 {
    81 	switch (flags & (RPMSENSE_LESS | RPMSENSE_EQUAL | RPMSENSE_GREATER)) {
    82 	case RPMSENSE_LESS:
    83 		return RAZOR_VERSION_LESS;
    84 	case RPMSENSE_LESS|RPMSENSE_EQUAL:
    85 		return RAZOR_VERSION_LESS_OR_EQUAL;
    86 	case RPMSENSE_EQUAL:
    87 		return RAZOR_VERSION_EQUAL;
    88 	case RPMSENSE_GREATER|RPMSENSE_EQUAL:
    89 		return RAZOR_VERSION_GREATER_OR_EQUAL;
    90 	case RPMSENSE_GREATER:
    91 		return RAZOR_VERSION_GREATER;
    92 	}
    93 
    94 	/* FIXME? */
    95 	return RAZOR_VERSION_EQUAL;
    96 }
    97 
    98 static void
    99 import_properties(struct razor_importer *importer, unsigned long type,
   100 		  struct razor_rpm *rpm,
   101 		  int name_tag, int version_tag, int flags_tag)
   102 {
   103 	const char *name, *version;
   104 	const uint_32 *flags;
   105 	uint_32 f;
   106 	unsigned int i, count;
   107 
   108 	name = razor_rpm_get_indirect(rpm, name_tag, &count);
   109 	if (name == NULL)
   110 		return;
   111 
   112 	flags = razor_rpm_get_indirect(rpm, flags_tag, &count);
   113 
   114 	version = razor_rpm_get_indirect(rpm, version_tag, &count);
   115 	for (i = 0; i < count; i++) {
   116 		f = rpm_to_razor_flags(ntohl(flags[i]));
   117 		razor_importer_add_property(importer, name, f, version, type);
   118 		name += strlen(name) + 1;
   119 		version += strlen(version) + 1;
   120 	}
   121 }
   122 
   123 static void
   124 import_files(struct razor_importer *importer, struct razor_rpm *rpm)
   125 {
   126 	const char *name;
   127 	const uint32_t *index;
   128 	unsigned int i, count;
   129 	char buffer[256];
   130 
   131 	/* assert: count is the same for all arrays */
   132 
   133 	index = razor_rpm_get_indirect(rpm, RPMTAG_DIRINDEXES, &count);
   134 	name = razor_rpm_get_indirect(rpm, RPMTAG_BASENAMES, &count);
   135 	for (i = 0; i < count; i++) {
   136 		snprintf(buffer, sizeof buffer,
   137 			 "%s%s", rpm->dirs[ntohl(*index)], name);
   138 		razor_importer_add_file(importer, buffer);
   139 		name += strlen(name) + 1;
   140 		index++;
   141 	}
   142 }
   143 
   144 struct razor_rpm *
   145 razor_rpm_open(const char *filename)
   146 {
   147 	struct razor_rpm *rpm;
   148 	struct rpm_header_index *base, *index;
   149 	struct stat buf;
   150 	unsigned int count, i, nindex, hsize;
   151 	const char *name;
   152 	int fd;
   153 
   154 	rpm = malloc(sizeof *rpm);
   155 	memset(rpm, 0, sizeof *rpm);
   156 
   157 	fd = open(filename, O_RDONLY);
   158 	if (fd < 0) {
   159 		fprintf(stderr, "couldn't open %s\n", filename);
   160 		return NULL;
   161 	}
   162 
   163 	if (fstat(fd, &buf) < 0) {
   164 		fprintf(stderr, "failed to stat %s (%m)\n", filename);
   165 		return NULL;
   166 	}
   167 
   168 	rpm->size = buf.st_size;
   169 	rpm->map = mmap(NULL, rpm->size, PROT_READ, MAP_PRIVATE, fd, 0);
   170 	if (rpm->map == MAP_FAILED) {
   171 		fprintf(stderr, "couldn't mmap %s\n", filename);
   172 		return NULL;
   173 	}
   174 	close(fd);
   175 
   176 	rpm->signature = rpm->map + RPM_LEAD_SIZE;
   177 	nindex = ntohl(rpm->signature->nindex);
   178 	hsize = ntohl(rpm->signature->hsize);
   179 	rpm->header = (void *) (rpm->signature + 1) +
   180 		ALIGN(nindex * sizeof *index + hsize, 8);
   181 	nindex = ntohl(rpm->header->nindex);
   182 	hsize = ntohl(rpm->header->hsize);
   183 	rpm->payload = (void *) (rpm->header + 1) +
   184 		nindex * sizeof *index + hsize;
   185 
   186 	base = (struct rpm_header_index *) (rpm->header + 1);
   187 	rpm->pool = (void *) base + nindex * sizeof *index;
   188 
   189 	/* Look up dir names now so we can index them directly. */
   190 	name = razor_rpm_get_indirect(rpm, RPMTAG_DIRNAMES, &count);
   191 	if (name) {
   192 		rpm->dirs = calloc(count, sizeof *rpm->dirs);
   193 		for (i = 0; i < count; i++) {
   194 			rpm->dirs[i] = name;
   195 			name += strlen(name) + 1;
   196 		}
   197 	} else {
   198 		name = razor_rpm_get_indirect(rpm, RPMTAG_OLDFILENAMES,
   199 					      &count);
   200 		if (name) {
   201 			fprintf(stderr, "old filenames not supported\n");
   202 			return NULL;
   203 		}
   204 	}
   205 
   206 	return rpm;
   207 }
   208 
   209 struct cpio_file_header {
   210 	char magic[6];
   211 	char inode[8];
   212 	char mode[8];
   213 	char uid[8];
   214 	char gid[8];
   215 	char nlink[8];
   216 	char mtime[8];
   217 	char filesize[8];
   218 	char devmajor[8];
   219 	char devminor[8];
   220 	char rdevmajor[8];
   221 	char rdevminor[8];
   222 	char namesize[8];
   223 	char checksum[8];
   224 	char filename[0];
   225 };
   226 
   227 /* gzip flags */
   228 #define ASCII_FLAG   0x01 /* bit 0 set: file probably ascii text */
   229 #define HEAD_CRC     0x02 /* bit 1 set: header CRC present */
   230 #define EXTRA_FIELD  0x04 /* bit 2 set: extra field present */
   231 #define ORIG_NAME    0x08 /* bit 3 set: original file name present */
   232 #define COMMENT      0x10 /* bit 4 set: file comment present */
   233 #define RESERVED     0xE0 /* bits 5..7: reserved */
   234 
   235 struct installer {
   236 	const char *root;
   237 	struct razor_rpm *rpm;
   238 	z_stream stream;
   239 	unsigned char buffer[32768];
   240 	size_t rest, length;
   241 };
   242 
   243 static int
   244 installer_inflate(struct installer *installer)
   245 {
   246 	size_t length;
   247 	int err;
   248 
   249 	if (ALIGN(installer->rest, 4) > sizeof installer->buffer)
   250 		length = sizeof installer->buffer;
   251 	else
   252 		length = installer->rest;
   253 
   254 	installer->stream.next_out = installer->buffer;
   255 	installer->stream.avail_out = ALIGN(length, 4);
   256 	err = inflate(&installer->stream, Z_SYNC_FLUSH);
   257 	if (err != Z_OK && err != Z_STREAM_END) {
   258 		fprintf(stderr, "inflate error: %d (%m)\n", err);
   259 		return -1;
   260 	}
   261 
   262 	installer->rest -= length;
   263 	installer->length = length;
   264 
   265 	return 0;
   266 }
   267 
   268 static int
   269 create_path(struct installer *installer,
   270 	    const char *path, const char *name, unsigned int mode)
   271 {
   272 	char buffer[PATH_MAX];
   273 	struct stat buf;
   274 	int fd, ret;
   275 
   276 	if (razor_create_dir(installer->root, path) < 0)
   277 		return -1;
   278 
   279 	/* assertion: root doesn't end in a slash, path begins and end
   280 	 * with a slash, name does not begin with a slash. */
   281 	snprintf(buffer, sizeof buffer, "%s%s%s",
   282 		 installer->root, path, name);
   283 
   284 	switch (mode >> 12) {
   285 	case REG:
   286 		/* FIXME: handle the case where a file is already there. */
   287 		fd = open(buffer, O_WRONLY | O_CREAT | O_TRUNC, mode & 0x1ff);
   288 		if (fd < 0){
   289 			fprintf(stderr, "failed to create file %s\n", buffer);
   290 			return -1;
   291 		}
   292 		while (installer->rest > 0) {
   293 			if (installer_inflate(installer)) {
   294 				fprintf(stderr, "failed to inflate\n");
   295 				return -1;
   296 			}
   297 			if (razor_write(fd, installer->buffer,
   298 					installer->length)) {
   299 				fprintf(stderr, "failed to write payload\n");
   300 				return -1;
   301 			}
   302 		}
   303 		if (close(fd) < 0) {
   304 			fprintf(stderr, "failed to close %s: %m\n", buffer);
   305 			return -1;
   306 		}
   307 		return 0;
   308 	case XDIR:
   309 		ret = mkdir(buffer, mode & 0x1ff);
   310 		if (ret == 0 || errno != EEXIST)
   311 			return ret;
   312 		if (stat(buffer, &buf) || !S_ISDIR(buf.st_mode)) {
   313 			/* FIXME: also check that mode match. */
   314 			fprintf(stderr,
   315 				"%s exists but is not a directory\n", buffer);
   316 			return -1;
   317 		}
   318 		return 0;
   319 	case PIPE:
   320 	case CDEV:
   321 	case BDEV:
   322 	case SOCK:
   323 		printf("%s: unhandled file type %d\n", buffer, mode >> 12);
   324 		return 0;
   325 	case LINK:
   326 		if (installer_inflate(installer)) {
   327 			fprintf(stderr, "failed to inflate\n");
   328 			return -1;
   329 		}
   330 		if (installer->length >= sizeof installer->buffer) {
   331 			fprintf(stderr, "link name too long\n");
   332 			return -1;
   333 		}
   334 		installer->buffer[installer->length] = '\0';
   335 		if (symlink((const char *) installer->buffer, buffer)) {
   336 			fprintf(stderr, "failed to create symlink, %m\n");
   337 			return -1;
   338 		}
   339 		return 0;
   340 	default:
   341 		printf("%s: unknown file type %d\n", buffer, mode >> 12);
   342 		return 0;
   343 	}
   344 }
   345 
   346 static int
   347 run_script(struct installer *installer,
   348 	   unsigned int program_tag, unsigned int script_tag)
   349 {
   350 	int pid, status, fd[2];
   351 	const char *script = NULL, *program = NULL;
   352 
   353 	program = razor_rpm_get_indirect(installer->rpm, program_tag, NULL);
   354 	script = razor_rpm_get_indirect(installer->rpm, script_tag, NULL);
   355 	if (program == NULL && script == NULL) {
   356 		return;
   357 	} else if (program == NULL) {
   358 		program = "/bin/sh";
   359 	}
   360 
   361 	if (pipe(fd) < 0) {
   362 		fprintf(stderr, "failed to create pipe\n");
   363 		return -1;
   364 	}
   365 	pid = fork();
   366 	if (pid < 0) {
   367 		fprintf(stderr, "failed to fork, %m\n");
   368 	} else if (pid == 0) {
   369 		if (dup2(fd[0], STDIN_FILENO) < 0) {
   370 			fprintf(stderr, "failed redirect stdin, %m\n");
   371 			return -1;
   372 		}
   373 		if (close(fd[0]) < 0 || close(fd[1]) < 0) {
   374 			fprintf(stderr, "failed to close pipe, %m\n");
   375 			return -1;
   376 		}
   377 		if (chroot(installer->root) < 0) {
   378 			fprintf(stderr, "failed to chroot to %s, %m\n",
   379 				installer->root);
   380 			return -1;
   381 		}
   382 		printf("executing program %s in chroot %s\n",
   383 		       program, installer->root);
   384 		if (execl(program, program, NULL)) {
   385 			fprintf(stderr, "failed to exec %s, %m\n", program);
   386 			exit(-1);
   387 		}
   388 	} else {
   389 		if (script && razor_write(fd[1], script, strlen(script)) < 0) {
   390 			fprintf(stderr, "failed to pipe script, %m\n");
   391 			return -1;
   392 		}
   393 		if (close(fd[0]) || close(fd[1])) {
   394 			fprintf(stderr, "failed to close pipe, %m\n");
   395 			return -1;
   396 		}
   397 		if (wait(&status) < 0) {
   398 			fprintf(stderr, "wait for child failed, %m");
   399 			return -1;
   400 		}
   401 		if (status)
   402 			printf("script exited with status %d\n", status);
   403 	}
   404 
   405 	return 0;
   406 }
   407 
   408 static int
   409 installer_init(struct installer *installer)
   410 {
   411 	unsigned char *gz_header;
   412 	int method, flags, err;
   413 
   414 	gz_header = installer->rpm->payload;
   415 	if (gz_header[0] != 0x1f || gz_header[1] != 0x8b) {
   416 		fprintf(stderr, "payload section doesn't have gz header\n");
   417 		return -1;
   418 	}
   419 
   420 	method = gz_header[2];
   421 	flags = gz_header[3];
   422 
   423 	if (method != Z_DEFLATED || flags != 0) {
   424 		fprintf(stderr,
   425 			"unknown payload compression method or flags set\n");
   426 		return -1;
   427 	}
   428 
   429 	installer->stream.zalloc = NULL;
   430 	installer->stream.zfree = NULL;
   431 	installer->stream.opaque = NULL;
   432 
   433 	installer->stream.next_in  = gz_header + 10;
   434 	installer->stream.avail_in =
   435 		(installer->rpm->map + installer->rpm->size) -
   436 		(void *) installer->stream.next_in;
   437 	installer->stream.next_out = NULL;
   438 	installer->stream.avail_out = 0;
   439 
   440 	err = inflateInit2(&installer->stream, -MAX_WBITS);
   441 	if (err != Z_OK) {
   442 		fprintf(stderr, "inflateInit error: %d\n", err);
   443 		return -1;
   444 	}
   445 
   446 	return 0;
   447 }
   448 
   449 static int
   450 installer_finish(struct installer *installer)
   451 {
   452 	int err;
   453 
   454 	err = inflateEnd(&installer->stream);
   455 
   456 	if (err != Z_OK) {
   457 		fprintf(stderr, "inflateEnd error: %d\n", err);
   458 		return -1;
   459 	}	    
   460 
   461 	return 0;
   462 }
   463 
   464 int
   465 razor_rpm_install(struct razor_rpm *rpm, const char *root)
   466 {
   467 	struct installer installer;
   468 	unsigned int count, i, length;
   469 	struct cpio_file_header *header;
   470 	const uint32_t *size, *index, *flags;
   471 	const unsigned short *mode;
   472 	const char *name, *dir;
   473 	struct stat buf;
   474 
   475 	installer.rpm = rpm;
   476 	installer.root = root;
   477 
   478 	/* FIXME: Only do this before a transaction, not per rpm. */
   479 	if (stat(root, &buf) < 0 || !S_ISDIR(buf.st_mode)) {
   480 		fprintf(stderr,
   481 			"root installation directory \"%s\" does not exist\n",
   482 			root);
   483 		return -1;
   484 	}
   485 
   486 	if (installer_init(&installer))
   487 		return -1;
   488 
   489 	run_script(&installer, RPMTAG_PREINPROG, RPMTAG_PREIN);
   490 
   491 	name = razor_rpm_get_indirect(rpm, RPMTAG_BASENAMES, &count);
   492 	size = razor_rpm_get_indirect(rpm, RPMTAG_FILESIZES, &count);
   493 	index = razor_rpm_get_indirect(rpm, RPMTAG_DIRINDEXES, &count);
   494 	mode = razor_rpm_get_indirect(rpm, RPMTAG_FILEMODES, &count);
   495 	flags = razor_rpm_get_indirect(rpm, RPMTAG_FILEFLAGS, &count);
   496 
   497 	for (i = 0; name && i < count; i++) {
   498 		dir = rpm->dirs[ntohl(*index)];
   499 
   500 		/* Skip past the cpio header block unless it's a ghost file,
   501 		 * in which case doesn't appear in the cpio archive. */
   502 		if (!(ntohl(*flags) & RPMFILE_GHOST)) {
   503 			/* Plus two for the leading '.' and the terminating NUL. */
   504 			length = sizeof *header + strlen(dir) + strlen(name) + 2;
   505 			installer.rest = ALIGN(length, 4);
   506 			if (installer_inflate(&installer))
   507 				return -1;
   508 		}
   509 
   510 		installer.rest = ntohl(*size);
   511 		if (create_path(&installer, dir, name, ntohs(*mode)) < 0)
   512 			return -1;
   513 
   514 		name += strlen(name) + 1;
   515 		index++;
   516 		size++;
   517 		mode++;
   518 		flags++;
   519 	}
   520 
   521 	if (installer_finish(&installer))
   522 		return -1;
   523 
   524 	run_script(&installer, RPMTAG_POSTINPROG, RPMTAG_POSTIN);
   525 
   526 	return 0;
   527 }
   528 
   529 int
   530 razor_rpm_close(struct razor_rpm *rpm)
   531 {
   532 	int err;
   533 
   534 	free(rpm->dirs);
   535 	err = munmap(rpm->map, rpm->size);
   536 	free(rpm);
   537 
   538 	return err;
   539 }
   540 
   541 int
   542 razor_importer_add_rpm(struct razor_importer *importer, struct razor_rpm *rpm)
   543 {
   544 	const char *name, *version, *release, *arch;
   545 	const uint_32 *epoch;
   546 	char evr[128], buf[16];
   547 
   548 	name = razor_rpm_get_indirect(rpm, RPMTAG_NAME, NULL);
   549 	epoch = razor_rpm_get_indirect(rpm, RPMTAG_EPOCH, NULL);
   550 	version = razor_rpm_get_indirect(rpm, RPMTAG_VERSION, NULL);
   551 	release = razor_rpm_get_indirect(rpm, RPMTAG_RELEASE, NULL);
   552 	arch = razor_rpm_get_indirect(rpm, RPMTAG_ARCH, NULL);
   553 
   554 	if (epoch) {
   555 		snprintf(buf, sizeof buf, "%u", ntohl(*epoch));
   556 		razor_build_evr(evr, sizeof evr, buf, version, release);
   557 	} else {
   558 		razor_build_evr(evr, sizeof evr, NULL, version, release);
   559 	}
   560 	razor_importer_begin_package(importer, name, evr, arch);
   561 
   562 	import_properties(importer, RAZOR_PROPERTY_REQUIRES, rpm,
   563 			  RPMTAG_REQUIRENAME,
   564 			  RPMTAG_REQUIREVERSION,
   565 			  RPMTAG_REQUIREFLAGS);
   566 
   567 	import_properties(importer, RAZOR_PROPERTY_PROVIDES, rpm,
   568 			  RPMTAG_PROVIDENAME,
   569 			  RPMTAG_PROVIDEVERSION,
   570 			  RPMTAG_PROVIDEFLAGS);
   571 
   572 	import_properties(importer, RAZOR_PROPERTY_OBSOLETES, rpm,
   573 			  RPMTAG_OBSOLETENAME,
   574 			  RPMTAG_OBSOLETEVERSION,
   575 			  RPMTAG_OBSOLETEFLAGS);
   576 
   577 	import_properties(importer, RAZOR_PROPERTY_CONFLICTS, rpm,
   578 			  RPMTAG_CONFLICTNAME,
   579 			  RPMTAG_CONFLICTVERSION,
   580 			  RPMTAG_CONFLICTFLAGS);
   581 
   582 	import_files(importer, rpm);
   583 
   584 	razor_importer_finish_package(importer);
   585 
   586 	return 0;
   587 }
   588 
   589 union rpm_entry {
   590 	void *p;
   591 	char *string;
   592 	char **list;
   593 	uint_32 *flags;
   594 	uint_32 integer;
   595 };
   596 
   597 static void
   598 add_properties(struct razor_importer *importer,
   599 	       enum razor_property_type property_type,
   600 	       Header h, int_32 name_tag, int_32 version_tag, int_32 flags_tag)
   601 {
   602 	union rpm_entry names, versions, flags;
   603 	int_32 i, type, count;
   604 
   605 	headerGetEntry(h, name_tag, &type, &names.p, &count);
   606 	headerGetEntry(h, version_tag, &type, &versions.p, &count);
   607 	headerGetEntry(h, flags_tag, &type, &flags.p, &count);
   608 
   609 	for (i = 0; i < count; i++)
   610 		razor_importer_add_property(importer,
   611 					    names.list[i],
   612 					    rpm_to_razor_flags (flags.flags[i]),
   613 					    versions.list[i],
   614 					    property_type);
   615 }
   616 
   617 struct razor_set *
   618 razor_set_create_from_rpmdb(void)
   619 {
   620 	struct razor_importer *importer;
   621 	rpmdbMatchIterator iter;
   622 	Header h;
   623 	int_32 type, count, i;
   624 	union rpm_entry name, epoch, version, release, arch;
   625 	union rpm_entry basenames, dirnames, dirindexes;
   626 	char filename[PATH_MAX], evr[128], buf[16];
   627 	rpmdb db;
   628 
   629 	rpmReadConfigFiles(NULL, NULL);
   630 
   631 	if (rpmdbOpen("", &db, O_RDONLY, 0644) != 0) {
   632 		fprintf(stderr, "cannot open rpm database\n");
   633 		exit(1);
   634 	}
   635 
   636 	importer = razor_importer_new();
   637 
   638 	iter = rpmdbInitIterator(db, 0, NULL, 0);
   639 	while (h = rpmdbNextIterator(iter), h != NULL) {
   640 		headerGetEntry(h, RPMTAG_NAME, &type, &name.p, &count);
   641 		headerGetEntry(h, RPMTAG_EPOCH, &type, &epoch.p, &count);
   642 		headerGetEntry(h, RPMTAG_VERSION, &type, &version.p, &count);
   643 		headerGetEntry(h, RPMTAG_RELEASE, &type, &release.p, &count);
   644 		headerGetEntry(h, RPMTAG_ARCH, &type, &arch.p, &count);
   645 
   646 		if (epoch.flags != NULL) {
   647 			snprintf(buf, sizeof buf, "%u", *epoch.flags);
   648 			razor_build_evr(evr, sizeof evr,
   649 					buf, version.string, release.string);
   650 		} else {
   651 			razor_build_evr(evr, sizeof evr,
   652 					NULL, version.string, release.string);
   653 		}
   654 
   655 		razor_importer_begin_package(importer,
   656 					     name.string, evr, arch.string);
   657 
   658 		add_properties(importer, RAZOR_PROPERTY_REQUIRES, h,
   659 			       RPMTAG_REQUIRENAME,
   660 			       RPMTAG_REQUIREVERSION,
   661 			       RPMTAG_REQUIREFLAGS);
   662 
   663 		add_properties(importer, RAZOR_PROPERTY_PROVIDES, h,
   664 			       RPMTAG_PROVIDENAME,
   665 			       RPMTAG_PROVIDEVERSION,
   666 			       RPMTAG_PROVIDEFLAGS);
   667 
   668 		add_properties(importer, RAZOR_PROPERTY_OBSOLETES, h,
   669 			       RPMTAG_OBSOLETENAME,
   670 			       RPMTAG_OBSOLETEVERSION,
   671 			       RPMTAG_OBSOLETEFLAGS);
   672 
   673 		add_properties(importer, RAZOR_PROPERTY_CONFLICTS, h,
   674 			       RPMTAG_CONFLICTNAME,
   675 			       RPMTAG_CONFLICTVERSION,
   676 			       RPMTAG_CONFLICTFLAGS);
   677 
   678 		headerGetEntry(h, RPMTAG_BASENAMES, &type,
   679 			       &basenames.p, &count);
   680 		headerGetEntry(h, RPMTAG_DIRNAMES, &type,
   681 			       &dirnames.p, &count);
   682 		headerGetEntry(h, RPMTAG_DIRINDEXES, &type,
   683 			       &dirindexes.p, &count);
   684 		for (i = 0; i < count; i++) {
   685 			snprintf(filename, sizeof filename, "%s%s",
   686 				 dirnames.list[dirindexes.flags[i]],
   687 				 basenames.list[i]);
   688 			razor_importer_add_file(importer, filename);
   689 		}
   690 
   691 		razor_importer_finish_package(importer);
   692 	}
   693 
   694 	rpmdbClose(db);
   695 
   696 	return razor_importer_finish(importer);
   697 }