sqliterepo: Add checksum autodetection
authorTomas Mlcoch <tmlcoch@redhat.com>
Tue, 5 May 2015 16:03:15 +0000 (18:03 +0200)
committerTomas Mlcoch <tmlcoch@redhat.com>
Tue, 5 May 2015 16:03:15 +0000 (18:03 +0200)
src/sqliterepo_c.c

index ff9a69390bc170ef999751e437c3154c563176da..48e8296db8d3566c3a0f549c8a79b81498fc2c4e 100644 (file)
@@ -42,6 +42,8 @@
 #include "xml_dump.h"
 
 
+#define DEFAULT_CHECKSUM    CR_CHECKSUM_SHA256
+
 /**
  * Command line options
  */
@@ -85,7 +87,7 @@ sqliterepocmdoptions_new(void)
     options->chcksum_type = NULL;
     options->local_sqlite = FALSE;
     options->compression_type = CR_CW_BZ2_COMPRESSION;
-    options->checksum_type = CR_CHECKSUM_SHA256;
+    options->checksum_type = CR_CHECKSUM_UNKNOWN;
 
     return options;
 }
@@ -433,12 +435,6 @@ compress_sqlite_dbs(const gchar *tmp_out_repo,
     cr_repomd_record_load_contentstat(fil_db_rec, fil_db_task->stat);
     cr_repomd_record_load_contentstat(oth_db_rec, oth_db_task->stat);
 
-    printf("%s\n", pri_db_task->stat->checksum);
-    printf("%s\n", pri_db_rec->checksum ? pri_db_rec->checksum : "");
-    printf("%s\n", pri_db_rec->checksum_type ? pri_db_rec->checksum_type : "");
-    printf("%s\n", pri_db_rec->checksum_open ? pri_db_rec->checksum_open : "");
-    printf("%s\n", pri_db_rec->checksum_open_type ? pri_db_rec->checksum_open_type : "");
-
     // Free the compression tasks
     cr_compressiontask_free(pri_db_task, NULL);
     cr_compressiontask_free(fil_db_task, NULL);
@@ -827,6 +823,28 @@ generate_sqlite_from_xml(const gchar *path,
         return FALSE;
     }
 
+    // Auto-detect used checksum algorithm if not specified explicitly
+    if (checksum_type == CR_CHECKSUM_UNKNOWN) {
+        cr_RepomdRecord *rec = cr_repomd_get_record(repomd, "primary");
+
+        if (!rec) {
+            g_set_error(err, CREATEREPO_C_ERROR, CRE_ERROR,
+                        "repomd.xml is missing primary metadata");
+            return FALSE;
+        }
+
+        if (rec->checksum_type)
+            checksum_type = cr_checksum_type(rec->checksum_type);
+        else if (rec->checksum_open_type)
+            checksum_type = cr_checksum_type(rec->checksum_open_type);
+
+        if (checksum_type == CR_CHECKSUM_UNKNOWN) {
+            g_debug("Cannot auto-detect checksum type, using default %s",
+                    cr_checksum_name_str(DEFAULT_CHECKSUM));
+            checksum_type = DEFAULT_CHECKSUM;
+        }
+    }
+
     // Open sqlite databases
     _cleanup_free_ gchar *pri_db_filename = NULL;
     _cleanup_free_ gchar *fil_db_filename = NULL;