librazor/rpm.c
author J. Ali Harlow <ali@juiblex.co.uk>
Fri Jun 08 18:02:49 2018 +0100 (2018-06-08)
changeset 501 850be6a6885c
parent 475 008c75a5e08d
permissions -rw-r--r--
Added tag 0.7 for changeset f98d77376544
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@475
     4
 * Copyright (C) 2009, 2011, 2012, 2014, 2016  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@327
    21
#include "config.h"
ali@327
    22
rhughes@241
    23
#include <stdio.h>
rhughes@241
    24
#include <stddef.h>
rhughes@241
    25
#include <stdlib.h>
rhughes@241
    26
#include <string.h>
rhughes@241
    27
#include <errno.h>
ali@330
    28
#if HAVE_SYS_WAIT_H
rhughes@241
    29
#include <sys/wait.h>
ali@330
    30
#endif
rhughes@241
    31
#include <fcntl.h>
rhughes@241
    32
#include <unistd.h>
ali@329
    33
#if MSWIN_API
ali@335
    34
#include <winsock2.h>	/* For ntohl() */
ali@329
    35
#else
rhughes@241
    36
#include <arpa/inet.h>
ali@329
    37
#endif
ali@325
    38
#include <limits.h>
rhughes@241
    39
#include <zlib.h>
richard@301
    40
#include <assert.h>
rhughes@241
    41
rhughes@241
    42
#include "razor.h"
rhughes@241
    43
#include "razor-internal.h"
rhughes@241
    44
ali@345
    45
#ifndef O_BINARY
ali@345
    46
#define O_BINARY	0
ali@345
    47
#endif
ali@345
    48
ali@460
    49
struct rpm_lead {
ali@460
    50
	unsigned char magic[4];
ali@460
    51
	unsigned char major, minor;
ali@460
    52
	short type;
ali@460
    53
	short archnum;
ali@460
    54
	char name[66];
ali@460
    55
	short osnum;
ali@460
    56
	short signature_type;
ali@460
    57
	char reserved[16];
ali@460
    58
};
ali@460
    59
ali@460
    60
#define	RPM_LEAD_SIZE sizeof(struct rpm_lead)
ali@460
    61
#define	RPM_LEAD_MAGIC "\xED\xAB\xEE\xDB"
rhughes@241
    62
rhughes@241
    63
enum {
rhughes@241
    64
    PIPE	=  1,	/*!< pipe/fifo */
rhughes@241
    65
    CDEV	=  2,	/*!< character device */
rhughes@241
    66
    XDIR	=  4,	/*!< directory */
rhughes@241
    67
    BDEV	=  6,	/*!< block device */
rhughes@241
    68
    REG		=  8,	/*!< regular file */
rhughes@241
    69
    LINK	= 10,	/*!< hard link */
rhughes@241
    70
    SOCK	= 12	/*!< socket */
rhughes@241
    71
};
rhughes@241
    72
rhughes@241
    73
enum {
krh@247
    74
    RPMSENSE_LESS		= 1 << 1,
krh@247
    75
    RPMSENSE_GREATER		= 1 << 2,
krh@247
    76
    RPMSENSE_EQUAL		= 1 << 3,
krh@247
    77
    RPMSENSE_PREREQ		= 1 << 6,
krh@247
    78
    RPMSENSE_SCRIPT_PRE		= 1 << 9,
krh@247
    79
    RPMSENSE_SCRIPT_POST	= 1 << 10,
krh@247
    80
    RPMSENSE_SCRIPT_PREUN	= 1 << 11,
krh@247
    81
    RPMSENSE_SCRIPT_POSTUN	= 1 << 12,
rhughes@241
    82
};
rhughes@241
    83
rhughes@241
    84
