rpm.c
author Kristian H?gsberg <krh@redhat.com>
Fri Nov 16 17:14:51 2007 -0500 (2007-11-16)
changeset 83 43ef0eb1b603
parent 75 93278d8ec39c
child 84 6883222205ad
permissions -rw-r--r--
Fix file tree traversal to not skip the last entry.
krh@74
     1
#include <stdio.h>
krh@74
     2
#include <string.h>
krh@74
     3
#include <sys/stat.h>
krh@74
     4
#include <sys/mman.h>
krh@74
     5
#include <fcntl.h>
krh@74
     6
#include <unistd.h>
krh@74
     7
#include <arpa/inet.h>
krh@74
     8
#include <rpm/rpmlib.h>
krh@77
     9
#include <zlib.h>
krh@74
    10
krh@75
    11
#include "razor.h"
krh@75
    12
krh@74
    13
#define	RPM_LEAD_SIZE 96
krh@74
    14
krh@74
    15
struct rpm_lead {
krh@74
    16
	unsigned char magic[4];
krh@74
    17
	unsigned char major;
krh@74
    18
	unsigned char minor;
krh@74
    19
	short type;
krh@74
    20
	short archnum;
krh@74
    21
	char name[66];
krh@74
    22
	short osnum;
krh@74
    23
	short signature_type;
krh@74
    24
	char reserved[16];
krh@74
    25
};
krh@74
    26
krh@74
    27
struct rpm_header {
krh@74
    28
	unsigned char magic[4];
krh@74
    29
	unsigned char reserved[4];
krh@74
    30
	int nindex;
krh@74
    31
	int hsize;
krh@74
    32
};
krh@74
    33
krh@74
    34
struct rpm_header_index {
krh@74
    35
	int tag;
krh@74
    36
	int type;
krh@74
    37
	int offset;
krh@74
    38
	int count;
krh@74
    39
};
krh@74
    40
krh@75
    41
struct properties {
krh@75
    42
	struct rpm_header_index *name;
krh@75
    43
	struct rpm_header_index *version;
krh@75
    44
	struct rpm_header_index *flags;
krh@75
    45
};
krh@75
    46
krh@77
    47
struct razor_rpm {
krh@75
    48
	struct rpm_header *signature;
krh@75
    49
	struct rpm_header *header;
krh@75
    50
krh@75
    51
	struct rpm_header_index *name;
krh@75
    52
	struct rpm_header_index *version;
krh@75
    53
	struct rpm_header_index *release;
krh@75
    54
krh@75
    55
	struct rpm_header_index *dirnames;
krh@75
    56
	struct rpm_header_index *dirindexes;
krh@75
    57
	struct rpm_header_index *basenames;
krh@77
    58
	struct rpm_header_index *filesizes;
krh@77
    59
	struct rpm_header_index *filemodes;
krh@77
    60
	struct rpm_header_index *filestates;
krh@77
    61
	const char **dirs;
krh@75
    62
krh@75
    63
	struct properties provides;
krh@75
    64
	struct properties requires;
krh@75
    65
	struct properties obsoletes;
krh@75
    66
	struct properties conflicts;
krh@75
    67
krh@75
    68
	const char *pool;
krh@75
    69
	void *map;
krh@75
    70
	size_t size;
krh@77
    71
	void *payload;
krh@75
    72
};
krh@75
    73
krh@74
    74
#define ALIGN(value, base) (((value) + (base - 1)) & ~((base) - 1))
krh@74
    75
krh@75
    76
static void
krh@75
    77
import_properties(struct razor_importer *importer,
krh@75
    78
		  struct properties *properties,
krh@75
    79
		  const char *pool, unsigned long type)
