/* * rpmopen.c : open an extract informations from RPM files. * * $Id: rpmopen.c,v 1.1 1997/11/11 07:12:32 veillard Exp $ * * $Log: rpmopen.c,v $ * Revision 1.1 1997/11/11 07:12:32 veillard * Initial revision * */ #include #include #include #include #include #include #include #include #include "rpmdata.h" /* * Get the internal number associated to an RPM tag. */ static int getTagNumber(char *tag) { int i; const struct headerTagTableEntry * t; for (i = 0, t = rpmTagTable; i < rpmTagTableSize; i++, t++) { if (!strcasecmp(tag, t->name)) return(t->val); } fprintf(stderr, "getTagNumber(%s) : unknown tag !\n", tag); return(-1); } /* * rpmOpen : open an RPM file, read and parse the header and * fill the informations in the database. */ int rpmOpen(char *nameRpm) { int fd; int rc; Header h = NULL; int isSource; char * name, * version, * release; int_32 count, type; void * p; int val, i; rpmDataPtr rpm = NULL; static char buffer[80000]; /* open the file for reading */ if ((fd = open(nameRpm, O_RDONLY)) < 0) { fprintf(stderr, "open of %s failed: %s\n", nameRpm, strerror(errno)); goto error; } /* read the RPM header */ rc = rpmReadPackageHeader(fd, &h, &isSource, NULL, NULL); switch (rc) { case 0: if (!h) { fprintf(stderr, "old format source packages cannot be queried\n"); goto error; } break; case 1: fprintf(stderr, "%s does not appear to be a RPM package\n", nameRpm); goto error; case 2: fprintf(stderr, "query of %s failed\n", nameRpm); goto error; default: fprintf(stderr, "rpmReadPackageHeader(%s): unknown error %d\n", nameRpm, rc); goto error; } /* extract informations from the header */ headerGetEntry(h, RPMTAG_NAME, &type, (void **) &name, &count); headerGetEntry(h, RPMTAG_VERSION, &type, (void **) &version, &count); headerGetEntry(h, RPMTAG_RELEASE, &type, (void **) &release, &count); /* allocate a new rpmData block, fill it */ rpm = (rpmDataPtr) malloc(sizeof(rpmData)); if (rpm == NULL) { fprintf(stderr, "cannot allocate %d bytes: %s\n", sizeof(rpmData), strerror(errno)); goto error; } rpm->name = strdup(name); rpm->version = strdup(version); rpm->release = strdup(release); /* get all the ressources provided by this RPM */ val = getTagNumber("RPMTAG_PROVIDES"); if (!headerGetEntry(h, val, &type, &p, &count) || !p) { rpm->nb_ressources = 0; } else { if (count >= MAX_RESS) { fprintf(stderr, "MAX_RESS %d overflow, increase the limit!\n", MAX_RESS); count = MAX_RESS; } rpm->nb_ressources = count; for (i = 0; i < count;i++) { rpm->ressources[i] = rpmRessAdd(((char **) p)[i], rpm); } } /* insert the package informations in the database */ rpm->next = rpmList; rpmList = rpm; /* free the header and close the descriptor */ headerFree(h); close(fd); return(0); error: if (rpm != NULL) free(rpm); if (h != NULL) headerFree(h); if (fd >= 0) close(fd); return(-1); } int main(int argc, char *argv[]) { int i; if (argc < 2) { fprintf(stderr, "usage : %s rpm1 rpm2 ...\n", argv[0]); exit(0); } for (i = 1; i < argc ; i++) { rpmOpen(argv[i]); } rpmDataPrintAll(); return(0); }