summaryrefslogtreecommitdiff
path: root/src/import-rpmdb.c
blob: 59f41f21540c3a04615e72a052b2b070ded0aac6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 * Copyright (C) 2008  Kristian Høgsberg <krh@redhat.com>
 * Copyright (C) 2008  Red Hat, Inc
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include <fcntl.h>
#include <limits.h>
#include <rpm/rpmlib.h>
#include <rpm/rpmdb.h>
#define _RPM_4_4_COMPAT 1
#include <rpm/rpmlegacy.h>

#include "razor.h"

union rpm_entry {
	void *p;
	char *string;
	char **list;
	uint_32 *flags;
	uint_32 integer;
};

static uint32_t
rpm_to_razor_flags(uint32_t flags)
{
	uint32_t razor_flags;

	razor_flags = 0;
	if (flags & RPMSENSE_LESS)
		razor_flags |= RAZOR_PROPERTY_LESS;
	if (flags & RPMSENSE_EQUAL)
		razor_flags |= RAZOR_PROPERTY_EQUAL;
	if (flags & RPMSENSE_GREATER)
		razor_flags |= RAZOR_PROPERTY_GREATER;

	if (flags & RPMSENSE_SCRIPT_PRE)
		razor_flags |= RAZOR_PROPERTY_PRE;
	if (flags & RPMSENSE_SCRIPT_POST)
		razor_flags |= RAZOR_PROPERTY_POST;
	if (flags & RPMSENSE_SCRIPT_PREUN)
		razor_flags |= RAZOR_PROPERTY_PREUN;
	if (flags & RPMSENSE_SCRIPT_POSTUN)
		razor_flags |= RAZOR_PROPERTY_POSTUN;

	return razor_flags;
}

static void
add_properties(struct razor_importer *importer,
	       uint32_t type_flags,
	       Header h, int32_t name_tag, int32_t version_tag, int32_t flags_tag)
{
	union rpm_entry names, versions, flags;
	int32_t i, type, count;

	headerGetEntry(h, name_tag, &type, &names.p, &count);
	headerGetEntry(h, version_tag, &type, &versions.p, &count);
	headerGetEntry(h, flags_tag, &type, &flags.p, &count);

	for (i = 0; i < count; i++)
		razor_importer_add_property(importer,
					    names.list[i],
					    rpm_to_razor_flags (flags.flags[i]) | type_flags,
					    versions.list[i]);
}

struct razor_set *
razor_set_create_from_rpmdb(void)
{
	struct razor_importer *importer;
	rpmdbMatchIterator iter;
	Header h;
	int32_t type, count, i;
	union rpm_entry name, epoch, version, release, arch;
	union rpm_entry summary, description, url, license;
	union rpm_entry basenames, dirnames, dirindexes;
	char filename[PATH_MAX], evr[128], buf[16];
	rpmdb db;
	int imported_count = 0;

	rpmReadConfigFiles(NULL, NULL);

	if (rpmdbOpen("", &db, O_RDONLY, 0644) != 0) {
		fprintf(stderr, "cannot open rpm database\n");
		exit(1);
	}

	importer = razor_importer_create();

	iter = rpmdbInitIterator(db, 0, NULL, 0);
	while (h = rpmdbNextIterator(iter), h != NULL) {
		headerGetEntry(h, RPMTAG_NAME, &type, &name.p, &count);
		headerGetEntry(h, RPMTAG_EPOCH, &type, &epoch.p, &count);
		headerGetEntry(h, RPMTAG_VERSION, &type, &version.p, &count);
		headerGetEntry(h, RPMTAG_RELEASE, &type, &release.p, &count);
		headerGetEntry(h, RPMTAG_ARCH, &type, &arch.p, &count);
		headerGetEntry(h, RPMTAG_SUMMARY, &type, &summary.p, &count);
		headerGetEntry(h, RPMTAG_DESCRIPTION, &type, &description.p,
			       &count);
		headerGetEntry(h, RPMTAG_URL, &type, &url.p, &count);
		headerGetEntry(h, RPMTAG_LICENSE, &type, &license.p, &count);

		if (epoch.flags != NULL) {
			snprintf(buf, sizeof buf, "%u", *epoch.flags);
			razor_build_evr(evr, sizeof evr,
					buf, version.string, release.string);
		} else {
			razor_build_evr(evr, sizeof evr,
					NULL, version.string, release.string);
		}

		razor_importer_begin_package(importer,
					     name.string, evr, arch.string);
		razor_importer_add_details(importer, summary.string,
					   description.string, url.string,
					   license.string);

		add_properties(importer, RAZOR_PROPERTY_REQUIRES, h,
			       RPMTAG_REQUIRENAME,
			       RPMTAG_REQUIREVERSION,
			       RPMTAG_REQUIREFLAGS);

		add_properties(importer, RAZOR_PROPERTY_PROVIDES, h,
			       RPMTAG_PROVIDENAME,
			       RPMTAG_PROVIDEVERSION,
			       RPMTAG_PROVIDEFLAGS);

		add_properties(importer, RAZOR_PROPERTY_OBSOLETES, h,
			       RPMTAG_OBSOLETENAME,
			       RPMTAG_OBSOLETEVERSION,
			       RPMTAG_OBSOLETEFLAGS);

		add_properties(importer, RAZOR_PROPERTY_CONFLICTS, h,
			       RPMTAG_CONFLICTNAME,
			       RPMTAG_CONFLICTVERSION,
			       RPMTAG_CONFLICTFLAGS);

		headerGetEntry(h, RPMTAG_BASENAMES, &type,
			       &basenames.p, &count);
		headerGetEntry(h, RPMTAG_DIRNAMES, &type,
			       &dirnames.p, &count);
		headerGetEntry(h, RPMTAG_DIRINDEXES, &type,
			       &dirindexes.p, &count);
		for (i = 0; i < count; i++) {
			snprintf(filename, sizeof filename, "%s%s",
				 dirnames.list[dirindexes.flags[i]],
				 basenames.list[i]);
			razor_importer_add_file(importer, filename);
		}

		razor_importer_finish_package(importer);

		printf("\rimporting %d", ++imported_count);
		fflush(stdout);
	}

	rpmdbClose(db);

	printf("\nsaving\n");
	return razor_importer_finish(importer);
}