Annotation of rpm2html/rpmopen.c, revision 1.5

1.1       veillard    1: /*
                      2:  * rpmopen.c : open an extract informations from RPM files.
                      3:  *
1.5     ! veillard    4:  * $Id: rpmopen.c,v 1.4 1997/11/11 22:28:11 veillard Exp $
1.2       veillard    5:  *
                      6:  * $Log: rpmopen.c,v $
1.5     ! veillard    7:  * Revision 1.4  1997/11/11 22:28:11  veillard
        !             8:  * Added the support for dependancies on resources, Daniel.
        !             9:  *
1.4       veillard   10:  * Revision 1.3  1997/11/11 21:18:53  veillard
                     11:  * Improved a lot, removed bugs related to sorting, Daniel.
                     12:  *
1.3       veillard   13:  * Revision 1.2  1997/11/11 11:10:11  veillard
                     14:  * State after one night of work, Daniel
                     15:  *
1.2       veillard   16:  * Revision 1.1.1.1  1997/11/11 07:12:32  veillard
                     17:  * First revision of RPM-Check
1.1       veillard   18:  *
                     19:  */
                     20: 
                     21: #include <sys/types.h>
                     22: #include <sys/stat.h>
                     23: #include <fcntl.h>
                     24: #include <stdio.h>
                     25: #include <stdlib.h>
                     26: #include <string.h>
                     27: #include <unistd.h>
                     28: 
                     29: #include <rpm/rpmlib.h>
                     30: 
                     31: #include "rpmdata.h"
                     32: 
                     33: /*
                     34:  * Get the internal number associated to an RPM tag.
                     35:  */
                     36: static int getTagNumber(char *tag) {
                     37:     int i;
                     38:     const struct headerTagTableEntry * t;
                     39: 
                     40:     for (i = 0, t = rpmTagTable; i < rpmTagTableSize; i++, t++) {
                     41:        if (!strcasecmp(tag, t->name)) return(t->val);
                     42:     }
                     43:     fprintf(stderr, "getTagNumber(%s) : unknown tag !\n", tag);
                     44:     return(-1);
                     45: }
                     46: 
                     47: /*
                     48:  * rpmOpen : open an RPM file, read and parse the header and 
                     49:  *           fill the informations in the database.
                     50:  */
                     51: int rpmOpen(char *nameRpm) {
                     52:     int fd;
                     53:     int rc;
                     54:     Header h = NULL;
                     55:     int isSource;
1.2       veillard   56:     char * name = NULL, * version = NULL, * release = NULL;
1.1       veillard   57:     int_32 count, type;
1.2       veillard   58:     void * p = NULL;
1.1       veillard   59:     int val, i;
                     60:     rpmDataPtr rpm = NULL;
1.5     ! veillard   61:     static char buffer[500000];
1.1       veillard   62: 
                     63:     /* open the file for reading */
                     64:     if ((fd = open(nameRpm, O_RDONLY)) < 0) {
                     65:          fprintf(stderr, "open of %s failed: %s\n", nameRpm,
                     66:                 strerror(errno));
                     67:         goto error;
                     68:     }
                     69: 
                     70:     /* read the RPM header */
                     71:     rc = rpmReadPackageHeader(fd, &h, &isSource, NULL, NULL);
                     72:     switch (rc) {
                     73:        case 0:
                     74:            if (!h) {
                     75:                fprintf(stderr,
                     76:                        "old format source packages cannot be queried\n");
                     77:                goto error;
                     78:            }
                     79:            break;
                     80:        case 1:
                     81:            fprintf(stderr, "%s does not appear to be a RPM package\n",
                     82:                    nameRpm);
                     83:            goto error;
                     84:        case 2:
                     85:            fprintf(stderr, "query of %s failed\n", nameRpm);
                     86:            goto error;
                     87:        default:
                     88:            fprintf(stderr, "rpmReadPackageHeader(%s): unknown error %d\n",
                     89:                    nameRpm, rc);
                     90:            goto error;
                     91:     }
                     92: 
                     93:     /* extract informations from the header */
                     94:     headerGetEntry(h, RPMTAG_NAME, &type, (void **) &name, &count);
                     95:     headerGetEntry(h, RPMTAG_VERSION, &type, (void **) &version, &count);
                     96:     headerGetEntry(h, RPMTAG_RELEASE, &type, (void **) &release, &count);
                     97:         
                     98:     /* allocate a new rpmData block, fill it */
                     99:     rpm = (rpmDataPtr) malloc(sizeof(rpmData));
                    100:     if (rpm == NULL) {
                    101:          fprintf(stderr, "cannot allocate %d bytes: %s\n", sizeof(rpmData),
                    102:                 strerror(errno));
                    103:          goto error;
                    104:     }
1.2       veillard  105:     rpm->filename = strdup(nameRpm);
1.1       veillard  106:     rpm->name = strdup(name);
                    107:     rpm->version = strdup(version);
                    108:     rpm->release = strdup(release);
                    109: 
                    110:     /* get all the ressources provided by this RPM */
1.2       veillard  111:     if (!headerGetEntry(h, RPMTAG_SUMMARY, &type, &p, &count) || !p) {
1.3       veillard  112:         rpm->summary = "No Summary !";
1.2       veillard  113:     } else {
                    114:        rpm->summary = strdup((char *) p);
                    115:     }
                    116:     if (!headerGetEntry(h, RPMTAG_DESCRIPTION, &type, &p, &count) || !p) {
1.3       veillard  117:         rpm->description = "No Description !";
1.2       veillard  118:     } else {
                    119:        rpm->description = strdup((char *) p);
                    120:     }
                    121:     if (!headerGetEntry(h, RPMTAG_DISTRIBUTION, &type, &p, &count) || !p) {
1.3       veillard  122:         rpm->distribution = "unknown";
1.2       veillard  123:     } else {
                    124:        rpm->distribution = strdup((char *) p);
                    125:     }
                    126:     if (!headerGetEntry(h, RPMTAG_ARCH, &type, &p, &count) || !p) {
1.3       veillard  127:         rpm->arch = "None";
1.2       veillard  128:     } else {
                    129:        rpm->arch = strdup((char *) p);
                    130:     }
                    131:     if (!headerGetEntry(h, RPMTAG_VENDOR, &type, &p, &count) || !p) {
1.3       veillard  132:         rpm->vendor = "unknown";
1.2       veillard  133:     } else {
                    134:        rpm->vendor = strdup((char *) p);
                    135:     }
                    136:     if (!headerGetEntry(h, RPMTAG_GROUP, &type, &p, &count) || !p) {
1.3       veillard  137:         rpm->group = "unknown/group";
1.2       veillard  138:     } else {
                    139:        rpm->group = strdup((char *) p);
                    140:     }
                    141:     if (!headerGetEntry(h, RPMTAG_BUILDHOST, &type, &p, &count) || !p) {
1.3       veillard  142:         rpm->host = "unknown.host";
1.2       veillard  143:     } else {
                    144:        rpm->host = strdup((char *) p);
                    145:     }
                    146:     if (!headerGetEntry(h, RPMTAG_SIZE, &type, &p, &count) || !p) {
                    147:         rpm->size = 0;
                    148:     } else {
                    149:        rpm->size = *((int *) p);
                    150:     }
                    151:     if (!headerGetEntry(h, RPMTAG_BUILDTIME, &type, &p, &count) || !p) {
                    152:         rpm->date = 0;
                    153:     } else {
                    154:        rpm->date = *((int_32 *) p);
                    155:     }
                    156:     if (!headerGetEntry(h, RPMTAG_SOURCERPM, &type, &p, &count) || !p) {
                    157:         rpm->srcrpm = "";
                    158:     } else {
                    159:        rpm->srcrpm = strdup((char *) p);
                    160:     }
                    161: 
1.1       veillard  162:     val = getTagNumber("RPMTAG_PROVIDES");
                    163:     if (!headerGetEntry(h, val, &type, &p, &count) || !p) {
                    164:         rpm->nb_ressources = 0;
                    165:     } else {
                    166:        if (count >= MAX_RESS) {
                    167:            fprintf(stderr, "MAX_RESS %d overflow, increase the limit!\n",
                    168:                    MAX_RESS);
                    169:            count = MAX_RESS;
                    170:        }
                    171: 
                    172:        rpm->nb_ressources = count;
                    173:         for (i = 0; i < count;i++) {
                    174:            rpm->ressources[i] = rpmRessAdd(((char **) p)[i], rpm);
1.4       veillard  175:        }
                    176:     }
                    177: 
                    178:     val = getTagNumber("RPMTAG_REQUIRENAME");
                    179:     if (!headerGetEntry(h, val, &type, &p, &count) || !p) {
                    180:         rpm->nb_requires = 0;
                    181:     } else {
                    182:        if (count >= MAX_REQU) {
                    183:            fprintf(stderr, "MAX_REQU %d overflow, increase the limit!\n",
                    184:                    MAX_REQU);
                    185:            count = MAX_REQU;
                    186:        }
                    187: 
                    188:        rpm->nb_requires = count;
                    189:         for (i = 0; i < count;i++) {
                    190:            rpm->requires[i] = rpmRequAdd(((char **) p)[i], rpm);
1.1       veillard  191:        }
1.5     ! veillard  192:     }
        !           193:     val = getTagNumber("RPMTAG_FILENAMES");
        !           194:     if (!headerGetEntry(h, val, &type, &p, &count) || !p) {
        !           195:         rpm->filelist = "No file found in the package !";
        !           196:     } else {
        !           197:         char *ptr = buffer;
        !           198:        for (i = 0; i < count;i++) {
        !           199:            ptr += sprintf(ptr, "%s\n", ((char **) p)[i]);
        !           200:        }
        !           201:        rpm->filelist = strdup(buffer);
1.1       veillard  202:     }
                    203: 
                    204:     /* insert the package informations in the database */
                    205:     rpm->next = rpmList;
                    206:     rpmList = rpm;
                    207: 
                    208:     /* free the header and close the descriptor */
                    209:     headerFree(h);
                    210:     close(fd);
                    211:     return(0);
                    212: 
                    213: error:
                    214:     if (rpm != NULL) free(rpm);
                    215:     if (h != NULL) headerFree(h);
                    216:     if (fd >= 0) close(fd);
                    217:     return(-1);
                    218: }
                    219: 

Webmaster