rest: delete images that belong to deleted centers

When og_cmd_post_center_delete is invoked by the REST API the
images that contain the same id as the center being deleted are
also deleted.

Move the image deletion functionality into its own function called
og_delete_image to prevent code duplication as it is now required
in og_cmd_post_center_delete and og_cmd_delete_image.
master
Alejandro Sirgo Rica 2024-02-19 12:28:28 +01:00 committed by OpenGnSys Support Team
parent abfe81c1fa
commit 5ea25a11b2
1 changed files with 79 additions and 44 deletions

View File

@ -3354,12 +3354,59 @@ static int og_cmd_restore_image(json_t *element, struct og_msg_params *params)
clients);
}
static int og_cmd_delete_image(json_t *element, struct og_msg_params *params)
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 *key, *image;
struct og_dbi *dbi;
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;
const char *key;
json_t *value;
int err = 0;
@ -3386,49 +3433,11 @@ static int og_cmd_delete_image(json_t *element, struct og_msg_params *params)
return -1;
}
result = dbi_conn_queryf(dbi->conn,
"SELECT nombreca FROM imagenes "
"WHERE idimagen='%s'",
params->id);
if (!result) {
err = og_delete_image(dbi, atoi(params->id));
if (err < 0) {
og_dbi_close(dbi);
syslog(LOG_ERR, "failed to query database\n");
return -1;
return err;
}
if (!dbi_result_next_row(result)) {
dbi_result_free(result);
og_dbi_close(dbi);
syslog(LOG_ERR, "image does not exist in database\n");
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='%s'",
params->id);
if (!result) {
og_dbi_close(dbi);
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);
og_dbi_close(dbi);
return -1;
}
dbi_result_free(result);
unlink(filename);
unlink(checksum);
og_dbi_close(dbi);
@ -5553,6 +5562,7 @@ static int og_cmd_post_center_delete(json_t *element,
const char *key, *msglog;
struct og_dbi *dbi;
dbi_result result;
uint32_t image_id;
json_t *value;
int err = 0;
@ -5597,7 +5607,32 @@ static int og_cmd_post_center_delete(json_t *element,
dbi_result_free(result);
result = dbi_conn_queryf(dbi->conn,
"SELECT idimagen FROM imagenes WHERE idcentro=%s",
params->id);
if (!result) {
dbi_conn_error(dbi->conn, &msglog);
syslog(LOG_ERR, "failed to query database (%s:%d) %s\n",
__func__, __LINE__, msglog);
og_dbi_close(dbi);
return -1;
}
while (dbi_result_next_row(result)) {
image_id = dbi_result_get_uint(result, "idimagen");
err = og_delete_image(dbi, image_id);
if (err < 0) {
dbi_result_free(result);
og_dbi_close(dbi);
return err;
}
}
dbi_result_free(result);
og_dbi_close(dbi);
return 0;
}