summaryrefslogtreecommitdiffstats
path: root/src/dbi.c
diff options
context:
space:
mode:
authorAlejandro Sirgo Rica <asirgo@soleta.eu>2024-10-04 11:53:35 +0200
committerOpenGnSys Support Team <soporte-og@soleta.eu>2024-10-04 12:00:22 +0200
commitd6d15a753ea9abc98f8e401adc031b67679b34c6 (patch)
tree23cfc5aeb446dc49f3c3acc53054f2ecc68fa149 /src/dbi.c
parentd1734b30ec57588fe6fbaffe3482453b92e5a949 (diff)
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.
Diffstat (limited to 'src/dbi.c')
-rw-r--r--src/dbi.c51
1 files changed, 51 insertions, 0 deletions
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 <arpa/inet.h>
#include <string.h>
#include "dbi.h"
+#include "cfg.h"
#include <syslog.h>
#include <string.h>
+#include <unistd.h>
#include <stdio.h>
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;