Imported Upstream version 0.6.25
[platform/upstream/libsolv.git] / ext / repo_products.c
index c15051a..326f8fd 100644 (file)
  * for further information
  */
 
+#define _GNU_SOURCE
+#define _XOPEN_SOURCE
+#include <time.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
+#include <errno.h>
 #include <limits.h>
 #include <fcntl.h>
 #include <ctype.h>
@@ -56,7 +60,11 @@ enum state {
   STATE_REGISTER,
   STATE_TARGET,
   STATE_REGRELEASE,
+  STATE_REGFLAVOR,
   STATE_PRODUCTLINE,
+  STATE_REGUPDATES,
+  STATE_REGUPDREPO,
+  STATE_ENDOFLIFE,
   NUMSTATES
 };
 
@@ -85,10 +93,14 @@ static struct stateswitch stateswitches[] = {
   { STATE_PRODUCT,   "linguas",       STATE_LINGUAS,       0 },
   { STATE_PRODUCT,   "updaterepokey", STATE_UPDATEREPOKEY, 1 },
   { STATE_PRODUCT,   "cpeid",         STATE_CPEID,         1 },
+  { STATE_PRODUCT,   "endoflife",     STATE_ENDOFLIFE,     1 },
   { STATE_URLS,      "url",           STATE_URL,           1 },
   { STATE_LINGUAS,   "lang",          STATE_LANG,          0 },
   { STATE_REGISTER,  "target",        STATE_TARGET,        1 },
   { STATE_REGISTER,  "release",       STATE_REGRELEASE,    1 },
+  { STATE_REGISTER,  "flavor",        STATE_REGFLAVOR,     1 },
+  { STATE_REGISTER,  "updates",       STATE_REGUPDATES,    0 },
+  { STATE_REGUPDATES, "repository",   STATE_REGUPDREPO,    0 },
   { NUMSTATES }
 };
 
@@ -147,6 +159,29 @@ find_attr(const char *txt, const char **atts)
   return 0;
 }
 
+static time_t
+datestr2timestamp(const char *date)
+{
+  const char *p; 
+  struct tm tm; 
+
+  if (!date || !*date)
+    return 0;
+  for (p = date; *p >= '0' && *p <= '9'; p++)
+    ;   
+  if (!*p)
+    return atoi(date);
+  memset(&tm, 0, sizeof(tm));
+  p = strptime(date, "%F%T", &tm);
+  if (!p)
+    {
+      memset(&tm, 0, sizeof(tm));
+      p = strptime(date, "%F", &tm);
+      if (!p || *p)
+       return 0;
+    }
+  return timegm(&tm);
+}
 
 /*
  * XML callback: startElement
@@ -217,6 +252,17 @@ startElement(void *userData, const char *name, const char **atts)
     case STATE_URL:
       pd->urltype = pool_str2id(pd->pool, find_attr("name", atts), 1);
       break;
+    case STATE_REGUPDREPO:
+      {
+        const char *repoid = find_attr("repoid", atts);
+       if (repoid && *repoid)
+         {
+           Id h = repodata_new_handle(pd->data);
+           repodata_set_str(pd->data, h, PRODUCT_UPDATES_REPOID, repoid);
+           repodata_add_flexarray(pd->data, pd->handle, PRODUCT_UPDATES, h);
+         }
+       break;
+      }
     default:
       break;
     }
@@ -322,9 +368,17 @@ endElement(void *userData, const char *name)
     case STATE_REGRELEASE:
       repodata_set_str(pd->data, pd->handle, PRODUCT_REGISTER_RELEASE, pd->content);
       break;
+    case STATE_REGFLAVOR:
+      repodata_set_str(pd->data, pd->handle, PRODUCT_REGISTER_FLAVOR, pd->content);
+      break;
     case STATE_CPEID:
-      if (pd->content)
+      if (*pd->content)
         repodata_set_str(pd->data, pd->handle, SOLVABLE_CPEID, pd->content);
+      break;
+    case STATE_ENDOFLIFE:
+      /* FATE#320699: Support tri-state product-endoflife (tag absent, present but nodate(0), present + date) */
+      repodata_set_num(pd->data, pd->handle, PRODUCT_ENDOFLIFE, (*pd->content ? datestr2timestamp(pd->content) : 0));
+      break;
     default:
       break;
     }
@@ -383,7 +437,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;
     }
 
@@ -398,8 +452,12 @@ add_code11_product(struct parsedata *pd, FILE *fp)
       if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
        {
          pool_debug(pd->pool, SOLV_ERROR, "%s: %s at line %u:%u\n", pd->filename, XML_ErrorString(XML_GetErrorCode(parser)), (unsigned int)XML_GetCurrentLineNumber(parser), (unsigned int)XML_GetCurrentColumnNumber(parser));
-         pool_debug(pd->pool, SOLV_ERROR, "skipping this product\n");
          XML_ParserFree(parser);
+         if (pd->solvable)
+           {
+             repo_free_solvable(pd->repo, pd->solvable - pd->pool->solvables, 1);
+             pd->solvable = 0;
+           }
          return;
        }
       if (l == 0)
@@ -435,6 +493,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)
     {
@@ -458,7 +518,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;
@@ -470,6 +530,8 @@ repo_add_code11_products(Repo *repo, const char *dirpath, int flags)
     }
   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);
@@ -492,15 +554,14 @@ repo_add_code11_products(Repo *repo, const char *dirpath, int flags)
 /* Oh joy! Three parsers for the price of one! */
 
 int
-repo_add_products(Repo *repo, const char *proddir, const char *root, int flags)
+repo_add_products(Repo *repo, const char *proddir, int flags)
 {
-  char *fullpath;
+  const char *fullpath;
   DIR *dir;
-  int ret;
 
   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 */
@@ -510,32 +571,37 @@ repo_add_products(Repo *repo, const char *proddir, const char *root, int 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 */
-      ret = repo_add_zyppdb_products(repo, fullpath, flags);
-      solv_free(fullpath);
-      return ret;
+      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);
-      ret = repo_add_releasefile_products(repo, fullpath, flags);
-      solv_free(fullpath);
-      return ret;
+      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)