summaryrefslogtreecommitdiffstats
path: root/src
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
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')
-rw-r--r--src/dbi.c51
-rw-r--r--src/dbi.h1
-rw-r--r--src/rest.c53
3 files changed, 54 insertions, 51 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;
diff --git a/src/dbi.h b/src/dbi.h
index 9e8dd6e..46fbd70 100644
--- a/src/dbi.h
+++ b/src/dbi.h
@@ -135,6 +135,7 @@ int og_dbi_get_room_info(struct og_dbi *dbi, struct og_room *room,
int og_dbi_get_center_info(struct og_dbi *dbi, struct og_center *center);
bool og_dbi_get_image(struct og_dbi *dbi, struct og_image *image);
int og_dbi_add_image(struct og_dbi *dbi, struct og_image *image);
+int og_dbi_delete_image(struct og_dbi *dbi, const uint32_t image_id);
int og_dbi_schema_update(void);
diff --git a/src/rest.c b/src/rest.c
index 3f10685..dee85e1 100644
--- a/src/rest.c
+++ b/src/rest.c
@@ -3814,55 +3814,6 @@ static int og_cmd_restore_image(json_t *element, struct og_msg_params *params)
body);
}
-static int og_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;
-}
-
static int og_cmd_delete_image(json_t *element, struct og_msg_params *params)
{
struct og_dbi *dbi;
@@ -3893,7 +3844,7 @@ static int og_cmd_delete_image(json_t *element, struct og_msg_params *params)
return -1;
}
- err = og_delete_image(dbi, atoi(params->id));
+ err = og_dbi_delete_image(dbi, atoi(params->id));
if (err < 0) {
og_dbi_close(dbi);
return err;
@@ -4771,7 +4722,7 @@ static int og_cmd_post_center_delete(json_t *element,
while (dbi_result_next_row(result)) {
image_id = dbi_result_get_uint(result, "idimagen");
- err = og_delete_image(dbi, image_id);
+ err = og_dbi_delete_image(dbi, image_id);
if (err < 0) {
dbi_result_free(result);
og_dbi_close(dbi);