Add support for <contenthash> tag in repomd.xml
authorTomas Mlcoch <tmlcoch@redhat.com>
Mon, 16 Dec 2013 15:09:32 +0000 (16:09 +0100)
committerTomas Mlcoch <tmlcoch@redhat.com>
Mon, 16 Dec 2013 15:09:32 +0000 (16:09 +0100)
src/python/repomd-py.c
src/repomd.c
src/repomd.h
src/xml_dump_repomd.c
src/xml_parser_repomd.c
tests/python/tests/test_repomd.py

index 25b8a1c..ae64eef 100644 (file)
@@ -162,6 +162,22 @@ set_repoid(_RepomdObject *self, PyObject *args)
     Py_RETURN_NONE;
 }
 
+PyDoc_STRVAR(set_contenthash__doc__,
+"set_contenthash(contenthash, contenthash_type) -> None\n\n"
+"Set contenthash value and contenthash_type");
+
+static PyObject *
+set_contenthash(_RepomdObject *self, PyObject *args)
+{
+    char *contenthash, *contenthash_type;
+    if (!PyArg_ParseTuple(args, "zz:set_contenthash", &contenthash, &contenthash_type))
+        return NULL;
+    if (check_RepomdStatus(self))
+        return NULL;
+    cr_repomd_set_contenthash(self->repomd, contenthash, contenthash_type);
+    Py_RETURN_NONE;
+}
+
 PyDoc_STRVAR(add_distro_tag__doc__,
 "add_distro_tag(tag[, cpeid=None]) -> None\n\n"
 "Add distro tag");
@@ -252,6 +268,8 @@ static struct PyMethodDef repomd_methods[] = {
         set_revision__doc__},
     {"set_repoid", (PyCFunction)set_repoid, METH_VARARGS,
         set_repoid__doc__},
