src/import-rpmdb.c
author J. Ali Harlow <ali@juiblex.co.uk>
Fri Apr 17 21:09:55 2009 +0100 (2009-04-17)
changeset 357 39f8dab73084
parent 325 73393734833c
child 369 f8c27fe9fe63
permissions -rw-r--r--
Add hashtable test to expose confusion as to whether 0 is a valid value or not
rhughes@241
     1
/*
rhughes@241
     2
 * Copyright (C) 2008  Kristian Høgsberg <krh@redhat.com>
rhughes@241
     3
 * Copyright (C) 2008  Red Hat, Inc
rhughes@241
     4
 *
rhughes@241
     5
 * This program is free software; you can redistribute it and/or modify
rhughes@241
     6
 * it under the terms of the GNU General Public License as published by
rhughes@241
     7
 * the Free Software Foundation; either version 2 of the License, or
rhughes@241
     8
 * (at your option) any later version.
rhughes@241
     9
 *
rhughes@241
    10
 * This program is distributed in the hope that it will be useful,
rhughes@241
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
rhughes@241
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
rhughes@241
    13
 * GNU General Public License for more details.
rhughes@241
    14
 *
rhughes@241
    15
 * You should have received a copy of the GNU General Public License along
rhughes@241
    16
 * with this program; if not, write to the Free Software Foundation, Inc.,
rhughes@241
    17
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
rhughes@241
    18
 */
rhughes@241
    19
rhughes@241
    20
#include <stdio.h>
rhughes@241
    21
#include <stddef.h>
rhughes@241
    22
#include <string.h>
rhughes@241
    23
#include <fcntl.h>
ali@325
    24
#include <limits.h>
rhughes@241
    25
#include <rpm/rpmlib.h>
rhughes@241
    26
#include <rpm/rpmdb.h>
rhughes@241
    27
rhughes@241
    28
#include "razor.h"
rhughes@241
    29
rhughes@241
    30
union rpm_entry {
rhughes@241
    31
	void *p;
rhughes@241
    32
	char *string;
rhughes@241
    33
	char **list;
ali@356
    34
	uint32_t *flags;
ali@356
    35
	uint32_t integer;
rhughes@241
    36
};
rhughes@241
    37
krh@247
    38
static uint32_t
krh@247
    39
rpm_to_razor_flags(uint32_t flags)
rhughes@241
    40
{
krh@247
    41
	uint32_t razor_flags;
rhughes@241
    42
krh@247
    43
	razor_flags = 0;
krh@247
    44
	if (flags & RPMSENSE_LESS)
krh@247
    45
		razor_flags |= RAZOR_PROPERTY_LESS;
krh@247
    46
	if (flags & RPMSENSE_EQUAL)
krh@247
    47
		razor_flags |= RAZOR_PROPERTY_EQUAL;
krh@247
    48
	if (flags & RPMSENSE_GREATER)
krh@247
    49
		razor_flags |= RAZOR_PROPERTY_GREATER;
krh@247
    50
krh@247
    51
	if (flags & RPMSENSE_SCRIPT_PRE)
krh@247
    52
		razor_flags |= RAZOR_PROPERTY_PRE;
krh@247
    53
	if (flags & RPMSENSE_SCRIPT_POST)
krh@247
    54
		razor_flags |= RAZOR_PROPERTY_POST;
krh@247
    55
	if (flags & RPMSENSE_SCRIPT_PREUN)
krh@247
    56
		razor_flags |= RAZOR_PROPERTY_PREUN;
krh@247
    57
	if (flags & RPMSENSE_SCRIPT_POSTUN)
krh@247
    58
		razor_flags |= RAZOR_PROPERTY_POSTUN;
krh@247
    59
krh@247
    60
	return razor_flags;
rhughes@241
    61
}
rhughes@241
    62
rhughes@241
    63
static void
rhughes@241
    64
add_properties(struct razor_importer *importer,
ali@356
    65
	       uint32_t type_flags, Header h,
ali@356
    66
	       int32_t name_tag, int32_t version_tag, int32_t flags_tag)
rhughes@241
    67
{
rhughes@241
    68
	union rpm_entry names, versions, flags;
ali@356
    69
	int32_t i, type, count;
rhughes@241
    70
rhughes@241
    71
	headerGetEntry(h, name_tag, &type, &names.p, &count);
rhughes@241
    72
	headerGetEntry(h, version_tag, &type, &versions.p, &count);
rhughes@241
    73
	headerGetEntry(h, flags_tag, &type, &flags.p, &count);
rhughes@241
    74
rhughes@241
    75
	for (i = 0; i < count; i++)
rhughes@241
    76
		razor_importer_add_property(importer,
rhughes@241
    77
					    names.list[i],
krh@247
    78
					    rpm_to_razor_flags (flags.flags[i]) | type_flags,
krh@247
    79
					    versions.list[i]);
rhughes@241
    80
}
rhughes@241
    81
rhughes@241
    82
struct razor_set *
rhughes@241
    83