enum {
rhughes@241
    85
    RPMTAG_NAME  		= 1000,	/* s */
rhughes@241
    86
    RPMTAG_VERSION		= 1001,	/* s */
rhughes@241
    87
    RPMTAG_RELEASE		= 1002,	/* s */
rhughes@241
    88
    RPMTAG_EPOCH   		= 1003,	/* i */
rhughes@241
    89
    RPMTAG_SUMMARY		= 1004,	/* s{} */
rhughes@241
    90
    RPMTAG_DESCRIPTION		= 1005,	/* s{} */
rhughes@241
    91
    RPMTAG_BUILDTIME		= 1006,	/* i */
rhughes@241
    92
    RPMTAG_BUILDHOST		= 1007,	/* s */
rhughes@241
    93
    RPMTAG_INSTALLTIME		= 1008,	/* i */
rhughes@241
    94
    RPMTAG_SIZE			= 1009,	/* i */
rhughes@241
    95
    RPMTAG_DISTRIBUTION		= 1010,	/* s */
rhughes@241
    96
    RPMTAG_VENDOR		= 1011,	/* s */
rhughes@241
    97
    RPMTAG_GIF			= 1012,	/* x */
rhughes@241
    98
    RPMTAG_XPM			= 1013,	/* x */
rhughes@241
    99
    RPMTAG_LICENSE		= 1014,	/* s */
rhughes@241
   100
    RPMTAG_PACKAGER		= 1015,	/* s */
rhughes@241
   101
    RPMTAG_GROUP		= 1016,	/* s{} */
rhughes@241
   102
    RPMTAG_CHANGELOG		= 1017, /*!< s[] internal */
rhughes@241
   103
    RPMTAG_SOURCE		= 1018,	/* s[] */
rhughes@241
   104
    RPMTAG_PATCH		= 1019,	/* s[] */
rhughes@241
   105
    RPMTAG_URL			= 1020,	/* s */
rhughes@241
   106
    RPMTAG_OS			= 1021,	/* s legacy used int */
rhughes@241
   107
    RPMTAG_ARCH			= 1022,	/* s legacy used int */
rhughes@241
   108
    RPMTAG_PREIN		= 1023,	/* s */
rhughes@241
   109
    RPMTAG_POSTIN		= 1024,	/* s */
rhughes@241
   110
    RPMTAG_PREUN		= 1025,	/* s */
rhughes@241
   111
    RPMTAG_POSTUN		= 1026,	/* s */
rhughes@241
   112
    RPMTAG_OLDFILENAMES		= 1027, /* s[] obsolete */
rhughes@241
   113
    RPMTAG_FILESIZES		= 1028,	/* i */
rhughes@241
   114
    RPMTAG_FILESTATES		= 1029, /* c */
rhughes@241
   115
    RPMTAG_FILEMODES		= 1030,	/* h */
rhughes@241
   116
    RPMTAG_FILEUIDS		= 1031, /*!< internal */
rhughes@241
   117
    RPMTAG_FILEGIDS		= 1032, /*!< internal */
rhughes@241
   118
    RPMTAG_FILERDEVS		= 1033,	/* h */
rhughes@241
   119
    RPMTAG_FILEMTIMES		= 1034, /* i */
rhughes@241
   120
    RPMTAG_FILEMD5S		= 1035,	/* s[] */
rhughes@241
   121
    RPMTAG_FILELINKTOS		= 1036,	/* s[] */
rhughes@241
   122
    RPMTAG_FILEFLAGS		= 1037,	/* i */
rhughes@241
   123
    RPMTAG_ROOT			= 1038, /*!< internal - obsolete */
rhughes@241
   124
    RPMTAG_FILEUSERNAME		= 1039,	/* s[] */
rhughes@241
   125
    RPMTAG_FILEGROUPNAME	= 1040,	/* s[] */
rhughes@241
   126
    RPMTAG_EXCLUDE		= 1041, /*!< internal - obsolete */
rhughes@241
   127
    RPMTAG_EXCLUSIVE		= 1042, /*!< internal - obsolete */
rhughes@241
   128
    RPMTAG_ICON			= 1043,
rhughes@241
   129
    RPMTAG_SOURCERPM		= 1044,	/* s */
rhughes@241
   130
    RPMTAG_FILEVERIFYFLAGS	= 1045,	/* i */
rhughes@241
   131
    RPMTAG_ARCHIVESIZE		= 1046,	/* i */
rhughes@241
   132
    RPMTAG_PROVIDENAME		= 1047,	/* s[] */
rhughes@241
   133
    RPMTAG_REQUIREFLAGS		= 1048,	/* i */
rhughes@241
   134
    RPMTAG_REQUIRENAME		= 1049,	/* s[] */
rhughes@241
   135
    RPMTAG_REQUIREVERSION	= 1050,	/* s[] */
rhughes@241
   136
    RPMTAG_NOSOURCE		= 1051, /*!< internal */
rhughes@241
   137
    RPMTAG_NOPATCH		= 1052, /*!< internal */
rhughes@241
   138
    RPMTAG_CONFLICTFLAGS	= 1053, /* i */
rhughes@241
   139
    RPMTAG_CONFLICTNAME		= 1054,	/* s[] */
rhughes@241
   140
    RPMTAG_CONFLICTVERSION	= 1055,	/* s[] */
rhughes@241
   141
    RPMTAG_DEFAULTPREFIX	= 1056, /*!< internal - deprecated */
rhughes@241
   142
    RPMTAG_BUILDROOT		= 1057, /*!< internal */
rhughes@241
   143
    RPMTAG_INSTALLPREFIX	= 1058, /*!< internal - deprecated */
rhughes@241
   144
    RPMTAG_EXCLUDEARCH		= 1059,
rhughes@241
   145
    RPMTAG_EXCLUDEOS		= 1060,
rhughes@241
   146
    RPMTAG_EXCLUSIVEARCH	= 1061,
rhughes@241
   147
    RPMTAG_EXCLUSIVEOS		= 1062,
rhughes@241
   148
    RPMTAG_AUTOREQPROV		= 1063, /*!< internal */
rhughes@241
   149
    RPMTAG_RPMVERSION		= 1064,	/* s */
rhughes@241
   150
    RPMTAG_TRIGGERSCRIPTS	= 1065,	/* s[] */
rhughes@241
   151
    RPMTAG_TRIGGERNAME		= 1066,	/* s[] */
rhughes@241
   152
    RPMTAG_TRIGGERVERSION	= 1067,	/* s[] */
rhughes@241
   153
    RPMTAG_TRIGGERFLAGS		= 1068,	/* i */
rhughes@241
   154
    RPMTAG_TRIGGERINDEX		= 1069,	/* i */
rhughes@241
   155
    RPMTAG_VERIFYSCRIPT		= 1079,	/* s */
rhughes@241
   156
    RPMTAG_CHANGELOGTIME	= 1080,	/* i */
rhughes@241
   157
    RPMTAG_CHANGELOGNAME	= 1081,	/* s[] */
rhughes@241
   158
    RPMTAG_CHANGELOGTEXT	= 1082,	/* s[] */
rhughes@241
   159
    RPMTAG_BROKENMD5		= 1083, /*!< internal - obsolete */
rhughes@241
   160
    RPMTAG_PREREQ		= 1084, /*!< internal */
rhughes@241
   161
    RPMTAG_PREINPROG		= 1085,	/* s */
rhughes@241
   162
    RPMTAG_POSTINPROG		= 1086,	/* s */
rhughes@241
   163
    RPMTAG_PREUNPROG		= 1087,	/* s */
rhughes@241
   164
    RPMTAG_POSTUNPROG		= 1088,	/* s */
rhughes@241
   165
    RPMTAG_BUILDARCHS		= 1089,
rhughes@241
   166
    RPMTAG_OBSOLETENAME		= 1090,	/* s[] */
rhughes@241
   167
    RPMTAG_VERIFYSCRIPTPROG	= 1091,	/* s */
rhughes@241
   168
    RPMTAG_TRIGGERSCRIPTPROG	= 1092,	/* s */
rhughes@241
   169
    RPMTAG_DOCDIR		= 1093, /*!< internal */
rhughes@241
   170
    RPMTAG_COOKIE		= 1094,	/* s */
rhughes@241
   171
    RPMTAG_FILEDEVICES		= 1095,	/* i */
rhughes@241
   172
    RPMTAG_FILEINODES		= 1096,	/* i */
rhughes@241
   173
    RPMTAG_FILELANGS		= 1097,	/* s[] */
rhughes@241
   174
    RPMTAG_PREFIXES		= 1098,	/* s[] */
rhughes@241
   175
    RPMTAG_INSTPREFIXES		= 1099,	/* s[] */
rhughes@241
   176
    RPMTAG_TRIGGERIN		= 1100, /*!< internal */
rhughes@241
   177
    RPMTAG_TRIGGERUN		= 1101, /*!< internal */
rhughes@241
   178
    RPMTAG_TRIGGERPOSTUN	= 1102, /*!< internal */
rhughes@241
   179
    RPMTAG_AUTOREQ		= 1103, /*!< internal */
rhughes@241
   180
    RPMTAG_AUTOPROV		= 1104, /*!< internal */
rhughes@241
   181
    RPMTAG_CAPABILITY		= 1105, /*!< internal - obsolete */
rhughes@241
   182
    RPMTAG_SOURCEPACKAGE	= 1106, /*!< i src.rpm header marker */
rhughes@241
   183
    RPMTAG_OLDORIGFILENAMES	= 1107, /*!< internal - obsolete */
rhughes@241
   184
    RPMTAG_BUILDPREREQ		= 1108, /*!< internal */
rhughes@241
   185
    RPMTAG_BUILDREQUIRES	= 1109, /*!< internal */
rhughes@241
   186
    RPMTAG_BUILDCONFLICTS	= 1110, /*!< internal */
rhughes@241
   187
    RPMTAG_BUILDMACROS		= 1111, /*!< internal - unused */
rhughes@241
   188
    RPMTAG_PROVIDEFLAGS		= 1112,	/* i */
rhughes@241
   189
    RPMTAG_PROVIDEVERSION	= 1113,	/* s[] */
rhughes@241
   190
    RPMTAG_OBSOLETEFLAGS	= 1114,	/* i */
rhughes@241
   191
    RPMTAG_OBSOLETEVERSION	= 1115,	/* s[] */
rhughes@241
   192
    RPMTAG_DIRINDEXES		= 1116,	/* i */
rhughes@241
   193
    RPMTAG_BASENAMES		= 1117,	/* s[] */
rhughes@241
   194
    RPMTAG_DIRNAMES		= 1118,	/* s[] */
rhughes@241
   195
    RPMTAG_ORIGDIRINDEXES	= 1119, /*!< internal */
rhughes@241
   196
    RPMTAG_ORIGBASENAMES	= 1120, /*!< internal */
rhughes@241
   197
    RPMTAG_ORIGDIRNAMES		= 1121, /*!< internal */
rhughes@241
   198
    RPMTAG_OPTFLAGS		= 1122,	/* s */
rhughes@241
   199
    RPMTAG_DISTURL		= 1123,	/* s */
rhughes@241
   200
    RPMTAG_PAYLOADFORMAT	= 1124,	/* s */
rhughes@241
   201
    RPMTAG_PAYLOADCOMPRESSOR	= 1125,	/* s */
rhughes@241
   202
    RPMTAG_PAYLOADFLAGS		= 1126,	/* s */
rhughes@241
   203
    RPMTAG_INSTALLCOLOR		= 1127, /*!< i transaction color when installed */
rhughes@241
   204
    RPMTAG_INSTALLTID		= 1128,	/* i */
rhughes@241
   205
    RPMTAG_REMOVETID		= 1129,	/* i */
rhughes@241
   206
    RPMTAG_SHA1RHN		= 1130, /*!< internal - obsolete */
rhughes@241
   207
    RPMTAG_RHNPLATFORM		= 1131,	/* s */
rhughes@241
   208
    RPMTAG_PLATFORM		= 1132,	/* s */
rhughes@241
   209
    RPMTAG_PATCHESNAME		= 1133, /*!< placeholder (SuSE) */
rhughes@241
   210
    RPMTAG_PATCHESFLAGS		= 1134, /*!< placeholder (SuSE) */
rhughes@241
   211
    RPMTAG_PATCHESVERSION	= 1135, /*!< placeholder (SuSE) */
rhughes@241
   212
    RPMTAG_CACHECTIME		= 1136,	/* i */
rhughes@241
   213
    RPMTAG_CACHEPKGPATH		= 1137,	/* s */
rhughes@241
   214
    RPMTAG_CACHEPKGSIZE		= 1138,	/* i */
rhughes@241
   215
    RPMTAG_CACHEPKGMTIME	= 1139,	/* i */
rhughes@241
   216
    RPMTAG_FILECOLORS		= 1140,	/* i */
rhughes@241
   217
    RPMTAG_FILECLASS		= 1141,	/* i */
rhughes@241
   218
    RPMTAG_CLASSDICT		= 1142,	/* s[] */
rhughes@241
   219
    RPMTAG_FILEDEPENDSX		= 1143,	/* i */
rhughes@241
   220
    RPMTAG_FILEDEPENDSN		= 1144,	/* i */
rhughes@241
   221
    RPMTAG_DEPENDSDICT		= 1145,	/* i */
rhughes@241
   222
    RPMTAG_SOURCEPKGID		= 1146,	/* x */
rhughes@241
   223
    RPMTAG_FILECONTEXTS		= 1147,	/* s[] */
rhughes@241
   224
    RPMTAG_FSCONTEXTS		= 1148,	/*!< s[] extension */
rhughes@241
   225
    RPMTAG_RECONTEXTS		= 1149,	/*!< s[] extension */
rhughes@241
   226
    RPMTAG_POLICIES		= 1150,	/*!< s[] selinux *.te policy file. */
rhughes@241
   227
    RPMTAG_PRETRANS		= 1151,	/* s */
rhughes@241
   228
    RPMTAG_POSTTRANS		= 1152,	/* s */
rhughes@241
   229
    RPMTAG_PRETRANSPROG		= 1153,	/* s */
rhughes@241
   230
    RPMTAG_POSTTRANSPROG	= 1154,	/* s */
rhughes@241
   231
    RPMTAG_DISTTAG		= 1155,	/* s */
rhughes@241
   232
    RPMTAG_SUGGESTSNAME		= 1156,	/* s[] extension placeholder */
rhughes@241
   233
    RPMTAG_SUGGESTSVERSION	= 1157,	/* s[] extension placeholder */
rhughes@241
   234
    RPMTAG_SUGGESTSFLAGS	= 1158,	/* i   extension placeholder */
rhughes@241
   235
    RPMTAG_ENHANCESNAME		= 1159,	/* s[] extension placeholder */
rhughes@241
   236
    RPMTAG_ENHANCESVERSION	= 1160,	/* s[] extension placeholder */
rhughes@241
   237
    RPMTAG_ENHANCESFLAGS	= 1161,	/* i   extension placeholder */
rhughes@241
   238
    RPMTAG_PRIORITY		= 1162, /* i   extension placeholder */
rhughes@241
   239
    RPMTAG_CVSID		= 1163, /* s */
rhughes@241
   240
    RPMTAG_TRIGGERPREIN		= 1171, /*!< internal */
rhughes@241
   241
};
rhughes@241
   242
rhughes@241
   243
struct rpm_header {
rhughes@241
   244
	unsigned char magic[4];
rhughes@241
   245
	unsigned char reserved[4];
rhughes@241
   246
	int nindex;
rhughes@241
   247
	int hsize;
rhughes@241
   248
};
rhughes@241
   249
rhughes@241
   250
struct rpm_header_index {
rhughes@241
   251
	int tag;
rhughes@241
   252
	int type;
rhughes@241
   253
	int offset;
rhughes@241
   254
	int count;
rhughes@241
   255
};
rhughes@241
   256
rhughes@241
   257
