home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 February / CHIP_2_98.iso / misc / src / rpm / lib / lookup.c < prev    next >
C/C++ Source or Header  |  1997-09-17  |  3KB  |  122 lines

  1. #include "miscfn.h"
  2.  
  3. #if HAVE_ALLOCA_H
  4. # include <alloca.h>
  5. #endif
  6.  
  7. #include <string.h>
  8.  
  9. #include "intl.h"
  10. #include "rpmlib.h"
  11.  
  12. static int findMatches(rpmdb db, char * name, char * version, char * release,
  13.                dbiIndexSet * matches);
  14.  
  15. /* 0 found matches */
  16. /* 1 no matches */
  17. /* 2 error */
  18. int rpmdbFindByHeader(rpmdb db, Header h, dbiIndexSet * matches) {
  19.     char * name, * version, * release;
  20.  
  21.     headerGetEntry(h, RPMTAG_NAME, NULL, (void **) &name, NULL);
  22.     headerGetEntry(h, RPMTAG_VERSION, NULL, (void **) &version, NULL);
  23.     headerGetEntry(h, RPMTAG_RELEASE, NULL, (void **) &release, NULL);
  24.  
  25.     return findMatches(db, name, version, release, matches);
  26. }
  27.  
  28. /* 0 found matches */
  29. /* 1 no matches */
  30. /* 2 error */
  31. int rpmdbFindByLabel(rpmdb db, char * arg, dbiIndexSet * matches) {
  32.     char * localarg, * chptr;
  33.     char * release;
  34.     int rc;
  35.  
  36.     if (!strlen(arg)) return 1;
  37.  
  38.     /* did they give us just a name? */
  39.     rc = findMatches(db, arg, NULL, NULL, matches);
  40.     if (rc != 1) return rc;
  41.  
  42.     /* maybe a name and a release */
  43.     localarg = alloca(strlen(arg) + 1);
  44.     strcpy(localarg, arg);
  45.  
  46.     chptr = (localarg + strlen(localarg)) - 1;
  47.     while (chptr > localarg && *chptr != '-') chptr--;
  48.     if (chptr == localarg) return 1;
  49.  
  50.     *chptr = '\0';
  51.     rc = findMatches(db, localarg, chptr + 1, NULL, matches);
  52.     if (rc != 1) return rc;
  53.     
  54.     /* how about name-version-release? */
  55.  
  56.     release = chptr + 1;
  57.     while (chptr > localarg && *chptr != '-') chptr--;
  58.     if (chptr == localarg) return 1;
  59.  
  60.     *chptr = '\0';
  61.     return findMatches(db, localarg, chptr + 1, release, matches);
  62. }
  63.  
  64. /* 0 found matches */
  65. /* 1 no matches */
  66. /* 2 error */
  67. static int findMatches(rpmdb db, char * name, char * version, char * release,
  68.                dbiIndexSet * matches) {
  69.     int gotMatches;
  70.     int rc;
  71.     int i;
  72.     char * pkgRelease, * pkgVersion;
  73.     int count, type;
  74.     int goodRelease, goodVersion;
  75.     Header h;
  76.  
  77.     if ((rc = rpmdbFindPackage(db, name, matches))) {
  78.     if (rc == -1) return 2; else return 1;
  79.     }
  80.  
  81.     if (!version && !release) return 0;
  82.  
  83.     gotMatches = 0;
  84.  
  85.     /* make sure the version and releases match */
  86.     for (i = 0; i < matches->count; i++) {
  87.     if (matches->recs[i].recOffset) {
  88.         h = rpmdbGetRecord(db, matches->recs[i].recOffset);
  89.         if (!h) {
  90.         rpmError(RPMERR_DBCORRUPT, 
  91.              _("cannot read header at %d for lookup"), 
  92.             matches->recs[i].recOffset);
  93.         dbiFreeIndexRecord(*matches);
  94.         return 2;
  95.         }
  96.  
  97.         headerGetEntry(h, RPMTAG_VERSION, &type, (void **) &pkgVersion, 
  98.                &count);
  99.         headerGetEntry(h, RPMTAG_RELEASE, &type, (void **) &pkgRelease, 
  100.                &count);
  101.         
  102.         goodRelease = goodVersion = 1;
  103.  
  104.         if (release && strcmp(release, pkgRelease)) goodRelease = 0;
  105.         if (version && strcmp(version, pkgVersion)) goodVersion = 0;
  106.  
  107.         if (goodRelease && goodVersion) 
  108.         gotMatches = 1;
  109.         else 
  110.         matches->recs[i].recOffset = 0;
  111.     }
  112.     }
  113.  
  114.     if (!gotMatches) {
  115.     dbiFreeIndexRecord(*matches);
  116.     return 1;
  117.     }
  118.     
  119.     return 0;
  120. }
  121.  
  122.