/*
* rpmopen.c : open an extract informations from RPM files.
*
* $Id: rpmopen.c,v 1.3 1997/11/11 21:18:53 veillard Exp $
*
* $Log: rpmopen.c,v $
* Revision 1.3 1997/11/11 21:18:53 veillard
* Improved a lot, removed bugs related to sorting, Daniel.
*
* Revision 1.2 1997/11/11 11:10:11 veillard
* State after one night of work, Daniel
*
* Revision 1.1.1.1 1997/11/11 07:12:32 veillard
* First revision of RPM-Check
*
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <rpm/rpmlib.h>
#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 = NULL, * version = NULL, * release = NULL;
int_32 count, type;
void * p = NULL;
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->filename = strdup(nameRpm);
rpm->name = strdup(name);
rpm->version = strdup(version);
rpm->release = strdup(release);
/* get all the ressources provided by this RPM */
if (!headerGetEntry(h, RPMTAG_SUMMARY, &type, &p, &count) || !p) {
rpm->summary = "No Summary !";
} else {
rpm->summary = strdup((char *) p);
}
if (!headerGetEntry(h, RPMTAG_DESCRIPTION, &type, &p, &count) || !p) {
rpm->description = "No Description !";
} else {
rpm->description = strdup((char *) p);
}
if (!headerGetEntry(h, RPMTAG_DISTRIBUTION, &type, &p, &count) || !p) {
rpm->distribution = "unknown";
} else {
rpm->distribution = strdup((char *) p);
}
if (!headerGetEntry(h, RPMTAG_ARCH, &type, &p, &count) || !p) {
rpm->arch = "None";
} else {
rpm->arch = strdup((char *) p);
}
if (!headerGetEntry(h, RPMTAG_VENDOR, &type, &p, &count) || !p) {
rpm->vendor = "unknown";
} else {
rpm->vendor = strdup((char *) p);
}
if (!headerGetEntry(h, RPMTAG_GROUP, &type, &p, &count) || !p) {
rpm->group = "unknown/group";
} else {
rpm->group = strdup((char *) p);
}
if (!headerGetEntry(h, RPMTAG_BUILDHOST, &type, &p, &count) || !p) {
rpm->host = "unknown.host";
} else {
rpm->host = strdup((char *) p);
}
if (!headerGetEntry(h, RPMTAG_SIZE, &type, &p, &count) || !p) {
rpm->size = 0;
} else {
rpm->size = *((int *) p);
}
if (!headerGetEntry(h, RPMTAG_BUILDTIME, &type, &p, &count) || !p) {
rpm->date = 0;
} else {
rpm->date = *((int_32 *) p);
}
if (!headerGetEntry(h, RPMTAG_SOURCERPM, &type, &p, &count) || !p) {
rpm->srcrpm = "";
} else {
rpm->srcrpm = strdup((char *) p);
}
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);
}
Webmaster