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;
560 data
->win11_bypass_check
= 1;
561 data
->win11_bypass_nro
= 1;
563 strlcpy(data
->default_kbd_layout
, "QWERTY_USA");
564 strlcpy(data
->menu_language
, "en_US");
567 int ventoy_data_cmp_control(data_control
*data1
, data_control
*data2
)
569 if (data1
->default_menu_mode
!= data2
->default_menu_mode
||
570 data1
->treeview_style
!= data2
->treeview_style
||
571 data1
->filter_dot_underscore
!= data2
->filter_dot_underscore
||
572 data1
->sort_casesensitive
!= data2
->sort_casesensitive
||
573 data1
->max_search_level
!= data2
->max_search_level
||
574 data1
->vhd_no_warning
!= data2
->vhd_no_warning
||
575 data1
->filter_iso
!= data2
->filter_iso
||
576 data1
->filter_wim
!= data2
->filter_wim
||
577 data1
->filter_efi
!= data2
->filter_efi
||
578 data1
->filter_img
!= data2
->filter_img
||
579 data1
->filter_vhd
!= data2
->filter_vhd
||
580 data1
->filter_vtoy
!= data2
->filter_vtoy
||
581 data1
->win11_bypass_check
!= data2
->win11_bypass_check
||
582 data1
->win11_bypass_nro
!= data2
->win11_bypass_nro
||
583 data1
->linux_remount
!= data2
->linux_remount
||
584 data1
->password_asterisk
!= data2
->password_asterisk
||
585 data1
->secondary_menu
!= data2
->secondary_menu
||
586 data1
->menu_timeout
!= data2
->menu_timeout
||
587 data1
->secondary_menu_timeout
!= data2
->secondary_menu_timeout
)
592 if (strcmp(data1
->default_search_root
, data2
->default_search_root
) ||
593 strcmp(data1
->default_image
, data2
->default_image
) ||
594 strcmp(data1
->default_kbd_layout
, data2
->default_kbd_layout
) ||
595 strcmp(data1
->menu_language
, data2
->menu_language
))
603 int ventoy_data_save_control(data_control
*data
, const char *title
, char *buf
, int buflen
)
606 data_control
*def
= g_data_control
+ bios_max
;
608 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
610 VTOY_JSON_FMT_KEY_L(L1
, title
);
611 VTOY_JSON_FMT_ARY_BEGIN_N();
613 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_DEFAULT_MENU_MODE", default_menu_mode
);
614 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_TREE_VIEW_MENU_STYLE", treeview_style
);
615 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_FILT_DOT_UNDERSCORE_FILE", filter_dot_underscore
);
616 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_SORT_CASE_SENSITIVE", sort_casesensitive
);
618 if (data
->max_search_level
>= 0)
620 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_MAX_SEARCH_LEVEL", max_search_level
);
623 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_VHD_NO_WARNING", vhd_no_warning
);
624 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_FILE_FLT_ISO", filter_iso
);
625 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_FILE_FLT_WIM", filter_wim
);
626 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_FILE_FLT_EFI", filter_efi
);
627 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_FILE_FLT_IMG", filter_img
);
628 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_FILE_FLT_VHD", filter_vhd
);
629 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_FILE_FLT_VTOY", filter_vtoy
);
630 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_WIN11_BYPASS_CHECK", win11_bypass_check
);
631 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_WIN11_BYPASS_NRO", win11_bypass_nro
);
632 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_LINUX_REMOUNT", linux_remount
);
633 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_SECONDARY_BOOT_MENU", secondary_menu
);
634 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_SHOW_PASSWORD_ASTERISK", password_asterisk
);
635 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_MENU_TIMEOUT", menu_timeout
);
636 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_SECONDARY_TIMEOUT", secondary_menu_timeout
);
638 VTOY_JSON_FMT_CTRL_STRN(L2
, "VTOY_DEFAULT_KBD_LAYOUT", default_kbd_layout
);
639 VTOY_JSON_FMT_CTRL_STRN(L2
, "VTOY_MENU_LANGUAGE", menu_language
);
641 if (strcmp(def
->default_search_root
, data
->default_search_root
))
643 VTOY_JSON_FMT_CTRL_STRN_STR(L2
, "VTOY_DEFAULT_SEARCH_ROOT", ventoy_real_path(data
->default_search_root
));
646 if (strcmp(def
->default_image
, data
->default_image
))
648 VTOY_JSON_FMT_CTRL_STRN_STR(L2
, "VTOY_DEFAULT_IMAGE", ventoy_real_path(data
->default_image
));
651 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
652 VTOY_JSON_FMT_END(pos
);
657 int ventoy_data_json_control(data_control
*ctrl
, char *buf
, int buflen
)
663 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
664 VTOY_JSON_FMT_OBJ_BEGIN();
666 VTOY_JSON_FMT_SINT("default_menu_mode", ctrl
->default_menu_mode
);
667 VTOY_JSON_FMT_SINT("treeview_style", ctrl
->treeview_style
);
668 VTOY_JSON_FMT_SINT("filter_dot_underscore", ctrl
->filter_dot_underscore
);
669 VTOY_JSON_FMT_SINT("sort_casesensitive", ctrl
->sort_casesensitive
);
670 VTOY_JSON_FMT_SINT("max_search_level", ctrl
->max_search_level
);
671 VTOY_JSON_FMT_SINT("vhd_no_warning", ctrl
->vhd_no_warning
);
673 VTOY_JSON_FMT_SINT("filter_iso", ctrl
->filter_iso
);
674 VTOY_JSON_FMT_SINT("filter_wim", ctrl
->filter_wim
);
675 VTOY_JSON_FMT_SINT("filter_efi", ctrl
->filter_efi
);
676 VTOY_JSON_FMT_SINT("filter_img", ctrl
->filter_img
);
677 VTOY_JSON_FMT_SINT("filter_vhd", ctrl
->filter_vhd
);
678 VTOY_JSON_FMT_SINT("filter_vtoy", ctrl
->filter_vtoy
);
679 VTOY_JSON_FMT_SINT("win11_bypass_check", ctrl
->win11_bypass_check
);
680 VTOY_JSON_FMT_SINT("win11_bypass_nro", ctrl
->win11_bypass_nro
);
681 VTOY_JSON_FMT_SINT("linux_remount", ctrl
->linux_remount
);
682 VTOY_JSON_FMT_SINT("secondary_menu", ctrl
->secondary_menu
);
683 VTOY_JSON_FMT_SINT("password_asterisk", ctrl
->password_asterisk
);
684 VTOY_JSON_FMT_SINT("menu_timeout", ctrl
->menu_timeout
);
685 VTOY_JSON_FMT_SINT("secondary_menu_timeout", ctrl
->secondary_menu_timeout
);
686 VTOY_JSON_FMT_STRN("default_kbd_layout", ctrl
->default_kbd_layout
);
687 VTOY_JSON_FMT_STRN("menu_language", ctrl
->menu_language
);
690 if (ctrl
->default_search_root
[0] && ventoy_is_directory_exist("%s%s", g_cur_dir
, ctrl
->default_search_root
))
694 VTOY_JSON_FMT_STRN("default_search_root", ctrl
->default_search_root
);
695 VTOY_JSON_FMT_SINT("default_search_root_valid", valid
);
699 if (ctrl
->default_image
[0] && ventoy_is_file_exist("%s%s", g_cur_dir
, ctrl
->default_image
))
703 VTOY_JSON_FMT_STRN("default_image", ctrl
->default_image
);
704 VTOY_JSON_FMT_SINT("default_image_valid", valid
);
706 VTOY_JSON_FMT_KEY("menu_list");
707 VTOY_JSON_FMT_ARY_BEGIN();
709 for (i
= 0; g_ventoy_menu_lang
[i
][0]; i
++)
711 VTOY_JSON_FMT_ITEM(g_ventoy_menu_lang
[i
]);
713 VTOY_JSON_FMT_ARY_ENDEX();
715 VTOY_JSON_FMT_OBJ_END();
716 VTOY_JSON_FMT_END(pos
);
721 static int ventoy_api_get_control(struct mg_connection
*conn
, VTOY_JSON
*json
)
723 api_get_func(conn
, json
, control
);
727 static int ventoy_api_save_control(struct mg_connection
*conn
, VTOY_JSON
*json
)
731 data_control
*ctrl
= NULL
;
733 vtoy_json_get_int(json
, "index", &index
);
734 ctrl
= g_data_control
+ index
;
736 VTOY_JSON_INT("default_menu_mode", ctrl
->default_menu_mode
);
737 VTOY_JSON_INT("treeview_style", ctrl
->treeview_style
);
738 VTOY_JSON_INT("filter_dot_underscore", ctrl
->filter_dot_underscore
);
739 VTOY_JSON_INT("sort_casesensitive", ctrl
->sort_casesensitive
);
740 VTOY_JSON_INT("max_search_level", ctrl
->max_search_level
);
741 VTOY_JSON_INT("vhd_no_warning", ctrl
->vhd_no_warning
);
742 VTOY_JSON_INT("filter_iso", ctrl
->filter_iso
);
743 VTOY_JSON_INT("filter_wim", ctrl
->filter_wim
);
744 VTOY_JSON_INT("filter_efi", ctrl
->filter_efi
);
745 VTOY_JSON_INT("filter_img", ctrl
->filter_img
);
746 VTOY_JSON_INT("filter_vhd", ctrl
->filter_vhd
);
747 VTOY_JSON_INT("filter_vtoy", ctrl
->filter_vtoy
);
748 VTOY_JSON_INT("win11_bypass_check", ctrl
->win11_bypass_check
);
749 VTOY_JSON_INT("win11_bypass_nro", ctrl
->win11_bypass_nro
);
750 VTOY_JSON_INT("linux_remount", ctrl
->linux_remount
);
751 VTOY_JSON_INT("secondary_menu", ctrl
->secondary_menu
);
752 VTOY_JSON_INT("password_asterisk", ctrl
->password_asterisk
);
753 VTOY_JSON_INT("menu_timeout", ctrl
->menu_timeout
);
754 VTOY_JSON_INT("secondary_menu_timeout", ctrl
->secondary_menu_timeout
);
756 VTOY_JSON_STR("default_image", ctrl
->default_image
);
757 VTOY_JSON_STR("default_search_root", ctrl
->default_search_root
);
758 VTOY_JSON_STR("menu_language", ctrl
->menu_language
);
759 VTOY_JSON_STR("default_kbd_layout", ctrl
->default_kbd_layout
);
761 ret
= ventoy_data_save_all();
763 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
771 void ventoy_data_default_theme(data_theme
*data
)
773 memset(data
, 0, sizeof(data_theme
));
774 strlcpy(data
->gfxmode
, "1024x768");
775 scnprintf(data
->ventoy_left
, sizeof(data
->ventoy_left
), "5%%");
776 scnprintf(data
->ventoy_top
, sizeof(data
->ventoy_top
), "95%%");
777 scnprintf(data
->ventoy_color
, sizeof(data
->ventoy_color
), "%s", "#0000ff");
780 int ventoy_data_cmp_theme(data_theme
*data1
, data_theme
*data2
)
782 if (data1
->display_mode
!= data2
->display_mode
||
783 strcmp(data1
->ventoy_left
, data2
->ventoy_left
) ||
784 strcmp(data1
->ventoy_top
, data2
->ventoy_top
) ||
785 strcmp(data1
->gfxmode
, data2
->gfxmode
) ||
786 strcmp(data1
->ventoy_color
, data2
->ventoy_color
)
792 if (ventoy_path_list_cmp(data1
->filelist
, data2
->filelist
))
797 if (ventoy_path_list_cmp(data1
->fontslist
, data2
->fontslist
))
806 int ventoy_data_save_theme(data_theme
*data
, const char *title
, char *buf
, int buflen
)
809 path_node
*node
= NULL
;
810 data_theme
*def
= g_data_theme
+ bios_max
;
812 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
814 VTOY_JSON_FMT_KEY_L(L1
, title
);
815 VTOY_JSON_FMT_OBJ_BEGIN_N();
819 if (data
->filelist
->next
)
821 VTOY_JSON_FMT_KEY_L(L2
, "file");
822 VTOY_JSON_FMT_ARY_BEGIN_N();
824 for (node
= data
->filelist
; node
; node
= node
->next
)
826 VTOY_JSON_FMT_ITEM_PATH_LN(L3
, node
->path
);
829 VTOY_JSON_FMT_ARY_ENDEX_LN(L2
);
831 if (def
->default_file
!= data
->default_file
)
833 VTOY_JSON_FMT_SINT_LN(L2
, "default_file", data
->default_file
);
836 if (def
->resolution_fit
!= data
->resolution_fit
)
838 VTOY_JSON_FMT_SINT_LN(L2
, "resolution_fit", data
->resolution_fit
);
843 VTOY_JSON_FMT_STRN_PATH_LN(L2
, "file", data
->filelist
->path
);
847 if (data
->display_mode
!= def
->display_mode
)
849 if (display_mode_cli
== data
->display_mode
)
851 VTOY_JSON_FMT_STRN_LN(L2
, "display_mode", "CLI");
853 else if (display_mode_serial
== data
->display_mode
)
855 VTOY_JSON_FMT_STRN_LN(L2
, "display_mode", "serial");
857 else if (display_mode_ser_console
== data
->display_mode
)
859 VTOY_JSON_FMT_STRN_LN(L2
, "display_mode", "serial_console");
863 VTOY_JSON_FMT_STRN_LN(L2
, "display_mode", "GUI");
867 VTOY_JSON_FMT_DIFF_STRN(L2
, "gfxmode", gfxmode
);
869 VTOY_JSON_FMT_DIFF_STRN(L2
, "ventoy_left", ventoy_left
);
870 VTOY_JSON_FMT_DIFF_STRN(L2
, "ventoy_top", ventoy_top
);
871 VTOY_JSON_FMT_DIFF_STRN(L2
, "ventoy_color", ventoy_color
);
875 VTOY_JSON_FMT_KEY_L(L2
, "fonts");
876 VTOY_JSON_FMT_ARY_BEGIN_N();
878 for (node
= data
->fontslist
; node
; node
= node
->next
)
880 VTOY_JSON_FMT_ITEM_PATH_LN(L3
, node
->path
);
883 VTOY_JSON_FMT_ARY_ENDEX_LN(L2
);
886 VTOY_JSON_FMT_OBJ_ENDEX_LN(L1
);
887 VTOY_JSON_FMT_END(pos
);
893 int ventoy_data_json_theme(data_theme
*data
, char *buf
, int buflen
)
896 path_node
*node
= NULL
;
898 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
899 VTOY_JSON_FMT_OBJ_BEGIN();
901 VTOY_JSON_FMT_SINT("default_file", data
->default_file
);
902 VTOY_JSON_FMT_SINT("resolution_fit", data
->resolution_fit
);
903 VTOY_JSON_FMT_SINT("display_mode", data
->display_mode
);
904 VTOY_JSON_FMT_STRN("gfxmode", data
->gfxmode
);
906 VTOY_JSON_FMT_STRN("ventoy_color", data
->ventoy_color
);
907 VTOY_JSON_FMT_STRN("ventoy_left", data
->ventoy_left
);
908 VTOY_JSON_FMT_STRN("ventoy_top", data
->ventoy_top
);
910 VTOY_JSON_FMT_KEY("filelist");
911 VTOY_JSON_FMT_ARY_BEGIN();
912 for (node
= data
->filelist
; node
; node
= node
->next
)
914 VTOY_JSON_FMT_OBJ_BEGIN();
915 VTOY_JSON_FMT_STRN("path", node
->path
);
916 VTOY_JSON_FMT_SINT("valid", ventoy_is_file_exist("%s%s", g_cur_dir
, node
->path
));
917 VTOY_JSON_FMT_OBJ_ENDEX();
919 VTOY_JSON_FMT_ARY_ENDEX();
921 VTOY_JSON_FMT_KEY("fontslist");
922 VTOY_JSON_FMT_ARY_BEGIN();
923 for (node
= data
->fontslist
; node
; node
= node
->next
)
925 VTOY_JSON_FMT_OBJ_BEGIN();
926 VTOY_JSON_FMT_STRN("path", node
->path
);
927 VTOY_JSON_FMT_SINT("valid", ventoy_is_file_exist("%s%s", g_cur_dir
, node
->path
));
928 VTOY_JSON_FMT_OBJ_ENDEX();
930 VTOY_JSON_FMT_ARY_ENDEX();
932 VTOY_JSON_FMT_OBJ_END();
933 VTOY_JSON_FMT_END(pos
);
938 static int ventoy_api_get_theme(struct mg_connection
*conn
, VTOY_JSON
*json
)
940 api_get_func(conn
, json
, theme
);
944 static int ventoy_api_save_theme(struct mg_connection
*conn
, VTOY_JSON
*json
)
948 data_theme
*data
= NULL
;
950 vtoy_json_get_int(json
, "index", &index
);
951 data
= g_data_theme
+ index
;
953 VTOY_JSON_INT("default_file", data
->default_file
);
954 VTOY_JSON_INT("resolution_fit", data
->resolution_fit
);
955 VTOY_JSON_INT("display_mode", data
->display_mode
);
956 VTOY_JSON_STR("gfxmode", data
->gfxmode
);
957 VTOY_JSON_STR("ventoy_left", data
->ventoy_left
);
958 VTOY_JSON_STR("ventoy_top", data
->ventoy_top
);
959 VTOY_JSON_STR("ventoy_color", data
->ventoy_color
);
961 ret
= ventoy_data_save_all();
963 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
968 static int ventoy_api_theme_add_file(struct mg_connection
*conn
, VTOY_JSON
*json
)
972 const char *path
= NULL
;
973 path_node
*node
= NULL
;
974 path_node
*cur
= NULL
;
975 data_theme
*data
= NULL
;
977 vtoy_json_get_int(json
, "index", &index
);
978 data
= g_data_theme
+ index
;
980 path
= VTOY_JSON_STR_EX("path");
983 if (ventoy_is_real_exist_common(path
, data
->filelist
, path_node
))
985 ventoy_json_result(conn
, VTOY_JSON_DUPLICATE
);
989 node
= zalloc(sizeof(path_node
));
992 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
994 vtoy_list_add(data
->filelist
, cur
, node
);
998 ret
= ventoy_data_save_all();
1000 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1004 static int ventoy_api_theme_del_file(struct mg_connection
*conn
, VTOY_JSON
*json
)
1008 const char *path
= NULL
;
1009 path_node
*node
= NULL
;
1010 path_node
*last
= NULL
;
1011 data_theme
*data
= NULL
;
1013 vtoy_json_get_int(json
, "index", &index
);
1014 data
= g_data_theme
+ index
;
1016 path
= VTOY_JSON_STR_EX("path");
1019 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
1021 vtoy_list_free(path_node
, data
->filelist
);
1025 vtoy_list_del(last
, node
, data
->filelist
, path
);
1029 ret
= ventoy_data_save_all();
1031 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1035 static int ventoy_api_theme_add_font(struct mg_connection
*conn
, VTOY_JSON
*json
)
1039 const char *path
= NULL
;
1040 path_node
*node
= NULL
;
1041 path_node
*cur
= NULL
;
1042 data_theme
*data
= NULL
;
1044 vtoy_json_get_int(json
, "index", &index
);
1045 data
= g_data_theme
+ index
;
1047 path
= VTOY_JSON_STR_EX("path");
1050 if (ventoy_is_real_exist_common(path
, data
->fontslist
, path_node
))
1052 ventoy_json_result(conn
, VTOY_JSON_DUPLICATE
);
1056 node
= zalloc(sizeof(path_node
));
1059 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
1060 vtoy_list_add(data
->fontslist
, cur
, node
);
1064 ret
= ventoy_data_save_all();
1066 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1071 static int ventoy_api_theme_del_font(struct mg_connection
*conn
, VTOY_JSON
*json
)
1075 const char *path
= NULL
;
1076 path_node
*node
= NULL
;
1077 path_node
*last
= NULL
;
1078 data_theme
*data
= NULL
;
1080 vtoy_json_get_int(json
, "index", &index
);
1081 data
= g_data_theme
+ index
;
1083 path
= VTOY_JSON_STR_EX("path");
1086 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
1088 vtoy_list_free(path_node
, data
->fontslist
);
1092 vtoy_list_del(last
, node
, data
->fontslist
, path
);
1096 ret
= ventoy_data_save_all();
1098 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1105 void ventoy_data_default_menu_alias(data_alias
*data
)
1107 memset(data
, 0, sizeof(data_alias
));
1110 int ventoy_data_cmp_menu_alias(data_alias
*data1
, data_alias
*data2
)
1112 data_alias_node
*list1
= NULL
;
1113 data_alias_node
*list2
= NULL
;
1115 if (NULL
== data1
->list
&& NULL
== data2
->list
)
1119 else if (data1
->list
&& data2
->list
)
1121 list1
= data1
->list
;
1122 list2
= data2
->list
;
1124 while (list1
&& list2
)
1126 if ((list1
->type
!= list2
->type
) ||
1127 strcmp(list1
->path
, list2
->path
) ||
1128 strcmp(list1
->alias
, list2
->alias
))
1133 list1
= list1
->next
;
1134 list2
= list2
->next
;
1137 if (list1
== NULL
&& list2
== NULL
)
1152 int ventoy_data_save_menu_alias(data_alias
*data
, const char *title
, char *buf
, int buflen
)
1155 data_alias_node
*node
= NULL
;
1157 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1159 VTOY_JSON_FMT_KEY_L(L1
, title
);
1160 VTOY_JSON_FMT_ARY_BEGIN_N();
1162 for (node
= data
->list
; node
; node
= node
->next
)
1164 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
1166 if (node
->type
== path_type_file
)
1168 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "image", node
->path
);
1172 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "dir", node
->path
);
1175 VTOY_JSON_FMT_STRN_EX_LN(L3
, "alias", node
->alias
);
1177 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
1180 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
1181 VTOY_JSON_FMT_END(pos
);
1187 int ventoy_data_json_menu_alias(data_alias
*data
, char *buf
, int buflen
)
1191 data_alias_node
*node
= NULL
;
1193 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1194 VTOY_JSON_FMT_ARY_BEGIN();
1196 for (node
= data
->list
; node
; node
= node
->next
)
1198 VTOY_JSON_FMT_OBJ_BEGIN();
1200 VTOY_JSON_FMT_UINT("type", node
->type
);
1201 VTOY_JSON_FMT_STRN("path", node
->path
);
1202 if (node
->type
== path_type_file
)
1204 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
1208 valid
= ventoy_is_directory_exist("%s%s", g_cur_dir
, node
->path
);
1211 VTOY_JSON_FMT_SINT("valid", valid
);
1212 VTOY_JSON_FMT_STRN("alias", node
->alias
);
1214 VTOY_JSON_FMT_OBJ_ENDEX();
1217 VTOY_JSON_FMT_ARY_END();
1218 VTOY_JSON_FMT_END(pos
);
1223 static int ventoy_api_get_alias(struct mg_connection
*conn
, VTOY_JSON
*json
)
1225 api_get_func(conn
, json
, menu_alias
);
1229 static int ventoy_api_save_alias(struct mg_connection
*conn
, VTOY_JSON
*json
)
1232 ret
= ventoy_data_save_all();
1234 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1238 static int ventoy_api_alias_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
1242 int type
= path_type_file
;
1243 const char *path
= NULL
;
1244 const char *alias
= NULL
;
1245 data_alias_node
*node
= NULL
;
1246 data_alias_node
*cur
= NULL
;
1247 data_alias
*data
= NULL
;
1249 vtoy_json_get_int(json
, "index", &index
);
1250 data
= g_data_menu_alias
+ index
;
1252 vtoy_json_get_int(json
, "type", &type
);
1254 path
= VTOY_JSON_STR_EX("path");
1255 alias
= VTOY_JSON_STR_EX("alias");
1258 if (ventoy_is_real_exist_common(path
, data
->list
, data_alias_node
))
1260 ventoy_json_result(conn
, VTOY_JSON_DUPLICATE
);
1264 node
= zalloc(sizeof(data_alias_node
));
1268 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
1269 scnprintf(node
->alias
, sizeof(node
->alias
), "%s", alias
);
1271 vtoy_list_add(data
->list
, cur
, node
);
1275 ret
= ventoy_data_save_all();
1277 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1281 static int ventoy_api_alias_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
1285 const char *path
= NULL
;
1286 data_alias_node
*last
= NULL
;
1287 data_alias_node
*node
= NULL
;
1288 data_alias
*data
= NULL
;
1290 vtoy_json_get_int(json
, "index", &index
);
1291 data
= g_data_menu_alias
+ index
;
1293 path
= VTOY_JSON_STR_EX("path");
1296 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
1298 vtoy_list_free(data_alias_node
, data
->list
);
1302 vtoy_list_del(last
, node
, data
->list
, path
);
1306 ret
= ventoy_data_save_all();
1308 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1315 void ventoy_data_default_menu_tip(data_tip
*data
)
1317 memset(data
, 0, sizeof(data_tip
));
1319 scnprintf(data
->left
, sizeof(data
->left
), "10%%");
1320 scnprintf(data
->top
, sizeof(data
->top
), "81%%");
1321 scnprintf(data
->color
, sizeof(data
->color
), "%s", "blue");
1324 int ventoy_data_cmp_menu_tip(data_tip
*data1
, data_tip
*data2
)
1326 data_tip_node
*list1
= NULL
;
1327 data_tip_node
*list2
= NULL
;
1329 if (strcmp(data1
->left
, data2
->left
) || strcmp(data1
->top
, data2
->top
) || strcmp(data1
->color
, data2
->color
))
1334 if (NULL
== data1
->list
&& NULL
== data2
->list
)
1338 else if (data1
->list
&& data2
->list
)
1340 list1
= data1
->list
;
1341 list2
= data2
->list
;
1343 while (list1
&& list2
)
1345 if ((list1
->type
!= list2
->type
) ||
1346 strcmp(list1
->path
, list2
->path
) ||
1347 strcmp(list1
->tip
, list2
->tip
))
1352 list1
= list1
->next
;
1353 list2
= list2
->next
;
1356 if (list1
== NULL
&& list2
== NULL
)
1371 int ventoy_data_save_menu_tip(data_tip
*data
, const char *title
, char *buf
, int buflen
)
1374 data_tip_node
*node
= NULL
;
1375 data_tip
*def
= g_data_menu_tip
+ bios_max
;
1377 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1378 VTOY_JSON_FMT_KEY_L(L1
, title
);
1379 VTOY_JSON_FMT_OBJ_BEGIN_N();
1381 VTOY_JSON_FMT_DIFF_STRN(L2
, "left", left
);
1382 VTOY_JSON_FMT_DIFF_STRN(L2
, "top", top
);
1383 VTOY_JSON_FMT_DIFF_STRN(L2
, "color", color
);
1387 VTOY_JSON_FMT_KEY_L(L2
, "tips");
1388 VTOY_JSON_FMT_ARY_BEGIN_N();
1390 for (node
= data
->list
; node
; node
= node
->next
)
1392 VTOY_JSON_FMT_OBJ_BEGIN_LN(L3
);
1394 if (node
->type
== path_type_file
)
1396 VTOY_JSON_FMT_STRN_PATH_LN(L4
, "image", node
->path
);
1400 VTOY_JSON_FMT_STRN_PATH_LN(L4
, "dir", node
->path
);
1402 VTOY_JSON_FMT_STRN_EX_LN(L4
, "tip", node
->tip
);
1404 VTOY_JSON_FMT_OBJ_ENDEX_LN(L3
);
1407 VTOY_JSON_FMT_ARY_ENDEX_LN(L2
);
1410 VTOY_JSON_FMT_OBJ_ENDEX_LN(L1
);
1412 VTOY_JSON_FMT_END(pos
);
1418 int ventoy_data_json_menu_tip(data_tip
*data
, char *buf
, int buflen
)
1422 data_tip_node
*node
= NULL
;
1424 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1426 VTOY_JSON_FMT_OBJ_BEGIN();
1428 VTOY_JSON_FMT_STRN("left", data
->left
);
1429 VTOY_JSON_FMT_STRN("top", data
->top
);
1430 VTOY_JSON_FMT_STRN("color", data
->color
);
1432 VTOY_JSON_FMT_KEY("tips");
1433 VTOY_JSON_FMT_ARY_BEGIN();
1435 for (node
= data
->list
; node
; node
= node
->next
)
1437 VTOY_JSON_FMT_OBJ_BEGIN();
1439 VTOY_JSON_FMT_UINT("type", node
->type
);
1440 VTOY_JSON_FMT_STRN("path", node
->path
);
1441 if (node
->type
== path_type_file
)
1443 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
1447 valid
= ventoy_is_directory_exist("%s%s", g_cur_dir
, node
->path
);
1450 VTOY_JSON_FMT_SINT("valid", valid
);
1451 VTOY_JSON_FMT_STRN("tip", node
->tip
);
1453 VTOY_JSON_FMT_OBJ_ENDEX();
1456 VTOY_JSON_FMT_ARY_ENDEX();
1458 VTOY_JSON_FMT_OBJ_END();
1459 VTOY_JSON_FMT_END(pos
);
1464 static int ventoy_api_get_tip(struct mg_connection
*conn
, VTOY_JSON
*json
)
1466 api_get_func(conn
, json
, menu_tip
);
1470 static int ventoy_api_save_tip(struct mg_connection
*conn
, VTOY_JSON
*json
)
1474 data_tip
*data
= NULL
;
1476 vtoy_json_get_int(json
, "index", &index
);
1477 data
= g_data_menu_tip
+ index
;
1479 VTOY_JSON_STR("left", data
->left
);
1480 VTOY_JSON_STR("top", data
->top
);
1481 VTOY_JSON_STR("color", data
->color
);
1483 ret
= ventoy_data_save_all();
1485 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1489 static int ventoy_api_tip_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
1493 int type
= path_type_file
;
1494 const char *path
= NULL
;
1495 const char *tip
= NULL
;
1496 data_tip_node
*node
= NULL
;
1497 data_tip_node
*cur
= NULL
;
1498 data_tip
*data
= NULL
;
1500 vtoy_json_get_int(json
, "index", &index
);
1501 data
= g_data_menu_tip
+ index
;
1503 vtoy_json_get_int(json
, "type", &type
);
1505 path
= VTOY_JSON_STR_EX("path");
1506 tip
= VTOY_JSON_STR_EX("tip");
1509 if (ventoy_is_real_exist_common(path
, data
->list
, data_tip_node
))
1511 ventoy_json_result(conn
, VTOY_JSON_DUPLICATE
);
1515 node
= zalloc(sizeof(data_tip_node
));
1519 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
1520 scnprintf(node
->tip
, sizeof(node
->tip
), "%s", tip
);
1522 vtoy_list_add(data
->list
, cur
, node
);
1526 ret
= ventoy_data_save_all();
1528 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1532 static int ventoy_api_tip_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
1536 const char *path
= NULL
;
1537 data_tip_node
*last
= NULL
;
1538 data_tip_node
*node
= NULL
;
1539 data_tip
*data
= NULL
;
1541 vtoy_json_get_int(json
, "index", &index
);
1542 data
= g_data_menu_tip
+ index
;
1544 path
= VTOY_JSON_STR_EX("path");
1547 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
1549 vtoy_list_free(data_tip_node
, data
->list
);
1553 vtoy_list_del(last
, node
, data
->list
, path
);
1557 ret
= ventoy_data_save_all();
1559 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1566 void ventoy_data_default_menu_class(data_class
*data
)
1568 memset(data
, 0, sizeof(data_class
));
1571 int ventoy_data_cmp_menu_class(data_class
*data1
, data_class
*data2
)
1573 data_class_node
*list1
= NULL
;
1574 data_class_node
*list2
= NULL
;
1576 if (NULL
== data1
->list
&& NULL
== data2
->list
)
1580 else if (data1
->list
&& data2
->list
)
1582 list1
= data1
->list
;
1583 list2
= data2
->list
;
1585 while (list1
&& list2
)
1587 if ((list1
->type
!= list2
->type
) ||
1588 strcmp(list1
->path
, list2
->path
) ||
1589 strcmp(list1
->class, list2
->class))
1594 list1
= list1
->next
;
1595 list2
= list2
->next
;
1598 if (list1
== NULL
&& list2
== NULL
)
1613 int ventoy_data_save_menu_class(data_class
*data
, const char *title
, char *buf
, int buflen
)
1616 data_class_node
*node
= NULL
;
1618 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1620 VTOY_JSON_FMT_KEY_L(L1
, title
);
1621 VTOY_JSON_FMT_ARY_BEGIN_N();
1623 for (node
= data
->list
; node
; node
= node
->next
)
1625 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
1627 if (node
->type
== class_type_key
)
1629 VTOY_JSON_FMT_STRN_LN(L3
, "key", node
->path
);
1631 else if (node
->type
== class_type_dir
)
1633 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "dir", node
->path
);
1637 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "parent", node
->path
);
1639 VTOY_JSON_FMT_STRN_LN(L3
, "class", node
->class);
1641 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
1644 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
1645 VTOY_JSON_FMT_END(pos
);
1651 int ventoy_data_json_menu_class(data_class
*data
, char *buf
, int buflen
)
1655 data_class_node
*node
= NULL
;
1657 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1658 VTOY_JSON_FMT_ARY_BEGIN();
1660 for (node
= data
->list
; node
; node
= node
->next
)
1662 VTOY_JSON_FMT_OBJ_BEGIN();
1664 VTOY_JSON_FMT_UINT("type", node
->type
);
1665 VTOY_JSON_FMT_STRN("path", node
->path
);
1667 if (node
->type
== class_type_key
)
1673 valid
= ventoy_is_directory_exist("%s%s", g_cur_dir
, node
->path
);
1675 VTOY_JSON_FMT_SINT("valid", valid
);
1677 VTOY_JSON_FMT_STRN("class", node
->class);
1679 VTOY_JSON_FMT_OBJ_ENDEX();
1682 VTOY_JSON_FMT_ARY_END();
1683 VTOY_JSON_FMT_END(pos
);
1689 static int ventoy_api_get_class(struct mg_connection
*conn
, VTOY_JSON
*json
)
1691 api_get_func(conn
, json
, menu_class
);
1695 static int ventoy_api_save_class(struct mg_connection
*conn
, VTOY_JSON
*json
)
1698 ret
= ventoy_data_save_all();
1700 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1704 static int ventoy_api_class_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
1708 int type
= class_type_key
;
1709 const char *path
= NULL
;
1710 const char *class = NULL
;
1711 data_class_node
*node
= NULL
;
1712 data_class_node
*cur
= NULL
;
1713 data_class
*data
= NULL
;
1715 vtoy_json_get_int(json
, "index", &index
);
1716 data
= g_data_menu_class
+ index
;
1718 vtoy_json_get_int(json
, "type", &type
);
1720 path
= VTOY_JSON_STR_EX("path");
1721 class = VTOY_JSON_STR_EX("class");
1724 node
= zalloc(sizeof(data_class_node
));
1729 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
1730 scnprintf(node
->class, sizeof(node
->class), "%s", class);
1732 vtoy_list_add(data
->list
, cur
, node
);
1736 ret
= ventoy_data_save_all();
1738 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1742 static int ventoy_api_class_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
1746 const char *path
= NULL
;
1747 data_class_node
*last
= NULL
;
1748 data_class_node
*node
= NULL
;
1749 data_class
*data
= NULL
;
1751 vtoy_json_get_int(json
, "index", &index
);
1752 data
= g_data_menu_class
+ index
;
1754 path
= VTOY_JSON_STR_EX("path");
1757 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
1759 vtoy_list_free(data_class_node
, data
->list
);
1763 vtoy_list_del(last
, node
, data
->list
, path
);
1767 ret
= ventoy_data_save_all();
1769 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1776 void ventoy_data_default_auto_memdisk(data_auto_memdisk
*data
)
1778 memset(data
, 0, sizeof(data_auto_memdisk
));
1781 int ventoy_data_cmp_auto_memdisk(data_auto_memdisk
*data1
, data_auto_memdisk
*data2
)
1783 return ventoy_path_list_cmp(data1
->list
, data2
->list
);
1786 int ventoy_data_save_auto_memdisk(data_auto_memdisk
*data
, const char *title
, char *buf
, int buflen
)
1789 path_node
*node
= NULL
;
1791 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1793 VTOY_JSON_FMT_KEY_L(L1
, title
);
1794 VTOY_JSON_FMT_ARY_BEGIN_N();
1796 for (node
= data
->list
; node
; node
= node
->next
)
1798 VTOY_JSON_FMT_ITEM_PATH_LN(L2
, node
->path
);
1801 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
1802 VTOY_JSON_FMT_END(pos
);
1807 int ventoy_data_json_auto_memdisk(data_auto_memdisk
*data
, char *buf
, int buflen
)
1811 path_node
*node
= NULL
;
1813 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1814 VTOY_JSON_FMT_ARY_BEGIN();
1816 for (node
= data
->list
; node
; node
= node
->next
)
1818 VTOY_JSON_FMT_OBJ_BEGIN();
1820 VTOY_JSON_FMT_STRN("path", node
->path
);
1821 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
1822 VTOY_JSON_FMT_SINT("valid", valid
);
1824 VTOY_JSON_FMT_OBJ_ENDEX();
1827 VTOY_JSON_FMT_ARY_END();
1828 VTOY_JSON_FMT_END(pos
);
1833 static int ventoy_api_get_auto_memdisk(struct mg_connection
*conn
, VTOY_JSON
*json
)
1835 api_get_func(conn
, json
, auto_memdisk
);
1839 static int ventoy_api_save_auto_memdisk(struct mg_connection
*conn
, VTOY_JSON
*json
)
1843 ret
= ventoy_data_save_all();
1845 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1849 static int ventoy_api_auto_memdisk_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
1853 const char *path
= NULL
;
1854 path_node
*node
= NULL
;
1855 path_node
*cur
= NULL
;
1856 data_auto_memdisk
*data
= NULL
;
1858 vtoy_json_get_int(json
, "index", &index
);
1859 data
= g_data_auto_memdisk
+ index
;
1861 path
= VTOY_JSON_STR_EX("path");
1864 if (ventoy_is_real_exist_common(path
, data
->list
, path_node
))
1866 ventoy_json_result(conn
, VTOY_JSON_DUPLICATE
);
1870 node
= zalloc(sizeof(path_node
));
1873 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
1874 vtoy_list_add(data
->list
, cur
, node
);
1878 ret
= ventoy_data_save_all();
1880 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1884 static int ventoy_api_auto_memdisk_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
1888 const char *path
= NULL
;
1889 path_node
*last
= NULL
;
1890 path_node
*node
= NULL
;
1891 data_auto_memdisk
*data
= NULL
;
1893 vtoy_json_get_int(json
, "index", &index
);
1894 data
= g_data_auto_memdisk
+ index
;
1896 path
= VTOY_JSON_STR_EX("path");
1899 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
1901 vtoy_list_free(path_node
, data
->list
);
1905 vtoy_list_del(last
, node
, data
->list
, path
);
1909 ret
= ventoy_data_save_all();
1911 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1918 void ventoy_data_default_image_list(data_image_list
*data
)
1920 memset(data
, 0, sizeof(data_image_list
));
1923 int ventoy_data_cmp_image_list(data_image_list
*data1
, data_image_list
*data2
)
1925 if (data1
->type
!= data2
->type
)
1927 if (data1
->list
|| data2
->list
)
1937 return ventoy_path_list_cmp(data1
->list
, data2
->list
);
1940 int ventoy_data_save_image_list(data_image_list
*data
, const char *title
, char *buf
, int buflen
)
1944 path_node
*node
= NULL
;
1954 prelen
= (int)strlen("image_list");
1956 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1958 if (data
->type
== 0)
1960 scnprintf(newtitle
, sizeof(newtitle
), "image_list%s", title
+ prelen
);
1964 scnprintf(newtitle
, sizeof(newtitle
), "image_blacklist%s", title
+ prelen
);
1966 VTOY_JSON_FMT_KEY_L(L1
, newtitle
);
1968 VTOY_JSON_FMT_ARY_BEGIN_N();
1970 for (node
= data
->list
; node
; node
= node
->next
)
1972 VTOY_JSON_FMT_ITEM_PATH_LN(L2
, node
->path
);
1975 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
1976 VTOY_JSON_FMT_END(pos
);
1981 int ventoy_data_json_image_list(data_image_list
*data
, char *buf
, int buflen
)
1985 path_node
*node
= NULL
;
1987 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1989 VTOY_JSON_FMT_OBJ_BEGIN();
1990 VTOY_JSON_FMT_SINT("type", data
->type
);
1992 VTOY_JSON_FMT_KEY("list");
1993 VTOY_JSON_FMT_ARY_BEGIN();
1995 for (node
= data
->list
; node
; node
= node
->next
)
1997 VTOY_JSON_FMT_OBJ_BEGIN();
1999 VTOY_JSON_FMT_STRN("path", node
->path
);
2000 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
2001 VTOY_JSON_FMT_SINT("valid", valid
);
2003 VTOY_JSON_FMT_OBJ_ENDEX();
2006 VTOY_JSON_FMT_ARY_ENDEX();
2007 VTOY_JSON_FMT_OBJ_END();
2009 VTOY_JSON_FMT_END(pos
);
2014 static int ventoy_api_get_image_list(struct mg_connection
*conn
, VTOY_JSON
*json
)
2016 api_get_func(conn
, json
, image_list
);
2020 static int ventoy_api_save_image_list(struct mg_connection
*conn
, VTOY_JSON
*json
)
2024 data_image_list
*data
= NULL
;
2026 vtoy_json_get_int(json
, "index", &index
);
2027 data
= g_data_image_list
+ index
;
2029 VTOY_JSON_INT("type", data
->type
);
2031 ret
= ventoy_data_save_all();
2033 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2037 static int ventoy_api_image_list_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
2041 const char *path
= NULL
;
2042 path_node
*node
= NULL
;
2043 path_node
*cur
= NULL
;
2044 data_image_list
*data
= NULL
;
2046 vtoy_json_get_int(json
, "index", &index
);
2047 data
= g_data_image_list
+ index
;
2049 path
= VTOY_JSON_STR_EX("path");
2052 if (ventoy_is_real_exist_common(path
, data
->list
, path_node
))
2054 ventoy_json_result(conn
, VTOY_JSON_DUPLICATE
);
2058 node
= zalloc(sizeof(path_node
));
2061 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
2062 vtoy_list_add(data
->list
, cur
, node
);
2066 ret
= ventoy_data_save_all();
2068 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2072 static int ventoy_api_image_list_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
2076 const char *path
= NULL
;
2077 path_node
*last
= NULL
;
2078 path_node
*node
= NULL
;
2079 data_image_list
*data
= NULL
;
2081 vtoy_json_get_int(json
, "index", &index
);
2082 data
= g_data_image_list
+ index
;
2084 path
= VTOY_JSON_STR_EX("path");
2087 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
2089 vtoy_list_free(path_node
, data
->list
);
2093 vtoy_list_del(last
, node
, data
->list
, path
);
2097 ret
= ventoy_data_save_all();
2099 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2106 void ventoy_data_default_password(data_password
*data
)
2108 memset(data
, 0, sizeof(data_password
));
2111 int ventoy_data_cmp_password(data_password
*data1
, data_password
*data2
)
2113 menu_password
*list1
= NULL
;
2114 menu_password
*list2
= NULL
;
2116 if (strcmp(data1
->bootpwd
, data2
->bootpwd
) ||
2117 strcmp(data1
->isopwd
, data2
->isopwd
) ||
2118 strcmp(data1
->wimpwd
, data2
->wimpwd
) ||
2119 strcmp(data1
->vhdpwd
, data2
->vhdpwd
) ||
2120 strcmp(data1
->imgpwd
, data2
->imgpwd
) ||
2121 strcmp(data1
->efipwd
, data2
->efipwd
) ||
2122 strcmp(data1
->vtoypwd
, data2
->vtoypwd
)
2128 if (NULL
== data1
->list
&& NULL
== data2
->list
)
2132 else if (data1
->list
&& data2
->list
)
2134 list1
= data1
->list
;
2135 list2
= data2
->list
;
2137 while (list1
&& list2
)
2139 if ((list1
->type
!= list2
->type
) || strcmp(list1
->path
, list2
->path
))
2144 list1
= list1
->next
;
2145 list2
= list2
->next
;
2148 if (list1
== NULL
&& list2
== NULL
)
2163 int ventoy_data_save_password(data_password
*data
, const char *title
, char *buf
, int buflen
)
2166 menu_password
*node
= NULL
;
2167 data_password
*def
= g_data_password
+ bios_max
;
2169 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2170 VTOY_JSON_FMT_KEY_L(L1
, title
);
2171 VTOY_JSON_FMT_OBJ_BEGIN_N();
2173 VTOY_JSON_FMT_DIFF_STRN(L2
, "bootpwd", bootpwd
);
2174 VTOY_JSON_FMT_DIFF_STRN(L2
, "isopwd", isopwd
);
2175 VTOY_JSON_FMT_DIFF_STRN(L2
, "wimpwd", wimpwd
);
2176 VTOY_JSON_FMT_DIFF_STRN(L2
, "vhdpwd", vhdpwd
);
2177 VTOY_JSON_FMT_DIFF_STRN(L2
, "imgpwd", imgpwd
);
2178 VTOY_JSON_FMT_DIFF_STRN(L2
, "efipwd", efipwd
);
2179 VTOY_JSON_FMT_DIFF_STRN(L2
, "vtoypwd", vtoypwd
);
2183 VTOY_JSON_FMT_KEY_L(L2
, "menupwd");
2184 VTOY_JSON_FMT_ARY_BEGIN_N();
2186 for (node
= data
->list
; node
; node
= node
->next
)
2188 VTOY_JSON_FMT_OBJ_BEGIN_LN(L3
);
2190 if (node
->type
== 0)
2192 VTOY_JSON_FMT_STRN_PATH_LN(L4
, "file", node
->path
);
2196 VTOY_JSON_FMT_STRN_PATH_LN(L4
, "parent", node
->path
);
2198 VTOY_JSON_FMT_STRN_LN(L4
, "pwd", node
->pwd
);
2200 VTOY_JSON_FMT_OBJ_ENDEX_LN(L3
);
2203 VTOY_JSON_FMT_ARY_ENDEX_LN(L2
);
2206 VTOY_JSON_FMT_OBJ_ENDEX_LN(L1
);
2208 VTOY_JSON_FMT_END(pos
);
2214 int ventoy_data_json_password(data_password
*data
, char *buf
, int buflen
)
2218 menu_password
*node
= NULL
;
2220 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2222 VTOY_JSON_FMT_OBJ_BEGIN();
2224 VTOY_JSON_FMT_STRN("bootpwd", data
->bootpwd
);
2225 VTOY_JSON_FMT_STRN("isopwd", data
->isopwd
);
2226 VTOY_JSON_FMT_STRN("wimpwd", data
->wimpwd
);
2227 VTOY_JSON_FMT_STRN("vhdpwd", data
->vhdpwd
);
2228 VTOY_JSON_FMT_STRN("imgpwd", data
->imgpwd
);
2229 VTOY_JSON_FMT_STRN("efipwd", data
->efipwd
);
2230 VTOY_JSON_FMT_STRN("vtoypwd", data
->vtoypwd
);
2232 VTOY_JSON_FMT_KEY("list");
2233 VTOY_JSON_FMT_ARY_BEGIN();
2235 for (node
= data
->list
; node
; node
= node
->next
)
2237 VTOY_JSON_FMT_OBJ_BEGIN();
2239 VTOY_JSON_FMT_SINT("type", node
->type
);
2240 VTOY_JSON_FMT_STRN("path", node
->path
);
2241 if (node
->type
== path_type_file
)
2243 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
2247 valid
= ventoy_is_directory_exist("%s%s", g_cur_dir
, node
->path
);
2250 VTOY_JSON_FMT_SINT("valid", valid
);
2251 VTOY_JSON_FMT_STRN("pwd", node
->pwd
);
2253 VTOY_JSON_FMT_OBJ_ENDEX();
2256 VTOY_JSON_FMT_ARY_ENDEX();
2258 VTOY_JSON_FMT_OBJ_END();
2259 VTOY_JSON_FMT_END(pos
);
2264 static int ventoy_api_get_password(struct mg_connection
*conn
, VTOY_JSON
*json
)
2266 api_get_func(conn
, json
, password
);
2270 static int ventoy_api_save_password(struct mg_connection
*conn
, VTOY_JSON
*json
)
2274 data_password
*data
= NULL
;
2276 vtoy_json_get_int(json
, "index", &index
);
2277 data
= g_data_password
+ index
;
2279 VTOY_JSON_STR("bootpwd", data
->bootpwd
);
2280 VTOY_JSON_STR("isopwd", data
->isopwd
);
2281 VTOY_JSON_STR("wimpwd", data
->wimpwd
);
2282 VTOY_JSON_STR("vhdpwd", data
->vhdpwd
);
2283 VTOY_JSON_STR("imgpwd", data
->imgpwd
);
2284 VTOY_JSON_STR("efipwd", data
->efipwd
);
2285 VTOY_JSON_STR("vtoypwd", data
->vtoypwd
);
2287 ret
= ventoy_data_save_all();
2289 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2293 static int ventoy_api_password_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
2298 const char *path
= NULL
;
2299 const char *pwd
= NULL
;
2300 menu_password
*node
= NULL
;
2301 menu_password
*cur
= NULL
;
2302 data_password
*data
= NULL
;
2304 vtoy_json_get_int(json
, "index", &index
);
2305 data
= g_data_password
+ index
;
2307 vtoy_json_get_int(json
, "type", &type
);
2309 path
= VTOY_JSON_STR_EX("path");
2310 pwd
= VTOY_JSON_STR_EX("pwd");
2313 if (ventoy_is_real_exist_common(path
, data
->list
, menu_password
))
2315 ventoy_json_result(conn
, VTOY_JSON_DUPLICATE
);
2319 node
= zalloc(sizeof(menu_password
));
2323 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
2324 scnprintf(node
->pwd
, sizeof(node
->pwd
), "%s", pwd
);
2326 vtoy_list_add(data
->list
, cur
, node
);
2330 ret
= ventoy_data_save_all();
2332 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2336 static int ventoy_api_password_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
2340 const char *path
= NULL
;
2341 menu_password
*last
= NULL
;
2342 menu_password
*node
= NULL
;
2343 data_password
*data
= NULL
;
2345 vtoy_json_get_int(json
, "index", &index
);
2346 data
= g_data_password
+ index
;
2348 path
= VTOY_JSON_STR_EX("path");
2351 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
2353 vtoy_list_free(menu_password
, data
->list
);
2357 vtoy_list_del(last
, node
, data
->list
, path
);
2361 ret
= ventoy_data_save_all();
2363 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2370 void ventoy_data_default_conf_replace(data_conf_replace
*data
)
2372 memset(data
, 0, sizeof(data_conf_replace
));
2375 int ventoy_data_cmp_conf_replace(data_conf_replace
*data1
, data_conf_replace
*data2
)
2377 conf_replace_node
*list1
= NULL
;
2378 conf_replace_node
*list2
= NULL
;
2380 if (NULL
== data1
->list
&& NULL
== data2
->list
)
2384 else if (data1
->list
&& data2
->list
)
2386 list1
= data1
->list
;
2387 list2
= data2
->list
;
2389 while (list1
&& list2
)
2391 if (list1
->image
!= list2
->image
||
2392 strcmp(list1
->path
, list2
->path
) ||
2393 strcmp(list1
->org
, list2
->org
) ||
2394 strcmp(list1
->new, list2
->new)
2400 list1
= list1
->next
;
2401 list2
= list2
->next
;
2404 if (list1
== NULL
&& list2
== NULL
)
2419 int ventoy_data_save_conf_replace(data_conf_replace
*data
, const char *title
, char *buf
, int buflen
)
2422 conf_replace_node
*node
= NULL
;
2424 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2426 VTOY_JSON_FMT_KEY_L(L1
, title
);
2427 VTOY_JSON_FMT_ARY_BEGIN_N();
2429 for (node
= data
->list
; node
; node
= node
->next
)
2431 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
2433 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "iso", node
->path
);
2434 VTOY_JSON_FMT_STRN_LN(L3
, "org", node
->org
);
2435 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "new", node
->new);
2438 VTOY_JSON_FMT_SINT_LN(L3
, "img", node
->image
);
2441 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
2444 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
2445 VTOY_JSON_FMT_END(pos
);
2451 int ventoy_data_json_conf_replace(data_conf_replace
*data
, char *buf
, int buflen
)
2454 conf_replace_node
*node
= NULL
;
2456 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2457 VTOY_JSON_FMT_ARY_BEGIN();
2459 for (node
= data
->list
; node
; node
= node
->next
)
2461 VTOY_JSON_FMT_OBJ_BEGIN();
2463 VTOY_JSON_FMT_STRN("path", node
->path
);
2464 VTOY_JSON_FMT_SINT("valid", ventoy_check_fuzzy_path(node
->path
, 1));
2465 VTOY_JSON_FMT_STRN("org", node
->org
);
2466 VTOY_JSON_FMT_STRN("new", node
->new);
2467 VTOY_JSON_FMT_SINT("new_valid", ventoy_is_file_exist("%s%s", g_cur_dir
, node
->new));
2468 VTOY_JSON_FMT_SINT("img", node
->image
);
2470 VTOY_JSON_FMT_OBJ_ENDEX();
2473 VTOY_JSON_FMT_ARY_END();
2474 VTOY_JSON_FMT_END(pos
);
2479 static int ventoy_api_get_conf_replace(struct mg_connection
*conn
, VTOY_JSON
*json
)
2481 api_get_func(conn
, json
, conf_replace
);
2485 static int ventoy_api_save_conf_replace(struct mg_connection
*conn
, VTOY_JSON
*json
)
2488 ret
= ventoy_data_save_all();
2490 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2494 static int ventoy_api_conf_replace_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
2499 const char *path
= NULL
;
2500 const char *org
= NULL
;
2501 const char *new = NULL
;
2502 conf_replace_node
*node
= NULL
;
2503 conf_replace_node
*cur
= NULL
;
2504 data_conf_replace
*data
= NULL
;
2506 vtoy_json_get_int(json
, "img", &image
);
2508 vtoy_json_get_int(json
, "index", &index
);
2509 data
= g_data_conf_replace
+ index
;
2511 path
= VTOY_JSON_STR_EX("path");
2512 org
= VTOY_JSON_STR_EX("org");
2513 new = VTOY_JSON_STR_EX("new");
2514 if (path
&& org
&& new)
2516 node
= zalloc(sizeof(conf_replace_node
));
2519 node
->image
= image
;
2520 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
2521 scnprintf(node
->org
, sizeof(node
->org
), "%s", org
);
2522 scnprintf(node
->new, sizeof(node
->new), "%s", new);
2524 vtoy_list_add(data
->list
, cur
, node
);
2528 ret
= ventoy_data_save_all();
2530 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2534 static int ventoy_api_conf_replace_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
2538 const char *path
= NULL
;
2539 conf_replace_node
*last
= NULL
;
2540 conf_replace_node
*node
= NULL
;
2541 data_conf_replace
*data
= NULL
;
2543 vtoy_json_get_int(json
, "index", &index
);
2544 data
= g_data_conf_replace
+ index
;
2546 path
= VTOY_JSON_STR_EX("path");
2549 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
2551 vtoy_list_free(conf_replace_node
, data
->list
);
2555 vtoy_list_del(last
, node
, data
->list
, path
);
2559 ret
= ventoy_data_save_all();
2561 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2569 void ventoy_data_default_dud(data_dud
*data
)
2571 memset(data
, 0, sizeof(data_dud
));
2574 int ventoy_data_cmp_dud(data_dud
*data1
, data_dud
*data2
)
2576 dud_node
*list1
= NULL
;
2577 dud_node
*list2
= NULL
;
2579 if (NULL
== data1
->list
&& NULL
== data2
->list
)
2583 else if (data1
->list
&& data2
->list
)
2585 list1
= data1
->list
;
2586 list2
= data2
->list
;
2588 while (list1
&& list2
)
2590 if (strcmp(list1
->path
, list2
->path
))
2595 /* no need to compare dud list with default */
2596 list1
= list1
->next
;
2597 list2
= list2
->next
;
2600 if (list1
== NULL
&& list2
== NULL
)
2615 int ventoy_data_save_dud(data_dud
*data
, const char *title
, char *buf
, int buflen
)
2618 dud_node
*node
= NULL
;
2619 path_node
*pathnode
= NULL
;
2621 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2623 VTOY_JSON_FMT_KEY_L(L1
, title
);
2624 VTOY_JSON_FMT_ARY_BEGIN_N();
2626 for (node
= data
->list
; node
; node
= node
->next
)
2628 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
2629 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "image", node
->path
);
2631 VTOY_JSON_FMT_KEY_L(L3
, "dud");
2632 VTOY_JSON_FMT_ARY_BEGIN_N();
2633 for (pathnode
= node
->list
; pathnode
; pathnode
= pathnode
->next
)
2635 VTOY_JSON_FMT_ITEM_PATH_LN(L4
, pathnode
->path
);
2637 VTOY_JSON_FMT_ARY_ENDEX_LN(L3
);
2639 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
2642 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
2643 VTOY_JSON_FMT_END(pos
);
2649 int ventoy_data_json_dud(data_dud
*data
, char *buf
, int buflen
)
2653 dud_node
*node
= NULL
;
2654 path_node
*pathnode
= NULL
;
2656 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2657 VTOY_JSON_FMT_ARY_BEGIN();
2659 for (node
= data
->list
; node
; node
= node
->next
)
2661 VTOY_JSON_FMT_OBJ_BEGIN();
2663 VTOY_JSON_FMT_STRN("path", node
->path
);
2664 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
2665 VTOY_JSON_FMT_SINT("valid", valid
);
2668 VTOY_JSON_FMT_KEY("list");
2669 VTOY_JSON_FMT_ARY_BEGIN();
2670 for (pathnode
= node
->list
; pathnode
; pathnode
= pathnode
->next
)
2672 VTOY_JSON_FMT_OBJ_BEGIN();
2673 VTOY_JSON_FMT_STRN("path", pathnode
->path
);
2675 valid
= ventoy_is_file_exist("%s%s", g_cur_dir
, pathnode
->path
);
2676 VTOY_JSON_FMT_SINT("valid", valid
);
2677 VTOY_JSON_FMT_OBJ_ENDEX();
2679 VTOY_JSON_FMT_ARY_ENDEX();
2682 VTOY_JSON_FMT_OBJ_ENDEX();
2685 VTOY_JSON_FMT_ARY_END();
2686 VTOY_JSON_FMT_END(pos
);
2691 static int ventoy_api_get_dud(struct mg_connection
*conn
, VTOY_JSON
*json
)
2693 api_get_func(conn
, json
, dud
);
2697 static int ventoy_api_save_dud(struct mg_connection
*conn
, VTOY_JSON
*json
)
2700 ret
= ventoy_data_save_all();
2702 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2707 static int ventoy_api_dud_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
2711 const char *path
= NULL
;
2712 dud_node
*node
= NULL
;
2713 dud_node
*cur
= NULL
;
2714 data_dud
*data
= NULL
;
2715 VTOY_JSON
*array
= NULL
;
2717 vtoy_json_get_int(json
, "index", &index
);
2718 data
= g_data_dud
+ index
;
2720 array
= vtoy_json_find_item(json
, JSON_TYPE_ARRAY
, "dud");
2721 path
= VTOY_JSON_STR_EX("path");
2724 if (ventoy_is_real_exist_common(path
, data
->list
, dud_node
))
2726 ventoy_json_result(conn
, VTOY_JSON_DUPLICATE
);
2730 node
= zalloc(sizeof(dud_node
));
2733 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
2734 node
->list
= ventoy_path_node_add_array(array
);
2736 vtoy_list_add(data
->list
, cur
, node
);
2740 ret
= ventoy_data_save_all();
2742 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2746 static int ventoy_api_dud_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
2750 const char *path
= NULL
;
2751 dud_node
*next
= NULL
;
2752 dud_node
*last
= NULL
;
2753 dud_node
*node
= NULL
;
2754 data_dud
*data
= NULL
;
2756 vtoy_json_get_int(json
, "index", &index
);
2757 data
= g_data_dud
+ index
;
2759 path
= VTOY_JSON_STR_EX("path");
2762 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
2764 for (node
= data
->list
; node
; node
= next
)
2767 ventoy_free_path_node_list(node
->list
);
2774 vtoy_list_del_ex(last
, node
, data
->list
, path
, ventoy_free_path_node_list
);
2778 ret
= ventoy_data_save_all();
2780 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2785 static int ventoy_api_dud_add_inner(struct mg_connection
*conn
, VTOY_JSON
*json
)
2789 const char *path
= NULL
;
2790 const char *outpath
= NULL
;
2791 path_node
*pcur
= NULL
;
2792 path_node
*pnode
= NULL
;
2793 dud_node
*node
= NULL
;
2794 data_dud
*data
= NULL
;
2796 vtoy_json_get_int(json
, "index", &index
);
2797 data
= g_data_dud
+ index
;
2799 path
= VTOY_JSON_STR_EX("path");
2800 outpath
= VTOY_JSON_STR_EX("outpath");
2801 if (path
&& outpath
)
2803 for (node
= data
->list
; node
; node
= node
->next
)
2805 if (strcmp(outpath
, node
->path
) == 0)
2807 pnode
= zalloc(sizeof(path_node
));
2810 scnprintf(pnode
->path
, sizeof(pnode
->path
), "%s", path
);
2811 vtoy_list_add(node
->list
, pcur
, pnode
);
2819 ret
= ventoy_data_save_all();
2821 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2825 static int ventoy_api_dud_del_inner(struct mg_connection
*conn
, VTOY_JSON
*json
)
2829 const char *path
= NULL
;
2830 const char *outpath
= NULL
;
2831 path_node
*plast
= NULL
;
2832 path_node
*pnode
= NULL
;
2833 dud_node
*node
= NULL
;
2834 data_dud
*data
= NULL
;
2836 vtoy_json_get_int(json
, "index", &index
);
2837 data
= g_data_dud
+ index
;
2839 path
= VTOY_JSON_STR_EX("path");
2840 outpath
= VTOY_JSON_STR_EX("outpath");
2841 if (path
&& outpath
)
2843 for (node
= data
->list
; node
; node
= node
->next
)
2845 if (strcmp(outpath
, node
->path
) == 0)
2847 vtoy_list_del(plast
, pnode
, node
->list
, path
);
2853 ret
= ventoy_data_save_all();
2855 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2863 void ventoy_data_default_auto_install(data_auto_install
*data
)
2865 memset(data
, 0, sizeof(data_auto_install
));
2868 int ventoy_data_cmp_auto_install(data_auto_install
*data1
, data_auto_install
*data2
)
2870 auto_install_node
*list1
= NULL
;
2871 auto_install_node
*list2
= NULL
;
2873 if (NULL
== data1
->list
&& NULL
== data2
->list
)
2877 else if (data1
->list
&& data2
->list
)
2879 list1
= data1
->list
;
2880 list2
= data2
->list
;
2882 while (list1
&& list2
)
2884 if (list1
->timeout
!= list2
->timeout
||
2885 list1
->autosel
!= list2
->autosel
||
2886 strcmp(list1
->path
, list2
->path
))
2891 /* no need to compare auto install list with default */
2892 list1
= list1
->next
;
2893 list2
= list2
->next
;
2896 if (list1
== NULL
&& list2
== NULL
)
2911 int ventoy_data_save_auto_install(data_auto_install
*data
, const char *title
, char *buf
, int buflen
)
2914 auto_install_node
*node
= NULL
;
2915 path_node
*pathnode
= NULL
;
2917 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2919 VTOY_JSON_FMT_KEY_L(L1
, title
);
2920 VTOY_JSON_FMT_ARY_BEGIN_N();
2922 for (node
= data
->list
; node
; node
= node
->next
)
2924 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
2925 if (node
->type
== 0)
2927 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "image", node
->path
);
2931 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "parent", node
->path
);
2935 VTOY_JSON_FMT_KEY_L(L3
, "template");
2936 VTOY_JSON_FMT_ARY_BEGIN_N();
2937 for (pathnode
= node
->list
; pathnode
; pathnode
= pathnode
->next
)
2939 VTOY_JSON_FMT_ITEM_PATH_LN(L4
, pathnode
->path
);
2941 VTOY_JSON_FMT_ARY_ENDEX_LN(L3
);
2943 if (node
->timeouten
)
2945 VTOY_JSON_FMT_SINT_LN(L3
, "timeout", node
->timeout
);
2948 if (node
->autoselen
)
2950 VTOY_JSON_FMT_SINT_LN(L3
, "autosel", node
->autosel
);
2953 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
2956 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
2957 VTOY_JSON_FMT_END(pos
);
2963 int ventoy_data_json_auto_install(data_auto_install
*data
, char *buf
, int buflen
)
2967 auto_install_node
*node
= NULL
;
2968 path_node
*pathnode
= NULL
;
2970 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2971 VTOY_JSON_FMT_ARY_BEGIN();
2973 for (node
= data
->list
; node
; node
= node
->next
)
2975 VTOY_JSON_FMT_OBJ_BEGIN();
2977 VTOY_JSON_FMT_STRN("path", node
->path
);
2979 if (node
->type
== 0)
2981 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
2985 valid
= ventoy_is_directory_exist("%s%s", g_cur_dir
, node
->path
);
2987 VTOY_JSON_FMT_SINT("valid", valid
);
2988 VTOY_JSON_FMT_SINT("type", node
->type
);
2990 VTOY_JSON_FMT_BOOL("timeouten", node
->timeouten
);
2991 VTOY_JSON_FMT_BOOL("autoselen", node
->autoselen
);
2993 VTOY_JSON_FMT_SINT("autosel", node
->autosel
);
2994 VTOY_JSON_FMT_SINT("timeout", node
->timeout
);
2996 VTOY_JSON_FMT_KEY("list");
2997 VTOY_JSON_FMT_ARY_BEGIN();
2998 for (pathnode
= node
->list
; pathnode
; pathnode
= pathnode
->next
)
3000 VTOY_JSON_FMT_OBJ_BEGIN();
3001 VTOY_JSON_FMT_STRN("path", pathnode
->path
);
3003 valid
= ventoy_is_file_exist("%s%s", g_cur_dir
, pathnode
->path
);
3004 VTOY_JSON_FMT_SINT("valid", valid
);
3005 VTOY_JSON_FMT_OBJ_ENDEX();
3007 VTOY_JSON_FMT_ARY_ENDEX();
3010 VTOY_JSON_FMT_OBJ_ENDEX();
3013 VTOY_JSON_FMT_ARY_END();
3014 VTOY_JSON_FMT_END(pos
);
3019 static int ventoy_api_get_auto_install(struct mg_connection
*conn
, VTOY_JSON
*json
)
3021 api_get_func(conn
, json
, auto_install
);
3025 static int ventoy_api_save_auto_install(struct mg_connection
*conn
, VTOY_JSON
*json
)
3031 uint8_t timeouten
= 0;
3032 uint8_t autoselen
= 0;
3033 auto_install_node
*node
= NULL
;
3034 data_auto_install
*data
= NULL
;
3036 vtoy_json_get_int(json
, "index", &index
);
3037 vtoy_json_get_int(json
, "id", &id
);
3039 vtoy_json_get_bool(json
, "timeouten", &timeouten
);
3040 vtoy_json_get_bool(json
, "autoselen", &autoselen
);
3042 data
= g_data_auto_install
+ index
;
3046 for (node
= data
->list
; node
; node
= node
->next
)
3050 node
->timeouten
= (int)timeouten
;
3051 node
->autoselen
= (int)autoselen
;
3052 VTOY_JSON_INT("timeout", node
->timeout
);
3053 VTOY_JSON_INT("autosel", node
->autosel
);
3059 ret
= ventoy_data_save_all();
3061 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3066 static int ventoy_api_auto_install_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
3071 const char *path
= NULL
;
3072 auto_install_node
*node
= NULL
;
3073 auto_install_node
*cur
= NULL
;
3074 data_auto_install
*data
= NULL
;
3075 VTOY_JSON
*array
= NULL
;
3077 vtoy_json_get_int(json
, "type", &type
);
3078 vtoy_json_get_int(json
, "index", &index
);
3079 data
= g_data_auto_install
+ index
;
3081 array
= vtoy_json_find_item(json
, JSON_TYPE_ARRAY
, "template");
3082 path
= VTOY_JSON_STR_EX("path");
3085 if (ventoy_is_real_exist_common(path
, data
->list
, auto_install_node
))
3087 ventoy_json_result(conn
, VTOY_JSON_DUPLICATE
);
3091 node
= zalloc(sizeof(auto_install_node
));
3095 node
->timeouten
= 0;
3096 node
->autoselen
= 0;
3099 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
3100 node
->list
= ventoy_path_node_add_array(array
);
3102 vtoy_list_add(data
->list
, cur
, node
);
3106 ret
= ventoy_data_save_all();
3108 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3112 static int ventoy_api_auto_install_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
3116 const char *path
= NULL
;
3117 auto_install_node
*last
= NULL
;
3118 auto_install_node
*next
= NULL
;
3119 auto_install_node
*node
= NULL
;
3120 data_auto_install
*data
= NULL
;
3122 vtoy_json_get_int(json
, "index", &index
);
3123 data
= g_data_auto_install
+ index
;
3125 path
= VTOY_JSON_STR_EX("path");
3128 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
3130 for (node
= data
->list
; node
; node
= next
)
3133 ventoy_free_path_node_list(node
->list
);
3140 vtoy_list_del_ex(last
, node
, data
->list
, path
, ventoy_free_path_node_list
);
3144 ret
= ventoy_data_save_all();
3146 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3150 static int ventoy_api_auto_install_add_inner(struct mg_connection
*conn
, VTOY_JSON
*json
)
3154 const char *path
= NULL
;
3155 const char *outpath
= NULL
;
3156 path_node
*pcur
= NULL
;
3157 path_node
*pnode
= NULL
;
3158 auto_install_node
*node
= NULL
;
3159 data_auto_install
*data
= NULL
;
3161 vtoy_json_get_int(json
, "index", &index
);
3162 data
= g_data_auto_install
+ index
;
3164 path
= VTOY_JSON_STR_EX("path");
3165 outpath
= VTOY_JSON_STR_EX("outpath");
3166 if (path
&& outpath
)
3168 for (node
= data
->list
; node
; node
= node
->next
)
3170 if (strcmp(outpath
, node
->path
) == 0)
3172 pnode
= zalloc(sizeof(path_node
));
3175 scnprintf(pnode
->path
, sizeof(pnode
->path
), "%s", path
);
3176 vtoy_list_add(node
->list
, pcur
, pnode
);
3184 ret
= ventoy_data_save_all();
3186 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3190 static int ventoy_api_auto_install_del_inner(struct mg_connection
*conn
, VTOY_JSON
*json
)
3194 const char *path
= NULL
;
3195 const char *outpath
= NULL
;
3196 path_node
*plast
= NULL
;
3197 path_node
*pnode
= NULL
;
3198 auto_install_node
*node
= NULL
;
3199 data_auto_install
*data
= NULL
;
3201 vtoy_json_get_int(json
, "index", &index
);
3202 data
= g_data_auto_install
+ index
;
3204 path
= VTOY_JSON_STR_EX("path");
3205 outpath
= VTOY_JSON_STR_EX("outpath");
3206 if (path
&& outpath
)
3208 for (node
= data
->list
; node
; node
= node
->next
)
3210 if (strcmp(outpath
, node
->path
) == 0)
3212 vtoy_list_del(plast
, pnode
, node
->list
, path
);
3218 ret
= ventoy_data_save_all();
3220 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3229 void ventoy_data_default_persistence(data_persistence
*data
)
3231 memset(data
, 0, sizeof(data_persistence
));
3234 int ventoy_data_cmp_persistence(data_persistence
*data1
, data_persistence
*data2
)
3236 persistence_node
*list1
= NULL
;
3237 persistence_node
*list2
= NULL
;
3239 if (NULL
== data1
->list
&& NULL
== data2
->list
)
3243 else if (data1
->list
&& data2
->list
)
3245 list1
= data1
->list
;
3246 list2
= data2
->list
;
3248 while (list1
&& list2
)
3250 if (list1
->timeout
!= list2
->timeout
||
3251 list1
->autosel
!= list2
->autosel
||
3252 strcmp(list1
->path
, list2
->path
))
3257 /* no need to compare auto install list with default */
3258 list1
= list1
->next
;
3259 list2
= list2
->next
;
3262 if (list1
== NULL
&& list2
== NULL
)
3277 int ventoy_data_save_persistence(data_persistence
*data
, const char *title
, char *buf
, int buflen
)
3280 persistence_node
*node
= NULL
;
3281 path_node
*pathnode
= NULL
;
3283 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
3285 VTOY_JSON_FMT_KEY_L(L1
, title
);
3286 VTOY_JSON_FMT_ARY_BEGIN_N();
3288 for (node
= data
->list
; node
; node
= node
->next
)
3290 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
3291 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "image", node
->path
);
3292 VTOY_JSON_FMT_KEY_L(L3
, "backend");
3293 VTOY_JSON_FMT_ARY_BEGIN_N();
3294 for (pathnode
= node
->list
; pathnode
; pathnode
= pathnode
->next
)
3296 VTOY_JSON_FMT_ITEM_PATH_LN(L4
, pathnode
->path
);
3298 VTOY_JSON_FMT_ARY_ENDEX_LN(L3
);
3300 if (node
->timeouten
)
3302 VTOY_JSON_FMT_SINT_LN(L3
, "timeout", node
->timeout
);
3305 if (node
->autoselen
)
3307 VTOY_JSON_FMT_SINT_LN(L3
, "autosel", node
->autosel
);
3310 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
3313 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
3314 VTOY_JSON_FMT_END(pos
);
3320 int ventoy_data_json_persistence(data_persistence
*data
, char *buf
, int buflen
)
3324 persistence_node
*node
= NULL
;
3325 path_node
*pathnode
= NULL
;
3327 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
3328 VTOY_JSON_FMT_ARY_BEGIN();
3330 for (node
= data
->list
; node
; node
= node
->next
)
3332 VTOY_JSON_FMT_OBJ_BEGIN();
3334 VTOY_JSON_FMT_STRN("path", node
->path
);
3336 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
3337 VTOY_JSON_FMT_SINT("valid", valid
);
3338 VTOY_JSON_FMT_SINT("type", node
->type
);
3340 VTOY_JSON_FMT_BOOL("timeouten", node
->timeouten
);
3341 VTOY_JSON_FMT_BOOL("autoselen", node
->autoselen
);
3343 VTOY_JSON_FMT_SINT("autosel", node
->autosel
);
3344 VTOY_JSON_FMT_SINT("timeout", node
->timeout
);
3346 VTOY_JSON_FMT_KEY("list");
3347 VTOY_JSON_FMT_ARY_BEGIN();
3348 for (pathnode
= node
->list
; pathnode
; pathnode
= pathnode
->next
)
3350 VTOY_JSON_FMT_OBJ_BEGIN();
3351 VTOY_JSON_FMT_STRN("path", pathnode
->path
);
3353 valid
= ventoy_is_file_exist("%s%s", g_cur_dir
, pathnode
->path
);
3354 VTOY_JSON_FMT_SINT("valid", valid
);
3355 VTOY_JSON_FMT_OBJ_ENDEX();
3357 VTOY_JSON_FMT_ARY_ENDEX();
3360 VTOY_JSON_FMT_OBJ_ENDEX();
3363 VTOY_JSON_FMT_ARY_END();
3364 VTOY_JSON_FMT_END(pos
);
3369 static int ventoy_api_get_persistence(struct mg_connection
*conn
, VTOY_JSON
*json
)
3371 api_get_func(conn
, json
, persistence
);
3375 static int ventoy_api_save_persistence(struct mg_connection
*conn
, VTOY_JSON
*json
)
3381 uint8_t timeouten
= 0;
3382 uint8_t autoselen
= 0;
3383 persistence_node
*node
= NULL
;
3384 data_persistence
*data
= NULL
;
3386 vtoy_json_get_int(json
, "index", &index
);
3387 vtoy_json_get_int(json
, "id", &id
);
3389 vtoy_json_get_bool(json
, "timeouten", &timeouten
);
3390 vtoy_json_get_bool(json
, "autoselen", &autoselen
);
3392 data
= g_data_persistence
+ index
;
3396 for (node
= data
->list
; node
; node
= node
->next
)
3400 node
->timeouten
= (int)timeouten
;
3401 node
->autoselen
= (int)autoselen
;
3402 VTOY_JSON_INT("timeout", node
->timeout
);
3403 VTOY_JSON_INT("autosel", node
->autosel
);
3409 ret
= ventoy_data_save_all();
3411 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3416 static int ventoy_api_persistence_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
3420 const char *path
= NULL
;
3421 persistence_node
*node
= NULL
;
3422 persistence_node
*cur
= NULL
;
3423 data_persistence
*data
= NULL
;
3424 VTOY_JSON
*array
= NULL
;
3426 vtoy_json_get_int(json
, "index", &index
);
3427 data
= g_data_persistence
+ index
;
3429 array
= vtoy_json_find_item(json
, JSON_TYPE_ARRAY
, "backend");
3430 path
= VTOY_JSON_STR_EX("path");
3433 if (ventoy_is_real_exist_common(path
, data
->list
, persistence_node
))
3435 ventoy_json_result(conn
, VTOY_JSON_DUPLICATE
);
3439 node
= zalloc(sizeof(persistence_node
));
3442 node
->timeouten
= 0;
3443 node
->autoselen
= 0;
3446 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
3447 node
->list
= ventoy_path_node_add_array(array
);
3449 vtoy_list_add(data
->list
, cur
, node
);
3453 ret
= ventoy_data_save_all();
3455 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3459 static int ventoy_api_persistence_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
3463 const char *path
= NULL
;
3464 persistence_node
*last
= NULL
;
3465 persistence_node
*next
= NULL
;
3466 persistence_node
*node
= NULL
;
3467 data_persistence
*data
= NULL
;
3469 vtoy_json_get_int(json
, "index", &index
);
3470 data
= g_data_persistence
+ index
;
3472 path
= VTOY_JSON_STR_EX("path");
3475 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
3477 for (node
= data
->list
; node
; node
= next
)
3480 ventoy_free_path_node_list(node
->list
);
3487 vtoy_list_del_ex(last
, node
, data
->list
, path
, ventoy_free_path_node_list
);
3491 ret
= ventoy_data_save_all();
3493 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3497 static int ventoy_api_persistence_add_inner(struct mg_connection
*conn
, VTOY_JSON
*json
)
3501 const char *path
= NULL
;
3502 const char *outpath
= NULL
;
3503 path_node
*pcur
= NULL
;
3504 path_node
*pnode
= NULL
;
3505 persistence_node
*node
= NULL
;
3506 data_persistence
*data
= NULL
;
3508 vtoy_json_get_int(json
, "index", &index
);
3509 data
= g_data_persistence
+ index
;
3511 path
= VTOY_JSON_STR_EX("path");
3512 outpath
= VTOY_JSON_STR_EX("outpath");
3513 if (path
&& outpath
)
3515 for (node
= data
->list
; node
; node
= node
->next
)
3517 if (strcmp(outpath
, node
->path
) == 0)
3519 pnode
= zalloc(sizeof(path_node
));
3522 scnprintf(pnode
->path
, sizeof(pnode
->path
), "%s", path
);
3523 vtoy_list_add(node
->list
, pcur
, pnode
);
3531 ret
= ventoy_data_save_all();
3533 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3537 static int ventoy_api_persistence_del_inner(struct mg_connection
*conn
, VTOY_JSON
*json
)
3541 const char *path
= NULL
;
3542 const char *outpath
= NULL
;
3543 path_node
*plast
= NULL
;
3544 path_node
*pnode
= NULL
;
3545 persistence_node
*node
= NULL
;
3546 data_persistence
*data
= NULL
;
3548 vtoy_json_get_int(json
, "index", &index
);
3549 data
= g_data_persistence
+ index
;
3551 path
= VTOY_JSON_STR_EX("path");
3552 outpath
= VTOY_JSON_STR_EX("outpath");
3553 if (path
&& outpath
)
3555 for (node
= data
->list
; node
; node
= node
->next
)
3557 if (strcmp(outpath
, node
->path
) == 0)
3559 vtoy_list_del(plast
, pnode
, node
->list
, path
);
3565 ret
= ventoy_data_save_all();
3567 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3575 void ventoy_data_default_injection(data_injection
*data
)
3577 memset(data
, 0, sizeof(data_injection
));
3580 int ventoy_data_cmp_injection(data_injection
*data1
, data_injection
*data2
)
3582 injection_node
*list1
= NULL
;
3583 injection_node
*list2
= NULL
;
3585 if (NULL
== data1
->list
&& NULL
== data2
->list
)
3589 else if (data1
->list
&& data2
->list
)
3591 list1
= data1
->list
;
3592 list2
= data2
->list
;
3594 while (list1
&& list2
)
3596 if ((list1
->type
!= list2
->type
) ||
3597 strcmp(list1
->path
, list2
->path
) ||
3598 strcmp(list1
->archive
, list2
->archive
))
3603 list1
= list1
->next
;
3604 list2
= list2
->next
;
3607 if (list1
== NULL
&& list2
== NULL
)
3622 int ventoy_data_save_injection(data_injection
*data
, const char *title
, char *buf
, int buflen
)
3625 injection_node
*node
= NULL
;
3627 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
3629 VTOY_JSON_FMT_KEY_L(L1
, title
);
3630 VTOY_JSON_FMT_ARY_BEGIN_N();
3632 for (node
= data
->list
; node
; node
= node
->next
)
3634 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
3636 if (node
->type
== 0)
3638 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "image", node
->path
);
3642 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "parent", node
->path
);
3644 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "archive", node
->archive
);
3646 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
3649 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
3650 VTOY_JSON_FMT_END(pos
);
3656 int ventoy_data_json_injection(data_injection
*data
, char *buf
, int buflen
)
3660 injection_node
*node
= NULL
;
3662 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
3663 VTOY_JSON_FMT_ARY_BEGIN();
3665 for (node
= data
->list
; node
; node
= node
->next
)
3667 VTOY_JSON_FMT_OBJ_BEGIN();
3669 VTOY_JSON_FMT_UINT("type", node
->type
);
3670 VTOY_JSON_FMT_STRN("path", node
->path
);
3672 if (node
->type
== 0)
3674 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
3678 valid
= ventoy_is_directory_exist("%s%s", g_cur_dir
, node
->path
);
3680 VTOY_JSON_FMT_SINT("valid", valid
);
3682 VTOY_JSON_FMT_STRN("archive", node
->archive
);
3684 valid
= ventoy_is_file_exist("%s%s", g_cur_dir
, node
->archive
);
3685 VTOY_JSON_FMT_SINT("archive_valid", valid
);
3687 VTOY_JSON_FMT_OBJ_ENDEX();
3690 VTOY_JSON_FMT_ARY_END();
3691 VTOY_JSON_FMT_END(pos
);
3697 static int ventoy_api_get_injection(struct mg_connection
*conn
, VTOY_JSON
*json
)
3699 api_get_func(conn
, json
, injection
);
3703 static int ventoy_api_save_injection(struct mg_connection
*conn
, VTOY_JSON
*json
)
3706 ret
= ventoy_data_save_all();
3708 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3712 static int ventoy_api_injection_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
3717 const char *path
= NULL
;
3718 const char *archive
= NULL
;
3719 injection_node
*node
= NULL
;
3720 injection_node
*cur
= NULL
;
3721 data_injection
*data
= NULL
;
3723 vtoy_json_get_int(json
, "index", &index
);
3724 data
= g_data_injection
+ index
;
3726 vtoy_json_get_int(json
, "type", &type
);
3728 path
= VTOY_JSON_STR_EX("path");
3729 archive
= VTOY_JSON_STR_EX("archive");
3730 if (path
&& archive
)
3732 if (ventoy_is_real_exist_common(path
, data
->list
, injection_node
))
3734 ventoy_json_result(conn
, VTOY_JSON_DUPLICATE
);
3738 node
= zalloc(sizeof(injection_node
));
3743 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
3744 scnprintf(node
->archive
, sizeof(node
->archive
), "%s", archive
);
3746 vtoy_list_add(data
->list
, cur
, node
);
3750 ret
= ventoy_data_save_all();
3752 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3756 static int ventoy_api_injection_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
3760 const char *path
= NULL
;
3761 injection_node
*last
= NULL
;
3762 injection_node
*node
= NULL
;
3763 data_injection
*data
= NULL
;
3765 vtoy_json_get_int(json
, "index", &index
);
3766 data
= g_data_injection
+ index
;
3768 path
= VTOY_JSON_STR_EX("path");
3771 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
3773 vtoy_list_free(injection_node
, data
->list
);
3777 vtoy_list_del(last
, node
, data
->list
, path
);
3781 ret
= ventoy_data_save_all();
3783 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3790 static int ventoy_api_preview_json(struct mg_connection
*conn
, VTOY_JSON
*json
)
3795 int utf16enclen
= 0;
3796 char *encodebuf
= NULL
;
3797 unsigned short *utf16buf
= NULL
;
3801 /* We can not use json directly, because it will be formated in the JS. */
3803 len
= ventoy_data_real_save_all(0);
3805 utf16buf
= (unsigned short *)malloc(2 * len
+ 16);
3811 utf16enclen
= (int)utf8_to_utf16((unsigned char *)JSON_SAVE_BUFFER
, len
, utf16buf
, len
+ 2);
3813 encodebuf
= (char *)malloc(utf16enclen
* 4 + 16);
3819 for (i
= 0; i
< utf16enclen
; i
++)
3821 scnprintf(encodebuf
+ i
* 4, 5, "%04X", utf16buf
[i
]);
3825 VTOY_JSON_FMT_BEGIN(pos
, JSON_BUFFER
, JSON_BUF_MAX
);
3826 VTOY_JSON_FMT_OBJ_BEGIN();
3827 VTOY_JSON_FMT_STRN("json", (encodebuf
? encodebuf
: ""));
3828 VTOY_JSON_FMT_OBJ_END();
3829 VTOY_JSON_FMT_END(pos
);
3831 CHECK_FREE(encodebuf
);
3832 CHECK_FREE(utf16buf
);
3834 ventoy_json_buffer(conn
, JSON_BUFFER
, pos
);
3842 int ventoy_data_save_all(void)
3844 ventoy_set_writeback_event();
3848 int ventoy_data_real_save_all(int apilock
)
3856 pthread_mutex_lock(&g_api_mutex
);
3859 ssprintf(pos
, JSON_SAVE_BUFFER
, JSON_BUF_MAX
, "{\n");
3861 ventoy_save_plug(control
);
3862 ventoy_save_plug(theme
);
3863 ventoy_save_plug(menu_alias
);
3864 ventoy_save_plug(menu_tip
);
3865 ventoy_save_plug(menu_class
);
3866 ventoy_save_plug(auto_install
);
3867 ventoy_save_plug(persistence
);
3868 ventoy_save_plug(injection
);
3869 ventoy_save_plug(conf_replace
);
3870 ventoy_save_plug(password
);
3871 ventoy_save_plug(image_list
);
3872 ventoy_save_plug(auto_memdisk
);
3873 ventoy_save_plug(dud
);
3875 if (JSON_SAVE_BUFFER
[pos
- 1] == '\n' && JSON_SAVE_BUFFER
[pos
- 2] == ',')
3877 JSON_SAVE_BUFFER
[pos
- 2] = '\n';
3880 ssprintf(pos
, JSON_SAVE_BUFFER
, JSON_BUF_MAX
, "}\n");
3884 pthread_mutex_unlock(&g_api_mutex
);
3890 int ventoy_http_writeback(void)
3896 ventoy_get_json_path(filename
, NULL
);
3898 pos
= ventoy_data_real_save_all(1);
3901 printf("%s", JSON_SAVE_BUFFER
);
3904 ret
= ventoy_write_buf_to_file(filename
, JSON_SAVE_BUFFER
, pos
);
3907 vlog("Failed to write ventoy.json file.\n");
3908 g_sysinfo
.config_save_error
= 1;
3915 static JSON_CB g_ventoy_json_cb
[] =
3917 { "sysinfo", ventoy_api_sysinfo
},
3918 { "handshake", ventoy_api_handshake
},
3919 { "check_path", ventoy_api_check_exist
},
3920 { "check_path2", ventoy_api_check_exist2
},
3921 { "check_fuzzy", ventoy_api_check_fuzzy
},
3923 { "device_info", ventoy_api_device_info
},
3925 { "get_control", ventoy_api_get_control
},
3926 { "save_control", ventoy_api_save_control
},
3928 { "get_theme", ventoy_api_get_theme
},
3929 { "save_theme", ventoy_api_save_theme
},
3930 { "theme_add_file", ventoy_api_theme_add_file
},
3931 { "theme_del_file", ventoy_api_theme_del_file
},
3932 { "theme_add_font", ventoy_api_theme_add_font
},
3933 { "theme_del_font", ventoy_api_theme_del_font
},
3935 { "get_alias", ventoy_api_get_alias
},
3936 { "save_alias", ventoy_api_save_alias
},
3937 { "alias_add", ventoy_api_alias_add
},
3938 { "alias_del", ventoy_api_alias_del
},
3940 { "get_tip", ventoy_api_get_tip
},
3941 { "save_tip", ventoy_api_save_tip
},
3942 { "tip_add", ventoy_api_tip_add
},
3943 { "tip_del", ventoy_api_tip_del
},
3945 { "get_class", ventoy_api_get_class
},
3946 { "save_class", ventoy_api_save_class
},
3947 { "class_add", ventoy_api_class_add
},
3948 { "class_del", ventoy_api_class_del
},
3950 { "get_auto_memdisk", ventoy_api_get_auto_memdisk
},
3951 { "save_auto_memdisk", ventoy_api_save_auto_memdisk
},
3952 { "auto_memdisk_add", ventoy_api_auto_memdisk_add
},
3953 { "auto_memdisk_del", ventoy_api_auto_memdisk_del
},
3955 { "get_image_list", ventoy_api_get_image_list
},
3956 { "save_image_list", ventoy_api_save_image_list
},
3957 { "image_list_add", ventoy_api_image_list_add
},
3958 { "image_list_del", ventoy_api_image_list_del
},
3960 { "get_conf_replace", ventoy_api_get_conf_replace
},
3961 { "save_conf_replace", ventoy_api_save_conf_replace
},
3962 { "conf_replace_add", ventoy_api_conf_replace_add
},
3963 { "conf_replace_del", ventoy_api_conf_replace_del
},
3965 { "get_dud", ventoy_api_get_dud
},
3966 { "save_dud", ventoy_api_save_dud
},
3967 { "dud_add", ventoy_api_dud_add
},
3968 { "dud_del", ventoy_api_dud_del
},
3969 { "dud_add_inner", ventoy_api_dud_add_inner
},
3970 { "dud_del_inner", ventoy_api_dud_del_inner
},
3972 { "get_auto_install", ventoy_api_get_auto_install
},
3973 { "save_auto_install", ventoy_api_save_auto_install
},
3974 { "auto_install_add", ventoy_api_auto_install_add
},
3975 { "auto_install_del", ventoy_api_auto_install_del
},
3976 { "auto_install_add_inner", ventoy_api_auto_install_add_inner
},
3977 { "auto_install_del_inner", ventoy_api_auto_install_del_inner
},
3979 { "get_persistence", ventoy_api_get_persistence
},
3980 { "save_persistence", ventoy_api_save_persistence
},
3981 { "persistence_add", ventoy_api_persistence_add
},
3982 { "persistence_del", ventoy_api_persistence_del
},
3983 { "persistence_add_inner", ventoy_api_persistence_add_inner
},
3984 { "persistence_del_inner", ventoy_api_persistence_del_inner
},
3986 { "get_password", ventoy_api_get_password
},
3987 { "save_password", ventoy_api_save_password
},
3988 { "password_add", ventoy_api_password_add
},
3989 { "password_del", ventoy_api_password_del
},
3991 { "get_injection", ventoy_api_get_injection
},
3992 { "save_injection", ventoy_api_save_injection
},
3993 { "injection_add", ventoy_api_injection_add
},
3994 { "injection_del", ventoy_api_injection_del
},
3995 { "preview_json", ventoy_api_preview_json
},
3999 static int ventoy_json_handler(struct mg_connection
*conn
, VTOY_JSON
*json
, char *jsonstr
)
4002 const char *method
= NULL
;
4004 method
= vtoy_json_get_string_ex(json
, "method");
4007 ventoy_json_result(conn
, VTOY_JSON_SUCCESS_RET
);
4011 if (strcmp(method
, "handshake") == 0)
4013 ventoy_api_handshake(conn
, json
);
4017 for (i
= 0; i
< (int)(sizeof(g_ventoy_json_cb
) / sizeof(g_ventoy_json_cb
[0])); i
++)
4019 if (strcmp(method
, g_ventoy_json_cb
[i
].method
) == 0)
4021 g_ventoy_json_cb
[i
].callback(conn
, json
);
4029 static int ventoy_request_handler(struct mg_connection
*conn
)
4033 VTOY_JSON
*json
= NULL
;
4034 char *post_data_buf
= NULL
;
4035 const struct mg_request_info
*ri
= NULL
;
4036 char stack_buf
[512];
4038 ri
= mg_get_request_info(conn
);
4040 if (strcmp(ri
->uri
, "/vtoy/json") == 0)
4042 if (ri
->content_length
> 500)
4044 post_data_buf
= malloc((int)(ri
->content_length
+ 4));
4045 post_buf_len
= (int)(ri
->content_length
+ 1);
4049 post_data_buf
= stack_buf
;
4050 post_buf_len
= sizeof(stack_buf
);
4053 post_data_len
= mg_read(conn
, post_data_buf
, post_buf_len
);
4054 post_data_buf
[post_data_len
] = 0;
4056 json
= vtoy_json_create();
4057 if (JSON_SUCCESS
== vtoy_json_parse(json
, post_data_buf
))
4059 pthread_mutex_lock(&g_api_mutex
);
4060 ventoy_json_handler(conn
, json
->pstChild
, post_data_buf
);
4061 pthread_mutex_unlock(&g_api_mutex
);
4065 ventoy_json_result(conn
, VTOY_JSON_INVALID_RET
);
4068 vtoy_json_destroy(json
);
4070 if (post_data_buf
!= stack_buf
)
4072 free(post_data_buf
);
4082 const char *ventoy_web_openfile(const struct mg_connection
*conn
, const char *path
, size_t *data_len
)
4084 ventoy_file
*node
= NULL
;
4093 node
= ventoy_tar_find_file(path
);
4096 *data_len
= node
->size
;
4108 static int ventoy_parse_control(VTOY_JSON
*json
, void *p
)
4111 VTOY_JSON
*node
= NULL
;
4112 VTOY_JSON
*child
= NULL
;
4113 data_control
*data
= (data_control
*)p
;
4115 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4120 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4122 if (node
->enDataType
== JSON_TYPE_OBJECT
)
4124 child
= node
->pstChild
;
4126 if (child
->enDataType
!= JSON_TYPE_STRING
)
4131 if (strcmp(child
->pcName
, "VTOY_DEFAULT_MENU_MODE") == 0)
4133 CONTROL_PARSE_INT_DEF_0(child
, data
->default_menu_mode
);
4135 else if (strcmp(child
->pcName
, "VTOY_WIN11_BYPASS_CHECK") == 0)
4137 CONTROL_PARSE_INT_DEF_1(child
, data
->win11_bypass_check
);
4139 else if (strcmp(child
->pcName
, "VTOY_WIN11_BYPASS_NRO") == 0)
4141 CONTROL_PARSE_INT_DEF_1(child
, data
->win11_bypass_nro
);
4143 else if (strcmp(child
->pcName
, "VTOY_LINUX_REMOUNT") == 0)
4145 CONTROL_PARSE_INT_DEF_0(child
, data
->linux_remount
);
4147 else if (strcmp(child
->pcName
, "VTOY_SECONDARY_BOOT_MENU") == 0)
4149 CONTROL_PARSE_INT_DEF_1(child
, data
->secondary_menu
);
4151 else if (strcmp(child
->pcName
, "VTOY_SHOW_PASSWORD_ASTERISK") == 0)
4153 CONTROL_PARSE_INT_DEF_1(child
, data
->password_asterisk
);
4155 else if (strcmp(child
->pcName
, "VTOY_TREE_VIEW_MENU_STYLE") == 0)
4157 CONTROL_PARSE_INT_DEF_0(child
, data
->treeview_style
);
4159 else if (strcmp(child
->pcName
, "VTOY_FILT_DOT_UNDERSCORE_FILE") == 0)
4161 CONTROL_PARSE_INT_DEF_1(child
, data
->filter_dot_underscore
);
4163 else if (strcmp(child
->pcName
, "VTOY_SORT_CASE_SENSITIVE") == 0)
4165 CONTROL_PARSE_INT_DEF_0(child
, data
->sort_casesensitive
);
4167 else if (strcmp(child
->pcName
, "VTOY_MAX_SEARCH_LEVEL") == 0)
4169 if (strcmp(child
->unData
.pcStrVal
, "max") == 0)
4171 data
->max_search_level
= -1;
4175 data
->max_search_level
= (int)strtol(child
->unData
.pcStrVal
, NULL
, 10);
4178 else if (strcmp(child
->pcName
, "VTOY_DEFAULT_SEARCH_ROOT") == 0)
4180 strlcpy(data
->default_search_root
, child
->unData
.pcStrVal
);
4182 else if (strcmp(child
->pcName
, "VTOY_DEFAULT_IMAGE") == 0)
4184 strlcpy(data
->default_image
, child
->unData
.pcStrVal
);
4186 else if (strcmp(child
->pcName
, "VTOY_DEFAULT_KBD_LAYOUT") == 0)
4188 for (i
= 0; g_ventoy_kbd_layout
[i
]; i
++)
4190 if (strcmp(child
->unData
.pcStrVal
, g_ventoy_kbd_layout
[i
]) == 0)
4192 strlcpy(data
->default_kbd_layout
, child
->unData
.pcStrVal
);
4197 else if (strcmp(child
->pcName
, "VTOY_MENU_LANGUAGE") == 0)
4199 for (i
= 0; g_ventoy_menu_lang
[i
][0]; i
++)
4201 if (strcmp(child
->unData
.pcStrVal
, g_ventoy_menu_lang
[i
]) == 0)
4203 strlcpy(data
->menu_language
, child
->unData
.pcStrVal
);
4208 else if (strcmp(child
->pcName
, "VTOY_MENU_TIMEOUT") == 0)
4210 data
->menu_timeout
= (int)strtol(child
->unData
.pcStrVal
, NULL
, 10);
4212 else if (strcmp(child
->pcName
, "VTOY_SECONDARY_TIMEOUT") == 0)
4214 data
->secondary_menu_timeout
= (int)strtol(child
->unData
.pcStrVal
, NULL
, 10);
4216 else if (strcmp(child
->pcName
, "VTOY_VHD_NO_WARNING") == 0)
4218 CONTROL_PARSE_INT_DEF_0(child
, data
->vhd_no_warning
);
4220 else if (strcmp(child
->pcName
, "VTOY_FILE_FLT_ISO") == 0)
4222 CONTROL_PARSE_INT_DEF_0(child
, data
->filter_iso
);
4224 else if (strcmp(child
->pcName
, "VTOY_FILE_FLT_IMG") == 0)
4226 CONTROL_PARSE_INT_DEF_0(child
, data
->filter_img
);
4228 else if (strcmp(child
->pcName
, "VTOY_FILE_FLT_EFI") == 0)
4230 CONTROL_PARSE_INT_DEF_0(child
, data
->filter_efi
);
4232 else if (strcmp(child
->pcName
, "VTOY_FILE_FLT_WIM") == 0)
4234 CONTROL_PARSE_INT_DEF_0(child
, data
->filter_wim
);
4236 else if (strcmp(child
->pcName
, "VTOY_FILE_FLT_VHD") == 0)
4238 CONTROL_PARSE_INT_DEF_0(child
, data
->filter_vhd
);
4240 else if (strcmp(child
->pcName
, "VTOY_FILE_FLT_VTOY") == 0)
4242 CONTROL_PARSE_INT_DEF_0(child
, data
->filter_vtoy
);
4250 static int ventoy_parse_theme(VTOY_JSON
*json
, void *p
)
4252 const char *dismode
= NULL
;
4253 VTOY_JSON
*child
= NULL
;
4254 VTOY_JSON
*node
= NULL
;
4255 path_node
*tail
= NULL
;
4256 path_node
*pnode
= NULL
;
4257 data_theme
*data
= (data_theme
*)p
;
4259 if (json
->enDataType
!= JSON_TYPE_OBJECT
)
4264 child
= json
->pstChild
;
4266 dismode
= vtoy_json_get_string_ex(child
, "display_mode");
4267 vtoy_json_get_string(child
, "ventoy_left", sizeof(data
->ventoy_left
), data
->ventoy_left
);
4268 vtoy_json_get_string(child
, "ventoy_top", sizeof(data
->ventoy_top
), data
->ventoy_top
);
4269 vtoy_json_get_string(child
, "ventoy_color", sizeof(data
->ventoy_color
), data
->ventoy_color
);
4271 vtoy_json_get_int(child
, "default_file", &(data
->default_file
));
4272 vtoy_json_get_int(child
, "resolution_fit", &(data
->resolution_fit
));
4273 vtoy_json_get_string(child
, "gfxmode", sizeof(data
->gfxmode
), data
->gfxmode
);
4274 vtoy_json_get_string(child
, "serial_param", sizeof(data
->serial_param
), data
->serial_param
);
4278 if (strcmp(dismode
, "CLI") == 0)
4280 data
->display_mode
= display_mode_cli
;
4282 else if (strcmp(dismode
, "serial") == 0)
4284 data
->display_mode
= display_mode_serial
;
4286 else if (strcmp(dismode
, "serial_console") == 0)
4288 data
->display_mode
= display_mode_ser_console
;
4292 data
->display_mode
= display_mode_gui
;
4296 node
= vtoy_json_find_item(child
, JSON_TYPE_STRING
, "file");
4299 data
->default_file
= 0;
4300 data
->resolution_fit
= 0;
4302 pnode
= zalloc(sizeof(path_node
));
4305 strlcpy(pnode
->path
, node
->unData
.pcStrVal
);
4306 data
->filelist
= pnode
;
4311 node
= vtoy_json_find_item(child
, JSON_TYPE_ARRAY
, "file");
4314 for (node
= node
->pstChild
; node
; node
= node
->pstNext
)
4316 if (node
->enDataType
== JSON_TYPE_STRING
)
4318 pnode
= zalloc(sizeof(path_node
));
4321 strlcpy(pnode
->path
, node
->unData
.pcStrVal
);
4329 data
->filelist
= tail
= pnode
;
4338 node
= vtoy_json_find_item(child
, JSON_TYPE_ARRAY
, "fonts");
4341 for (node
= node
->pstChild
; node
; node
= node
->pstNext
)
4343 if (node
->enDataType
== JSON_TYPE_STRING
)
4345 pnode
= zalloc(sizeof(path_node
));
4348 strlcpy(pnode
->path
, node
->unData
.pcStrVal
);
4349 if (data
->fontslist
)
4356 data
->fontslist
= tail
= pnode
;
4365 static int ventoy_parse_menu_alias(VTOY_JSON
*json
, void *p
)
4368 const char *path
= NULL
;
4369 const char *alias
= NULL
;
4370 data_alias
*data
= (data_alias
*)p
;
4371 data_alias_node
*tail
= NULL
;
4372 data_alias_node
*pnode
= NULL
;
4373 VTOY_JSON
*node
= NULL
;
4375 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4380 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4382 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4387 type
= path_type_file
;
4388 path
= vtoy_json_get_string_ex(node
->pstChild
, "image");
4391 path
= vtoy_json_get_string_ex(node
->pstChild
, "dir");
4392 type
= path_type_dir
;
4394 alias
= vtoy_json_get_string_ex(node
->pstChild
, "alias");
4398 pnode
= zalloc(sizeof(data_alias_node
));
4402 strlcpy(pnode
->path
, path
);
4403 strlcpy(pnode
->alias
, alias
);
4412 data
->list
= tail
= pnode
;
4421 static int ventoy_parse_menu_tip(VTOY_JSON
*json
, void *p
)
4424 const char *path
= NULL
;
4425 const char *tip
= NULL
;
4426 data_tip
*data
= (data_tip
*)p
;
4427 data_tip_node
*tail
= NULL
;
4428 data_tip_node
*pnode
= NULL
;
4429 VTOY_JSON
*node
= NULL
;
4430 VTOY_JSON
*tips
= NULL
;
4432 if (json
->enDataType
!= JSON_TYPE_OBJECT
)
4437 vtoy_json_get_string(json
->pstChild
, "left", sizeof(data
->left
), data
->left
);
4438 vtoy_json_get_string(json
->pstChild
, "top", sizeof(data
->top
), data
->top
);
4439 vtoy_json_get_string(json
->pstChild
, "color", sizeof(data
->color
), data
->color
);
4441 tips
= vtoy_json_find_item(json
->pstChild
, JSON_TYPE_ARRAY
, "tips");
4447 for (node
= tips
->pstChild
; node
; node
= node
->pstNext
)
4449 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4454 type
= path_type_file
;
4455 path
= vtoy_json_get_string_ex(node
->pstChild
, "image");
4458 path
= vtoy_json_get_string_ex(node
->pstChild
, "dir");
4459 type
= path_type_dir
;
4461 tip
= vtoy_json_get_string_ex(node
->pstChild
, "tip");
4465 pnode
= zalloc(sizeof(data_tip_node
));
4469 strlcpy(pnode
->path
, path
);
4470 strlcpy(pnode
->tip
, tip
);
4479 data
->list
= tail
= pnode
;
4487 static int ventoy_parse_menu_class(VTOY_JSON
*json
, void *p
)
4490 const char *path
= NULL
;
4491 const char *class = NULL
;
4492 data_class
*data
= (data_class
*)p
;
4493 data_class_node
*tail
= NULL
;
4494 data_class_node
*pnode
= NULL
;
4495 VTOY_JSON
*node
= NULL
;
4497 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4502 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4504 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4509 type
= class_type_key
;
4510 path
= vtoy_json_get_string_ex(node
->pstChild
, "key");
4513 type
= class_type_dir
;
4514 path
= vtoy_json_get_string_ex(node
->pstChild
, "dir");
4517 type
= class_type_parent
;
4518 path
= vtoy_json_get_string_ex(node
->pstChild
, "parent");
4521 class = vtoy_json_get_string_ex(node
->pstChild
, "class");
4525 pnode
= zalloc(sizeof(data_class_node
));
4529 strlcpy(pnode
->path
, path
);
4530 strlcpy(pnode
->class, class);
4539 data
->list
= tail
= pnode
;
4547 static int ventoy_parse_auto_install(VTOY_JSON
*json
, void *p
)
4555 const char *path
= NULL
;
4556 const char *file
= NULL
;
4557 data_auto_install
*data
= (data_auto_install
*)p
;
4558 auto_install_node
*tail
= NULL
;
4559 auto_install_node
*pnode
= NULL
;
4560 path_node
*pathnode
= NULL
;
4561 path_node
*pathtail
= NULL
;
4562 VTOY_JSON
*node
= NULL
;
4563 VTOY_JSON
*filelist
= NULL
;
4564 VTOY_JSON
*filenode
= NULL
;
4566 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4571 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4573 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4579 path
= vtoy_json_get_string_ex(node
->pstChild
, "image");
4582 path
= vtoy_json_get_string_ex(node
->pstChild
, "parent");
4590 file
= vtoy_json_get_string_ex(node
->pstChild
, "template");
4593 pnode
= zalloc(sizeof(auto_install_node
));
4598 strlcpy(pnode
->path
, path
);
4600 pathnode
= zalloc(sizeof(path_node
));
4603 strlcpy(pathnode
->path
, file
);
4604 pnode
->list
= pathnode
;
4618 data
->list
= tail
= pnode
;
4626 timeouten
= autoselen
= 0;
4627 if (JSON_SUCCESS
== vtoy_json_get_int(node
->pstChild
, "timeout", &timeout
))
4631 if (JSON_SUCCESS
== vtoy_json_get_int(node
->pstChild
, "autosel", &autosel
))
4636 filelist
= vtoy_json_find_item(node
->pstChild
, JSON_TYPE_ARRAY
, "template");
4642 pnode
= zalloc(sizeof(auto_install_node
));
4649 pnode
->autoselen
= autoselen
;
4650 pnode
->timeouten
= timeouten
;
4651 pnode
->timeout
= timeout
;
4652 pnode
->autosel
= autosel
;
4653 strlcpy(pnode
->path
, path
);
4656 for (filenode
= filelist
->pstChild
; filenode
; filenode
= filenode
->pstNext
)
4658 if (filenode
->enDataType
!= JSON_TYPE_STRING
)
4663 pathnode
= zalloc(sizeof(path_node
));
4667 strlcpy(pathnode
->path
, filenode
->unData
.pcStrVal
);
4671 pathtail
->next
= pathnode
;
4672 pathtail
= pathnode
;
4676 pnode
->list
= pathtail
= pathnode
;
4687 if (pnode
->autoselen
&& pnode
->autosel
> count
)
4699 data
->list
= tail
= pnode
;
4706 static int ventoy_parse_persistence(VTOY_JSON
*json
, void *p
)
4713 const char *path
= NULL
;
4714 const char *file
= NULL
;
4715 data_persistence
*data
= (data_persistence
*)p
;
4716 persistence_node
*tail
= NULL
;
4717 persistence_node
*pnode
= NULL
;
4718 path_node
*pathnode
= NULL
;
4719 path_node
*pathtail
= NULL
;
4720 VTOY_JSON
*node
= NULL
;
4721 VTOY_JSON
*filelist
= NULL
;
4722 VTOY_JSON
*filenode
= NULL
;
4724 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4729 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4731 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4736 path
= vtoy_json_get_string_ex(node
->pstChild
, "image");
4742 file
= vtoy_json_get_string_ex(node
->pstChild
, "backend");
4745 pnode
= zalloc(sizeof(persistence_node
));
4750 strlcpy(pnode
->path
, path
);
4752 pathnode
= zalloc(sizeof(path_node
));
4755 strlcpy(pathnode
->path
, file
);
4756 pnode
->list
= pathnode
;
4770 data
->list
= tail
= pnode
;
4778 timeouten
= autoselen
= 0;
4779 if (JSON_SUCCESS
== vtoy_json_get_int(node
->pstChild
, "timeout", &timeout
))
4783 if (JSON_SUCCESS
== vtoy_json_get_int(node
->pstChild
, "autosel", &autosel
))
4788 filelist
= vtoy_json_find_item(node
->pstChild
, JSON_TYPE_ARRAY
, "backend");
4794 pnode
= zalloc(sizeof(persistence_node
));
4801 pnode
->autoselen
= autoselen
;
4802 pnode
->timeouten
= timeouten
;
4803 pnode
->timeout
= timeout
;
4804 pnode
->autosel
= autosel
;
4805 strlcpy(pnode
->path
, path
);
4808 for (filenode
= filelist
->pstChild
; filenode
; filenode
= filenode
->pstNext
)
4810 if (filenode
->enDataType
!= JSON_TYPE_STRING
)
4815 pathnode
= zalloc(sizeof(path_node
));
4819 strlcpy(pathnode
->path
, filenode
->unData
.pcStrVal
);
4823 pathtail
->next
= pathnode
;
4824 pathtail
= pathnode
;
4828 pnode
->list
= pathtail
= pathnode
;
4839 if (pnode
->autoselen
&& pnode
->autosel
> count
)
4851 data
->list
= tail
= pnode
;
4858 static int ventoy_parse_injection(VTOY_JSON
*json
, void *p
)
4861 const char *path
= NULL
;
4862 const char *archive
= NULL
;
4863 data_injection
*data
= (data_injection
*)p
;
4864 injection_node
*tail
= NULL
;
4865 injection_node
*pnode
= NULL
;
4866 VTOY_JSON
*node
= NULL
;
4868 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4873 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4875 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4881 path
= vtoy_json_get_string_ex(node
->pstChild
, "image");
4884 path
= vtoy_json_get_string_ex(node
->pstChild
, "parent");
4887 archive
= vtoy_json_get_string_ex(node
->pstChild
, "archive");
4889 if (path
&& archive
)
4891 pnode
= zalloc(sizeof(injection_node
));
4895 strlcpy(pnode
->path
, path
);
4896 strlcpy(pnode
->archive
, archive
);
4905 data
->list
= tail
= pnode
;
4913 static int ventoy_parse_conf_replace(VTOY_JSON
*json
, void *p
)
4916 const char *path
= NULL
;
4917 const char *org
= NULL
;
4918 const char *new = NULL
;
4919 data_conf_replace
*data
= (data_conf_replace
*)p
;
4920 conf_replace_node
*tail
= NULL
;
4921 conf_replace_node
*pnode
= NULL
;
4922 VTOY_JSON
*node
= NULL
;
4924 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4929 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4931 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4936 path
= vtoy_json_get_string_ex(node
->pstChild
, "iso");
4937 org
= vtoy_json_get_string_ex(node
->pstChild
, "org");
4938 new = vtoy_json_get_string_ex(node
->pstChild
, "new");
4941 vtoy_json_get_int(node
->pstChild
, "img", &img
);
4943 if (path
&& org
&& new)
4945 pnode
= zalloc(sizeof(conf_replace_node
));
4948 strlcpy(pnode
->path
, path
);
4949 strlcpy(pnode
->org
, org
);
4950 strlcpy(pnode
->new, new);
4963 data
->list
= tail
= pnode
;
4971 static int ventoy_parse_password(VTOY_JSON
*json
, void *p
)
4974 const char *bootpwd
= NULL
;
4975 const char *isopwd
= NULL
;
4976 const char *wimpwd
= NULL
;
4977 const char *imgpwd
= NULL
;
4978 const char *efipwd
= NULL
;
4979 const char *vhdpwd
= NULL
;
4980 const char *vtoypwd
= NULL
;
4981 const char *path
= NULL
;
4982 const char *pwd
= NULL
;
4983 data_password
*data
= (data_password
*)p
;
4984 menu_password
*tail
= NULL
;
4985 menu_password
*pnode
= NULL
;
4986 VTOY_JSON
*node
= NULL
;
4987 VTOY_JSON
*menupwd
= NULL
;
4989 if (json
->enDataType
!= JSON_TYPE_OBJECT
)
4994 bootpwd
= vtoy_json_get_string_ex(json
->pstChild
, "bootpwd");
4995 isopwd
= vtoy_json_get_string_ex(json
->pstChild
, "isopwd");
4996 wimpwd
= vtoy_json_get_string_ex(json
->pstChild
, "wimpwd");
4997 imgpwd
= vtoy_json_get_string_ex(json
->pstChild
, "imgpwd");
4998 efipwd
= vtoy_json_get_string_ex(json
->pstChild
, "efipwd");
4999 vhdpwd
= vtoy_json_get_string_ex(json
->pstChild
, "vhdpwd");
5000 vtoypwd
= vtoy_json_get_string_ex(json
->pstChild
, "vtoypwd");
5003 if (bootpwd
) strlcpy(data
->bootpwd
, bootpwd
);
5004 if (isopwd
) strlcpy(data
->isopwd
, isopwd
);
5005 if (wimpwd
) strlcpy(data
->wimpwd
, wimpwd
);
5006 if (imgpwd
) strlcpy(data
->imgpwd
, imgpwd
);
5007 if (efipwd
) strlcpy(data
->efipwd
, efipwd
);
5008 if (vhdpwd
) strlcpy(data
->vhdpwd
, vhdpwd
);
5009 if (vtoypwd
) strlcpy(data
->vtoypwd
, vtoypwd
);
5012 menupwd
= vtoy_json_find_item(json
->pstChild
, JSON_TYPE_ARRAY
, "menupwd");
5018 for (node
= menupwd
->pstChild
; node
; node
= node
->pstNext
)
5020 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
5026 path
= vtoy_json_get_string_ex(node
->pstChild
, "file");
5029 path
= vtoy_json_get_string_ex(node
->pstChild
, "parent");
5032 pwd
= vtoy_json_get_string_ex(node
->pstChild
, "pwd");
5036 pnode
= zalloc(sizeof(menu_password
));
5040 strlcpy(pnode
->path
, path
);
5041 strlcpy(pnode
->pwd
, pwd
);
5050 data
->list
= tail
= pnode
;
5059 static int ventoy_parse_image_list_real(VTOY_JSON
*json
, int type
, void *p
)
5061 VTOY_JSON
*node
= NULL
;
5062 data_image_list
*data
= (data_image_list
*)p
;
5063 path_node
*tail
= NULL
;
5064 path_node
*pnode
= NULL
;
5066 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
5073 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
5075 if (node
->enDataType
== JSON_TYPE_STRING
)
5077 pnode
= zalloc(sizeof(path_node
));
5080 strlcpy(pnode
->path
, node
->unData
.pcStrVal
);
5088 data
->list
= tail
= pnode
;
5096 static int ventoy_parse_image_blacklist(VTOY_JSON
*json
, void *p
)
5098 return ventoy_parse_image_list_real(json
, 1, p
);
5100 static int ventoy_parse_image_list(VTOY_JSON
*json
, void *p
)
5102 return ventoy_parse_image_list_real(json
, 0, p
);
5105 static int ventoy_parse_auto_memdisk(VTOY_JSON
*json
, void *p
)
5107 VTOY_JSON
*node
= NULL
;
5108 data_auto_memdisk
*data
= (data_auto_memdisk
*)p
;
5109 path_node
*tail
= NULL
;
5110 path_node
*pnode
= NULL
;
5112 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
5117 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
5119 if (node
->enDataType
== JSON_TYPE_STRING
)
5121 pnode
= zalloc(sizeof(path_node
));
5124 strlcpy(pnode
->path
, node
->unData
.pcStrVal
);
5132 data
->list
= tail
= pnode
;
5140 static int ventoy_parse_dud(VTOY_JSON
*json
, void *p
)
5143 const char *path
= NULL
;
5144 const char *file
= NULL
;
5145 data_dud
*data
= (data_dud
*)p
;
5146 dud_node
*tail
= NULL
;
5147 dud_node
*pnode
= NULL
;
5148 path_node
*pathnode
= NULL
;
5149 path_node
*pathtail
= NULL
;
5150 VTOY_JSON
*node
= NULL
;
5151 VTOY_JSON
*filelist
= NULL
;
5152 VTOY_JSON
*filenode
= NULL
;
5154 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
5159 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
5161 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
5166 path
= vtoy_json_get_string_ex(node
->pstChild
, "image");
5172 file
= vtoy_json_get_string_ex(node
->pstChild
, "dud");
5175 pnode
= zalloc(sizeof(dud_node
));
5178 strlcpy(pnode
->path
, path
);
5180 pathnode
= zalloc(sizeof(path_node
));
5183 strlcpy(pathnode
->path
, file
);
5184 pnode
->list
= pathnode
;
5198 data
->list
= tail
= pnode
;
5205 filelist
= vtoy_json_find_item(node
->pstChild
, JSON_TYPE_ARRAY
, "dud");
5211 pnode
= zalloc(sizeof(dud_node
));
5217 strlcpy(pnode
->path
, path
);
5219 for (filenode
= filelist
->pstChild
; filenode
; filenode
= filenode
->pstNext
)
5221 if (filenode
->enDataType
!= JSON_TYPE_STRING
)
5226 pathnode
= zalloc(sizeof(path_node
));
5229 strlcpy(pathnode
->path
, filenode
->unData
.pcStrVal
);
5234 pathtail
->next
= pathnode
;
5235 pathtail
= pathnode
;
5239 pnode
->list
= pathtail
= pathnode
;
5257 data
->list
= tail
= pnode
;
5271 static int ventoy_load_old_json(const char *filename
)
5276 char *buffer
= NULL
;
5277 unsigned char *start
= NULL
;
5278 VTOY_JSON
*json
= NULL
;
5279 VTOY_JSON
*node
= NULL
;
5280 VTOY_JSON
*next
= NULL
;
5282 ret
= ventoy_read_file_to_buf(filename
, 4, (void **)&buffer
, &buflen
);
5285 vlog("Failed to read old ventoy.json file.\n");
5290 start
= (unsigned char *)buffer
;
5292 if (start
[0] == 0xef && start
[1] == 0xbb && start
[2] == 0xbf)
5296 else if ((start
[0] == 0xff && start
[1] == 0xfe) || (start
[0] == 0xfe && start
[1] == 0xff))
5298 vlog("ventoy.json is in UCS-2 encoding, ignore it.\n");
5303 json
= vtoy_json_create();
5310 if (vtoy_json_parse_ex(json
, buffer
+ offset
, buflen
- offset
) == JSON_SUCCESS
)
5312 vlog("parse ventoy.json success\n");
5314 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
5315 for (next
= node
->pstNext
; next
; next
= next
->pstNext
)
5317 if (node
->pcName
&& next
->pcName
&& strcmp(node
->pcName
, next
->pcName
) == 0)
5319 vlog("ventoy.json contains duplicate key <%s>.\n", node
->pcName
);
5320 g_sysinfo
.invalid_config
= 1;
5326 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
5328 ventoy_parse_json(control
);
5329 ventoy_parse_json(theme
);
5330 ventoy_parse_json(menu_alias
);
5331 ventoy_parse_json(menu_tip
);
5332 ventoy_parse_json(menu_class
);
5333 ventoy_parse_json(auto_install
);
5334 ventoy_parse_json(persistence
);
5335 ventoy_parse_json(injection
);
5336 ventoy_parse_json(conf_replace
);
5337 ventoy_parse_json(password
);
5338 ventoy_parse_json(image_list
);
5339 ventoy_parse_json(image_blacklist
);
5340 ventoy_parse_json(auto_memdisk
);
5341 ventoy_parse_json(dud
);
5346 vlog("ventoy.json has syntax error.\n");
5347 g_sysinfo
.syntax_error
= 1;
5352 vtoy_json_destroy(json
);
5359 int ventoy_http_start(const char *ip
, const char *port
)
5365 char backupname
[128];
5366 struct mg_callbacks callbacks
;
5367 const char *options
[] =
5369 "listening_ports", "24681",
5370 "document_root", "www",
5371 "index_files", "index.html",
5372 "num_threads", "16",
5373 "error_log_file", LOG_FILE
,
5374 "request_timeout_ms", "10000",
5378 for (i
= 0; i
<= bios_max
; i
++)
5380 ventoy_data_default_control(g_data_control
+ i
);
5381 ventoy_data_default_theme(g_data_theme
+ i
);
5382 ventoy_data_default_menu_alias(g_data_menu_alias
+ i
);
5383 ventoy_data_default_menu_class(g_data_menu_class
+ i
);
5384 ventoy_data_default_menu_tip(g_data_menu_tip
+ i
);
5385 ventoy_data_default_auto_install(g_data_auto_install
+ i
);
5386 ventoy_data_default_persistence(g_data_persistence
+ i
);
5387 ventoy_data_default_injection(g_data_injection
+ i
);
5388 ventoy_data_default_conf_replace(g_data_conf_replace
+ i
);
5389 ventoy_data_default_password(g_data_password
+ i
);
5390 ventoy_data_default_image_list(g_data_image_list
+ i
);
5391 ventoy_data_default_auto_memdisk(g_data_auto_memdisk
+ i
);
5392 ventoy_data_default_dud(g_data_dud
+ i
);
5395 ventoy_get_json_path(filename
, backupname
);
5396 if (ventoy_is_file_exist("%s", filename
))
5398 ventoy_copy_file(filename
, backupname
);
5399 ret
= ventoy_load_old_json(filename
);
5402 ventoy_data_real_save_all(0);
5407 scnprintf(addr
, sizeof(addr
), "%s:%s", ip
, port
);
5410 memset(&callbacks
, 0, sizeof(callbacks
));
5411 callbacks
.begin_request
= ventoy_request_handler
;
5413 callbacks
.open_file
= ventoy_web_openfile
;
5415 g_ventoy_http_ctx
= mg_start(&callbacks
, NULL
, options
);
5417 ventoy_start_writeback_thread(ventoy_http_writeback
);
5419 return g_ventoy_http_ctx
? 0 : 1;
5422 int ventoy_http_stop(void)
5424 if (g_ventoy_http_ctx
)
5426 mg_stop(g_ventoy_http_ctx
);
5429 ventoy_stop_writeback_thread();
5433 int ventoy_http_init(void)
5438 char *Buffer
= NULL
;
5441 ventoy_read_file_to_buf("www/menulist", 4, (void **)&Buffer
, &BufLen
);
5444 for (i
= 0; i
< BufLen
/ 5; i
++)
5446 memcpy(g_ventoy_menu_lang
[i
], Buffer
+ i
* 5, 5);
5447 g_ventoy_menu_lang
[i
][5] = 0;
5454 file
= ventoy_tar_find_file("www/menulist");
5457 for (i
= 0; i
< file
->size
/ 5; i
++)
5459 memcpy(g_ventoy_menu_lang
[i
], (char *)(file
->addr
) + i
* 5, 5);
5460 g_ventoy_menu_lang
[i
][5] = 0;
5465 if (!g_pub_json_buffer
)
5467 g_pub_json_buffer
= malloc(JSON_BUF_MAX
* 2);
5468 g_pub_save_buffer
= g_pub_json_buffer
+ JSON_BUF_MAX
;
5472 pthread_mutex_init(&g_api_mutex
, NULL
);
5476 void ventoy_http_exit(void)
5478 check_free(g_pub_json_buffer
);
5479 g_pub_json_buffer
= NULL
;
5480 g_pub_save_buffer
= NULL
;
5482 pthread_mutex_destroy(&g_api_mutex
);