struct razor_rpm {
rhughes@241
   258
	struct rpm_header *signature;
rhughes@241
   259
	struct rpm_header *header;
rhughes@241
   260
	const char **dirs;
ali@351
   261
	unsigned int n_prefixes;
ali@351
   262
	const char **prefixes;
rhughes@241
   263
	const char *pool;
rhughes@241
   264
	void *map;
rhughes@241
   265
	size_t size;
rhughes@241
   266
	void *payload;
ali@351
   267
	struct razor_relocations *relocations;
ali@369
   268
	char *evr;
rhughes@241
   269
};
rhughes@241
   270
ali@351
   271
enum razor_relocation_flags {
ali@351
   272
	RAZOR_RELOCATION_ACTIVE		= 1 << 0,
ali@351
   273
};
ali@351
   274
ali@351
   275
struct razor_relocation {
ali@351
   276
	enum razor_relocation_flags flags;
ali@351
   277
	size_t oldlen;
ali@351
   278
	size_t newlen;
ali@351
   279
	char *oldpath;
ali@351
   280
	char *newpath;
ali@351
   281
};
ali@351
   282
ali@351
   283
struct razor_relocations {
ali@351
   284
	/* Ordered such that if oldpath 1 starts with oldpath 2, then
ali@351
   285
	 * oldpath 1 is listed first (ie., /usr/bin comes before /usr)
ali@351
   286
	 * and terminated with a NULL oldpath.
ali@351
   287
	 */
ali@351
   288
	struct razor_relocation *relocations;
ali@351
   289
	int n_relocations;
ali@351
   290
	char *path;
ali@351
   291
};
ali@351
   292
ali@351
   293
RAZOR_EXPORT struct razor_relocations *razor_relocations_create(void)
ali@351
   294
{
ali@351
   295
	return calloc(1, sizeof(struct razor_relocations));
ali@351
   296
}
ali@351
   297
ali@351
   298
RAZOR_EXPORT void razor_relocations_add(struct razor_relocations *rr,
ali@351
   299
			   const char *oldpath, const char *newpath)
ali@351
   300
{
ali@351
   301
	int i, found = 0;
ali@351
   302
	size_t len;
ali@351
   303
ali@351
   304
	if (newpath && !strcmp(oldpath, newpath))
ali@351
   305
		newpath = NULL;
ali@351
   306
ali@351
   307
	for (i = 0; i < rr->n_relocations; i++) {
ali@351
   308
		len = rr->relocations[i].oldlen;
ali@351
   309
		if (!strncmp(rr->relocations[i].oldpath, oldpath, len)) {
ali@351
   310
			found = !strcmp(rr->relocations[i].oldpath, oldpath);
ali@351
   311
			break;
ali@351
   312
		}
ali@351
   313
	}
ali@351
   314
ali@351
   315
	if (!newpath) {
ali@351
   316
		if (found) {
ali@351
   317
			free(rr->relocations[i].oldpath);
ali@351
   318
			free(rr->relocations[i].newpath);
ali@351
   319
			do {
ali@351
   320
				rr->relocations[i] = rr->relocations[i + 1];
ali@351
   321
			} while (rr->relocations[++i].oldpath);
ali@351
   322
		}
ali@351
   323
		return;
ali@351
   324
	}
ali@351
   325
ali@351
   326
	if (found) {
ali@351
   327
		free(rr->relocations[i].newpath);
ali@351
   328
		rr->relocations[i].newpath = strdup(newpath);
ali@351
   329
		rr->relocations[i].newlen = strlen(newpath);
ali@351
   330
		return;
ali@351
   331
	}
ali@351
   332
ali@351
   333
	if (!rr->n_relocations++)
ali@351
   334
		rr->relocations = calloc(1, sizeof *rr->relocations);
ali@351
   335
	else {
ali@351
   336
		rr->relocations = realloc(rr->relocations,
ali@351
   337
					  rr->n_relocations * sizeof *rr->relocations);
ali@351
   338
		memmove(rr->relocations + i + 1, rr->relocations + i,
ali@351
   339
			(rr->n_relocations - i - 1) * sizeof *rr->relocations);
ali@351
   340
	}
ali@351
   341
ali@351
   342
	rr->relocations[i].flags = 0;
ali@351
   343
	rr->relocations[i].oldpath = strdup(oldpath);
ali@351
   344
	rr->relocations[i].newpath = strdup(newpath);
ali@351
   345
	rr->relocations[i].oldlen = strlen(oldpath);
ali@351
   346
	rr->relocations[i].newlen = strlen(newpath);
ali@351
   347
}
ali@351
   348
ali@351
   349
RAZOR_EXPORT void
ali@351
   350
razor_relocations_set_rpm(struct razor_relocations *rr, struct razor_rpm *rpm)
ali@351
   351
{
ali@351
   352
	int i, j;
ali@351
   353
ali@351
   354
	for (i = 0; i < rr->n_relocations; i++) {
ali@351
   355
		rr->relocations[i].flags &= ~RAZOR_RELOCATION_ACTIVE;
ali@351
   356
		for (j = 0; j < rpm->n_prefixes; j++)
ali@351
   357
			if (!strcmp(rpm->prefixes[j],
ali@351
   358
				    rr->relocations[i].oldpath)) {
ali@351
   359
				rr->relocations[i].flags |= RAZOR_RELOCATION_ACTIVE;
ali@351
   360
				break;
ali@351
   361
			}
ali@351
   362
	}
ali@351
   363
}
ali@351
   364
ali@351
   365
RAZOR_EXPORT const char *
ali@351
   366
razor_relocations_apply(struct razor_relocations *rr, const char *path)
ali@351
   367
{
ali@351
   368
	int i;
ali@351
   369
	size_t len;
ali@351
   370
ali@351
   371
	for (i = 0; i < rr->n_relocations; i++)
ali@351
   372
		if (rr->relocations[i].flags & RAZOR_RELOCATION_ACTIVE &&
ali@351
   373
		    !strncmp(path, rr->relocations[i].oldpath,
ali@351
   374
			     rr->relocations[i].oldlen))
ali@351
   375
			break;
ali@351
   376
ali@351
   377
	if (i < rr->n_relocations) {
ali@351
   378
		free(rr->path);
ali@351
   379
		len = strlen(path + rr->relocations[i].oldlen) +
ali@351
   380
		      rr->relocations[i].newlen;
ali@351
   381
		rr->path = malloc(len + 1);
ali@351
   382
		memcpy(rr->path, rr->relocations[i].newpath,
ali@351
   383
		       rr->relocations[i].newlen);
ali@351
   384
		strcpy(rr->path + rr->relocations[i].newlen,
ali@351
   385
		       path + rr->relocations[i].oldlen);
ali@351
   386
		return rr->path;
ali@351
   387
	} else
ali@351
   388
		return path;
ali@351
   389
}
ali@351
   390
ali@351
   391
RAZOR_EXPORT void razor_relocations_destroy(struct razor_relocations *rr)
ali@351
   392
{
ali@458
   393
	int i;
ali@458
   394
ali@458
   395
	for (i = 0; i < rr->n_relocations; i++) {
ali@458
   396
	    free(rr->relocations[i].oldpath);
ali@458
   397
	    free(rr->relocations[i].newpath);
ali@458
   398
	}
ali@351
   399
	free(rr->path);
ali@351
   400
	free(rr->relocations);
ali@351
   401
	free(rr);
ali@351
   402
}
ali@351
   403
rhughes@241
   404
static struct rpm_header_index *
rhughes@241
   405
razor_rpm_get_header(struct razor_rpm *rpm, unsigned int tag)
rhughes@241
   406
{
rhughes@241
   407
	struct rpm_header_index *index, *end;
rhughes@241
   408
rhughes@241
   409
	index = (struct rpm_header_index *) (rpm->header + 1);
rhughes@241
   410
	end = index + ntohl(rpm->header->nindex);
rhughes@241
   411
	while (index < end) {
rhughes@241
   412
		if (ntohl(index->tag) == tag)
rhughes@241
   413
			return index;
rhughes@241
   414
		index++;
rhughes@241
   415
	}
rhughes@241
   416
rhughes@241
   417
	return NULL;
rhughes@241
   418
}
rhughes@241
   419
rhughes@241
   420
static const void *
rhughes@241
   421
razor_rpm_get_indirect(struct razor_rpm *rpm,
rhughes@241
   422
		       unsigned int tag, unsigned int *count)
rhughes@241
   423
{
rhughes@241
   424
	struct rpm_header_index *index;
rhughes@241
   425
rhughes@241
   426
	index = razor_rpm_get_header(rpm, tag);
rhughes@241
   427
	if (index != NULL) {
rhughes@241
   428
		if (count)
rhughes@241
   429
			*count = ntohl(index->count);
rhughes@241
   430
rhughes@241
   431
		return rpm->pool + ntohl(index->offset);
rhughes@241
   432
	}
rhughes@241
   433
rhughes@241
   434
	return NULL;
rhughes@241
   435
}
rhughes@241
   436
krh@247
   437
static uint32_t
rhughes@241
   438
rpm_to_razor_flags(uint32_t flags)
rhughes@241
   439
{
krh@247
   440
	uint32_t razor_flags;
rhughes@241
   441
krh@247
   442
	razor_flags = 0;
krh@247
   443
	if (flags & RPMSENSE_LESS)
krh@247
   444
		razor_flags |= RAZOR_PROPERTY_LESS;
krh@247
   445
	if (flags & RPMSENSE_EQUAL)
krh@247
   446
		razor_flags |= RAZOR_PROPERTY_EQUAL;
krh@247
   447
	if (flags & RPMSENSE_GREATER)
krh@247
   448
		razor_flags |= RAZOR_PROPERTY_GREATER;
krh@247
   449
krh@247
   450
	if (flags & RPMSENSE_SCRIPT_PRE)
krh@247
   451
		razor_flags |= RAZOR_PROPERTY_PRE;
krh@247
   452
	if (flags & RPMSENSE_SCRIPT_POST)
krh@247
   453
		razor_flags |= RAZOR_PROPERTY_POST;
krh@247
   454
	if (flags & RPMSENSE_SCRIPT_PREUN)
krh@247
   455
		razor_flags |= RAZOR_PROPERTY_PREUN;
krh@247
   456
	if (flags & RPMSENSE_SCRIPT_POSTUN)
krh@247
   457
		razor_flags |= RAZOR_PROPERTY_POSTUN;
krh@247
   458
	
krh@247
   459
	return razor_flags;
rhughes@241
   460
}
rhughes@241
   461