krh@75
    80
{
krh@75
    81
	const char *name, *version;
krh@75
    82
	int i, count;
krh@75
    83
krh@75
    84
	/* assert: count is the same for all arrays */
krh@75
    85
krh@75
    86
	if (properties->name == NULL)
krh@75
    87
		return;
krh@75
    88
krh@75
    89
	count = ntohl(properties->name->count);
krh@75
    90
	name = pool + ntohl(properties->name->offset);
krh@75
    91
	version = pool + ntohl(properties->version->offset);
krh@75
    92
	for (i = 0; i < count; i++) {
krh@75
    93
		razor_importer_add_property(importer, name, version, type);
krh@75
    94
		name += strlen(name) + 1;
krh@75
    95
		version += strlen(version) + 1;
krh@75
    96
	}
krh@75
    97
}
krh@75
    98
krh@75
    99
static void
krh@77
   100
import_files(struct razor_importer *importer, struct razor_rpm *rpm)
krh@75
   101
{
krh@77
   102
	const char *name;
krh@75
   103
	unsigned long *index;
krh@75
   104
	int i, count;
krh@75
   105
	char buffer[256];
krh@75
   106
krh@75
   107
	/* assert: count is the same for all arrays */
krh@75
   108
krh@75
   109
	if (rpm->dirnames == NULL)
krh@75
   110
		return;
krh@75
   111
krh@75
   112
	count = ntohl(rpm->basenames->count);
krh@75
   113
	index = (unsigned long *) (rpm->pool + ntohl(rpm->dirindexes->offset));
krh@75
   114
	name = rpm->pool + ntohl(rpm->basenames->offset);
krh@75
   115
	for (i = 0; i < count; i++) {
krh@75
   116
		snprintf(buffer, sizeof buffer,
krh@77
   117
			 "%s%s", rpm->dirs[ntohl(*index)], name);
krh@75
   118
		razor_importer_add_file(importer, buffer);
krh@75
   119
		name += strlen(name) + 1;
krh@75
   120
		index++;
krh@75
   121
	}
krh@75
   122
}
krh@75
   123
krh@77
   124
struct razor_rpm *
krh@77
   125
