From d6d15a753ea9abc98f8e401adc031b67679b34c6 Mon Sep 17 00:00:00 2001 From: Alejandro Sirgo Rica Date: Fri, 4 Oct 2024 11:53:35 +0200 Subject: src: move og_delete_image to dbi.c Move og_delete_image into dbi.c as og_dbi_delete_image to use it from client.c no functional changes. --- src/dbi.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) (limited to 'src/dbi.c') diff --git a/src/dbi.c b/src/dbi.c index 3122bce..dc8b5d0 100644 --- a/src/dbi.c +++ b/src/dbi.c @@ -12,8 +12,10 @@ #include #include #include "dbi.h" +#include "cfg.h" #include #include +#include #include struct og_dbi *og_dbi_open(struct og_dbi_config *config) @@ -235,6 +237,55 @@ int og_dbi_add_image(struct og_dbi *dbi, struct og_image *image) return 0; } +int og_dbi_delete_image(struct og_dbi *dbi, const uint32_t image_id) +{ + char filename[PATH_MAX + 1], checksum[PATH_MAX + 1]; + const char *image; + dbi_result result; + + result = dbi_conn_queryf(dbi->conn, + "SELECT nombreca FROM imagenes " + "WHERE idimagen='%u'", + image_id); + if (!result) { + syslog(LOG_ERR, "failed to query database\n"); + return -1; + } + if (!dbi_result_next_row(result)) { + dbi_result_free(result); + syslog(LOG_ERR, "image \"%u\" does not exist in database\n", image_id); + return -1; + } + + image = dbi_result_get_string(result, "nombreca"); + snprintf(filename, sizeof(filename), "%s/%s.img", ogconfig.repo.dir, + image); + snprintf(checksum, sizeof(checksum), "%s/%s.img.full.sum", ogconfig.repo.dir, + image); + dbi_result_free(result); + + result = dbi_conn_queryf(dbi->conn, + "DELETE FROM imagenes " + "WHERE idimagen='%u'", + image_id); + if (!result) { + syslog(LOG_ERR, "failed to query database\n"); + return -1; + } + if (dbi_result_get_numrows_affected(result) < 1) { + syslog(LOG_ERR, "delete did not modify any row (%s:%d)\n", + __func__, __LINE__); + dbi_result_free(result); + return -1; + } + dbi_result_free(result); + + unlink(filename); + unlink(checksum); + + return 0; +} + bool og_dbi_get_image(struct og_dbi *dbi, struct og_image *image) { const char *msglog; -- cgit v1.2.3-18-g5258