Imported Upstream version 0.7.17
[platform/upstream/libsolv.git] / ext / repo_rpmdb_librpm.h
index 35a46fa..3f9798c 100644 (file)
@@ -12,6 +12,7 @@
  *
  */
 
+#include <rpm/rpmlib.h>
 #include <rpm/rpmts.h>
 #include <rpm/rpmmacro.h>
 
@@ -24,6 +25,7 @@ struct rpmdbstate {
 
   int dbenvopened;     /* database environment opened */
   const char *dbpath;  /* path to the database */
+  int dbpath_allocated;        /* do we need to free the path? */
 
   rpmts ts;
   rpmdbMatchIterator mi;       /* iterator over packages database */
@@ -45,6 +47,13 @@ access_rootdir(struct rpmdbstate *state, const char *dir, int mode)
 static void
 detect_dbpath(struct rpmdbstate *state)
 {
+  state->dbpath = rpmExpand("%{?_dbpath}", NULL);
+  if (state->dbpath && *state->dbpath)
+    {
+      state->dbpath_allocated = 1;
+      return;
+    }
+  solv_free((char *)state->dbpath);
   state->dbpath = access_rootdir(state, "/var/lib/rpm", W_OK) == -1
                   && (access_rootdir(state, "/usr/share/rpm/Packages", R_OK) == 0 || access_rootdir(state, "/usr/share/rpm/rpmdb.sqlite", R_OK) == 0)
                   ? "/usr/share/rpm" : "/var/lib/rpm";
@@ -90,6 +99,24 @@ stat_database(struct rpmdbstate *state, struct stat *statbuf)
   return 0;
 }
 
+/* rpm-4.16.0 cannot read the database if _db_backend is not set */
+#ifndef HAVE_RPMDBNEXTITERATORHEADERBLOB
+static void
+set_db_backend()
+{
+  static int db_backend_set;
+  char *db_backend;
+
+  if (db_backend_set)
+    return;
+  db_backend_set = 1;
+  db_backend = rpmExpand("%{?_db_backend}", NULL);
+  if (!db_backend || !*db_backend)
+    rpmReadConfigFiles(NULL, NULL);
+  solv_free(db_backend);
+}
+#endif
+
 static int
 opendbenv(struct rpmdbstate *state)
 {
@@ -108,6 +135,10 @@ opendbenv(struct rpmdbstate *state)
       delMacro(NULL, "_dbpath");
       return 0;
     }
+#ifndef HAVE_RPMDBNEXTITERATORHEADERBLOB
+  if (!strcmp(RPMVERSION, "4.16.0"))
+    set_db_backend();
+#endif
   if (rpmtsOpenDB(ts, O_RDONLY))
     {
       pool_error(state->pool, 0, "rpmtsOpenDB failed: %s", strerror(errno));