src/import-rpmdb.c
author Kristian H?gsberg <krh@redhat.com>
Sat Jun 28 19:22:55 2008 -0400 (2008-06-28)
changeset 298 ddc35bb593ef
parent 259 5b0601d184ed
child 325 73393734833c
permissions -rw-r--r--
Merge commit 'fdo/master'
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>
rhughes@241
    24
#include <rpm/rpmlib.h>
rhughes@241
    25
#include <rpm/rpmdb.h>
rhughes@241
    26
rhughes@241
    27
#include "razor.h"
rhughes@241
    28
rhughes@241
    29
union rpm_entry {
rhughes@241
    30
	void *p;
rhughes@241
    31
	char *string;
rhughes@241
    32
	char **list;
rhughes@241
    33
	uint_32 *flags;
rhughes@241
    34
	uint_32 integer;
rhughes@241
    35
};
rhughes@241
    36
krh@247
    37
static uint32_t
krh@247
    38
rpm_to_razor_flags(uint32_t flags)
rhughes@241
    39
{
krh@247
    40
	uint32_t razor_flags;
rhughes@241
    41
krh@247
    42
	razor_flags = 0;
krh@247
    43
	if (flags & RPMSENSE_LESS)
krh@247
    44
		razor_flags |= RAZOR_PROPERTY_LESS;
krh@247
    45
	if (flags & RPMSENSE_EQUAL)
krh@247
    46
		razor_flags |= RAZOR_PROPERTY_EQUAL;
krh@247
    47
	if (flags & RPMSENSE_GREATER)
krh@247
    48
		razor_flags |= RAZOR_PROPERTY_GREATER;
krh@247
    49
krh@247
    50
	if (flags & RPMSENSE_SCRIPT_PRE)
krh@247
    51
		razor_flags |= RAZOR_PROPERTY_PRE;
krh@247
    52
	if (flags & RPMSENSE_SCRIPT_POST)
krh@247
    53
		razor_flags |= RAZOR_PROPERTY_POST;
krh@247
    54
	if (flags & RPMSENSE_SCRIPT_PREUN)
krh@247
    55
		razor_flags |= RAZOR_PROPERTY_PREUN;
krh@247
    56
	if (flags & RPMSENSE_SCRIPT_POSTUN)
krh@247
    57
		razor_flags |= RAZOR_PROPERTY_POSTUN;
krh@247
    58
krh@247
    59
	return razor_flags;
rhughes@241
    60
}
rhughes@241
    61
rhughes@241
    62
static void
rhughes@241
    63
add_properties(struct razor_importer *importer,
krh@247
    64
	       uint32_t type_flags,
rhughes@241
    65
	       Header h, int_32 name_tag, int_32 version_tag, int_32 flags_tag)
rhughes@241
    66
{
rhughes@241
    67
	union rpm_entry names, versions, flags;
rhughes@241
    68
	int_32 i, type, count;
rhughes@241
    69
rhughes@241
    70
	headerGetEntry(h, name_tag, &type, &names.p, &count);
rhughes@241
    71
	headerGetEntry(h, version_tag, &type, &versions.p, &count);
rhughes@241
    72
	headerGetEntry(h, flags_tag, &type, &flags.p, &count);
rhughes@241
    73
rhughes@241
    74
	for (i = 0; i < count; i++)
rhughes@241
    75
		razor_importer_add_property(importer,
rhughes@241
    76
					    names.list[i],
krh@247
    77
					    rpm_to_razor_flags (flags.flags[i]) | type_flags,
krh@247
    78
					    versions.list[i]);
rhughes@241
    79
}
rhughes@241
    80
rhughes@241
    81
struct razor_set *
rhughes@241
    82
