]> glassweightruler.freedombox.rocks Git - Ventoy.git/blobdiff - Plugson/src/Web/ventoy_http.c
VentoyPlugson Update:
[Ventoy.git] / Plugson / src / Web / ventoy_http.c
index c24096d21f15f185d91948670afef864953d1e8c..be9639192c54897020d52da157ae3dbc6f5b8cd0 100644 (file)
@@ -60,6 +60,17 @@ static const char *g_ventoy_kbd_layout[] =
     NULL
 };
 
+#define VTOY_DEL_ALL_PATH   "4119ae33-98ea-448e-b9c0-569aafcf1fb4"
+
+static int g_json_exist[plugin_type_max][bios_max];
+static const char *g_plugin_name[plugin_type_max] = 
+{
+    "control", "theme", "menu_alias", "menu_tip", 
+    "menu_class", "auto_install", "persistence", "injection", 
+    "conf_replace", "password", "image_list", 
+    "auto_memdisk", "dud"
+};
+
 static char g_ventoy_help_lang[MAX_LANGUAGE][8];
 
 static char g_pub_path[2 * MAX_PATH];
@@ -325,8 +336,7 @@ static int ventoy_api_sysinfo(struct mg_connection *conn, VTOY_JSON *json)
     
     VTOY_JSON_FMT_SINT("invalid_config", g_sysinfo.invalid_config);
     g_sysinfo.invalid_config = 0;
-
-
+    
     #if defined(_MSC_VER) || defined(WIN32)
     VTOY_JSON_FMT_STRN("os", "windows");
     #else
@@ -342,13 +352,31 @@ static int ventoy_api_sysinfo(struct mg_connection *conn, VTOY_JSON *json)
 
 static int ventoy_api_handshake(struct mg_connection *conn, VTOY_JSON *json)
 {
+    int i = 0;
+    int j = 0;
     int pos = 0;
+    char key[128];
     
     (void)json;
 
     VTOY_JSON_FMT_BEGIN(pos, JSON_BUFFER, JSON_BUF_MAX);
     VTOY_JSON_FMT_OBJ_BEGIN();
     VTOY_JSON_FMT_SINT("status", 0);
+    VTOY_JSON_FMT_SINT("save_error", g_sysinfo.config_save_error);
+    g_sysinfo.config_save_error = 0;
+
+    for (i = 0; i < plugin_type_max; i++)
+    {
+        scnprintf(key, sizeof(key), "exist_%s", g_plugin_name[i]);
+        VTOY_JSON_FMT_KEY(key);
+        VTOY_JSON_FMT_ARY_BEGIN();
+        for (j = 0; j < bios_max; j++)
+        {
+            VTOY_JSON_FMT_ITEM_INT(g_json_exist[i][j]);
+        }
+        VTOY_JSON_FMT_ARY_ENDEX();
+    }
+    
     VTOY_JSON_FMT_OBJ_END();
     VTOY_JSON_FMT_END(pos);
 
@@ -485,11 +513,11 @@ void ventoy_data_default_control(data_control *data)
 {
     memset(data, 0, sizeof(data_control));
 
-    data->default_menu_mode = 1;
+    data->secondary_menu = 1;
     data->filter_dot_underscore = 1;
-    data->treeview_style = 1;
     data->max_search_level = -1;
     data->menu_timeout = 0;
+    data->secondary_menu_timeout = 0;
     
     strlcpy(data->default_kbd_layout, "QWERTY_USA");
     strlcpy(data->help_text_language, "en_US");
@@ -511,7 +539,9 @@ int ventoy_data_cmp_control(data_control *data1, data_control *data2)
         data1->filter_vtoy != data2->filter_vtoy ||
         data1->win11_bypass_check != data2->win11_bypass_check ||
         data1->linux_remount != data2->linux_remount ||
-        data1->menu_timeout != data2->menu_timeout)
+        data1->secondary_menu != data2->secondary_menu ||
+        data1->menu_timeout != data2->menu_timeout ||
+        data1->secondary_menu_timeout != data2->secondary_menu_timeout)
     {
         return 1;
     }
