[recipes/image-download: 11/12] Add version information to the keyfiles



commit bb50d35a6977de90a0705d7d06f814e3178d1cc2
Author: Matthias Clasen <mclasen redhat com>
Date:   Sun Apr 2 12:04:21 2017 -0400

    Add version information to the keyfiles
    
    This is just to prepare for the eventuality that
    we may have to make changes to the key file format
    in the future.

 data/chefs.db         |    3 +++
 data/recipes.db       |    3 +++
 src/gr-recipe-store.c |   47 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 53 insertions(+), 0 deletions(-)
---
diff --git a/data/chefs.db b/data/chefs.db
index 0616647..32d43d3 100644
--- a/data/chefs.db
+++ b/data/chefs.db
@@ -1,3 +1,6 @@
+[Metadata]
+Version=1
+
 [aday]
 Image=images/allan_day.jpg
 Name=Allan
diff --git a/data/recipes.db b/data/recipes.db
index b365a2e..706e822 100644
--- a/data/recipes.db
+++ b/data/recipes.db
@@ -1,3 +1,6 @@
+[Metadata]
+Version=1
+
 [R_Neapolitan_Pizza_by_peter]
 Name=Neapolitan Pizza
 Author=peter
diff --git a/src/gr-recipe-store.c b/src/gr-recipe-store.c
index e75787c..d7f339f 100644
--- a/src/gr-recipe-store.c
+++ b/src/gr-recipe-store.c
@@ -183,6 +183,7 @@ load_recipes (GrRecipeStore *self,
         g_auto(GStrv) groups = NULL;
         gsize length, length2;
         int i, j;
+        int version;
 
         keyfile = g_key_file_new ();
 
@@ -195,8 +196,26 @@ load_recipes (GrRecipeStore *self,
                         g_info ("No recipe db at: %s", path);
                 return FALSE;
         }
+
         g_info ("Load recipe db: %s", path);
 
+        version = g_key_file_get_integer (keyfile, "Metadata", "Version", &error);
+        if (error) {
+                if (g_error_matches (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND) ||
+                    g_error_matches (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND)) {
+                        g_info ("No metadata found, assuming version 1");
+                        version = 1;
+                }
+                else {
+                        g_error ("Failed to read recipe db: %s", error->message);
+                        return FALSE;
+                }
+                g_clear_error (&error);
+        }
+        if (version != 1) {
+                g_error ("Don't know how to handle recipe db version %d", version);
+        }
+
         groups = g_key_file_get_groups (keyfile, &length);
         for (i = 0; i < length; i++) {
                 GrRecipe *recipe;
@@ -222,6 +241,9 @@ load_recipes (GrRecipeStore *self,
                 g_autoptr(GDateTime) mtime = NULL;
                 char *tmp;
 
+                if (strcmp (groups[i], "Metadata") == 0)
+                        continue;
+
                 g_clear_error (&error);
 
                 id = groups[i];
@@ -488,6 +510,8 @@ save_recipes (GrRecipeStore *self)
 
         g_info ("Save recipe db: %s", path);
 
+        g_key_file_set_integer (keyfile, "Metadata", "Version", 1);
+
         keys = g_hash_table_get_keys (self->recipes);
         keys = g_list_sort (keys, (GCompareFunc)strcmp);
         for (l = keys; l; l = l->next) {
@@ -711,6 +735,7 @@ load_chefs (GrRecipeStore *self,
         g_auto(GStrv) groups = NULL;
         gsize length;
         int i;
+        int version;
 
         keyfile = g_key_file_new ();
 
@@ -731,6 +756,23 @@ load_chefs (GrRecipeStore *self,
 
         g_info ("Load chefs db: %s", path);
 
+        version = g_key_file_get_integer (keyfile, "Metadata", "Version", &error);
+        if (error) {
+                if (g_error_matches (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND) ||
+                    g_error_matches (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND)) {
+                        g_info ("No metadata found, assuming version 1");
+                        version = 1;
+                }
+                else {
+                        g_error ("Failed to read chefs db: %s", error->message);
+                        return FALSE;
+                }
+                g_clear_error (&error);
+        }
+        if (version != 1) {
+                g_error ("Don't know how to handle chefs db version %d", version);
+        }
+
         groups = g_key_file_get_groups (keyfile, &length);
         for (i = 0; i < length; i++) {
                 GrChef *chef;
@@ -740,6 +782,9 @@ load_chefs (GrRecipeStore *self,
                 g_autofree char *description = NULL;
                 g_autofree char *image_path = NULL;
 
+                if (strcmp (groups[i], "Metadata") == 0)
+                        continue;
+
                 g_clear_error (&error);
 
                 id = groups[i];
@@ -814,6 +859,8 @@ save_chefs (GrRecipeStore *store)
 
         g_info ("Save chefs db: %s", path);
 
+        g_key_file_set_integer (keyfile, "Metadata", "Version", 1);
+
         keys = g_hash_table_get_keys (store->chefs);
         keys = g_list_sort (keys, (GCompareFunc)strcmp);
         for (l = keys; l; l = l->next) {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]