repomd: Add cr_repomd_record_load_contentstat function.
authorTomas Mlcoch <tmlcoch@redhat.com>
Tue, 11 Jun 2013 08:42:26 +0000 (10:42 +0200)
committerTomas Mlcoch <tmlcoch@redhat.com>
Tue, 11 Jun 2013 08:42:26 +0000 (10:42 +0200)
src/createrepo_c.c
src/mergerepo_c.c
src/python/repomdrecord-py.c
src/repomd.c
src/repomd.h
tests/python/tests/test_repomdrecord.py

index ec81c84f4fd5ed5fc86ae8241c0a4f9cecd8821d..49ec317302c3952932c0c33601db26e947094d3f 100644 (file)
@@ -1034,29 +1034,9 @@ main(int argc, char **argv)
 
     // XML
 
-    pri_xml_rec->checksum_open = cr_safe_string_chunk_insert(
-                                pri_xml_rec->chunk,
-                                pri_stat->checksum);
-    pri_xml_rec->checksum_open_type = cr_safe_string_chunk_insert(
-                                pri_xml_rec->chunk,
-                                cr_checksum_name_str(pri_stat->checksum_type));
-    pri_xml_rec->size_open = pri_stat->size;
-
-    fil_xml_rec->checksum_open = cr_safe_string_chunk_insert(
-                                fil_xml_rec->chunk,
-                                fil_stat->checksum);
-    fil_xml_rec->checksum_open_type = cr_safe_string_chunk_insert(
-                                fil_xml_rec->chunk,
-                                cr_checksum_name_str(fil_stat->checksum_type));
-    fil_xml_rec->size_open = fil_stat->size;
-
-    oth_xml_rec->checksum_open = cr_safe_string_chunk_insert(
-                                oth_xml_rec->chunk,
-                                oth_stat->checksum);
-    oth_xml_rec->checksum_open_type = cr_safe_string_chunk_insert(
-                                oth_xml_rec->chunk,
-                                cr_checksum_name_str(oth_stat->checksum_type));
-    oth_xml_rec->size_open = oth_stat->size;
+    cr_repomd_record_load_contentstat(pri_xml_rec, pri_stat);
+    cr_repomd_record_load_contentstat(fil_xml_rec, fil_stat);
+    cr_repomd_record_load_contentstat(oth_xml_rec, oth_stat);
 
     cr_repomd_record_fill(pri_xml_rec, cmd_options->checksum_type, NULL);
     cr_repomd_record_fill(fil_xml_rec, cmd_options->checksum_type, NULL);
index 20ffd6be6089c532e96df41b357d0766560795e0..9251a0891eb4734e8f4ca1040f4dd48ddc1148ad 100644 (file)
@@ -1201,29 +1201,9 @@ dump_merged_metadata(GHashTable *merged_hashtable,
 
     // XML
 
-    pri_xml_rec->checksum_open = cr_safe_string_chunk_insert(
-                                pri_xml_rec->chunk,
-                                pri_stat->checksum);
-    pri_xml_rec->checksum_open_type = cr_safe_string_chunk_insert(
-                                pri_xml_rec->chunk,
-                                cr_checksum_name_str(pri_stat->checksum_type));
-    pri_xml_rec->size_open = pri_stat->size;
-
-    fil_xml_rec->checksum_open = cr_safe_string_chunk_insert(
-                                fil_xml_rec->chunk,
-                                fil_stat->checksum);
-    fil_xml_rec->checksum_open_type = cr_safe_string_chunk_insert(
-                                fil_xml_rec->chunk,
-                                cr_checksum_name_str(fil_stat->checksum_type));
-    fil_xml_rec->size_open = fil_stat->size;
-
-    oth_xml_rec->checksum_open = cr_safe_string_chunk_insert(
-                                oth_xml_rec->chunk,
-                                oth_stat->checksum);
-    oth_xml_rec->checksum_open_type = cr_safe_string_chunk_insert(
-                                oth_xml_rec->chunk,
-                                cr_checksum_name_str(oth_stat->checksum_type));
-    oth_xml_rec->size_open = oth_stat->size;
+    cr_repomd_record_load_contentstat(pri_xml_rec, pri_stat);
+    cr_repomd_record_load_contentstat(fil_xml_rec, fil_stat);
+    cr_repomd_record_load_contentstat(oth_xml_rec, oth_stat);
 
     cr_repomd_record_fill(pri_xml_rec, CR_CHECKSUM_SHA256, NULL);
     cr_repomd_record_fill(fil_xml_rec, CR_CHECKSUM_SHA256, NULL);
index 80176eef6fd1ade8dbbb39b5345af1dde31cf4fc..3aaa834486bb90165905789dc9e93e0e757cbe8c 100644 (file)
@@ -24,6 +24,7 @@
 #include "repomdrecord-py.h"
 #include "exception-py.h"
 #include "typeconversion.h"
+#include "contentstat-py.h"
 
 typedef struct {
     PyObject_HEAD
@@ -207,11 +208,30 @@ rename_file(_RepomdRecordObject *self, void *nothing)
     Py_RETURN_NONE;
 }
 
+static PyObject *
+load_contentstat(_RepomdRecordObject *self, PyObject *args)
+{
+    PyObject *contentstat;
+
+    if (!PyArg_ParseTuple(args, "O!:load_contentstat",
+                          &ContentStat_Type,
+                          &contentstat))
+        return NULL;
+
+    if (check_RepomdRecordStatus(self))
+        return NULL;
+
+    cr_repomd_record_load_contentstat(self->record,
+                                      ContentStat_FromPyObject(contentstat));
+    Py_RETURN_NONE;
+}
+
 static struct PyMethodDef repomdrecord_methods[] = {
     {"copy", (PyCFunction)copy_repomdrecord, METH_NOARGS, NULL},
     {"fill", (PyCFunction)fill, METH_VARARGS, NULL},
     {"compress_and_fill", (PyCFunction)compress_and_fill, METH_VARARGS, NULL},
     {"rename_file", (PyCFunction)rename_file, METH_NOARGS, NULL},
+    {"load_contentstat", (PyCFunction)load_contentstat, METH_VARARGS, NULL},
     {NULL} /* sentinel */
 };
 
index 98802d2b2ef94734a6b4d8d99593563bf32c728e..1ad9e2fcca2190fb416ddf14029be09b195cd767 100644 (file)
@@ -589,6 +589,20 @@ cr_repomd_record_rename_file(cr_RepomdRecord *md, GError **err)
     return CRE_OK;
 }
 