razor_rpm_open(const char *filename)
krh@74
   126
{
krh@77
   127
	struct razor_rpm *rpm;
krh@74
   128
	struct rpm_header_index *base, *index;
krh@75
   129
	struct stat buf;
krh@77
   130
	int fd, nindex, hsize, i, count;
krh@77
   131
	const char *name;
krh@74
   132
krh@77
   133
	rpm = malloc(sizeof *rpm);
krh@75
   134
	memset(rpm, 0, sizeof *rpm);
krh@75
   135
	if (stat(filename, &buf) < 0) {
krh@75
   136
		fprintf(stderr, "no such file %s (%m)\n", filename);
krh@77
   137
		return NULL;
krh@75
   138
	}
krh@74
   139
krh@75
   140
	fd = open(filename, O_RDONLY);
krh@75
   141
	if (fd < 0) {
krh@75
   142
		fprintf(stderr, "couldn't open %s\n", filename);
krh@77
   143
		return NULL;
krh@75
   144
	}
krh@75
   145
	rpm->size = buf.st_size;
krh@75
   146
	rpm->map = mmap(NULL, rpm->size, PROT_READ, MAP_PRIVATE, fd, 0);
krh@75
   147
	if (rpm->map == MAP_FAILED) {
krh@75
   148
		fprintf(stderr, "couldn't mmap %s\n", filename);
krh@77
   149
		return NULL;
krh@75
   150
	}
krh@75
   151
	close(fd);
krh@75
   152
krh@75
   153
	rpm->signature = rpm->map + RPM_LEAD_SIZE;
krh@75
   154
	nindex = ntohl(rpm->signature->nindex);
krh@75
   155
	hsize = ntohl(rpm->signature->hsize);
krh@75
   156
	rpm->header = (void *) (rpm->signature + 1) +
krh@75
   157
		ALIGN(nindex * sizeof *index + hsize, 8);
krh@77
   158
	nindex = ntohl(rpm->header->nindex);
krh@77
   159
	hsize = ntohl(rpm->header->hsize);
krh@77
   160
	rpm->payload = (void *) (rpm->header + 1) +
krh@77
   161
		nindex * sizeof *index + hsize;
krh@75
   162
krh@75
   163
	base = (struct rpm_header_index *) (rpm->header + 1);
krh@75
   164
	rpm->pool = (void *) base + nindex * sizeof *index;
krh@75
   165
krh@74
   166
	for (i = 0; i < nindex; i++) {
krh@74
   167
		index = base + i;
krh@75
   168
		switch (ntohl(index->tag)) {
krh@74
   169
		case RPMTAG_NAME:
krh@75
   170
			rpm->name = index;
krh@74
   171
			break;
krh@74
   172
		case RPMTAG_VERSION:
krh@75
   173
			rpm->version = index;
krh@74
   174
			break;
krh@74
   175
		case RPMTAG_RELEASE:
krh@75
   176
			rpm->release = index;
krh@74
   177
			break;
krh@75
   178
krh@74
   179
		case RPMTAG_REQUIRENAME:
krh@75
   180
			rpm->requires.name = index;
krh@74
   181
			break;
krh@75
   182
		case RPMTAG_REQUIREVERSION:
krh@75
   183
			rpm->requires.version = index;
krh@74
   184
			break;
krh@75
   185
		case RPMTAG_REQUIREFLAGS:
krh@75
   186
			rpm->requires.flags = index;
krh@75
   187
			break;
krh@74
   188
krh@75
   189
		case RPMTAG_PROVIDENAME:
krh@75
   190
			rpm->provides.name = index;
krh@74
   191
			break;
krh@75
   192
		case RPMTAG_PROVIDEVERSION:
krh@75
   193
			rpm->provides.version = index;
krh@75
   194
			break;
krh@75
   195
		case RPMTAG_PROVIDEFLAGS:
krh@75
   196
			rpm->provides.flags = index;
krh@75
   197
			break;
krh@75
   198
krh@75
   199
		case RPMTAG_OBSOLETENAME:
krh@75
   200
			rpm->obsoletes.name = index;
krh@75
   201
			break;
krh@75
   202
		case RPMTAG_OBSOLETEVERSION:
krh@75
   203
			rpm->obsoletes.version = index;
krh@75
   204
			break;
krh@75
   205
		case RPMTAG_OBSOLETEFLAGS:
krh@75
   206
			rpm->obsoletes.flags = index;
krh@75
   207
			break;
krh@75
   208
krh@75
   209
		case RPMTAG_CONFLICTNAME:
krh@75
   210
			rpm->conflicts.name = index;
krh@75
   211
			break;
krh@75
   212
		case RPMTAG_CONFLICTVERSION:
krh@75
   213
			rpm->conflicts.version = index;
krh@75
   214
			break;
krh@75
   215
		case RPMTAG_CONFLICTFLAGS:
krh@75
   216
			rpm->conflicts.flags = index;
krh@75
   217
			break;
krh@75
   218
krh@75
   219
		case RPMTAG_DIRINDEXES:
krh@75
   220
			rpm->dirindexes = index;
krh@75
   221
			break;
krh@75
   222
		case RPMTAG_BASENAMES:
krh@75
   223
			rpm->basenames = index;
krh@75
   224
			break;
krh@75
   225
		case RPMTAG_DIRNAMES:
krh@75
   226
			rpm->dirnames = index;
krh@74
   227
			break;
krh@77
   228
		case RPMTAG_FILESIZES:
krh@77
   229
			rpm->filesizes = index;
krh@77
   230
			break;
krh@77
   231
		case RPMTAG_FILEMODES:
krh@77
   232
			rpm->filemodes = index;
krh@77
   233
			break;
krh@77
   234
		case RPMTAG_FILESTATES:
krh@77
   235
			rpm->filestates = index;
krh@77
   236
			break;
krh@74
   237
		}
krh@74
   238
	}
krh@75
   239
krh@77
   240
	/* Look up dir names now so we can index them directly. */
krh@77
   241
	if (rpm->dirnames != NULL) {
krh@77
   242
		count = ntohl(rpm->dirnames->count);
krh@77
   243
		rpm->dirs = calloc(count, sizeof *rpm->dirs);
krh@77
   244
		name = rpm->pool + ntohl(rpm->dirnames->offset);
krh@77
   245
		for (i = 0; i < count; i++) {
krh@77
   246
			rpm->dirs[i] = name;
krh@77
   247
			name += strlen(name) + 1;
krh@77
   248
		}
krh@77
   249
	}
krh@77
   250
krh@77
   251
	return rpm;
krh@77
   252
}
krh@77
   253
