rest: extract og_get_boot_modes from og_cmd_get_modes()

og_cmd_get_modes() now uses a helper function that returns a list of all boot
modes, this is for a new API REST validate that boot mode is set to something
that exists in a follow up patch.
master
Javier Hernandez 2023-11-03 20:44:38 +01:00 committed by OpenGnSys Support Team
parent 4c1995d28f
commit fa57af3c13
1 changed files with 62 additions and 22 deletions

View File

@ -1012,15 +1012,68 @@ static int og_cmd_reboot(json_t *element, struct og_msg_params *params)
#define OG_TFTP_TMPL_PATH_UEFI "/opt/opengnsys/tftpboot/grub/templates" #define OG_TFTP_TMPL_PATH_UEFI "/opt/opengnsys/tftpboot/grub/templates"
#define OG_TFTP_TMPL_PATH "/opt/opengnsys/tftpboot/menu.lst/templates" #define OG_TFTP_TMPL_PATH "/opt/opengnsys/tftpboot/menu.lst/templates"
struct og_boot_mode {
struct list_head list;
char name[FILENAME_MAX];
};
static void og_boot_mode_free(struct list_head *boot_mode_list)
{
struct og_boot_mode *mode, *tmp;
list_for_each_entry_safe(mode, tmp, boot_mode_list, list) {
list_del(&mode->list);
free(mode);
}
}
static int og_get_boot_modes(struct list_head *boot_mode_list)
{
struct og_boot_mode *mode;
struct dirent *dent;
DIR *d;
d = opendir(OG_TFTP_TMPL_PATH);
if (!d) {
syslog(LOG_ERR, "Cannot open directory %s (%s:%d)\n",
OG_TFTP_TMPL_PATH, __func__, __LINE__);
return -1;
}
dent = readdir(d);
while (dent) {
if (dent->d_type != DT_REG) {
dent = readdir(d);
continue;
}
mode = calloc(1, sizeof(struct og_boot_mode));
if (!mode) {
closedir(d);
og_boot_mode_free(boot_mode_list);
return -1;
}
snprintf(mode->name, FILENAME_MAX, "%s", dent->d_name);
list_add_tail(&mode->list, boot_mode_list);
dent = readdir(d);
}
closedir(d);
return 0;
}
static int og_cmd_get_modes(json_t *element, struct og_msg_params *params, static int og_cmd_get_modes(json_t *element, struct og_msg_params *params,
char *buffer_reply) char *buffer_reply)
{ {
struct og_buffer og_buffer = { struct og_buffer og_buffer = {
.data = buffer_reply .data = buffer_reply
}; };
struct og_boot_mode *mode;
LIST_HEAD(boot_mode_list);
json_t *root, *modes; json_t *root, *modes;
struct dirent *dent; int ret;
DIR *d = NULL;
root = json_object(); root = json_object();
if (!root) if (!root)
@ -1032,35 +1085,22 @@ static int og_cmd_get_modes(json_t *element, struct og_msg_params *params,
return -1; return -1;
} }
d = opendir(OG_TFTP_TMPL_PATH); if (og_get_boot_modes(&boot_mode_list) < 0) {
if (!d) {
json_decref(modes); json_decref(modes);
json_decref(root); json_decref(root);
syslog(LOG_ERR, "Cannot open directory %s\n",
OG_TFTP_TMPL_PATH);
return -1; return -1;
} }
dent = readdir(d); list_for_each_entry(mode, &boot_mode_list, list)
while (dent) { json_array_append_new(modes, json_string(mode->name));
if (dent->d_type != DT_REG) {
dent = readdir(d); og_boot_mode_free(&boot_mode_list);
continue;
}
json_array_append_new(modes, json_string(dent->d_name));
dent = readdir(d);
}
json_object_set_new(root, "modes", modes); json_object_set_new(root, "modes", modes);
if (json_dump_callback(root, og_json_dump_clients, &og_buffer, 0)) { ret = json_dump_callback(root, og_json_dump_clients, &og_buffer, 0);
json_decref(root); json_decref(root);
return -1;
}
json_decref(root); return ret;
closedir(d);
return 0;
} }
static int og_change_db_mode(struct og_dbi *dbi, const char *mac, static int og_change_db_mode(struct og_dbi *dbi, const char *mac,