src/import-rpmdb.c
changeset 241 c3eb520e2219
child 247 63444a10fb8e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/import-rpmdb.c	Mon Jun 16 15:40:30 2008 -0400
     1.3 @@ -0,0 +1,157 @@
     1.4 +/*
     1.5 + * Copyright (C) 2008  Kristian Høgsberg <krh@redhat.com>
     1.6 + * Copyright (C) 2008  Red Hat, Inc
     1.7 + *
     1.8 + * This program is free software; you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation; either version 2 of the License, or
    1.11 + * (at your option) any later version.
    1.12 + *
    1.13 + * This program is distributed in the hope that it will be useful,
    1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.16 + * GNU General Public License for more details.
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License along
    1.19 + * with this program; if not, write to the Free Software Foundation, Inc.,
    1.20 + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + */
    1.22 +
    1.23 +#include <stdio.h>
    1.24 +#include <stddef.h>
    1.25 +#include <string.h>
    1.26 +#include <fcntl.h>
    1.27 +#include <rpm/rpmlib.h>
    1.28 +#include <rpm/rpmdb.h>
    1.29 +
    1.30 +#include "razor.h"
    1.31 +
    1.32 +union rpm_entry {
    1.33 +	void *p;
    1.34 +	char *string;
    1.35 +	char **list;
    1.36 +	uint_32 *flags;
    1.37 +	uint_32 integer;
    1.38 +};
    1.39 +
    1.40 +static enum razor_version_relation
    1.41 +rpm_to_razor_flags (uint_32 flags)
    1.42 +{
    1.43 +	switch (flags & (RPMSENSE_LESS | RPMSENSE_EQUAL | RPMSENSE_GREATER)) {
    1.44 +	case RPMSENSE_LESS:
    1.45 +		return RAZOR_VERSION_LESS;
    1.46 +	case RPMSENSE_LESS|RPMSENSE_EQUAL:
    1.47 +		return RAZOR_VERSION_LESS_OR_EQUAL;
    1.48 +	case RPMSENSE_EQUAL:
    1.49 +		return RAZOR_VERSION_EQUAL;
    1.50 +	case RPMSENSE_GREATER|RPMSENSE_EQUAL:
    1.51 +		return RAZOR_VERSION_GREATER_OR_EQUAL;
    1.52 +	case RPMSENSE_GREATER:
    1.53 +		return RAZOR_VERSION_GREATER;
    1.54 +	}
    1.55 +
    1.56 +	/* FIXME? */
    1.57 +	return RAZOR_VERSION_EQUAL;
    1.58 +}
    1.59 +
    1.60 +static void
    1.61 +add_properties(struct razor_importer *importer,
    1.62 +	       enum razor_property_type property_type,
    1.63 +	       Header h, int_32 name_tag, int_32 version_tag, int_32 flags_tag)
    1.64 +{
    1.65 +	union rpm_entry names, versions, flags;
    1.66 +	int_32 i, type, count;
    1.67 +
    1.68 +	headerGetEntry(h, name_tag, &type, &names.p, &count);
    1.69 +	headerGetEntry(h, version_tag, &type, &versions.p, &count);
    1.70 +	headerGetEntry(h, flags_tag, &type, &flags.p, &count);
    1.71 +
    1.72 +	for (i = 0; i < count; i++)
    1.73 +		razor_importer_add_property(importer,
    1.74 +					    names.list[i],
    1.75 +					    rpm_to_razor_flags (flags.flags[i]),
    1.76 +					    versions.list[i],
    1.77 +					    property_type);
    1.78 +}
    1.79 +
    1.80 +struct razor_set *
    1.81 +razor_set_create_from_rpmdb(void)
    1.82 +{
    1.83 +	struct razor_importer *importer;
    1.84 +	rpmdbMatchIterator iter;
    1.85 +	Header h;
    1.86 +	int_32 type, count, i;
    1.87 +	union rpm_entry name, epoch, version, release, arch;
    1.88 +	union rpm_entry basenames, dirnames, dirindexes;
    1.89 +	char filename[PATH_MAX], evr[128], buf[16];
    1.90 +	rpmdb db;
    1.91 +
    1.92 +	rpmReadConfigFiles(NULL, NULL);
    1.93 +
    1.94 +	if (rpmdbOpen("", &db, O_RDONLY, 0644) != 0) {
    1.95 +		fprintf(stderr, "cannot open rpm database\n");
    1.96 +		exit(1);
    1.97 +	}
    1.98 +
    1.99 +	importer = razor_importer_new();
   1.100 +
   1.101 +	iter = rpmdbInitIterator(db, 0, NULL, 0);
   1.102 +	while (h = rpmdbNextIterator(iter), h != NULL) {
   1.103 +		headerGetEntry(h, RPMTAG_NAME, &type, &name.p, &count);
   1.104 +		headerGetEntry(h, RPMTAG_EPOCH, &type, &epoch.p, &count);
   1.105 +		headerGetEntry(h, RPMTAG_VERSION, &type, &version.p, &count);
   1.106 +		headerGetEntry(h, RPMTAG_RELEASE, &type, &release.p, &count);
   1.107 +		headerGetEntry(h, RPMTAG_ARCH, &type, &arch.p, &count);
   1.108 +
   1.109 +		if (epoch.flags != NULL) {
   1.110 +			snprintf(buf, sizeof buf, "%u", *epoch.flags);
   1.111 +			razor_build_evr(evr, sizeof evr,
   1.112 +					buf, version.string, release.string);
   1.113 +		} else {
   1.114 +			razor_build_evr(evr, sizeof evr,
   1.115 +					NULL, version.string, release.string);
   1.116 +		}
   1.117 +
   1.118 +		razor_importer_begin_package(importer,
   1.119 +					     name.string, evr, arch.string);
   1.120 +
   1.121 +		add_properties(importer, RAZOR_PROPERTY_REQUIRES, h,
   1.122 +			       RPMTAG_REQUIRENAME,
   1.123 +			       RPMTAG_REQUIREVERSION,
   1.124 +			       RPMTAG_REQUIREFLAGS);
   1.125 +
   1.126 +		add_properties(importer, RAZOR_PROPERTY_PROVIDES, h,
   1.127 +			       RPMTAG_PROVIDENAME,
   1.128 +			       RPMTAG_PROVIDEVERSION,
   1.129 +			       RPMTAG_PROVIDEFLAGS);
   1.130 +
   1.131 +		add_properties(importer, RAZOR_PROPERTY_OBSOLETES, h,
   1.132 +			       RPMTAG_OBSOLETENAME,
   1.133 +			       RPMTAG_OBSOLETEVERSION,
   1.134 +			       RPMTAG_OBSOLETEFLAGS);
   1.135 +
   1.136 +		add_properties(importer, RAZOR_PROPERTY_CONFLICTS, h,
   1.137 +			       RPMTAG_CONFLICTNAME,
   1.138 +			       RPMTAG_CONFLICTVERSION,
   1.139 +			       RPMTAG_CONFLICTFLAGS);
   1.140 +
   1.141 +		headerGetEntry(h, RPMTAG_BASENAMES, &type,
   1.142 +			       &basenames.p, &count);
   1.143 +		headerGetEntry(h, RPMTAG_DIRNAMES, &type,
   1.144 +			       &dirnames.p, &count);
   1.145 +		headerGetEntry(h, RPMTAG_DIRINDEXES, &type,
   1.146 +			       &dirindexes.p, &count);
   1.147 +		for (i = 0; i < count; i++) {
   1.148 +			snprintf(filename, sizeof filename, "%s%s",
   1.149 +				 dirnames.list[dirindexes.flags[i]],
   1.150 +				 basenames.list[i]);
   1.151 +			razor_importer_add_file(importer, filename);
   1.152 +		}
   1.153 +
   1.154 +		razor_importer_finish_package(importer);
   1.155 +	}
   1.156 +
   1.157 +	rpmdbClose(db);
   1.158 +
   1.159 +	return razor_importer_finish(importer);
   1.160 +}