accepts package specifications like name-version, name-version-release
authorewt <devnull@localhost>
Wed, 14 Feb 1996 20:56:22 +0000 (20:56 +0000)
committerewt <devnull@localhost>
Wed, 14 Feb 1996 20:56:22 +0000 (20:56 +0000)
CVS patchset: 272
CVS date: 1996/02/14 20:56:22

install.c
query.c
query.h

index 7ab8027..ecccfe0 100644 (file)
--- a/install.c
+++ b/install.c
@@ -6,6 +6,7 @@
 #include "install.h"
 #include "lib/rpmlib.h"
 #include "messages.h"
+#include "query.h"
 
 static int hashesPrinted = 0;
 
@@ -100,6 +101,8 @@ void doUninstall(char * prefix, char * arg, int test, int uninstallFlags) {
     dbIndexSet matches;
     int i;
     int mode;
+    int rc;
+    int count;
 
     if (test) 
        mode = O_RDONLY;
@@ -111,15 +114,27 @@ void doUninstall(char * prefix, char * arg, int test, int uninstallFlags) {
        exit(1);
     }
    
-    if (rpmdbFindPackage(db, arg, &matches)) {
+    rc = findPackageByLabel(db, arg, &matches);
+    if (rc == 1) 
        fprintf(stderr, "package %s is not installed\n", arg);
-    } else {
-       if (matches.count > 1) {
+    else if (rc == 2) 
+       fprintf(stderr, "error searching for package %s\n", arg);
+    else {
+       count = 0;
+       for (i = 0; i < matches.count; i++)
+           if (matches.recs[i].recOffset) count++;
+
+       if (count > 1) {
            fprintf(stderr, "\"%s\" specifies multiple packages\n", arg);
        }
        else { 
            for (i = 0; i < matches.count; i++) {
-               rpmRemovePackage(prefix, db, matches.recs[i].recOffset, test);
+               if (matches.recs[i].recOffset) {
+                   message(MESS_DEBUG, "uninstalling record number %d\n",
+                               matches.recs[i].recOffset);
+                   rpmRemovePackage(prefix, db, matches.recs[i].recOffset, 
+                                    test);
+               }
            }
        }
 
diff --git a/query.c b/query.c
index b49b373..60059d1 100644 (file)
--- a/query.c
+++ b/query.c
@@ -9,11 +9,13 @@
 #include "rpmlib.h"
 #include "query.h"
 
-void printHeader(Header h, int queryFlags);
-char * getString(Header h, int tag, char * def);    
-void showMatches(rpmdb db, dbIndexSet matches, int queryFlags);
+static void printHeader(Header h, int queryFlags);
+static char * getString(Header h, int tag, char * def);    
+static void showMatches(rpmdb db, dbIndexSet matches, int queryFlags);
+static int findMatches(rpmdb db, char * name, char * version, char * release,
+                      dbIndexSet * matches);
 
-char * getString(Header h, int tag, char * def) {
+static char * getString(Header h, int tag, char * def) {
     char * str;
     int count, type;
    
@@ -24,7 +26,7 @@ char * getString(Header h, int tag, char * def) {
     return str;
 }
 
-void printHeader(Header h, int queryFlags) {
+static void printHeader(Header h, int queryFlags) {
     char * name, * version, * release;
     char * distribution, * vendor, * group, *description, * buildHost;
     uint_32 * size;
@@ -134,17 +136,22 @@ void printHeader(Header h, int queryFlags) {
     }
 }
 
-void showMatches(rpmdb db, dbIndexSet matches, int queryFlags) {
+static void showMatches(rpmdb db, dbIndexSet matches, int queryFlags) {
     int i;
     Header h;
 
     for (i = 0; i < matches.count; i++) {
-       h = rpmdbGetRecord(db, matches.recs[i].recOffset);
-       if (!h) {
-           fprintf(stderr, "error: could not read database record\n");
-       } else {
-           printHeader(h, queryFlags);
-           freeHeader(h);
+       if (matches.recs[i].recOffset) {
+           message(MESS_DEBUG, "querying record number %d\n",
+                       matches.recs[i].recOffset);
+           
+           h = rpmdbGetRecord(db, matches.recs[i].recOffset);
+           if (!h) {
+               fprintf(stderr, "error: could not read database record\n");
+           } else {
+               printHeader(h, queryFlags);
+               freeHeader(h);
+           }
        }
     }
 }
@@ -222,8 +229,11 @@ void doQuery(char * prefix, enum querysources source, int queryFlags,
 
       case QUERY_SPACKAGE:
       case QUERY_PACKAGE:
-       if (rpmdbFindPackage(db, arg, &matches)) {
+       rc = findPackageByLabel(db, arg, &matches);
+       if (rc == 1) 
            fprintf(stderr, "package %s is not installed\n", arg);
+       else if (rc == 2) {
+           fprintf(stderr, "error looking for package %s\n", arg);
        } else {
            showMatches(db, matches, queryFlags);
            freeDBIndexRecord(matches);
@@ -235,3 +245,93 @@ void doQuery(char * prefix, enum querysources source, int queryFlags,
        rpmdbClose(db);
     }
 }
+
+/* 0 found matches */
+/* 1 no matches */
+/* 2 error */
+int findPackageByLabel(rpmdb db, char * arg, dbIndexSet * matches) {
+    char * localarg, * chptr;
+    char * release;
+    int rc;
+    if (!strlen(arg)) return 1;
+
+    /* did they give us just a name? */
+    rc = findMatches(db, arg, NULL, NULL, matches);
+    if (rc != 1) return rc;
+
+    /* maybe a name and a release */
+    localarg = alloca(strlen(arg) + 1);
+    strcpy(localarg, arg);
+
+    chptr = (localarg + strlen(localarg)) - 1;
+    while (chptr > localarg && *chptr != '-') chptr--;
+    if (chptr == localarg) return 1;
+
+    *chptr = '\0';
+    rc = findMatches(db, localarg, chptr + 1, NULL, matches);
+    if (rc != 1) return rc;
+    
+    /* how about name-version-release? */
+
+    release = chptr + 1;
+    while (chptr > localarg && *chptr != '-') chptr--;
+    if (chptr == localarg) return 1;
+
+    *chptr = '\0';
+    return findMatches(db, localarg, chptr + 1, release, matches);
+}
+
+/* 0 found matches */
+/* 1 no matches */
+/* 2 error */
+int findMatches(rpmdb db, char * name, char * version, char * release,
+               dbIndexSet * matches) {
+    int gotMatches;
+    int rc;
+    int i;
+    char * pkgRelease, * pkgVersion;
+    int count, type;
+    int goodRelease, goodVersion;
+    Header h;
+
+    if ((rc = rpmdbFindPackage(db, name, matches))) {
+       if (rc == -1) return 2; else return 1;
+    }
+
+    if (!version && !release) return 0;
+
+    gotMatches = 0;
+
+    /* make sure the version and releases match */
+    for (i = 0; i < matches->count; i++) {
+       if (matches->recs[i].recOffset) {
+           h = rpmdbGetRecord(db, matches->recs[i].recOffset);
+           if (!h) {
+               fprintf(stderr, "error: could not read database record\n");
+               freeDBIndexRecord(*matches);
+               return 2;
+           }
+
+           getEntry(h, RPMTAG_VERSION, &type, (void **) &pkgVersion, &count);
+           getEntry(h, RPMTAG_RELEASE, &type, (void **) &pkgRelease, &count);
+           
+           goodRelease = goodVersion = 1;
+
+           if (release && strcmp(release, pkgRelease)) goodRelease = 0;
+           if (version && strcmp(version, pkgVersion)) goodVersion = 0;
+
+           if (goodRelease && goodVersion) 
+               gotMatches = 1;
+           else 
+               matches->recs[i].recOffset = 0;
+       }
+    }
+
+    if (!gotMatches) {
+       freeDBIndexRecord(*matches);
+       return 1;
+    }
+    
+    return 0;
+}
diff --git a/query.h b/query.h
index 7b8cda6..6a6a1ce 100644 (file)
--- a/query.h
+++ b/query.h
@@ -1,6 +1,8 @@
 #ifndef H_QUERY
 #define H_QUERY
 
+#include <rpmlib.h>
+
 enum querysources { QUERY_PATH, QUERY_PACKAGE, QUERY_ALL, QUERY_SPATH,
                    QUERY_SPACKAGE, QUERY_RPM, QUERY_SRPM, QUERY_GROUP,
                    QUERY_SGROUP };
@@ -14,4 +16,9 @@ enum querysources { QUERY_PATH, QUERY_PACKAGE, QUERY_ALL, QUERY_SPATH,
 void doQuery(char * prefix, enum querysources source, int queryFlags, 
             char * arg);
 
+/* 0 found matches */
+/* 1 no matches */
+/* 2 error */
+int findPackageByLabel(rpmdb db, char * arg, dbIndexSet * matches);
+
 #endif