10 #include <arpa/inet.h>
11 #include <rpm/rpmlib.h>
12 #include <rpm/rpmdb.h>
16 #include "razor-internal.h"
18 #define RPM_LEAD_SIZE 96
21 unsigned char magic[4];
22 unsigned char reserved[4];
27 struct rpm_header_index {
35 struct rpm_header *signature;
36 struct rpm_header *header;
44 static struct rpm_header_index *
45 razor_rpm_get_header(struct razor_rpm *rpm, unsigned int tag)
47 struct rpm_header_index *index, *end;
49 index = (struct rpm_header_index *) (rpm->header + 1);
50 end = index + ntohl(rpm->header->nindex);
52 if (ntohl(index->tag) == tag)
61 razor_rpm_get_indirect(struct razor_rpm *rpm,
62 unsigned int tag, unsigned int *count)
64 struct rpm_header_index *index;
66 index = razor_rpm_get_header(rpm, tag);
69 *count = ntohl(index->count);
71 return rpm->pool + ntohl(index->offset);
77 static enum razor_version_relation
78 rpm_to_razor_flags (uint_32 flags)
80 switch (flags & (RPMSENSE_LESS | RPMSENSE_EQUAL | RPMSENSE_GREATER)) {
82 return RAZOR_VERSION_LESS;
83 case RPMSENSE_LESS|RPMSENSE_EQUAL:
84 return RAZOR_VERSION_LESS_OR_EQUAL;
86 return RAZOR_VERSION_EQUAL;
87 case RPMSENSE_GREATER|RPMSENSE_EQUAL:
88 return RAZOR_VERSION_GREATER_OR_EQUAL;
89 case RPMSENSE_GREATER:
90 return RAZOR_VERSION_GREATER;
94 return RAZOR_VERSION_EQUAL;
98 import_properties(struct razor_importer *importer, unsigned long type,
99 struct razor_rpm *rpm,
100 int name_tag, int version_tag, int flags_tag)
102 const char *name, *version;
104 unsigned int i, count;
106 name = razor_rpm_get_indirect(rpm, name_tag, &count);
110 flags = *(uint_32 *)razor_rpm_get_indirect(rpm, flags_tag, &count);
112 /* FIXME: Concat version and release. */
113 version = razor_rpm_get_indirect(rpm, version_tag, &count);
114 for (i = 0; i < count; i++) {
115 razor_importer_add_property(importer, name,
116 rpm_to_razor_flags (flags),
118 name += strlen(name) + 1;
119 version += strlen(version) + 1;
124 import_files(struct razor_importer *importer, struct razor_rpm *rpm)
127 const unsigned long *index;
128 unsigned int i, count;
131 /* assert: count is the same for all arrays */
133 index = razor_rpm_get_indirect(rpm, RPMTAG_DIRINDEXES, &count);
134 name = razor_rpm_get_indirect(rpm, RPMTAG_BASENAMES, &count);
135 for (i = 0; i < count; i++) {
136 snprintf(buffer, sizeof buffer,
137 "%s%s", rpm->dirs[ntohl(*index)], name);
138 razor_importer_add_file(importer, buffer);
139 name += strlen(name) + 1;
145 razor_rpm_open(const char *filename)
147 struct razor_rpm *rpm;
148 struct rpm_header_index *base, *index;
150 unsigned int count, i, nindex, hsize;
154 rpm = malloc(sizeof *rpm);
155 memset(rpm, 0, sizeof *rpm);
157 fd = open(filename, O_RDONLY);
159 fprintf(stderr, "couldn't open %s\n", filename);
163 if (fstat(fd, &buf) < 0) {
164 fprintf(stderr, "failed to stat %s (%m)\n", filename);
168 rpm->size = buf.st_size;
169 rpm->map = mmap(NULL, rpm->size, PROT_READ, MAP_PRIVATE, fd, 0);
170 if (rpm->map == MAP_FAILED) {
171 fprintf(stderr, "couldn't mmap %s\n", filename);
176 rpm->signature = rpm->map + RPM_LEAD_SIZE;
177 nindex = ntohl(rpm->signature->nindex);
178 hsize = ntohl(rpm->signature->hsize);
179 rpm->header = (void *) (rpm->signature + 1) +
180 ALIGN(nindex * sizeof *index + hsize, 8);
181 nindex = ntohl(rpm->header->nindex);
182 hsize = ntohl(rpm->header->hsize);
183 rpm->payload = (void *) (rpm->header + 1) +
184 nindex * sizeof *index + hsize;
186 base = (struct rpm_header_index *) (rpm->header + 1);
187 rpm->pool = (void *) base + nindex * sizeof *index;
189 /* Look up dir names now so we can index them directly. */
190 name = razor_rpm_get_indirect(rpm, RPMTAG_DIRNAMES, &count);
192 fprintf(stderr, "old filename style not handled\n");
196 rpm->dirs = calloc(count, sizeof *rpm->dirs);
197 for (i = 0; i < count; i++) {
199 name += strlen(name) + 1;
205 struct cpio_file_header {
224 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
225 #define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
226 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
227 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
228 #define COMMENT 0x10 /* bit 4 set: file comment present */
229 #define RESERVED 0xE0 /* bits 5..7: reserved */
233 struct razor_rpm *rpm;
235 unsigned char buffer[32768];
240 installer_inflate(struct installer *installer)
245 if (ALIGN(installer->rest, 4) > sizeof installer->buffer)
246 length = sizeof installer->buffer;
248 length = installer->rest;
250 installer->stream.next_out = installer->buffer;
251 installer->stream.avail_out = ALIGN(length, 4);
252 err = inflate(&installer->stream, Z_SYNC_FLUSH);
253 if (err != Z_OK && err != Z_STREAM_END) {
254 fprintf(stderr, "inflate error: %d (%m)\n", err);
258 installer->rest -= length;
259 installer->length = length;
265 create_path(struct installer *installer,
266 const char *path, const char *name, unsigned int mode)
268 char buffer[PATH_MAX];
271 if (razor_create_dir(installer->root, path) < 0)
274 snprintf(buffer, sizeof buffer, "%s%s/%s",
275 installer->root, path, name);
277 switch (mode >> 12) {
279 fd = open(buffer, O_WRONLY | O_CREAT | O_TRUNC, mode & 0x1ff);
281 fprintf(stderr, "failed to create file %s\n", buffer);
284 while (installer->rest > 0) {
285 if (installer_inflate(installer)) {
286 fprintf(stderr, "failed to inflate\n");
289 if (razor_write(fd, installer->buffer,
290 installer->length)) {
291 fprintf(stderr, "failed to write payload\n");
296 fprintf(stderr, "failed to close %s: %m\n", buffer);
301 return mkdir(buffer, mode & 0x1ff);
306 printf("%s: unhandled file type %d\n", buffer, mode >> 12);
309 if (installer_inflate(installer)) {
310 fprintf(stderr, "failed to inflate\n");
313 if (installer->length >= sizeof installer->buffer) {
314 fprintf(stderr, "link name too long\n");
317 installer->buffer[installer->length] = '\0';
318 if (symlink((const char *) installer->buffer, buffer)) {
319 fprintf(stderr, "failed to create symlink, %m\n");
324 printf("%s: unknown file type %d\n", buffer, mode >> 12);
330 run_script(struct installer *installer,
331 unsigned int program_tag, unsigned int script_tag)
333 int pid, status, fd[2];
334 const char *script = NULL, *program = NULL;
336 program = razor_rpm_get_indirect(installer->rpm, program_tag, NULL);
337 script = razor_rpm_get_indirect(installer->rpm, script_tag, NULL);
338 if (program == NULL && script == NULL) {
339 printf("no script or program for tags %d and %d\n",
340 program_tag, script_tag);
342 } else if (program == NULL) {
347 fprintf(stderr, "failed to create pipe\n");
352 fprintf(stderr, "failed to fork, %m\n");
353 } else if (pid == 0) {
354 if (dup2(fd[0], STDIN_FILENO) < 0) {
355 fprintf(stderr, "failed redirect stdin, %m\n");
358 if (close(fd[0]) < 0 || close(fd[1]) < 0) {
359 fprintf(stderr, "failed to close pipe, %m\n");
362 if (chroot(installer->root) < 0) {
363 fprintf(stderr, "failed to chroot to %s, %m\n",
367 printf("executing program %s in chroot %s\n",
368 program, installer->root);
369 if (execl(program, program, NULL)) {
370 fprintf(stderr, "failed to exec %s, %m\n", program);
374 if (script && write(fd[1], script, strlen(script)) < 0) {
375 fprintf(stderr, "failed to pipe script, %m\n");
378 if (close(fd[0]) || close(fd[1])) {
379 fprintf(stderr, "failed to close pipe, %m\n");
382 if (wait(&status) < 0) {
383 fprintf(stderr, "wait for child failed, %m");
386 printf("script exited with status %d\n", status);
393 installer_init(struct installer *installer)
395 unsigned char *gz_header;
396 int method, flags, err;
398 gz_header = installer->rpm->payload;
399 if (gz_header[0] != 0x1f || gz_header[1] != 0x8b) {
400 fprintf(stderr, "payload section doesn't have gz header\n");
404 method = gz_header[2];
405 flags = gz_header[3];
407 if (method != Z_DEFLATED || flags != 0) {
409 "unknown payload compression method or flags set\n");
413 installer->stream.zalloc = NULL;
414 installer->stream.zfree = NULL;
415 installer->stream.opaque = NULL;
417 installer->stream.next_in = gz_header + 10;
418 installer->stream.avail_in =
419 (installer->rpm->map + installer->rpm->size) -
420 (void *) installer->stream.next_in;
421 installer->stream.next_out = NULL;
422 installer->stream.avail_out = 0;
424 err = inflateInit2(&installer->stream, -MAX_WBITS);
426 fprintf(stderr, "inflateInit error: %d\n", err);
434 installer_finish(struct installer *installer)
438 err = inflateEnd(&installer->stream);
441 fprintf(stderr, "inflateEnd error: %d\n", err);
449 razor_rpm_install(struct razor_rpm *rpm, const char *root)
451 struct installer installer;
452 unsigned int count, i, length;
453 struct cpio_file_header *header;
454 const unsigned long *size, *index, *flags;
455 const unsigned short *mode;
456 const char *name, *dir;
460 installer.root = root;
462 /* FIXME: Only do this before a transaction, not per rpm. */
463 if (stat(root, &buf) < 0 || !S_ISDIR(buf.st_mode)) {
465 "root installation directory \"%s\" does not exist\n",
470 if (installer_init(&installer))
473 run_script(&installer, RPMTAG_PREINPROG, RPMTAG_PREIN);
475 name = razor_rpm_get_indirect(rpm, RPMTAG_BASENAMES, &count);
476 size = razor_rpm_get_indirect(rpm, RPMTAG_FILESIZES, &count);
477 index = razor_rpm_get_indirect(rpm, RPMTAG_DIRINDEXES, &count);
478 mode = razor_rpm_get_indirect(rpm, RPMTAG_FILEMODES, &count);
479 flags = razor_rpm_get_indirect(rpm, RPMTAG_FILEFLAGS, &count);
481 for (i = 0; i < count; i++) {
482 dir = rpm->dirs[ntohl(*index)];
484 /* Skip past the cpio header block unless it's a ghost file,
485 * in which case doesn't appear in the cpio archive. */
486 if (!(ntohl(*flags) & RPMFILE_GHOST)) {
487 /* Plus two for the leading '.' and the terminating NUL. */
488 length = sizeof *header + strlen(dir) + strlen(name) + 2;
489 installer.rest = ALIGN(length, 4);
490 if (installer_inflate(&installer))
494 installer.rest = ntohl(*size);
495 if (create_path(&installer, dir, name, ntohs(*mode)) < 0)
498 name += strlen(name) + 1;
505 if (installer_finish(&installer))
508 run_script(&installer, RPMTAG_POSTINPROG, RPMTAG_POSTIN);
514 razor_rpm_close(struct razor_rpm *rpm)
519 err = munmap(rpm->map, rpm->size);
526 razor_importer_add_rpm(struct razor_importer *importer, struct razor_rpm *rpm)
528 const char *name, *version, *release;
530 name = razor_rpm_get_indirect(rpm, RPMTAG_NAME, NULL);
531 version = razor_rpm_get_indirect(rpm, RPMTAG_VERSION, NULL);
532 release = razor_rpm_get_indirect(rpm, RPMTAG_RELEASE, NULL);
534 /* FIXME: Concatenate version and release. */
535 razor_importer_begin_package(importer, name, version);
537 import_properties(importer, RAZOR_PROPERTY_REQUIRES, rpm,
539 RPMTAG_REQUIREVERSION,
540 RPMTAG_REQUIREFLAGS);
542 import_properties(importer, RAZOR_PROPERTY_PROVIDES, rpm,
544 RPMTAG_PROVIDEVERSION,
545 RPMTAG_PROVIDEFLAGS);
547 import_properties(importer, RAZOR_PROPERTY_OBSOLETES, rpm,
549 RPMTAG_OBSOLETEVERSION,
550 RPMTAG_OBSOLETEFLAGS);
552 import_properties(importer, RAZOR_PROPERTY_CONFLICTS, rpm,
554 RPMTAG_CONFLICTVERSION,
555 RPMTAG_CONFLICTFLAGS);
557 import_files(importer, rpm);
559 razor_importer_finish_package(importer);
572 add_properties(struct razor_importer *importer,
573 enum razor_property_type property_type,
574 Header h, int_32 name_tag, int_32 version_tag, int_32 flags_tag)
576 union rpm_entry names, versions, flags;
577 int_32 i, type, count;
579 headerGetEntry(h, name_tag, &type, &names.p, &count);
580 headerGetEntry(h, version_tag, &type, &versions.p, &count);
581 headerGetEntry(h, flags_tag, &type, &flags.p, &count);
583 for (i = 0; i < count; i++)
584 razor_importer_add_property(importer,
586 rpm_to_razor_flags (flags.flags[i]),
592 razor_set_create_from_rpmdb(void)
594 struct razor_importer *importer;
595 rpmdbMatchIterator iter;
597 int_32 type, count, i;
598 union rpm_entry name, version, release;
599 union rpm_entry basenames, dirnames, dirindexes;
600 char filename[PATH_MAX];
603 rpmReadConfigFiles(NULL, NULL);
605 if (rpmdbOpen("", &db, O_RDONLY, 0644) != 0) {
606 fprintf(stderr, "cannot open rpm database\n");
610 importer = razor_importer_new();
612 iter = rpmdbInitIterator(db, 0, NULL, 0);
613 while (h = rpmdbNextIterator(iter), h != NULL) {
614 headerGetEntry(h, RPMTAG_NAME, &type, &name.p, &count);
615 headerGetEntry(h, RPMTAG_VERSION, &type, &version.p, &count);
616 headerGetEntry(h, RPMTAG_RELEASE, &type, &release.p, &count);
617 snprintf(filename, sizeof filename, "%s-%s",
618 version.string, release.string);
619 razor_importer_begin_package(importer, name.string, filename);
621 add_properties(importer, RAZOR_PROPERTY_REQUIRES, h,
623 RPMTAG_REQUIREVERSION,
624 RPMTAG_REQUIREFLAGS);
626 add_properties(importer, RAZOR_PROPERTY_PROVIDES, h,
628 RPMTAG_PROVIDEVERSION,
629 RPMTAG_PROVIDEFLAGS);
631 add_properties(importer, RAZOR_PROPERTY_OBSOLETES, h,
633 RPMTAG_OBSOLETEVERSION,
634 RPMTAG_OBSOLETEFLAGS);
636 add_properties(importer, RAZOR_PROPERTY_CONFLICTS, h,
638 RPMTAG_CONFLICTVERSION,
639 RPMTAG_CONFLICTFLAGS);
641 headerGetEntry(h, RPMTAG_BASENAMES, &type,
642 &basenames.p, &count);
643 headerGetEntry(h, RPMTAG_DIRNAMES, &type,
644 &dirnames.p, &count);
645 headerGetEntry(h, RPMTAG_DIRINDEXES, &type,
646 &dirindexes.p, &count);
647 for (i = 0; i < count; i++) {
648 snprintf(filename, sizeof filename, "%s%s",
649 dirnames.list[dirindexes.flags[i]],
651 razor_importer_add_file(importer, filename);
654 razor_importer_finish_package(importer);
659 return razor_importer_finish(importer);