summaryrefslogtreecommitdiff
path: root/librazor/rpm.c
blob: 8bd52893d7fcaecd5d66e64c65466bc609241a8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
/*
 * Copyright (C) 2008  Kristian Høgsberg <krh@redhat.com>
 * Copyright (C) 2008  Red Hat, Inc
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <dirent.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <zlib.h>

#include "razor.h"
#include "razor-internal.h"

#define	RPM_LEAD_SIZE 96

enum {
    PIPE	=  1,	/*!< pipe/fifo */
    CDEV	=  2,	/*!< character device */
    XDIR	=  4,	/*!< directory */
    BDEV	=  6,	/*!< block device */
    REG		=  8,	/*!< regular file */
    LINK	= 10,	/*!< hard link */
    SOCK	= 12	/*!< socket */
};

enum {
    RPMSENSE_LESS		= 1 << 1,
    RPMSENSE_GREATER		= 1 << 2,
    RPMSENSE_EQUAL		= 1 << 3,
    RPMSENSE_PREREQ		= 1 << 6,
    RPMSENSE_SCRIPT_PRE		= 1 << 9,
    RPMSENSE_SCRIPT_POST	= 1 << 10,
    RPMSENSE_SCRIPT_PREUN	= 1 << 11,
    RPMSENSE_SCRIPT_POSTUN	= 1 << 12,
};

