diff options
Diffstat (limited to 'src/analysis/db/snapshot.c')
-rw-r--r-- | src/analysis/db/snapshot.c | 140 |
1 files changed, 81 insertions, 59 deletions
diff --git a/src/analysis/db/snapshot.c b/src/analysis/db/snapshot.c index d75ac49..a5b7280 100644 --- a/src/analysis/db/snapshot.c +++ b/src/analysis/db/snapshot.c @@ -29,7 +29,6 @@ #include <stdio.h> #include <string.h> #include <unistd.h> -#include <openssl/rand.h> #include <i18n.h> @@ -55,7 +54,7 @@ typedef struct _snapshot_node_t char *name; /* Nom de l'instantané */ char *desc; /* Description associée */ - char *id; /* Identifiant attribué */ + snapshot_id_t id; /* Identifiant attribué */ char *path; /* Fichier extrait */ struct _snapshot_node_t **children; /* Sous-noeuds rattachés */ @@ -65,7 +64,7 @@ typedef struct _snapshot_node_t /* Constitue un nouveau noeud d'instantané. */ -static snapshot_node_t *create_snapshot_node(const char *); +static snapshot_node_t *create_snapshot_node(const snapshot_id_t *); /* Libère la mémoire occupée par un noeud d'instantané. */ static void destroy_snapshot_node(snapshot_node_t *); @@ -80,7 +79,7 @@ static bool check_snapshot_nodes(const snapshot_node_t *); static DBError save_snapshot_node(const snapshot_node_t *, xmlDocPtr, xmlXPathContextPtr, struct archive *); /* Recherche le noeud d'instantané lié à un identifiant. */ -static snapshot_node_t *find_snapshot_node(snapshot_node_t *, const char *); +static snapshot_node_t *find_snapshot_node(snapshot_node_t *, const snapshot_id_t *); /* Ajoute un instantané comme prolongement d'un instantané. */ static void add_snapshot_node(snapshot_node_t *, snapshot_node_t *); @@ -145,36 +144,23 @@ static GDbSnapshot *g_db_snapshot_new(const char *, const char *); * * ******************************************************************************/ -static snapshot_node_t *create_snapshot_node(const char *id) +static snapshot_node_t *create_snapshot_node(const snapshot_id_t *id) { snapshot_node_t *result; /* Nouvel instantané à renvoyer*/ - int ret; /* Bilan d'une génération */ - unsigned char rand[32]; /* Tirage aléatoire */ - size_t i; /* Boucle de parcours */ - char _id[65]; /* Identifiant nouveau */ - - static char *alphabet = "0123456789abcdef"; + snapshot_id_t _id; /* Identifiant nouveau */ + bool status; /* Bilan d'une génération */ if (id == NULL) { - ret = RAND_bytes(rand, sizeof(rand)); + status = init_snapshot_id(&_id); - if (ret != 1) + if (!status) { - LOG_ERROR_OPENSSL; result = NULL; goto exit; } - for (i = 0; i < sizeof(rand); i++) - { - _id[i * 2 + 0] = alphabet[rand[i] & 0xf]; - _id[i * 2 + 1] = alphabet[(rand[i] >> 4) & 0xf]; - } - - _id[64] = '\0'; - - id = _id; + id = &_id; } @@ -185,7 +171,7 @@ static snapshot_node_t *create_snapshot_node(const char *id) result->name = NULL; result->desc = NULL; - result->id = strdup(id); + copy_snapshot_id(&result->id, id); result->path = NULL; result->children = NULL; @@ -224,8 +210,6 @@ static void destroy_snapshot_node(snapshot_node_t *node) if (node->desc != NULL) free(node->desc); - free(node->id); - if (node->path != NULL) { ret = unlink(node->path); @@ -262,7 +246,9 @@ static bool setup_snapshot_node_db_path(snapshot_node_t *node, const char *tmpdi bool result; /* Bilan à retourner */ int ret; /* Bilan d'une génération */ - ret = asprintf(&node->path, "%s" G_DIR_SEPARATOR_S "%s_%s_db.sql", tmpdir, hash, node->id); + ret = asprintf(&node->path, "%s" G_DIR_SEPARATOR_S "%s_%s_db.sql", + tmpdir, hash, snapshot_id_as_string(&node->id)); + result = (ret > 0); if (result) @@ -296,7 +282,8 @@ static bool check_snapshot_nodes(const snapshot_node_t *node) result = (node->path != NULL); if (!result) - log_variadic_message(LMT_ERROR, _("Database is missing for snapshot '%s'"), node->id); + log_variadic_message(LMT_ERROR, _("Database is missing for snapshot '%s'"), + snapshot_id_as_string(&node->id)); for (i = 0; i < node->count && result; i++) result = check_snapshot_nodes(node->children[i]); @@ -333,7 +320,7 @@ static DBError save_snapshot_node(const snapshot_node_t *node, xmlDocPtr xdoc, x /* Sauvegarde de la base de données */ - ret = asprintf(&name, "%s.db", node->id); + ret = asprintf(&name, "%s.db", snapshot_id_as_string(&node->id)); if (ret < 0) { @@ -382,7 +369,7 @@ static DBError save_snapshot_node(const snapshot_node_t *node, xmlDocPtr xdoc, x goto exit; } - status = _add_string_attribute_to_node(xml_node, "id", node->id); + status = _add_string_attribute_to_node(xml_node, "id", snapshot_id_as_string(&node->id)); if (!status) { @@ -392,7 +379,7 @@ static DBError save_snapshot_node(const snapshot_node_t *node, xmlDocPtr xdoc, x if (node->parent != NULL) { - status = _add_string_attribute_to_node(xml_node, "parent", node->parent->id); + status = _add_string_attribute_to_node(xml_node, "parent", snapshot_id_as_string(&node->parent->id)); if (!status) { @@ -429,12 +416,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 char *id) +static snapshot_node_t *find_snapshot_node(snapshot_node_t *node, const snapshot_id_t *id) { snapshot_node_t *result; /* Noeud trouvé à renvoyer */ size_t i; /* Boucle de parcours */ - if (strcmp(node->id, id) == 0) + if (cmp_snapshot_id(&node->id, id) == 0) result = node; else @@ -697,9 +684,11 @@ GDbSnapshot *g_db_snapshot_new_from_xml(const char *tmpdir, const char *hash, xm size_t count; /* Nombre de contenus premiers */ size_t i; /* Boucle de parcours */ xmlNodePtr xml_node; /* Noeud XML avec propriétés */ - char *parent_id; /* Identifiant de noeud parent */ + char *raw_id; /* Identifiant brut à convertir*/ + snapshot_id_t parent_id; /* Identifiant de noeud parent */ snapshot_node_t *parent; /* Instantané parent trouvé */ - char *node_id; /* Identifiant de noeud courant*/ + snapshot_id_t node_id; /* Identifiant de noeud courant*/ + bool status; /* Bilan d'une conversion */ snapshot_node_t *node; /* Instantané nouveau constitué*/ result = g_db_snapshot_new(tmpdir, hash); @@ -714,33 +703,47 @@ GDbSnapshot *g_db_snapshot_new_from_xml(const char *tmpdir, const char *hash, xm { xml_node = NODE_FROM_PATH_OBJ(xobject, i); - parent_id = qck_get_node_prop_value(xml_node, "parent"); + raw_id = qck_get_node_prop_value(xml_node, "parent"); - if (parent_id == NULL) + if (raw_id == NULL) parent = NULL; else { if (result->nodes == NULL) parent = NULL; + else - parent = find_snapshot_node(result->nodes, parent_id); + { + status = init_snapshot_id_from_text(&node_id, raw_id); + + if (status) + parent = find_snapshot_node(result->nodes, &parent_id); + else + parent = NULL; - free(parent_id); + } + + free(raw_id); if (parent == NULL) goto bad_xml; } - node_id = qck_get_node_prop_value(xml_node, "id"); + raw_id = qck_get_node_prop_value(xml_node, "id"); - if (node_id == NULL) + if (raw_id == NULL) goto bad_xml; - node = create_snapshot_node(node_id); + status = init_snapshot_id_from_text(&node_id, raw_id); + + free(raw_id); - free(node_id); + if (!status) + goto bad_xml; + + node = create_snapshot_node(&node_id); if (node == NULL) goto bad_xml; @@ -763,14 +766,20 @@ GDbSnapshot *g_db_snapshot_new_from_xml(const char *tmpdir, const char *hash, xm /* Détermination de l'instantané courant */ - node_id = get_node_text_value(context, "/ChrysalideBinary/CurrentSnapshot"); + raw_id = get_node_text_value(context, "/ChrysalideBinary/CurrentSnapshot"); - if (node_id == NULL) + if (raw_id == NULL) result->current = result->nodes; + else { - result->current = find_snapshot_node(result->nodes, node_id); - free(node_id); + status = init_snapshot_id_from_text(&node_id, raw_id); + + free(raw_id); + + if (status) + result->current = find_snapshot_node(result->nodes, &node_id); + } if (result->current == NULL) @@ -812,7 +821,9 @@ bool g_db_snapshot_fill(GDbSnapshot *snap, struct archive *archive) int ret; /* Bilan d'un appel */ const char *path; /* Désignation d'un fichier */ const char *dot; /* Début de l'extension */ - char *node_id; /* Identifiant de noeud */ + char *raw_id; /* Identifiant brut à convertir*/ + snapshot_id_t node_id; /* Identifiant de noeud courant*/ + bool status; /* Bilan d'une conversion */ snapshot_node_t *node; /* Instantané trouvé */ result = false; @@ -826,11 +837,16 @@ bool g_db_snapshot_fill(GDbSnapshot *snap, struct archive *archive) if (!_endswith(path, ".db", &dot)) continue; - node_id = strndup(path, dot - path); + raw_id = strndup(path, dot - path); + + status = init_snapshot_id_from_text(&node_id, raw_id); - node = find_snapshot_node(snap->nodes, node_id); + free(raw_id); - free(node_id); + if (!status) + break; + + node = find_snapshot_node(snap->nodes, &node_id); if (!setup_snapshot_node_db_path(node, snap->tmpdir, snap->hash)) break; @@ -877,7 +893,8 @@ DBError g_db_snapshot_save(const GDbSnapshot *snap, xmlDocPtr xdoc, xmlXPathCont assert(snap->current != NULL); - status = add_content_to_node(xdoc, context, "/ChrysalideBinary/CurrentSnapshot", snap->current->id); + status = add_content_to_node(xdoc, context, "/ChrysalideBinary/CurrentSnapshot", + snapshot_id_as_string(&snap->current->id)); if (!status) result = DBE_XML_ERROR; @@ -893,25 +910,30 @@ DBError g_db_snapshot_save(const GDbSnapshot *snap, xmlDocPtr xdoc, xmlXPathCont /****************************************************************************** * * * Paramètres : snap = gestionnaire d'instantanés à consulter. * +* id = identifiant de l'instantané courant. [OUT] * * * * Description : Fournit l'identifiant de l'instanné courant. * * * -* Retour : Identifiant de l'instantané courant. * +* Retour : Validité de la trouvaille. * * * * Remarques : - * * * ******************************************************************************/ -const char *g_db_snapshot_get_current_id(const GDbSnapshot *snap) +bool g_db_snapshot_get_current_id(const GDbSnapshot *snap, snapshot_id_t *id) { - char *result; /* Indentifiant à retourner */ + bool result; /* Bilan à retourner */ assert(snap->current != NULL); if (snap->current == NULL) - result = NULL; + result = false; + else - result = snap->current->id; + { + copy_snapshot_id(id, &snap->current->id); + result = true; + } return result; @@ -931,7 +953,7 @@ const char *g_db_snapshot_get_current_id(const GDbSnapshot *snap) * * ******************************************************************************/ -sqlite3 *g_db_snapshot_get_database(const GDbSnapshot *snap, const char *id) +sqlite3 *g_db_snapshot_get_database(const GDbSnapshot *snap, const snapshot_id_t *id) { sqlite3 *result; /* Base SQLite à retourner */ snapshot_node_t *node; /* Instantané trouvé */ @@ -941,7 +963,7 @@ sqlite3 *g_db_snapshot_get_database(const GDbSnapshot *snap, const char *id) if (node == NULL) { - log_variadic_message(LMT_ERROR, _("Snapshot not found for id '%s'"), id); + log_variadic_message(LMT_ERROR, _("Snapshot not found for id '%s'"), *id); result = NULL; } |