razor_set_create_from_rpmdb(void)
rhughes@241
    84
{
rhughes@241
    85
	struct razor_importer *importer;
rhughes@241
    86
	rpmdbMatchIterator iter;
rhughes@241
    87
	Header h;
ali@356
    88
	int32_t type, count, i;
rhughes@241
    89
	union rpm_entry name, epoch, version, release, arch;
jbowes@258
    90
	union rpm_entry summary, description, url, license;
rhughes@241
    91
	union rpm_entry basenames, dirnames, dirindexes;
rhughes@241
    92
	char filename[PATH_MAX], evr[128], buf[16];
rhughes@241
    93
	rpmdb db;
jbowes@263
    94
	int imported_count = 0;
rhughes@241
    95
rhughes@241
    96
	rpmReadConfigFiles(NULL, NULL);
rhughes@241
    97
rhughes@241
    98
	if (rpmdbOpen("", &db, O_RDONLY, 0644) != 0) {
rhughes@241
    99
		fprintf(stderr, "cannot open rpm database\n");
rhughes@241
   100
		exit(1);
rhughes@241
   101
	}
rhughes@241
   102
krh@249
   103
	importer = razor_importer_create();
rhughes@241
   104
rhughes@241
   105
	iter = rpmdbInitIterator(db, 0, NULL, 0);
rhughes@241
   106
	while (h = rpmdbNextIterator(iter), h != NULL) {
rhughes@241
   107
		headerGetEntry(h, RPMTAG_NAME, &type, &name.p, &count);
rhughes@241
   108
		headerGetEntry(h, RPMTAG_EPOCH, &type, &epoch.p, &count);
rhughes@241
   109
		headerGetEntry(h, RPMTAG_VERSION, &type, &version.p, &count);
rhughes@241
   110
		headerGetEntry(h, RPMTAG_RELEASE, &type, &release.p, &count);
rhughes@241
   111
		headerGetEntry(h, RPMTAG_ARCH, &type, &arch.p, &count);
jbowes@258
   112
		headerGetEntry(h, RPMTAG_SUMMARY, &type, &summary.p, &count);
jbowes@258
   113
		headerGetEntry(h, RPMTAG_DESCRIPTION, &type, &description.p,
jbowes@258
   114
			       &count);
jbowes@258
   115
		headerGetEntry(h, RPMTAG_URL, &type, &url.p, &count);
jbowes@258
   116
		headerGetEntry(h, RPMTAG_LICENSE, &type, &license.p, &count);
rhughes@241
   117
rhughes@241
   118
		if (epoch.flags != NULL) {
rhughes@241
   119
			snprintf(buf, sizeof buf, "%u", *epoch.flags);
rhughes@241
   120
			razor_build_evr(evr, sizeof evr,
rhughes@241
   121
					buf, version.string, release.string);
rhughes@241
   122
		} else {
rhughes@241
   123
			razor_build_evr(evr, sizeof evr,
rhughes@241
   124
					NULL, version.string, release.string);
rhughes@241
   125
		}
rhughes@241
   126
rhughes@241
   127
		razor_importer_begin_package(importer,
rhughes@241
   128
					     name.string, evr, arch.string);
jbowes@258
   129
		razor_importer_add_details(importer, summary.string,
jbowes@258
   130
					   description.string, url.string,
jbowes@258
   131
					   license.string);
rhughes@241
   132
rhughes@241
   133
		add_properties(importer, RAZOR_PROPERTY_REQUIRES, h,
rhughes@241
   134
			       RPMTAG_REQUIRENAME,
rhughes@241
   135
			       RPMTAG_REQUIREVERSION,
rhughes@241
   136
			       RPMTAG_REQUIREFLAGS);
rhughes@241
   137
rhughes@241
   138
		add_properties(importer, RAZOR_PROPERTY_PROVIDES, h,
rhughes@241
   139
			       RPMTAG_PROVIDENAME,
rhughes@241
   140
			       RPMTAG_PROVIDEVERSION,
rhughes@241
   141
			       RPMTAG_PROVIDEFLAGS);
rhughes@241
   142
rhughes@241
   143
		add_properties(importer, RAZOR_PROPERTY_OBSOLETES, h,
rhughes@241
   144
			       RPMTAG_OBSOLETENAME,
rhughes@241
   145
			       RPMTAG_OBSOLETEVERSION,
rhughes@241
   146
			       RPMTAG_OBSOLETEFLAGS);
rhughes@241
   147
rhughes@241
   148
		add_properties(importer, RAZOR_PROPERTY_CONFLICTS, h,
rhughes@241
   149
			       RPMTAG_CONFLICTNAME,
rhughes@241
   150
			       RPMTAG_CONFLICTVERSION,
rhughes@241
   151
			       RPMTAG_CONFLICTFLAGS);
rhughes@241
   152
rhughes@241
   153
		headerGetEntry(h, RPMTAG_BASENAMES, &type,
rhughes@241
   154
			       &basenames.p, &count);
rhughes@241
   155
		headerGetEntry(h, RPMTAG_DIRNAMES, &type,
rhughes@241
   156
			       &dirnames.p, &count);
rhughes@241
   157
		headerGetEntry(h, RPMTAG_DIRINDEXES, &type,
rhughes@241
   158
			       &dirindexes.p, &count);
rhughes@241
   159
		for (i = 0; i < count; i++) {
rhughes@241
   160
			snprintf(filename, sizeof filename, "%s%s",
rhughes@241
   161
				 dirnames.list[dirindexes.flags[i]],
rhughes@241
   162
				 basenames.list[i]);
rhughes@241
   163
			razor_importer_add_file(importer, filename);
rhughes@241
   164
		}
rhughes@241
   165
rhughes@241
   166
		razor_importer_finish_package(importer);
jbowes@263
   167
jbowes@263
   168
		printf("\rimporting %d", ++imported_count);
jbowes@263
   169
		fflush(stdout);
rhughes@241
   170
	}
rhughes@241
   171
rhughes@241
   172
	rpmdbClose(db);
rhughes@241
   173
jbowes@263
   174
	printf("\nsaving\n");
rhughes@241
   175
	return razor_importer_finish(importer);
rhughes@241
   176
}