+    {"set_contenthash", (PyCFunction)set_contenthash, METH_VARARGS,
+        set_contenthash__doc__},
     {"add_distro_tag", (PyCFunction)add_distro_tag, METH_VARARGS|METH_KEYWORDS,
         add_distro_tag__doc__},
     {"add_repo_tag", (PyCFunction)add_repo_tag, METH_VARARGS,
@@ -430,6 +448,10 @@ static PyGetSetDef repomd_getsetters[] = {
         "Repoid value", OFFSET(repoid)},
     {"repoid_type",      (getter)get_str,  (setter)set_str,
         "Repoid type value", OFFSET(repoid_type)},
+     {"contenthash",     (getter)get_str,  (setter)set_str,
+        "Contenthash value", OFFSET(contenthash)},
+    {"contenthash_type", (getter)get_str,  (setter)set_str,
+        "contenthash type value", OFFSET(contenthash_type)},
     {"repo_tags",        (getter)get_list, (setter)set_list,
         "List of repo tags", &(list_convertors[0])},
     {"distro_tags",      (getter)get_list, (setter)set_list,
index 8b8d9a4..96e0fc9 100644 (file)
@@ -657,6 +657,14 @@ cr_repomd_set_repoid(cr_Repomd *repomd, const char *repoid, const char *type)
 }
 
 void
+cr_repomd_set_contenthash(cr_Repomd *repomd, const char *hash, const char *type)
+{
+    if (!repomd) return;
+    repomd->contenthash = cr_safe_string_chunk_insert(repomd->chunk, hash);
+    repomd->contenthash_type = cr_safe_string_chunk_insert(repomd->chunk, type);
+}
+
+void
 cr_repomd_add_distro_tag(cr_Repomd *repomd,
                          const char *cpeid,
                          const char *tag)
index 442a679..a3d74e9 100644 (file)
@@ -98,8 +98,10 @@ typedef struct {
  */
 typedef struct {
     gchar *revision;            /*!< Revison */
-    gchar *repoid;              /*!< RepoId */
-    gchar *repoid_type;         /*!< RepoId type ("sha256", ...) */
+    gchar *repoid;              /*!< OBSOLETE, replaced by contenthash */
+    gchar *repoid_type;         /*!< OBSOLETE, replaced by contenthash_type */
+    gchar *contenthash;         /*!< Content hash */
+    gchar *contenthash_type;    /*!< Content hash type ("sha256", ...) */
     GSList *repo_tags;          /*!< List of strings */
     GSList *content_tags;       /*!< List of strings */
     GSList *distro_tags;        /*!< List of cr_DistroTag* */
@@ -193,15 +195,24 @@ void cr_repomd_set_record(cr_Repomd *repomd, cr_RepomdRecord *record);
  */
 void cr_repomd_set_revision(cr_Repomd *repomd, const char *revision);
 
-/** Set a repoid
+/** Set a repoid - OBSOLETE, use cr_repomd_set_contenthash instead
  * @param repomd                cr_Repomd object
- * @param type                  Type of hash function used to calculate repoid
  * @param repoid                RepoId
+ * @param type                  Type of hash function used to calculate repoid
  */
 void cr_repomd_set_repoid(cr_Repomd *repomd,
                           const char *repoid,
                           const char *type);
 
+/** Set a contenthash
+ * @param repomd                cr_Repomd object
+ * @param hash                  content hash
+ * @param type                  Type of hash function
+ */
+void cr_repomd_set_contenthash(cr_Repomd *repomd,
+                               const char *hash,
+                               const char *type);
+
 /** Add distro tag.
  * @param repomd                cr_Repomd object
  * @param cpeid                 cpeid string (could be NULL)
index 6da5646..c1b3f7e 100644 (file)
@@ -145,6 +145,21 @@ cr_xml_dump_repomd_body(xmlNodePtr root, cr_Repomd *repomd)
     }
 
     // **********************************
+    // Element: Contenthash
+    // **********************************
+
+    if (repomd->contenthash) {
+        xmlNodePtr contenthash_elem = cr_xmlNewTextChild(root,
+                                                NULL,
+                                                BAD_CAST "contenthash",
+                                                BAD_CAST repomd->contenthash);
+        if (repomd->contenthash_type)
+            cr_xmlNewProp(contenthash_elem,
+                          BAD_CAST "type",
+                          BAD_CAST repomd->contenthash_type);
+    }
+
+    // **********************************
     // Element: Tags
     // **********************************
 
index dd05713..0484779 100644 (file)
@@ -38,6 +38,7 @@ typedef enum {
     STATE_REPOMD,
     STATE_REVISION,
     STATE_REPOID,
+    STATE_CONTENTHASH,
     STATE_TAGS,
     STATE_REPO,
     STATE_CONTENT,
@@ -62,6 +63,7 @@ static cr_StatesSwitch stateswitches[] = {
     { STATE_START,      "repomd",           STATE_REPOMD,       0 },
     { STATE_REPOMD,     "revision",         STATE_REVISION,     1 },
     { STATE_REPOMD,     "repoid",           STATE_REPOID,       1 },
+    { STATE_REPOMD,     "contenthash",      STATE_CONTENTHASH,  1 },
     { STATE_REPOMD,     "tags",             STATE_TAGS,         0 },
     { STATE_REPOMD,     "data",             STATE_DATA,         0 },
     { STATE_TAGS,       "repo",             STATE_REPO,         1 },
@@ -137,6 +139,16 @@ cr_start_handler(void *pdata, const char *element, const char **attr)
                                                             val);
         break;
 
+    case STATE_CONTENTHASH:
+        assert(pd->repomd);
+        assert(!pd->repomdrecord);
+
+        val = cr_find_attr("type", attr);
+        if (val)
+            pd->repomd->contenthash_type = g_string_chunk_insert(
+                                                    pd->repomd->chunk, val);
+        break;
+
     case STATE_DISTRO:
         assert(pd->repomd);
         assert(!pd->repomdrecord);
@@ -271,6 +283,14 @@ cr_end_handler(void *pdata, const char *element)
                                                    pd->content);
         break;
 
+    case STATE_CONTENTHASH:
+        assert(pd->repomd);
+        assert(!pd->repomdrecord);
+
+        pd->repomd->contenthash = g_string_chunk_insert(pd->repomd->chunk,
+                                                        pd->content);
+        break;
+
     case STATE_TAGS:
         break;
 
index bc18f22..df3fca4 100644 (file)
@@ -48,8 +48,12 @@ class TestCaseRepomd(unittest.TestCase):
         self.assertEqual(md.revision, "foobar")
 
         self.assertEqual(md.repoid, None);
-        md.set_repoid("fooid", "sha256")
-        self.assertEqual(md.repoid, "fooid")
+        md.set_repoid("barid", "sha256")
+        self.assertEqual(md.repoid, "barid")
+
+        self.assertEqual(md.contenthash, None);
+        md.set_contenthash("fooid", "sha256")
+        self.assertEqual(md.contenthash, "fooid")
 
         self.assertEqual(md.distro_tags, [])
         md.add_distro_tag("tag1")
@@ -75,7 +79,8 @@ class TestCaseRepomd(unittest.TestCase):
 """<?xml version="1.0" encoding="UTF-8"?>
 <repomd xmlns="http://linux.duke.edu/metadata/repo" xmlns:rpm="http://linux.duke.edu/metadata/rpm">
   <revision>foobar</revision>
-  <repoid type="sha256">fooid</repoid>
+  <repoid type="sha256">barid</repoid>
+  <contenthash type="sha256">fooid</contenthash>
   <tags>
     <content>contenttag</content>
     <repo>repotag</repo>
@@ -95,6 +100,7 @@ class TestCaseRepomd(unittest.TestCase):
         self.assertEqual(len(md.records), 1)
 
         md.repoid = None
+        md.contenthash = None
 
         xml = md.xml_dump()
         self.assertEqual(xml,