rhughes@241
   462
static void
krh@247
   463
import_properties(struct razor_importer *importer, uint32_t type,
rhughes@241
   464
		  struct razor_rpm *rpm,
rhughes@241
   465
		  int name_tag, int version_tag, int flags_tag)
rhughes@241
   466
{
rhughes@241
   467
	const char *name, *version;
rhughes@241
   468
	const uint32_t *flags;
rhughes@241
   469
	uint32_t f;
rhughes@241
   470
	unsigned int i, count;
rhughes@241
   471
rhughes@241
   472
	name = razor_rpm_get_indirect(rpm, name_tag, &count);
rhughes@241
   473
	if (name == NULL)
rhughes@241
   474
		return;
rhughes@241
   475
rhughes@241
   476
	flags = razor_rpm_get_indirect(rpm, flags_tag, &count);
rhughes@241
   477
rhughes@241
   478
	version = razor_rpm_get_indirect(rpm, version_tag, &count);
rhughes@241
   479
	for (i = 0; i < count; i++) {
rhughes@241
   480
		f = rpm_to_razor_flags(ntohl(flags[i]));
krh@247
   481
		razor_importer_add_property(importer, name, f | type, version);
rhughes@241
   482
		name += strlen(name) + 1;
rhughes@241
   483
		version += strlen(version) + 1;
rhughes@241
   484
	}
rhughes@241
   485
}
rhughes@241
   486
rhughes@241
   487
static void
rhughes@241
   488
import_files(struct razor_importer *importer, struct razor_rpm *rpm)
rhughes@241
   489
{
rhughes@241
   490
	const char *name;
rhughes@241
   491
	const uint32_t *index;
rhughes@241
   492
	unsigned int i, count;
rhughes@241
   493
	char buffer[256];
rhughes@241
   494
krh@246
   495
	if (rpm->dirs == NULL)
krh@246
   496
		return;
krh@246
   497
rhughes@241
   498
	/* assert: count is the same for all arrays */
rhughes@241
   499
	index = razor_rpm_get_indirect(rpm, RPMTAG_DIRINDEXES, &count);
rhughes@241
   500
	name = razor_rpm_get_indirect(rpm, RPMTAG_BASENAMES, &count);
rhughes@241
   501
	for (i = 0; i < count; i++) {
rhughes@241
   502
		snprintf(buffer, sizeof buffer,
rhughes@241
   503
			 "%s%s", rpm->dirs[ntohl(*index)], name);
rhughes@241
   504
		razor_importer_add_file(importer, buffer);
rhughes@241
   505
		name += strlen(name) + 1;
rhughes@241
   506
		index++;
rhughes@241
   507
	}
rhughes@241
   508
}
rhughes@241
   509
ali@369
   510
static void
ali@369
   511
razor_rpm_build_evr(struct razor_rpm *rpm)
ali@369
   512
{
ali@369
   513
	const char *version, *release;
ali@369
   514
	const uint32_t *epoch;
ali@369
   515
	char evr[128], buf[16];
ali@369
   516
ali@369
   517
	epoch = razor_rpm_get_indirect(rpm, RPMTAG_EPOCH, NULL);
ali@369
   518
	version = razor_rpm_get_indirect(rpm, RPMTAG_VERSION, NULL);
ali@369
   519
	release = razor_rpm_get_indirect(rpm, RPMTAG_RELEASE, NULL);
ali@369
   520
	if (epoch)
ali@369
   521
		snprintf(buf, sizeof buf, "%lu", (unsigned long)ntohl(*epoch));
ali@369
   522
	razor_build_evr(evr, sizeof evr, epoch ? buf : NULL, version, release);
ali@369
   523
	rpm->evr = strdup(evr);
ali@369
   524
}
ali@369
   525
ali@369
   526
static const char *
ali@372
   527
razor_rpm_get_details_string(struct razor_rpm *rpm, enum razor_detail_type type)
ali@369
   528
{
ali@369
   529
	switch(type) {
ali@369
   530
	case RAZOR_DETAIL_NAME:
ali@369
   531
		return razor_rpm_get_indirect(rpm, RPMTAG_NAME, NULL);
ali@369
   532
ali@369
   533
	case RAZOR_DETAIL_VERSION:
ali@369
   534
		if (!rpm->evr)
ali@369
   535
			razor_rpm_build_evr(rpm);
ali@369
   536
		return rpm->evr;
ali@369
   537
ali@369
   538
	case RAZOR_DETAIL_ARCH:
ali@369
   539
		return razor_rpm_get_indirect(rpm, RPMTAG_ARCH, NULL);
ali@369
   540
ali@369
   541
	case RAZOR_DETAIL_SUMMARY:
ali@369
   542
		return razor_rpm_get_indirect(rpm, RPMTAG_SUMMARY, NULL);
ali@369
   543
ali@369
   544
	case RAZOR_DETAIL_DESCRIPTION:
ali@369
   545
		return razor_rpm_get_indirect(rpm, RPMTAG_DESCRIPTION, NULL);
ali@369
   546
ali@369
   547
	case RAZOR_DETAIL_URL:
ali@369
   548
		return razor_rpm_get_indirect(rpm, RPMTAG_URL, NULL);
ali@369
   549
ali@369
   550
	case RAZOR_DETAIL_LICENSE:
ali@369
   551
		return razor_rpm_get_indirect(rpm, RPMTAG_LICENSE, NULL);
ali@369
   552
ali@369
   553
	case RAZOR_DETAIL_PREUNPROG:
ali@369
   554
		return razor_rpm_get_indirect(rpm, RPMTAG_PREUNPROG, NULL);
ali@369
   555
ali@369
   556
	case RAZOR_DETAIL_PREUN:
ali@369
   557
		return razor_rpm_get_indirect(rpm, RPMTAG_PREUN, NULL);
ali@369
   558
ali@369
   559
	case RAZOR_DETAIL_POSTUNPROG:
ali@369
   560
		return razor_rpm_get_indirect(rpm, RPMTAG_POSTUNPROG, NULL);
ali@369
   561
ali@369
   562
	case RAZOR_DETAIL_POSTUN:
ali@369
   563
		return razor_rpm_get_indirect(rpm, RPMTAG_POSTUN, NULL);
ali@369
   564
ali@369
   565
	default:
ali@369
   566
		fprintf(stderr, "type %u not found\n", type);
ali@369
   567
		return NULL;
ali@369
   568
	}
ali@369
   569
}
ali@369
   570
ali@372
   571
static const char *const *
ali@372
   572
razor_rpm_get_details_array(struct razor_rpm *rpm, enum razor_detail_type type)
ali@372
   573
{
ali@372
   574
	switch(type) {
ali@372
   575
	case RAZOR_DETAIL_PREFIXES:
ali@372
   576
		return rpm->prefixes;
ali@372
   577
ali@372
   578
	default:
ali@372
   579
		/* Impossible */
ali@372
   580
		fprintf(stderr, "type %u not found\n", type);
ali@372
   581
		return NULL;
ali@372
   582
	}
ali@372
   583
}
ali@372
   584
ali@369
   585
void
ali@369
   586
razor_rpm_get_details_varg(struct razor_rpm *rpm, va_list args)
ali@369
   587
{
ali@369
   588
	int i;
ali@369
   589
	enum razor_detail_type type;
ali@372
   590
	const char **string;
ali@372
   591
	const char *const **array;
ali@369
   592
ali@369
   593
	for (i = 0;; i += 2) {
ali@369
   594
		type = va_arg(args, enum razor_detail_type);
ali@369
   595
		if (type == RAZOR_DETAIL_LAST)
ali@369
   596
			break;
ali@372
   597
		if (type == RAZOR_DETAIL_PREFIXES) {
ali@372
   598
			array = va_arg(args, const char *const **);
ali@372
   599
			*array = razor_rpm_get_details_array(rpm, type);
ali@372
   600
		} else {
ali@372
   601
			string = va_arg(args, const char **);
ali@372
   602
			*string = razor_rpm_get_details_string(rpm, type);
ali@372
   603
		}
ali@369
   604
        }
ali@369
   605
}
ali@369
   606
ali@369
   607
RAZOR_EXPORT void
ali@369
   608
razor_rpm_get_details(struct razor_rpm *rpm, ...)
ali@369
   609
{
ali@369
   610
	va_list args;
ali@369
   611
ali@369
   612
	assert(rpm != NULL);
ali@369
   613
ali@369
   614
	va_start(args, rpm);
ali@369
   615
	razor_rpm_get_details_varg(rpm, args);
ali@369
   616
	va_end(args);
ali@369
   617
}
ali@369
   618
krh@269
   619
RAZOR_EXPORT struct razor_rpm *
ali@475
   620