krh@77
   254
struct cpio_file_header {
krh@77
   255
	char magic[6];
krh@77
   256
	char inode[8];
krh@77
   257
	char mode[8];
krh@77
   258
	char uid[8];
krh@77
   259
	char gid[8];
krh@77
   260
	char nlink[8];
krh@77
   261
	char mtime[8];
krh@77
   262
	char filesize[8];
krh@77
   263
	char devmajor[8];
krh@77
   264
	char devminor[8];
krh@77
   265
	char rdevmajor[8];
krh@77
   266
	char rdevminor[8];
krh@77
   267
	char namesize[8];
krh@77
   268
	char checksum[8];
krh@77
   269
	char filename[0];
krh@77
   270
};
krh@77
   271
krh@77
   272
/* gzip flags */
krh@77
   273
#define ASCII_FLAG   0x01 /* bit 0 set: file probably ascii text */
krh@77
   274
#define HEAD_CRC     0x02 /* bit 1 set: header CRC present */
krh@77
   275
#define EXTRA_FIELD  0x04 /* bit 2 set: extra field present */
krh@77
   276
#define ORIG_NAME    0x08 /* bit 3 set: original file name present */
krh@77
   277
#define COMMENT      0x10 /* bit 4 set: file comment present */
krh@77
   278
#define RESERVED     0xE0 /* bits 5..7: reserved */
krh@77
   279
krh@77
   280
static int
krh@77
   281
create_path(const char *root, const char *path,
krh@77
   282
	    const char *name, unsigned mode, int *fd)
krh@77
   283
{
krh@77
   284
	char buffer[256], *p;
krh@77
   285
	const char *slash, *next;
krh@77
   286
	struct stat buf;
krh@77
   287
krh@77
   288
	/* Create all sub-directories in dir and then create name. We
krh@77
   289
	 * know root exists and is a dir, root does not end in a '/',
krh@77
   290
	 * and path has a leading '/'. */
krh@77
   291
krh@77
   292
	strcpy(buffer, root);
krh@77
   293
	p = buffer + strlen(buffer);
krh@77
   294
	slash = path;
krh@77
   295
	for (slash = path; slash[1] != '\0'; slash = next) {
krh@77
   296
		next = strchr(slash + 1, '/');
krh@77
   297
		memcpy(p, slash, next - slash);
krh@77
   298
		p += next - slash;
krh@77
   299
		*p = '\0';
krh@77
   300
krh@77
   301
		if (stat(buffer, &buf) == 0) {
krh@77
   302
			if (!S_ISDIR(buf.st_mode)) {
krh@77
   303
				fprintf(stderr,
krh@77
   304
					"%s exists but is not a directory\n",
krh@77
   305
					buffer);
krh@77
   306
				return -1;
krh@77
   307
			}
krh@77
   308
		} else if (mkdir(buffer, 0777) < 0) {
krh@77
   309
			fprintf(stderr, "failed to make directory %s: %m\n",
krh@77
   310
				buffer);
krh@77
   311
			return -1;
krh@77
   312
		}
krh@77
   313
		/* FIXME: permissions */
krh@77
   314
	}
krh@77
   315
krh@77
   316
	*p++ = '/';
krh@77
   317
	strcpy(p, name);
krh@77
   318
krh@77
   319
	switch (mode >> 12) {
krh@77
   320
	case REG:
krh@77
   321
	default:
krh@77
   322
		*fd = open(buffer, O_WRONLY | O_CREAT | O_TRUNC, mode & 0x1ff);
krh@77
   323
		return *fd;
krh@77
   324
	case XDIR:
krh@77
   325
		*fd = -1;
krh@77
   326
		return mkdir(buffer, mode & 0x1ff);
krh@77
   327
	}
krh@77
   328
}
krh@77
   329
krh@77
   330
int
krh@77
   331
