remove obsolete states, add support for product type, fix flags parsing
[platform/upstream/libsolv.git] / ext / repo_products.c
index 802dabe..4609ceb 100644 (file)
@@ -14,6 +14,7 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
+#include <errno.h>
 #include <limits.h>
 #include <fcntl.h>
 #include <ctype.h>
 #include "tools_util.h"
 #include "repo_content.h"
 #include "repo_zyppdb.h"
+#include "repo_products.h"
 #include "repo_releasefile_products.h"
 
 
-//#define DUMPOUT 0
-
 enum state {
-  STATE_START,           // 0
-  STATE_PRODUCT,         // 1
-  STATE_VENDOR,          // 2
-  STATE_NAME,            // 3
-  STATE_VERSION,         // 4
-  STATE_RELEASE,         // 5
-  STATE_ARCH,            // 6
-  STATE_SUMMARY,         // 7
+  STATE_START,
+  STATE_PRODUCT,
+  STATE_VENDOR,
+  STATE_NAME,
+  STATE_VERSION,
+  STATE_RELEASE,
+  STATE_ARCH,
+  STATE_SUMMARY,
   STATE_SHORTSUMMARY,
-  STATE_DESCRIPTION,     // 8
-  STATE_UPDATEREPOKEY,   // 9 should go away
-  STATE_CPEID,         // 9
-  STATE_URLS,            // 10
-  STATE_URL,             // 11
-  STATE_RUNTIMECONFIG,   // 12
-  STATE_LINGUAS,         // 13
-  STATE_LANG,            // 14
-  STATE_REGISTER,        // 15
-  STATE_TARGET,          // 16
-  STATE_REGRELEASE,      // 18
-  STATE_PRODUCTLINE,     // 19
-  NUMSTATES              // 0
+  STATE_DESCRIPTION,
+  STATE_UPDATEREPOKEY,
+  STATE_CPEID,
+  STATE_URLS,
+  STATE_URL,
+  STATE_RUNTIMECONFIG,
+  STATE_LINGUAS,
+  STATE_LANG,
+  STATE_REGISTER,
+  STATE_TARGET,
+  STATE_REGRELEASE,
+  STATE_PRODUCTLINE,
+  NUMSTATES
 };
 
 struct stateswitch {
@@ -115,7 +115,7 @@ struct parsedata {
 
   const char *tmpvers;
   const char *tmprel;
-  const char *tmpurltype;
+  Id urltype;
 
   unsigned int ctime;
 
@@ -133,18 +133,17 @@ struct parsedata {
  * find value for xml attribute
  * I: txt, name of attribute
  * I: atts, list of key/value attributes
- * I: dup, strdup it
  * O: pointer to value of matching key, or NULL
  *
  */
 
 static inline const char *
-find_attr(const char *txt, const char **atts, int dup)
+find_attr(const char *txt, const char **atts)
 {
   for (; *atts; atts += 2)
     {
       if (!strcmp(*atts, txt))
-        return dup ? solv_strdup(atts[1]) : atts[1];
+        return atts[1];
     }
   return 0;
 }
@@ -163,7 +162,7 @@ startElement(void *userData, const char *name, const char **atts)
   struct stateswitch *sw;
 
 #if 0
-      fprintf(stderr, "start: [%d]%s\n", pd->state, name);
+  fprintf(stderr, "start: [%d]%s\n", pd->state, name);
 #endif
   if (pd->depth != pd->statedepth)
     {
@@ -201,7 +200,7 @@ startElement(void *userData, const char *name, const char **atts)
     case STATE_PRODUCT:
       /* parse 'schemeversion' and store in global variable */
       {
-        const char * scheme = find_attr("schemeversion", atts, 0);
+        const char * scheme = find_attr("schemeversion", atts);
         pd->productscheme = (scheme && *scheme) ? atoi(scheme) : -1;
       }
       if (!s)
@@ -213,13 +212,11 @@ startElement(void *userData, const char *name, const char **atts)
 
       /* <summary lang="xy">... */
     case STATE_SUMMARY:
-      pd->tmplang = find_attr("lang", atts, 1);
-      break;
     case STATE_DESCRIPTION:
-      pd->tmplang = find_attr("lang", atts, 1);
+      pd->tmplang = join_dup(&pd->jd, find_attr("lang", atts));
       break;
     case STATE_URL:
-      pd->tmpurltype = find_attr("name", atts, 1);
+      pd->urltype = pool_str2id(pd->pool, find_attr("name", atts), 1);
       break;
     default:
       break;
@@ -234,7 +231,7 @@ endElement(void *userData, const char *name)
   Solvable *s = pd->solvable;
 
 #if 0
-      fprintf(stderr, "end: [%d]%s\n", pd->state, name);
+  fprintf(stderr, "end: [%d]%s\n", pd->state, name);
 #endif
   if (pd->depth != pd->statedepth)
     {
@@ -306,22 +303,19 @@ endElement(void *userData, const char *name)
       break;
     case STATE_SUMMARY:
       repodata_set_str(pd->data, pd->handle, pool_id2langid(pd->pool, SOLVABLE_SUMMARY, pd->tmplang, 1), pd->content);
-      pd->tmplang = solv_free((void *)pd->tmplang);
       break;
     case STATE_SHORTSUMMARY:
       repodata_set_str(pd->data, pd->handle, PRODUCT_SHORTLABEL, pd->content);
       break;
     case STATE_DESCRIPTION:
       repodata_set_str(pd->data, pd->handle, pool_id2langid(pd->pool, SOLVABLE_DESCRIPTION, pd->tmplang, 1), pd->content);
-      pd->tmplang = solv_free((void *)pd->tmplang);
       break;
     case STATE_URL:
-      if (pd->tmpurltype)
+      if (pd->urltype)
         {
           repodata_add_poolstr_array(pd->data, pd->handle, PRODUCT_URL, pd->content);
-          repodata_add_idarray(pd->data, pd->handle, PRODUCT_URL_TYPE, pool_str2id(pd->pool, pd->tmpurltype, 1));
+          repodata_add_idarray(pd->data, pd->handle, PRODUCT_URL_TYPE, pd->urltype);
         }
-      pd->tmpurltype = solv_free((void *)pd->tmpurltype);
       break;
     case STATE_TARGET:
       repodata_set_str(pd->data, pd->handle, PRODUCT_REGISTER_TARGET, pd->content);
@@ -330,7 +324,7 @@ endElement(void *userData, const char *name)
       repodata_set_str(pd->data, pd->handle, PRODUCT_REGISTER_RELEASE, pd->content);
       break;
     case STATE_CPEID:
-      if (pd->content)
+      if (*pd->content)
         repodata_set_str(pd->data, pd->handle, SOLVABLE_CPEID, pd->content);
     default:
       break;
@@ -390,7 +384,7 @@ add_code11_product(struct parsedata *pd, FILE *fp)
   else
     {
       pd->currentproduct = pd->baseproduct + 1; /* make it != baseproduct if stat fails */
-      perror("fstat");
+      pool_error(pd->pool, 0, "fstat: %s", strerror(errno));
       pd->ctime = 0;
     }
 
@@ -416,7 +410,7 @@ add_code11_product(struct parsedata *pd, FILE *fp)
 }
 
 
-void
+int
 repo_add_code11_products(Repo *repo, const char *dirpath, int flags)
 {
   Repodata *data;
@@ -442,6 +436,8 @@ repo_add_code11_products(Repo *repo, const char *dirpath, int flags)
       pd.sbtab[sw->to] = sw->from;
     }
 
+  if (flags & REPO_USE_ROOTDIR)
+    dirpath = pool_prepend_rootdir(repo->pool, dirpath);
   dir = opendir(dirpath);
   if (dir)
     {
@@ -465,7 +461,7 @@ repo_add_code11_products(Repo *repo, const char *dirpath, int flags)
          fp = fopen(fullpath, "r");
          if (!fp)
            {
-             perror(fullpath);
+             pool_error(repo->pool, 0, "%s: %s", fullpath, strerror(errno));
              continue;
            }
          pd.filename = fullpath;
@@ -475,12 +471,14 @@ repo_add_code11_products(Repo *repo, const char *dirpath, int flags)
        }
       closedir(dir);
     }
-  solv_free((void *)pd.tmplang);
   solv_free(pd.content);
   join_freemem(&pd.jd);
+  if (flags & REPO_USE_ROOTDIR)
+    solv_free((char *)dirpath);
 
   if (!(flags & REPO_NO_INTERNALIZE))
     repodata_internalize(data);
+  return 0;
 }
 
 
@@ -498,55 +496,60 @@ repo_add_code11_products(Repo *repo, const char *dirpath, int flags)
 
 /* Oh joy! Three parsers for the price of one! */
 
-void
-repo_add_products(Repo *repo, const char *proddir, const char *root, int flags)
+int
+repo_add_products(Repo *repo, const char *proddir, int flags)
 {
-  char *fullpath;
+  const char *fullpath;
   DIR *dir;
 
   if (proddir)
     {
-      dir = opendir(proddir);
+      dir = opendir(flags & REPO_USE_ROOTDIR ? pool_prepend_rootdir_tmp(repo->pool, proddir) : proddir);
       if (dir)
        {
          /* assume code11 stype products */
          closedir(dir);
-         repo_add_code11_products(repo, proddir, flags);
-         return;
+         return repo_add_code11_products(repo, proddir, flags);
        }
     }
 
   /* code11 didn't work, try old code10 zyppdb */
-  fullpath = solv_dupjoin(root ? root : 0, "/var/lib/zypp/db/products", 0);
+  fullpath = "/var/lib/zypp/db/products";
+  if (flags & REPO_USE_ROOTDIR)
+    fullpath = pool_prepend_rootdir_tmp(repo->pool, fullpath);
   dir = opendir(fullpath);
   if (dir)
     {
       closedir(dir);
       /* assume code10 style products */
-      repo_add_zyppdb_products(repo, fullpath, flags);
-      solv_free(fullpath);
-      return;
+      return repo_add_zyppdb_products(repo, "/var/lib/zypp/db/products", flags);
     }
-  solv_free(fullpath);
 
   /* code10/11 didn't work, try -release files parsing */
-  fullpath = solv_dupjoin(root ? root : 0, "/etc", 0);
+  fullpath = "/etc";
+  if (flags & REPO_USE_ROOTDIR)
+    fullpath = pool_prepend_rootdir_tmp(repo->pool, fullpath);
   dir = opendir(fullpath);
   if (dir)
     {
       closedir(dir);
-      repo_add_releasefile_products(repo, fullpath, flags);
-      solv_free(fullpath);
-      return;
+      return repo_add_releasefile_products(repo, "/etc", flags);
     }
 
-  /* no luck. print an error message in case the root argument is wrong */
-  perror(fullpath);
-  solv_free(fullpath);
+  /* no luck. check if the rootdir exists */
+  fullpath = pool_get_rootdir(repo->pool);
+  if (fullpath && *fullpath)
+    {
+      dir = opendir(fullpath);
+      if (!dir)
+       return pool_error(repo->pool, -1, "%s: %s", fullpath, strerror(errno));
+      closedir(dir);
+    }
 
   /* the least we can do... */
   if (!(flags & REPO_NO_INTERNALIZE) && (flags & REPO_REUSE_REPODATA) != 0)
     repodata_internalize(repo_last_repodata(repo));
+  return 0;
 }
 
 /* EOF */