enum {
    RPMTAG_NAME  		= 1000,	/* s */
    RPMTAG_VERSION		= 1001,	/* s */
    RPMTAG_RELEASE		= 1002,	/* s */
    RPMTAG_EPOCH   		= 1003,	/* i */
    RPMTAG_SUMMARY		= 1004,	/* s{} */
    RPMTAG_DESCRIPTION		= 1005,	/* s{} */
    RPMTAG_BUILDTIME		= 1006,	/* i */
    RPMTAG_BUILDHOST		= 1007,	/* s */
    RPMTAG_INSTALLTIME		= 1008,	/* i */
    RPMTAG_SIZE			= 1009,	/* i */
    RPMTAG_DISTRIBUTION		= 1010,	/* s */
    RPMTAG_VENDOR		= 1011,	/* s */
    RPMTAG_GIF			= 1012,	/* x */
    RPMTAG_XPM			= 1013,	/* x */
    RPMTAG_LICENSE		= 1014,	/* s */
    RPMTAG_PACKAGER		= 1015,	/* s */
    RPMTAG_GROUP		= 1016,	/* s{} */
    RPMTAG_CHANGELOG		= 1017, /*!< s[] internal */
    RPMTAG_SOURCE		= 1018,	/* s[] */
    RPMTAG_PATCH		= 1019,	/* s[] */
    RPMTAG_URL			= 1020,	/* s */
    RPMTAG_OS			= 1021,	/* s legacy used int */
    RPMTAG_ARCH			= 1022,	/* s legacy used int */
    RPMTAG_PREIN		= 1023,	/* s */
    RPMTAG_POSTIN		= 1024,	/* s */
    RPMTAG_PREUN		= 1025,	/* s */
    RPMTAG_POSTUN		= 1026,	/* s */
    RPMTAG_OLDFILENAMES		= 1027, /* s[] obsolete */
    RPMTAG_FILESIZES		= 1028,	/* i */
    RPMTAG_FILESTATES		= 1029, /* c */
    RPMTAG_FILEMODES		= 1030,	/* h */
    RPMTAG_FILEUIDS		= 1031, /*!< internal */
    RPMTAG_FILEGIDS		= 1032, /*!< internal */
    RPMTAG_FILERDEVS		= 1033,	/* h */
    RPMTAG_FILEMTIMES		= 1034, /* i */
    RPMTAG_FILEMD5S		= 1035,	/* s[] */
    RPMTAG_FILELINKTOS		= 1036,	/* s[] */
    RPMTAG_FILEFLAGS		= 1037,	/* i */
    RPMTAG_ROOT			= 1038, /*!< internal - obsolete */
    RPMTAG_FILEUSERNAME		= 1039,	/* s[] */
    RPMTAG_FILEGROUPNAME	= 1040,	/* s[] */
    RPMTAG_EXCLUDE		= 1041, /*!< internal - obsolete */
    RPMTAG_EXCLUSIVE		= 1042, /*!< internal - obsolete */
    RPMTAG_ICON			= 1043,
    RPMTAG_SOURCERPM		= 1044,	/* s */
    RPMTAG_FILEVERIFYFLAGS	= 1045,	/* i */
    RPMTAG_ARCHIVESIZE		= 1046,	/* i */
    RPMTAG_PROVIDENAME		= 1047,	/* s[] */
    RPMTAG_REQUIREFLAGS		= 1048,	/* i */
    RPMTAG_REQUIRENAME		= 1049,	/* s[] */
    RPMTAG_REQUIREVERSION	= 1050,	/* s[] */
    RPMTAG_NOSOURCE		= 1051, /*!< internal */
    RPMTAG_NOPATCH		= 1052, /*!< internal */
    RPMTAG_CONFLICTFLAGS	= 1053, /* i */
    RPMTAG_CONFLICTNAME		= 1054,	/* s[] */
    RPMTAG_CONFLICTVERSION	= 1055,	/* s[] */
    RPMTAG_DEFAULTPREFIX	= 1056, /*!< internal - deprecated */
    RPMTAG_BUILDROOT		= 1057, /*!< internal */
    RPMTAG_INSTALLPREFIX	= 1058, /*!< internal - deprecated */
    RPMTAG_EXCLUDEARCH		= 1059,
    RPMTAG_EXCLUDEOS		= 1060,
    RPMTAG_EXCLUSIVEARCH	= 1061,
    RPMTAG_EXCLUSIVEOS		= 1062,
    RPMTAG_AUTOREQPROV		= 1063, /*!< internal */
    RPMTAG_RPMVERSION		= 1064,	/* s */
    RPMTAG_TRIGGERSCRIPTS	= 1065,	/* s[] */
    RPMTAG_TRIGGERNAME		= 1066,	/* s[] */
    RPMTAG_TRIGGERVERSION	= 1067,	/* s[] */
    RPMTAG_TRIGGERFLAGS		= 1068,	/* i */
    RPMTAG_TRIGGERINDEX		= 1069,	/* i */
    RPMTAG_VERIFYSCRIPT		= 1079,	/* s */
    RPMTAG_CHANGELOGTIME	= 1080,	/* i */
    RPMTAG_CHANGELOGNAME	= 1081,	/* s[] */
    RPMTAG_CHANGELOGTEXT	= 1082,	/* s[] */
    RPMTAG_BROKENMD5		= 1083, /*!< internal - obsolete */
    RPMTAG_PREREQ		= 1084, /*!< internal */
    RPMTAG_PREINPROG		= 1085,	/* s */
    RPMTAG_POSTINPROG		= 1086,	/* s */
    RPMTAG_PREUNPROG		= 1087,	/* s */
    RPMTAG_POSTUNPROG		= 1088,	/* s */
    RPMTAG_BUILDARCHS		= 1089,
    RPMTAG_OBSOLETENAME		= 1090,	/* s[] */
    RPMTAG_VERIFYSCRIPTPROG	= 1091,	/* s */
    RPMTAG_TRIGGERSCRIPTPROG	= 1092,	/* s */
    RPMTAG_DOCDIR		= 1093, /*!< internal */
    RPMTAG_COOKIE		= 1094,	/* s */
    RPMTAG_FILEDEVICES		= 1095,	/* i */
    RPMTAG_FILEINODES		= 1096,	/* i */
    RPMTAG_FILELANGS		= 1097,	/* s[] */
    RPMTAG_PREFIXES		= 1098,	/* s[] */
    RPMTAG_INSTPREFIXES		= 1099,	/* s[] */
    RPMTAG_TRIGGERIN		= 1100, /*!< internal */
    RPMTAG_TRIGGERUN		= 1101, /*!< internal */
    RPMTAG_TRIGGERPOSTUN	= 1102, /*!< internal */
    RPMTAG_AUTOREQ		= 1103, /*!< internal */
    RPMTAG_AUTOPROV		= 1104, /*!< internal */
    RPMTAG_CAPABILITY		= 1105, /*!< internal - obsolete */
    RPMTAG_SOURCEPACKAGE	= 1106, /*!< i src.rpm header marker */
    RPMTAG_OLDORIGFILENAMES	= 1107, /*!< internal - obsolete */
    RPMTAG_BUILDPREREQ		= 1108, /*!< internal */
    RPMTAG_BUILDREQUIRES	= 1109, /*!< internal */
    RPMTAG_BUILDCONFLICTS	= 1110, /*!< internal */
    RPMTAG_BUILDMACROS		= 1111, /*!< internal - unused */
    RPMTAG_PROVIDEFLAGS		= 1112,	/* i */
    RPMTAG_PROVIDEVERSION	= 1113,	/* s[] */
    RPMTAG_OBSOLETEFLAGS	= 1114,	/* i */
    RPMTAG_OBSOLETEVERSION	= 1115,	/* s[] */
    RPMTAG_DIRINDEXES		= 1116,	/* i */
    RPMTAG_BASENAMES		= 1117,	/* s[] */
    RPMTAG_DIRNAMES		= 1118,	/* s[] */
    RPMTAG_ORIGDIRINDEXES	= 1119, /*!< internal */
    RPMTAG_ORIGBASENAMES	= 1120, /*!< internal */
    RPMTAG_ORIGDIRNAMES		= 1121, /*!< internal */
    RPMTAG_OPTFLAGS		= 1122,	/* s */
    RPMTAG_DISTURL		= 1123,	/* s */
    RPMTAG_PAYLOADFORMAT	= 1124,	/* s */
    RPMTAG_PAYLOADCOMPRESSOR	= 1125,	/* s */
    RPMTAG_PAYLOADFLAGS		= 1126,	/* s */
    RPMTAG_INSTALLCOLOR		= 1127, /*!< i transaction color when installed */
    RPMTAG_INSTALLTID		= 1128,	/* i */
    RPMTAG_REMOVETID		= 1129,	/* i */
    RPMTAG_SHA1RHN		= 1130, /*!< internal - obsolete */
    RPMTAG_RHNPLATFORM		= 1131,	/* s */
    RPMTAG_PLATFORM		= 1132,	/* s */
    RPMTAG_PATCHESNAME		= 1133, /*!< placeholder (SuSE) */
    RPMTAG_PATCHESFLAGS		= 1134, /*!< placeholder (SuSE) */
    RPMTAG_PATCHESVERSION	= 1135, /*!< placeholder (SuSE) */
    RPMTAG_CACHECTIME		= 1136,	/* i */
    RPMTAG_CACHEPKGPATH		= 1137,	/* s */
    RPMTAG_CACHEPKGSIZE		= 1138,	/* i */
    RPMTAG_CACHEPKGMTIME	= 1139,	/* i */
    RPMTAG_FILECOLORS		= 1140,	/* i */
    RPMTAG_FILECLASS		= 1141,	/* i */
    RPMTAG_CLASSDICT		= 1142,	/* s[] */
    RPMTAG_FILEDEPENDSX		= 1143,	/* i */
    RPMTAG_FILEDEPENDSN		= 1144,	/* i */
    RPMTAG_DEPENDSDICT		= 1145,	/* i */
    RPMTAG_SOURCEPKGID		= 1146,	/* x */
    RPMTAG_FILECONTEXTS		= 1147,	/* s[] */
    RPMTAG_FSCONTEXTS		= 1148,	/*!< s[] extension */
    RPMTAG_RECONTEXTS		= 1149,	/*!< s[] extension */
    RPMTAG_POLICIES		= 1150,	/*!< s[] selinux *.te policy file. */
    RPMTAG_PRETRANS		= 1151,	/* s */
    RPMTAG_POSTTRANS		= 1152,	/* s */
    RPMTAG_PRETRANSPROG		= 1153,	/* s */
    RPMTAG_POSTTRANSPROG	= 1154,	/* s */
    RPMTAG_DISTTAG		= 1155,	/* s */
    RPMTAG_SUGGESTSNAME		= 1156,	/* s[] extension placeholder */
    RPMTAG_SUGGESTSVERSION	= 1157,	/* s[] extension placeholder */
    RPMTAG_SUGGESTSFLAGS	= 1158,	/* i   extension placeholder */
    RPMTAG_ENHANCESNAME		= 1159,	/* s[] extension placeholder */
    RPMTAG_ENHANCESVERSION	= 1160,	/* s[] extension placeholder */
    RPMTAG_ENHANCESFLAGS	= 1161,	/* i   extension placeholder */
    RPMTAG_PRIORITY		= 1162, /* i   extension placeholder */
    RPMTAG_CVSID		= 1163, /* s */
    RPMTAG_TRIGGERPREIN		= 1171, /*!< internal */
};