razor_rpm_open(const char *uri, struct razor_error **error)
rhughes@241
   621
{
ali@460
   622
	struct rpm_lead *lead;
rhughes@241
   623
	struct razor_rpm *rpm;
rhughes@241
   624
	struct rpm_header_index *base, *index;
rhughes@241
   625
	unsigned int count, i, nindex, hsize;
ali@351
   626
	const char *name, *prefix;
rhughes@241
   627
ali@475
   628
	assert (uri != NULL);
richard@301
   629
ali@403
   630
	rpm = zalloc(sizeof *rpm);
ali@403
   631
	if (rpm == NULL) {
ali@447
   632
		razor_set_error(error, RAZOR_POSIX_ERROR, ENOMEM, NULL,
ali@447
   633
				"Not enough memory");
krh@246
   634
		return NULL;
ali@403
   635
	}
rhughes@241
   636
	memset(rpm, 0, sizeof *rpm);
rhughes@241
   637
ali@475
   638
	rpm->map = razor_uri_get_contents(uri, &rpm->size, 0, error);
ali@322
   639
	if (!rpm->map) {
ali@403
   640
		free(rpm);
rhughes@241
   641
		return NULL;
rhughes@241
   642
	}
rhughes@241
   643
ali@460
   644
	lead = rpm->map;
ali@460
   645
	if (rpm->size < RPM_LEAD_SIZE ||
ali@475
   646
	    strncmp((char *)lead->magic,RPM_LEAD_MAGIC,4) || lead->major != 3) {
ali@460
   647
		razor_rpm_close(rpm);
ali@460
   648
		razor_set_error(error, RAZOR_GENERAL_ERROR,
ali@460
   649
				RAZOR_GENERAL_ERROR_RPM_UNSUPPORTED,
ali@475
   650
				uri, "Not a recognized RPM format file");
ali@460
   651
		return NULL;
ali@460
   652
	}
ali@460
   653
rhughes@241
   654
	rpm->signature = rpm->map + RPM_LEAD_SIZE;
rhughes@241
   655
	nindex = ntohl(rpm->signature->nindex);
rhughes@241
   656
	hsize = ntohl(rpm->signature->hsize);
rhughes@241
   657
	rpm->header = (void *) (rpm->signature + 1) +
rhughes@241
   658
		ALIGN(nindex * sizeof *index + hsize, 8);
rhughes@241
   659
	nindex = ntohl(rpm->header->nindex);
rhughes@241
   660
	hsize = ntohl(rpm->header->hsize);
rhughes@241
   661
	rpm->payload = (void *) (rpm->header + 1) +
rhughes@241
   662
		nindex * sizeof *index + hsize;
rhughes@241
   663
rhughes@241
   664
	base = (struct rpm_header_index *) (rpm->header + 1);
rhughes@241
   665
	rpm->pool = (void *) base + nindex * sizeof *index;
rhughes@241
   666
rhughes@241
   667
	/* Look up dir names now so we can index them directly. */
rhughes@241
   668
	name = razor_rpm_get_indirect(rpm, RPMTAG_DIRNAMES, &count);
rhughes@241
   669
	if (name) {
rhughes@241
   670
		rpm->dirs = calloc(count, sizeof *rpm->dirs);
rhughes@241
   671
		for (i = 0; i < count; i++) {
rhughes@241
   672
			rpm->dirs[i] = name;
rhughes@241
   673
			name += strlen(name) + 1;
rhughes@241
   674
		}
rhughes@241
   675
	} else {
rhughes@241
   676
		name = razor_rpm_get_indirect(rpm, RPMTAG_OLDFILENAMES,
rhughes@241
   677
					      &count);
rhughes@241
   678
		if (name) {
ali@403
   679
			razor_rpm_close(rpm);
ali@447
   680
			razor_set_error(error, RAZOR_GENERAL_ERROR,
ali@447
   681
					RAZOR_GENERAL_ERROR_RPM_UNSUPPORTED,
ali@475
   682
					uri, "Old filenames not supported");
rhughes@241
   683
			return NULL;
rhughes@241
   684
		}
rhughes@241
   685
	}
rhughes@241
   686
ali@351
   687
	prefix = razor_rpm_get_indirect(rpm, RPMTAG_PREFIXES, &count);
ali@351
   688
	if (prefix) {
ali@372
   689
		rpm->prefixes = calloc(count + 1, sizeof *rpm->prefixes);
ali@351
   690
		for (i = 0; i < count; i++) {
ali@351
   691
			rpm->prefixes[i] = prefix;
ali@351
   692
			prefix += strlen(prefix) + 1;
ali@351
   693
		}
ali@372
   694
		rpm->prefixes[i] = NULL;
ali@351
   695
		rpm->n_prefixes = count;
ali@351
   696
	} else {
ali@351
   697
		prefix = razor_rpm_get_indirect(rpm, RPMTAG_DEFAULTPREFIX,
ali@351
   698
						&count);
ali@351
   699
		if (prefix) {
ali@403
   700
			razor_rpm_close(rpm);
ali@447
   701
			razor_set_error(error, RAZOR_GENERAL_ERROR,
ali@447
   702
					RAZOR_GENERAL_ERROR_RPM_UNSUPPORTED,
ali@475
   703
					uri, "Default prefix not supported");
ali@351
   704
			return NULL;
ali@351
   705
		}
ali@351
   706
	}
ali@351
   707
rhughes@241
   708
	return rpm;
rhughes@241
   709
}
rhughes@241
   710
ali@351
   711
RAZOR_EXPORT void razor_rpm_set_relocations(struct razor_rpm *rpm,
ali@351
   712
					    struct razor_relocations *rr)
ali@351
   713
{
ali@351
   714
	assert (rpm != NULL);
ali@351
   715
ali@351
   716
	rpm->relocations = rr;
ali@351
   717
}
ali@351
   718
rhughes@241
   719
struct cpio_file_header {
rhughes@241
   720
	char magic[6];
rhughes@241
   721
	char inode[8];
rhughes@241
   722
	char mode[8];
rhughes@241
   723
	char uid[8];
rhughes@241
   724
	char gid[8];
rhughes@241
   725
	char nlink[8];
rhughes@241
   726
	char mtime[8];
rhughes@241
   727
	char filesize[8];
rhughes@241
   728
	char devmajor[8];
rhughes@241
   729
	char devminor[8];
rhughes@241
   730
	char rdevmajor[8];
rhughes@241
   731
	char rdevminor[8];
rhughes@241
   732
	char namesize[8];
rhughes@241
   733
	char checksum[8];
rhughes@241
   734
	char filename[0];
rhughes@241
   735
};
rhughes@241
   736
rhughes@241
   737
/* gzip flags */
rhughes@241
   738
#define ASCII_FLAG   0x01 /* bit 0 set: file probably ascii text */
rhughes@241
   739
#define HEAD_CRC     0x02 /* bit 1 set: header CRC present */
rhughes@241
   740
#define EXTRA_FIELD  0x04 /* bit 2 set: extra field present */
rhughes@241
   741
#define ORIG_NAME    0x08 /* bit 3 set: original file name present */
rhughes@241
   742
#define COMMENT      0x10 /* bit 4 set: file comment present */
rhughes@241
   743
#define RESERVED     0xE0 /* bits 5..7: reserved */
rhughes@241
   744
rhughes@241
   745
struct installer {
ali@475
   746
	const char *root_uri;
rhughes@241
   747
	struct razor_rpm *rpm;
ali@403
   748
	struct razor_atomic *atomic;
rhughes@241
   749
	z_stream stream;
rhughes@241
   750
	unsigned char buffer[32768];
rhughes@241
   751
	size_t rest, length;
rhughes@241
   752
};
rhughes@241
   753
rhughes@241
   754
static int
rhughes@241
   755
installer_inflate(struct installer *installer)
rhughes@241
   756
{
rhughes@241
   757
	size_t length;
rhughes@241
   758
	int err;
rhughes@241
   759
rhughes@241
   760
	if (installer->rest > sizeof installer->buffer)
rhughes@241
   761
		length = sizeof installer->buffer;
rhughes@241
   762
	else
rhughes@241
   763
		length = installer->rest;
rhughes@241
   764
rhughes@241
   765
	installer->stream.next_out = installer->buffer;
rhughes@241
   766
	installer->stream.avail_out = length;
rhughes@241
   767
	err = inflate(&installer->stream, Z_SYNC_FLUSH);
rhughes@241
   768
	if (err != Z_OK && err != Z_STREAM_END) {
ali@447
   769
		razor_atomic_abort(installer->atomic, RAZOR_ZLIB_ERROR, err,
ali@447
   770
				   "Failed to inflate");
rhughes@241
   771
		return -1;
rhughes@241
   772
	}
rhughes@241
   773
rhughes@241
   774
	installer->rest -= length;
rhughes@241
   775
	installer->length = length;
rhughes@241
   776
rhughes@241
   777
	return 0;
rhughes@241
   778
}
rhughes@241
   779
rhughes@241
   780
static int
rhughes@241
   781
installer_align(struct installer *installer, size_t size)
rhughes@241
   782
{
rhughes@241
   783
	unsigned char buffer[4];
rhughes@241
   784
	int err;
rhughes@241
   785
rhughes@241
   786
	installer->stream.next_out = buffer;
rhughes@241
   787
	installer->stream.avail_out =
rhughes@241
   788
		(size - installer->stream.total_out) & (size - 1);
rhughes@241
   789
rhughes@241
   790
	if (installer->stream.avail_out == 0)
rhughes@241
   791
		return 0;
rhughes@241
   792
rhughes@241
   793
	err = inflate(&installer->stream, Z_SYNC_FLUSH);
rhughes@241
   794
	if (err != Z_OK && err != Z_STREAM_END) {
ali@447
   795
		razor_atomic_abort(installer->atomic, RAZOR_ZLIB_ERROR, err,
ali@447
   796
				   "Failed to inflate");
rhughes@241
   797
		return -1;
rhughes@241
   798
	}
rhughes@241
   799
rhughes@241
   800
	return 0;
rhughes@241
   801
}
rhughes@241
   802
rhughes@241
   803
static int
rhughes@241
   804