+void
+cr_repomd_record_load_contentstat(cr_RepomdRecord *record,
+                                  cr_ContentStat *stats)
+{
+    if (!stats)
+        return;
+
+    record->checksum_open = cr_safe_string_chunk_insert(record->chunk,
+                                                        stats->checksum);
+    record->checksum_open_type = cr_safe_string_chunk_insert(record->chunk,
+                                cr_checksum_name_str(stats->checksum_type));
+    record->size_open = stats->size;
+}
+
 cr_Repomd *
 cr_repomd_new()
 {
index 1767315adf66553d807a32a81e4791f9b546e5eb..1acec822ec8129eb808bf866ee353925ea1850f1 100644 (file)
@@ -131,8 +131,11 @@ cr_RepomdRecord *cr_repomd_record_copy(const cr_RepomdRecord *orig);
 
 /** Fill unfilled items in the cr_RepomdRecord (calculate checksums,
  * get file size before/after compression, etc.).
- * Note: For groupfile you shoud use cr_repomd_record_compress_and_fill
- * function.
+ * Note: If checksum_open, checksum_open_type and size_open are filed
+ * then their calculation will be skiped. This items could be filled
+ * directly on our own or use function for load them from a cr_ContentStat.
+ * If no open stats are supplied, then this function has to decompress
+ * the file for the open checksum calculation.
  * @param record                cr_RepomdRecord object
  * @param checksum_type         type of checksum to use
  * @param err                   GError **
@@ -166,6 +169,14 @@ int cr_repomd_record_compress_and_fill(cr_RepomdRecord *record,
  */
 int cr_repomd_record_rename_file(cr_RepomdRecord *record, GError **err);
 
+/** Load the open stats (checksum_open, checksum_open_type and size_open)
+ * from the cr_ContentStat object.
+ * @param record                cr_RepomdRecord
+ * @param stats                 cr_ContentStat
+ */
+void cr_repomd_record_load_contentstat(cr_RepomdRecord *record,
+                                       cr_ContentStat *stats);
+
 /** Create new empty cr_Repomd object wich represents content of repomd.xml.
  */
 cr_Repomd *cr_repomd_new();
index 36c2251b7b7ae8c0778cf8c123ef80fb5223e18d..f5b4b6c506b1f43730a61374e24c76967a3f1865 100644 (file)
@@ -128,3 +128,22 @@ class TestCaseRepomdRecord(unittest.TestCase):
             sorted(['10091f8e2e235ae875cb18c91c443891c7f1a599d41f44d518e8af759a6c8109-primary.xml.gz',
                     'b33fc63178d852333a826385bc15d9b72cb6658be7fb927ec28c4e40b5d426fb-primary.xml']))
 
+    def test_repomdrecord_load_contentstat(self):
+        rec = cr.RepomdRecord("primary", None)
+        self.assertTrue(rec)
+
+        stat = cr.ContentStat(cr.SHA256)
+        stat.checksum = "foobar"
+        stat.checksum_type = cr.SHA256
+        stat.size = 123
+
+        self.assertEqual(rec.checksum_open, None)
+        self.assertEqual(rec.checksum_open_type, None)
+        self.assertEqual(rec.size, 0)
+
+        rec.load_contentstat(stat);
+
+        self.assertEqual(rec.checksum_open, "foobar")
+        self.assertEqual(rec.checksum_open_type, "sha256")
+        self.assertEqual(rec.size_open, 123)
+