Annotation of rpm2html/rpmopen.c, revision 1.1
1.1 ! veillard 1: /*
! 2: * rpmopen.c : open an extract informations from RPM files.
! 3: *
! 4: * $Id$
! 5: *
! 6: * $Log$
! 7: */
! 8:
! 9: #include <sys/types.h>
! 10: #include <sys/stat.h>
! 11: #include <fcntl.h>
! 12: #include <stdio.h>
! 13: #include <stdlib.h>
! 14: #include <string.h>
! 15: #include <unistd.h>
! 16:
! 17: #include <rpm/rpmlib.h>
! 18:
! 19: #include "rpmdata.h"
! 20:
! 21: /*
! 22: * Get the internal number associated to an RPM tag.
! 23: */
! 24: static int getTagNumber(char *tag) {
! 25: int i;
! 26: const struct headerTagTableEntry * t;
! 27:
! 28: for (i = 0, t = rpmTagTable; i < rpmTagTableSize; i++, t++) {
! 29: if (!strcasecmp(tag, t->name)) return(t->val);
! 30: }
! 31: fprintf(stderr, "getTagNumber(%s) : unknown tag !\n", tag);
! 32: return(-1);
! 33: }
! 34:
! 35: /*
! 36: * rpmOpen : open an RPM file, read and parse the header and
! 37: * fill the informations in the database.
! 38: */
! 39: int rpmOpen(char *nameRpm) {
! 40: int fd;
! 41: int rc;
! 42: Header h = NULL;
! 43: int isSource;
! 44: char * name, * version, * release;
! 45: int_32 count, type;
! 46: void * p;
! 47: int val, i;
! 48: rpmDataPtr rpm = NULL;
! 49: static char buffer[80000];
! 50:
! 51: /* open the file for reading */
! 52: if ((fd = open(nameRpm, O_RDONLY)) < 0) {
! 53: fprintf(stderr, "open of %s failed: %s\n", nameRpm,
! 54: strerror(errno));
! 55: goto error;
! 56: }
! 57:
! 58: /* read the RPM header */
! 59: rc = rpmReadPackageHeader(fd, &h, &isSource, NULL, NULL);
! 60: switch (rc) {
! 61: case 0:
! 62: if (!h) {
! 63: fprintf(stderr,
! 64: "old format source packages cannot be queried\n");
! 65: goto error;
! 66: }
! 67: break;
! 68: case 1:
! 69: fprintf(stderr, "%s does not appear to be a RPM package\n",
! 70: nameRpm);
! 71: goto error;
! 72: case 2:
! 73: fprintf(stderr, "query of %s failed\n", nameRpm);
! 74: goto error;
! 75: default:
! 76: fprintf(stderr, "rpmReadPackageHeader(%s): unknown error %d\n",
! 77: nameRpm, rc);
! 78: goto error;
! 79: }
! 80:
! 81: /* extract informations from the header */
! 82: headerGetEntry(h, RPMTAG_NAME, &type, (void **) &name, &count);
! 83: headerGetEntry(h, RPMTAG_VERSION, &type, (void **) &version, &count);
! 84: headerGetEntry(h, RPMTAG_RELEASE, &type, (void **) &release, &count);
! 85:
! 86: /* allocate a new rpmData block, fill it */
! 87: rpm = (rpmDataPtr) malloc(sizeof(rpmData));
! 88: if (rpm == NULL) {
! 89: fprintf(stderr, "cannot allocate %d bytes: %s\n", sizeof(rpmData),
! 90: strerror(errno));
! 91: goto error;
! 92: }
! 93: rpm->name = strdup(name);
! 94: rpm->version = strdup(version);
! 95: rpm->release = strdup(release);
! 96:
! 97: /* get all the ressources provided by this RPM */
! 98: val = getTagNumber("RPMTAG_PROVIDES");
! 99: if (!headerGetEntry(h, val, &type, &p, &count) || !p) {
! 100: rpm->nb_ressources = 0;
! 101: } else {
! 102: if (count >= MAX_RESS) {
! 103: fprintf(stderr, "MAX_RESS %d overflow, increase the limit!\n",
! 104: MAX_RESS);
! 105: count = MAX_RESS;
! 106: }
! 107:
! 108: rpm->nb_ressources = count;
! 109: for (i = 0; i < count;i++) {
! 110: rpm->ressources[i] = rpmRessAdd(((char **) p)[i], rpm);
! 111: }
! 112: }
! 113:
! 114: /* insert the package informations in the database */
! 115: rpm->next = rpmList;
! 116: rpmList = rpm;
! 117:
! 118: /* free the header and close the descriptor */
! 119: headerFree(h);
! 120: close(fd);
! 121: return(0);
! 122:
! 123: error:
! 124: if (rpm != NULL) free(rpm);
! 125: if (h != NULL) headerFree(h);
! 126: if (fd >= 0) close(fd);
! 127: return(-1);
! 128: }
! 129:
! 130: int main(int argc, char *argv[]) {
! 131: int i;
! 132:
! 133: if (argc < 2) {
! 134: fprintf(stderr, "usage : %s rpm1 rpm2 ...\n", argv[0]);
! 135: exit(0);
! 136: }
! 137: for (i = 1; i < argc ; i++) {
! 138: rpmOpen(argv[i]);
! 139: }
! 140: rpmDataPrintAll();
! 141:
! 142: return(0);
! 143: }
! 144:
Webmaster