create_path(struct installer *installer, const char *path, unsigned int mode)
rhughes@241
   805
{
ali@475
   806
	const char *s, *relative;
ali@475
   807
	char *uri, *buffer;
ali@475
   808
	int h, ret, is_within_root;
ali@475
   809
	struct razor_error *tmp_error = NULL;
rhughes@241
   810
ali@475
   811
	uri = razor_path_to_uri(path);
ali@475
   812
ali@475
   813
	if (str_has_prefix(uri, "file:///"))
ali@475
   814
		relative = uri + 8;
ali@475
   815
	else if (str_has_prefix(uri, "file:/"))
ali@475
   816
		relative = uri + 6;
ali@475
   817
	else if (str_has_prefix(uri, "file:"))
ali@475
   818
		relative = uri + 5;
ali@475
   819
	else if (str_has_prefix(uri, "/"))
ali@475
   820
		relative = uri + 1;
ali@475
   821
	else
ali@475
   822
		relative = uri;
ali@475
   823
ali@475
   824
	buffer = razor_resolve_uri_root(installer->root_uri, relative, 1,
ali@475
   825
					&tmp_error);
ali@475
   826
ali@475
   827
	if (!buffer) {
ali@475
   828
		razor_atomic_propagate_error(installer->atomic, tmp_error,
ali@475
   829
					     NULL);
ali@475
   830
		free(uri);
rhughes@241
   831
		return -1;
ali@475
   832
	}
rhughes@241
   833
ali@475
   834
	is_within_root = installer->root_uri &&
ali@475
   835
			 str_has_prefix(buffer, installer->root_uri);
ali@475
   836
ali@475
   837
	if (is_within_root)
ali@475
   838
		ret = razor_atomic_make_dirs(installer->atomic,
ali@475
   839
					     installer->root_uri, relative);
ali@475
   840
	else
ali@475
   841
		ret = razor_atomic_make_dirs(installer->atomic, "", buffer);
ali@475
   842
ali@475
   843
	free(uri);
ali@475
   844
ali@475
   845
	if (ret)
ali@475
   846
		return ret;
rhughes@241
   847
rhughes@241
   848
	switch (mode >> 12) {
rhughes@241
   849
	case REG:
rhughes@241
   850
		/* FIXME: handle the case where a file is already there. */
ali@403
   851
		h = razor_atomic_create_file(installer->atomic, buffer, mode);
ali@403
   852
		free(buffer);
ali@403
   853
		if (h < 0)
rhughes@241
   854
			return -1;
ali@403
   855
		while (installer->rest > 0) {
ali@403
   856
			if (installer_inflate(installer))
ali@403
   857
				return -1;
ali@403
   858
			if (razor_atomic_write(installer->atomic, h,
ali@403
   859
					       installer->buffer,
ali@403
   860
					       installer->length))
ali@403
   861
				return -1;
rhughes@241
   862
		}
ali@475
   863
		ret = razor_atomic_close(installer->atomic, h);
ali@475
   864
		break;
rhughes@241
   865
	case XDIR:
ali@403
   866
		ret = razor_atomic_create_dir(installer->atomic, buffer, mode);
ali@403
   867
		free(buffer);
ali@403
   868
		return ret;
rhughes@241
   869
	case LINK:
ali@327
   870
#if HAVE_SYMLINK
ali@403
   871
		if (installer_inflate(installer))
rhughes@241
   872
			return -1;
rhughes@241
   873
		if (installer->length >= sizeof installer->buffer) {
ali@403
   874
			razor_atomic_abort(installer->atomic,
ali@475
   875
					   RAZOR_GENERAL_ERROR,
ali@475
   876
					   RAZOR_GENERAL_ERROR_RPM_UNSUPPORTED,
ali@475
   877
					   "Link target too long");
rhughes@241
   878
			return -1;
rhughes@241
   879
		}
rhughes@241
   880
		installer->buffer[installer->length] = '\0';
ali@403
   881
		ret = razor_atomic_create_symlink(installer->atomic,
ali@475
   882
						  (const char *)installer->buffer,
ali@475
   883
						  buffer);
ali@403
   884
		free(buffer);
ali@475
   885
		break;
ali@327
   886
#else
ali@403
   887
		s = "Symbolic links";
ali@403
   888
		goto unsupported;
ali@327
   889
#endif
ali@327
   890
	case PIPE:
ali@403
   891
		s = "Named pipes";
ali@403
   892
unsupported:
ali@403
   893
		free(buffer);
ali@403
   894
		buffer = razor_concat(s, " are not supported on this platform",
ali@475
   895
				      NULL);
ali@447
   896
		razor_atomic_abort(installer->atomic, RAZOR_GENERAL_ERROR,
ali@475
   897
				   RAZOR_GENERAL_ERROR_RPM_UNSUPPORTED, buffer);
ali@403
   898
		free(buffer);
ali@403
   899
		return -1;
ali@327
   900
	case CDEV:
ali@403
   901
		s = "Character devices";
ali@403
   902
		goto unsupported;
ali@327
   903
	case BDEV:
ali@403
   904
		s = "Block devices";
ali@403
   905
		goto unsupported;
ali@327
   906
	case SOCK:
ali@403
   907
		s = "Named sockets";
ali@403
   908
		goto unsupported;
rhughes@241
   909
	default:
ali@403
   910
		free(buffer);
ali@447
   911
		razor_atomic_abort(installer->atomic, RAZOR_GENERAL_ERROR,
ali@475
   912
				   RAZOR_GENERAL_ERROR_RPM_UNSUPPORTED,
ali@475
   913
				   "Unknown file type");
ali@403
   914
		return -1;
rhughes@241
   915
	}
ali@475
   916
ali@475
   917
	return ret;
rhughes@241
   918
}
rhughes@241
   919
ali@353
   920
static int chroot_push(const char *root)
ali@353
   921
{
ali@353
   922
	int fd;
ali@353
   923
#if HAVE_CHROOT
ali@353
   924
	if (geteuid() == 0) {
ali@353
   925
		fd = open("/", O_RDONLY, 0);
ali@353
   926
		if (chroot(root) < 0) {
ali@353
   927
			fprintf(stderr, "failed to chroot to %s: %s\n",
ali@353
   928
				root, strerror(errno));
ali@353
   929
			exit(-1);
ali@353
   930
		}
ali@353
   931
	} else
ali@353
   932
#endif
ali@353
   933
		fd = -1;
ali@353
   934
	return fd;
ali@353
   935
}
ali@353
   936
ali@353
   937
static void chroot_pop(int fd)
rhughes@241
   938
{
ali@328
   939
#if HAVE_CHROOT
ali@353
   940
	if (fd >= 0) {
ali@353
   941
		fchdir(fd);
ali@353
   942
		close(fd);
ali@353
   943
		chroot(".");
ali@353
   944
	}
ali@353
   945
#endif
ali@353
   946
}
ali@353
   947
ali@353
   948
static int
ali@475
   949
run_script_lua(const char *root_uri, unsigned int script_tag,
ali@475
   950
	       const char *script, int arg1, struct razor_error **error)
ali@353
   951
{
ali@353
   952
	int root_fd, retval;
ali@475
   953
	const char *name;
ali@352
   954
#if HAVE_LUA
ali@475
   955
	char *root;
ali@475
   956
#endif
ali@353
   957
ali@353
   958
	switch(script_tag) {
ali@353
   959
		case RPMTAG_PREIN:
ali@353
   960
			name = "%pre";
ali@353
   961
			break;
ali@353
   962
		case RPMTAG_POSTIN:
ali@353
   963
			name = "%post";
ali@353
   964
			break;
ali@353
   965
		case RPMTAG_PREUN:
ali@353
   966
			name = "%preun";
ali@353
   967
			break;
ali@353
   968
		case RPMTAG_POSTUN:
ali@353
   969
			name = "%postun";
ali@353
   970
			break;
ali@353
   971
		default:
ali@353
   972
			name = "script";
ali@353
   973
			break;
ali@353
   974
	}
ali@475
   975
ali@475
   976
#if HAVE_LUA
ali@475
   977
	root = razor_path_from_uri(root_uri, error);
ali@475
   978
	if (!root)
ali@475
   979
		return -1;
ali@475
   980
	root_fd = root ? chroot_push(root) : -1;
ali@376
   981
	retval = run_lua_script(root_fd < 0 ? root : NULL, name, script, -1,
ali@376
   982
				arg1);
ali@475
   983
	free(root);
ali@353
   984
	chroot_pop(root_fd);
ali@353
   985
#else	/* HAVE_LUA */
ali@475
   986
	razor_set_error(error, RAZOR_GENERAL_ERROR, RAZOR_GENERAL_ERROR_FAILED,
ali@475
   987
			name, "lua not available to run script");
ali@353
   988
	retval = -1;
ali@353
   989
#endif	/* HAVE_LUA */
ali@353
   990
ali@353
   991
	return retval;
ali@353
   992
}
ali@353
   993
ali@353
   994
static int
ali@475
   995
run_script_external(const char *root_uri, const char *program,
ali@475
   996
		    const char *script, int arg1, struct razor_error **error)
