diff options
-rw-r--r-- | src/dbi.c | 36 | ||||
-rw-r--r-- | src/dbi.h | 13 | ||||
-rw-r--r-- | src/rest.c | 40 |
3 files changed, 83 insertions, 6 deletions
@@ -123,3 +123,39 @@ int og_dbi_get_computer_info(struct og_dbi *dbi, struct og_computer *computer, return 0; } + +#define OG_UNASSIGNED_SW_ID 0 +#define OG_DEFAULT_REPO_ID 1 +#define OG_IMAGE_DEFAULT_TYPE 1 /* monolithic */ + +int og_dbi_add_image(struct og_dbi *dbi, const struct og_image *image) +{ + const char *msglog; + dbi_result result; + + result = dbi_conn_queryf(dbi->conn, + "INSERT INTO imagenes (nombreca, " + "descripcion, " + "idperfilsoft, " + "idcentro, " + "comentarios, " + "grupoid, " + "idrepositorio, " + "tipo, " + "ruta) " + "VALUES ('%s', '%s', %u, %lu, '', %u, %lu, %u, '')", + image->name, image->description, + OG_UNASSIGNED_SW_ID, image->center_id, + image->group_id, OG_DEFAULT_REPO_ID, + OG_IMAGE_DEFAULT_TYPE); + + if (!result) { + dbi_conn_error(dbi->conn, &msglog); + syslog(LOG_ERR, "failed to add client to database (%s:%d) %s\n", + __func__, __LINE__, msglog); + return -1; + } + + dbi_result_free(result); + return dbi_conn_sequence_last(dbi->conn, NULL); +} @@ -3,6 +3,7 @@ #include <dbi/dbi.h> #include <stdbool.h> +#include <sys/stat.h> struct og_dbi_config { const char *user; @@ -24,6 +25,7 @@ void og_dbi_close(struct og_dbi *db); #define OG_DB_CENTER_NAME_MAXLEN 100 #define OG_DB_ROOM_NAME_MAXLEN 100 #define OG_DB_SERIAL_NUMBER_MAXLEN 25 +#define OG_DB_IMAGE_DESCRIPTION_MAXLEN 250 #define OG_DB_IMAGE_NAME_MAXLEN 50 #define OG_DB_FILESYSTEM_MAXLEN 16 #define OG_DB_NETDRIVER_MAXLEN 30 @@ -46,6 +48,16 @@ struct og_image_legacy { char code[OG_DB_INT8_MAXLEN + 1]; }; +struct og_image { + char name[OG_DB_IMAGE_NAME_MAXLEN + 1]; + char description[OG_DB_IMAGE_DESCRIPTION_MAXLEN + 1]; + const char *filename; + uint64_t center_id; + uint64_t datasize; + uint64_t group_id; + struct stat image_stats; +}; + struct og_legacy_partition { char partition[OG_DB_SMALLINT_MAXLEN + 1]; char code[OG_DB_INT8_MAXLEN + 1]; @@ -77,5 +89,6 @@ struct og_computer { struct in_addr; int og_dbi_get_computer_info(struct og_dbi *dbi, struct og_computer *computer, struct in_addr addr); +int og_dbi_add_image(struct og_dbi *dbi, const struct og_image *image); #endif @@ -1589,12 +1589,6 @@ static int og_cmd_software(json_t *element, struct og_msg_params *params) #define OG_IMAGE_TYPE_MAXLEN 4 -struct og_image { - const char *filename; - uint64_t datasize; - struct stat image_stats; -}; - static int og_get_image_stats(const char *name, struct stat *image_stats) { @@ -1744,7 +1738,10 @@ static int og_cmd_images(char *buffer_reply) static int og_cmd_create_image(json_t *element, struct og_msg_params *params) { + char new_image_id[OG_DB_INT_MAXLEN + 1]; + struct og_image image = {}; json_t *value, *clients; + struct og_dbi *dbi; const char *key; int err = 0; @@ -1772,8 +1769,17 @@ static int og_cmd_create_image(json_t *element, struct og_msg_params *params) } else if (!strcmp(key, "code")) { err = og_json_parse_string(value, ¶ms->code); params->flags |= OG_REST_PARAM_CODE; + } else if (!strcmp(key, "description")) { + err = og_json_parse_string_copy(value, + image.description, + sizeof(image.description)); + } else if (!strcmp(key, "group_id")) { + err = og_json_parse_uint64(value, &image.group_id); + } else if (!strcmp(key, "center_id")) { + err = og_json_parse_uint64(value, &image.center_id); } + if (err < 0) break; } @@ -1787,6 +1793,28 @@ static int og_cmd_create_image(json_t *element, struct og_msg_params *params) OG_REST_PARAM_REPO)) return -1; + /* If there is a description, this means the image is not in the DB. */ + if (image.description[0]) { + snprintf(image.name, sizeof(image.name), "%s", params->name); + + dbi = og_dbi_open(&ogconfig.db); + if (!dbi) { + syslog(LOG_ERR, + "cannot open connection database (%s:%d)\n", + __func__, __LINE__); + return -1; + } + + err = og_dbi_add_image(dbi, &image); + + og_dbi_close(dbi); + if (err < 0) + return err; + + snprintf(new_image_id, sizeof(new_image_id), "%u", err); + params->id = new_image_id; + } + clients = json_copy(element); json_object_del(clients, "clients"); |