summaryrefslogtreecommitdiffstats
path: root/src/dbi.c
diff options
context:
space:
mode:
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;