@@ -556,7 +586,9 @@ int ventoy_data_save_control(data_control *data, const char *title, char *buf, i
     VTOY_JSON_FMT_CTRL_INT(L2, "VTOY_FILE_FLT_VTOY", filter_vtoy);
     VTOY_JSON_FMT_CTRL_INT(L2, "VTOY_WIN11_BYPASS_CHECK",  win11_bypass_check);
     VTOY_JSON_FMT_CTRL_INT(L2, "VTOY_LINUX_REMOUNT",  linux_remount);
+    VTOY_JSON_FMT_CTRL_INT(L2, "VTOY_SECONDARY_BOOT_MENU",  secondary_menu);
     VTOY_JSON_FMT_CTRL_INT(L2, "VTOY_MENU_TIMEOUT",  menu_timeout);
+    VTOY_JSON_FMT_CTRL_INT(L2, "VTOY_SECONDARY_TIMEOUT",  secondary_menu_timeout);
 
     VTOY_JSON_FMT_CTRL_STRN(L2, "VTOY_DEFAULT_KBD_LAYOUT", default_kbd_layout);        
     VTOY_JSON_FMT_CTRL_STRN(L2, "VTOY_HELP_TXT_LANGUAGE", help_text_language);  
@@ -601,7 +633,9 @@ int ventoy_data_json_control(data_control *ctrl, char *buf, int buflen)
     VTOY_JSON_FMT_SINT("filter_vtoy", ctrl->filter_vtoy);
     VTOY_JSON_FMT_SINT("win11_bypass_check",  ctrl->win11_bypass_check);
     VTOY_JSON_FMT_SINT("linux_remount",  ctrl->linux_remount);
+    VTOY_JSON_FMT_SINT("secondary_menu",  ctrl->secondary_menu);
     VTOY_JSON_FMT_SINT("menu_timeout",  ctrl->menu_timeout);
+    VTOY_JSON_FMT_SINT("secondary_menu_timeout",  ctrl->secondary_menu_timeout);
     VTOY_JSON_FMT_STRN("default_kbd_layout",  ctrl->default_kbd_layout);
     VTOY_JSON_FMT_STRN("help_text_language",  ctrl->help_text_language);
 
@@ -667,7 +701,9 @@ static int ventoy_api_save_control(struct mg_connection *conn, VTOY_JSON *json)
     VTOY_JSON_INT("filter_vtoy", ctrl->filter_vtoy);
     VTOY_JSON_INT("win11_bypass_check", ctrl->win11_bypass_check);
     VTOY_JSON_INT("linux_remount", ctrl->linux_remount);
+    VTOY_JSON_INT("secondary_menu", ctrl->secondary_menu);
     VTOY_JSON_INT("menu_timeout", ctrl->menu_timeout);
+    VTOY_JSON_INT("secondary_menu_timeout", ctrl->secondary_menu_timeout);
 
     VTOY_JSON_STR("default_image", ctrl->default_image);
     VTOY_JSON_STR("default_search_root", ctrl->default_search_root);
@@ -709,7 +745,7 @@ int ventoy_data_cmp_theme(data_theme *data1, data_theme *data2)
     {
         return 1;
     }
-    
+
     if (ventoy_path_list_cmp(data1->fontslist, data2->fontslist))
     {
         return 1;
@@ -918,7 +954,14 @@ static int ventoy_api_theme_del_file(struct mg_connection *conn, VTOY_JSON *json
     path = VTOY_JSON_STR_EX("path");
     if (path)
     {
-        vtoy_list_del(last, node, data->filelist, path);
+        if (strcmp(path, VTOY_DEL_ALL_PATH) == 0)
+        {
+            vtoy_list_free(path_node, data->filelist);
+        }
+        else
+        {
+            vtoy_list_del(last, node, data->filelist, path);
+        }    
     }
 
     ret = ventoy_data_save_all();
@@ -973,7 +1016,14 @@ static int ventoy_api_theme_del_font(struct mg_connection *conn, VTOY_JSON *json
     path = VTOY_JSON_STR_EX("path");
     if (path)
     {
-        vtoy_list_del(last, node, data->fontslist, path);
+        if (strcmp(path, VTOY_DEL_ALL_PATH) == 0)
+        {
+            vtoy_list_free(path_node, data->fontslist);
+        }
+        else
+        {
+            vtoy_list_del(last, node, data->fontslist, path);            
+        }    
     }
 
     ret = ventoy_data_save_all();
@@ -1170,7 +1220,14 @@ static int ventoy_api_alias_del(struct mg_connection *conn, VTOY_JSON *json)
     path = VTOY_JSON_STR_EX("path");
     if (path)
     {
-        vtoy_list_del(last, node, data->list, path);
+        if (strcmp(path, VTOY_DEL_ALL_PATH) == 0)
+        {
+            vtoy_list_free(data_alias_node, data->list);          
+        }
+        else
+        {
+            vtoy_list_del(last, node, data->list, path);            
+        }
     }
 
     ret = ventoy_data_save_all();
@@ -1408,7 +1465,14 @@ static int ventoy_api_tip_del(struct mg_connection *conn, VTOY_JSON *json)
     path = VTOY_JSON_STR_EX("path");
     if (path)
     {
-        vtoy_list_del(last, node, data->list, path);
+        if (strcmp(path, VTOY_DEL_ALL_PATH) == 0)
+        {
+            vtoy_list_free(data_tip_node, data->list);          
+        }
+        else
+        {
+            vtoy_list_del(last, node, data->list, path);  
+        }
     }
 
     ret = ventoy_data_save_all();
@@ -1611,7 +1675,14 @@ static int ventoy_api_class_del(struct mg_connection *conn, VTOY_JSON *json)
     path = VTOY_JSON_STR_EX("path");
     if (path)
     {
-        vtoy_list_del(last, node, data->list, path);
+        if (strcmp(path, VTOY_DEL_ALL_PATH) == 0)
+        {
+            vtoy_list_free(data_class_node, data->list);
+        }
+        else
+        {
+            vtoy_list_del(last, node, data->list, path);            
+        }
     }
 
     ret = ventoy_data_save_all();
@@ -1740,7 +1811,14 @@ static int ventoy_api_auto_memdisk_del(struct mg_connection *conn, VTOY_JSON *js
     path = VTOY_JSON_STR_EX("path");
     if (path)
     {
-        vtoy_list_del(last, node, data->list, path);
+        if (strcmp(path, VTOY_DEL_ALL_PATH) == 0)
+        {
+            vtoy_list_free(path_node, data->list);
+        }
+        else
+        {
+            vtoy_list_del(last, node, data->list, path);            
+        }
     }
 
     ret = ventoy_data_save_all();
@@ -1777,7 +1855,9 @@ int ventoy_data_cmp_image_list(data_image_list *data1, data_image_list *data2)
 int ventoy_data_save_image_list(data_image_list *data, const char *title, char *buf, int buflen)
 {
     int pos = 0;
+    int prelen;
     path_node *node = NULL;
+    char newtitle[64];
 
     (void)title;
 
@@ -1785,17 +1865,20 @@ int ventoy_data_save_image_list(data_image_list *data, const char *title, char *
     {
         return 0;
     }
-    
+
+    prelen = (int)strlen("image_list");
+
     VTOY_JSON_FMT_BEGIN(pos, buf, buflen);
 
     if (data->type == 0)
     {
-        VTOY_JSON_FMT_KEY_L(L1, "image_list");
+        scnprintf(newtitle, sizeof(newtitle), "image_list%s", title + prelen);
     }
     else
     {
-        VTOY_JSON_FMT_KEY_L(L1, "image_blacklist");
+        scnprintf(newtitle, sizeof(newtitle), "image_blacklist%s", title + prelen);
     }
+    VTOY_JSON_FMT_KEY_L(L1, newtitle);
     
     VTOY_JSON_FMT_ARY_BEGIN_N();
 
@@ -1910,7 +1993,14 @@ static int ventoy_api_image_list_del(struct mg_connection *conn, VTOY_JSON *json
     path = VTOY_JSON_STR_EX("path");
     if (path)
     {
-        vtoy_list_del(last, node, data->list, path);
+        if (strcmp(path, VTOY_DEL_ALL_PATH) == 0)
+        {
+            vtoy_list_free(path_node, data->list);
+        }
+        else
+        {
+            vtoy_list_del(last, node, data->list, path);            
+        }
     }
 
     ret = ventoy_data_save_all();
@@ -2161,7 +2251,14 @@ static int ventoy_api_password_del(struct mg_connection *conn, VTOY_JSON *json)
     path = VTOY_JSON_STR_EX("path");
     if (path)
     {
-        vtoy_list_del(last, node, data->list, path);
+        if (strcmp(path, VTOY_DEL_ALL_PATH) == 0)
+        {
+            vtoy_list_free(menu_password, data->list);
+        }
+        else
+        {
+            vtoy_list_del(last, node, data->list, path);            
+        }
     }
 
     ret = ventoy_data_save_all();
@@ -2352,7 +2449,14 @@ static int ventoy_api_conf_replace_del(struct mg_connection *conn, VTOY_JSON *js
     path = VTOY_JSON_STR_EX("path");
     if (path)
     {
-        vtoy_list_del(last, node, data->list, path);
+        if (strcmp(path, VTOY_DEL_ALL_PATH) == 0)
+        {
+            vtoy_list_free(conf_replace_node, data->list);
+        }
+        else
+        {
+            vtoy_list_del(last, node, data->list, path);            
+        }
     }
 
     ret = ventoy_data_save_all();
@@ -2541,6 +2645,7 @@ static int ventoy_api_dud_del(struct mg_connection *conn, VTOY_JSON *json)
     int ret;
     int index = 0;
     const char *path = NULL;
+    dud_node *next = NULL;
     dud_node *last = NULL;
     dud_node *node = NULL;
     data_dud *data = NULL;
@@ -2551,7 +2656,20 @@ static int ventoy_api_dud_del(struct mg_connection *conn, VTOY_JSON *json)
     path = VTOY_JSON_STR_EX("path");
     if (path)
     {
-        vtoy_list_del_ex(last, node, data->list, path, ventoy_free_path_node_list);
+        if (strcmp(path, VTOY_DEL_ALL_PATH) == 0)
+        {
+            for (node = data->list; node; node = next)
+            {
+                next = node->next;
+                ventoy_free_path_node_list(node->list);
+                free(node);
+            }
+            data->list = NULL;
+        }
+        else
+        {
+            vtoy_list_del_ex(last, node, data->list, path, ventoy_free_path_node_list);
+        }
     }
 
     ret = ventoy_data_save_all();
@@ -2888,6 +3006,7 @@ static int ventoy_api_auto_install_del(struct mg_connection *conn, VTOY_JSON *js
     int index = 0;
     const char *path = NULL;
     auto_install_node *last = NULL;
+    auto_install_node *next = NULL;
     auto_install_node *node = NULL;
     data_auto_install *data = NULL;
     
@@ -2897,7 +3016,20 @@ static int ventoy_api_auto_install_del(struct mg_connection *conn, VTOY_JSON *js
     path = VTOY_JSON_STR_EX("path");
     if (path)
     {
-        vtoy_list_del_ex(last, node, data->list, path, ventoy_free_path_node_list);
+        if (strcmp(path, VTOY_DEL_ALL_PATH) == 0)
+        {
+            for (node = data->list; node; node = next)
+            {
+                next = node->next;
+                ventoy_free_path_node_list(node->list);
+                free(node);
+            }
+            data->list = NULL;
+        }
+        else
+        {
+            vtoy_list_del_ex(last, node, data->list, path, ventoy_free_path_node_list);            
+        }
     }
 
     ret = ventoy_data_save_all();
@@ -3215,6 +3347,7 @@ static int ventoy_api_persistence_del(struct mg_connection *conn, VTOY_JSON *jso
     int index = 0;
     const char *path = NULL;
     persistence_node *last = NULL;
+    persistence_node *next = NULL;
     persistence_node *node = NULL;
     data_persistence *data = NULL;
     
@@ -3224,7 +3357,20 @@ static int ventoy_api_persistence_del(struct mg_connection *conn, VTOY_JSON *jso
     path = VTOY_JSON_STR_EX("path");
     if (path)
     {
-        vtoy_list_del_ex(last, node, data->list, path, ventoy_free_path_node_list);
+        if (strcmp(path, VTOY_DEL_ALL_PATH) == 0)
+        {
+            for (node = data->list; node; node = next)
+            {
+                next = node->next;
+                ventoy_free_path_node_list(node->list);
+                free(node);
+            }
+            data->list = NULL;
+        }
+        else
+        {
+            vtoy_list_del_ex(last, node, data->list, path, ventoy_free_path_node_list);            
+        }    
     }
 
     ret = ventoy_data_save_all();
@@ -3501,7 +3647,14 @@ static int ventoy_api_injection_del(struct mg_connection *conn, VTOY_JSON *json)
     path = VTOY_JSON_STR_EX("path");
     if (path)
     {
-        vtoy_list_del(last, node, data->list, path);
+        if (strcmp(path, VTOY_DEL_ALL_PATH) == 0)
+        {
+            vtoy_list_free(injection_node, data->list);
+        }
+        else
+        {
+            vtoy_list_del(last, node, data->list, path);            
+        }
     }
 
     ret = ventoy_data_save_all();
@@ -3510,6 +3663,34 @@ static int ventoy_api_injection_del(struct mg_connection *conn, VTOY_JSON *json)
     return 0;
 }
 
+#if 0
+#endif
+
+static int ventoy_api_preview_json(struct mg_connection *conn, VTOY_JSON *json)
+{
+    int pos = 0;
+    int len = 0;
+    int encodelen = 0;
+    char *encodebuf = NULL;
+    
+    (void)json;
+
+    len = ventoy_data_real_save_all(0);
+    encodebuf = ventoy_base64_encode(JSON_SAVE_BUFFER, len, &encodelen);
+    encodebuf[encodelen] = 0;
+
+    VTOY_JSON_FMT_BEGIN(pos, JSON_BUFFER, JSON_BUF_MAX);
+    VTOY_JSON_FMT_OBJ_BEGIN();
+    VTOY_JSON_FMT_STRN("json", encodebuf);
+    VTOY_JSON_FMT_OBJ_END();
+    VTOY_JSON_FMT_END(pos);
+
+    free(encodebuf);
+
+    ventoy_json_buffer(conn, JSON_BUFFER, pos);
+    return 0;
+}
+
 
 #if 0
 #endif
@@ -3520,14 +3701,17 @@ int ventoy_data_save_all(void)
     return 0;
 }
 
-int ventoy_data_real_save_all(void)
+int ventoy_data_real_save_all(int apilock)
 {
     int i = 0;
     int pos = 0;
     char title[64];
 
-    pthread_mutex_lock(&g_api_mutex);
-    
+    if (apilock)
+    {
+        pthread_mutex_lock(&g_api_mutex);        
+    }
+
     ssprintf(pos, JSON_SAVE_BUFFER, JSON_BUF_MAX, "{\n");
 
     ventoy_save_plug(control);
@@ -3551,7 +3735,10 @@ int ventoy_data_real_save_all(void)
     }
     ssprintf(pos, JSON_SAVE_BUFFER, JSON_BUF_MAX, "}\n");
 
-    pthread_mutex_unlock(&g_api_mutex);
+    if (apilock)
+    {
+        pthread_mutex_unlock(&g_api_mutex);        
+    }
 
     return pos;
 }
@@ -3564,7 +3751,7 @@ int ventoy_http_writeback(void)
 
     ventoy_get_json_path(filename, NULL);
 
-    pos = ventoy_data_real_save_all();
+    pos = ventoy_data_real_save_all(1);
         
     #ifdef VENTOY_SIM
     printf("%s", JSON_SAVE_BUFFER);
@@ -3574,6 +3761,7 @@ int ventoy_http_writeback(void)
     if (ret)
     {
         vlog("Failed to write ventoy.json file.\n");
+        g_sysinfo.config_save_error = 1;
     }
 
     return 0;
@@ -3660,11 +3848,11 @@ static JSON_CB g_ventoy_json_cb[] =
     { "save_injection",     ventoy_api_save_injection     },   
     { "injection_add",      ventoy_api_injection_add      },
     { "injection_del",      ventoy_api_injection_del      },
-
+    { "preview_json",       ventoy_api_preview_json       },
     
 };
 
-static int ventoy_json_handler(struct mg_connection *conn, VTOY_JSON *json)
+static int ventoy_json_handler(struct mg_connection *conn, VTOY_JSON *json, char *jsonstr)
 {
     int i;
     const char *method = NULL;
@@ -3681,7 +3869,7 @@ static int ventoy_json_handler(struct mg_connection *conn, VTOY_JSON *json)
         ventoy_api_handshake(conn, json);
         return 0;
     }
-    
+
     for (i = 0; i < (int)(sizeof(g_ventoy_json_cb) / sizeof(g_ventoy_json_cb[0])); i++)
     {
         if (strcmp(method, g_ventoy_json_cb[i].method) == 0)
@@ -3725,7 +3913,7 @@ static int ventoy_request_handler(struct mg_connection *conn)
         if (JSON_SUCCESS == vtoy_json_parse(json, post_data_buf))
         {
             pthread_mutex_lock(&g_api_mutex);
-            ventoy_json_handler(conn, json->pstChild);
+            ventoy_json_handler(conn, json->pstChild, post_data_buf);
             pthread_mutex_unlock(&g_api_mutex);
         }
         else
@@ -3803,6 +3991,10 @@ static int ventoy_parse_control(VTOY_JSON *json, void *p)
             {
                 CONTROL_PARSE_INT(child, data->linux_remount);
             }
+            else if (strcmp(child->pcName, "VTOY_SECONDARY_BOOT_MENU") == 0)
+            {
+                CONTROL_PARSE_INT(child, data->secondary_menu);
+            }
             else if (strcmp(child->pcName, "VTOY_TREE_VIEW_MENU_STYLE") == 0)
             {
                 CONTROL_PARSE_INT(child, data->treeview_style);
@@ -3860,6 +4052,10 @@ static int ventoy_parse_control(VTOY_JSON *json, void *p)
             {
                 data->menu_timeout = (int)strtol(child->unData.pcStrVal, NULL, 10);
             }
+            else if (strcmp(child->pcName, "VTOY_SECONDARY_TIMEOUT") == 0)
+            {
+                data->secondary_menu_timeout = (int)strtol(child->unData.pcStrVal, NULL, 10);
+            }
             else if (strcmp(child->pcName, "VTOY_VHD_NO_WARNING") == 0)
             {
                 CONTROL_PARSE_INT(child, data->vhd_no_warning);
@@ -5003,7 +5199,8 @@ end:
 
 int ventoy_http_start(const char *ip, const char *port)
 {
-    int i;
+    int i = 0;
+    int ret = 0;
     char addr[128];
     char filename[128];
     char backupname[128];
@@ -5040,10 +5237,13 @@ int ventoy_http_start(const char *ip, const char *port)
     if (ventoy_is_file_exist("%s", filename))
     {
         ventoy_copy_file(filename, backupname);
-        ventoy_load_old_json(filename);
+        ret = ventoy_load_old_json(filename);
+        if (ret == 0)
+        {
+            ventoy_data_real_save_all(0);
+        }
     }
 
-
     /* option */
     scnprintf(addr, sizeof(addr), "%s:%s", ip, port);
     options[1] = addr;