#941 pass og_dbi to og_dbi_get_computer_info()

Reuse the existing dbi handler, instead of opening a new one.
master
OpenGnSys Support Team 2020-09-18 15:29:47 +02:00
parent 3cb98c7187
commit d7e2022bdd
3 changed files with 55 additions and 52 deletions

View File

@ -129,14 +129,6 @@ static int og_resp_hardware(json_t *data, struct og_client *cli)
return -1;
}
err = og_dbi_get_computer_info(&computer, cli->addr.sin_addr);
if (err < 0)
return -1;
snprintf(legacy.center, sizeof(legacy.center), "%d", computer.center);
snprintf(legacy.id, sizeof(legacy.id), "%d", computer.id);
snprintf(legacy.hardware, sizeof(legacy.hardware), "%s", hardware);
dbi = og_dbi_open(&dbi_config);
if (!dbi) {
syslog(LOG_ERR, "cannot open connection database (%s:%d)\n",
@ -144,6 +136,16 @@ static int og_resp_hardware(json_t *data, struct og_client *cli)
return -1;
}
err = og_dbi_get_computer_info(dbi, &computer, cli->addr.sin_addr);
if (err < 0) {
og_dbi_close(dbi);
return -1;
}
snprintf(legacy.center, sizeof(legacy.center), "%d", computer.center);
snprintf(legacy.id, sizeof(legacy.id), "%d", computer.id);
snprintf(legacy.hardware, sizeof(legacy.hardware), "%s", hardware);
res = actualizaHardware(dbi, legacy.hardware, legacy.id, computer.name,
legacy.center);
og_dbi_close(dbi);
@ -195,15 +197,6 @@ static int og_resp_software(json_t *data, struct og_client *cli)
return -1;
}
err = og_dbi_get_computer_info(&computer, cli->addr.sin_addr);
if (err < 0)
return -1;
snprintf(legacy.software, sizeof(legacy.software), "%s", software);
snprintf(legacy.part, sizeof(legacy.part), "%s", partition);
snprintf(legacy.id, sizeof(legacy.id), "%d", computer.id);
snprintf(legacy.center, sizeof(legacy.center), "%d", computer.center);
dbi = og_dbi_open(&dbi_config);
if (!dbi) {
syslog(LOG_ERR, "cannot open connection database (%s:%d)\n",
@ -211,6 +204,17 @@ static int og_resp_software(json_t *data, struct og_client *cli)
return -1;
}
err = og_dbi_get_computer_info(dbi, &computer, cli->addr.sin_addr);
if (err < 0) {
og_dbi_close(dbi);
return -1;
}
snprintf(legacy.software, sizeof(legacy.software), "%s", software);
snprintf(legacy.part, sizeof(legacy.part), "%s", partition);
snprintf(legacy.id, sizeof(legacy.id), "%d", computer.id);
snprintf(legacy.center, sizeof(legacy.center), "%d", computer.center);
res = actualizaSoftware(dbi, legacy.software, legacy.part, legacy.id,
computer.name, legacy.center);
og_dbi_close(dbi);
@ -310,10 +314,6 @@ static int og_resp_refresh(json_t *data, struct og_client *cli)
return err;
}
err = og_dbi_get_computer_info(&computer, cli->addr.sin_addr);
if (err < 0)
return -1;
if (strlen(serial_number) > 0)
snprintf(cfg, sizeof(cfg), "ser=%s\n", serial_number);
@ -349,6 +349,13 @@ static int og_resp_refresh(json_t *data, struct og_client *cli)
__func__, __LINE__);
return -1;
}
err = og_dbi_get_computer_info(dbi, &computer, cli->addr.sin_addr);
if (err < 0) {
og_dbi_close(dbi);
return -1;
}
res = actualizaConfiguracion(dbi, cfg, computer.id);
og_dbi_close(dbi);
@ -453,9 +460,18 @@ static int og_resp_image_create(json_t *data, struct og_client *cli)
return -1;
}
err = og_dbi_get_computer_info(&computer, cli->addr.sin_addr);
if (err < 0)
dbi = og_dbi_open(&dbi_config);
if (!dbi) {
syslog(LOG_ERR, "cannot open connection database (%s:%d)\n",
__func__, __LINE__);
return -1;
}
err = og_dbi_get_computer_info(dbi, &computer, cli->addr.sin_addr);
if (err < 0) {
og_dbi_close(dbi);
return -1;
}
snprintf(soft_legacy.center, sizeof(soft_legacy.center), "%d",
computer.center);
@ -470,13 +486,6 @@ static int og_resp_image_create(json_t *data, struct og_client *cli)
snprintf(img_legacy.name, sizeof(img_legacy.name), "%s", name);
snprintf(img_legacy.repo, sizeof(img_legacy.repo), "%s", repo);
dbi = og_dbi_open(&dbi_config);
if (!dbi) {
syslog(LOG_ERR, "cannot open connection database (%s:%d)\n",
__func__, __LINE__);
return -1;
}
res = actualizaSoftware(dbi,
soft_legacy.software,
img_legacy.part,
@ -551,16 +560,6 @@ static int og_resp_image_restore(json_t *data, struct og_client *cli)
return -1;
}
err = og_dbi_get_computer_info(&computer, cli->addr.sin_addr);
if (err < 0)
return -1;
snprintf(img_legacy.image_id, sizeof(img_legacy.image_id), "%s",
image_id);
snprintf(img_legacy.part, sizeof(img_legacy.part), "%s", partition);
snprintf(img_legacy.disk, sizeof(img_legacy.disk), "%s", disk);
snprintf(soft_legacy.id, sizeof(soft_legacy.id), "%d", computer.id);
dbi = og_dbi_open(&dbi_config);
if (!dbi) {
syslog(LOG_ERR, "cannot open connection database (%s:%d)\n",
@ -587,6 +586,18 @@ static int og_resp_image_restore(json_t *data, struct og_client *cli)
"%d", dbi_result_get_uint(query_result, "idperfilsoft"));
dbi_result_free(query_result);
err = og_dbi_get_computer_info(dbi, &computer, cli->addr.sin_addr);
if (err < 0) {
og_dbi_close(dbi);
return -1;
}
snprintf(img_legacy.image_id, sizeof(img_legacy.image_id), "%s",
image_id);
snprintf(img_legacy.part, sizeof(img_legacy.part), "%s", partition);
snprintf(img_legacy.disk, sizeof(img_legacy.disk), "%s", disk);
snprintf(soft_legacy.id, sizeof(soft_legacy.id), "%d", computer.id);
res = actualizaRestauracionImagen(dbi,
img_legacy.image_id,
img_legacy.disk,

View File

@ -49,18 +49,12 @@ void og_dbi_close(struct og_dbi *dbi)
free(dbi);
}
int og_dbi_get_computer_info(struct og_computer *computer, struct in_addr addr)
int og_dbi_get_computer_info(struct og_dbi *dbi, struct og_computer *computer,
struct in_addr addr)
{
const char *msglog;
struct og_dbi *dbi;
dbi_result result;
dbi = og_dbi_open(&dbi_config);
if (!dbi) {
syslog(LOG_ERR, "cannot open connection database (%s:%d)\n",
__func__, __LINE__);
return -1;
}
result = dbi_conn_queryf(dbi->conn,
"SELECT ordenadores.idordenador,"
" ordenadores.nombreordenador,"
@ -74,14 +68,12 @@ int og_dbi_get_computer_info(struct og_computer *computer, struct in_addr addr)
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;
}
if (!dbi_result_next_row(result)) {
syslog(LOG_ERR, "client does not exist in database (%s:%d)\n",
__func__, __LINE__);
dbi_result_free(result);
og_dbi_close(dbi);
return -1;
}
@ -94,7 +86,6 @@ int og_dbi_get_computer_info(struct og_computer *computer, struct in_addr addr)
OG_DB_COMPUTER_NAME_MAXLEN);
dbi_result_free(result);
og_dbi_close(dbi);
return 0;
}

View File

@ -58,6 +58,7 @@ struct og_computer {
};
struct in_addr;
int og_dbi_get_computer_info(struct og_computer *computer, struct in_addr addr);
int og_dbi_get_computer_info(struct og_dbi *dbi, struct og_computer *computer,
struct in_addr addr);
#endif