razor_set_create_from_rpmdb(void)
rhughes@241
    83
{
rhughes@241
    84
	struct razor_importer *importer;
rhughes@241
    85
	rpmdbMatchIterator iter;
rhughes@241
    86
	Header h;
rhughes@241
    87
	int_32 type, count, i;
rhughes@241
    88
	union rpm_entry name, epoch, version, release, arch;
jbowes@258
    89
	union rpm_entry summary, description, url, license;
rhughes@241
    90
	union rpm_entry basenames, dirnames, dirindexes;
rhughes@241
    91
	char filename[PATH_MAX], evr[128], buf[16];
rhughes@241
    92
	rpmdb db;
jbowes@263
    93
	int imported_count = 0;
rhughes@241
    94
rhughes@241
    95
	rpmReadConfigFiles(NULL, NULL);
rhughes@241
    96
rhughes@241
    97
	if (rpmdbOpen("", &db, O_RDONLY, 0644) != 0) {
rhughes@241
    98
		fprintf(stderr, "cannot open rpm database\n");
rhughes@241
    99
		exit(1);
rhughes@241
   100
	}
rhughes@241
   101
krh@249
   102
	importer = razor_importer_create();
rhughes@241
   103
rhughes@241
   104
	iter = rpmdbInitIterator(db, 0, NULL, 0);
rhughes@241
   105
	while (h = rpmdbNextIterator(iter), h != NULL) {
rhughes@241
   106
		headerGetEntry(h, RPMTAG_NAME, &type, &name.p, &count);
rhughes@241
   107
		headerGetEntry(h, RPMTAG_EPOCH, &type, &epoch.p, &count);
rhughes@241
   108
		headerGetEntry(h, RPMTAG_VERSION, &type, &version.p, &count);
rhughes@241
   109
		headerGetEntry(h, RPMTAG_RELEASE, &type, &release.p, &count);
rhughes@241
   110
		headerGetEntry(h, RPMTAG_ARCH, &type, &arch.p, &count);
jbowes@258
   111
		headerGetEntry(h, RPMTAG_SUMMARY, &type, &summary.p, &count);
jbowes@258
   112
		headerGetEntry(h, RPMTAG_DESCRIPTION, &type, &description.p,
jbowes@258
   113
			       &count);
jbowes@258
   114
		headerGetEntry(h, RPMTAG_URL, &type, &url.p, &count);
jbowes@258
   115
		headerGetEntry(h, RPMTAG_LICENSE, &type, &license.p, &count);
rhughes@241
   116
rhughes@241
   117
		if (epoch.flags != NULL) {
rhughes@241
   118
			snprintf(buf, sizeof buf, "%u", *epoch.flags);
rhughes@241
   119
			razor_build_evr(evr, sizeof evr,
rhughes@241
   120
					buf, version.string, release.string);
rhughes@241
   121
		} else {
rhughes@241
   122
			razor_build_evr(evr, sizeof evr,
rhughes@241
   123
					NULL, version.string, release.string);
rhughes@241
   124
		}
rhughes@241
   125
rhughes@241
   126
		razor_importer_begin_package(importer,
rhughes@241
   127
					     name.string, evr, arch.string);
jbowes@258
   128
		razor_importer_add_details(importer, summary.string,
jbowes@258
   129
					   description.string, url.string,
jbowes@258
   130
					   license.string);
rhughes@241
   131
rhughes@241
   132
		add_properties(importer, RAZOR_PROPERTY_REQUIRES, h,
rhughes@241
   133
			       RPMTAG_REQUIRENAME,
rhughes@241
   134
			       RPMTAG_REQUIREVERSION,
rhughes@241
   135
			       RPMTAG_REQUIREFLAGS);
rhughes@241
   136
rhughes@241
   137
		add_properties(importer, RAZOR_PROPERTY_PROVIDES, h,
rhughes@241
   138
			       RPMTAG_PROVIDENAME,
rhughes@241
   139
			       RPMTAG_PROVIDEVERSION,
rhughes@241
   140
			       RPMTAG_PROVIDEFLAGS);
rhughes@241
   141
rhughes@241
   142
		add_properties(importer, RAZOR_PROPERTY_OBSOLETES, h,
rhughes@241
   143
			       RPMTAG_OBSOLETENAME,
rhughes@241
   144
			       RPMTAG_OBSOLETEVERSION,
rhughes@241
   145
			       RPMTAG_OBSOLETEFLAGS);
rhughes@241
   146
rhughes@241
   147
		add_properties(importer, RAZOR_PROPERTY_CONFLICTS, h,
rhughes@241
   148
			       RPMTAG_CONFLICTNAME,
rhughes@241
   149
			       RPMTAG_CONFLICTVERSION,
rhughes@241
   150
			       RPMTAG_CONFLICTFLAGS);
rhughes@241
   151
rhughes@241
   152
		headerGetEntry(h, RPMTAG_BASENAMES, &type,
rhughes@241
   153
			       &basenames.p, &count);
rhughes@241
   154
		headerGetEntry(h, RPMTAG_DIRNAMES, &type,
rhughes@241
   155
			       &dirnames.p, &count);
rhughes@241
   156
		headerGetEntry(h, RPMTAG_DIRINDEXES, &type,
rhughes@241
   157
			       &dirindexes.p, &count);
rhughes@241
   158
		for (i = 0; i < count; i++) {
rhughes@241
   159
			snprintf(filename, sizeof filename, "%s%s",
rhughes@241
   160
				 dirnames.list[dirindexes.flags[i]],
rhughes@241
   161
				 basenames.list[i]);
rhughes@241
   162
			razor_importer_add_file(importer, filename);
rhughes@241
   163
		}
rhughes@241
   164
rhughes@241
   165
		razor_importer_finish_package(importer);
jbowes@263
   166
jbowes@263
   167
		printf("\rimporting %d", ++imported_count);
jbowes@263
   168
		fflush(stdout);
rhughes@241
   169
	}
rhughes@241
   170
rhughes@241
   171
	rpmdbClose(db);
rhughes@241
   172
jbowes@263
   173
	printf("\nsaving\n");
rhughes@241
   174
	return razor_importer_finish(importer);
rhughes@241
   175
}