rpm.c
author Kristian H?gsberg <krh@redhat.com>
Sat Dec 29 15:36:13 2007 -0500 (2007-12-29)
changeset 87 c1e04d4e3bc9
parent 86 e44ccd213756
child 88 4c38558a4873
permissions -rw-r--r--
Drop table based section lookup and look up sections on demand.
     1 #include <stdio.h>
     2 #include <stddef.h>
     3 #include <string.h>
     4 #include <sys/stat.h>
     5 #include <sys/mman.h>
     6 #include <sys/types.h>
     7 #include <sys/wait.h>
     8 #include <fcntl.h>
     9 #include <unistd.h>
    10 #include <arpa/inet.h>
    11 #include <rpm/rpmlib.h>
    12 #include <zlib.h>
    13 
    14 #include "razor.h"
    15 
    16 #define	RPM_LEAD_SIZE 96
    17 
    18 struct rpm_header {
    19 	unsigned char magic[4];
    20 	unsigned char reserved[4];
    21 	int nindex;
    22 	int hsize;
    23 };
    24 
    25 struct rpm_header_index {
    26 	int tag;
    27 	int type;
    28 	int offset;
    29 	int count;
    30 };
    31 
    32 struct razor_rpm {
    33 	struct rpm_header *signature;
    34 	struct rpm_header *header;
    35 	const char **dirs;
    36 	const char *pool;
    37 	void *map;
    38 	size_t size;
    39 	void *payload;
    40 };
    41 
    42 #define ALIGN(value, base) (((value) + (base - 1)) & ~((base) - 1))
    43 
    44 static struct rpm_header_index *
    45 razor_rpm_get_header(struct razor_rpm *rpm, unsigned int tag)
    46 {
    47 	struct rpm_header_index *index, *end;
    48 
    49 	index = (struct rpm_header_index *) (rpm->header + 1);
    50 	end = index + ntohl(rpm->header->nindex);
    51 	while (index < end) {
    52 		if (ntohl(index->tag) == tag)
    53 			return index;
    54 		index++;
    55 	}
    56 
    57 	return NULL;
    58 }
    59 
    60 static const void *
    61 razor_rpm_get_indirect(struct razor_rpm *rpm,
    62 		       unsigned int tag, unsigned int *count)
    63 {
    64 	struct rpm_header_index *index;
    65 
    66 	index = razor_rpm_get_header(rpm, tag);
    67 	if (index != NULL) {
    68 		if (count)
    69 			*count = ntohl(index->count);
    70 
    71 		return rpm->pool + ntohl(index->offset);
    72 	}
    73 
    74 	return NULL;
    75 }
    76 
    77 static void
    78 import_properties(struct razor_importer *importer, unsigned long type,
    79 		  struct razor_rpm *rpm,
    80 		  int name_tag, int version_tag, int flags_tag)
    81 {
    82 	const char *name, *version;
    83 	unsigned int i, count;
    84 
    85 	name = razor_rpm_get_indirect(rpm, name_tag, &count);
    86 	if (name == NULL)
    87 		return;
    88 
    89 	/* FIXME: Concat version and release. */
    90 	version = razor_rpm_get_indirect(rpm, version_tag, &count);
    91 	for (i = 0; i < count; i++) {
    92 		razor_importer_add_property(importer, name, version, type);
    93 		name += strlen(name) + 1;
    94 		version += strlen(version) + 1;
    95 	}
    96 }
    97 
    98 static void
    99 import_files(struct razor_importer *importer, struct razor_rpm *rpm)
   100 {
   101 	const char *name;
   102 	const unsigned long *index;
   103 	unsigned int i, count;
   104 	char buffer[256];
   105 
   106 	/* assert: count is the same for all arrays */
   107 
   108 	index = razor_rpm_get_indirect(rpm, RPMTAG_DIRINDEXES, &count);
   109 	name = razor_rpm_get_indirect(rpm, RPMTAG_BASENAMES, &count);
   110 	for (i = 0; i < count; i++) {
   111 		snprintf(buffer, sizeof buffer,
   112 			 "%s%s", rpm->dirs[ntohl(*index)], name);
   113 		razor_importer_add_file(importer, buffer);
   114 		name += strlen(name) + 1;
   115 		index++;
   116 	}
   117 }
   118 
   119 struct razor_rpm *
   120 razor_rpm_open(const char *filename)
   121 {
   122 	struct razor_rpm *rpm;
   123 	struct rpm_header_index *base, *index;
   124 	struct stat buf;
   125 	unsigned int count, i, nindex, hsize;
   126 	const char *name;
   127 	int fd;
   128 
   129 	rpm = malloc(sizeof *rpm);
   130 	memset(rpm, 0, sizeof *rpm);
   131 	if (stat(filename, &buf) < 0) {
   132 		fprintf(stderr, "no such file %s (%m)\n", filename);
   133 		return NULL;
   134 	}
   135 
   136 	fd = open(filename, O_RDONLY);
   137 	if (fd < 0) {
   138 		fprintf(stderr, "couldn't open %s\n", filename);
   139 		return NULL;
   140 	}
   141 	rpm->size = buf.st_size;
   142 	rpm->map = mmap(NULL, rpm->size, PROT_READ, MAP_PRIVATE, fd, 0);
   143 	if (rpm->map == MAP_FAILED) {
   144 		fprintf(stderr, "couldn't mmap %s\n", filename);
   145 		return NULL;
   146 	}
   147 	close(fd);
   148 
   149 	rpm->signature = rpm->map + RPM_LEAD_SIZE;
   150 	nindex = ntohl(rpm->signature->nindex);
   151 	hsize = ntohl(rpm->signature->hsize);
   152 	rpm->header = (void *) (rpm->signature + 1) +
   153 		ALIGN(nindex * sizeof *index + hsize, 8);
   154 	nindex = ntohl(rpm->header->nindex);
   155 	hsize = ntohl(rpm->header->hsize);
   156 	rpm->payload = (void *) (rpm->header + 1) +
   157 		nindex * sizeof *index + hsize;
   158 
   159 	base = (struct rpm_header_index *) (rpm->header + 1);
   160 	rpm->pool = (void *) base + nindex * sizeof *index;
   161 
   162 	/* Look up dir names now so we can index them directly. */
   163 	name = razor_rpm_get_indirect(rpm, RPMTAG_DIRNAMES, &count);
   164 	if (name == NULL) {
   165 		fprintf(stderr, "old filename style not handled\n");
   166 		return NULL;
   167 	}
   168 
   169 	rpm->dirs = calloc(count, sizeof *rpm->dirs);
   170 	for (i = 0; i < count; i++) {
   171 		rpm->dirs[i] = name;
   172 		name += strlen(name) + 1;
   173 	}
   174 
   175 	return rpm;
   176 }
   177 
   178 struct cpio_file_header {
   179 	char magic[6];
   180 	char inode[8];
   181 	char mode[8];
   182 	char uid[8];
   183 	char gid[8];
   184 	char nlink[8];
   185 	char mtime[8];
   186 	char filesize[8];
   187 	char devmajor[8];
   188 	char devminor[8];
   189 	char rdevmajor[8];
   190 	char rdevminor[8];
   191 	char namesize[8];
   192 	char checksum[8];
   193 	char filename[0];
   194 };
   195 
   196 /* gzip flags */
   197 #define ASCII_FLAG   0x01 /* bit 0 set: file probably ascii text */
   198 #define HEAD_CRC     0x02 /* bit 1 set: header CRC present */
   199 #define EXTRA_FIELD  0x04 /* bit 2 set: extra field present */
   200 #define ORIG_NAME    0x08 /* bit 3 set: original file name present */
   201 #define COMMENT      0x10 /* bit 4 set: file comment present */
   202 #define RESERVED     0xE0 /* bits 5..7: reserved */
   203 
   204 struct installer {
   205 	const char *root;
   206 	struct razor_rpm *rpm;
   207 	z_stream stream;
   208 	unsigned char buffer[32768];
   209 	size_t rest, length;
   210 };
   211 
   212 static int
   213 installer_inflate(struct installer *installer)
   214 {
   215 	size_t length;
   216 	int err;
   217 
   218 	if (ALIGN(installer->rest, 4) > sizeof installer->buffer)
   219 		length = sizeof installer->buffer;
   220 	else
   221 		length = installer->rest;
   222 
   223 	installer->stream.next_out = installer->buffer;
   224 	installer->stream.avail_out = ALIGN(length, 4);
   225 	err = inflate(&installer->stream, Z_SYNC_FLUSH);
   226 	if (err != Z_OK && err != Z_STREAM_END) {
   227 		fprintf(stderr, "inflate error: %d (%m)\n", err);
   228 		return -1;
   229 	}
   230 
   231 	installer->rest -= length;
   232 	installer->length = length;
   233 
   234 	return 0;
   235 }
   236 
   237 static int
   238 xwrite(int fd, const void *data, size_t size)
   239 {
   240 	size_t rest;
   241 	ssize_t written;
   242 	const unsigned char *p;
   243 
   244 	rest = size;
   245 	p = data;
   246 	while (rest > 0) {
   247 		written = write(fd, p, rest);
   248 		if (written < 0) {
   249 			fprintf(stderr, "write error: %m\n");
   250 			return -1;
   251 		}
   252 		rest -= written;
   253 		p += written;
   254 	}
   255 
   256 	return 0;
   257 }
   258 
   259 static int
   260 create_path(struct installer *installer,
   261 	    const char *path, const char *name, unsigned int mode)
   262 {
   263 	char buffer[256], *p;
   264 	const char *slash, *next;
   265 	struct stat buf;
   266 	int fd;
   267 
   268 	/* Create all sub-directories in dir and then create name. We
   269 	 * know root exists and is a dir, root does not end in a '/',
   270 	 * and path has a leading '/'. */
   271 
   272 	strcpy(buffer, installer->root);
   273 	p = buffer + strlen(buffer);
   274 	slash = path;
   275 	for (slash = path; slash[1] != '\0'; slash = next) {
   276 		next = strchr(slash + 1, '/');
   277 		memcpy(p, slash, next - slash);
   278 		p += next - slash;
   279 		*p = '\0';
   280 
   281 		if (stat(buffer, &buf) == 0) {
   282 			if (!S_ISDIR(buf.st_mode)) {
   283 				fprintf(stderr,
   284 					"%s exists but is not a directory\n",
   285 					buffer);
   286 				return -1;
   287 			}
   288 		} else if (mkdir(buffer, 0777) < 0) {
   289 			fprintf(stderr, "failed to make directory %s: %m\n",
   290 				buffer);
   291 			return -1;
   292 		}
   293 		/* FIXME: permissions */
   294 	}
   295 
   296 	*p++ = '/';
   297 	strcpy(p, name);
   298 
   299 	switch (mode >> 12) {
   300 	case REG:
   301 		fd = open(buffer, O_WRONLY | O_CREAT | O_TRUNC, mode & 0x1ff);
   302 		if (fd < 0){
   303 			fprintf(stderr, "failed to create file %s\n", buffer);
   304 			return -1;
   305 		}
   306 		while (installer->rest > 0) {
   307 			if (installer_inflate(installer)) {
   308 				fprintf(stderr, "failed to inflate\n");
   309 				return -1;
   310 			}
   311 			if (xwrite(fd, installer->buffer, installer->length)) {
   312 				fprintf(stderr, "failed to write payload\n");
   313 				return -1;
   314 			}
   315 		}
   316 		if (close(fd) < 0) {
   317 			fprintf(stderr, "failed to close %s: %m\n", buffer);
   318 			return -1;
   319 		}
   320 		return 0;
   321 	case XDIR:
   322 		return mkdir(buffer, mode & 0x1ff);
   323 	case PIPE:
   324 	case CDEV:
   325 	case BDEV:
   326 	case SOCK:
   327 		printf("%s: unhandled file type %d\n", buffer, mode >> 12);
   328 		return 0;
   329 	case LINK:
   330 		if (installer_inflate(installer)) {
   331 			fprintf(stderr, "failed to inflate\n");
   332 			return -1;
   333 		}
   334 		if (installer->length >= sizeof installer->buffer) {
   335 			fprintf(stderr, "link name too long\n");
   336 			return -1;
   337 		}
   338 		installer->buffer[installer->length] = '\0';
   339 		if (symlink((const char *) installer->buffer, buffer)) {
   340 			fprintf(stderr, "failed to create symlink, %m\n");
   341 			return -1;
   342 		}
   343 		return 0;
   344 	default:
   345 		printf("%s: unknown file type %d\n", buffer, mode >> 12);
   346 		return 0;
   347 	}
   348 }
   349 
   350 static int
   351 run_script(struct installer *installer,
   352 	   unsigned int program_tag, unsigned int script_tag)
   353 {
   354 	int pid, status, fd[2];
   355 	const char *script = NULL, *program = NULL;
   356 
   357 	program = razor_rpm_get_indirect(installer->rpm, program_tag, NULL);
   358 	script = razor_rpm_get_indirect(installer->rpm, script_tag, NULL);
   359 	if (program == NULL && script == NULL) {
   360 		printf("no script or program for tags %d and %d\n",
   361 		       program_tag, script_tag);
   362 		return -1;
   363 	} else if (program == NULL) {
   364 		program = "/bin/sh";
   365 	}
   366 
   367 	if (pipe(fd) < 0) {
   368 		fprintf(stderr, "failed to create pipe\n");
   369 		return -1;
   370 	}
   371 	pid = fork();
   372 	if (pid < 0) {
   373 		fprintf(stderr, "failed to fork, %m\n");
   374 	} else if (pid == 0) {
   375 		if (dup2(fd[0], STDIN_FILENO) < 0) {
   376 			fprintf(stderr, "failed redirect stdin, %m\n");
   377 			return -1;
   378 		}
   379 		if (close(fd[0]) < 0 || close(fd[1]) < 0) {
   380 			fprintf(stderr, "failed to close pipe, %m\n");
   381 			exit(-1);
   382 		}
   383 		if (chroot(installer->root) < 0) {
   384 			fprintf(stderr, "failed to chroot to %s, %m\n",
   385 				installer->root);
   386 			return -1;
   387 		}
   388 		printf("executing program %s in chroot %s\n",
   389 		       program, installer->root);
   390 		if (execl(program, program, NULL)) {
   391 			fprintf(stderr, "failed to exec %s, %m\n", program);
   392 			return -1;
   393 		}
   394 	} else {
   395 		if (script && write(fd[1], script, strlen(script)) < 0) {
   396 			fprintf(stderr, "failed to pipe script, %m\n");
   397 			return -1;
   398 		}
   399 		if (close(fd[0]) || close(fd[1])) {
   400 			fprintf(stderr, "failed to close pipe, %m\n");
   401 			return -1;
   402 		}
   403 		if (wait(&status) < 0) {
   404 			fprintf(stderr, "wait for child failed, %m");
   405 			return -1;
   406 		}
   407 		printf("script exited with status %d\n", status);
   408 	}
   409 
   410 	return 0;
   411 }
   412 
   413 static int
   414 installer_init(struct installer *installer)
   415 {
   416 	unsigned char *gz_header;
   417 	int method, flags, err;
   418 
   419 	gz_header = installer->rpm->payload;
   420 	if (gz_header[0] != 0x1f || gz_header[1] != 0x8b) {
   421 		fprintf(stderr, "payload section doesn't have gz header\n");
   422 		return -1;
   423 	}
   424 
   425 	method = gz_header[2];
   426 	flags = gz_header[3];
   427 
   428 	if (method != Z_DEFLATED || flags != 0) {
   429 		fprintf(stderr,
   430 			"unknown payload compression method or flags set\n");
   431 		return -1;
   432 	}
   433 
   434 	installer->stream.zalloc = NULL;
   435 	installer->stream.zfree = NULL;
   436 	installer->stream.opaque = NULL;
   437 
   438 	installer->stream.next_in  = gz_header + 10;
   439 	installer->stream.avail_in =
   440 		(installer->rpm->map + installer->rpm->size) -
   441 		(void *) installer->stream.next_in;
   442 	installer->stream.next_out = NULL;
   443 	installer->stream.avail_out = 0;
   444 
   445 	err = inflateInit2(&installer->stream, -MAX_WBITS);
   446 	if (err != Z_OK) {
   447 		fprintf(stderr, "inflateInit error: %d\n", err);
   448 		return -1;
   449 	}
   450 
   451 	return 0;
   452 }
   453 
   454 static int
   455 installer_finish(struct installer *installer)
   456 {
   457 	int err;
   458 
   459 	err = inflateEnd(&installer->stream);
   460 
   461 	if (err != Z_OK) {
   462 		fprintf(stderr, "inflateEnd error: %d\n", err);
   463 		return -1;
   464 	}	    
   465 
   466 	return 0;
   467 }
   468 
   469 int
   470 razor_rpm_install(struct razor_rpm *rpm, const char *root)
   471 {
   472 	struct installer installer;
   473 	unsigned int count, i, length;
   474 	struct cpio_file_header *header;
   475 	const unsigned long *size, *index, *flags;
   476 	const unsigned short *mode;
   477 	const char *name, *dir;
   478 	struct stat buf;
   479 
   480 	installer.rpm = rpm;
   481 	installer.root = root;
   482 
   483 	/* FIXME: Only do this before a transaction, not per rpm. */
   484 	if (stat(root, &buf) < 0 || !S_ISDIR(buf.st_mode)) {
   485 		fprintf(stderr,
   486 			"root installation directory \"%s\" does not exist\n",
   487 			root);
   488 		return -1;
   489 	}
   490 
   491 	if (installer_init(&installer))
   492 		return -1;
   493 
   494 	run_script(&installer, RPMTAG_PREINPROG, RPMTAG_PREIN);
   495 
   496 	name = razor_rpm_get_indirect(rpm, RPMTAG_BASENAMES, &count);
   497 	size = razor_rpm_get_indirect(rpm, RPMTAG_FILESIZES, &count);
   498 	index = razor_rpm_get_indirect(rpm, RPMTAG_DIRINDEXES, &count);
   499 	mode = razor_rpm_get_indirect(rpm, RPMTAG_FILEMODES, &count);
   500 	flags = razor_rpm_get_indirect(rpm, RPMTAG_FILEFLAGS, &count);
   501 
   502 	for (i = 0; i < count; i++) {
   503 		dir = rpm->dirs[ntohl(*index)];
   504 
   505 		/* Skip past the cpio header block unless it's a ghost file,
   506 		 * in which case doesn't appear in the cpio archive. */
   507 		if (!(ntohl(*flags) & RPMFILE_GHOST)) {
   508 			/* Plus two for the leading '.' and the terminating NUL. */
   509 			length = sizeof *header + strlen(dir) + strlen(name) + 2;
   510 			installer.rest = ALIGN(length, 4);
   511 			if (installer_inflate(&installer))
   512 				return -1;
   513 		}
   514 
   515 		installer.rest = ntohl(*size);
   516 		if (create_path(&installer, dir, name, ntohs(*mode)) < 0)
   517 			return -1;
   518 
   519 		name += strlen(name) + 1;
   520 		index++;
   521 		size++;
   522 		mode++;
   523 		flags++;
   524 	}
   525 
   526 	if (installer_finish(&installer))
   527 		return -1;
   528 
   529 	run_script(&installer, RPMTAG_POSTINPROG, RPMTAG_POSTIN);
   530 
   531 	return 0;
   532 }
   533 
   534 int
   535 razor_rpm_close(struct razor_rpm *rpm)
   536 {
   537 	int err;
   538 
   539 	free(rpm->dirs);
   540 	err = munmap(rpm->map, rpm->size);
   541 	free(rpm);
   542 
   543 	return err;
   544 }
   545 
   546 int
   547 razor_importer_add_rpm(struct razor_importer *importer, struct razor_rpm *rpm)
   548 {
   549 	const char *name, *version, *release;
   550 
   551 	name = razor_rpm_get_indirect(rpm, RPMTAG_NAME, NULL);
   552 	version = razor_rpm_get_indirect(rpm, RPMTAG_VERSION, NULL);
   553 	release = razor_rpm_get_indirect(rpm, RPMTAG_RELEASE, NULL);
   554 
   555 	/* FIXME: Concatenate version and release. */
   556 	razor_importer_begin_package(importer, name, version);
   557 
   558 	import_properties(importer, RAZOR_PROPERTY_REQUIRES, rpm,
   559 			  RPMTAG_REQUIRENAME,
   560 			  RPMTAG_REQUIREVERSION,
   561 			  RPMTAG_REQUIREFLAGS);
   562 
   563 	import_properties(importer, RAZOR_PROPERTY_PROVIDES, rpm,
   564 			  RPMTAG_PROVIDENAME,
   565 			  RPMTAG_PROVIDEVERSION,
   566 			  RPMTAG_PROVIDEFLAGS);
   567 
   568 	import_properties(importer, RAZOR_PROPERTY_OBSOLETES, rpm,
   569 			  RPMTAG_OBSOLETENAME,
   570 			  RPMTAG_OBSOLETEVERSION,
   571 			  RPMTAG_OBSOLETEFLAGS);
   572 
   573 	import_properties(importer, RAZOR_PROPERTY_CONFLICTS, rpm,
   574 			  RPMTAG_CONFLICTNAME,
   575 			  RPMTAG_CONFLICTVERSION,
   576 			  RPMTAG_CONFLICTFLAGS);
   577 
   578 	import_files(importer, rpm);
   579 
   580 	razor_importer_finish_package(importer);
   581 
   582 	return 0;
   583 }