src/import-rpmdb.c
author J. Ali Harlow <ali@juiblex.co.uk>
Sat Oct 04 18:12:58 2014 +0100 (2014-10-04)
changeset 454 56ff755c268c
parent 372 6e93e5485947
child 475 008c75a5e08d
permissions -rw-r--r--
Only export symbols starting with razor_ in dynamic library.

Apart from being good practice to avoid clashes with higher-level
libraries and the application, this also fixes an obscure bug: The
gnulib library is used both by librazor (the dynamic library) and
by razor (the executable). In doing so, we want to have two separate
copies of the library despite the code duplication this involves.
Without the explicit limit to export only razor_ symbols, the razor
executable under mingw64 was picking up the getopt_long function
from librazor and the optind variable from libgnu which meant that
it did not see optind changing. Hiding librazor's copy of getopt
causes the linker to find libgnu's copy and everything works.

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