]> 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 6de2159a0a4f932e6a4ac9db03b350f22bae554e..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>
 
@@ -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;
@@ -913,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;
@@ -928,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)
         {
@@ -974,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;
@@ -983,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)
         {
@@ -1192,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)
         {
@@ -1437,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)
         {
@@ -1786,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)
         {
@@ -1968,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)
         {
@@ -2223,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)
         {
@@ -2628,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)
         {
@@ -2983,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)
         {
@@ -3325,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)
         {
@@ -3618,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)
         {