struct rpm_header {
	unsigned char magic[4];
	unsigned char reserved[4];
	int nindex;
	int hsize;
};

struct rpm_header_index {
	int tag;
	int type;
	int offset;
	int count;
};

struct razor_rpm {
	struct rpm_header *signature;
	struct rpm_header *header;
	const char **dirs;
	const char *pool;
	void *map;
	size_t size;
	void *payload;
};

static struct rpm_header_index *
razor_rpm_get_header(struct razor_rpm *rpm, unsigned int tag)
{
	struct rpm_header_index *index, *end;

	index = (struct rpm_header_index *) (rpm->header + 1);
	end = index + ntohl(rpm->header->nindex);
	while (index < end) {
		if (ntohl(index->tag) == tag)
			return index;
		index++;
	}

	return NULL;
}

static const void *
razor_rpm_get_indirect(struct razor_rpm *rpm,
		       unsigned int tag, unsigned int *count)
{
	struct rpm_header_index *index;

	index = razor_rpm_get_header(rpm, tag);
	if (index != NULL) {
		if (count)
			*count = ntohl(index->count);

		return rpm->pool + ntohl(index->offset);
	}

	return NULL;
}

static uint32_t
rpm_to_razor_flags(uint32_t flags)
{
	uint32_t razor_flags;

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

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

static void
import_properties(struct razor_importer *importer, uint32_t type,
		  struct razor_rpm *rpm,
		  int name_tag, int version_tag, int flags_tag)
{
	const char *name, *version;
	const uint32_t *flags;
	uint32_t f;
	unsigned int i, count;

	name = razor_rpm_get_indirect(rpm, name_tag, &count);
	if (name == NULL)
		return;

	flags = razor_rpm_get_indirect(rpm, flags_tag, &count);

	version = razor_rpm_get_indirect(rpm, version_tag, &count);
	for (i = 0; i < count; i++) {
		f = rpm_to_razor_flags(ntohl(flags[i]));
		razor_importer_add_property(importer, name, f | type, version);
		name += strlen(name) + 1;
		version += strlen(version) + 1;
	}
}

static void
import_files(struct razor_importer *importer, struct razor_rpm *rpm)
{
	const char *name;
	const uint32_t *index;
	unsigned int i, count;
	char buffer[256];

	if (rpm->dirs == NULL)
		return;

	/* assert: count is the same for all arrays */
	index = razor_rpm_get_indirect(rpm, RPMTAG_DIRINDEXES, &count);
	name = razor_rpm_get_indirect(rpm, RPMTAG_BASENAMES, &count);
	for (i = 0; i < count; i++) {
		snprintf(buffer, sizeof buffer,
			 "%s%s", rpm->dirs[ntohl(*index)], name);
		razor_importer_add_file(importer, buffer);
		name += strlen(name) + 1;
		index++;
	}
}

struct razor_rpm *
razor_rpm_open(const char *filename)
{
	struct razor_rpm *rpm;
	struct rpm_header_index *base, *index;
	struct stat buf;
	unsigned int count, i, nindex, hsize;
	const char *name;
	int fd;

	rpm = malloc(sizeof *rpm);
	if (rpm == NULL)
		return NULL;
	memset(rpm, 0, sizeof *rpm);

	fd = open(filename, O_RDONLY);
	if (fd < 0) {
		fprintf(stderr, "couldn't open %s\n", filename);
		return NULL;
	}

	if (fstat(fd, &buf) < 0) {
		fprintf(stderr, "failed to stat %s (%m)\n", filename);
		return NULL;
	}

	rpm->size = buf.st_size;
	rpm->map = mmap(NULL, rpm->size, PROT_READ, MAP_PRIVATE, fd, 0);
	if (rpm->map == MAP_FAILED) {
		fprintf(stderr, "couldn't mmap %s\n", filename);
		return NULL;
	}
	close(fd);

	rpm->signature = rpm->map + RPM_LEAD_SIZE;
	nindex = ntohl(rpm->signature->nindex);
	hsize = ntohl(rpm->signature->hsize);
	rpm->header = (void *) (rpm->signature + 1) +
		ALIGN(nindex * sizeof *index + hsize, 8);
	nindex = ntohl(rpm->header->nindex);
	hsize = ntohl(rpm->header->hsize);
	rpm->payload = (void *) (rpm->header + 1) +
		nindex * sizeof *index + hsize;

	base = (struct rpm_header_index *) (rpm->header + 1);
	rpm->pool = (void *) base + nindex * sizeof *index;

	/* Look up dir names now so we can index them directly. */
	name = razor_rpm_get_indirect(rpm, RPMTAG_DIRNAMES, &count);
	if (name) {
		rpm->dirs = calloc(count, sizeof *rpm->dirs);
		for (i = 0; i < count; i++) {
			rpm->dirs[i] = name;
			name += strlen(name) + 1;
		}
	} else {
		name = razor_rpm_get_indirect(rpm, RPMTAG_OLDFILENAMES,
					      &count);
		if (name) {
			fprintf(stderr, "old filenames not supported\n");
			return NULL;
		}
	}

	return rpm;
}

struct cpio_file_header {
	char magic[6];
	char inode[8];
	char mode[8];
	char uid[8];
	char gid[8];
	char nlink[8];
	char mtime[8];
	char filesize[8];
	char devmajor[8];
	char devminor[8];
	char rdevmajor[8];
	char rdevminor[8];
	char namesize[8];
	char checksum[8];
	char filename[0];
};

/* gzip flags */
#define ASCII_FLAG   0x01 /* bit 0 set: file probably ascii text */
#define HEAD_CRC     0x02 /* bit 1 set: header CRC present */
#define EXTRA_FIELD  0x04 /* bit 2 set: extra field present */
#define ORIG_NAME    0x08 /* bit 3 set: original file name present */
#define COMMENT      0x10 /* bit 4 set: file comment present */
#define RESERVED     0xE0 /* bits 5..7: reserved */

struct installer {
	const char *root;
	struct razor_rpm *rpm;
	z_stream stream;
	unsigned char buffer[32768];
	size_t rest, length;
};

static int
installer_inflate(struct installer *installer)
{
	size_t length;
	int err;

	if (installer->rest > sizeof installer->buffer)
		length = sizeof installer->buffer;
	else
		length = installer->rest;

	installer->stream.next_out = installer->buffer;
	installer->stream.avail_out = length;
	err = inflate(&installer->stream, Z_SYNC_FLUSH);
	if (err != Z_OK && err != Z_STREAM_END) {
		fprintf(stderr, "inflate error: %d (%m)\n", err);
		return -1;
	}

	installer->rest -= length;
	installer->length = length;

	return 0;
}

static int
installer_align(struct installer *installer, size_t size)
{
	unsigned char buffer[4];
	int err;

	installer->stream.next_out = buffer;
	installer->stream.avail_out =
		(size - installer->stream.total_out) & (size - 1);

	if (installer->stream.avail_out == 0)
		return 0;

	err = inflate(&installer->stream, Z_SYNC_FLUSH);
	if (err != Z_OK && err != Z_STREAM_END) {
		fprintf(stderr, "inflate error: %d (%m)\n", err);
		return -1;
	}

	return 0;
}

static int
create_path(struct installer *installer, const char *path, unsigned int mode)
{
	char buffer[PATH_MAX];
	struct stat buf;
	int fd, ret;

	if (razor_create_dir(installer->root, path) < 0)
		return -1;

	snprintf(buffer, sizeof buffer, "%s%s", installer->root, path);

	switch (mode >> 12) {
	case REG:
		/* FIXME: handle the case where a file is already there. */
		fd = open(buffer, O_WRONLY | O_CREAT | O_TRUNC, mode & 0x1ff);
		if (fd < 0){
			fprintf(stderr, "failed to create file %s\n", buffer);
			return -1;
		}
		while (installer->rest > 0) {
			if (installer_inflate(installer)) {
				fprintf(stderr, "failed to inflate\n");
				return -1;
			}
			if (razor_write(fd, installer->buffer,
					installer->length)) {
				fprintf(stderr, "failed to write payload\n");
				return -1;
			}
		}
		if (close(fd) < 0) {
			fprintf(stderr, "failed to close %s: %m\n", buffer);
			return -1;
		}
		return 0;
	case XDIR:
		ret = mkdir(buffer, mode & 0x1ff);
		if (ret == 0 || errno != EEXIST)
			return ret;
		if (stat(buffer, &buf) || !S_ISDIR(buf.st_mode)) {
			/* FIXME: also check that mode match. */
			fprintf(stderr,
				"%s exists but is not a directory\n", buffer);
			return -1;
		}
		return 0;
	case PIPE:
	case CDEV:
	case BDEV:
	case SOCK:
		printf("%s: unhandled file type %d\n", buffer, mode >> 12);
		return 0;
	case LINK:
		if (installer_inflate(installer)) {
			fprintf(stderr, "failed to inflate\n");
			return -1;
		}
		if (installer->length >= sizeof installer->buffer) {
			fprintf(stderr, "link name too long\n");
			return -1;
		}
		installer->buffer[installer->length] = '\0';
		if (symlink((const char *) installer->buffer, buffer)) {
			fprintf(stderr, "failed to create symlink, %m\n");
			return -1;
		}
		return 0;
	default:
		printf("%s: unknown file type %d\n", buffer, mode >> 12);
		return 0;
	}
}

static int
run_script(struct installer *installer,
	   unsigned int program_tag, unsigned int script_tag)
{
	int pid, status, fd[2];
	const char *script = NULL, *program = NULL;

	program = razor_rpm_get_indirect(installer->rpm, program_tag, NULL);
	script = razor_rpm_get_indirect(installer->rpm, script_tag, NULL);
	if (program == NULL && script == NULL) {
		return 0;
	} else if (program == NULL) {
		program = "/bin/sh";
	}

	if (pipe(fd) < 0) {
		fprintf(stderr, "failed to create pipe\n");
		return -1;
	}
	pid = fork();
	if (pid < 0) {
		fprintf(stderr, "failed to fork, %m\n");
	} else if (pid == 0) {
		if (dup2(fd[0], STDIN_FILENO) < 0) {
			fprintf(stderr, "failed redirect stdin, %m\n");
			exit(-1);
		}
		if (close(fd[0]) < 0 || close(fd[1]) < 0) {
			fprintf(stderr, "failed to close pipe, %m\n");
			exit(-1);
		}
		if (chroot(installer->root) < 0) {
			fprintf(stderr, "failed to chroot to %s, %m\n",
				installer->root);
			exit(-1);
		}
		printf("executing program %s in chroot %s\n",
		       program, installer->root);
		if (execl(program, program, NULL)) {
			fprintf(stderr, "failed to exec %s, %m\n", program);
			exit(-1);
		}
	} else {
		if (script && razor_write(fd[1], script, strlen(script)) < 0) {
			fprintf(stderr, "failed to pipe script, %m\n");
			return -1;
		}
		if (close(fd[0]) || close(fd[1])) {
			fprintf(stderr, "failed to close pipe, %m\n");
			return -1;
		}
		if (wait(&status) < 0) {
			fprintf(stderr, "wait for child failed, %m");
			return -1;
		}
		if (status)
			printf("script exited with status %d\n", status);
	}

	return 0;
}

static int
installer_init(struct installer *installer)
{
	unsigned char *gz_header;
	int method, flags, err;

	gz_header = installer->rpm->payload;
	if (gz_header[0] != 0x1f || gz_header[1] != 0x8b) {
		fprintf(stderr, "payload section doesn't have gz header\n");
		return -1;
	}

	method = gz_header[2];
	flags = gz_header[3];

	if (method != Z_DEFLATED || flags != 0) {
		fprintf(stderr,
			"unknown payload compression method or flags set\n");
		return -1;
	}

	installer->stream.zalloc = NULL;
	installer->stream.zfree = NULL;
	installer->stream.opaque = NULL;

	installer->stream.next_in  = gz_header + 10;
	installer->stream.avail_in =
		(installer->rpm->map + installer->rpm->size) -
		(void *) installer->stream.next_in;
	installer->stream.next_out = NULL;
	installer->stream.avail_out = 0;

	err = inflateInit2(&installer->stream, -MAX_WBITS);
	if (err != Z_OK) {
		fprintf(stderr, "inflateInit error: %d\n", err);
		return -1;
	}

	return 0;
}

static int
installer_finish(struct installer *installer)
{
	int err;

	err = inflateEnd(&installer->stream);

	if (err != Z_OK) {
		fprintf(stderr, "inflateEnd error: %d\n", err);
		return -1;
	}

	return 0;
}

static unsigned long
fixed_hex_to_ulong(const char *hex, int length)
{
	long l;
	int i;

	for (i = 0, l = 0; i < length; i++) {
		if (hex[i] < 'a')
			l = l * 16 + hex[i] - '0';
		else
			l = l * 16 + hex[i] - 'a' + 10;
	}

	return l;
}

int
razor_rpm_install(struct razor_rpm *rpm, const char *root)
{
	struct installer installer;
	struct cpio_file_header *header;
	struct stat buf;
	unsigned int mode;
	char *path;
	size_t filesize;

	installer.rpm = rpm;
	installer.root = root;

	/* FIXME: Only do this before a transaction, not per rpm. */
	if (stat(root, &buf) < 0 || !S_ISDIR(buf.st_mode)) {
		fprintf(stderr,
			"root installation directory \"%s\" does not exist\n",
			root);
		return -1;
	}

	if (installer_init(&installer))
		return -1;

	run_script(&installer, RPMTAG_PREINPROG, RPMTAG_PREIN);

	while (installer.stream.avail_in > 0) {
		installer.rest = sizeof *header;
		if (installer_inflate(&installer))
			return -1;

		header = (struct cpio_file_header *) installer.buffer;
		mode = fixed_hex_to_ulong(header->mode, sizeof header->mode);
		filesize = fixed_hex_to_ulong(header->filesize,
					      sizeof header->filesize);

		installer.rest = fixed_hex_to_ulong(header->namesize,
						    sizeof header->namesize);

		if (installer_inflate(&installer) ||
		    installer_align(&installer, 4))
			return -1;

		path = (char *) installer.buffer;
		/* This convention is so lame... */
		if (strcmp(path, "TRAILER!!!") == 0)
			break;

		installer.rest = filesize;
		if (create_path(&installer, path + 1, mode) < 0)
			return -1;
		if (installer_align(&installer, 4))
			return -1;
	}

	if (installer_finish(&installer))
		return -1;

	run_script(&installer, RPMTAG_POSTINPROG, RPMTAG_POSTIN);

	return 0;
}

int
razor_rpm_close(struct razor_rpm *rpm)
{
	int err;

	free(rpm->dirs);
	err = munmap(rpm->map, rpm->size);
	free(rpm);

	return err;
}

int
razor_importer_add_rpm(struct razor_importer *importer, struct razor_rpm *rpm)
{
	const char *name, *version, *release, *arch, *summary;
	const uint32_t *epoch;
	char evr[128], buf[16];

	name = razor_rpm_get_indirect(rpm, RPMTAG_NAME, NULL);
	epoch = razor_rpm_get_indirect(rpm, RPMTAG_EPOCH, NULL);
	version = razor_rpm_get_indirect(rpm, RPMTAG_VERSION, NULL);
	release = razor_rpm_get_indirect(rpm, RPMTAG_RELEASE, NULL);
	arch = razor_rpm_get_indirect(rpm, RPMTAG_ARCH, NULL);
	summary = razor_rpm_get_indirect(rpm, RPMTAG_SUMMARY, NULL);

	if (epoch) {
		snprintf(buf, sizeof buf, "%u", ntohl(*epoch));
		razor_build_evr(evr, sizeof evr, buf, version, release);
	} else {
		razor_build_evr(evr, sizeof evr, NULL, version, release);
	}
	razor_importer_begin_package(importer, name, evr, arch);

	import_properties(importer, RAZOR_PROPERTY_REQUIRES, rpm,
			  RPMTAG_REQUIRENAME,
			  RPMTAG_REQUIREVERSION,
			  RPMTAG_REQUIREFLAGS);

	import_properties(importer, RAZOR_PROPERTY_PROVIDES, rpm,
			  RPMTAG_PROVIDENAME,
			  RPMTAG_PROVIDEVERSION,
			  RPMTAG_PROVIDEFLAGS);

	import_properties(importer, RAZOR_PROPERTY_OBSOLETES, rpm,
			  RPMTAG_OBSOLETENAME,
			  RPMTAG_OBSOLETEVERSION,
			  RPMTAG_OBSOLETEFLAGS);

	import_properties(importer, RAZOR_PROPERTY_CONFLICTS, rpm,
			  RPMTAG_CONFLICTNAME,
			  RPMTAG_CONFLICTVERSION,
			  RPMTAG_CONFLICTFLAGS);

	import_files(importer, rpm);

	razor_importer_finish_package(importer);

	return 0;
}