]> glassweightruler.freedombox.rocks Git - Ventoy.git/blobdiff - Plugson/src/Web/ventoy_http.c
VentoyPlugson: Add Windows duplicate file path check for different upper/lower case.
[Ventoy.git] / Plugson / src / Web / ventoy_http.c
index a8a86dfb127e76be6856c5a7bc3a67984bbd0d69..2f1f88202e8a50b5b63321b037cf20fb0d7ade95 100644 (file)
@@ -21,6 +21,7 @@
 #include <stdint.h>
 #include <string.h>
 #include <stdarg.h>
+#include <stddef.h>
 #include <errno.h>
 #include <time.h>
 
@@ -71,7 +72,7 @@ static const char *g_plugin_name[plugin_type_max] =
     "auto_memdisk", "dud"
 };
 
-static char g_ventoy_help_lang[MAX_LANGUAGE][8];
+static char g_ventoy_menu_lang[MAX_LANGUAGE][8];
 
 static char g_pub_path[2 * MAX_PATH];
 static data_control g_data_control[bios_max + 1];
@@ -97,6 +98,9 @@ static char *g_pub_save_buffer = NULL;
 static pthread_mutex_t g_api_mutex;
 static struct mg_context *g_ventoy_http_ctx = NULL;
 
+#define ventoy_is_real_exist_common(xpath, xnode, xtype) \
+    ventoy_path_is_real_exist(xpath, xnode, offsetof(xtype, path), offsetof(xtype, next))
+
 static int ventoy_is_kbd_valid(const char *key)
 {
     int i = 0;
@@ -170,6 +174,40 @@ static void ventoy_free_path_node_list(path_node *list)
     }    
 }
 
+static int ventoy_path_is_real_exist(const char *path, void *head, size_t pathoff, size_t nextoff)
+{
+    char *node = NULL;
+    const char *nodepath = NULL;
+    const char *realpath = NULL;
+    char pathbuf[MAX_PATH];
+
+    if (strchr(path, '*'))
+    {
+        return 0;
+    }
+
+    realpath = ventoy_real_path(path);
+    scnprintf(pathbuf, sizeof(pathbuf), "%s", realpath);
+
+    node = (char *)head;
+    while (node)
+    {
+        nodepath = node + pathoff;
+        if (NULL == strchr(nodepath, '*'))
+        {
+            realpath = ventoy_real_path(nodepath);
+            if (strcmp(pathbuf, realpath) == 0)
+            {
+                return 1;
+            }
+        }
+        
+        memcpy(&node, node + nextoff, sizeof(node));
+    }
+
+    return 0;
+}
+
 static path_node * ventoy_path_node_add_array(VTOY_JSON *array)
 {
     path_node *head = NULL;
@@ -521,7 +559,7 @@ void ventoy_data_default_control(data_control *data)
     data->secondary_menu_timeout = 0;
     
     strlcpy(data->default_kbd_layout, "QWERTY_USA");
-    strlcpy(data->help_text_language, "en_US");
+    strlcpy(data->menu_language, "en_US");
 }
 
 int ventoy_data_cmp_control(data_control *data1, data_control *data2)
@@ -551,7 +589,7 @@ int ventoy_data_cmp_control(data_control *data1, data_control *data2)
     if (strcmp(data1->default_search_root, data2->default_search_root) ||
         strcmp(data1->default_image, data2->default_image) ||
         strcmp(data1->default_kbd_layout, data2->default_kbd_layout) ||
-        strcmp(data1->help_text_language, data2->help_text_language))
+        strcmp(data1->menu_language, data2->menu_language))
     {
         return 1;
     }
@@ -594,7 +632,7 @@ int ventoy_data_save_control(data_control *data, const char *title, char *buf, i
     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);  
+    VTOY_JSON_FMT_CTRL_STRN(L2, "VTOY_MENU_LANGUAGE", menu_language);  
 
     if (strcmp(def->default_search_root, data->default_search_root))
     {
@@ -641,7 +679,7 @@ int ventoy_data_json_control(data_control *ctrl, char *buf, int buflen)
     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);
+    VTOY_JSON_FMT_STRN("menu_language",  ctrl->menu_language);
 
     valid = 0;
     if (ctrl->default_search_root[0] && ventoy_is_directory_exist("%s%s", g_cur_dir, ctrl->default_search_root))
@@ -660,16 +698,15 @@ int ventoy_data_json_control(data_control *ctrl, char *buf, int buflen)
     VTOY_JSON_FMT_STRN("default_image", ctrl->default_image);
     VTOY_JSON_FMT_SINT("default_image_valid", valid);
 
-    VTOY_JSON_FMT_KEY("help_list");
+    VTOY_JSON_FMT_KEY("menu_list");
     VTOY_JSON_FMT_ARY_BEGIN();
 
