#include "install.h"
#include "lib/rpmlib.h"
#include "messages.h"
+#include "query.h"
static int hashesPrinted = 0;
dbIndexSet matches;
int i;
int mode;
+ int rc;
+ int count;
if (test)
mode = O_RDONLY;
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);
+ }
}
}
#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;
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;
}
}
-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);
+ }
}
}
}
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);
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;
+}