summaryrefslogtreecommitdiffstats
path: root/src/rest.c
diff options
context:
space:
mode:
authorJavier Hernandez <jhernandez@soleta.eu>2023-11-03 20:44:38 +0100
committerOpenGnSys Support Team <soporte-og@soleta.eu>2023-11-06 10:11:44 +0100
commitfa57af3c13df1106e0e6992827d041a9a0aa1a43 (patch)
tree6b6051929eb39432287f6931baf80b293dd7bdbb /src/rest.c
parent4c1995d28fb09156e89e54e437ffedb16ca945b7 (diff)
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.
Diffstat (limited to 'src/rest.c')
-rw-r--r--src/rest.c84
1 files changed, 62 insertions, 22 deletions
diff --git a/src/rest.c b/src/rest.c
index 6e4ba04..3abf96e 100644
--- a/src/rest.c
+++ b/src/rest.c
@@ -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 "/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,
char *buffer_reply)
{
struct og_buffer og_buffer = {
.data = buffer_reply
};
+ struct og_boot_mode *mode;
+ LIST_HEAD(boot_mode_list);
json_t *root, *modes;
- struct dirent *dent;
- DIR *d = NULL;
+ int ret;
root = json_object();
if (!root)
@@ -1032,35 +1085,22 @@ static int og_cmd_get_modes(json_t *element, struct og_msg_params *params,
return -1;
}
- d = opendir(OG_TFTP_TMPL_PATH);
- if (!d) {
+ if (og_get_boot_modes(&boot_mode_list) < 0) {
json_decref(modes);
json_decref(root);
- syslog(LOG_ERR, "Cannot open directory %s\n",
- OG_TFTP_TMPL_PATH);
return -1;
}
- dent = readdir(d);
- while (dent) {
- if (dent->d_type != DT_REG) {
- dent = readdir(d);
- continue;
- }
- json_array_append_new(modes, json_string(dent->d_name));
- dent = readdir(d);
- }
- json_object_set_new(root, "modes", modes);
+ list_for_each_entry(mode, &boot_mode_list, list)
+ json_array_append_new(modes, json_string(mode->name));
- if (json_dump_callback(root, og_json_dump_clients, &og_buffer, 0)) {
- json_decref(root);
- return -1;
- }
+ og_boot_mode_free(&boot_mode_list);
+ json_object_set_new(root, "modes", modes);
+ ret = json_dump_callback(root, og_json_dump_clients, &og_buffer, 0);
json_decref(root);
- closedir(d);
- return 0;
+ return ret;
}
static int og_change_db_mode(struct og_dbi *dbi, const char *mac,