-    for (i = 0; g_ventoy_help_lang[i][0]; i++)
+    for (i = 0; g_ventoy_menu_lang[i][0]; i++)
     {
-        VTOY_JSON_FMT_ITEM(g_ventoy_help_lang[i]);        
+        VTOY_JSON_FMT_ITEM(g_ventoy_menu_lang[i]);        
     }
     VTOY_JSON_FMT_ARY_ENDEX();
-
-
+    
     VTOY_JSON_FMT_OBJ_END();
     VTOY_JSON_FMT_END(pos);
 
@@ -712,7 +749,7 @@ static int ventoy_api_save_control(struct mg_connection *conn, VTOY_JSON *json)
 
     VTOY_JSON_STR("default_image", ctrl->default_image);
     VTOY_JSON_STR("default_search_root", ctrl->default_search_root);
-    VTOY_JSON_STR("help_text_language", ctrl->help_text_language);
+    VTOY_JSON_STR("menu_language", ctrl->menu_language);
     VTOY_JSON_STR("default_kbd_layout", ctrl->default_kbd_layout);
     
     ret = ventoy_data_save_all();
@@ -914,6 +951,7 @@ static int ventoy_api_save_theme(struct mg_connection *conn, VTOY_JSON *json)
     return 0;
 }
 
+
 static int ventoy_api_theme_add_file(struct mg_connection *conn, VTOY_JSON *json)
 {
     int ret;
@@ -929,6 +967,12 @@ static int ventoy_api_theme_add_file(struct mg_connection *conn, VTOY_JSON *json
     path = VTOY_JSON_STR_EX("path");
     if (path)
     {
+        if (ventoy_is_real_exist_common(path, data->filelist, path_node))
+        {
+            ventoy_json_result(conn, VTOY_JSON_DUPLICATE);
+            return 0;
+        }
+
         node = zalloc(sizeof(path_node));
         if (node)
         {
@@ -975,7 +1019,6 @@ static int ventoy_api_theme_del_file(struct mg_connection *conn, VTOY_JSON *json
     return 0;
 }
 
-
 static int ventoy_api_theme_add_font(struct mg_connection *conn, VTOY_JSON *json)
 {
     int ret;
@@ -984,13 +1027,19 @@ static int ventoy_api_theme_add_font(struct mg_connection *conn, VTOY_JSON *json
     path_node *node = NULL;
     path_node *cur = NULL;
     data_theme *data = NULL;
-    
+
     vtoy_json_get_int(json, "index", &index);
     data = g_data_theme + index;
 
     path = VTOY_JSON_STR_EX("path");
     if (path)
     {
+        if (ventoy_is_real_exist_common(path, data->fontslist, path_node))
+        {
+            ventoy_json_result(conn, VTOY_JSON_DUPLICATE);
+            return 0;
+        }
+
         node = zalloc(sizeof(path_node));
         if (node)
         {
@@ -1193,6 +1242,12 @@ static int ventoy_api_alias_add(struct mg_connection *conn, VTOY_JSON *json)
     alias = VTOY_JSON_STR_EX("alias");
     if (path && alias)
     {
+        if (ventoy_is_real_exist_common(path, data->list, data_alias_node))
+        {
+            ventoy_json_result(conn, VTOY_JSON_DUPLICATE);
+            return 0;
+        }
+
         node = zalloc(sizeof(data_alias_node));
         if (node)
         {
@@ -1438,6 +1493,12 @@ static int ventoy_api_tip_add(struct mg_connection *conn, VTOY_JSON *json)
     tip = VTOY_JSON_STR_EX("tip");
     if (path && tip)
     {
+        if (ventoy_is_real_exist_common(path, data->list, data_tip_node))
+        {
+            ventoy_json_result(conn, VTOY_JSON_DUPLICATE);
+            return 0;
+        }
+    
         node = zalloc(sizeof(data_tip_node));
         if (node)
         {
@@ -1787,6 +1848,12 @@ static int ventoy_api_auto_memdisk_add(struct mg_connection *conn, VTOY_JSON *js
     path = VTOY_JSON_STR_EX("path");
     if (path)
     {
+        if (ventoy_is_real_exist_common(path, data->list, path_node))
+        {
+            ventoy_json_result(conn, VTOY_JSON_DUPLICATE);
+            return 0;
+        }
+
         node = zalloc(sizeof(path_node));
         if (node)
         {
@@ -1969,6 +2036,12 @@ static int ventoy_api_image_list_add(struct mg_connection *conn, VTOY_JSON *json
     path = VTOY_JSON_STR_EX("path");
     if (path)
     {
+        if (ventoy_is_real_exist_common(path, data->list, path_node))
+        {
+            ventoy_json_result(conn, VTOY_JSON_DUPLICATE);
+            return 0;
+        }
+
         node = zalloc(sizeof(path_node));
         if (node)
         {
@@ -2224,6 +2297,12 @@ static int ventoy_api_password_add(struct mg_connection *conn, VTOY_JSON *json)
     pwd = VTOY_JSON_STR_EX("pwd");
     if (path && pwd)
     {
+        if (ventoy_is_real_exist_common(path, data->list, menu_password))
+        {
+            ventoy_json_result(conn, VTOY_JSON_DUPLICATE);
+            return 0;
+        }
+
         node = zalloc(sizeof(menu_password));
         if (node)
         {
@@ -2629,6 +2708,12 @@ static int ventoy_api_dud_add(struct mg_connection *conn, VTOY_JSON *json)
     path = VTOY_JSON_STR_EX("path");
     if (path && array)
     {
+        if (ventoy_is_real_exist_common(path, data->list, dud_node))
+        {
+            ventoy_json_result(conn, VTOY_JSON_DUPLICATE);
+            return 0;
+        }
+        
         node = zalloc(sizeof(dud_node));
         if (node)
         {
@@ -2984,6 +3069,12 @@ static int ventoy_api_auto_install_add(struct mg_connection *conn, VTOY_JSON *js
     path = VTOY_JSON_STR_EX("path");
     if (path && array)
     {
+        if (ventoy_is_real_exist_common(path, data->list, auto_install_node))
+        {
+            ventoy_json_result(conn, VTOY_JSON_DUPLICATE);
+            return 0;
+        }     
+    
         node = zalloc(sizeof(auto_install_node));
         if (node)
         {
@@ -3326,6 +3417,12 @@ static int ventoy_api_persistence_add(struct mg_connection *conn, VTOY_JSON *jso
     path = VTOY_JSON_STR_EX("path");
     if (path && array)
     {
+        if (ventoy_is_real_exist_common(path, data->list, persistence_node))
+        {
+            ventoy_json_result(conn, VTOY_JSON_DUPLICATE);
+            return 0;
+        }
+        
         node = zalloc(sizeof(persistence_node));
         if (node)
         {
@@ -3619,6 +3716,12 @@ static int ventoy_api_injection_add(struct mg_connection *conn, VTOY_JSON *json)
     archive = VTOY_JSON_STR_EX("archive");
     if (path && archive)
     {
+        if (ventoy_is_real_exist_common(path, data->list, injection_node))
+        {
+            ventoy_json_result(conn, VTOY_JSON_DUPLICATE);
+            return 0;
+        }
+    
         node = zalloc(sizeof(injection_node));
         if (node)
         {
@@ -3692,7 +3795,7 @@ static int ventoy_api_preview_json(struct mg_connection *conn, VTOY_JSON *json)
         goto json;
     }
 
-    utf16enclen = utf8_to_utf16((unsigned char *)JSON_SAVE_BUFFER, len, utf16buf, len + 2);
+    utf16enclen = (int)utf8_to_utf16((unsigned char *)JSON_SAVE_BUFFER, len, utf16buf, len + 2);
 
     encodebuf = (char *)malloc(utf16enclen * 4 + 16);
     if (!encodebuf)
@@ -4074,13 +4177,13 @@ static int ventoy_parse_control(VTOY_JSON *json, void *p)
                     }
                 }
             }
-            else if (strcmp(child->pcName, "VTOY_HELP_TXT_LANGUAGE") == 0)
+            else if (strcmp(child->pcName, "VTOY_MENU_LANGUAGE") == 0)
             {
-                for (i = 0; g_ventoy_help_lang[i][0]; i++)
+                for (i = 0; g_ventoy_menu_lang[i][0]; i++)
                 {
-                    if (strcmp(child->unData.pcStrVal, g_ventoy_help_lang[i]) == 0)
+                    if (strcmp(child->unData.pcStrVal, g_ventoy_menu_lang[i]) == 0)
                     {
-                        strlcpy(data->help_text_language, child->unData.pcStrVal);
+                        strlcpy(data->menu_language, child->unData.pcStrVal);
                         break;
                     }
                 }
@@ -5316,25 +5419,26 @@ int ventoy_http_init(void)
     char *Buffer = NULL;
     int BufLen = 0;
 
-    ventoy_read_file_to_buf("www/helplist", 4, (void **)&Buffer, &BufLen);
+    ventoy_read_file_to_buf("www/menulist", 4, (void **)&Buffer, &BufLen);
     if (Buffer)
     {
         for (i = 0; i < BufLen / 5; i++)
         {
-            memcpy(g_ventoy_help_lang[i], Buffer + i * 5, 5);
-            g_ventoy_help_lang[i][5] = 0;
+            memcpy(g_ventoy_menu_lang[i], Buffer + i * 5, 5);
+            g_ventoy_menu_lang[i][5] = 0;
         }
         free(Buffer);
     }
 #else
     ventoy_file *file;
-    file = ventoy_tar_find_file("www/helplist");
+    
+    file = ventoy_tar_find_file("www/menulist");
     if (file)
     {
         for (i = 0; i < file->size / 5; i++)
         {
-            memcpy(g_ventoy_help_lang[i], (char *)(file->addr) + i * 5, 5);
-            g_ventoy_help_lang[i][5] = 0;
+            memcpy(g_ventoy_menu_lang[i], (char *)(file->addr) + i * 5, 5);
+            g_ventoy_menu_lang[i][5] = 0;
         }
     }
 #endif