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