razor_rpm_install(struct razor_rpm *rpm, const char *root)
krh@77
   332
{
krh@77
   333
	z_stream stream;
krh@77
   334
	unsigned char payload[32768], *gz_header;
krh@77
   335
	char buffer[256];
krh@77
   336
	int err, method, flags, count, i, fd, written;
krh@77
   337
	struct cpio_file_header *header;
krh@77
   338
	unsigned long *size, *index, rest, length;
krh@77
   339
	unsigned short *mode;
krh@77
   340
	const char *name, *dir;
krh@77
   341
	struct stat buf;
krh@77
   342
krh@77
   343
	if (stat(root, &buf) < 0 || !S_ISDIR(buf.st_mode)) {
krh@77
   344
		fprintf(stderr,
krh@77
   345
			"root installation directory \"%s\" does not exist\n",
krh@77
   346
			root);
krh@77
   347
		return -1;
krh@77
   348
	}
krh@77
   349
krh@77
   350
	gz_header = rpm->payload;
krh@77
   351
	if (gz_header[0] != 0x1f || gz_header[1] != 0x8b) {
krh@77
   352
		fprintf(stderr, "payload section doesn't have gz header\n");
krh@77
   353
		return -1;
krh@77
   354
	}
krh@77
   355
krh@77
   356
	method = gz_header[2];
krh@77
   357
	flags = gz_header[3];
krh@77
   358
krh@77
   359
	if (method != Z_DEFLATED || flags != 0) {
krh@77
   360
		fprintf(stderr,
krh@77
   361
			"unknown payload compression method or flags set\n");
krh@77
   362
		return -1;
krh@77
   363
	}
krh@77
   364
krh@77
   365
	stream.zalloc = NULL;
krh@77
   366
	stream.zfree = NULL;
krh@77
   367
	stream.opaque = NULL;
krh@77
   368
krh@77
   369
	stream.next_in  = gz_header + 10;
krh@77
   370
	stream.avail_in = (rpm->map + rpm->size) - (void *) stream.next_in;
krh@77
   371
	stream.next_out = NULL;
krh@77
   372
	stream.avail_out = 0;
krh@77
   373
krh@77
   374
	err = inflateInit2(&stream, -MAX_WBITS);
krh@77
   375
	if (err != Z_OK) {
krh@77
   376
		fprintf(stderr, "inflateInit error: %d\n", err);
krh@77
   377
		return -1;
krh@77
   378
	}
krh@77
   379
krh@77
   380
	count = ntohl(rpm->basenames->count);
krh@77
   381
	size = (unsigned long *) (rpm->pool + ntohl(rpm->filesizes->offset));
krh@77
   382
	index = (unsigned long *) (rpm->pool + ntohl(rpm->dirindexes->offset));
krh@77
   383
	mode = (unsigned short *) (rpm->pool + ntohl(rpm->filemodes->offset));
krh@77
   384
	name = rpm->pool + ntohl(rpm->basenames->offset);
krh@77
   385
	for (i = 0; i < count; i++) {
krh@77
   386
		dir = rpm->dirs[ntohl(*index)];
krh@77
   387
		snprintf(buffer, sizeof buffer, "%s%s", dir, name);
krh@77
   388
krh@77
   389
		stream.next_out = payload;
krh@77
   390
		/* Plus two for the leading '.' and the terminating NUL. */
krh@77
   391
		stream.avail_out =
krh@77
   392
			ALIGN(sizeof *header + strlen(buffer) + 2, 4);
krh@77
   393
		err = inflate(&stream, Z_SYNC_FLUSH);
krh@77
   394
		if (err != Z_OK) {
krh@77
   395
			fprintf(stderr, "inflate error: %d\n", err);
krh@77
   396
			return -1;
krh@77
   397
		}
krh@77
   398
	    
krh@77
   399
		header = (struct cpio_file_header *) payload;
krh@77
   400
krh@77
   401
		/* FIXME: Figure out if it's a symlink, device file,
krh@77
   402
		 * directorys or whatever.  Maybe do this upfront. */
krh@77
   403
		if (create_path(root, dir, name, ntohs(*mode), &fd) < 0)
krh@77
   404
			return -1;
krh@77
   405
		if (ntohs(*mode) >> 12 == XDIR)
krh@77
   406
			rest = 0;
krh@77
   407
		else
krh@77
   408
			rest = ntohl(*size);
krh@77
   409
krh@77
   410
		while (rest > 0) {
krh@77
   411
			if (ALIGN(rest, 4) > sizeof payload)
krh@77
   412
				length = sizeof payload;
krh@77
   413
			else
krh@77
   414
				length = rest;
krh@77
   415
			stream.next_out = payload;
krh@77
   416
			stream.avail_out = ALIGN(length, 4);
krh@77
   417
			err = inflate(&stream, Z_SYNC_FLUSH);
krh@77
   418
			if (err != Z_OK && err != Z_STREAM_END) {
krh@77
   419
				fprintf(stderr,
krh@77
   420
					"inflate error: %d (%m)\n", err);
krh@77
   421
				return -1;
krh@77
   422
			}
krh@77
   423
			rest -= length;
krh@77
   424
			stream.next_out = payload;
krh@77
   425
			while (length > 0) {
krh@77
   426
				written = write(fd, stream.next_out, length);
krh@77
   427
				if (written < 0) {
krh@77
   428
					fprintf(stderr, "write error: %m\n");
krh@77
   429
					return -1;
krh@77
   430
				}
krh@77
   431
				length -= written;
krh@77
   432
			}
krh@77
   433
		}
krh@77
   434
		if (fd > 0 && close(fd) < 0) {
krh@77
   435
			fprintf(stderr, "failed to close \"%s/%s%s\": %m\n",
krh@77
   436
				root, dir, name);
krh@77
   437
			return -1;
krh@77
   438
		}
krh@77
   439
		name += strlen(name) + 1;
krh@77
   440
		index++;
krh@77
   441
		size++;
krh@77
   442
		mode++;
krh@77
   443
	}
krh@77
   444
krh@77
   445
	err = inflateEnd(&stream);
krh@77
   446
krh@77
   447
	if (err != Z_OK) {
krh@77
   448
		fprintf(stderr, "inflateEnd error: %d\n", err);
krh@77
   449
		return -1;
krh@77
   450
	}	    
krh@77
   451
krh@75
   452
	return 0;
krh@74
   453
}
krh@74
   454