ali@353
   997
{
ali@353
   998
	int root_fd, retval;
ali@328
   999
	FILE *fp;
ali@475
  1000
	char buf[32], *command, *root;
rhughes@241
  1001
ali@353
  1002
	if (program == NULL) {
ali@328
  1003
#if MSWIN_API
ali@328
  1004
		program = getenv("COMSPEC");
ali@328
  1005
		if (program) {
ali@328
  1006
			program = strchr(program, '=');
ali@328
  1007
			if (program)
ali@328
  1008
				program++;
ali@328
  1009
		}
ali@328
  1010
		if (!program)
ali@328
  1011
			program = "c:\\windows\\system32\\cmd.exe";
ali@328
  1012
#else
rhughes@241
  1013
		program = "/bin/sh";
ali@328
  1014
#endif
rhughes@241
  1015
	}
rhughes@241
  1016
ali@475
  1017
	root = razor_path_from_uri(root_uri, error);
ali@475
  1018
	if (!root)
ali@475
  1019
		return -1;
ali@353
  1020
	root_fd = chroot_push(root);
ali@475
  1021
	free(root);
ali@376
  1022
	if (arg1 >= 0) {
ali@376
  1023
		sprintf(buf, "%d", arg1);
ali@376
  1024
		command = malloc(strlen(program) + strlen(buf) + 2);
ali@376
  1025
		sprintf(command, "%s %s", program, buf);
ali@376
  1026
	} else
ali@376
  1027
		command = strdup(program);
ali@376
  1028
	fp = popen(command, "w");
ali@376
  1029
	free(command);
rhughes@241
  1030
ali@353
  1031
	if (!fp) {
ali@475
  1032
		razor_set_error_posix(error, program);
ali@353
  1033
		retval = -1;
ali@370
  1034
	} else if (script && fwrite(script, strlen(script), 1, fp) != 1) {
ali@475
  1035
		razor_set_error_posix(error,
ali@475
  1036
				      "failed to write script to program");
ali@353
  1037
		retval = -1;
ali@353
  1038
	} else
ali@353
  1039
		retval = 0;
ali@353
  1040
ali@353
  1041
	if (fp)
ali@353
  1042
		pclose(fp);
ali@353
  1043
	chroot_pop(root_fd);
ali@353
  1044
ali@353
  1045
	return retval;
ali@353
  1046
}
ali@353
  1047
ali@353
  1048
static int
ali@475
  1049
run_script(struct installer *installer, unsigned int program_tag,
ali@475
  1050
	   unsigned int script_tag, int arg1, struct razor_error **error)
ali@353
  1051
{
ali@362
  1052
	int i, retval;
ali@362
  1053
	struct razor_rpm *rpm = installer->rpm;
ali@362
  1054
	const char *script = NULL, *program = NULL, *prefix;
ali@376
  1055
	char buf[32];
ali@372
  1056
	struct environment env;
ali@353
  1057
ali@362
  1058
	program = razor_rpm_get_indirect(rpm, program_tag, NULL);
ali@362
  1059
	script = razor_rpm_get_indirect(rpm, script_tag, NULL);
ali@353
  1060
	if (program == NULL && script == NULL)
ali@362
  1061
		return 0;
ali@362
  1062
ali@362
  1063
	if (rpm->relocations) {
ali@372
  1064
		environment_init(&env);
ali@362
  1065
		for(i = 0; i < rpm->n_prefixes; i++) {
ali@362
  1066
			prefix = razor_relocations_apply(rpm->relocations,
ali@362
  1067
							 rpm->prefixes[i]);
ali@362
  1068
			sprintf(buf, "RPM_INSTALL_PREFIX%d", i);
ali@372
  1069
			environment_add_variable(&env, buf, prefix);
ali@362
  1070
		}
ali@372
  1071
		environment_set(&env);
ali@362
  1072
	}
ali@362
  1073
ali@369
  1074
	if (program && strcmp(program, "<lua>") == 0)
ali@475
  1075
		retval = run_script_lua(installer->root_uri, script_tag,
ali@475
  1076
					script, arg1, error);
ali@353
  1077
	else
ali@475
  1078
		retval = run_script_external(installer->root_uri, program,
ali@475
  1079
					     script, arg1, error);
ali@353
  1080
ali@362
  1081
	if (rpm->relocations) {
ali@372
  1082
		environment_unset(&env);
ali@372
  1083
		environment_release(&env);
ali@362
  1084
	}
ali@362
  1085
ali@353
  1086
	return retval;
rhughes@241
  1087
}
rhughes@241
  1088
ali@369
  1089
int
ali@479
  1090
razor_run_script(const char *root_uri, enum razor_property_flags script,
ali@475
  1091
		 const char *program, const char *body, int arg1,
ali@475
  1092
		 struct razor_error **error)
ali@369
  1093
{
ali@369
  1094
	int retval;
ali@369
  1095
	unsigned int script_tag;
ali@369
  1096
ali@369
  1097
	if (program && !*program)
ali@369
  1098
		program = NULL;
ali@369
  1099
	if (body && !*body)
ali@369
  1100
		body = NULL;
ali@369
  1101
	if (program == NULL && body == NULL)
ali@369
  1102
		return 0;
ali@369
  1103
ali@369
  1104
	if (program && strcmp(program, "<lua>") == 0)
ali@369
  1105
	{
ali@369
  1106
		switch(script) {
ali@369
  1107
		case RAZOR_PROPERTY_PRE:
ali@369
  1108
			script_tag = RPMTAG_PREIN;
ali@369
  1109
			break;
ali@369
  1110
		case RAZOR_PROPERTY_POST:
ali@369
  1111
			script_tag = RPMTAG_POSTIN;
ali@369
  1112
			break;
ali@369
  1113
		case RAZOR_PROPERTY_PREUN:
ali@369
  1114
			script_tag = RPMTAG_PREUN;
ali@369
  1115
			break;
ali@369
  1116
		case RAZOR_PROPERTY_POSTUN:
ali@369
  1117
			script_tag = RPMTAG_POSTUN;
ali@369
  1118
			break;
ali@369
  1119
		default:
ali@369
  1120
			script_tag = 0;
ali@369
  1121
			break;
ali@369
  1122
		}
ali@479
  1123
		retval = run_script_lua(root_uri, script_tag, body, arg1, error);
ali@369
  1124
	}
ali@369
  1125
	else
ali@479
  1126
		retval = run_script_external(root_uri, program, body, arg1,
ali@479
  1127
					     error);
ali@369
  1128
ali@369
  1129
	return retval;
ali@369
  1130
}
ali@369
  1131
rhughes@241
  1132
static int
rhughes@241
  1133
installer_init(struct installer *installer)
rhughes@241
  1134
{
rhughes@241
  1135
	unsigned char *gz_header;
rhughes@241
  1136
	int method, flags, err;
ali@403
  1137
	char buffer[32], *s;
rhughes@241
  1138
rhughes@241
  1139
	gz_header = installer->rpm->payload;
rhughes@241
  1140
	if (gz_header[0] != 0x1f || gz_header[1] != 0x8b) {
ali@447
  1141
		razor_atomic_abort(installer->atomic, RAZOR_GENERAL_ERROR,
ali@447
  1142
				   RAZOR_GENERAL_ERROR_RPM_UNSUPPORTED,
ali@403
  1143
				   "Payload section doesn't have gz header");
rhughes@241
  1144
		return -1;
rhughes@241
  1145
	}
rhughes@241
  1146
rhughes@241
  1147
	method = gz_header[2];
rhughes@241
  1148
	flags = gz_header[3];
rhughes@241
  1149
rhughes@241
  1150
	if (method != Z_DEFLATED || flags != 0) {
ali@447
  1151
		razor_atomic_abort(installer->atomic, RAZOR_GENERAL_ERROR,
ali@447
  1152
				   RAZOR_GENERAL_ERROR_RPM_UNSUPPORTED,
ali@403
  1153
				   "Unknown payload compression method or "
ali@403
  1154
				   "flags set");
rhughes@241
  1155
		return -1;
rhughes@241
  1156
	}
rhughes@241
  1157
rhughes@241
  1158
	installer->stream.zalloc = NULL;
rhughes@241
  1159
	installer->stream.zfree = NULL;
rhughes@241
  1160
	installer->stream.opaque = NULL;
rhughes@241
  1161
rhughes@241
  1162
	installer->stream.next_in  = gz_header + 10;
rhughes@241
  1163
	installer->stream.avail_in =
rhughes@241
  1164
		(installer->rpm->map + installer->rpm->size) -
rhughes@241
  1165
		(void *) installer->stream.next_in;
rhughes@241
  1166
	installer->stream.next_out = NULL;
rhughes@241
  1167
	installer->stream.avail_out = 0;
rhughes@241
  1168
rhughes@241
  1169
	err = inflateInit2(&installer->stream, -MAX_WBITS);
rhughes@241
  1170
	if (err != Z_OK) {
ali@403
  1171
		sprintf(buffer, "%d", err);
ali@442
  1172
		s = razor_concat("inflateEnd error: ", buffer, NULL);
ali@447
  1173
		razor_atomic_abort(installer->atomic, RAZOR_ZLIB_ERROR, err, s);
ali@403
  1174
		free(s);
rhughes@241
  1175
		return -1;
rhughes@241
  1176
	}
rhughes@241
  1177
rhughes@241
  1178
	return 0;
rhughes@241
  1179
}
rhughes@241
  1180
rhughes@241
  1181
static int
rhughes@241
  1182
installer_finish(struct installer *installer)
rhughes@241
  1183
{
rhughes@241
  1184
	int err;
ali@403
  1185
	char buffer[32], *s;
rhughes@241
  1186
rhughes@241
  1187
	err = inflateEnd(&installer->stream);
rhughes@241
  1188
rhughes@241
  1189
	if (err != Z_OK) {
ali@403
  1190
		sprintf(buffer, "%d", err);
ali@442
  1191
		s = razor_concat("inflateEnd error: ", buffer, NULL);
ali@447
  1192
		razor_atomic_abort(installer->atomic, RAZOR_ZLIB_ERROR, err, s);
ali@403
  1193
		free(s);
rhughes@241
  1194
	}
rhughes@241
  1195
ali@403
  1196
	return razor_atomic_in_error_state(installer->atomic);
rhughes@241
  1197
}
rhughes@241
  1198
rhughes@241
  1199
static unsigned long
rhughes@241
  1200
fixed_hex_to_ulong(const char *hex, int length)
rhughes@241
  1201
{
rhughes@241
  1202
	long l;
rhughes@241
  1203
	int i;
rhughes@241
  1204
rhughes@241
  1205
	for (i = 0, l = 0; i < length; i++) {
rhughes@241
  1206
		if (hex[i] < 'a')
rhughes@241
  1207
			l = l * 16 + hex[i] - '0';
rhughes@241
  1208
		else
rhughes@241
  1209
			l = l * 16 + hex[i] - 'a' + 10;
rhughes@241
  1210
	}
rhughes@241
  1211
rhughes@241
  1212
	return l;
rhughes@241
  1213
}
rhughes@241
  1214
