From bcdf953ef6616c404d013f3473fb12a7bf43440b Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Tue, 15 Oct 2019 08:39:47 +0200
Subject: Switched to snapshot information structures.

---
 src/analysis/db/misc/snapshot.h |   5 ++
 src/analysis/db/snapshot.c      | 153 ++++++++++++++++++++++++++++------------
 2 files changed, 111 insertions(+), 47 deletions(-)

diff --git a/src/analysis/db/misc/snapshot.h b/src/analysis/db/misc/snapshot.h
index e3a003c..37fad7f 100644
--- a/src/analysis/db/misc/snapshot.h
+++ b/src/analysis/db/misc/snapshot.h
@@ -96,6 +96,11 @@ bool init_snapshot_info_from_text(snapshot_info_t *, const char *, uint64_t, con
 /* Libère la mémoire occupée par une description d'instantané. */
 void exit_snapshot_info(snapshot_info_t *);
 
+#define get_snapshot_info_id(nfo) &(nfo)->id
+#define get_snapshot_info_creation(nfo) (nfo)->created
+#define get_snapshot_info_name(nfo) (nfo)->name
+#define get_snapshot_info_desc(nfo) (nfo)->desc
+
 /* Effectue une copie de description d'instantané. */
 void copy_snapshot_info(snapshot_info_t *, const snapshot_info_t *);
 
diff --git a/src/analysis/db/snapshot.c b/src/analysis/db/snapshot.c
index a5b7280..6b762b7 100644
--- a/src/analysis/db/snapshot.c
+++ b/src/analysis/db/snapshot.c
@@ -51,10 +51,8 @@ typedef struct _snapshot_node_t
 {
     struct _snapshot_node_t *parent;        /* Parent hiérarchique         */
 
-    char *name;                             /* Nom de l'instantané         */
-    char *desc;                             /* Description associée        */
+    snapshot_info_t info;                   /* Détails de l'instantané     */
 
-    snapshot_id_t id;                       /* Identifiant attribué        */
     char *path;                             /* Fichier extrait             */
 
     struct _snapshot_node_t **children;     /* Sous-noeuds rattachés       */
@@ -64,7 +62,7 @@ typedef struct _snapshot_node_t
 
 
 /* Constitue un nouveau noeud d'instantané. */
-static snapshot_node_t *create_snapshot_node(const snapshot_id_t *);
+static snapshot_node_t *create_snapshot_node(const char *, uint64_t, const char *, const char *);
 
 /* Libère la mémoire occupée par un noeud d'instantané. */
 static void destroy_snapshot_node(snapshot_node_t *);
@@ -134,7 +132,10 @@ static GDbSnapshot *g_db_snapshot_new(const char *, const char *);
 
 /******************************************************************************
 *                                                                             *
-*  Paramètres  : id = éventuel identifiant prédéfini.                         *
+*  Paramètres  : id      = source de données éventuelle pour l'identifiant.   *
+*                created = source de données pour la date de création.        *
+*                name    = source de données éventuelle pour le nom.          *
+*                desc    = source de données éventuelle pour la description.  *
 *                                                                             *
 *  Description : Constitue un nouveau noeud d'instantané.                     *
 *                                                                             *
@@ -144,34 +145,27 @@ static GDbSnapshot *g_db_snapshot_new(const char *, const char *);
 *                                                                             *
 ******************************************************************************/
 
-static snapshot_node_t *create_snapshot_node(const snapshot_id_t *id)
+static snapshot_node_t *create_snapshot_node(const char *id, uint64_t created, const char *name, const char *desc)
 {
     snapshot_node_t *result;                /* Nouvel instantané à renvoyer*/
-    snapshot_id_t _id;                      /* Identifiant nouveau         */
     bool status;                            /* Bilan d'une génération      */
 
-    if (id == NULL)
-    {
-        status = init_snapshot_id(&_id);
-
-        if (!status)
-        {
-            result = NULL;
-            goto exit;
-        }
-
-        id = &_id;
-
-    }
-
     result = malloc(sizeof(snapshot_node_t));
 
     result->parent = NULL;
 
-    result->name = NULL;
-    result->desc = NULL;
+    if (id == NULL)
+        status = init_snapshot_info(&result->info);
+    else
+        status = init_snapshot_info_from_text(&result->info, id, created, name, desc);
+
+    if (!status)
+    {
+        free(result);
+        result = NULL;
+        goto exit;
+    }
 
-    copy_snapshot_id(&result->id, id);
     result->path = NULL;
 
     result->children = NULL;
@@ -204,11 +198,7 @@ static void destroy_snapshot_node(snapshot_node_t *node)
     for (i = 0; i < node->count; i++)
         destroy_snapshot_node(node->children[i]);
 
-    if (node->name != NULL)
-        free(node->name);
-
-    if (node->desc != NULL)
-        free(node->desc);
+    exit_snapshot_info(&node->info);
 
     if (node->path != NULL)
     {
@@ -244,10 +234,13 @@ static void destroy_snapshot_node(snapshot_node_t *node)
 static bool setup_snapshot_node_db_path(snapshot_node_t *node, const char *tmpdir, const char *hash)
 {
     bool result;                            /* Bilan à retourner           */
+    snapshot_id_t *id;                      /* Identifiant attribué        */
     int ret;                                /* Bilan d'une génération      */
 
+    id = get_snapshot_info_id(&node->info);
+
     ret = asprintf(&node->path, "%s" G_DIR_SEPARATOR_S "%s_%s_db.sql",
-                   tmpdir, hash, snapshot_id_as_string(&node->id));
+                   tmpdir, hash, snapshot_id_as_string(id));
 
     result = (ret > 0);
 
@@ -277,13 +270,16 @@ static bool setup_snapshot_node_db_path(snapshot_node_t *node, const char *tmpdi
 static bool check_snapshot_nodes(const snapshot_node_t *node)
 {
     bool result;                            /* Bilan à retourner           */
+    const snapshot_id_t *id;                /* Identifiant attribué        */
     size_t i;                               /* Boucle de parcours          */
 
     result = (node->path != NULL);
 
     if (!result)
-        log_variadic_message(LMT_ERROR, _("Database is missing for snapshot '%s'"),
-                             snapshot_id_as_string(&node->id));
+    {
+        id = get_snapshot_info_id(&node->info);
+        log_variadic_message(LMT_ERROR, _("Database is missing for snapshot '%s'"), snapshot_id_as_string(id));
+    }
 
     for (i = 0; i < node->count && result; i++)
         result = check_snapshot_nodes(node->children[i]);
@@ -311,16 +307,21 @@ static bool check_snapshot_nodes(const snapshot_node_t *node)
 static DBError save_snapshot_node(const snapshot_node_t *node, xmlDocPtr xdoc, xmlXPathContextPtr context, struct archive *archive)
 {
     DBError result;                         /* Conclusion à retourner      */
+    const snapshot_id_t *id;                /* Identifiant attribué        */
     char *name;                             /* Désignation d'une entrée    */
     int ret;                                /* Bilan d'un appel            */
     CPError error;                          /* Bilan d'une compression     */
     xmlNodePtr xml_node;                    /* Nouveau noeud XML           */
     bool status;                            /* Bilan d'un ajout XML        */
+    timestamp_t created;                    /* Date de création            */
+    const char *value;                      /* Valeur éventuelle à inscrire*/
     size_t i;                               /* Boucle de parcours          */
 
     /* Sauvegarde de la base de données */
 
-    ret = asprintf(&name, "%s.db", snapshot_id_as_string(&node->id));
+    id = get_snapshot_info_id(&node->info);
+
+    ret = asprintf(&name, "%s.db", snapshot_id_as_string(id));
 
     if (ret < 0)
     {
@@ -369,7 +370,17 @@ static DBError save_snapshot_node(const snapshot_node_t *node, xmlDocPtr xdoc, x
         goto exit;
     }
 
-    status = _add_string_attribute_to_node(xml_node, "id", snapshot_id_as_string(&node->id));
+    status = _add_string_attribute_to_node(xml_node, "id", snapshot_id_as_string(id));
+
+    if (!status)
+    {
+        result = DBE_XML_ERROR;
+        goto exit;
+    }
+
+    created = get_snapshot_info_creation(&node->info);
+
+    status = _add_uint64_attribute_to_node(xml_node, "created", created);
 
     if (!status)
     {
@@ -377,9 +388,39 @@ static DBError save_snapshot_node(const snapshot_node_t *node, xmlDocPtr xdoc, x
         goto exit;
     }
 
+    value = get_snapshot_info_name(&node->info);
+
+    if (value != NULL)
+    {
+        status = _add_string_attribute_to_node(xml_node, "name", value);
+
+        if (!status)
+        {
+            result = DBE_XML_ERROR;
+            goto exit;
+        }
+
+    }
+
+    value = get_snapshot_info_desc(&node->info);
+
+    if (value != NULL)
+    {
+        status = _add_string_attribute_to_node(xml_node, "desc", value);
+
+        if (!status)
+        {
+            result = DBE_XML_ERROR;
+            goto exit;
+        }
+
+    }
+
     if (node->parent != NULL)
     {
-        status = _add_string_attribute_to_node(xml_node, "parent", snapshot_id_as_string(&node->parent->id));
+        id = get_snapshot_info_id(&node->parent->info);
+
+        status = _add_string_attribute_to_node(xml_node, "parent", snapshot_id_as_string(id));
 
         if (!status)
         {
@@ -419,9 +460,12 @@ static DBError save_snapshot_node(const snapshot_node_t *node, xmlDocPtr xdoc, x
 static snapshot_node_t *find_snapshot_node(snapshot_node_t *node, const snapshot_id_t *id)
 {
     snapshot_node_t *result;                /* Noeud trouvé à renvoyer     */
+    snapshot_id_t *node_id;                 /* Identifiant attribué        */
     size_t i;                               /* Boucle de parcours          */
 
-    if (cmp_snapshot_id(&node->id, id) == 0)
+    node_id = get_snapshot_info_id(&node->info);
+
+    if (cmp_snapshot_id(node_id, id) == 0)
         result = node;
 
     else
@@ -619,7 +663,7 @@ GDbSnapshot *g_db_snapshot_new_empty(const char *tmpdir, const char *hash, GList
 
     result = g_db_snapshot_new(tmpdir, hash);
 
-    result->nodes = create_snapshot_node(NULL);
+    result->nodes = create_snapshot_node(NULL, 0, NULL, NULL);
 
     status = setup_snapshot_node_db_path(result->nodes, tmpdir, hash);
     if (!status) goto error;
@@ -685,11 +729,14 @@ GDbSnapshot *g_db_snapshot_new_from_xml(const char *tmpdir, const char *hash, xm
     size_t i;                               /* Boucle de parcours          */
     xmlNodePtr xml_node;                    /* Noeud XML avec propriétés   */
     char *raw_id;                           /* Identifiant brut à convertir*/
+    bool status;                            /* Bilan d'une conversion      */
     snapshot_id_t parent_id;                /* Identifiant de noeud parent */
     snapshot_node_t *parent;                /* Instantané parent trouvé    */
-    snapshot_id_t node_id;                  /* Identifiant de noeud courant*/
-    bool status;                            /* Bilan d'une conversion      */
+    uint64_t created;                       /* Date de création            */
+    char *name;                             /* Nom d'instantané            */
+    char *desc;                             /* Description d'instantané    */
     snapshot_node_t *node;                  /* Instantané nouveau constitué*/
+    snapshot_id_t node_id;                  /* Identifiant de noeud courant*/
 
     result = g_db_snapshot_new(tmpdir, hash);
 
@@ -715,7 +762,7 @@ GDbSnapshot *g_db_snapshot_new_from_xml(const char *tmpdir, const char *hash, xm
 
             else
             {
-                status = init_snapshot_id_from_text(&node_id, raw_id);
+                status = init_snapshot_id_from_text(&parent_id, raw_id);
 
                 if (status)
                     parent = find_snapshot_node(result->nodes, &parent_id);
@@ -736,14 +783,24 @@ GDbSnapshot *g_db_snapshot_new_from_xml(const char *tmpdir, const char *hash, xm
         if (raw_id == NULL)
             goto bad_xml;
 
-        status = init_snapshot_id_from_text(&node_id, raw_id);
-
-        free(raw_id);
+        status = qck_get_node_prop_uint64_value(xml_node, "created", &created);
 
         if (!status)
             goto bad_xml;
 
-        node = create_snapshot_node(&node_id);
+        name = qck_get_node_prop_value(xml_node, "name");
+
+        desc = qck_get_node_prop_value(xml_node, "desc");
+
+        node = create_snapshot_node(raw_id, created, name, desc);
+
+        free(raw_id);
+
+        if (name != NULL)
+            free(name);
+
+        if (desc != NULL)
+            free(desc);
 
         if (node == NULL)
             goto bad_xml;
@@ -889,12 +946,14 @@ bool g_db_snapshot_fill(GDbSnapshot *snap, struct archive *archive)
 DBError g_db_snapshot_save(const GDbSnapshot *snap, xmlDocPtr xdoc, xmlXPathContextPtr context, struct archive *archive)
 {
     DBError result;                         /* Conclusion à retourner      */
+    const snapshot_id_t *id;                /* Identifiant attribué        */
     bool status;                            /* Bilan d'un ajout XML        */
 
     assert(snap->current != NULL);
 
-    status = add_content_to_node(xdoc, context, "/ChrysalideBinary/CurrentSnapshot",
-                                 snapshot_id_as_string(&snap->current->id));
+    id = get_snapshot_info_id(&snap->current->info);
+
+    status = add_content_to_node(xdoc, context, "/ChrysalideBinary/CurrentSnapshot", snapshot_id_as_string(id));
 
     if (!status)
         result = DBE_XML_ERROR;
@@ -931,7 +990,7 @@ bool g_db_snapshot_get_current_id(const GDbSnapshot *snap, snapshot_id_t *id)
 
     else
     {
-        copy_snapshot_id(id, &snap->current->id);
+        copy_snapshot_id(id, get_snapshot_info_id(&snap->current->info));
         result = true;
     }
 
-- 
cgit v0.11.2-87-g4458