krh@77
   455
int
krh@77
   456
razor_rpm_close(struct razor_rpm *rpm)
krh@74
   457
{
krh@77
   458
	int err;
krh@77
   459
krh@77
   460
	free(rpm->dirs);
krh@77
   461
	err = munmap(rpm->map, rpm->size);
krh@77
   462
	free(rpm);
krh@77
   463
krh@77
   464
	return err;
krh@75
   465
}
krh@74
   466
krh@75
   467
int
krh@77
   468
razor_importer_add_rpm(struct razor_importer *importer, struct razor_rpm *rpm)
krh@75
   469
{
krh@77
   470
	razor_importer_begin_package(importer,
krh@77
   471
				     rpm->pool + ntohl(rpm->name->offset),
krh@77
   472
				     rpm->pool + ntohl(rpm->version->offset));
krh@75
   473
krh@77
   474
	import_properties(importer, &rpm->requires,
krh@77
   475
			  rpm->pool, RAZOR_PROPERTY_REQUIRES);
krh@77
   476
	import_properties(importer, &rpm->provides,
krh@77
   477
			  rpm->pool, RAZOR_PROPERTY_PROVIDES);
krh@77
   478
	import_properties(importer, &rpm->conflicts,
krh@77
   479
			  rpm->pool, RAZOR_PROPERTY_CONFLICTS);
krh@77
   480
	import_properties(importer, &rpm->obsoletes,
krh@77
   481
			  rpm->pool, RAZOR_PROPERTY_OBSOLETES);
krh@77
   482
	import_files(importer, rpm);
krh@74
   483
krh@75
   484
	razor_importer_finish_package(importer);
krh@74
   485
krh@75
   486
	return 0;
krh@74
   487
}