krh@269
  1215
RAZOR_EXPORT int
ali@403
  1216
razor_rpm_install(struct razor_rpm *rpm, struct razor_atomic *atomic,
ali@475
  1217
		  const char *root_uri, int install_count,
ali@403
  1218
		  enum razor_stage_type stage)
rhughes@241
  1219
{
rhughes@241
  1220
	struct installer installer;
rhughes@241
  1221
	struct cpio_file_header *header;
rhughes@241
  1222
	unsigned int mode;
ali@424
  1223
	const char *path;
rhughes@241
  1224
	size_t filesize;
ali@403
  1225
	char *s;
ali@475
  1226
	int retval = 0, domain, code;
ali@475
  1227
	struct razor_error *tmp_error = NULL;
rhughes@241
  1228
richard@301
  1229
	assert (rpm != NULL);
ali@475
  1230
	assert (root_uri != NULL);
richard@301
  1231
rhughes@241
  1232
	installer.rpm = rpm;
ali@475
  1233
	installer.root_uri = root_uri;
ali@403
  1234
	installer.atomic = atomic;
rhughes@241
  1235
rhughes@241
  1236
	/* FIXME: Only do this before a transaction, not per rpm. */
ali@475
  1237
	if (*root_uri && razor_uri_is_directory(root_uri, &tmp_error) <= 0) {
ali@475
  1238
		if (tmp_error) {
ali@475
  1239
			domain = razor_error_get_domain(tmp_error);
ali@475
  1240
			code = razor_error_get_code(tmp_error);
ali@475
  1241
			razor_error_free(tmp_error);
ali@475
  1242
			tmp_error = NULL;
ali@475
  1243
		} else {
ali@475
  1244
			domain = RAZOR_POSIX_ERROR;
ali@475
  1245
			code = ENOTDIR;
ali@475
  1246
		}
ali@475
  1247
		s = razor_concat(root_uri, ": Directory does not exist", NULL);
ali@475
  1248
		razor_atomic_abort(atomic, domain, code, s);
ali@403
  1249
		free(s);
rhughes@241
  1250
		return -1;
rhughes@241
  1251
	}
rhughes@241
  1252
ali@351
  1253
	if (rpm->relocations)
ali@351
  1254
		razor_relocations_set_rpm(rpm->relocations, rpm);
ali@351
  1255
ali@475
  1256
	if (stage & RAZOR_STAGE_SCRIPTS_PRE) {
ali@422
  1257
		retval = run_script(&installer, RPMTAG_PREINPROG, RPMTAG_PREIN,
ali@475
  1258
				    install_count, &tmp_error);
ali@475
  1259
		if (retval) {
ali@475
  1260
			razor_atomic_propagate_error(atomic, tmp_error, NULL);
ali@475
  1261
			tmp_error = NULL;
ali@475
  1262
		}
ali@475
  1263
	}
rhughes@241
  1264
ali@422
  1265
	if (!retval && (stage & RAZOR_STAGE_FILES)) {
ali@403
  1266
		if (installer_init(&installer))
rhughes@241
  1267
			return -1;
rhughes@241
  1268
ali@403
  1269
		while (installer.stream.avail_in > 0) {
ali@403
  1270
			installer.rest = sizeof *header;
ali@403
  1271
			if (installer_inflate(&installer))
ali@403
  1272
				break;
rhughes@241
  1273
ali@403
  1274
			header = (struct cpio_file_header *) installer.buffer;
ali@403
  1275
			mode = fixed_hex_to_ulong(header->mode,
ali@403
  1276
						  sizeof header->mode);
ali@403
  1277
			filesize = fixed_hex_to_ulong(header->filesize,
ali@403
  1278
						      sizeof header->filesize);
rhughes@241
  1279
ali@403
  1280
			installer.rest =
ali@403
  1281
			  fixed_hex_to_ulong(header->namesize,
ali@403
  1282
					     sizeof header->namesize);
rhughes@241
  1283
ali@403
  1284
			if (installer_inflate(&installer) ||
ali@403
  1285
			    installer_align(&installer, 4))
ali@403
  1286
				break;
rhughes@241
  1287
ali@403
  1288
			path = (const char *) installer.buffer;
ali@403
  1289
			/* This convention is so lame... */
ali@403
  1290
			if (strcmp(path, "TRAILER!!!") == 0)
ali@403
  1291
				break;
ali@403
  1292
ali@403
  1293
			installer.rest = filesize;
ali@403
  1294
			path++;
ali@403
  1295
			if (rpm->relocations)
ali@403
  1296
				path = razor_relocations_apply(rpm->relocations,
ali@403
  1297
							       path);
ali@403
  1298
			if (create_path(&installer, path, mode))
ali@403
  1299
				break;
ali@403
  1300
			if (installer_align(&installer, 4))
ali@403
  1301
				break;
ali@403
  1302
		}
ali@403
  1303
ali@403
  1304
		if (installer_finish(&installer))
rhughes@241
  1305
			return -1;
ali@422
  1306
ali@422
  1307
		retval = razor_atomic_in_error_state(atomic);
rhughes@241
  1308
	}
rhughes@241
  1309
ali@475
  1310
	if (!retval && (stage & RAZOR_STAGE_SCRIPTS_POST)) {
ali@422
  1311
		retval = run_script(&installer, RPMTAG_POSTINPROG,
ali@475
  1312
				    RPMTAG_POSTIN, install_count, &tmp_error);
ali@475
  1313
		if (retval) {
ali@475
  1314
			razor_atomic_propagate_error(atomic, tmp_error, NULL);
ali@475
  1315
			tmp_error = NULL;
ali@475
  1316
		}
ali@475
  1317
	}
rhughes@241
  1318
ali@422
  1319
	return retval;
rhughes@241
  1320
}
rhughes@241
  1321
krh@269
  1322
RAZOR_EXPORT int
rhughes@241
  1323
razor_rpm_close(struct razor_rpm *rpm)
rhughes@241
  1324
{
rhughes@241
  1325
	int err;
rhughes@241
  1326
richard@301
  1327
	assert (rpm != NULL);
richard@301
  1328
rhughes@241
  1329
	free(rpm->dirs);
ali@351
  1330
	free(rpm->prefixes);
ali@475
  1331
	err = razor_uri_free_contents(rpm->map, rpm->size);
ali@369
  1332
	free(rpm->evr);
rhughes@241
  1333
	free(rpm);
rhughes@241
  1334
rhughes@241
  1335
	return err;
rhughes@241
  1336
}
rhughes@241
  1337
krh@269
  1338
RAZOR_EXPORT int
rhughes@241
  1339
razor_importer_add_rpm(struct razor_importer *importer, struct razor_rpm *rpm)
rhughes@241
  1340
{
ali@369
  1341
	const char *name, *version, *arch;
jbowes@289
  1342
	const char *summary, *description, *url, *license;
rhughes@241
  1343
richard@301
  1344
	assert (importer != NULL);
richard@301
  1345
	assert (rpm != NULL);
richard@301
  1346
ali@369
  1347
	razor_rpm_get_details(rpm,
ali@369
  1348
			      RAZOR_DETAIL_NAME, &name,
ali@369
  1349
			      RAZOR_DETAIL_VERSION, &version,
ali@369
  1350
			      RAZOR_DETAIL_ARCH, &arch,
ali@369
  1351
			      RAZOR_DETAIL_SUMMARY, &summary,
ali@369
  1352
			      RAZOR_DETAIL_DESCRIPTION, &description,
ali@369
  1353
			      RAZOR_DETAIL_URL, &url,
ali@369
  1354
			      RAZOR_DETAIL_LICENSE, &license,
ali@369
  1355
			      RAZOR_DETAIL_LAST);
jbowes@289
  1356
ali@369
  1357
	razor_importer_begin_package(importer, name, version, arch);
rhughes@241
  1358
jbowes@289
  1359
	razor_importer_add_details(importer, summary, description, url,
jbowes@289
  1360
				   license);
jbowes@289
  1361
rhughes@241
  1362
	import_properties(importer, RAZOR_PROPERTY_REQUIRES, rpm,
rhughes@241
  1363
			  RPMTAG_REQUIRENAME,
rhughes@241
  1364
			  RPMTAG_REQUIREVERSION,
rhughes@241
  1365
			  RPMTAG_REQUIREFLAGS);
rhughes@241
  1366
rhughes@241
  1367
	import_properties(importer, RAZOR_PROPERTY_PROVIDES, rpm,
rhughes@241
  1368
			  RPMTAG_PROVIDENAME,
rhughes@241
  1369
			  RPMTAG_PROVIDEVERSION,
rhughes@241
  1370
			  RPMTAG_PROVIDEFLAGS);
rhughes@241
  1371
rhughes@241
  1372
	import_properties(importer, RAZOR_PROPERTY_OBSOLETES, rpm,
rhughes@241
  1373
			  RPMTAG_OBSOLETENAME,
rhughes@241
  1374
			  RPMTAG_OBSOLETEVERSION,
rhughes@241
  1375
			  RPMTAG_OBSOLETEFLAGS);
rhughes@241
  1376
rhughes@241
  1377
	import_properties(importer, RAZOR_PROPERTY_CONFLICTS, rpm,
rhughes@241
  1378
			  RPMTAG_CONFLICTNAME,
rhughes@241
  1379
			  RPMTAG_CONFLICTVERSION,
rhughes@241
  1380
			  RPMTAG_CONFLICTFLAGS);
rhughes@241
  1381
rhughes@241
  1382
	import_files(importer, rpm);
rhughes@241
  1383
rhughes@241
  1384
	razor_importer_finish_package(importer);
rhughes@241
  1385
rhughes@241
  1386
	return 0;
rhughes@241
  1387
}