1 /******************************************************************************
2 * ventoy_http.c ---- ventoy http
3 * Copyright (c) 2021, longpanda <admin@ventoy.net>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 3 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
28 #if defined(_MSC_VER) || defined(WIN32)
32 #include <sys/types.h>
33 #include <sys/ioctl.h>
35 #include <sys/types.h>
36 #include <sys/mount.h>
38 #include <linux/limits.h>
43 #include <ventoy_define.h>
44 #include <ventoy_json.h>
45 #include <ventoy_util.h>
46 #include <ventoy_disk.h>
47 #include <ventoy_http.h>
48 #include "fat_filelib.h"
50 static const char *g_json_title_postfix
[bios_max
+ 1] =
52 "", "_legacy", "_uefi", "_ia32", "_aa64", "_mips", ""
55 static const char *g_ventoy_kbd_layout
[] =
57 "QWERTY_USA", "AZERTY", "CZECH_QWERTY", "CZECH_QWERTZ", "DANISH",
58 "DVORAK_USA", "FRENCH", "GERMAN", "ITALIANO", "JAPAN_106", "LATIN_USA",
59 "PORTU_BRAZIL", "QWERTY_UK", "QWERTZ", "QWERTZ_HUN", "QWERTZ_SLOV_CROAT",
60 "SPANISH", "SWEDISH", "TURKISH_Q", "VIETNAMESE",
64 #define VTOY_DEL_ALL_PATH "4119ae33-98ea-448e-b9c0-569aafcf1fb4"
66 static int g_json_exist
[plugin_type_max
][bios_max
];
67 static const char *g_plugin_name
[plugin_type_max
] =
69 "control", "theme", "menu_alias", "menu_tip",
70 "menu_class", "auto_install", "persistence", "injection",
71 "conf_replace", "password", "image_list",
75 static char g_ventoy_menu_lang
[MAX_LANGUAGE
][8];
77 static char g_pub_path
[2 * MAX_PATH
];
78 static data_control g_data_control
[bios_max
+ 1];
79 static data_theme g_data_theme
[bios_max
+ 1];
80 static data_alias g_data_menu_alias
[bios_max
+ 1];
81 static data_tip g_data_menu_tip
[bios_max
+ 1];
82 static data_class g_data_menu_class
[bios_max
+ 1];
83 static data_image_list g_data_image_list
[bios_max
+ 1];
84 static data_image_list
*g_data_image_blacklist
= g_data_image_list
;
85 static data_auto_memdisk g_data_auto_memdisk
[bios_max
+ 1];
86 static data_password g_data_password
[bios_max
+ 1];
87 static data_conf_replace g_data_conf_replace
[bios_max
+ 1];
88 static data_injection g_data_injection
[bios_max
+ 1];
89 static data_auto_install g_data_auto_install
[bios_max
+ 1];
90 static data_persistence g_data_persistence
[bios_max
+ 1];
91 static data_dud g_data_dud
[bios_max
+ 1];
93 static char *g_pub_json_buffer
= NULL
;
94 static char *g_pub_save_buffer
= NULL
;
95 #define JSON_BUFFER g_pub_json_buffer
96 #define JSON_SAVE_BUFFER g_pub_save_buffer
98 static pthread_mutex_t g_api_mutex
;
99 static struct mg_context
*g_ventoy_http_ctx
= NULL
;
101 #define ventoy_is_real_exist_common(xpath, xnode, xtype) \
102 ventoy_path_is_real_exist(xpath, xnode, offsetof(xtype, path), offsetof(xtype, next))
104 static int ventoy_is_kbd_valid(const char *key
)
108 for (i
= 0; g_ventoy_kbd_layout
[i
]; i
++)
110 if (strcmp(g_ventoy_kbd_layout
[i
], key
) == 0)
119 static const char * ventoy_real_path(const char *org
)
123 if (g_sysinfo
.pathcase
)
125 scnprintf(g_pub_path
, MAX_PATH
, "%s", org
);
126 count
= ventoy_path_case(g_pub_path
+ 1, 1);
140 static int ventoy_json_result(struct mg_connection
*conn
, const char *err
)
143 "HTTP/1.1 200 OK \r\n"
144 "Content-Type: application/json\r\n"
145 "Content-Length: %d\r\n"
147 (int)strlen(err
), err
);
152 static int ventoy_json_buffer(struct mg_connection
*conn
, const char *json_buf
, int json_len
)
155 "HTTP/1.1 200 OK \r\n"
156 "Content-Type: application/json\r\n"
157 "Content-Length: %d\r\n"
164 static void ventoy_free_path_node_list(path_node
*list
)
166 path_node
*next
= NULL
;
167 path_node
*node
= list
;
177 static int ventoy_path_is_real_exist(const char *path
, void *head
, size_t pathoff
, size_t nextoff
)
180 const char *nodepath
= NULL
;
181 const char *realpath
= NULL
;
182 char pathbuf
[MAX_PATH
];
184 if (strchr(path
, '*'))
189 realpath
= ventoy_real_path(path
);
190 scnprintf(pathbuf
, sizeof(pathbuf
), "%s", realpath
);
195 nodepath
= node
+ pathoff
;
196 if (NULL
== strchr(nodepath
, '*'))
198 realpath
= ventoy_real_path(nodepath
);
199 if (strcmp(pathbuf
, realpath
) == 0)
205 memcpy(&node
, node
+ nextoff
, sizeof(node
));
211 static path_node
* ventoy_path_node_add_array(VTOY_JSON
*array
)
213 path_node
*head
= NULL
;
214 path_node
*node
= NULL
;
215 path_node
*cur
= NULL
;
216 VTOY_JSON
*item
= NULL
;
218 for (item
= array
->pstChild
; item
; item
= item
->pstNext
)
220 node
= zalloc(sizeof(path_node
));
223 scnprintf(node
->path
, sizeof(node
->path
), "%s", item
->unData
.pcStrVal
);
224 vtoy_list_add(head
, cur
, node
);
231 static int ventoy_check_fuzzy_path(char *path
, int prefix
)
243 pos
= strchr(path
, '*');
246 for (cur
= pos
; *cur
; cur
++)
271 rc
= ventoy_is_directory_exist("%s%s", g_cur_dir
, path
);
275 rc
= ventoy_is_directory_exist("%s", path
);
296 return ventoy_is_file_exist("%s%s", g_cur_dir
, path
);
300 return ventoy_is_file_exist("%s", path
);
305 static int ventoy_path_list_cmp(path_node
*list1
, path_node
*list2
)
307 if (NULL
== list1
&& NULL
== list2
)
311 else if (list1
&& list2
)
313 while (list1
&& list2
)
315 if (strcmp(list1
->path
, list2
->path
))
324 if (list1
== NULL
&& list2
== NULL
)
337 static int ventoy_api_device_info(struct mg_connection
*conn
, VTOY_JSON
*json
)
343 VTOY_JSON_FMT_BEGIN(pos
, JSON_BUFFER
, JSON_BUF_MAX
);
344 VTOY_JSON_FMT_OBJ_BEGIN();
346 VTOY_JSON_FMT_STRN("dev_name", g_sysinfo
.cur_model
);
347 VTOY_JSON_FMT_STRN("dev_capacity", g_sysinfo
.cur_capacity
);
348 VTOY_JSON_FMT_STRN("dev_fs", g_sysinfo
.cur_fsname
);
349 VTOY_JSON_FMT_STRN("ventoy_ver", g_sysinfo
.cur_ventoy_ver
);
350 VTOY_JSON_FMT_SINT("part_style", g_sysinfo
.cur_part_style
);
351 VTOY_JSON_FMT_SINT("secure_boot", g_sysinfo
.cur_secureboot
);
353 VTOY_JSON_FMT_OBJ_END();
354 VTOY_JSON_FMT_END(pos
);
356 ventoy_json_buffer(conn
, JSON_BUFFER
, pos
);
360 static int ventoy_api_sysinfo(struct mg_connection
*conn
, VTOY_JSON
*json
)
366 VTOY_JSON_FMT_BEGIN(pos
, JSON_BUFFER
, JSON_BUF_MAX
);
367 VTOY_JSON_FMT_OBJ_BEGIN();
368 VTOY_JSON_FMT_STRN("language", ventoy_get_os_language());
369 VTOY_JSON_FMT_STRN("curdir", g_cur_dir
);
372 VTOY_JSON_FMT_SINT("syntax_error", g_sysinfo
.syntax_error
);
373 g_sysinfo
.syntax_error
= 0;
375 VTOY_JSON_FMT_SINT("invalid_config", g_sysinfo
.invalid_config
);
376 g_sysinfo
.invalid_config
= 0;
378 #if defined(_MSC_VER) || defined(WIN32)
379 VTOY_JSON_FMT_STRN("os", "windows");
381 VTOY_JSON_FMT_STRN("os", "linux");
384 VTOY_JSON_FMT_OBJ_END();
385 VTOY_JSON_FMT_END(pos
);
387 ventoy_json_buffer(conn
, JSON_BUFFER
, pos
);
391 static int ventoy_api_handshake(struct mg_connection
*conn
, VTOY_JSON
*json
)
400 VTOY_JSON_FMT_BEGIN(pos
, JSON_BUFFER
, JSON_BUF_MAX
);
401 VTOY_JSON_FMT_OBJ_BEGIN();
402 VTOY_JSON_FMT_SINT("status", 0);
403 VTOY_JSON_FMT_SINT("save_error", g_sysinfo
.config_save_error
);
404 g_sysinfo
.config_save_error
= 0;
406 for (i
= 0; i
< plugin_type_max
; i
++)
408 scnprintf(key
, sizeof(key
), "exist_%s", g_plugin_name
[i
]);
409 VTOY_JSON_FMT_KEY(key
);
410 VTOY_JSON_FMT_ARY_BEGIN();
411 for (j
= 0; j
< bios_max
; j
++)
413 VTOY_JSON_FMT_ITEM_INT(g_json_exist
[i
][j
]);
415 VTOY_JSON_FMT_ARY_ENDEX();
418 VTOY_JSON_FMT_OBJ_END();
419 VTOY_JSON_FMT_END(pos
);
421 ventoy_json_buffer(conn
, JSON_BUFFER
, pos
);
425 static int ventoy_api_check_exist(struct mg_connection
*conn
, VTOY_JSON
*json
)
430 const char *path
= NULL
;
432 path
= vtoy_json_get_string_ex(json
, "path");
433 vtoy_json_get_int(json
, "dir", &dir
);
439 exist
= ventoy_is_directory_exist("%s", path
);
443 exist
= ventoy_is_file_exist("%s", path
);
447 VTOY_JSON_FMT_BEGIN(pos
, JSON_BUFFER
, JSON_BUF_MAX
);
448 VTOY_JSON_FMT_OBJ_BEGIN();
449 VTOY_JSON_FMT_SINT("exist", exist
);
450 VTOY_JSON_FMT_OBJ_END();
451 VTOY_JSON_FMT_END(pos
);
453 ventoy_json_buffer(conn
, JSON_BUFFER
, pos
);
457 static int ventoy_api_check_exist2(struct mg_connection
*conn
, VTOY_JSON
*json
)
466 const char *path1
= NULL
;
467 const char *path2
= NULL
;
469 path1
= vtoy_json_get_string_ex(json
, "path1");
470 path2
= vtoy_json_get_string_ex(json
, "path2");
471 vtoy_json_get_int(json
, "dir1", &dir1
);
472 vtoy_json_get_int(json
, "dir2", &dir2
);
473 vtoy_json_get_int(json
, "fuzzy1", &fuzzy1
);
474 vtoy_json_get_int(json
, "fuzzy2", &fuzzy2
);
480 exist1
= ventoy_is_directory_exist("%s", path1
);
486 exist1
= ventoy_check_fuzzy_path((char *)path1
, 0);
490 exist1
= ventoy_is_file_exist("%s", path1
);
499 exist2
= ventoy_is_directory_exist("%s", path2
);
505 exist2
= ventoy_check_fuzzy_path((char *)path2
, 0);
509 exist2
= ventoy_is_file_exist("%s", path2
);
514 VTOY_JSON_FMT_BEGIN(pos
, JSON_BUFFER
, JSON_BUF_MAX
);
515 VTOY_JSON_FMT_OBJ_BEGIN();
516 VTOY_JSON_FMT_SINT("exist1", exist1
);
517 VTOY_JSON_FMT_SINT("exist2", exist2
);
518 VTOY_JSON_FMT_OBJ_END();
519 VTOY_JSON_FMT_END(pos
);
521 ventoy_json_buffer(conn
, JSON_BUFFER
, pos
);
525 static int ventoy_api_check_fuzzy(struct mg_connection
*conn
, VTOY_JSON
*json
)
529 const char *path
= NULL
;
531 path
= vtoy_json_get_string_ex(json
, "path");
534 exist
= ventoy_check_fuzzy_path((char *)path
, 0);
537 VTOY_JSON_FMT_BEGIN(pos
, JSON_BUFFER
, JSON_BUF_MAX
);
538 VTOY_JSON_FMT_OBJ_BEGIN();
539 VTOY_JSON_FMT_SINT("exist", exist
);
540 VTOY_JSON_FMT_OBJ_END();
541 VTOY_JSON_FMT_END(pos
);
543 ventoy_json_buffer(conn
, JSON_BUFFER
, pos
);
550 void ventoy_data_default_control(data_control
*data
)
552 memset(data
, 0, sizeof(data_control
));
554 data
->password_asterisk
= 1;
555 data
->secondary_menu
= 1;
556 data
->filter_dot_underscore
= 1;
557 data
->max_search_level
= -1;
558 data
->menu_timeout
= 0;
559 data
->secondary_menu_timeout
= 0;
561 strlcpy(data
->default_kbd_layout
, "QWERTY_USA");
562 strlcpy(data
->menu_language
, "en_US");
565 int ventoy_data_cmp_control(data_control
*data1
, data_control
*data2
)
567 if (data1
->default_menu_mode
!= data2
->default_menu_mode
||
568 data1
->treeview_style
!= data2
->treeview_style
||
569 data1
->filter_dot_underscore
!= data2
->filter_dot_underscore
||
570 data1
->sort_casesensitive
!= data2
->sort_casesensitive
||
571 data1
->max_search_level
!= data2
->max_search_level
||
572 data1
->vhd_no_warning
!= data2
->vhd_no_warning
||
573 data1
->filter_iso
!= data2
->filter_iso
||
574 data1
->filter_wim
!= data2
->filter_wim
||
575 data1
->filter_efi
!= data2
->filter_efi
||
576 data1
->filter_img
!= data2
->filter_img
||
577 data1
->filter_vhd
!= data2
->filter_vhd
||
578 data1
->filter_vtoy
!= data2
->filter_vtoy
||
579 data1
->win11_bypass_check
!= data2
->win11_bypass_check
||
580 data1
->linux_remount
!= data2
->linux_remount
||
581 data1
->password_asterisk
!= data2
->password_asterisk
||
582 data1
->secondary_menu
!= data2
->secondary_menu
||
583 data1
->menu_timeout
!= data2
->menu_timeout
||
584 data1
->secondary_menu_timeout
!= data2
->secondary_menu_timeout
)
589 if (strcmp(data1
->default_search_root
, data2
->default_search_root
) ||
590 strcmp(data1
->default_image
, data2
->default_image
) ||
591 strcmp(data1
->default_kbd_layout
, data2
->default_kbd_layout
) ||
592 strcmp(data1
->menu_language
, data2
->menu_language
))
600 int ventoy_data_save_control(data_control
*data
, const char *title
, char *buf
, int buflen
)
603 data_control
*def
= g_data_control
+ bios_max
;
605 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
607 VTOY_JSON_FMT_KEY_L(L1
, title
);
608 VTOY_JSON_FMT_ARY_BEGIN_N();
610 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_DEFAULT_MENU_MODE", default_menu_mode
);
611 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_TREE_VIEW_MENU_STYLE", treeview_style
);
612 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_FILT_DOT_UNDERSCORE_FILE", filter_dot_underscore
);
613 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_SORT_CASE_SENSITIVE", sort_casesensitive
);
615 if (data
->max_search_level
>= 0)
617 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_MAX_SEARCH_LEVEL", max_search_level
);
620 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_VHD_NO_WARNING", vhd_no_warning
);
621 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_FILE_FLT_ISO", filter_iso
);
622 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_FILE_FLT_WIM", filter_wim
);
623 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_FILE_FLT_EFI", filter_efi
);
624 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_FILE_FLT_IMG", filter_img
);
625 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_FILE_FLT_VHD", filter_vhd
);
626 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_FILE_FLT_VTOY", filter_vtoy
);
627 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_WIN11_BYPASS_CHECK", win11_bypass_check
);
628 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_LINUX_REMOUNT", linux_remount
);
629 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_SECONDARY_BOOT_MENU", secondary_menu
);
630 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_SHOW_PASSWORD_ASTERISK", password_asterisk
);
631 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_MENU_TIMEOUT", menu_timeout
);
632 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_SECONDARY_TIMEOUT", secondary_menu_timeout
);
634 VTOY_JSON_FMT_CTRL_STRN(L2
, "VTOY_DEFAULT_KBD_LAYOUT", default_kbd_layout
);
635 VTOY_JSON_FMT_CTRL_STRN(L2
, "VTOY_MENU_LANGUAGE", menu_language
);
637 if (strcmp(def
->default_search_root
, data
->default_search_root
))
639 VTOY_JSON_FMT_CTRL_STRN_STR(L2
, "VTOY_DEFAULT_SEARCH_ROOT", ventoy_real_path(data
->default_search_root
));
642 if (strcmp(def
->default_image
, data
->default_image
))
644 VTOY_JSON_FMT_CTRL_STRN_STR(L2
, "VTOY_DEFAULT_IMAGE", ventoy_real_path(data
->default_image
));
647 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
648 VTOY_JSON_FMT_END(pos
);
653 int ventoy_data_json_control(data_control
*ctrl
, char *buf
, int buflen
)
659 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
660 VTOY_JSON_FMT_OBJ_BEGIN();
662 VTOY_JSON_FMT_SINT("default_menu_mode", ctrl
->default_menu_mode
);
663 VTOY_JSON_FMT_SINT("treeview_style", ctrl
->treeview_style
);
664 VTOY_JSON_FMT_SINT("filter_dot_underscore", ctrl
->filter_dot_underscore
);
665 VTOY_JSON_FMT_SINT("sort_casesensitive", ctrl
->sort_casesensitive
);
666 VTOY_JSON_FMT_SINT("max_search_level", ctrl
->max_search_level
);
667 VTOY_JSON_FMT_SINT("vhd_no_warning", ctrl
->vhd_no_warning
);
669 VTOY_JSON_FMT_SINT("filter_iso", ctrl
->filter_iso
);
670 VTOY_JSON_FMT_SINT("filter_wim", ctrl
->filter_wim
);
671 VTOY_JSON_FMT_SINT("filter_efi", ctrl
->filter_efi
);
672 VTOY_JSON_FMT_SINT("filter_img", ctrl
->filter_img
);
673 VTOY_JSON_FMT_SINT("filter_vhd", ctrl
->filter_vhd
);
674 VTOY_JSON_FMT_SINT("filter_vtoy", ctrl
->filter_vtoy
);
675 VTOY_JSON_FMT_SINT("win11_bypass_check", ctrl
->win11_bypass_check
);
676 VTOY_JSON_FMT_SINT("linux_remount", ctrl
->linux_remount
);
677 VTOY_JSON_FMT_SINT("secondary_menu", ctrl
->secondary_menu
);
678 VTOY_JSON_FMT_SINT("password_asterisk", ctrl
->password_asterisk
);
679 VTOY_JSON_FMT_SINT("menu_timeout", ctrl
->menu_timeout
);
680 VTOY_JSON_FMT_SINT("secondary_menu_timeout", ctrl
->secondary_menu_timeout
);
681 VTOY_JSON_FMT_STRN("default_kbd_layout", ctrl
->default_kbd_layout
);
682 VTOY_JSON_FMT_STRN("menu_language", ctrl
->menu_language
);
685 if (ctrl
->default_search_root
[0] && ventoy_is_directory_exist("%s%s", g_cur_dir
, ctrl
->default_search_root
))
689 VTOY_JSON_FMT_STRN("default_search_root", ctrl
->default_search_root
);
690 VTOY_JSON_FMT_SINT("default_search_root_valid", valid
);
694 if (ctrl
->default_image
[0] && ventoy_is_file_exist("%s%s", g_cur_dir
, ctrl
->default_image
))
698 VTOY_JSON_FMT_STRN("default_image", ctrl
->default_image
);
699 VTOY_JSON_FMT_SINT("default_image_valid", valid
);
701 VTOY_JSON_FMT_KEY("menu_list");
702 VTOY_JSON_FMT_ARY_BEGIN();
704 for (i
= 0; g_ventoy_menu_lang
[i
][0]; i
++)
706 VTOY_JSON_FMT_ITEM(g_ventoy_menu_lang
[i
]);
708 VTOY_JSON_FMT_ARY_ENDEX();
710 VTOY_JSON_FMT_OBJ_END();
711 VTOY_JSON_FMT_END(pos
);
716 static int ventoy_api_get_control(struct mg_connection
*conn
, VTOY_JSON
*json
)
718 api_get_func(conn
, json
, control
);
722 static int ventoy_api_save_control(struct mg_connection
*conn
, VTOY_JSON
*json
)
726 data_control
*ctrl
= NULL
;
728 vtoy_json_get_int(json
, "index", &index
);
729 ctrl
= g_data_control
+ index
;
731 VTOY_JSON_INT("default_menu_mode", ctrl
->default_menu_mode
);
732 VTOY_JSON_INT("treeview_style", ctrl
->treeview_style
);
733 VTOY_JSON_INT("filter_dot_underscore", ctrl
->filter_dot_underscore
);
734 VTOY_JSON_INT("sort_casesensitive", ctrl
->sort_casesensitive
);
735 VTOY_JSON_INT("max_search_level", ctrl
->max_search_level
);
736 VTOY_JSON_INT("vhd_no_warning", ctrl
->vhd_no_warning
);
737 VTOY_JSON_INT("filter_iso", ctrl
->filter_iso
);
738 VTOY_JSON_INT("filter_wim", ctrl
->filter_wim
);
739 VTOY_JSON_INT("filter_efi", ctrl
->filter_efi
);
740 VTOY_JSON_INT("filter_img", ctrl
->filter_img
);
741 VTOY_JSON_INT("filter_vhd", ctrl
->filter_vhd
);
742 VTOY_JSON_INT("filter_vtoy", ctrl
->filter_vtoy
);
743 VTOY_JSON_INT("win11_bypass_check", ctrl
->win11_bypass_check
);
744 VTOY_JSON_INT("linux_remount", ctrl
->linux_remount
);
745 VTOY_JSON_INT("secondary_menu", ctrl
->secondary_menu
);
746 VTOY_JSON_INT("password_asterisk", ctrl
->password_asterisk
);
747 VTOY_JSON_INT("menu_timeout", ctrl
->menu_timeout
);
748 VTOY_JSON_INT("secondary_menu_timeout", ctrl
->secondary_menu_timeout
);
750 VTOY_JSON_STR("default_image", ctrl
->default_image
);
751 VTOY_JSON_STR("default_search_root", ctrl
->default_search_root
);
752 VTOY_JSON_STR("menu_language", ctrl
->menu_language
);
753 VTOY_JSON_STR("default_kbd_layout", ctrl
->default_kbd_layout
);
755 ret
= ventoy_data_save_all();
757 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
765 void ventoy_data_default_theme(data_theme
*data
)
767 memset(data
, 0, sizeof(data_theme
));
768 strlcpy(data
->gfxmode
, "1024x768");
769 scnprintf(data
->ventoy_left
, sizeof(data
->ventoy_left
), "5%%");
770 scnprintf(data
->ventoy_top
, sizeof(data
->ventoy_top
), "95%%");
771 scnprintf(data
->ventoy_color
, sizeof(data
->ventoy_color
), "%s", "#0000ff");
774 int ventoy_data_cmp_theme(data_theme
*data1
, data_theme
*data2
)
776 if (data1
->display_mode
!= data2
->display_mode
||
777 strcmp(data1
->ventoy_left
, data2
->ventoy_left
) ||
778 strcmp(data1
->ventoy_top
, data2
->ventoy_top
) ||
779 strcmp(data1
->gfxmode
, data2
->gfxmode
) ||
780 strcmp(data1
->ventoy_color
, data2
->ventoy_color
)
786 if (ventoy_path_list_cmp(data1
->filelist
, data2
->filelist
))
791 if (ventoy_path_list_cmp(data1
->fontslist
, data2
->fontslist
))
800 int ventoy_data_save_theme(data_theme
*data
, const char *title
, char *buf
, int buflen
)
803 path_node
*node
= NULL
;
804 data_theme
*def
= g_data_theme
+ bios_max
;
806 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
808 VTOY_JSON_FMT_KEY_L(L1
, title
);
809 VTOY_JSON_FMT_OBJ_BEGIN_N();
813 if (data
->filelist
->next
)
815 VTOY_JSON_FMT_KEY_L(L2
, "file");
816 VTOY_JSON_FMT_ARY_BEGIN_N();
818 for (node
= data
->filelist
; node
; node
= node
->next
)
820 VTOY_JSON_FMT_ITEM_PATH_LN(L3
, node
->path
);
823 VTOY_JSON_FMT_ARY_ENDEX_LN(L2
);
825 if (def
->default_file
!= data
->default_file
)
827 VTOY_JSON_FMT_SINT_LN(L2
, "default_file", data
->default_file
);
830 if (def
->resolution_fit
!= data
->resolution_fit
)
832 VTOY_JSON_FMT_SINT_LN(L2
, "resolution_fit", data
->resolution_fit
);
837 VTOY_JSON_FMT_STRN_PATH_LN(L2
, "file", data
->filelist
->path
);
841 if (data
->display_mode
!= def
->display_mode
)
843 if (display_mode_cli
== data
->display_mode
)
845 VTOY_JSON_FMT_STRN_LN(L2
, "display_mode", "CLI");
847 else if (display_mode_serial
== data
->display_mode
)
849 VTOY_JSON_FMT_STRN_LN(L2
, "display_mode", "serial");
851 else if (display_mode_ser_console
== data
->display_mode
)
853 VTOY_JSON_FMT_STRN_LN(L2
, "display_mode", "serial_console");
857 VTOY_JSON_FMT_STRN_LN(L2
, "display_mode", "GUI");
861 VTOY_JSON_FMT_DIFF_STRN(L2
, "gfxmode", gfxmode
);
863 VTOY_JSON_FMT_DIFF_STRN(L2
, "ventoy_left", ventoy_left
);
864 VTOY_JSON_FMT_DIFF_STRN(L2
, "ventoy_top", ventoy_top
);
865 VTOY_JSON_FMT_DIFF_STRN(L2
, "ventoy_color", ventoy_color
);
869 VTOY_JSON_FMT_KEY_L(L2
, "fonts");
870 VTOY_JSON_FMT_ARY_BEGIN_N();
872 for (node
= data
->fontslist
; node
; node
= node
->next
)
874 VTOY_JSON_FMT_ITEM_PATH_LN(L3
, node
->path
);
877 VTOY_JSON_FMT_ARY_ENDEX_LN(L2
);
880 VTOY_JSON_FMT_OBJ_ENDEX_LN(L1
);
881 VTOY_JSON_FMT_END(pos
);
887 int ventoy_data_json_theme(data_theme
*data
, char *buf
, int buflen
)
890 path_node
*node
= NULL
;
892 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
893 VTOY_JSON_FMT_OBJ_BEGIN();
895 VTOY_JSON_FMT_SINT("default_file", data
->default_file
);
896 VTOY_JSON_FMT_SINT("resolution_fit", data
->resolution_fit
);
897 VTOY_JSON_FMT_SINT("display_mode", data
->display_mode
);
898 VTOY_JSON_FMT_STRN("gfxmode", data
->gfxmode
);
900 VTOY_JSON_FMT_STRN("ventoy_color", data
->ventoy_color
);
901 VTOY_JSON_FMT_STRN("ventoy_left", data
->ventoy_left
);
902 VTOY_JSON_FMT_STRN("ventoy_top", data
->ventoy_top
);
904 VTOY_JSON_FMT_KEY("filelist");
905 VTOY_JSON_FMT_ARY_BEGIN();
906 for (node
= data
->filelist
; node
; node
= node
->next
)
908 VTOY_JSON_FMT_OBJ_BEGIN();
909 VTOY_JSON_FMT_STRN("path", node
->path
);
910 VTOY_JSON_FMT_SINT("valid", ventoy_is_file_exist("%s%s", g_cur_dir
, node
->path
));
911 VTOY_JSON_FMT_OBJ_ENDEX();
913 VTOY_JSON_FMT_ARY_ENDEX();
915 VTOY_JSON_FMT_KEY("fontslist");
916 VTOY_JSON_FMT_ARY_BEGIN();
917 for (node
= data
->fontslist
; node
; node
= node
->next
)
919 VTOY_JSON_FMT_OBJ_BEGIN();
920 VTOY_JSON_FMT_STRN("path", node
->path
);
921 VTOY_JSON_FMT_SINT("valid", ventoy_is_file_exist("%s%s", g_cur_dir
, node
->path
));
922 VTOY_JSON_FMT_OBJ_ENDEX();
924 VTOY_JSON_FMT_ARY_ENDEX();
926 VTOY_JSON_FMT_OBJ_END();
927 VTOY_JSON_FMT_END(pos
);
932 static int ventoy_api_get_theme(struct mg_connection
*conn
, VTOY_JSON
*json
)
934 api_get_func(conn
, json
, theme
);
938 static int ventoy_api_save_theme(struct mg_connection
*conn
, VTOY_JSON
*json
)
942 data_theme
*data
= NULL
;
944 vtoy_json_get_int(json
, "index", &index
);
945 data
= g_data_theme
+ index
;
947 VTOY_JSON_INT("default_file", data
->default_file
);
948 VTOY_JSON_INT("resolution_fit", data
->resolution_fit
);
949 VTOY_JSON_INT("display_mode", data
->display_mode
);
950 VTOY_JSON_STR("gfxmode", data
->gfxmode
);
951 VTOY_JSON_STR("ventoy_left", data
->ventoy_left
);
952 VTOY_JSON_STR("ventoy_top", data
->ventoy_top
);
953 VTOY_JSON_STR("ventoy_color", data
->ventoy_color
);
955 ret
= ventoy_data_save_all();
957 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
962 static int ventoy_api_theme_add_file(struct mg_connection
*conn
, VTOY_JSON
*json
)
966 const char *path
= NULL
;
967 path_node
*node
= NULL
;
968 path_node
*cur
= NULL
;
969 data_theme
*data
= NULL
;
971 vtoy_json_get_int(json
, "index", &index
);
972 data
= g_data_theme
+ index
;
974 path
= VTOY_JSON_STR_EX("path");
977 if (ventoy_is_real_exist_common(path
, data
->filelist
, path_node
))
979 ventoy_json_result(conn
, VTOY_JSON_DUPLICATE
);
983 node
= zalloc(sizeof(path_node
));
986 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
988 vtoy_list_add(data
->filelist
, cur
, node
);
992 ret
= ventoy_data_save_all();
994 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
998 static int ventoy_api_theme_del_file(struct mg_connection
*conn
, VTOY_JSON
*json
)
1002 const char *path
= NULL
;
1003 path_node
*node
= NULL
;
1004 path_node
*last
= NULL
;
1005 data_theme
*data
= NULL
;
1007 vtoy_json_get_int(json
, "index", &index
);
1008 data
= g_data_theme
+ index
;
1010 path
= VTOY_JSON_STR_EX("path");
1013 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
1015 vtoy_list_free(path_node
, data
->filelist
);
1019 vtoy_list_del(last
, node
, data
->filelist
, path
);
1023 ret
= ventoy_data_save_all();
1025 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1029 static int ventoy_api_theme_add_font(struct mg_connection
*conn
, VTOY_JSON
*json
)
1033 const char *path
= NULL
;
1034 path_node
*node
= NULL
;
1035 path_node
*cur
= NULL
;
1036 data_theme
*data
= NULL
;
1038 vtoy_json_get_int(json
, "index", &index
);
1039 data
= g_data_theme
+ index
;
1041 path
= VTOY_JSON_STR_EX("path");
1044 if (ventoy_is_real_exist_common(path
, data
->fontslist
, path_node
))
1046 ventoy_json_result(conn
, VTOY_JSON_DUPLICATE
);
1050 node
= zalloc(sizeof(path_node
));
1053 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
1054 vtoy_list_add(data
->fontslist
, cur
, node
);
1058 ret
= ventoy_data_save_all();
1060 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1065 static int ventoy_api_theme_del_font(struct mg_connection
*conn
, VTOY_JSON
*json
)
1069 const char *path
= NULL
;
1070 path_node
*node
= NULL
;
1071 path_node
*last
= NULL
;
1072 data_theme
*data
= NULL
;
1074 vtoy_json_get_int(json
, "index", &index
);
1075 data
= g_data_theme
+ index
;
1077 path
= VTOY_JSON_STR_EX("path");
1080 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
1082 vtoy_list_free(path_node
, data
->fontslist
);
1086 vtoy_list_del(last
, node
, data
->fontslist
, path
);
1090 ret
= ventoy_data_save_all();
1092 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1099 void ventoy_data_default_menu_alias(data_alias
*data
)
1101 memset(data
, 0, sizeof(data_alias
));
1104 int ventoy_data_cmp_menu_alias(data_alias
*data1
, data_alias
*data2
)
1106 data_alias_node
*list1
= NULL
;
1107 data_alias_node
*list2
= NULL
;
1109 if (NULL
== data1
->list
&& NULL
== data2
->list
)
1113 else if (data1
->list
&& data2
->list
)
1115 list1
= data1
->list
;
1116 list2
= data2
->list
;
1118 while (list1
&& list2
)
1120 if ((list1
->type
!= list2
->type
) ||
1121 strcmp(list1
->path
, list2
->path
) ||
1122 strcmp(list1
->alias
, list2
->alias
))
1127 list1
= list1
->next
;
1128 list2
= list2
->next
;
1131 if (list1
== NULL
&& list2
== NULL
)
1146 int ventoy_data_save_menu_alias(data_alias
*data
, const char *title
, char *buf
, int buflen
)
1149 data_alias_node
*node
= NULL
;
1151 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1153 VTOY_JSON_FMT_KEY_L(L1
, title
);
1154 VTOY_JSON_FMT_ARY_BEGIN_N();
1156 for (node
= data
->list
; node
; node
= node
->next
)
1158 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
1160 if (node
->type
== path_type_file
)
1162 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "image", node
->path
);
1166 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "dir", node
->path
);
1169 VTOY_JSON_FMT_STRN_EX_LN(L3
, "alias", node
->alias
);
1171 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
1174 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
1175 VTOY_JSON_FMT_END(pos
);
1181 int ventoy_data_json_menu_alias(data_alias
*data
, char *buf
, int buflen
)
1185 data_alias_node
*node
= NULL
;
1187 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1188 VTOY_JSON_FMT_ARY_BEGIN();
1190 for (node
= data
->list
; node
; node
= node
->next
)
1192 VTOY_JSON_FMT_OBJ_BEGIN();
1194 VTOY_JSON_FMT_UINT("type", node
->type
);
1195 VTOY_JSON_FMT_STRN("path", node
->path
);
1196 if (node
->type
== path_type_file
)
1198 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
1202 valid
= ventoy_is_directory_exist("%s%s", g_cur_dir
, node
->path
);
1205 VTOY_JSON_FMT_SINT("valid", valid
);
1206 VTOY_JSON_FMT_STRN("alias", node
->alias
);
1208 VTOY_JSON_FMT_OBJ_ENDEX();
1211 VTOY_JSON_FMT_ARY_END();
1212 VTOY_JSON_FMT_END(pos
);
1217 static int ventoy_api_get_alias(struct mg_connection
*conn
, VTOY_JSON
*json
)
1219 api_get_func(conn
, json
, menu_alias
);
1223 static int ventoy_api_save_alias(struct mg_connection
*conn
, VTOY_JSON
*json
)
1226 ret
= ventoy_data_save_all();
1228 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1232 static int ventoy_api_alias_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
1236 int type
= path_type_file
;
1237 const char *path
= NULL
;
1238 const char *alias
= NULL
;
1239 data_alias_node
*node
= NULL
;
1240 data_alias_node
*cur
= NULL
;
1241 data_alias
*data
= NULL
;
1243 vtoy_json_get_int(json
, "index", &index
);
1244 data
= g_data_menu_alias
+ index
;
1246 vtoy_json_get_int(json
, "type", &type
);
1248 path
= VTOY_JSON_STR_EX("path");
1249 alias
= VTOY_JSON_STR_EX("alias");
1252 if (ventoy_is_real_exist_common(path
, data
->list
, data_alias_node
))
1254 ventoy_json_result(conn
, VTOY_JSON_DUPLICATE
);
1258 node
= zalloc(sizeof(data_alias_node
));
1262 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
1263 scnprintf(node
->alias
, sizeof(node
->alias
), "%s", alias
);
1265 vtoy_list_add(data
->list
, cur
, node
);
1269 ret
= ventoy_data_save_all();
1271 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1275 static int ventoy_api_alias_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
1279 const char *path
= NULL
;
1280 data_alias_node
*last
= NULL
;
1281 data_alias_node
*node
= NULL
;
1282 data_alias
*data
= NULL
;
1284 vtoy_json_get_int(json
, "index", &index
);
1285 data
= g_data_menu_alias
+ index
;
1287 path
= VTOY_JSON_STR_EX("path");
1290 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
1292 vtoy_list_free(data_alias_node
, data
->list
);
1296 vtoy_list_del(last
, node
, data
->list
, path
);
1300 ret
= ventoy_data_save_all();
1302 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1309 void ventoy_data_default_menu_tip(data_tip
*data
)
1311 memset(data
, 0, sizeof(data_tip
));
1313 scnprintf(data
->left
, sizeof(data
->left
), "10%%");
1314 scnprintf(data
->top
, sizeof(data
->top
), "81%%");
1315 scnprintf(data
->color
, sizeof(data
->color
), "%s", "blue");
1318 int ventoy_data_cmp_menu_tip(data_tip
*data1
, data_tip
*data2
)
1320 data_tip_node
*list1
= NULL
;
1321 data_tip_node
*list2
= NULL
;
1323 if (strcmp(data1
->left
, data2
->left
) || strcmp(data1
->top
, data2
->top
) || strcmp(data1
->color
, data2
->color
))
1328 if (NULL
== data1
->list
&& NULL
== data2
->list
)
1332 else if (data1
->list
&& data2
->list
)
1334 list1
= data1
->list
;
1335 list2
= data2
->list
;
1337 while (list1
&& list2
)
1339 if ((list1
->type
!= list2
->type
) ||
1340 strcmp(list1
->path
, list2
->path
) ||
1341 strcmp(list1
->tip
, list2
->tip
))
1346 list1
= list1
->next
;
1347 list2
= list2
->next
;
1350 if (list1
== NULL
&& list2
== NULL
)
1365 int ventoy_data_save_menu_tip(data_tip
*data
, const char *title
, char *buf
, int buflen
)
1368 data_tip_node
*node
= NULL
;
1369 data_tip
*def
= g_data_menu_tip
+ bios_max
;
1371 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1372 VTOY_JSON_FMT_KEY_L(L1
, title
);
1373 VTOY_JSON_FMT_OBJ_BEGIN_N();
1375 VTOY_JSON_FMT_DIFF_STRN(L2
, "left", left
);
1376 VTOY_JSON_FMT_DIFF_STRN(L2
, "top", top
);
1377 VTOY_JSON_FMT_DIFF_STRN(L2
, "color", color
);
1381 VTOY_JSON_FMT_KEY_L(L2
, "tips");
1382 VTOY_JSON_FMT_ARY_BEGIN_N();
1384 for (node
= data
->list
; node
; node
= node
->next
)
1386 VTOY_JSON_FMT_OBJ_BEGIN_LN(L3
);
1388 if (node
->type
== path_type_file
)
1390 VTOY_JSON_FMT_STRN_PATH_LN(L4
, "image", node
->path
);
1394 VTOY_JSON_FMT_STRN_PATH_LN(L4
, "dir", node
->path
);
1396 VTOY_JSON_FMT_STRN_EX_LN(L4
, "tip", node
->tip
);
1398 VTOY_JSON_FMT_OBJ_ENDEX_LN(L3
);
1401 VTOY_JSON_FMT_ARY_ENDEX_LN(L2
);
1404 VTOY_JSON_FMT_OBJ_ENDEX_LN(L1
);
1406 VTOY_JSON_FMT_END(pos
);
1412 int ventoy_data_json_menu_tip(data_tip
*data
, char *buf
, int buflen
)
1416 data_tip_node
*node
= NULL
;
1418 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1420 VTOY_JSON_FMT_OBJ_BEGIN();
1422 VTOY_JSON_FMT_STRN("left", data
->left
);
1423 VTOY_JSON_FMT_STRN("top", data
->top
);
1424 VTOY_JSON_FMT_STRN("color", data
->color
);
1426 VTOY_JSON_FMT_KEY("tips");
1427 VTOY_JSON_FMT_ARY_BEGIN();
1429 for (node
= data
->list
; node
; node
= node
->next
)
1431 VTOY_JSON_FMT_OBJ_BEGIN();
1433 VTOY_JSON_FMT_UINT("type", node
->type
);
1434 VTOY_JSON_FMT_STRN("path", node
->path
);
1435 if (node
->type
== path_type_file
)
1437 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
1441 valid
= ventoy_is_directory_exist("%s%s", g_cur_dir
, node
->path
);
1444 VTOY_JSON_FMT_SINT("valid", valid
);
1445 VTOY_JSON_FMT_STRN("tip", node
->tip
);
1447 VTOY_JSON_FMT_OBJ_ENDEX();
1450 VTOY_JSON_FMT_ARY_ENDEX();
1452 VTOY_JSON_FMT_OBJ_END();
1453 VTOY_JSON_FMT_END(pos
);
1458 static int ventoy_api_get_tip(struct mg_connection
*conn
, VTOY_JSON
*json
)
1460 api_get_func(conn
, json
, menu_tip
);
1464 static int ventoy_api_save_tip(struct mg_connection
*conn
, VTOY_JSON
*json
)
1468 data_tip
*data
= NULL
;
1470 vtoy_json_get_int(json
, "index", &index
);
1471 data
= g_data_menu_tip
+ index
;
1473 VTOY_JSON_STR("left", data
->left
);
1474 VTOY_JSON_STR("top", data
->top
);
1475 VTOY_JSON_STR("color", data
->color
);
1477 ret
= ventoy_data_save_all();
1479 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1483 static int ventoy_api_tip_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
1487 int type
= path_type_file
;
1488 const char *path
= NULL
;
1489 const char *tip
= NULL
;
1490 data_tip_node
*node
= NULL
;
1491 data_tip_node
*cur
= NULL
;
1492 data_tip
*data
= NULL
;
1494 vtoy_json_get_int(json
, "index", &index
);
1495 data
= g_data_menu_tip
+ index
;
1497 vtoy_json_get_int(json
, "type", &type
);
1499 path
= VTOY_JSON_STR_EX("path");
1500 tip
= VTOY_JSON_STR_EX("tip");
1503 if (ventoy_is_real_exist_common(path
, data
->list
, data_tip_node
))
1505 ventoy_json_result(conn
, VTOY_JSON_DUPLICATE
);
1509 node
= zalloc(sizeof(data_tip_node
));
1513 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
1514 scnprintf(node
->tip
, sizeof(node
->tip
), "%s", tip
);
1516 vtoy_list_add(data
->list
, cur
, node
);
1520 ret
= ventoy_data_save_all();
1522 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1526 static int ventoy_api_tip_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
1530 const char *path
= NULL
;
1531 data_tip_node
*last
= NULL
;
1532 data_tip_node
*node
= NULL
;
1533 data_tip
*data
= NULL
;
1535 vtoy_json_get_int(json
, "index", &index
);
1536 data
= g_data_menu_tip
+ index
;
1538 path
= VTOY_JSON_STR_EX("path");
1541 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
1543 vtoy_list_free(data_tip_node
, data
->list
);
1547 vtoy_list_del(last
, node
, data
->list
, path
);
1551 ret
= ventoy_data_save_all();
1553 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1560 void ventoy_data_default_menu_class(data_class
*data
)
1562 memset(data
, 0, sizeof(data_class
));
1565 int ventoy_data_cmp_menu_class(data_class
*data1
, data_class
*data2
)
1567 data_class_node
*list1
= NULL
;
1568 data_class_node
*list2
= NULL
;
1570 if (NULL
== data1
->list
&& NULL
== data2
->list
)
1574 else if (data1
->list
&& data2
->list
)
1576 list1
= data1
->list
;
1577 list2
= data2
->list
;
1579 while (list1
&& list2
)
1581 if ((list1
->type
!= list2
->type
) ||
1582 strcmp(list1
->path
, list2
->path
) ||
1583 strcmp(list1
->class, list2
->class))
1588 list1
= list1
->next
;
1589 list2
= list2
->next
;
1592 if (list1
== NULL
&& list2
== NULL
)
1607 int ventoy_data_save_menu_class(data_class
*data
, const char *title
, char *buf
, int buflen
)
1610 data_class_node
*node
= NULL
;
1612 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1614 VTOY_JSON_FMT_KEY_L(L1
, title
);
1615 VTOY_JSON_FMT_ARY_BEGIN_N();
1617 for (node
= data
->list
; node
; node
= node
->next
)
1619 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
1621 if (node
->type
== class_type_key
)
1623 VTOY_JSON_FMT_STRN_LN(L3
, "key", node
->path
);
1625 else if (node
->type
== class_type_dir
)
1627 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "dir", node
->path
);
1631 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "parent", node
->path
);
1633 VTOY_JSON_FMT_STRN_LN(L3
, "class", node
->class);
1635 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
1638 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
1639 VTOY_JSON_FMT_END(pos
);
1645 int ventoy_data_json_menu_class(data_class
*data
, char *buf
, int buflen
)
1649 data_class_node
*node
= NULL
;
1651 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1652 VTOY_JSON_FMT_ARY_BEGIN();
1654 for (node
= data
->list
; node
; node
= node
->next
)
1656 VTOY_JSON_FMT_OBJ_BEGIN();
1658 VTOY_JSON_FMT_UINT("type", node
->type
);
1659 VTOY_JSON_FMT_STRN("path", node
->path
);
1661 if (node
->type
== class_type_key
)
1667 valid
= ventoy_is_directory_exist("%s%s", g_cur_dir
, node
->path
);
1669 VTOY_JSON_FMT_SINT("valid", valid
);
1671 VTOY_JSON_FMT_STRN("class", node
->class);
1673 VTOY_JSON_FMT_OBJ_ENDEX();
1676 VTOY_JSON_FMT_ARY_END();
1677 VTOY_JSON_FMT_END(pos
);
1683 static int ventoy_api_get_class(struct mg_connection
*conn
, VTOY_JSON
*json
)
1685 api_get_func(conn
, json
, menu_class
);
1689 static int ventoy_api_save_class(struct mg_connection
*conn
, VTOY_JSON
*json
)
1692 ret
= ventoy_data_save_all();
1694 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1698 static int ventoy_api_class_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
1702 int type
= class_type_key
;
1703 const char *path
= NULL
;
1704 const char *class = NULL
;
1705 data_class_node
*node
= NULL
;
1706 data_class_node
*cur
= NULL
;
1707 data_class
*data
= NULL
;
1709 vtoy_json_get_int(json
, "index", &index
);
1710 data
= g_data_menu_class
+ index
;
1712 vtoy_json_get_int(json
, "type", &type
);
1714 path
= VTOY_JSON_STR_EX("path");
1715 class = VTOY_JSON_STR_EX("class");
1718 node
= zalloc(sizeof(data_class_node
));
1723 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
1724 scnprintf(node
->class, sizeof(node
->class), "%s", class);
1726 vtoy_list_add(data
->list
, cur
, node
);
1730 ret
= ventoy_data_save_all();
1732 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1736 static int ventoy_api_class_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
1740 const char *path
= NULL
;
1741 data_class_node
*last
= NULL
;
1742 data_class_node
*node
= NULL
;
1743 data_class
*data
= NULL
;
1745 vtoy_json_get_int(json
, "index", &index
);
1746 data
= g_data_menu_class
+ index
;
1748 path
= VTOY_JSON_STR_EX("path");
1751 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
1753 vtoy_list_free(data_class_node
, data
->list
);
1757 vtoy_list_del(last
, node
, data
->list
, path
);
1761 ret
= ventoy_data_save_all();
1763 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1770 void ventoy_data_default_auto_memdisk(data_auto_memdisk
*data
)
1772 memset(data
, 0, sizeof(data_auto_memdisk
));
1775 int ventoy_data_cmp_auto_memdisk(data_auto_memdisk
*data1
, data_auto_memdisk
*data2
)
1777 return ventoy_path_list_cmp(data1
->list
, data2
->list
);
1780 int ventoy_data_save_auto_memdisk(data_auto_memdisk
*data
, const char *title
, char *buf
, int buflen
)
1783 path_node
*node
= NULL
;
1785 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1787 VTOY_JSON_FMT_KEY_L(L1
, title
);
1788 VTOY_JSON_FMT_ARY_BEGIN_N();
1790 for (node
= data
->list
; node
; node
= node
->next
)
1792 VTOY_JSON_FMT_ITEM_PATH_LN(L2
, node
->path
);
1795 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
1796 VTOY_JSON_FMT_END(pos
);
1801 int ventoy_data_json_auto_memdisk(data_auto_memdisk
*data
, char *buf
, int buflen
)
1805 path_node
*node
= NULL
;
1807 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1808 VTOY_JSON_FMT_ARY_BEGIN();
1810 for (node
= data
->list
; node
; node
= node
->next
)
1812 VTOY_JSON_FMT_OBJ_BEGIN();
1814 VTOY_JSON_FMT_STRN("path", node
->path
);
1815 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
1816 VTOY_JSON_FMT_SINT("valid", valid
);
1818 VTOY_JSON_FMT_OBJ_ENDEX();
1821 VTOY_JSON_FMT_ARY_END();
1822 VTOY_JSON_FMT_END(pos
);
1827 static int ventoy_api_get_auto_memdisk(struct mg_connection
*conn
, VTOY_JSON
*json
)
1829 api_get_func(conn
, json
, auto_memdisk
);
1833 static int ventoy_api_save_auto_memdisk(struct mg_connection
*conn
, VTOY_JSON
*json
)
1837 ret
= ventoy_data_save_all();
1839 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1843 static int ventoy_api_auto_memdisk_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
1847 const char *path
= NULL
;
1848 path_node
*node
= NULL
;
1849 path_node
*cur
= NULL
;
1850 data_auto_memdisk
*data
= NULL
;
1852 vtoy_json_get_int(json
, "index", &index
);
1853 data
= g_data_auto_memdisk
+ index
;
1855 path
= VTOY_JSON_STR_EX("path");
1858 if (ventoy_is_real_exist_common(path
, data
->list
, path_node
))
1860 ventoy_json_result(conn
, VTOY_JSON_DUPLICATE
);
1864 node
= zalloc(sizeof(path_node
));
1867 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
1868 vtoy_list_add(data
->list
, cur
, node
);
1872 ret
= ventoy_data_save_all();
1874 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1878 static int ventoy_api_auto_memdisk_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
1882 const char *path
= NULL
;
1883 path_node
*last
= NULL
;
1884 path_node
*node
= NULL
;
1885 data_auto_memdisk
*data
= NULL
;
1887 vtoy_json_get_int(json
, "index", &index
);
1888 data
= g_data_auto_memdisk
+ index
;
1890 path
= VTOY_JSON_STR_EX("path");
1893 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
1895 vtoy_list_free(path_node
, data
->list
);
1899 vtoy_list_del(last
, node
, data
->list
, path
);
1903 ret
= ventoy_data_save_all();
1905 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1912 void ventoy_data_default_image_list(data_image_list
*data
)
1914 memset(data
, 0, sizeof(data_image_list
));
1917 int ventoy_data_cmp_image_list(data_image_list
*data1
, data_image_list
*data2
)
1919 if (data1
->type
!= data2
->type
)
1921 if (data1
->list
|| data2
->list
)
1931 return ventoy_path_list_cmp(data1
->list
, data2
->list
);
1934 int ventoy_data_save_image_list(data_image_list
*data
, const char *title
, char *buf
, int buflen
)
1938 path_node
*node
= NULL
;
1948 prelen
= (int)strlen("image_list");
1950 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1952 if (data
->type
== 0)
1954 scnprintf(newtitle
, sizeof(newtitle
), "image_list%s", title
+ prelen
);
1958 scnprintf(newtitle
, sizeof(newtitle
), "image_blacklist%s", title
+ prelen
);
1960 VTOY_JSON_FMT_KEY_L(L1
, newtitle
);
1962 VTOY_JSON_FMT_ARY_BEGIN_N();
1964 for (node
= data
->list
; node
; node
= node
->next
)
1966 VTOY_JSON_FMT_ITEM_PATH_LN(L2
, node
->path
);
1969 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
1970 VTOY_JSON_FMT_END(pos
);
1975 int ventoy_data_json_image_list(data_image_list
*data
, char *buf
, int buflen
)
1979 path_node
*node
= NULL
;
1981 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1983 VTOY_JSON_FMT_OBJ_BEGIN();
1984 VTOY_JSON_FMT_SINT("type", data
->type
);
1986 VTOY_JSON_FMT_KEY("list");
1987 VTOY_JSON_FMT_ARY_BEGIN();
1989 for (node
= data
->list
; node
; node
= node
->next
)
1991 VTOY_JSON_FMT_OBJ_BEGIN();
1993 VTOY_JSON_FMT_STRN("path", node
->path
);
1994 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
1995 VTOY_JSON_FMT_SINT("valid", valid
);
1997 VTOY_JSON_FMT_OBJ_ENDEX();
2000 VTOY_JSON_FMT_ARY_ENDEX();
2001 VTOY_JSON_FMT_OBJ_END();
2003 VTOY_JSON_FMT_END(pos
);
2008 static int ventoy_api_get_image_list(struct mg_connection
*conn
, VTOY_JSON
*json
)
2010 api_get_func(conn
, json
, image_list
);
2014 static int ventoy_api_save_image_list(struct mg_connection
*conn
, VTOY_JSON
*json
)
2018 data_image_list
*data
= NULL
;
2020 vtoy_json_get_int(json
, "index", &index
);
2021 data
= g_data_image_list
+ index
;
2023 VTOY_JSON_INT("type", data
->type
);
2025 ret
= ventoy_data_save_all();
2027 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2031 static int ventoy_api_image_list_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
2035 const char *path
= NULL
;
2036 path_node
*node
= NULL
;
2037 path_node
*cur
= NULL
;
2038 data_image_list
*data
= NULL
;
2040 vtoy_json_get_int(json
, "index", &index
);
2041 data
= g_data_image_list
+ index
;
2043 path
= VTOY_JSON_STR_EX("path");
2046 if (ventoy_is_real_exist_common(path
, data
->list
, path_node
))
2048 ventoy_json_result(conn
, VTOY_JSON_DUPLICATE
);
2052 node
= zalloc(sizeof(path_node
));
2055 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
2056 vtoy_list_add(data
->list
, cur
, node
);
2060 ret
= ventoy_data_save_all();
2062 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2066 static int ventoy_api_image_list_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
2070 const char *path
= NULL
;
2071 path_node
*last
= NULL
;
2072 path_node
*node
= NULL
;
2073 data_image_list
*data
= NULL
;
2075 vtoy_json_get_int(json
, "index", &index
);
2076 data
= g_data_image_list
+ index
;
2078 path
= VTOY_JSON_STR_EX("path");
2081 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
2083 vtoy_list_free(path_node
, data
->list
);
2087 vtoy_list_del(last
, node
, data
->list
, path
);
2091 ret
= ventoy_data_save_all();
2093 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2100 void ventoy_data_default_password(data_password
*data
)
2102 memset(data
, 0, sizeof(data_password
));
2105 int ventoy_data_cmp_password(data_password
*data1
, data_password
*data2
)
2107 menu_password
*list1
= NULL
;
2108 menu_password
*list2
= NULL
;
2110 if (strcmp(data1
->bootpwd
, data2
->bootpwd
) ||
2111 strcmp(data1
->isopwd
, data2
->isopwd
) ||
2112 strcmp(data1
->wimpwd
, data2
->wimpwd
) ||
2113 strcmp(data1
->vhdpwd
, data2
->vhdpwd
) ||
2114 strcmp(data1
->imgpwd
, data2
->imgpwd
) ||
2115 strcmp(data1
->efipwd
, data2
->efipwd
) ||
2116 strcmp(data1
->vtoypwd
, data2
->vtoypwd
)
2122 if (NULL
== data1
->list
&& NULL
== data2
->list
)
2126 else if (data1
->list
&& data2
->list
)
2128 list1
= data1
->list
;
2129 list2
= data2
->list
;
2131 while (list1
&& list2
)
2133 if ((list1
->type
!= list2
->type
) || strcmp(list1
->path
, list2
->path
))
2138 list1
= list1
->next
;
2139 list2
= list2
->next
;
2142 if (list1
== NULL
&& list2
== NULL
)
2157 int ventoy_data_save_password(data_password
*data
, const char *title
, char *buf
, int buflen
)
2160 menu_password
*node
= NULL
;
2161 data_password
*def
= g_data_password
+ bios_max
;
2163 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2164 VTOY_JSON_FMT_KEY_L(L1
, title
);
2165 VTOY_JSON_FMT_OBJ_BEGIN_N();
2167 VTOY_JSON_FMT_DIFF_STRN(L2
, "bootpwd", bootpwd
);
2168 VTOY_JSON_FMT_DIFF_STRN(L2
, "isopwd", isopwd
);
2169 VTOY_JSON_FMT_DIFF_STRN(L2
, "wimpwd", wimpwd
);
2170 VTOY_JSON_FMT_DIFF_STRN(L2
, "vhdpwd", vhdpwd
);
2171 VTOY_JSON_FMT_DIFF_STRN(L2
, "imgpwd", imgpwd
);
2172 VTOY_JSON_FMT_DIFF_STRN(L2
, "efipwd", efipwd
);
2173 VTOY_JSON_FMT_DIFF_STRN(L2
, "vtoypwd", vtoypwd
);
2177 VTOY_JSON_FMT_KEY_L(L2
, "menupwd");
2178 VTOY_JSON_FMT_ARY_BEGIN_N();
2180 for (node
= data
->list
; node
; node
= node
->next
)
2182 VTOY_JSON_FMT_OBJ_BEGIN_LN(L3
);
2184 if (node
->type
== 0)
2186 VTOY_JSON_FMT_STRN_PATH_LN(L4
, "file", node
->path
);
2190 VTOY_JSON_FMT_STRN_PATH_LN(L4
, "parent", node
->path
);
2192 VTOY_JSON_FMT_STRN_LN(L4
, "pwd", node
->pwd
);
2194 VTOY_JSON_FMT_OBJ_ENDEX_LN(L3
);
2197 VTOY_JSON_FMT_ARY_ENDEX_LN(L2
);
2200 VTOY_JSON_FMT_OBJ_ENDEX_LN(L1
);
2202 VTOY_JSON_FMT_END(pos
);
2208 int ventoy_data_json_password(data_password
*data
, char *buf
, int buflen
)
2212 menu_password
*node
= NULL
;
2214 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2216 VTOY_JSON_FMT_OBJ_BEGIN();
2218 VTOY_JSON_FMT_STRN("bootpwd", data
->bootpwd
);
2219 VTOY_JSON_FMT_STRN("isopwd", data
->isopwd
);
2220 VTOY_JSON_FMT_STRN("wimpwd", data
->wimpwd
);
2221 VTOY_JSON_FMT_STRN("vhdpwd", data
->vhdpwd
);
2222 VTOY_JSON_FMT_STRN("imgpwd", data
->imgpwd
);
2223 VTOY_JSON_FMT_STRN("efipwd", data
->efipwd
);
2224 VTOY_JSON_FMT_STRN("vtoypwd", data
->vtoypwd
);
2226 VTOY_JSON_FMT_KEY("list");
2227 VTOY_JSON_FMT_ARY_BEGIN();
2229 for (node
= data
->list
; node
; node
= node
->next
)
2231 VTOY_JSON_FMT_OBJ_BEGIN();
2233 VTOY_JSON_FMT_SINT("type", node
->type
);
2234 VTOY_JSON_FMT_STRN("path", node
->path
);
2235 if (node
->type
== path_type_file
)
2237 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
2241 valid
= ventoy_is_directory_exist("%s%s", g_cur_dir
, node
->path
);
2244 VTOY_JSON_FMT_SINT("valid", valid
);
2245 VTOY_JSON_FMT_STRN("pwd", node
->pwd
);
2247 VTOY_JSON_FMT_OBJ_ENDEX();
2250 VTOY_JSON_FMT_ARY_ENDEX();
2252 VTOY_JSON_FMT_OBJ_END();
2253 VTOY_JSON_FMT_END(pos
);
2258 static int ventoy_api_get_password(struct mg_connection
*conn
, VTOY_JSON
*json
)
2260 api_get_func(conn
, json
, password
);
2264 static int ventoy_api_save_password(struct mg_connection
*conn
, VTOY_JSON
*json
)
2268 data_password
*data
= NULL
;
2270 vtoy_json_get_int(json
, "index", &index
);
2271 data
= g_data_password
+ index
;
2273 VTOY_JSON_STR("bootpwd", data
->bootpwd
);
2274 VTOY_JSON_STR("isopwd", data
->isopwd
);
2275 VTOY_JSON_STR("wimpwd", data
->wimpwd
);
2276 VTOY_JSON_STR("vhdpwd", data
->vhdpwd
);
2277 VTOY_JSON_STR("imgpwd", data
->imgpwd
);
2278 VTOY_JSON_STR("efipwd", data
->efipwd
);
2279 VTOY_JSON_STR("vtoypwd", data
->vtoypwd
);
2281 ret
= ventoy_data_save_all();
2283 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2287 static int ventoy_api_password_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
2292 const char *path
= NULL
;
2293 const char *pwd
= NULL
;
2294 menu_password
*node
= NULL
;
2295 menu_password
*cur
= NULL
;
2296 data_password
*data
= NULL
;
2298 vtoy_json_get_int(json
, "index", &index
);
2299 data
= g_data_password
+ index
;
2301 vtoy_json_get_int(json
, "type", &type
);
2303 path
= VTOY_JSON_STR_EX("path");
2304 pwd
= VTOY_JSON_STR_EX("pwd");
2307 if (ventoy_is_real_exist_common(path
, data
->list
, menu_password
))
2309 ventoy_json_result(conn
, VTOY_JSON_DUPLICATE
);
2313 node
= zalloc(sizeof(menu_password
));
2317 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
2318 scnprintf(node
->pwd
, sizeof(node
->pwd
), "%s", pwd
);
2320 vtoy_list_add(data
->list
, cur
, node
);
2324 ret
= ventoy_data_save_all();
2326 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2330 static int ventoy_api_password_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
2334 const char *path
= NULL
;
2335 menu_password
*last
= NULL
;
2336 menu_password
*node
= NULL
;
2337 data_password
*data
= NULL
;
2339 vtoy_json_get_int(json
, "index", &index
);
2340 data
= g_data_password
+ index
;
2342 path
= VTOY_JSON_STR_EX("path");
2345 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
2347 vtoy_list_free(menu_password
, data
->list
);
2351 vtoy_list_del(last
, node
, data
->list
, path
);
2355 ret
= ventoy_data_save_all();
2357 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2364 void ventoy_data_default_conf_replace(data_conf_replace
*data
)
2366 memset(data
, 0, sizeof(data_conf_replace
));
2369 int ventoy_data_cmp_conf_replace(data_conf_replace
*data1
, data_conf_replace
*data2
)
2371 conf_replace_node
*list1
= NULL
;
2372 conf_replace_node
*list2
= NULL
;
2374 if (NULL
== data1
->list
&& NULL
== data2
->list
)
2378 else if (data1
->list
&& data2
->list
)
2380 list1
= data1
->list
;
2381 list2
= data2
->list
;
2383 while (list1
&& list2
)
2385 if (list1
->image
!= list2
->image
||
2386 strcmp(list1
->path
, list2
->path
) ||
2387 strcmp(list1
->org
, list2
->org
) ||
2388 strcmp(list1
->new, list2
->new)
2394 list1
= list1
->next
;
2395 list2
= list2
->next
;
2398 if (list1
== NULL
&& list2
== NULL
)
2413 int ventoy_data_save_conf_replace(data_conf_replace
*data
, const char *title
, char *buf
, int buflen
)
2416 conf_replace_node
*node
= NULL
;
2418 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2420 VTOY_JSON_FMT_KEY_L(L1
, title
);
2421 VTOY_JSON_FMT_ARY_BEGIN_N();
2423 for (node
= data
->list
; node
; node
= node
->next
)
2425 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
2427 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "iso", node
->path
);
2428 VTOY_JSON_FMT_STRN_LN(L3
, "org", node
->org
);
2429 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "new", node
->new);
2432 VTOY_JSON_FMT_SINT_LN(L3
, "img", node
->image
);
2435 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
2438 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
2439 VTOY_JSON_FMT_END(pos
);
2445 int ventoy_data_json_conf_replace(data_conf_replace
*data
, char *buf
, int buflen
)
2448 conf_replace_node
*node
= NULL
;
2450 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2451 VTOY_JSON_FMT_ARY_BEGIN();
2453 for (node
= data
->list
; node
; node
= node
->next
)
2455 VTOY_JSON_FMT_OBJ_BEGIN();
2457 VTOY_JSON_FMT_STRN("path", node
->path
);
2458 VTOY_JSON_FMT_SINT("valid", ventoy_check_fuzzy_path(node
->path
, 1));
2459 VTOY_JSON_FMT_STRN("org", node
->org
);
2460 VTOY_JSON_FMT_STRN("new", node
->new);
2461 VTOY_JSON_FMT_SINT("new_valid", ventoy_is_file_exist("%s%s", g_cur_dir
, node
->new));
2462 VTOY_JSON_FMT_SINT("img", node
->image
);
2464 VTOY_JSON_FMT_OBJ_ENDEX();
2467 VTOY_JSON_FMT_ARY_END();
2468 VTOY_JSON_FMT_END(pos
);
2473 static int ventoy_api_get_conf_replace(struct mg_connection
*conn
, VTOY_JSON
*json
)
2475 api_get_func(conn
, json
, conf_replace
);
2479 static int ventoy_api_save_conf_replace(struct mg_connection
*conn
, VTOY_JSON
*json
)
2482 ret
= ventoy_data_save_all();
2484 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2488 static int ventoy_api_conf_replace_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
2493 const char *path
= NULL
;
2494 const char *org
= NULL
;
2495 const char *new = NULL
;
2496 conf_replace_node
*node
= NULL
;
2497 conf_replace_node
*cur
= NULL
;
2498 data_conf_replace
*data
= NULL
;
2500 vtoy_json_get_int(json
, "img", &image
);
2502 vtoy_json_get_int(json
, "index", &index
);
2503 data
= g_data_conf_replace
+ index
;
2505 path
= VTOY_JSON_STR_EX("path");
2506 org
= VTOY_JSON_STR_EX("org");
2507 new = VTOY_JSON_STR_EX("new");
2508 if (path
&& org
&& new)
2510 node
= zalloc(sizeof(conf_replace_node
));
2513 node
->image
= image
;
2514 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
2515 scnprintf(node
->org
, sizeof(node
->org
), "%s", org
);
2516 scnprintf(node
->new, sizeof(node
->new), "%s", new);
2518 vtoy_list_add(data
->list
, cur
, node
);
2522 ret
= ventoy_data_save_all();
2524 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2528 static int ventoy_api_conf_replace_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
2532 const char *path
= NULL
;
2533 conf_replace_node
*last
= NULL
;
2534 conf_replace_node
*node
= NULL
;
2535 data_conf_replace
*data
= NULL
;
2537 vtoy_json_get_int(json
, "index", &index
);
2538 data
= g_data_conf_replace
+ index
;
2540 path
= VTOY_JSON_STR_EX("path");
2543 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
2545 vtoy_list_free(conf_replace_node
, data
->list
);
2549 vtoy_list_del(last
, node
, data
->list
, path
);
2553 ret
= ventoy_data_save_all();
2555 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2563 void ventoy_data_default_dud(data_dud
*data
)
2565 memset(data
, 0, sizeof(data_dud
));
2568 int ventoy_data_cmp_dud(data_dud
*data1
, data_dud
*data2
)
2570 dud_node
*list1
= NULL
;
2571 dud_node
*list2
= NULL
;
2573 if (NULL
== data1
->list
&& NULL
== data2
->list
)
2577 else if (data1
->list
&& data2
->list
)
2579 list1
= data1
->list
;
2580 list2
= data2
->list
;
2582 while (list1
&& list2
)
2584 if (strcmp(list1
->path
, list2
->path
))
2589 /* no need to compare dud list with default */
2590 list1
= list1
->next
;
2591 list2
= list2
->next
;
2594 if (list1
== NULL
&& list2
== NULL
)
2609 int ventoy_data_save_dud(data_dud
*data
, const char *title
, char *buf
, int buflen
)
2612 dud_node
*node
= NULL
;
2613 path_node
*pathnode
= NULL
;
2615 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2617 VTOY_JSON_FMT_KEY_L(L1
, title
);
2618 VTOY_JSON_FMT_ARY_BEGIN_N();
2620 for (node
= data
->list
; node
; node
= node
->next
)
2622 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
2623 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "image", node
->path
);
2625 VTOY_JSON_FMT_KEY_L(L3
, "dud");
2626 VTOY_JSON_FMT_ARY_BEGIN_N();
2627 for (pathnode
= node
->list
; pathnode
; pathnode
= pathnode
->next
)
2629 VTOY_JSON_FMT_ITEM_PATH_LN(L4
, pathnode
->path
);
2631 VTOY_JSON_FMT_ARY_ENDEX_LN(L3
);
2633 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
2636 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
2637 VTOY_JSON_FMT_END(pos
);
2643 int ventoy_data_json_dud(data_dud
*data
, char *buf
, int buflen
)
2647 dud_node
*node
= NULL
;
2648 path_node
*pathnode
= NULL
;
2650 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2651 VTOY_JSON_FMT_ARY_BEGIN();
2653 for (node
= data
->list
; node
; node
= node
->next
)
2655 VTOY_JSON_FMT_OBJ_BEGIN();
2657 VTOY_JSON_FMT_STRN("path", node
->path
);
2658 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
2659 VTOY_JSON_FMT_SINT("valid", valid
);
2662 VTOY_JSON_FMT_KEY("list");
2663 VTOY_JSON_FMT_ARY_BEGIN();
2664 for (pathnode
= node
->list
; pathnode
; pathnode
= pathnode
->next
)
2666 VTOY_JSON_FMT_OBJ_BEGIN();
2667 VTOY_JSON_FMT_STRN("path", pathnode
->path
);
2669 valid
= ventoy_is_file_exist("%s%s", g_cur_dir
, pathnode
->path
);
2670 VTOY_JSON_FMT_SINT("valid", valid
);
2671 VTOY_JSON_FMT_OBJ_ENDEX();
2673 VTOY_JSON_FMT_ARY_ENDEX();
2676 VTOY_JSON_FMT_OBJ_ENDEX();
2679 VTOY_JSON_FMT_ARY_END();
2680 VTOY_JSON_FMT_END(pos
);
2685 static int ventoy_api_get_dud(struct mg_connection
*conn
, VTOY_JSON
*json
)
2687 api_get_func(conn
, json
, dud
);
2691 static int ventoy_api_save_dud(struct mg_connection
*conn
, VTOY_JSON
*json
)
2694 ret
= ventoy_data_save_all();
2696 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2701 static int ventoy_api_dud_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
2705 const char *path
= NULL
;
2706 dud_node
*node
= NULL
;
2707 dud_node
*cur
= NULL
;
2708 data_dud
*data
= NULL
;
2709 VTOY_JSON
*array
= NULL
;
2711 vtoy_json_get_int(json
, "index", &index
);
2712 data
= g_data_dud
+ index
;
2714 array
= vtoy_json_find_item(json
, JSON_TYPE_ARRAY
, "dud");
2715 path
= VTOY_JSON_STR_EX("path");
2718 if (ventoy_is_real_exist_common(path
, data
->list
, dud_node
))
2720 ventoy_json_result(conn
, VTOY_JSON_DUPLICATE
);
2724 node
= zalloc(sizeof(dud_node
));
2727 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
2728 node
->list
= ventoy_path_node_add_array(array
);
2730 vtoy_list_add(data
->list
, cur
, node
);
2734 ret
= ventoy_data_save_all();
2736 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2740 static int ventoy_api_dud_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
2744 const char *path
= NULL
;
2745 dud_node
*next
= NULL
;
2746 dud_node
*last
= NULL
;
2747 dud_node
*node
= NULL
;
2748 data_dud
*data
= NULL
;
2750 vtoy_json_get_int(json
, "index", &index
);
2751 data
= g_data_dud
+ index
;
2753 path
= VTOY_JSON_STR_EX("path");
2756 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
2758 for (node
= data
->list
; node
; node
= next
)
2761 ventoy_free_path_node_list(node
->list
);
2768 vtoy_list_del_ex(last
, node
, data
->list
, path
, ventoy_free_path_node_list
);
2772 ret
= ventoy_data_save_all();
2774 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2779 static int ventoy_api_dud_add_inner(struct mg_connection
*conn
, VTOY_JSON
*json
)
2783 const char *path
= NULL
;
2784 const char *outpath
= NULL
;
2785 path_node
*pcur
= NULL
;
2786 path_node
*pnode
= NULL
;
2787 dud_node
*node
= NULL
;
2788 data_dud
*data
= NULL
;
2790 vtoy_json_get_int(json
, "index", &index
);
2791 data
= g_data_dud
+ index
;
2793 path
= VTOY_JSON_STR_EX("path");
2794 outpath
= VTOY_JSON_STR_EX("outpath");
2795 if (path
&& outpath
)
2797 for (node
= data
->list
; node
; node
= node
->next
)
2799 if (strcmp(outpath
, node
->path
) == 0)
2801 pnode
= zalloc(sizeof(path_node
));
2804 scnprintf(pnode
->path
, sizeof(pnode
->path
), "%s", path
);
2805 vtoy_list_add(node
->list
, pcur
, pnode
);
2813 ret
= ventoy_data_save_all();
2815 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2819 static int ventoy_api_dud_del_inner(struct mg_connection
*conn
, VTOY_JSON
*json
)
2823 const char *path
= NULL
;
2824 const char *outpath
= NULL
;
2825 path_node
*plast
= NULL
;
2826 path_node
*pnode
= NULL
;
2827 dud_node
*node
= NULL
;
2828 data_dud
*data
= NULL
;
2830 vtoy_json_get_int(json
, "index", &index
);
2831 data
= g_data_dud
+ index
;
2833 path
= VTOY_JSON_STR_EX("path");
2834 outpath
= VTOY_JSON_STR_EX("outpath");
2835 if (path
&& outpath
)
2837 for (node
= data
->list
; node
; node
= node
->next
)
2839 if (strcmp(outpath
, node
->path
) == 0)
2841 vtoy_list_del(plast
, pnode
, node
->list
, path
);
2847 ret
= ventoy_data_save_all();
2849 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2857 void ventoy_data_default_auto_install(data_auto_install
*data
)
2859 memset(data
, 0, sizeof(data_auto_install
));
2862 int ventoy_data_cmp_auto_install(data_auto_install
*data1
, data_auto_install
*data2
)
2864 auto_install_node
*list1
= NULL
;
2865 auto_install_node
*list2
= NULL
;
2867 if (NULL
== data1
->list
&& NULL
== data2
->list
)
2871 else if (data1
->list
&& data2
->list
)
2873 list1
= data1
->list
;
2874 list2
= data2
->list
;
2876 while (list1
&& list2
)
2878 if (list1
->timeout
!= list2
->timeout
||
2879 list1
->autosel
!= list2
->autosel
||
2880 strcmp(list1
->path
, list2
->path
))
2885 /* no need to compare auto install list with default */
2886 list1
= list1
->next
;
2887 list2
= list2
->next
;
2890 if (list1
== NULL
&& list2
== NULL
)
2905 int ventoy_data_save_auto_install(data_auto_install
*data
, const char *title
, char *buf
, int buflen
)
2908 auto_install_node
*node
= NULL
;
2909 path_node
*pathnode
= NULL
;
2911 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2913 VTOY_JSON_FMT_KEY_L(L1
, title
);
2914 VTOY_JSON_FMT_ARY_BEGIN_N();
2916 for (node
= data
->list
; node
; node
= node
->next
)
2918 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
2919 if (node
->type
== 0)
2921 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "image", node
->path
);
2925 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "parent", node
->path
);
2929 VTOY_JSON_FMT_KEY_L(L3
, "template");
2930 VTOY_JSON_FMT_ARY_BEGIN_N();
2931 for (pathnode
= node
->list
; pathnode
; pathnode
= pathnode
->next
)
2933 VTOY_JSON_FMT_ITEM_PATH_LN(L4
, pathnode
->path
);
2935 VTOY_JSON_FMT_ARY_ENDEX_LN(L3
);
2937 if (node
->timeouten
)
2939 VTOY_JSON_FMT_SINT_LN(L3
, "timeout", node
->timeout
);
2942 if (node
->autoselen
)
2944 VTOY_JSON_FMT_SINT_LN(L3
, "autosel", node
->autosel
);
2947 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
2950 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
2951 VTOY_JSON_FMT_END(pos
);
2957 int ventoy_data_json_auto_install(data_auto_install
*data
, char *buf
, int buflen
)
2961 auto_install_node
*node
= NULL
;
2962 path_node
*pathnode
= NULL
;
2964 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2965 VTOY_JSON_FMT_ARY_BEGIN();
2967 for (node
= data
->list
; node
; node
= node
->next
)
2969 VTOY_JSON_FMT_OBJ_BEGIN();
2971 VTOY_JSON_FMT_STRN("path", node
->path
);
2973 if (node
->type
== 0)
2975 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
2979 valid
= ventoy_is_directory_exist("%s%s", g_cur_dir
, node
->path
);
2981 VTOY_JSON_FMT_SINT("valid", valid
);
2982 VTOY_JSON_FMT_SINT("type", node
->type
);
2984 VTOY_JSON_FMT_BOOL("timeouten", node
->timeouten
);
2985 VTOY_JSON_FMT_BOOL("autoselen", node
->autoselen
);
2987 VTOY_JSON_FMT_SINT("autosel", node
->autosel
);
2988 VTOY_JSON_FMT_SINT("timeout", node
->timeout
);
2990 VTOY_JSON_FMT_KEY("list");
2991 VTOY_JSON_FMT_ARY_BEGIN();
2992 for (pathnode
= node
->list
; pathnode
; pathnode
= pathnode
->next
)
2994 VTOY_JSON_FMT_OBJ_BEGIN();
2995 VTOY_JSON_FMT_STRN("path", pathnode
->path
);
2997 valid
= ventoy_is_file_exist("%s%s", g_cur_dir
, pathnode
->path
);
2998 VTOY_JSON_FMT_SINT("valid", valid
);
2999 VTOY_JSON_FMT_OBJ_ENDEX();
3001 VTOY_JSON_FMT_ARY_ENDEX();
3004 VTOY_JSON_FMT_OBJ_ENDEX();
3007 VTOY_JSON_FMT_ARY_END();
3008 VTOY_JSON_FMT_END(pos
);
3013 static int ventoy_api_get_auto_install(struct mg_connection
*conn
, VTOY_JSON
*json
)
3015 api_get_func(conn
, json
, auto_install
);
3019 static int ventoy_api_save_auto_install(struct mg_connection
*conn
, VTOY_JSON
*json
)
3025 uint8_t timeouten
= 0;
3026 uint8_t autoselen
= 0;
3027 auto_install_node
*node
= NULL
;
3028 data_auto_install
*data
= NULL
;
3030 vtoy_json_get_int(json
, "index", &index
);
3031 vtoy_json_get_int(json
, "id", &id
);
3033 vtoy_json_get_bool(json
, "timeouten", &timeouten
);
3034 vtoy_json_get_bool(json
, "autoselen", &autoselen
);
3036 data
= g_data_auto_install
+ index
;
3040 for (node
= data
->list
; node
; node
= node
->next
)
3044 node
->timeouten
= (int)timeouten
;
3045 node
->autoselen
= (int)autoselen
;
3046 VTOY_JSON_INT("timeout", node
->timeout
);
3047 VTOY_JSON_INT("autosel", node
->autosel
);
3053 ret
= ventoy_data_save_all();
3055 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3060 static int ventoy_api_auto_install_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
3065 const char *path
= NULL
;
3066 auto_install_node
*node
= NULL
;
3067 auto_install_node
*cur
= NULL
;
3068 data_auto_install
*data
= NULL
;
3069 VTOY_JSON
*array
= NULL
;
3071 vtoy_json_get_int(json
, "type", &type
);
3072 vtoy_json_get_int(json
, "index", &index
);
3073 data
= g_data_auto_install
+ index
;
3075 array
= vtoy_json_find_item(json
, JSON_TYPE_ARRAY
, "template");
3076 path
= VTOY_JSON_STR_EX("path");
3079 if (ventoy_is_real_exist_common(path
, data
->list
, auto_install_node
))
3081 ventoy_json_result(conn
, VTOY_JSON_DUPLICATE
);
3085 node
= zalloc(sizeof(auto_install_node
));
3089 node
->timeouten
= 0;
3090 node
->autoselen
= 0;
3093 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
3094 node
->list
= ventoy_path_node_add_array(array
);
3096 vtoy_list_add(data
->list
, cur
, node
);
3100 ret
= ventoy_data_save_all();
3102 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3106 static int ventoy_api_auto_install_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
3110 const char *path
= NULL
;
3111 auto_install_node
*last
= NULL
;
3112 auto_install_node
*next
= NULL
;
3113 auto_install_node
*node
= NULL
;
3114 data_auto_install
*data
= NULL
;
3116 vtoy_json_get_int(json
, "index", &index
);
3117 data
= g_data_auto_install
+ index
;
3119 path
= VTOY_JSON_STR_EX("path");
3122 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
3124 for (node
= data
->list
; node
; node
= next
)
3127 ventoy_free_path_node_list(node
->list
);
3134 vtoy_list_del_ex(last
, node
, data
->list
, path
, ventoy_free_path_node_list
);
3138 ret
= ventoy_data_save_all();
3140 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3144 static int ventoy_api_auto_install_add_inner(struct mg_connection
*conn
, VTOY_JSON
*json
)
3148 const char *path
= NULL
;
3149 const char *outpath
= NULL
;
3150 path_node
*pcur
= NULL
;
3151 path_node
*pnode
= NULL
;
3152 auto_install_node
*node
= NULL
;
3153 data_auto_install
*data
= NULL
;
3155 vtoy_json_get_int(json
, "index", &index
);
3156 data
= g_data_auto_install
+ index
;
3158 path
= VTOY_JSON_STR_EX("path");
3159 outpath
= VTOY_JSON_STR_EX("outpath");
3160 if (path
&& outpath
)
3162 for (node
= data
->list
; node
; node
= node
->next
)
3164 if (strcmp(outpath
, node
->path
) == 0)
3166 pnode
= zalloc(sizeof(path_node
));
3169 scnprintf(pnode
->path
, sizeof(pnode
->path
), "%s", path
);
3170 vtoy_list_add(node
->list
, pcur
, pnode
);
3178 ret
= ventoy_data_save_all();
3180 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3184 static int ventoy_api_auto_install_del_inner(struct mg_connection
*conn
, VTOY_JSON
*json
)
3188 const char *path
= NULL
;
3189 const char *outpath
= NULL
;
3190 path_node
*plast
= NULL
;
3191 path_node
*pnode
= NULL
;
3192 auto_install_node
*node
= NULL
;
3193 data_auto_install
*data
= NULL
;
3195 vtoy_json_get_int(json
, "index", &index
);
3196 data
= g_data_auto_install
+ index
;
3198 path
= VTOY_JSON_STR_EX("path");
3199 outpath
= VTOY_JSON_STR_EX("outpath");
3200 if (path
&& outpath
)
3202 for (node
= data
->list
; node
; node
= node
->next
)
3204 if (strcmp(outpath
, node
->path
) == 0)
3206 vtoy_list_del(plast
, pnode
, node
->list
, path
);
3212 ret
= ventoy_data_save_all();
3214 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3223 void ventoy_data_default_persistence(data_persistence
*data
)
3225 memset(data
, 0, sizeof(data_persistence
));
3228 int ventoy_data_cmp_persistence(data_persistence
*data1
, data_persistence
*data2
)
3230 persistence_node
*list1
= NULL
;
3231 persistence_node
*list2
= NULL
;
3233 if (NULL
== data1
->list
&& NULL
== data2
->list
)
3237 else if (data1
->list
&& data2
->list
)
3239 list1
= data1
->list
;
3240 list2
= data2
->list
;
3242 while (list1
&& list2
)
3244 if (list1
->timeout
!= list2
->timeout
||
3245 list1
->autosel
!= list2
->autosel
||
3246 strcmp(list1
->path
, list2
->path
))
3251 /* no need to compare auto install list with default */
3252 list1
= list1
->next
;
3253 list2
= list2
->next
;
3256 if (list1
== NULL
&& list2
== NULL
)
3271 int ventoy_data_save_persistence(data_persistence
*data
, const char *title
, char *buf
, int buflen
)
3274 persistence_node
*node
= NULL
;
3275 path_node
*pathnode
= NULL
;
3277 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
3279 VTOY_JSON_FMT_KEY_L(L1
, title
);
3280 VTOY_JSON_FMT_ARY_BEGIN_N();
3282 for (node
= data
->list
; node
; node
= node
->next
)
3284 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
3285 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "image", node
->path
);
3286 VTOY_JSON_FMT_KEY_L(L3
, "backend");
3287 VTOY_JSON_FMT_ARY_BEGIN_N();
3288 for (pathnode
= node
->list
; pathnode
; pathnode
= pathnode
->next
)
3290 VTOY_JSON_FMT_ITEM_PATH_LN(L4
, pathnode
->path
);
3292 VTOY_JSON_FMT_ARY_ENDEX_LN(L3
);
3294 if (node
->timeouten
)
3296 VTOY_JSON_FMT_SINT_LN(L3
, "timeout", node
->timeout
);
3299 if (node
->autoselen
)
3301 VTOY_JSON_FMT_SINT_LN(L3
, "autosel", node
->autosel
);
3304 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
3307 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
3308 VTOY_JSON_FMT_END(pos
);
3314 int ventoy_data_json_persistence(data_persistence
*data
, char *buf
, int buflen
)
3318 persistence_node
*node
= NULL
;
3319 path_node
*pathnode
= NULL
;
3321 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
3322 VTOY_JSON_FMT_ARY_BEGIN();
3324 for (node
= data
->list
; node
; node
= node
->next
)
3326 VTOY_JSON_FMT_OBJ_BEGIN();
3328 VTOY_JSON_FMT_STRN("path", node
->path
);
3330 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
3331 VTOY_JSON_FMT_SINT("valid", valid
);
3332 VTOY_JSON_FMT_SINT("type", node
->type
);
3334 VTOY_JSON_FMT_BOOL("timeouten", node
->timeouten
);
3335 VTOY_JSON_FMT_BOOL("autoselen", node
->autoselen
);
3337 VTOY_JSON_FMT_SINT("autosel", node
->autosel
);
3338 VTOY_JSON_FMT_SINT("timeout", node
->timeout
);
3340 VTOY_JSON_FMT_KEY("list");
3341 VTOY_JSON_FMT_ARY_BEGIN();
3342 for (pathnode
= node
->list
; pathnode
; pathnode
= pathnode
->next
)
3344 VTOY_JSON_FMT_OBJ_BEGIN();
3345 VTOY_JSON_FMT_STRN("path", pathnode
->path
);
3347 valid
= ventoy_is_file_exist("%s%s", g_cur_dir
, pathnode
->path
);
3348 VTOY_JSON_FMT_SINT("valid", valid
);
3349 VTOY_JSON_FMT_OBJ_ENDEX();
3351 VTOY_JSON_FMT_ARY_ENDEX();
3354 VTOY_JSON_FMT_OBJ_ENDEX();
3357 VTOY_JSON_FMT_ARY_END();
3358 VTOY_JSON_FMT_END(pos
);
3363 static int ventoy_api_get_persistence(struct mg_connection
*conn
, VTOY_JSON
*json
)
3365 api_get_func(conn
, json
, persistence
);
3369 static int ventoy_api_save_persistence(struct mg_connection
*conn
, VTOY_JSON
*json
)
3375 uint8_t timeouten
= 0;
3376 uint8_t autoselen
= 0;
3377 persistence_node
*node
= NULL
;
3378 data_persistence
*data
= NULL
;
3380 vtoy_json_get_int(json
, "index", &index
);
3381 vtoy_json_get_int(json
, "id", &id
);
3383 vtoy_json_get_bool(json
, "timeouten", &timeouten
);
3384 vtoy_json_get_bool(json
, "autoselen", &autoselen
);
3386 data
= g_data_persistence
+ index
;
3390 for (node
= data
->list
; node
; node
= node
->next
)
3394 node
->timeouten
= (int)timeouten
;
3395 node
->autoselen
= (int)autoselen
;
3396 VTOY_JSON_INT("timeout", node
->timeout
);
3397 VTOY_JSON_INT("autosel", node
->autosel
);
3403 ret
= ventoy_data_save_all();
3405 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3410 static int ventoy_api_persistence_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
3414 const char *path
= NULL
;
3415 persistence_node
*node
= NULL
;
3416 persistence_node
*cur
= NULL
;
3417 data_persistence
*data
= NULL
;
3418 VTOY_JSON
*array
= NULL
;
3420 vtoy_json_get_int(json
, "index", &index
);
3421 data
= g_data_persistence
+ index
;
3423 array
= vtoy_json_find_item(json
, JSON_TYPE_ARRAY
, "backend");
3424 path
= VTOY_JSON_STR_EX("path");
3427 if (ventoy_is_real_exist_common(path
, data
->list
, persistence_node
))
3429 ventoy_json_result(conn
, VTOY_JSON_DUPLICATE
);
3433 node
= zalloc(sizeof(persistence_node
));
3436 node
->timeouten
= 0;
3437 node
->autoselen
= 0;
3440 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
3441 node
->list
= ventoy_path_node_add_array(array
);
3443 vtoy_list_add(data
->list
, cur
, node
);
3447 ret
= ventoy_data_save_all();
3449 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3453 static int ventoy_api_persistence_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
3457 const char *path
= NULL
;
3458 persistence_node
*last
= NULL
;
3459 persistence_node
*next
= NULL
;
3460 persistence_node
*node
= NULL
;
3461 data_persistence
*data
= NULL
;
3463 vtoy_json_get_int(json
, "index", &index
);
3464 data
= g_data_persistence
+ index
;
3466 path
= VTOY_JSON_STR_EX("path");
3469 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
3471 for (node
= data
->list
; node
; node
= next
)
3474 ventoy_free_path_node_list(node
->list
);
3481 vtoy_list_del_ex(last
, node
, data
->list
, path
, ventoy_free_path_node_list
);
3485 ret
= ventoy_data_save_all();
3487 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3491 static int ventoy_api_persistence_add_inner(struct mg_connection
*conn
, VTOY_JSON
*json
)
3495 const char *path
= NULL
;
3496 const char *outpath
= NULL
;
3497 path_node
*pcur
= NULL
;
3498 path_node
*pnode
= NULL
;
3499 persistence_node
*node
= NULL
;
3500 data_persistence
*data
= NULL
;
3502 vtoy_json_get_int(json
, "index", &index
);
3503 data
= g_data_persistence
+ index
;
3505 path
= VTOY_JSON_STR_EX("path");
3506 outpath
= VTOY_JSON_STR_EX("outpath");
3507 if (path
&& outpath
)
3509 for (node
= data
->list
; node
; node
= node
->next
)
3511 if (strcmp(outpath
, node
->path
) == 0)
3513 pnode
= zalloc(sizeof(path_node
));
3516 scnprintf(pnode
->path
, sizeof(pnode
->path
), "%s", path
);
3517 vtoy_list_add(node
->list
, pcur
, pnode
);
3525 ret
= ventoy_data_save_all();
3527 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3531 static int ventoy_api_persistence_del_inner(struct mg_connection
*conn
, VTOY_JSON
*json
)
3535 const char *path
= NULL
;
3536 const char *outpath
= NULL
;
3537 path_node
*plast
= NULL
;
3538 path_node
*pnode
= NULL
;
3539 persistence_node
*node
= NULL
;
3540 data_persistence
*data
= NULL
;
3542 vtoy_json_get_int(json
, "index", &index
);
3543 data
= g_data_persistence
+ index
;
3545 path
= VTOY_JSON_STR_EX("path");
3546 outpath
= VTOY_JSON_STR_EX("outpath");
3547 if (path
&& outpath
)
3549 for (node
= data
->list
; node
; node
= node
->next
)
3551 if (strcmp(outpath
, node
->path
) == 0)
3553 vtoy_list_del(plast
, pnode
, node
->list
, path
);
3559 ret
= ventoy_data_save_all();
3561 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3569 void ventoy_data_default_injection(data_injection
*data
)
3571 memset(data
, 0, sizeof(data_injection
));
3574 int ventoy_data_cmp_injection(data_injection
*data1
, data_injection
*data2
)
3576 injection_node
*list1
= NULL
;
3577 injection_node
*list2
= NULL
;
3579 if (NULL
== data1
->list
&& NULL
== data2
->list
)
3583 else if (data1
->list
&& data2
->list
)
3585 list1
= data1
->list
;
3586 list2
= data2
->list
;
3588 while (list1
&& list2
)
3590 if ((list1
->type
!= list2
->type
) ||
3591 strcmp(list1
->path
, list2
->path
) ||
3592 strcmp(list1
->archive
, list2
->archive
))
3597 list1
= list1
->next
;
3598 list2
= list2
->next
;
3601 if (list1
== NULL
&& list2
== NULL
)
3616 int ventoy_data_save_injection(data_injection
*data
, const char *title
, char *buf
, int buflen
)
3619 injection_node
*node
= NULL
;
3621 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
3623 VTOY_JSON_FMT_KEY_L(L1
, title
);
3624 VTOY_JSON_FMT_ARY_BEGIN_N();
3626 for (node
= data
->list
; node
; node
= node
->next
)
3628 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
3630 if (node
->type
== 0)
3632 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "image", node
->path
);
3636 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "parent", node
->path
);
3638 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "archive", node
->archive
);
3640 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
3643 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
3644 VTOY_JSON_FMT_END(pos
);
3650 int ventoy_data_json_injection(data_injection
*data
, char *buf
, int buflen
)
3654 injection_node
*node
= NULL
;
3656 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
3657 VTOY_JSON_FMT_ARY_BEGIN();
3659 for (node
= data
->list
; node
; node
= node
->next
)
3661 VTOY_JSON_FMT_OBJ_BEGIN();
3663 VTOY_JSON_FMT_UINT("type", node
->type
);
3664 VTOY_JSON_FMT_STRN("path", node
->path
);
3666 if (node
->type
== 0)
3668 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
3672 valid
= ventoy_is_directory_exist("%s%s", g_cur_dir
, node
->path
);
3674 VTOY_JSON_FMT_SINT("valid", valid
);
3676 VTOY_JSON_FMT_STRN("archive", node
->archive
);
3678 valid
= ventoy_is_file_exist("%s%s", g_cur_dir
, node
->archive
);
3679 VTOY_JSON_FMT_SINT("archive_valid", valid
);
3681 VTOY_JSON_FMT_OBJ_ENDEX();
3684 VTOY_JSON_FMT_ARY_END();
3685 VTOY_JSON_FMT_END(pos
);
3691 static int ventoy_api_get_injection(struct mg_connection
*conn
, VTOY_JSON
*json
)
3693 api_get_func(conn
, json
, injection
);
3697 static int ventoy_api_save_injection(struct mg_connection
*conn
, VTOY_JSON
*json
)
3700 ret
= ventoy_data_save_all();
3702 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3706 static int ventoy_api_injection_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
3711 const char *path
= NULL
;
3712 const char *archive
= NULL
;
3713 injection_node
*node
= NULL
;
3714 injection_node
*cur
= NULL
;
3715 data_injection
*data
= NULL
;
3717 vtoy_json_get_int(json
, "index", &index
);
3718 data
= g_data_injection
+ index
;
3720 vtoy_json_get_int(json
, "type", &type
);
3722 path
= VTOY_JSON_STR_EX("path");
3723 archive
= VTOY_JSON_STR_EX("archive");
3724 if (path
&& archive
)
3726 if (ventoy_is_real_exist_common(path
, data
->list
, injection_node
))
3728 ventoy_json_result(conn
, VTOY_JSON_DUPLICATE
);
3732 node
= zalloc(sizeof(injection_node
));
3737 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
3738 scnprintf(node
->archive
, sizeof(node
->archive
), "%s", archive
);
3740 vtoy_list_add(data
->list
, cur
, node
);
3744 ret
= ventoy_data_save_all();
3746 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3750 static int ventoy_api_injection_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
3754 const char *path
= NULL
;
3755 injection_node
*last
= NULL
;
3756 injection_node
*node
= NULL
;
3757 data_injection
*data
= NULL
;
3759 vtoy_json_get_int(json
, "index", &index
);
3760 data
= g_data_injection
+ index
;
3762 path
= VTOY_JSON_STR_EX("path");
3765 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
3767 vtoy_list_free(injection_node
, data
->list
);
3771 vtoy_list_del(last
, node
, data
->list
, path
);
3775 ret
= ventoy_data_save_all();
3777 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3784 static int ventoy_api_preview_json(struct mg_connection
*conn
, VTOY_JSON
*json
)
3789 int utf16enclen
= 0;
3790 char *encodebuf
= NULL
;
3791 unsigned short *utf16buf
= NULL
;
3795 /* We can not use json directly, because it will be formated in the JS. */
3797 len
= ventoy_data_real_save_all(0);
3799 utf16buf
= (unsigned short *)malloc(2 * len
+ 16);
3805 utf16enclen
= (int)utf8_to_utf16((unsigned char *)JSON_SAVE_BUFFER
, len
, utf16buf
, len
+ 2);
3807 encodebuf
= (char *)malloc(utf16enclen
* 4 + 16);
3813 for (i
= 0; i
< utf16enclen
; i
++)
3815 scnprintf(encodebuf
+ i
* 4, 5, "%04X", utf16buf
[i
]);
3819 VTOY_JSON_FMT_BEGIN(pos
, JSON_BUFFER
, JSON_BUF_MAX
);
3820 VTOY_JSON_FMT_OBJ_BEGIN();
3821 VTOY_JSON_FMT_STRN("json", (encodebuf
? encodebuf
: ""));
3822 VTOY_JSON_FMT_OBJ_END();
3823 VTOY_JSON_FMT_END(pos
);
3825 CHECK_FREE(encodebuf
);
3826 CHECK_FREE(utf16buf
);
3828 ventoy_json_buffer(conn
, JSON_BUFFER
, pos
);
3836 int ventoy_data_save_all(void)
3838 ventoy_set_writeback_event();
3842 int ventoy_data_real_save_all(int apilock
)
3850 pthread_mutex_lock(&g_api_mutex
);
3853 ssprintf(pos
, JSON_SAVE_BUFFER
, JSON_BUF_MAX
, "{\n");
3855 ventoy_save_plug(control
);
3856 ventoy_save_plug(theme
);
3857 ventoy_save_plug(menu_alias
);
3858 ventoy_save_plug(menu_tip
);
3859 ventoy_save_plug(menu_class
);
3860 ventoy_save_plug(auto_install
);
3861 ventoy_save_plug(persistence
);
3862 ventoy_save_plug(injection
);
3863 ventoy_save_plug(conf_replace
);
3864 ventoy_save_plug(password
);
3865 ventoy_save_plug(image_list
);
3866 ventoy_save_plug(auto_memdisk
);
3867 ventoy_save_plug(dud
);
3869 if (JSON_SAVE_BUFFER
[pos
- 1] == '\n' && JSON_SAVE_BUFFER
[pos
- 2] == ',')
3871 JSON_SAVE_BUFFER
[pos
- 2] = '\n';
3874 ssprintf(pos
, JSON_SAVE_BUFFER
, JSON_BUF_MAX
, "}\n");
3878 pthread_mutex_unlock(&g_api_mutex
);
3884 int ventoy_http_writeback(void)
3890 ventoy_get_json_path(filename
, NULL
);
3892 pos
= ventoy_data_real_save_all(1);
3895 printf("%s", JSON_SAVE_BUFFER
);
3898 ret
= ventoy_write_buf_to_file(filename
, JSON_SAVE_BUFFER
, pos
);
3901 vlog("Failed to write ventoy.json file.\n");
3902 g_sysinfo
.config_save_error
= 1;
3909 static JSON_CB g_ventoy_json_cb
[] =
3911 { "sysinfo", ventoy_api_sysinfo
},
3912 { "handshake", ventoy_api_handshake
},
3913 { "check_path", ventoy_api_check_exist
},
3914 { "check_path2", ventoy_api_check_exist2
},
3915 { "check_fuzzy", ventoy_api_check_fuzzy
},
3917 { "device_info", ventoy_api_device_info
},
3919 { "get_control", ventoy_api_get_control
},
3920 { "save_control", ventoy_api_save_control
},
3922 { "get_theme", ventoy_api_get_theme
},
3923 { "save_theme", ventoy_api_save_theme
},
3924 { "theme_add_file", ventoy_api_theme_add_file
},
3925 { "theme_del_file", ventoy_api_theme_del_file
},
3926 { "theme_add_font", ventoy_api_theme_add_font
},
3927 { "theme_del_font", ventoy_api_theme_del_font
},
3929 { "get_alias", ventoy_api_get_alias
},
3930 { "save_alias", ventoy_api_save_alias
},
3931 { "alias_add", ventoy_api_alias_add
},
3932 { "alias_del", ventoy_api_alias_del
},
3934 { "get_tip", ventoy_api_get_tip
},
3935 { "save_tip", ventoy_api_save_tip
},
3936 { "tip_add", ventoy_api_tip_add
},
3937 { "tip_del", ventoy_api_tip_del
},
3939 { "get_class", ventoy_api_get_class
},
3940 { "save_class", ventoy_api_save_class
},
3941 { "class_add", ventoy_api_class_add
},
3942 { "class_del", ventoy_api_class_del
},
3944 { "get_auto_memdisk", ventoy_api_get_auto_memdisk
},
3945 { "save_auto_memdisk", ventoy_api_save_auto_memdisk
},
3946 { "auto_memdisk_add", ventoy_api_auto_memdisk_add
},
3947 { "auto_memdisk_del", ventoy_api_auto_memdisk_del
},
3949 { "get_image_list", ventoy_api_get_image_list
},
3950 { "save_image_list", ventoy_api_save_image_list
},
3951 { "image_list_add", ventoy_api_image_list_add
},
3952 { "image_list_del", ventoy_api_image_list_del
},
3954 { "get_conf_replace", ventoy_api_get_conf_replace
},
3955 { "save_conf_replace", ventoy_api_save_conf_replace
},
3956 { "conf_replace_add", ventoy_api_conf_replace_add
},
3957 { "conf_replace_del", ventoy_api_conf_replace_del
},
3959 { "get_dud", ventoy_api_get_dud
},
3960 { "save_dud", ventoy_api_save_dud
},
3961 { "dud_add", ventoy_api_dud_add
},
3962 { "dud_del", ventoy_api_dud_del
},
3963 { "dud_add_inner", ventoy_api_dud_add_inner
},
3964 { "dud_del_inner", ventoy_api_dud_del_inner
},
3966 { "get_auto_install", ventoy_api_get_auto_install
},
3967 { "save_auto_install", ventoy_api_save_auto_install
},
3968 { "auto_install_add", ventoy_api_auto_install_add
},
3969 { "auto_install_del", ventoy_api_auto_install_del
},
3970 { "auto_install_add_inner", ventoy_api_auto_install_add_inner
},
3971 { "auto_install_del_inner", ventoy_api_auto_install_del_inner
},
3973 { "get_persistence", ventoy_api_get_persistence
},
3974 { "save_persistence", ventoy_api_save_persistence
},
3975 { "persistence_add", ventoy_api_persistence_add
},
3976 { "persistence_del", ventoy_api_persistence_del
},
3977 { "persistence_add_inner", ventoy_api_persistence_add_inner
},
3978 { "persistence_del_inner", ventoy_api_persistence_del_inner
},
3980 { "get_password", ventoy_api_get_password
},
3981 { "save_password", ventoy_api_save_password
},
3982 { "password_add", ventoy_api_password_add
},
3983 { "password_del", ventoy_api_password_del
},
3985 { "get_injection", ventoy_api_get_injection
},
3986 { "save_injection", ventoy_api_save_injection
},
3987 { "injection_add", ventoy_api_injection_add
},
3988 { "injection_del", ventoy_api_injection_del
},
3989 { "preview_json", ventoy_api_preview_json
},
3993 static int ventoy_json_handler(struct mg_connection
*conn
, VTOY_JSON
*json
, char *jsonstr
)
3996 const char *method
= NULL
;
3998 method
= vtoy_json_get_string_ex(json
, "method");
4001 ventoy_json_result(conn
, VTOY_JSON_SUCCESS_RET
);
4005 if (strcmp(method
, "handshake") == 0)
4007 ventoy_api_handshake(conn
, json
);
4011 for (i
= 0; i
< (int)(sizeof(g_ventoy_json_cb
) / sizeof(g_ventoy_json_cb
[0])); i
++)
4013 if (strcmp(method
, g_ventoy_json_cb
[i
].method
) == 0)
4015 g_ventoy_json_cb
[i
].callback(conn
, json
);
4023 static int ventoy_request_handler(struct mg_connection
*conn
)
4027 VTOY_JSON
*json
= NULL
;
4028 char *post_data_buf
= NULL
;
4029 const struct mg_request_info
*ri
= NULL
;
4030 char stack_buf
[512];
4032 ri
= mg_get_request_info(conn
);
4034 if (strcmp(ri
->uri
, "/vtoy/json") == 0)
4036 if (ri
->content_length
> 500)
4038 post_data_buf
= malloc((int)(ri
->content_length
+ 4));
4039 post_buf_len
= (int)(ri
->content_length
+ 1);
4043 post_data_buf
= stack_buf
;
4044 post_buf_len
= sizeof(stack_buf
);
4047 post_data_len
= mg_read(conn
, post_data_buf
, post_buf_len
);
4048 post_data_buf
[post_data_len
] = 0;
4050 json
= vtoy_json_create();
4051 if (JSON_SUCCESS
== vtoy_json_parse(json
, post_data_buf
))
4053 pthread_mutex_lock(&g_api_mutex
);
4054 ventoy_json_handler(conn
, json
->pstChild
, post_data_buf
);
4055 pthread_mutex_unlock(&g_api_mutex
);
4059 ventoy_json_result(conn
, VTOY_JSON_INVALID_RET
);
4062 vtoy_json_destroy(json
);
4064 if (post_data_buf
!= stack_buf
)
4066 free(post_data_buf
);
4076 const char *ventoy_web_openfile(const struct mg_connection
*conn
, const char *path
, size_t *data_len
)
4078 ventoy_file
*node
= NULL
;
4087 node
= ventoy_tar_find_file(path
);
4090 *data_len
= node
->size
;
4102 static int ventoy_parse_control(VTOY_JSON
*json
, void *p
)
4105 VTOY_JSON
*node
= NULL
;
4106 VTOY_JSON
*child
= NULL
;
4107 data_control
*data
= (data_control
*)p
;
4109 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4114 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4116 if (node
->enDataType
== JSON_TYPE_OBJECT
)
4118 child
= node
->pstChild
;
4120 if (child
->enDataType
!= JSON_TYPE_STRING
)
4125 if (strcmp(child
->pcName
, "VTOY_DEFAULT_MENU_MODE") == 0)
4127 CONTROL_PARSE_INT_DEF_0(child
, data
->default_menu_mode
);
4129 else if (strcmp(child
->pcName
, "VTOY_WIN11_BYPASS_CHECK") == 0)
4131 CONTROL_PARSE_INT_DEF_0(child
, data
->win11_bypass_check
);
4133 else if (strcmp(child
->pcName
, "VTOY_LINUX_REMOUNT") == 0)
4135 CONTROL_PARSE_INT_DEF_0(child
, data
->linux_remount
);
4137 else if (strcmp(child
->pcName
, "VTOY_SECONDARY_BOOT_MENU") == 0)
4139 CONTROL_PARSE_INT_DEF_1(child
, data
->secondary_menu
);
4141 else if (strcmp(child
->pcName
, "VTOY_SHOW_PASSWORD_ASTERISK") == 0)
4143 CONTROL_PARSE_INT_DEF_1(child
, data
->password_asterisk
);
4145 else if (strcmp(child
->pcName
, "VTOY_TREE_VIEW_MENU_STYLE") == 0)
4147 CONTROL_PARSE_INT_DEF_0(child
, data
->treeview_style
);
4149 else if (strcmp(child
->pcName
, "VTOY_FILT_DOT_UNDERSCORE_FILE") == 0)
4151 CONTROL_PARSE_INT_DEF_1(child
, data
->filter_dot_underscore
);
4153 else if (strcmp(child
->pcName
, "VTOY_SORT_CASE_SENSITIVE") == 0)
4155 CONTROL_PARSE_INT_DEF_0(child
, data
->sort_casesensitive
);
4157 else if (strcmp(child
->pcName
, "VTOY_MAX_SEARCH_LEVEL") == 0)
4159 if (strcmp(child
->unData
.pcStrVal
, "max") == 0)
4161 data
->max_search_level
= -1;
4165 data
->max_search_level
= (int)strtol(child
->unData
.pcStrVal
, NULL
, 10);
4168 else if (strcmp(child
->pcName
, "VTOY_DEFAULT_SEARCH_ROOT") == 0)
4170 strlcpy(data
->default_search_root
, child
->unData
.pcStrVal
);
4172 else if (strcmp(child
->pcName
, "VTOY_DEFAULT_IMAGE") == 0)
4174 strlcpy(data
->default_image
, child
->unData
.pcStrVal
);
4176 else if (strcmp(child
->pcName
, "VTOY_DEFAULT_KBD_LAYOUT") == 0)
4178 for (i
= 0; g_ventoy_kbd_layout
[i
]; i
++)
4180 if (strcmp(child
->unData
.pcStrVal
, g_ventoy_kbd_layout
[i
]) == 0)
4182 strlcpy(data
->default_kbd_layout
, child
->unData
.pcStrVal
);
4187 else if (strcmp(child
->pcName
, "VTOY_MENU_LANGUAGE") == 0)
4189 for (i
= 0; g_ventoy_menu_lang
[i
][0]; i
++)
4191 if (strcmp(child
->unData
.pcStrVal
, g_ventoy_menu_lang
[i
]) == 0)
4193 strlcpy(data
->menu_language
, child
->unData
.pcStrVal
);
4198 else if (strcmp(child
->pcName
, "VTOY_MENU_TIMEOUT") == 0)
4200 data
->menu_timeout
= (int)strtol(child
->unData
.pcStrVal
, NULL
, 10);
4202 else if (strcmp(child
->pcName
, "VTOY_SECONDARY_TIMEOUT") == 0)
4204 data
->secondary_menu_timeout
= (int)strtol(child
->unData
.pcStrVal
, NULL
, 10);
4206 else if (strcmp(child
->pcName
, "VTOY_VHD_NO_WARNING") == 0)
4208 CONTROL_PARSE_INT_DEF_0(child
, data
->vhd_no_warning
);
4210 else if (strcmp(child
->pcName
, "VTOY_FILE_FLT_ISO") == 0)
4212 CONTROL_PARSE_INT_DEF_0(child
, data
->filter_iso
);
4214 else if (strcmp(child
->pcName
, "VTOY_FILE_FLT_IMG") == 0)
4216 CONTROL_PARSE_INT_DEF_0(child
, data
->filter_img
);
4218 else if (strcmp(child
->pcName
, "VTOY_FILE_FLT_EFI") == 0)
4220 CONTROL_PARSE_INT_DEF_0(child
, data
->filter_efi
);
4222 else if (strcmp(child
->pcName
, "VTOY_FILE_FLT_WIM") == 0)
4224 CONTROL_PARSE_INT_DEF_0(child
, data
->filter_wim
);
4226 else if (strcmp(child
->pcName
, "VTOY_FILE_FLT_VHD") == 0)
4228 CONTROL_PARSE_INT_DEF_0(child
, data
->filter_vhd
);
4230 else if (strcmp(child
->pcName
, "VTOY_FILE_FLT_VTOY") == 0)
4232 CONTROL_PARSE_INT_DEF_0(child
, data
->filter_vtoy
);
4240 static int ventoy_parse_theme(VTOY_JSON
*json
, void *p
)
4242 const char *dismode
= NULL
;
4243 VTOY_JSON
*child
= NULL
;
4244 VTOY_JSON
*node
= NULL
;
4245 path_node
*tail
= NULL
;
4246 path_node
*pnode
= NULL
;
4247 data_theme
*data
= (data_theme
*)p
;
4249 if (json
->enDataType
!= JSON_TYPE_OBJECT
)
4254 child
= json
->pstChild
;
4256 dismode
= vtoy_json_get_string_ex(child
, "display_mode");
4257 vtoy_json_get_string(child
, "ventoy_left", sizeof(data
->ventoy_left
), data
->ventoy_left
);
4258 vtoy_json_get_string(child
, "ventoy_top", sizeof(data
->ventoy_top
), data
->ventoy_top
);
4259 vtoy_json_get_string(child
, "ventoy_color", sizeof(data
->ventoy_color
), data
->ventoy_color
);
4261 vtoy_json_get_int(child
, "default_file", &(data
->default_file
));
4262 vtoy_json_get_int(child
, "resolution_fit", &(data
->resolution_fit
));
4263 vtoy_json_get_string(child
, "gfxmode", sizeof(data
->gfxmode
), data
->gfxmode
);
4264 vtoy_json_get_string(child
, "serial_param", sizeof(data
->serial_param
), data
->serial_param
);
4268 if (strcmp(dismode
, "CLI") == 0)
4270 data
->display_mode
= display_mode_cli
;
4272 else if (strcmp(dismode
, "serial") == 0)
4274 data
->display_mode
= display_mode_serial
;
4276 else if (strcmp(dismode
, "serial_console") == 0)
4278 data
->display_mode
= display_mode_ser_console
;
4282 data
->display_mode
= display_mode_gui
;
4286 node
= vtoy_json_find_item(child
, JSON_TYPE_STRING
, "file");
4289 data
->default_file
= 0;
4290 data
->resolution_fit
= 0;
4292 pnode
= zalloc(sizeof(path_node
));
4295 strlcpy(pnode
->path
, node
->unData
.pcStrVal
);
4296 data
->filelist
= pnode
;
4301 node
= vtoy_json_find_item(child
, JSON_TYPE_ARRAY
, "file");
4304 for (node
= node
->pstChild
; node
; node
= node
->pstNext
)
4306 if (node
->enDataType
== JSON_TYPE_STRING
)
4308 pnode
= zalloc(sizeof(path_node
));
4311 strlcpy(pnode
->path
, node
->unData
.pcStrVal
);
4319 data
->filelist
= tail
= pnode
;
4328 node
= vtoy_json_find_item(child
, JSON_TYPE_ARRAY
, "fonts");
4331 for (node
= node
->pstChild
; node
; node
= node
->pstNext
)
4333 if (node
->enDataType
== JSON_TYPE_STRING
)
4335 pnode
= zalloc(sizeof(path_node
));
4338 strlcpy(pnode
->path
, node
->unData
.pcStrVal
);
4339 if (data
->fontslist
)
4346 data
->fontslist
= tail
= pnode
;
4355 static int ventoy_parse_menu_alias(VTOY_JSON
*json
, void *p
)
4358 const char *path
= NULL
;
4359 const char *alias
= NULL
;
4360 data_alias
*data
= (data_alias
*)p
;
4361 data_alias_node
*tail
= NULL
;
4362 data_alias_node
*pnode
= NULL
;
4363 VTOY_JSON
*node
= NULL
;
4365 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4370 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4372 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4377 type
= path_type_file
;
4378 path
= vtoy_json_get_string_ex(node
->pstChild
, "image");
4381 path
= vtoy_json_get_string_ex(node
->pstChild
, "dir");
4382 type
= path_type_dir
;
4384 alias
= vtoy_json_get_string_ex(node
->pstChild
, "alias");
4388 pnode
= zalloc(sizeof(data_alias_node
));
4392 strlcpy(pnode
->path
, path
);
4393 strlcpy(pnode
->alias
, alias
);
4402 data
->list
= tail
= pnode
;
4411 static int ventoy_parse_menu_tip(VTOY_JSON
*json
, void *p
)
4414 const char *path
= NULL
;
4415 const char *tip
= NULL
;
4416 data_tip
*data
= (data_tip
*)p
;
4417 data_tip_node
*tail
= NULL
;
4418 data_tip_node
*pnode
= NULL
;
4419 VTOY_JSON
*node
= NULL
;
4420 VTOY_JSON
*tips
= NULL
;
4422 if (json
->enDataType
!= JSON_TYPE_OBJECT
)
4427 vtoy_json_get_string(json
->pstChild
, "left", sizeof(data
->left
), data
->left
);
4428 vtoy_json_get_string(json
->pstChild
, "top", sizeof(data
->top
), data
->top
);
4429 vtoy_json_get_string(json
->pstChild
, "color", sizeof(data
->color
), data
->color
);
4431 tips
= vtoy_json_find_item(json
->pstChild
, JSON_TYPE_ARRAY
, "tips");
4437 for (node
= tips
->pstChild
; node
; node
= node
->pstNext
)
4439 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4444 type
= path_type_file
;
4445 path
= vtoy_json_get_string_ex(node
->pstChild
, "image");
4448 path
= vtoy_json_get_string_ex(node
->pstChild
, "dir");
4449 type
= path_type_dir
;
4451 tip
= vtoy_json_get_string_ex(node
->pstChild
, "tip");
4455 pnode
= zalloc(sizeof(data_tip_node
));
4459 strlcpy(pnode
->path
, path
);
4460 strlcpy(pnode
->tip
, tip
);
4469 data
->list
= tail
= pnode
;
4477 static int ventoy_parse_menu_class(VTOY_JSON
*json
, void *p
)
4480 const char *path
= NULL
;
4481 const char *class = NULL
;
4482 data_class
*data
= (data_class
*)p
;
4483 data_class_node
*tail
= NULL
;
4484 data_class_node
*pnode
= NULL
;
4485 VTOY_JSON
*node
= NULL
;
4487 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4492 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4494 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4499 type
= class_type_key
;
4500 path
= vtoy_json_get_string_ex(node
->pstChild
, "key");
4503 type
= class_type_dir
;
4504 path
= vtoy_json_get_string_ex(node
->pstChild
, "dir");
4507 type
= class_type_parent
;
4508 path
= vtoy_json_get_string_ex(node
->pstChild
, "parent");
4511 class = vtoy_json_get_string_ex(node
->pstChild
, "class");
4515 pnode
= zalloc(sizeof(data_class_node
));
4519 strlcpy(pnode
->path
, path
);
4520 strlcpy(pnode
->class, class);
4529 data
->list
= tail
= pnode
;
4537 static int ventoy_parse_auto_install(VTOY_JSON
*json
, void *p
)
4545 const char *path
= NULL
;
4546 const char *file
= NULL
;
4547 data_auto_install
*data
= (data_auto_install
*)p
;
4548 auto_install_node
*tail
= NULL
;
4549 auto_install_node
*pnode
= NULL
;
4550 path_node
*pathnode
= NULL
;
4551 path_node
*pathtail
= NULL
;
4552 VTOY_JSON
*node
= NULL
;
4553 VTOY_JSON
*filelist
= NULL
;
4554 VTOY_JSON
*filenode
= NULL
;
4556 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4561 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4563 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4569 path
= vtoy_json_get_string_ex(node
->pstChild
, "image");
4572 path
= vtoy_json_get_string_ex(node
->pstChild
, "parent");
4580 file
= vtoy_json_get_string_ex(node
->pstChild
, "template");
4583 pnode
= zalloc(sizeof(auto_install_node
));
4588 strlcpy(pnode
->path
, path
);
4590 pathnode
= zalloc(sizeof(path_node
));
4593 strlcpy(pathnode
->path
, file
);
4594 pnode
->list
= pathnode
;
4608 data
->list
= tail
= pnode
;
4616 timeouten
= autoselen
= 0;
4617 if (JSON_SUCCESS
== vtoy_json_get_int(node
->pstChild
, "timeout", &timeout
))
4621 if (JSON_SUCCESS
== vtoy_json_get_int(node
->pstChild
, "autosel", &autosel
))
4626 filelist
= vtoy_json_find_item(node
->pstChild
, JSON_TYPE_ARRAY
, "template");
4632 pnode
= zalloc(sizeof(auto_install_node
));
4639 pnode
->autoselen
= autoselen
;
4640 pnode
->timeouten
= timeouten
;
4641 pnode
->timeout
= timeout
;
4642 pnode
->autosel
= autosel
;
4643 strlcpy(pnode
->path
, path
);
4646 for (filenode
= filelist
->pstChild
; filenode
; filenode
= filenode
->pstNext
)
4648 if (filenode
->enDataType
!= JSON_TYPE_STRING
)
4653 pathnode
= zalloc(sizeof(path_node
));
4657 strlcpy(pathnode
->path
, filenode
->unData
.pcStrVal
);
4661 pathtail
->next
= pathnode
;
4662 pathtail
= pathnode
;
4666 pnode
->list
= pathtail
= pathnode
;
4677 if (pnode
->autoselen
&& pnode
->autosel
> count
)
4689 data
->list
= tail
= pnode
;
4696 static int ventoy_parse_persistence(VTOY_JSON
*json
, void *p
)
4703 const char *path
= NULL
;
4704 const char *file
= NULL
;
4705 data_persistence
*data
= (data_persistence
*)p
;
4706 persistence_node
*tail
= NULL
;
4707 persistence_node
*pnode
= NULL
;
4708 path_node
*pathnode
= NULL
;
4709 path_node
*pathtail
= NULL
;
4710 VTOY_JSON
*node
= NULL
;
4711 VTOY_JSON
*filelist
= NULL
;
4712 VTOY_JSON
*filenode
= NULL
;
4714 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4719 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4721 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4726 path
= vtoy_json_get_string_ex(node
->pstChild
, "image");
4732 file
= vtoy_json_get_string_ex(node
->pstChild
, "backend");
4735 pnode
= zalloc(sizeof(persistence_node
));
4740 strlcpy(pnode
->path
, path
);
4742 pathnode
= zalloc(sizeof(path_node
));
4745 strlcpy(pathnode
->path
, file
);
4746 pnode
->list
= pathnode
;
4760 data
->list
= tail
= pnode
;
4768 timeouten
= autoselen
= 0;
4769 if (JSON_SUCCESS
== vtoy_json_get_int(node
->pstChild
, "timeout", &timeout
))
4773 if (JSON_SUCCESS
== vtoy_json_get_int(node
->pstChild
, "autosel", &autosel
))
4778 filelist
= vtoy_json_find_item(node
->pstChild
, JSON_TYPE_ARRAY
, "backend");
4784 pnode
= zalloc(sizeof(persistence_node
));
4791 pnode
->autoselen
= autoselen
;
4792 pnode
->timeouten
= timeouten
;
4793 pnode
->timeout
= timeout
;
4794 pnode
->autosel
= autosel
;
4795 strlcpy(pnode
->path
, path
);
4798 for (filenode
= filelist
->pstChild
; filenode
; filenode
= filenode
->pstNext
)
4800 if (filenode
->enDataType
!= JSON_TYPE_STRING
)
4805 pathnode
= zalloc(sizeof(path_node
));
4809 strlcpy(pathnode
->path
, filenode
->unData
.pcStrVal
);
4813 pathtail
->next
= pathnode
;
4814 pathtail
= pathnode
;
4818 pnode
->list
= pathtail
= pathnode
;
4829 if (pnode
->autoselen
&& pnode
->autosel
> count
)
4841 data
->list
= tail
= pnode
;
4848 static int ventoy_parse_injection(VTOY_JSON
*json
, void *p
)
4851 const char *path
= NULL
;
4852 const char *archive
= NULL
;
4853 data_injection
*data
= (data_injection
*)p
;
4854 injection_node
*tail
= NULL
;
4855 injection_node
*pnode
= NULL
;
4856 VTOY_JSON
*node
= NULL
;
4858 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4863 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4865 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4871 path
= vtoy_json_get_string_ex(node
->pstChild
, "image");
4874 path
= vtoy_json_get_string_ex(node
->pstChild
, "parent");
4877 archive
= vtoy_json_get_string_ex(node
->pstChild
, "archive");
4879 if (path
&& archive
)
4881 pnode
= zalloc(sizeof(injection_node
));
4885 strlcpy(pnode
->path
, path
);
4886 strlcpy(pnode
->archive
, archive
);
4895 data
->list
= tail
= pnode
;
4903 static int ventoy_parse_conf_replace(VTOY_JSON
*json
, void *p
)
4906 const char *path
= NULL
;
4907 const char *org
= NULL
;
4908 const char *new = NULL
;
4909 data_conf_replace
*data
= (data_conf_replace
*)p
;
4910 conf_replace_node
*tail
= NULL
;
4911 conf_replace_node
*pnode
= NULL
;
4912 VTOY_JSON
*node
= NULL
;
4914 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4919 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4921 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4926 path
= vtoy_json_get_string_ex(node
->pstChild
, "iso");
4927 org
= vtoy_json_get_string_ex(node
->pstChild
, "org");
4928 new = vtoy_json_get_string_ex(node
->pstChild
, "new");
4931 vtoy_json_get_int(node
->pstChild
, "img", &img
);
4933 if (path
&& org
&& new)
4935 pnode
= zalloc(sizeof(conf_replace_node
));
4938 strlcpy(pnode
->path
, path
);
4939 strlcpy(pnode
->org
, org
);
4940 strlcpy(pnode
->new, new);
4953 data
->list
= tail
= pnode
;
4961 static int ventoy_parse_password(VTOY_JSON
*json
, void *p
)
4964 const char *bootpwd
= NULL
;
4965 const char *isopwd
= NULL
;
4966 const char *wimpwd
= NULL
;
4967 const char *imgpwd
= NULL
;
4968 const char *efipwd
= NULL
;
4969 const char *vhdpwd
= NULL
;
4970 const char *vtoypwd
= NULL
;
4971 const char *path
= NULL
;
4972 const char *pwd
= NULL
;
4973 data_password
*data
= (data_password
*)p
;
4974 menu_password
*tail
= NULL
;
4975 menu_password
*pnode
= NULL
;
4976 VTOY_JSON
*node
= NULL
;
4977 VTOY_JSON
*menupwd
= NULL
;
4979 if (json
->enDataType
!= JSON_TYPE_OBJECT
)
4984 bootpwd
= vtoy_json_get_string_ex(json
->pstChild
, "bootpwd");
4985 isopwd
= vtoy_json_get_string_ex(json
->pstChild
, "isopwd");
4986 wimpwd
= vtoy_json_get_string_ex(json
->pstChild
, "wimpwd");
4987 imgpwd
= vtoy_json_get_string_ex(json
->pstChild
, "imgpwd");
4988 efipwd
= vtoy_json_get_string_ex(json
->pstChild
, "efipwd");
4989 vhdpwd
= vtoy_json_get_string_ex(json
->pstChild
, "vhdpwd");
4990 vtoypwd
= vtoy_json_get_string_ex(json
->pstChild
, "vtoypwd");
4993 if (bootpwd
) strlcpy(data
->bootpwd
, bootpwd
);
4994 if (isopwd
) strlcpy(data
->isopwd
, isopwd
);
4995 if (wimpwd
) strlcpy(data
->wimpwd
, wimpwd
);
4996 if (imgpwd
) strlcpy(data
->imgpwd
, imgpwd
);
4997 if (efipwd
) strlcpy(data
->efipwd
, efipwd
);
4998 if (vhdpwd
) strlcpy(data
->vhdpwd
, vhdpwd
);
4999 if (vtoypwd
) strlcpy(data
->vtoypwd
, vtoypwd
);
5002 menupwd
= vtoy_json_find_item(json
->pstChild
, JSON_TYPE_ARRAY
, "menupwd");
5008 for (node
= menupwd
->pstChild
; node
; node
= node
->pstNext
)
5010 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
5016 path
= vtoy_json_get_string_ex(node
->pstChild
, "file");
5019 path
= vtoy_json_get_string_ex(node
->pstChild
, "parent");
5022 pwd
= vtoy_json_get_string_ex(node
->pstChild
, "pwd");
5026 pnode
= zalloc(sizeof(menu_password
));
5030 strlcpy(pnode
->path
, path
);
5031 strlcpy(pnode
->pwd
, pwd
);
5040 data
->list
= tail
= pnode
;
5049 static int ventoy_parse_image_list_real(VTOY_JSON
*json
, int type
, void *p
)
5051 VTOY_JSON
*node
= NULL
;
5052 data_image_list
*data
= (data_image_list
*)p
;
5053 path_node
*tail
= NULL
;
5054 path_node
*pnode
= NULL
;
5056 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
5063 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
5065 if (node
->enDataType
== JSON_TYPE_STRING
)
5067 pnode
= zalloc(sizeof(path_node
));
5070 strlcpy(pnode
->path
, node
->unData
.pcStrVal
);
5078 data
->list
= tail
= pnode
;
5086 static int ventoy_parse_image_blacklist(VTOY_JSON
*json
, void *p
)
5088 return ventoy_parse_image_list_real(json
, 1, p
);
5090 static int ventoy_parse_image_list(VTOY_JSON
*json
, void *p
)
5092 return ventoy_parse_image_list_real(json
, 0, p
);
5095 static int ventoy_parse_auto_memdisk(VTOY_JSON
*json
, void *p
)
5097 VTOY_JSON
*node
= NULL
;
5098 data_auto_memdisk
*data
= (data_auto_memdisk
*)p
;
5099 path_node
*tail
= NULL
;
5100 path_node
*pnode
= NULL
;
5102 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
5107 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
5109 if (node
->enDataType
== JSON_TYPE_STRING
)
5111 pnode
= zalloc(sizeof(path_node
));
5114 strlcpy(pnode
->path
, node
->unData
.pcStrVal
);
5122 data
->list
= tail
= pnode
;
5130 static int ventoy_parse_dud(VTOY_JSON
*json
, void *p
)
5133 const char *path
= NULL
;
5134 const char *file
= NULL
;
5135 data_dud
*data
= (data_dud
*)p
;
5136 dud_node
*tail
= NULL
;
5137 dud_node
*pnode
= NULL
;
5138 path_node
*pathnode
= NULL
;
5139 path_node
*pathtail
= NULL
;
5140 VTOY_JSON
*node
= NULL
;
5141 VTOY_JSON
*filelist
= NULL
;
5142 VTOY_JSON
*filenode
= NULL
;
5144 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
5149 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
5151 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
5156 path
= vtoy_json_get_string_ex(node
->pstChild
, "image");
5162 file
= vtoy_json_get_string_ex(node
->pstChild
, "dud");
5165 pnode
= zalloc(sizeof(dud_node
));
5168 strlcpy(pnode
->path
, path
);
5170 pathnode
= zalloc(sizeof(path_node
));
5173 strlcpy(pathnode
->path
, file
);
5174 pnode
->list
= pathnode
;
5188 data
->list
= tail
= pnode
;
5195 filelist
= vtoy_json_find_item(node
->pstChild
, JSON_TYPE_ARRAY
, "dud");
5201 pnode
= zalloc(sizeof(dud_node
));
5207 strlcpy(pnode
->path
, path
);
5209 for (filenode
= filelist
->pstChild
; filenode
; filenode
= filenode
->pstNext
)
5211 if (filenode
->enDataType
!= JSON_TYPE_STRING
)
5216 pathnode
= zalloc(sizeof(path_node
));
5219 strlcpy(pathnode
->path
, filenode
->unData
.pcStrVal
);
5224 pathtail
->next
= pathnode
;
5225 pathtail
= pathnode
;
5229 pnode
->list
= pathtail
= pathnode
;
5247 data
->list
= tail
= pnode
;
5261 static int ventoy_load_old_json(const char *filename
)
5266 char *buffer
= NULL
;
5267 unsigned char *start
= NULL
;
5268 VTOY_JSON
*json
= NULL
;
5269 VTOY_JSON
*node
= NULL
;
5270 VTOY_JSON
*next
= NULL
;
5272 ret
= ventoy_read_file_to_buf(filename
, 4, (void **)&buffer
, &buflen
);
5275 vlog("Failed to read old ventoy.json file.\n");
5280 start
= (unsigned char *)buffer
;
5282 if (start
[0] == 0xef && start
[1] == 0xbb && start
[2] == 0xbf)
5286 else if ((start
[0] == 0xff && start
[1] == 0xfe) || (start
[0] == 0xfe && start
[1] == 0xff))
5288 vlog("ventoy.json is in UCS-2 encoding, ignore it.\n");
5293 json
= vtoy_json_create();
5300 if (vtoy_json_parse_ex(json
, buffer
+ offset
, buflen
- offset
) == JSON_SUCCESS
)
5302 vlog("parse ventoy.json success\n");
5304 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
5305 for (next
= node
->pstNext
; next
; next
= next
->pstNext
)
5307 if (node
->pcName
&& next
->pcName
&& strcmp(node
->pcName
, next
->pcName
) == 0)
5309 vlog("ventoy.json contains duplicate key <%s>.\n", node
->pcName
);
5310 g_sysinfo
.invalid_config
= 1;
5316 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
5318 ventoy_parse_json(control
);
5319 ventoy_parse_json(theme
);
5320 ventoy_parse_json(menu_alias
);
5321 ventoy_parse_json(menu_tip
);
5322 ventoy_parse_json(menu_class
);
5323 ventoy_parse_json(auto_install
);
5324 ventoy_parse_json(persistence
);
5325 ventoy_parse_json(injection
);
5326 ventoy_parse_json(conf_replace
);
5327 ventoy_parse_json(password
);
5328 ventoy_parse_json(image_list
);
5329 ventoy_parse_json(image_blacklist
);
5330 ventoy_parse_json(auto_memdisk
);
5331 ventoy_parse_json(dud
);
5336 vlog("ventoy.json has syntax error.\n");
5337 g_sysinfo
.syntax_error
= 1;
5342 vtoy_json_destroy(json
);
5349 int ventoy_http_start(const char *ip
, const char *port
)
5355 char backupname
[128];
5356 struct mg_callbacks callbacks
;
5357 const char *options
[] =
5359 "listening_ports", "24681",
5360 "document_root", "www",
5361 "index_files", "index.html",
5362 "num_threads", "16",
5363 "error_log_file", LOG_FILE
,
5364 "request_timeout_ms", "10000",
5368 for (i
= 0; i
<= bios_max
; i
++)
5370 ventoy_data_default_control(g_data_control
+ i
);
5371 ventoy_data_default_theme(g_data_theme
+ i
);
5372 ventoy_data_default_menu_alias(g_data_menu_alias
+ i
);
5373 ventoy_data_default_menu_class(g_data_menu_class
+ i
);
5374 ventoy_data_default_menu_tip(g_data_menu_tip
+ i
);
5375 ventoy_data_default_auto_install(g_data_auto_install
+ i
);
5376 ventoy_data_default_persistence(g_data_persistence
+ i
);
5377 ventoy_data_default_injection(g_data_injection
+ i
);
5378 ventoy_data_default_conf_replace(g_data_conf_replace
+ i
);
5379 ventoy_data_default_password(g_data_password
+ i
);
5380 ventoy_data_default_image_list(g_data_image_list
+ i
);
5381 ventoy_data_default_auto_memdisk(g_data_auto_memdisk
+ i
);
5382 ventoy_data_default_dud(g_data_dud
+ i
);
5385 ventoy_get_json_path(filename
, backupname
);
5386 if (ventoy_is_file_exist("%s", filename
))
5388 ventoy_copy_file(filename
, backupname
);
5389 ret
= ventoy_load_old_json(filename
);
5392 ventoy_data_real_save_all(0);
5397 scnprintf(addr
, sizeof(addr
), "%s:%s", ip
, port
);
5400 memset(&callbacks
, 0, sizeof(callbacks
));
5401 callbacks
.begin_request
= ventoy_request_handler
;
5403 callbacks
.open_file
= ventoy_web_openfile
;
5405 g_ventoy_http_ctx
= mg_start(&callbacks
, NULL
, options
);
5407 ventoy_start_writeback_thread(ventoy_http_writeback
);
5409 return g_ventoy_http_ctx
? 0 : 1;
5412 int ventoy_http_stop(void)
5414 if (g_ventoy_http_ctx
)
5416 mg_stop(g_ventoy_http_ctx
);
5419 ventoy_stop_writeback_thread();
5423 int ventoy_http_init(void)
5428 char *Buffer
= NULL
;
5431 ventoy_read_file_to_buf("www/menulist", 4, (void **)&Buffer
, &BufLen
);
5434 for (i
= 0; i
< BufLen
/ 5; i
++)
5436 memcpy(g_ventoy_menu_lang
[i
], Buffer
+ i
* 5, 5);
5437 g_ventoy_menu_lang
[i
][5] = 0;
5444 file
= ventoy_tar_find_file("www/menulist");
5447 for (i
= 0; i
< file
->size
/ 5; i
++)
5449 memcpy(g_ventoy_menu_lang
[i
], (char *)(file
->addr
) + i
* 5, 5);
5450 g_ventoy_menu_lang
[i
][5] = 0;
5455 if (!g_pub_json_buffer
)
5457 g_pub_json_buffer
= malloc(JSON_BUF_MAX
* 2);
5458 g_pub_save_buffer
= g_pub_json_buffer
+ JSON_BUF_MAX
;
5462 pthread_mutex_init(&g_api_mutex
, NULL
);
5466 void ventoy_http_exit(void)
5468 check_free(g_pub_json_buffer
);
5469 g_pub_json_buffer
= NULL
;
5470 g_pub_save_buffer
= NULL
;
5472 pthread_mutex_destroy(&g_api_mutex
);