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/>.
27 #if defined(_MSC_VER) || defined(WIN32)
31 #include <sys/types.h>
32 #include <sys/ioctl.h>
34 #include <sys/types.h>
35 #include <sys/mount.h>
37 #include <linux/limits.h>
42 #include <ventoy_define.h>
43 #include <ventoy_json.h>
44 #include <ventoy_util.h>
45 #include <ventoy_disk.h>
46 #include <ventoy_http.h>
47 #include "fat_filelib.h"
49 static const char *g_json_title_postfix
[bios_max
+ 1] =
51 "", "_legacy", "_uefi", "_ia32", "_aa64", "_mips", ""
54 static const char *g_ventoy_kbd_layout
[] =
56 "QWERTY_USA", "AZERTY", "CZECH_QWERTY", "CZECH_QWERTZ", "DANISH",
57 "DVORAK_USA", "FRENCH", "GERMAN", "ITALIANO", "JAPAN_106", "LATIN_USA",
58 "PORTU_BRAZIL", "QWERTY_UK", "QWERTZ", "QWERTZ_HUN", "QWERTZ_SLOV_CROAT",
59 "SPANISH", "SWEDISH", "TURKISH_Q", "VIETNAMESE",
63 #define VTOY_DEL_ALL_PATH "4119ae33-98ea-448e-b9c0-569aafcf1fb4"
65 static int g_json_exist
[plugin_type_max
][bios_max
];
66 static const char *g_plugin_name
[plugin_type_max
] =
68 "control", "theme", "menu_alias", "menu_tip",
69 "menu_class", "auto_install", "persistence", "injection",
70 "conf_replace", "password", "image_list",
74 static char g_ventoy_help_lang
[MAX_LANGUAGE
][8];
76 static char g_pub_path
[2 * MAX_PATH
];
77 static data_control g_data_control
[bios_max
+ 1];
78 static data_theme g_data_theme
[bios_max
+ 1];
79 static data_alias g_data_menu_alias
[bios_max
+ 1];
80 static data_tip g_data_menu_tip
[bios_max
+ 1];
81 static data_class g_data_menu_class
[bios_max
+ 1];
82 static data_image_list g_data_image_list
[bios_max
+ 1];
83 static data_image_list
*g_data_image_blacklist
= g_data_image_list
;
84 static data_auto_memdisk g_data_auto_memdisk
[bios_max
+ 1];
85 static data_password g_data_password
[bios_max
+ 1];
86 static data_conf_replace g_data_conf_replace
[bios_max
+ 1];
87 static data_injection g_data_injection
[bios_max
+ 1];
88 static data_auto_install g_data_auto_install
[bios_max
+ 1];
89 static data_persistence g_data_persistence
[bios_max
+ 1];
90 static data_dud g_data_dud
[bios_max
+ 1];
92 static char *g_pub_json_buffer
= NULL
;
93 static char *g_pub_save_buffer
= NULL
;
94 #define JSON_BUFFER g_pub_json_buffer
95 #define JSON_SAVE_BUFFER g_pub_save_buffer
97 static pthread_mutex_t g_api_mutex
;
98 static struct mg_context
*g_ventoy_http_ctx
= NULL
;
100 static int ventoy_is_kbd_valid(const char *key
)
104 for (i
= 0; g_ventoy_kbd_layout
[i
]; i
++)
106 if (strcmp(g_ventoy_kbd_layout
[i
], key
) == 0)
115 static const char * ventoy_real_path(const char *org
)
119 if (g_sysinfo
.pathcase
)
121 scnprintf(g_pub_path
, MAX_PATH
, "%s", org
);
122 count
= ventoy_path_case(g_pub_path
+ 1, 1);
136 static int ventoy_json_result(struct mg_connection
*conn
, const char *err
)
139 "HTTP/1.1 200 OK \r\n"
140 "Content-Type: application/json\r\n"
141 "Content-Length: %d\r\n"
143 (int)strlen(err
), err
);
148 static int ventoy_json_buffer(struct mg_connection
*conn
, const char *json_buf
, int json_len
)
151 "HTTP/1.1 200 OK \r\n"
152 "Content-Type: application/json\r\n"
153 "Content-Length: %d\r\n"
160 static void ventoy_free_path_node_list(path_node
*list
)
162 path_node
*next
= NULL
;
163 path_node
*node
= list
;
173 static path_node
* ventoy_path_node_add_array(VTOY_JSON
*array
)
175 path_node
*head
= NULL
;
176 path_node
*node
= NULL
;
177 path_node
*cur
= NULL
;
178 VTOY_JSON
*item
= NULL
;
180 for (item
= array
->pstChild
; item
; item
= item
->pstNext
)
182 node
= zalloc(sizeof(path_node
));
185 scnprintf(node
->path
, sizeof(node
->path
), "%s", item
->unData
.pcStrVal
);
186 vtoy_list_add(head
, cur
, node
);
193 static int ventoy_check_fuzzy_path(char *path
, int prefix
)
205 pos
= strchr(path
, '*');
208 for (cur
= pos
; *cur
; cur
++)
233 rc
= ventoy_is_directory_exist("%s%s", g_cur_dir
, path
);
237 rc
= ventoy_is_directory_exist("%s", path
);
258 return ventoy_is_file_exist("%s%s", g_cur_dir
, path
);
262 return ventoy_is_file_exist("%s", path
);
267 static int ventoy_path_list_cmp(path_node
*list1
, path_node
*list2
)
269 if (NULL
== list1
&& NULL
== list2
)
273 else if (list1
&& list2
)
275 while (list1
&& list2
)
277 if (strcmp(list1
->path
, list2
->path
))
286 if (list1
== NULL
&& list2
== NULL
)
299 static int ventoy_api_device_info(struct mg_connection
*conn
, VTOY_JSON
*json
)
305 VTOY_JSON_FMT_BEGIN(pos
, JSON_BUFFER
, JSON_BUF_MAX
);
306 VTOY_JSON_FMT_OBJ_BEGIN();
308 VTOY_JSON_FMT_STRN("dev_name", g_sysinfo
.cur_model
);
309 VTOY_JSON_FMT_STRN("dev_capacity", g_sysinfo
.cur_capacity
);
310 VTOY_JSON_FMT_STRN("dev_fs", g_sysinfo
.cur_fsname
);
311 VTOY_JSON_FMT_STRN("ventoy_ver", g_sysinfo
.cur_ventoy_ver
);
312 VTOY_JSON_FMT_SINT("part_style", g_sysinfo
.cur_part_style
);
313 VTOY_JSON_FMT_SINT("secure_boot", g_sysinfo
.cur_secureboot
);
315 VTOY_JSON_FMT_OBJ_END();
316 VTOY_JSON_FMT_END(pos
);
318 ventoy_json_buffer(conn
, JSON_BUFFER
, pos
);
322 static int ventoy_api_sysinfo(struct mg_connection
*conn
, VTOY_JSON
*json
)
328 VTOY_JSON_FMT_BEGIN(pos
, JSON_BUFFER
, JSON_BUF_MAX
);
329 VTOY_JSON_FMT_OBJ_BEGIN();
330 VTOY_JSON_FMT_STRN("language", ventoy_get_os_language());
331 VTOY_JSON_FMT_STRN("curdir", g_cur_dir
);
334 VTOY_JSON_FMT_SINT("syntax_error", g_sysinfo
.syntax_error
);
335 g_sysinfo
.syntax_error
= 0;
337 VTOY_JSON_FMT_SINT("invalid_config", g_sysinfo
.invalid_config
);
338 g_sysinfo
.invalid_config
= 0;
340 #if defined(_MSC_VER) || defined(WIN32)
341 VTOY_JSON_FMT_STRN("os", "windows");
343 VTOY_JSON_FMT_STRN("os", "linux");
346 VTOY_JSON_FMT_OBJ_END();
347 VTOY_JSON_FMT_END(pos
);
349 ventoy_json_buffer(conn
, JSON_BUFFER
, pos
);
353 static int ventoy_api_handshake(struct mg_connection
*conn
, VTOY_JSON
*json
)
362 VTOY_JSON_FMT_BEGIN(pos
, JSON_BUFFER
, JSON_BUF_MAX
);
363 VTOY_JSON_FMT_OBJ_BEGIN();
364 VTOY_JSON_FMT_SINT("status", 0);
365 VTOY_JSON_FMT_SINT("save_error", g_sysinfo
.config_save_error
);
366 g_sysinfo
.config_save_error
= 0;
368 for (i
= 0; i
< plugin_type_max
; i
++)
370 scnprintf(key
, sizeof(key
), "exist_%s", g_plugin_name
[i
]);
371 VTOY_JSON_FMT_KEY(key
);
372 VTOY_JSON_FMT_ARY_BEGIN();
373 for (j
= 0; j
< bios_max
; j
++)
375 VTOY_JSON_FMT_ITEM_INT(g_json_exist
[i
][j
]);
377 VTOY_JSON_FMT_ARY_ENDEX();
380 VTOY_JSON_FMT_OBJ_END();
381 VTOY_JSON_FMT_END(pos
);
383 ventoy_json_buffer(conn
, JSON_BUFFER
, pos
);
387 static int ventoy_api_check_exist(struct mg_connection
*conn
, VTOY_JSON
*json
)
392 const char *path
= NULL
;
394 path
= vtoy_json_get_string_ex(json
, "path");
395 vtoy_json_get_int(json
, "dir", &dir
);
401 exist
= ventoy_is_directory_exist("%s", path
);
405 exist
= ventoy_is_file_exist("%s", path
);
409 VTOY_JSON_FMT_BEGIN(pos
, JSON_BUFFER
, JSON_BUF_MAX
);
410 VTOY_JSON_FMT_OBJ_BEGIN();
411 VTOY_JSON_FMT_SINT("exist", exist
);
412 VTOY_JSON_FMT_OBJ_END();
413 VTOY_JSON_FMT_END(pos
);
415 ventoy_json_buffer(conn
, JSON_BUFFER
, pos
);
419 static int ventoy_api_check_exist2(struct mg_connection
*conn
, VTOY_JSON
*json
)
428 const char *path1
= NULL
;
429 const char *path2
= NULL
;
431 path1
= vtoy_json_get_string_ex(json
, "path1");
432 path2
= vtoy_json_get_string_ex(json
, "path2");
433 vtoy_json_get_int(json
, "dir1", &dir1
);
434 vtoy_json_get_int(json
, "dir2", &dir2
);
435 vtoy_json_get_int(json
, "fuzzy1", &fuzzy1
);
436 vtoy_json_get_int(json
, "fuzzy2", &fuzzy2
);
442 exist1
= ventoy_is_directory_exist("%s", path1
);
448 exist1
= ventoy_check_fuzzy_path((char *)path1
, 0);
452 exist1
= ventoy_is_file_exist("%s", path1
);
461 exist2
= ventoy_is_directory_exist("%s", path2
);
467 exist2
= ventoy_check_fuzzy_path((char *)path2
, 0);
471 exist2
= ventoy_is_file_exist("%s", path2
);
476 VTOY_JSON_FMT_BEGIN(pos
, JSON_BUFFER
, JSON_BUF_MAX
);
477 VTOY_JSON_FMT_OBJ_BEGIN();
478 VTOY_JSON_FMT_SINT("exist1", exist1
);
479 VTOY_JSON_FMT_SINT("exist2", exist2
);
480 VTOY_JSON_FMT_OBJ_END();
481 VTOY_JSON_FMT_END(pos
);
483 ventoy_json_buffer(conn
, JSON_BUFFER
, pos
);
487 static int ventoy_api_check_fuzzy(struct mg_connection
*conn
, VTOY_JSON
*json
)
491 const char *path
= NULL
;
493 path
= vtoy_json_get_string_ex(json
, "path");
496 exist
= ventoy_check_fuzzy_path((char *)path
, 0);
499 VTOY_JSON_FMT_BEGIN(pos
, JSON_BUFFER
, JSON_BUF_MAX
);
500 VTOY_JSON_FMT_OBJ_BEGIN();
501 VTOY_JSON_FMT_SINT("exist", exist
);
502 VTOY_JSON_FMT_OBJ_END();
503 VTOY_JSON_FMT_END(pos
);
505 ventoy_json_buffer(conn
, JSON_BUFFER
, pos
);
512 void ventoy_data_default_control(data_control
*data
)
514 memset(data
, 0, sizeof(data_control
));
516 data
->password_asterisk
= 1;
517 data
->secondary_menu
= 1;
518 data
->filter_dot_underscore
= 1;
519 data
->max_search_level
= -1;
520 data
->menu_timeout
= 0;
521 data
->secondary_menu_timeout
= 0;
523 strlcpy(data
->default_kbd_layout
, "QWERTY_USA");
524 strlcpy(data
->help_text_language
, "en_US");
527 int ventoy_data_cmp_control(data_control
*data1
, data_control
*data2
)
529 if (data1
->default_menu_mode
!= data2
->default_menu_mode
||
530 data1
->treeview_style
!= data2
->treeview_style
||
531 data1
->filter_dot_underscore
!= data2
->filter_dot_underscore
||
532 data1
->sort_casesensitive
!= data2
->sort_casesensitive
||
533 data1
->max_search_level
!= data2
->max_search_level
||
534 data1
->vhd_no_warning
!= data2
->vhd_no_warning
||
535 data1
->filter_iso
!= data2
->filter_iso
||
536 data1
->filter_wim
!= data2
->filter_wim
||
537 data1
->filter_efi
!= data2
->filter_efi
||
538 data1
->filter_img
!= data2
->filter_img
||
539 data1
->filter_vhd
!= data2
->filter_vhd
||
540 data1
->filter_vtoy
!= data2
->filter_vtoy
||
541 data1
->win11_bypass_check
!= data2
->win11_bypass_check
||
542 data1
->linux_remount
!= data2
->linux_remount
||
543 data1
->password_asterisk
!= data2
->password_asterisk
||
544 data1
->secondary_menu
!= data2
->secondary_menu
||
545 data1
->menu_timeout
!= data2
->menu_timeout
||
546 data1
->secondary_menu_timeout
!= data2
->secondary_menu_timeout
)
551 if (strcmp(data1
->default_search_root
, data2
->default_search_root
) ||
552 strcmp(data1
->default_image
, data2
->default_image
) ||
553 strcmp(data1
->default_kbd_layout
, data2
->default_kbd_layout
) ||
554 strcmp(data1
->help_text_language
, data2
->help_text_language
))
562 int ventoy_data_save_control(data_control
*data
, const char *title
, char *buf
, int buflen
)
565 data_control
*def
= g_data_control
+ bios_max
;
567 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
569 VTOY_JSON_FMT_KEY_L(L1
, title
);
570 VTOY_JSON_FMT_ARY_BEGIN_N();
572 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_DEFAULT_MENU_MODE", default_menu_mode
);
573 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_TREE_VIEW_MENU_STYLE", treeview_style
);
574 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_FILT_DOT_UNDERSCORE_FILE", filter_dot_underscore
);
575 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_SORT_CASE_SENSITIVE", sort_casesensitive
);
577 if (data
->max_search_level
>= 0)
579 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_MAX_SEARCH_LEVEL", max_search_level
);
582 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_VHD_NO_WARNING", vhd_no_warning
);
583 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_FILE_FLT_ISO", filter_iso
);
584 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_FILE_FLT_WIM", filter_wim
);
585 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_FILE_FLT_EFI", filter_efi
);
586 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_FILE_FLT_IMG", filter_img
);
587 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_FILE_FLT_VHD", filter_vhd
);
588 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_FILE_FLT_VTOY", filter_vtoy
);
589 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_WIN11_BYPASS_CHECK", win11_bypass_check
);
590 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_LINUX_REMOUNT", linux_remount
);
591 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_SECONDARY_BOOT_MENU", secondary_menu
);
592 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_SHOW_PASSWORD_ASTERISK", password_asterisk
);
593 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_MENU_TIMEOUT", menu_timeout
);
594 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_SECONDARY_TIMEOUT", secondary_menu_timeout
);
596 VTOY_JSON_FMT_CTRL_STRN(L2
, "VTOY_DEFAULT_KBD_LAYOUT", default_kbd_layout
);
597 VTOY_JSON_FMT_CTRL_STRN(L2
, "VTOY_HELP_TXT_LANGUAGE", help_text_language
);
599 if (strcmp(def
->default_search_root
, data
->default_search_root
))
601 VTOY_JSON_FMT_CTRL_STRN_STR(L2
, "VTOY_DEFAULT_SEARCH_ROOT", ventoy_real_path(data
->default_search_root
));
604 if (strcmp(def
->default_image
, data
->default_image
))
606 VTOY_JSON_FMT_CTRL_STRN_STR(L2
, "VTOY_DEFAULT_IMAGE", ventoy_real_path(data
->default_image
));
609 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
610 VTOY_JSON_FMT_END(pos
);
615 int ventoy_data_json_control(data_control
*ctrl
, char *buf
, int buflen
)
621 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
622 VTOY_JSON_FMT_OBJ_BEGIN();
624 VTOY_JSON_FMT_SINT("default_menu_mode", ctrl
->default_menu_mode
);
625 VTOY_JSON_FMT_SINT("treeview_style", ctrl
->treeview_style
);
626 VTOY_JSON_FMT_SINT("filter_dot_underscore", ctrl
->filter_dot_underscore
);
627 VTOY_JSON_FMT_SINT("sort_casesensitive", ctrl
->sort_casesensitive
);
628 VTOY_JSON_FMT_SINT("max_search_level", ctrl
->max_search_level
);
629 VTOY_JSON_FMT_SINT("vhd_no_warning", ctrl
->vhd_no_warning
);
631 VTOY_JSON_FMT_SINT("filter_iso", ctrl
->filter_iso
);
632 VTOY_JSON_FMT_SINT("filter_wim", ctrl
->filter_wim
);
633 VTOY_JSON_FMT_SINT("filter_efi", ctrl
->filter_efi
);
634 VTOY_JSON_FMT_SINT("filter_img", ctrl
->filter_img
);
635 VTOY_JSON_FMT_SINT("filter_vhd", ctrl
->filter_vhd
);
636 VTOY_JSON_FMT_SINT("filter_vtoy", ctrl
->filter_vtoy
);
637 VTOY_JSON_FMT_SINT("win11_bypass_check", ctrl
->win11_bypass_check
);
638 VTOY_JSON_FMT_SINT("linux_remount", ctrl
->linux_remount
);
639 VTOY_JSON_FMT_SINT("secondary_menu", ctrl
->secondary_menu
);
640 VTOY_JSON_FMT_SINT("password_asterisk", ctrl
->password_asterisk
);
641 VTOY_JSON_FMT_SINT("menu_timeout", ctrl
->menu_timeout
);
642 VTOY_JSON_FMT_SINT("secondary_menu_timeout", ctrl
->secondary_menu_timeout
);
643 VTOY_JSON_FMT_STRN("default_kbd_layout", ctrl
->default_kbd_layout
);
644 VTOY_JSON_FMT_STRN("help_text_language", ctrl
->help_text_language
);
647 if (ctrl
->default_search_root
[0] && ventoy_is_directory_exist("%s%s", g_cur_dir
, ctrl
->default_search_root
))
651 VTOY_JSON_FMT_STRN("default_search_root", ctrl
->default_search_root
);
652 VTOY_JSON_FMT_SINT("default_search_root_valid", valid
);
656 if (ctrl
->default_image
[0] && ventoy_is_file_exist("%s%s", g_cur_dir
, ctrl
->default_image
))
660 VTOY_JSON_FMT_STRN("default_image", ctrl
->default_image
);
661 VTOY_JSON_FMT_SINT("default_image_valid", valid
);
663 VTOY_JSON_FMT_KEY("help_list");
664 VTOY_JSON_FMT_ARY_BEGIN();
666 for (i
= 0; g_ventoy_help_lang
[i
][0]; i
++)
668 VTOY_JSON_FMT_ITEM(g_ventoy_help_lang
[i
]);
670 VTOY_JSON_FMT_ARY_ENDEX();
673 VTOY_JSON_FMT_OBJ_END();
674 VTOY_JSON_FMT_END(pos
);
679 static int ventoy_api_get_control(struct mg_connection
*conn
, VTOY_JSON
*json
)
681 api_get_func(conn
, json
, control
);
685 static int ventoy_api_save_control(struct mg_connection
*conn
, VTOY_JSON
*json
)
689 data_control
*ctrl
= NULL
;
691 vtoy_json_get_int(json
, "index", &index
);
692 ctrl
= g_data_control
+ index
;
694 VTOY_JSON_INT("default_menu_mode", ctrl
->default_menu_mode
);
695 VTOY_JSON_INT("treeview_style", ctrl
->treeview_style
);
696 VTOY_JSON_INT("filter_dot_underscore", ctrl
->filter_dot_underscore
);
697 VTOY_JSON_INT("sort_casesensitive", ctrl
->sort_casesensitive
);
698 VTOY_JSON_INT("max_search_level", ctrl
->max_search_level
);
699 VTOY_JSON_INT("vhd_no_warning", ctrl
->vhd_no_warning
);
700 VTOY_JSON_INT("filter_iso", ctrl
->filter_iso
);
701 VTOY_JSON_INT("filter_wim", ctrl
->filter_wim
);
702 VTOY_JSON_INT("filter_efi", ctrl
->filter_efi
);
703 VTOY_JSON_INT("filter_img", ctrl
->filter_img
);
704 VTOY_JSON_INT("filter_vhd", ctrl
->filter_vhd
);
705 VTOY_JSON_INT("filter_vtoy", ctrl
->filter_vtoy
);
706 VTOY_JSON_INT("win11_bypass_check", ctrl
->win11_bypass_check
);
707 VTOY_JSON_INT("linux_remount", ctrl
->linux_remount
);
708 VTOY_JSON_INT("secondary_menu", ctrl
->secondary_menu
);
709 VTOY_JSON_INT("password_asterisk", ctrl
->password_asterisk
);
710 VTOY_JSON_INT("menu_timeout", ctrl
->menu_timeout
);
711 VTOY_JSON_INT("secondary_menu_timeout", ctrl
->secondary_menu_timeout
);
713 VTOY_JSON_STR("default_image", ctrl
->default_image
);
714 VTOY_JSON_STR("default_search_root", ctrl
->default_search_root
);
715 VTOY_JSON_STR("help_text_language", ctrl
->help_text_language
);
716 VTOY_JSON_STR("default_kbd_layout", ctrl
->default_kbd_layout
);
718 ret
= ventoy_data_save_all();
720 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
728 void ventoy_data_default_theme(data_theme
*data
)
730 memset(data
, 0, sizeof(data_theme
));
731 strlcpy(data
->gfxmode
, "1024x768");
732 scnprintf(data
->ventoy_left
, sizeof(data
->ventoy_left
), "5%%");
733 scnprintf(data
->ventoy_top
, sizeof(data
->ventoy_top
), "95%%");
734 scnprintf(data
->ventoy_color
, sizeof(data
->ventoy_color
), "%s", "#0000ff");
737 int ventoy_data_cmp_theme(data_theme
*data1
, data_theme
*data2
)
739 if (data1
->display_mode
!= data2
->display_mode
||
740 strcmp(data1
->ventoy_left
, data2
->ventoy_left
) ||
741 strcmp(data1
->ventoy_top
, data2
->ventoy_top
) ||
742 strcmp(data1
->gfxmode
, data2
->gfxmode
) ||
743 strcmp(data1
->ventoy_color
, data2
->ventoy_color
)
749 if (ventoy_path_list_cmp(data1
->filelist
, data2
->filelist
))
754 if (ventoy_path_list_cmp(data1
->fontslist
, data2
->fontslist
))
763 int ventoy_data_save_theme(data_theme
*data
, const char *title
, char *buf
, int buflen
)
766 path_node
*node
= NULL
;
767 data_theme
*def
= g_data_theme
+ bios_max
;
769 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
771 VTOY_JSON_FMT_KEY_L(L1
, title
);
772 VTOY_JSON_FMT_OBJ_BEGIN_N();
776 if (data
->filelist
->next
)
778 VTOY_JSON_FMT_KEY_L(L2
, "file");
779 VTOY_JSON_FMT_ARY_BEGIN_N();
781 for (node
= data
->filelist
; node
; node
= node
->next
)
783 VTOY_JSON_FMT_ITEM_PATH_LN(L3
, node
->path
);
786 VTOY_JSON_FMT_ARY_ENDEX_LN(L2
);
788 if (def
->default_file
!= data
->default_file
)
790 VTOY_JSON_FMT_SINT_LN(L2
, "default_file", data
->default_file
);
795 VTOY_JSON_FMT_STRN_PATH_LN(L2
, "file", data
->filelist
->path
);
799 if (data
->display_mode
!= def
->display_mode
)
801 if (display_mode_cli
== data
->display_mode
)
803 VTOY_JSON_FMT_STRN_LN(L2
, "display_mode", "CLI");
805 else if (display_mode_serial
== data
->display_mode
)
807 VTOY_JSON_FMT_STRN_LN(L2
, "display_mode", "serial");
809 else if (display_mode_ser_console
== data
->display_mode
)
811 VTOY_JSON_FMT_STRN_LN(L2
, "display_mode", "serial_console");
815 VTOY_JSON_FMT_STRN_LN(L2
, "display_mode", "GUI");
819 VTOY_JSON_FMT_DIFF_STRN(L2
, "gfxmode", gfxmode
);
821 VTOY_JSON_FMT_DIFF_STRN(L2
, "ventoy_left", ventoy_left
);
822 VTOY_JSON_FMT_DIFF_STRN(L2
, "ventoy_top", ventoy_top
);
823 VTOY_JSON_FMT_DIFF_STRN(L2
, "ventoy_color", ventoy_color
);
827 VTOY_JSON_FMT_KEY_L(L2
, "fonts");
828 VTOY_JSON_FMT_ARY_BEGIN_N();
830 for (node
= data
->fontslist
; node
; node
= node
->next
)
832 VTOY_JSON_FMT_ITEM_PATH_LN(L3
, node
->path
);
835 VTOY_JSON_FMT_ARY_ENDEX_LN(L2
);
838 VTOY_JSON_FMT_OBJ_ENDEX_LN(L1
);
839 VTOY_JSON_FMT_END(pos
);
845 int ventoy_data_json_theme(data_theme
*data
, char *buf
, int buflen
)
848 path_node
*node
= NULL
;
850 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
851 VTOY_JSON_FMT_OBJ_BEGIN();
853 VTOY_JSON_FMT_SINT("default_file", data
->default_file
);
854 VTOY_JSON_FMT_SINT("display_mode", data
->display_mode
);
855 VTOY_JSON_FMT_STRN("gfxmode", data
->gfxmode
);
857 VTOY_JSON_FMT_STRN("ventoy_color", data
->ventoy_color
);
858 VTOY_JSON_FMT_STRN("ventoy_left", data
->ventoy_left
);
859 VTOY_JSON_FMT_STRN("ventoy_top", data
->ventoy_top
);
861 VTOY_JSON_FMT_KEY("filelist");
862 VTOY_JSON_FMT_ARY_BEGIN();
863 for (node
= data
->filelist
; node
; node
= node
->next
)
865 VTOY_JSON_FMT_OBJ_BEGIN();
866 VTOY_JSON_FMT_STRN("path", node
->path
);
867 VTOY_JSON_FMT_SINT("valid", ventoy_is_file_exist("%s%s", g_cur_dir
, node
->path
));
868 VTOY_JSON_FMT_OBJ_ENDEX();
870 VTOY_JSON_FMT_ARY_ENDEX();
872 VTOY_JSON_FMT_KEY("fontslist");
873 VTOY_JSON_FMT_ARY_BEGIN();
874 for (node
= data
->fontslist
; node
; node
= node
->next
)
876 VTOY_JSON_FMT_OBJ_BEGIN();
877 VTOY_JSON_FMT_STRN("path", node
->path
);
878 VTOY_JSON_FMT_SINT("valid", ventoy_is_file_exist("%s%s", g_cur_dir
, node
->path
));
879 VTOY_JSON_FMT_OBJ_ENDEX();
881 VTOY_JSON_FMT_ARY_ENDEX();
883 VTOY_JSON_FMT_OBJ_END();
884 VTOY_JSON_FMT_END(pos
);
889 static int ventoy_api_get_theme(struct mg_connection
*conn
, VTOY_JSON
*json
)
891 api_get_func(conn
, json
, theme
);
895 static int ventoy_api_save_theme(struct mg_connection
*conn
, VTOY_JSON
*json
)
899 data_theme
*data
= NULL
;
901 vtoy_json_get_int(json
, "index", &index
);
902 data
= g_data_theme
+ index
;
904 VTOY_JSON_INT("default_file", data
->default_file
);
905 VTOY_JSON_INT("display_mode", data
->display_mode
);
906 VTOY_JSON_STR("gfxmode", data
->gfxmode
);
907 VTOY_JSON_STR("ventoy_left", data
->ventoy_left
);
908 VTOY_JSON_STR("ventoy_top", data
->ventoy_top
);
909 VTOY_JSON_STR("ventoy_color", data
->ventoy_color
);
911 ret
= ventoy_data_save_all();
913 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
917 static int ventoy_api_theme_add_file(struct mg_connection
*conn
, VTOY_JSON
*json
)
921 const char *path
= NULL
;
922 path_node
*node
= NULL
;
923 path_node
*cur
= NULL
;
924 data_theme
*data
= NULL
;
926 vtoy_json_get_int(json
, "index", &index
);
927 data
= g_data_theme
+ index
;
929 path
= VTOY_JSON_STR_EX("path");
932 node
= zalloc(sizeof(path_node
));
935 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
937 vtoy_list_add(data
->filelist
, cur
, node
);
941 ret
= ventoy_data_save_all();
943 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
947 static int ventoy_api_theme_del_file(struct mg_connection
*conn
, VTOY_JSON
*json
)
951 const char *path
= NULL
;
952 path_node
*node
= NULL
;
953 path_node
*last
= NULL
;
954 data_theme
*data
= NULL
;
956 vtoy_json_get_int(json
, "index", &index
);
957 data
= g_data_theme
+ index
;
959 path
= VTOY_JSON_STR_EX("path");
962 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
964 vtoy_list_free(path_node
, data
->filelist
);
968 vtoy_list_del(last
, node
, data
->filelist
, path
);
972 ret
= ventoy_data_save_all();
974 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
979 static int ventoy_api_theme_add_font(struct mg_connection
*conn
, VTOY_JSON
*json
)
983 const char *path
= NULL
;
984 path_node
*node
= NULL
;
985 path_node
*cur
= NULL
;
986 data_theme
*data
= NULL
;
988 vtoy_json_get_int(json
, "index", &index
);
989 data
= g_data_theme
+ index
;
991 path
= VTOY_JSON_STR_EX("path");
994 node
= zalloc(sizeof(path_node
));
997 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
998 vtoy_list_add(data
->fontslist
, cur
, node
);
1002 ret
= ventoy_data_save_all();
1004 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1009 static int ventoy_api_theme_del_font(struct mg_connection
*conn
, VTOY_JSON
*json
)
1013 const char *path
= NULL
;
1014 path_node
*node
= NULL
;
1015 path_node
*last
= NULL
;
1016 data_theme
*data
= NULL
;
1018 vtoy_json_get_int(json
, "index", &index
);
1019 data
= g_data_theme
+ index
;
1021 path
= VTOY_JSON_STR_EX("path");
1024 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
1026 vtoy_list_free(path_node
, data
->fontslist
);
1030 vtoy_list_del(last
, node
, data
->fontslist
, path
);
1034 ret
= ventoy_data_save_all();
1036 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1043 void ventoy_data_default_menu_alias(data_alias
*data
)
1045 memset(data
, 0, sizeof(data_alias
));
1048 int ventoy_data_cmp_menu_alias(data_alias
*data1
, data_alias
*data2
)
1050 data_alias_node
*list1
= NULL
;
1051 data_alias_node
*list2
= NULL
;
1053 if (NULL
== data1
->list
&& NULL
== data2
->list
)
1057 else if (data1
->list
&& data2
->list
)
1059 list1
= data1
->list
;
1060 list2
= data2
->list
;
1062 while (list1
&& list2
)
1064 if ((list1
->type
!= list2
->type
) ||
1065 strcmp(list1
->path
, list2
->path
) ||
1066 strcmp(list1
->alias
, list2
->alias
))
1071 list1
= list1
->next
;
1072 list2
= list2
->next
;
1075 if (list1
== NULL
&& list2
== NULL
)
1090 int ventoy_data_save_menu_alias(data_alias
*data
, const char *title
, char *buf
, int buflen
)
1093 data_alias_node
*node
= NULL
;
1095 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1097 VTOY_JSON_FMT_KEY_L(L1
, title
);
1098 VTOY_JSON_FMT_ARY_BEGIN_N();
1100 for (node
= data
->list
; node
; node
= node
->next
)
1102 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
1104 if (node
->type
== path_type_file
)
1106 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "image", node
->path
);
1110 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "dir", node
->path
);
1113 VTOY_JSON_FMT_STRN_EX_LN(L3
, "alias", node
->alias
);
1115 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
1118 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
1119 VTOY_JSON_FMT_END(pos
);
1125 int ventoy_data_json_menu_alias(data_alias
*data
, char *buf
, int buflen
)
1129 data_alias_node
*node
= NULL
;
1131 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1132 VTOY_JSON_FMT_ARY_BEGIN();
1134 for (node
= data
->list
; node
; node
= node
->next
)
1136 VTOY_JSON_FMT_OBJ_BEGIN();
1138 VTOY_JSON_FMT_UINT("type", node
->type
);
1139 VTOY_JSON_FMT_STRN("path", node
->path
);
1140 if (node
->type
== path_type_file
)
1142 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
1146 valid
= ventoy_is_directory_exist("%s%s", g_cur_dir
, node
->path
);
1149 VTOY_JSON_FMT_SINT("valid", valid
);
1150 VTOY_JSON_FMT_STRN("alias", node
->alias
);
1152 VTOY_JSON_FMT_OBJ_ENDEX();
1155 VTOY_JSON_FMT_ARY_END();
1156 VTOY_JSON_FMT_END(pos
);
1161 static int ventoy_api_get_alias(struct mg_connection
*conn
, VTOY_JSON
*json
)
1163 api_get_func(conn
, json
, menu_alias
);
1167 static int ventoy_api_save_alias(struct mg_connection
*conn
, VTOY_JSON
*json
)
1170 ret
= ventoy_data_save_all();
1172 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1176 static int ventoy_api_alias_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
1180 int type
= path_type_file
;
1181 const char *path
= NULL
;
1182 const char *alias
= NULL
;
1183 data_alias_node
*node
= NULL
;
1184 data_alias_node
*cur
= NULL
;
1185 data_alias
*data
= NULL
;
1187 vtoy_json_get_int(json
, "index", &index
);
1188 data
= g_data_menu_alias
+ index
;
1190 vtoy_json_get_int(json
, "type", &type
);
1192 path
= VTOY_JSON_STR_EX("path");
1193 alias
= VTOY_JSON_STR_EX("alias");
1196 node
= zalloc(sizeof(data_alias_node
));
1200 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
1201 scnprintf(node
->alias
, sizeof(node
->alias
), "%s", alias
);
1203 vtoy_list_add(data
->list
, cur
, node
);
1207 ret
= ventoy_data_save_all();
1209 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1213 static int ventoy_api_alias_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
1217 const char *path
= NULL
;
1218 data_alias_node
*last
= NULL
;
1219 data_alias_node
*node
= NULL
;
1220 data_alias
*data
= NULL
;
1222 vtoy_json_get_int(json
, "index", &index
);
1223 data
= g_data_menu_alias
+ index
;
1225 path
= VTOY_JSON_STR_EX("path");
1228 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
1230 vtoy_list_free(data_alias_node
, data
->list
);
1234 vtoy_list_del(last
, node
, data
->list
, path
);
1238 ret
= ventoy_data_save_all();
1240 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1247 void ventoy_data_default_menu_tip(data_tip
*data
)
1249 memset(data
, 0, sizeof(data_tip
));
1251 scnprintf(data
->left
, sizeof(data
->left
), "10%%");
1252 scnprintf(data
->top
, sizeof(data
->top
), "81%%");
1253 scnprintf(data
->color
, sizeof(data
->color
), "%s", "blue");
1256 int ventoy_data_cmp_menu_tip(data_tip
*data1
, data_tip
*data2
)
1258 data_tip_node
*list1
= NULL
;
1259 data_tip_node
*list2
= NULL
;
1261 if (strcmp(data1
->left
, data2
->left
) || strcmp(data1
->top
, data2
->top
) || strcmp(data1
->color
, data2
->color
))
1266 if (NULL
== data1
->list
&& NULL
== data2
->list
)
1270 else if (data1
->list
&& data2
->list
)
1272 list1
= data1
->list
;
1273 list2
= data2
->list
;
1275 while (list1
&& list2
)
1277 if ((list1
->type
!= list2
->type
) ||
1278 strcmp(list1
->path
, list2
->path
) ||
1279 strcmp(list1
->tip
, list2
->tip
))
1284 list1
= list1
->next
;
1285 list2
= list2
->next
;
1288 if (list1
== NULL
&& list2
== NULL
)
1303 int ventoy_data_save_menu_tip(data_tip
*data
, const char *title
, char *buf
, int buflen
)
1306 data_tip_node
*node
= NULL
;
1307 data_tip
*def
= g_data_menu_tip
+ bios_max
;
1309 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1310 VTOY_JSON_FMT_KEY_L(L1
, title
);
1311 VTOY_JSON_FMT_OBJ_BEGIN_N();
1313 VTOY_JSON_FMT_DIFF_STRN(L2
, "left", left
);
1314 VTOY_JSON_FMT_DIFF_STRN(L2
, "top", top
);
1315 VTOY_JSON_FMT_DIFF_STRN(L2
, "color", color
);
1319 VTOY_JSON_FMT_KEY_L(L2
, "tips");
1320 VTOY_JSON_FMT_ARY_BEGIN_N();
1322 for (node
= data
->list
; node
; node
= node
->next
)
1324 VTOY_JSON_FMT_OBJ_BEGIN_LN(L3
);
1326 if (node
->type
== path_type_file
)
1328 VTOY_JSON_FMT_STRN_PATH_LN(L4
, "image", node
->path
);
1332 VTOY_JSON_FMT_STRN_PATH_LN(L4
, "dir", node
->path
);
1334 VTOY_JSON_FMT_STRN_EX_LN(L4
, "tip", node
->tip
);
1336 VTOY_JSON_FMT_OBJ_ENDEX_LN(L3
);
1339 VTOY_JSON_FMT_ARY_ENDEX_LN(L2
);
1342 VTOY_JSON_FMT_OBJ_ENDEX_LN(L1
);
1344 VTOY_JSON_FMT_END(pos
);
1350 int ventoy_data_json_menu_tip(data_tip
*data
, char *buf
, int buflen
)
1354 data_tip_node
*node
= NULL
;
1356 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1358 VTOY_JSON_FMT_OBJ_BEGIN();
1360 VTOY_JSON_FMT_STRN("left", data
->left
);
1361 VTOY_JSON_FMT_STRN("top", data
->top
);
1362 VTOY_JSON_FMT_STRN("color", data
->color
);
1364 VTOY_JSON_FMT_KEY("tips");
1365 VTOY_JSON_FMT_ARY_BEGIN();
1367 for (node
= data
->list
; node
; node
= node
->next
)
1369 VTOY_JSON_FMT_OBJ_BEGIN();
1371 VTOY_JSON_FMT_UINT("type", node
->type
);
1372 VTOY_JSON_FMT_STRN("path", node
->path
);
1373 if (node
->type
== path_type_file
)
1375 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
1379 valid
= ventoy_is_directory_exist("%s%s", g_cur_dir
, node
->path
);
1382 VTOY_JSON_FMT_SINT("valid", valid
);
1383 VTOY_JSON_FMT_STRN("tip", node
->tip
);
1385 VTOY_JSON_FMT_OBJ_ENDEX();
1388 VTOY_JSON_FMT_ARY_ENDEX();
1390 VTOY_JSON_FMT_OBJ_END();
1391 VTOY_JSON_FMT_END(pos
);
1396 static int ventoy_api_get_tip(struct mg_connection
*conn
, VTOY_JSON
*json
)
1398 api_get_func(conn
, json
, menu_tip
);
1402 static int ventoy_api_save_tip(struct mg_connection
*conn
, VTOY_JSON
*json
)
1406 data_tip
*data
= NULL
;
1408 vtoy_json_get_int(json
, "index", &index
);
1409 data
= g_data_menu_tip
+ index
;
1411 VTOY_JSON_STR("left", data
->left
);
1412 VTOY_JSON_STR("top", data
->top
);
1413 VTOY_JSON_STR("color", data
->color
);
1415 ret
= ventoy_data_save_all();
1417 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1421 static int ventoy_api_tip_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
1425 int type
= path_type_file
;
1426 const char *path
= NULL
;
1427 const char *tip
= NULL
;
1428 data_tip_node
*node
= NULL
;
1429 data_tip_node
*cur
= NULL
;
1430 data_tip
*data
= NULL
;
1432 vtoy_json_get_int(json
, "index", &index
);
1433 data
= g_data_menu_tip
+ index
;
1435 vtoy_json_get_int(json
, "type", &type
);
1437 path
= VTOY_JSON_STR_EX("path");
1438 tip
= VTOY_JSON_STR_EX("tip");
1441 node
= zalloc(sizeof(data_tip_node
));
1445 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
1446 scnprintf(node
->tip
, sizeof(node
->tip
), "%s", tip
);
1448 vtoy_list_add(data
->list
, cur
, node
);
1452 ret
= ventoy_data_save_all();
1454 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1458 static int ventoy_api_tip_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
1462 const char *path
= NULL
;
1463 data_tip_node
*last
= NULL
;
1464 data_tip_node
*node
= NULL
;
1465 data_tip
*data
= NULL
;
1467 vtoy_json_get_int(json
, "index", &index
);
1468 data
= g_data_menu_tip
+ index
;
1470 path
= VTOY_JSON_STR_EX("path");
1473 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
1475 vtoy_list_free(data_tip_node
, data
->list
);
1479 vtoy_list_del(last
, node
, data
->list
, path
);
1483 ret
= ventoy_data_save_all();
1485 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1492 void ventoy_data_default_menu_class(data_class
*data
)
1494 memset(data
, 0, sizeof(data_class
));
1497 int ventoy_data_cmp_menu_class(data_class
*data1
, data_class
*data2
)
1499 data_class_node
*list1
= NULL
;
1500 data_class_node
*list2
= NULL
;
1502 if (NULL
== data1
->list
&& NULL
== data2
->list
)
1506 else if (data1
->list
&& data2
->list
)
1508 list1
= data1
->list
;
1509 list2
= data2
->list
;
1511 while (list1
&& list2
)
1513 if ((list1
->type
!= list2
->type
) ||
1514 strcmp(list1
->path
, list2
->path
) ||
1515 strcmp(list1
->class, list2
->class))
1520 list1
= list1
->next
;
1521 list2
= list2
->next
;
1524 if (list1
== NULL
&& list2
== NULL
)
1539 int ventoy_data_save_menu_class(data_class
*data
, const char *title
, char *buf
, int buflen
)
1542 data_class_node
*node
= NULL
;
1544 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1546 VTOY_JSON_FMT_KEY_L(L1
, title
);
1547 VTOY_JSON_FMT_ARY_BEGIN_N();
1549 for (node
= data
->list
; node
; node
= node
->next
)
1551 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
1553 if (node
->type
== class_type_key
)
1555 VTOY_JSON_FMT_STRN_LN(L3
, "key", node
->path
);
1557 else if (node
->type
== class_type_dir
)
1559 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "dir", node
->path
);
1563 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "parent", node
->path
);
1565 VTOY_JSON_FMT_STRN_LN(L3
, "class", node
->class);
1567 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
1570 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
1571 VTOY_JSON_FMT_END(pos
);
1577 int ventoy_data_json_menu_class(data_class
*data
, char *buf
, int buflen
)
1581 data_class_node
*node
= NULL
;
1583 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1584 VTOY_JSON_FMT_ARY_BEGIN();
1586 for (node
= data
->list
; node
; node
= node
->next
)
1588 VTOY_JSON_FMT_OBJ_BEGIN();
1590 VTOY_JSON_FMT_UINT("type", node
->type
);
1591 VTOY_JSON_FMT_STRN("path", node
->path
);
1593 if (node
->type
== class_type_key
)
1599 valid
= ventoy_is_directory_exist("%s%s", g_cur_dir
, node
->path
);
1601 VTOY_JSON_FMT_SINT("valid", valid
);
1603 VTOY_JSON_FMT_STRN("class", node
->class);
1605 VTOY_JSON_FMT_OBJ_ENDEX();
1608 VTOY_JSON_FMT_ARY_END();
1609 VTOY_JSON_FMT_END(pos
);
1615 static int ventoy_api_get_class(struct mg_connection
*conn
, VTOY_JSON
*json
)
1617 api_get_func(conn
, json
, menu_class
);
1621 static int ventoy_api_save_class(struct mg_connection
*conn
, VTOY_JSON
*json
)
1624 ret
= ventoy_data_save_all();
1626 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1630 static int ventoy_api_class_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
1634 int type
= class_type_key
;
1635 const char *path
= NULL
;
1636 const char *class = NULL
;
1637 data_class_node
*node
= NULL
;
1638 data_class_node
*cur
= NULL
;
1639 data_class
*data
= NULL
;
1641 vtoy_json_get_int(json
, "index", &index
);
1642 data
= g_data_menu_class
+ index
;
1644 vtoy_json_get_int(json
, "type", &type
);
1646 path
= VTOY_JSON_STR_EX("path");
1647 class = VTOY_JSON_STR_EX("class");
1650 node
= zalloc(sizeof(data_class_node
));
1655 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
1656 scnprintf(node
->class, sizeof(node
->class), "%s", class);
1658 vtoy_list_add(data
->list
, cur
, node
);
1662 ret
= ventoy_data_save_all();
1664 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1668 static int ventoy_api_class_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
1672 const char *path
= NULL
;
1673 data_class_node
*last
= NULL
;
1674 data_class_node
*node
= NULL
;
1675 data_class
*data
= NULL
;
1677 vtoy_json_get_int(json
, "index", &index
);
1678 data
= g_data_menu_class
+ index
;
1680 path
= VTOY_JSON_STR_EX("path");
1683 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
1685 vtoy_list_free(data_class_node
, data
->list
);
1689 vtoy_list_del(last
, node
, data
->list
, path
);
1693 ret
= ventoy_data_save_all();
1695 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1702 void ventoy_data_default_auto_memdisk(data_auto_memdisk
*data
)
1704 memset(data
, 0, sizeof(data_auto_memdisk
));
1707 int ventoy_data_cmp_auto_memdisk(data_auto_memdisk
*data1
, data_auto_memdisk
*data2
)
1709 return ventoy_path_list_cmp(data1
->list
, data2
->list
);
1712 int ventoy_data_save_auto_memdisk(data_auto_memdisk
*data
, const char *title
, char *buf
, int buflen
)
1715 path_node
*node
= NULL
;
1717 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1719 VTOY_JSON_FMT_KEY_L(L1
, title
);
1720 VTOY_JSON_FMT_ARY_BEGIN_N();
1722 for (node
= data
->list
; node
; node
= node
->next
)
1724 VTOY_JSON_FMT_ITEM_PATH_LN(L2
, node
->path
);
1727 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
1728 VTOY_JSON_FMT_END(pos
);
1733 int ventoy_data_json_auto_memdisk(data_auto_memdisk
*data
, char *buf
, int buflen
)
1737 path_node
*node
= NULL
;
1739 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1740 VTOY_JSON_FMT_ARY_BEGIN();
1742 for (node
= data
->list
; node
; node
= node
->next
)
1744 VTOY_JSON_FMT_OBJ_BEGIN();
1746 VTOY_JSON_FMT_STRN("path", node
->path
);
1747 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
1748 VTOY_JSON_FMT_SINT("valid", valid
);
1750 VTOY_JSON_FMT_OBJ_ENDEX();
1753 VTOY_JSON_FMT_ARY_END();
1754 VTOY_JSON_FMT_END(pos
);
1759 static int ventoy_api_get_auto_memdisk(struct mg_connection
*conn
, VTOY_JSON
*json
)
1761 api_get_func(conn
, json
, auto_memdisk
);
1765 static int ventoy_api_save_auto_memdisk(struct mg_connection
*conn
, VTOY_JSON
*json
)
1769 ret
= ventoy_data_save_all();
1771 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1775 static int ventoy_api_auto_memdisk_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
1779 const char *path
= NULL
;
1780 path_node
*node
= NULL
;
1781 path_node
*cur
= NULL
;
1782 data_auto_memdisk
*data
= NULL
;
1784 vtoy_json_get_int(json
, "index", &index
);
1785 data
= g_data_auto_memdisk
+ index
;
1787 path
= VTOY_JSON_STR_EX("path");
1790 node
= zalloc(sizeof(path_node
));
1793 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
1794 vtoy_list_add(data
->list
, cur
, node
);
1798 ret
= ventoy_data_save_all();
1800 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1804 static int ventoy_api_auto_memdisk_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
1808 const char *path
= NULL
;
1809 path_node
*last
= NULL
;
1810 path_node
*node
= NULL
;
1811 data_auto_memdisk
*data
= NULL
;
1813 vtoy_json_get_int(json
, "index", &index
);
1814 data
= g_data_auto_memdisk
+ index
;
1816 path
= VTOY_JSON_STR_EX("path");
1819 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
1821 vtoy_list_free(path_node
, data
->list
);
1825 vtoy_list_del(last
, node
, data
->list
, path
);
1829 ret
= ventoy_data_save_all();
1831 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1838 void ventoy_data_default_image_list(data_image_list
*data
)
1840 memset(data
, 0, sizeof(data_image_list
));
1843 int ventoy_data_cmp_image_list(data_image_list
*data1
, data_image_list
*data2
)
1845 if (data1
->type
!= data2
->type
)
1847 if (data1
->list
|| data2
->list
)
1857 return ventoy_path_list_cmp(data1
->list
, data2
->list
);
1860 int ventoy_data_save_image_list(data_image_list
*data
, const char *title
, char *buf
, int buflen
)
1864 path_node
*node
= NULL
;
1874 prelen
= (int)strlen("image_list");
1876 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1878 if (data
->type
== 0)
1880 scnprintf(newtitle
, sizeof(newtitle
), "image_list%s", title
+ prelen
);
1884 scnprintf(newtitle
, sizeof(newtitle
), "image_blacklist%s", title
+ prelen
);
1886 VTOY_JSON_FMT_KEY_L(L1
, newtitle
);
1888 VTOY_JSON_FMT_ARY_BEGIN_N();
1890 for (node
= data
->list
; node
; node
= node
->next
)
1892 VTOY_JSON_FMT_ITEM_PATH_LN(L2
, node
->path
);
1895 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
1896 VTOY_JSON_FMT_END(pos
);
1901 int ventoy_data_json_image_list(data_image_list
*data
, char *buf
, int buflen
)
1905 path_node
*node
= NULL
;
1907 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1909 VTOY_JSON_FMT_OBJ_BEGIN();
1910 VTOY_JSON_FMT_SINT("type", data
->type
);
1912 VTOY_JSON_FMT_KEY("list");
1913 VTOY_JSON_FMT_ARY_BEGIN();
1915 for (node
= data
->list
; node
; node
= node
->next
)
1917 VTOY_JSON_FMT_OBJ_BEGIN();
1919 VTOY_JSON_FMT_STRN("path", node
->path
);
1920 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
1921 VTOY_JSON_FMT_SINT("valid", valid
);
1923 VTOY_JSON_FMT_OBJ_ENDEX();
1926 VTOY_JSON_FMT_ARY_ENDEX();
1927 VTOY_JSON_FMT_OBJ_END();
1929 VTOY_JSON_FMT_END(pos
);
1934 static int ventoy_api_get_image_list(struct mg_connection
*conn
, VTOY_JSON
*json
)
1936 api_get_func(conn
, json
, image_list
);
1940 static int ventoy_api_save_image_list(struct mg_connection
*conn
, VTOY_JSON
*json
)
1944 data_image_list
*data
= NULL
;
1946 vtoy_json_get_int(json
, "index", &index
);
1947 data
= g_data_image_list
+ index
;
1949 VTOY_JSON_INT("type", data
->type
);
1951 ret
= ventoy_data_save_all();
1953 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1957 static int ventoy_api_image_list_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
1961 const char *path
= NULL
;
1962 path_node
*node
= NULL
;
1963 path_node
*cur
= NULL
;
1964 data_image_list
*data
= NULL
;
1966 vtoy_json_get_int(json
, "index", &index
);
1967 data
= g_data_image_list
+ index
;
1969 path
= VTOY_JSON_STR_EX("path");
1972 node
= zalloc(sizeof(path_node
));
1975 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
1976 vtoy_list_add(data
->list
, cur
, node
);
1980 ret
= ventoy_data_save_all();
1982 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1986 static int ventoy_api_image_list_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
1990 const char *path
= NULL
;
1991 path_node
*last
= NULL
;
1992 path_node
*node
= NULL
;
1993 data_image_list
*data
= NULL
;
1995 vtoy_json_get_int(json
, "index", &index
);
1996 data
= g_data_image_list
+ index
;
1998 path
= VTOY_JSON_STR_EX("path");
2001 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
2003 vtoy_list_free(path_node
, data
->list
);
2007 vtoy_list_del(last
, node
, data
->list
, path
);
2011 ret
= ventoy_data_save_all();
2013 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2020 void ventoy_data_default_password(data_password
*data
)
2022 memset(data
, 0, sizeof(data_password
));
2025 int ventoy_data_cmp_password(data_password
*data1
, data_password
*data2
)
2027 menu_password
*list1
= NULL
;
2028 menu_password
*list2
= NULL
;
2030 if (strcmp(data1
->bootpwd
, data2
->bootpwd
) ||
2031 strcmp(data1
->isopwd
, data2
->isopwd
) ||
2032 strcmp(data1
->wimpwd
, data2
->wimpwd
) ||
2033 strcmp(data1
->vhdpwd
, data2
->vhdpwd
) ||
2034 strcmp(data1
->imgpwd
, data2
->imgpwd
) ||
2035 strcmp(data1
->efipwd
, data2
->efipwd
) ||
2036 strcmp(data1
->vtoypwd
, data2
->vtoypwd
)
2042 if (NULL
== data1
->list
&& NULL
== data2
->list
)
2046 else if (data1
->list
&& data2
->list
)
2048 list1
= data1
->list
;
2049 list2
= data2
->list
;
2051 while (list1
&& list2
)
2053 if ((list1
->type
!= list2
->type
) || strcmp(list1
->path
, list2
->path
))
2058 list1
= list1
->next
;
2059 list2
= list2
->next
;
2062 if (list1
== NULL
&& list2
== NULL
)
2077 int ventoy_data_save_password(data_password
*data
, const char *title
, char *buf
, int buflen
)
2080 menu_password
*node
= NULL
;
2081 data_password
*def
= g_data_password
+ bios_max
;
2083 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2084 VTOY_JSON_FMT_KEY_L(L1
, title
);
2085 VTOY_JSON_FMT_OBJ_BEGIN_N();
2087 VTOY_JSON_FMT_DIFF_STRN(L2
, "bootpwd", bootpwd
);
2088 VTOY_JSON_FMT_DIFF_STRN(L2
, "isopwd", isopwd
);
2089 VTOY_JSON_FMT_DIFF_STRN(L2
, "wimpwd", wimpwd
);
2090 VTOY_JSON_FMT_DIFF_STRN(L2
, "vhdpwd", vhdpwd
);
2091 VTOY_JSON_FMT_DIFF_STRN(L2
, "imgpwd", imgpwd
);
2092 VTOY_JSON_FMT_DIFF_STRN(L2
, "efipwd", efipwd
);
2093 VTOY_JSON_FMT_DIFF_STRN(L2
, "vtoypwd", vtoypwd
);
2097 VTOY_JSON_FMT_KEY_L(L2
, "menupwd");
2098 VTOY_JSON_FMT_ARY_BEGIN_N();
2100 for (node
= data
->list
; node
; node
= node
->next
)
2102 VTOY_JSON_FMT_OBJ_BEGIN_LN(L3
);
2104 if (node
->type
== 0)
2106 VTOY_JSON_FMT_STRN_PATH_LN(L4
, "file", node
->path
);
2110 VTOY_JSON_FMT_STRN_PATH_LN(L4
, "parent", node
->path
);
2112 VTOY_JSON_FMT_STRN_LN(L4
, "pwd", node
->pwd
);
2114 VTOY_JSON_FMT_OBJ_ENDEX_LN(L3
);
2117 VTOY_JSON_FMT_ARY_ENDEX_LN(L2
);
2120 VTOY_JSON_FMT_OBJ_ENDEX_LN(L1
);
2122 VTOY_JSON_FMT_END(pos
);
2128 int ventoy_data_json_password(data_password
*data
, char *buf
, int buflen
)
2132 menu_password
*node
= NULL
;
2134 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2136 VTOY_JSON_FMT_OBJ_BEGIN();
2138 VTOY_JSON_FMT_STRN("bootpwd", data
->bootpwd
);
2139 VTOY_JSON_FMT_STRN("isopwd", data
->isopwd
);
2140 VTOY_JSON_FMT_STRN("wimpwd", data
->wimpwd
);
2141 VTOY_JSON_FMT_STRN("vhdpwd", data
->vhdpwd
);
2142 VTOY_JSON_FMT_STRN("imgpwd", data
->imgpwd
);
2143 VTOY_JSON_FMT_STRN("efipwd", data
->efipwd
);
2144 VTOY_JSON_FMT_STRN("vtoypwd", data
->vtoypwd
);
2146 VTOY_JSON_FMT_KEY("list");
2147 VTOY_JSON_FMT_ARY_BEGIN();
2149 for (node
= data
->list
; node
; node
= node
->next
)
2151 VTOY_JSON_FMT_OBJ_BEGIN();
2153 VTOY_JSON_FMT_SINT("type", node
->type
);
2154 VTOY_JSON_FMT_STRN("path", node
->path
);
2155 if (node
->type
== path_type_file
)
2157 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
2161 valid
= ventoy_is_directory_exist("%s%s", g_cur_dir
, node
->path
);
2164 VTOY_JSON_FMT_SINT("valid", valid
);
2165 VTOY_JSON_FMT_STRN("pwd", node
->pwd
);
2167 VTOY_JSON_FMT_OBJ_ENDEX();
2170 VTOY_JSON_FMT_ARY_ENDEX();
2172 VTOY_JSON_FMT_OBJ_END();
2173 VTOY_JSON_FMT_END(pos
);
2178 static int ventoy_api_get_password(struct mg_connection
*conn
, VTOY_JSON
*json
)
2180 api_get_func(conn
, json
, password
);
2184 static int ventoy_api_save_password(struct mg_connection
*conn
, VTOY_JSON
*json
)
2188 data_password
*data
= NULL
;
2190 vtoy_json_get_int(json
, "index", &index
);
2191 data
= g_data_password
+ index
;
2193 VTOY_JSON_STR("bootpwd", data
->bootpwd
);
2194 VTOY_JSON_STR("isopwd", data
->isopwd
);
2195 VTOY_JSON_STR("wimpwd", data
->wimpwd
);
2196 VTOY_JSON_STR("vhdpwd", data
->vhdpwd
);
2197 VTOY_JSON_STR("imgpwd", data
->imgpwd
);
2198 VTOY_JSON_STR("efipwd", data
->efipwd
);
2199 VTOY_JSON_STR("vtoypwd", data
->vtoypwd
);
2201 ret
= ventoy_data_save_all();
2203 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2207 static int ventoy_api_password_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
2212 const char *path
= NULL
;
2213 const char *pwd
= NULL
;
2214 menu_password
*node
= NULL
;
2215 menu_password
*cur
= NULL
;
2216 data_password
*data
= NULL
;
2218 vtoy_json_get_int(json
, "index", &index
);
2219 data
= g_data_password
+ index
;
2221 vtoy_json_get_int(json
, "type", &type
);
2223 path
= VTOY_JSON_STR_EX("path");
2224 pwd
= VTOY_JSON_STR_EX("pwd");
2227 node
= zalloc(sizeof(menu_password
));
2231 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
2232 scnprintf(node
->pwd
, sizeof(node
->pwd
), "%s", pwd
);
2234 vtoy_list_add(data
->list
, cur
, node
);
2238 ret
= ventoy_data_save_all();
2240 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2244 static int ventoy_api_password_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
2248 const char *path
= NULL
;
2249 menu_password
*last
= NULL
;
2250 menu_password
*node
= NULL
;
2251 data_password
*data
= NULL
;
2253 vtoy_json_get_int(json
, "index", &index
);
2254 data
= g_data_password
+ index
;
2256 path
= VTOY_JSON_STR_EX("path");
2259 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
2261 vtoy_list_free(menu_password
, data
->list
);
2265 vtoy_list_del(last
, node
, data
->list
, path
);
2269 ret
= ventoy_data_save_all();
2271 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2278 void ventoy_data_default_conf_replace(data_conf_replace
*data
)
2280 memset(data
, 0, sizeof(data_conf_replace
));
2283 int ventoy_data_cmp_conf_replace(data_conf_replace
*data1
, data_conf_replace
*data2
)
2285 conf_replace_node
*list1
= NULL
;
2286 conf_replace_node
*list2
= NULL
;
2288 if (NULL
== data1
->list
&& NULL
== data2
->list
)
2292 else if (data1
->list
&& data2
->list
)
2294 list1
= data1
->list
;
2295 list2
= data2
->list
;
2297 while (list1
&& list2
)
2299 if (list1
->image
!= list2
->image
||
2300 strcmp(list1
->path
, list2
->path
) ||
2301 strcmp(list1
->org
, list2
->org
) ||
2302 strcmp(list1
->new, list2
->new)
2308 list1
= list1
->next
;
2309 list2
= list2
->next
;
2312 if (list1
== NULL
&& list2
== NULL
)
2327 int ventoy_data_save_conf_replace(data_conf_replace
*data
, const char *title
, char *buf
, int buflen
)
2330 conf_replace_node
*node
= NULL
;
2332 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2334 VTOY_JSON_FMT_KEY_L(L1
, title
);
2335 VTOY_JSON_FMT_ARY_BEGIN_N();
2337 for (node
= data
->list
; node
; node
= node
->next
)
2339 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
2341 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "iso", node
->path
);
2342 VTOY_JSON_FMT_STRN_LN(L3
, "org", node
->org
);
2343 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "new", node
->new);
2346 VTOY_JSON_FMT_SINT_LN(L3
, "img", node
->image
);
2349 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
2352 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
2353 VTOY_JSON_FMT_END(pos
);
2359 int ventoy_data_json_conf_replace(data_conf_replace
*data
, char *buf
, int buflen
)
2362 conf_replace_node
*node
= NULL
;
2364 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2365 VTOY_JSON_FMT_ARY_BEGIN();
2367 for (node
= data
->list
; node
; node
= node
->next
)
2369 VTOY_JSON_FMT_OBJ_BEGIN();
2371 VTOY_JSON_FMT_STRN("path", node
->path
);
2372 VTOY_JSON_FMT_SINT("valid", ventoy_check_fuzzy_path(node
->path
, 1));
2373 VTOY_JSON_FMT_STRN("org", node
->org
);
2374 VTOY_JSON_FMT_STRN("new", node
->new);
2375 VTOY_JSON_FMT_SINT("new_valid", ventoy_is_file_exist("%s%s", g_cur_dir
, node
->new));
2376 VTOY_JSON_FMT_SINT("img", node
->image
);
2378 VTOY_JSON_FMT_OBJ_ENDEX();
2381 VTOY_JSON_FMT_ARY_END();
2382 VTOY_JSON_FMT_END(pos
);
2387 static int ventoy_api_get_conf_replace(struct mg_connection
*conn
, VTOY_JSON
*json
)
2389 api_get_func(conn
, json
, conf_replace
);
2393 static int ventoy_api_save_conf_replace(struct mg_connection
*conn
, VTOY_JSON
*json
)
2396 ret
= ventoy_data_save_all();
2398 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2402 static int ventoy_api_conf_replace_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
2407 const char *path
= NULL
;
2408 const char *org
= NULL
;
2409 const char *new = NULL
;
2410 conf_replace_node
*node
= NULL
;
2411 conf_replace_node
*cur
= NULL
;
2412 data_conf_replace
*data
= NULL
;
2414 vtoy_json_get_int(json
, "img", &image
);
2416 vtoy_json_get_int(json
, "index", &index
);
2417 data
= g_data_conf_replace
+ index
;
2419 path
= VTOY_JSON_STR_EX("path");
2420 org
= VTOY_JSON_STR_EX("org");
2421 new = VTOY_JSON_STR_EX("new");
2422 if (path
&& org
&& new)
2424 node
= zalloc(sizeof(conf_replace_node
));
2427 node
->image
= image
;
2428 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
2429 scnprintf(node
->org
, sizeof(node
->org
), "%s", org
);
2430 scnprintf(node
->new, sizeof(node
->new), "%s", new);
2432 vtoy_list_add(data
->list
, cur
, node
);
2436 ret
= ventoy_data_save_all();
2438 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2442 static int ventoy_api_conf_replace_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
2446 const char *path
= NULL
;
2447 conf_replace_node
*last
= NULL
;
2448 conf_replace_node
*node
= NULL
;
2449 data_conf_replace
*data
= NULL
;
2451 vtoy_json_get_int(json
, "index", &index
);
2452 data
= g_data_conf_replace
+ index
;
2454 path
= VTOY_JSON_STR_EX("path");
2457 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
2459 vtoy_list_free(conf_replace_node
, data
->list
);
2463 vtoy_list_del(last
, node
, data
->list
, path
);
2467 ret
= ventoy_data_save_all();
2469 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2477 void ventoy_data_default_dud(data_dud
*data
)
2479 memset(data
, 0, sizeof(data_dud
));
2482 int ventoy_data_cmp_dud(data_dud
*data1
, data_dud
*data2
)
2484 dud_node
*list1
= NULL
;
2485 dud_node
*list2
= NULL
;
2487 if (NULL
== data1
->list
&& NULL
== data2
->list
)
2491 else if (data1
->list
&& data2
->list
)
2493 list1
= data1
->list
;
2494 list2
= data2
->list
;
2496 while (list1
&& list2
)
2498 if (strcmp(list1
->path
, list2
->path
))
2503 /* no need to compare dud list with default */
2504 list1
= list1
->next
;
2505 list2
= list2
->next
;
2508 if (list1
== NULL
&& list2
== NULL
)
2523 int ventoy_data_save_dud(data_dud
*data
, const char *title
, char *buf
, int buflen
)
2526 dud_node
*node
= NULL
;
2527 path_node
*pathnode
= NULL
;
2529 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2531 VTOY_JSON_FMT_KEY_L(L1
, title
);
2532 VTOY_JSON_FMT_ARY_BEGIN_N();
2534 for (node
= data
->list
; node
; node
= node
->next
)
2536 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
2537 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "image", node
->path
);
2539 VTOY_JSON_FMT_KEY_L(L3
, "dud");
2540 VTOY_JSON_FMT_ARY_BEGIN_N();
2541 for (pathnode
= node
->list
; pathnode
; pathnode
= pathnode
->next
)
2543 VTOY_JSON_FMT_ITEM_PATH_LN(L4
, pathnode
->path
);
2545 VTOY_JSON_FMT_ARY_ENDEX_LN(L3
);
2547 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
2550 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
2551 VTOY_JSON_FMT_END(pos
);
2557 int ventoy_data_json_dud(data_dud
*data
, char *buf
, int buflen
)
2561 dud_node
*node
= NULL
;
2562 path_node
*pathnode
= NULL
;
2564 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2565 VTOY_JSON_FMT_ARY_BEGIN();
2567 for (node
= data
->list
; node
; node
= node
->next
)
2569 VTOY_JSON_FMT_OBJ_BEGIN();
2571 VTOY_JSON_FMT_STRN("path", node
->path
);
2572 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
2573 VTOY_JSON_FMT_SINT("valid", valid
);
2576 VTOY_JSON_FMT_KEY("list");
2577 VTOY_JSON_FMT_ARY_BEGIN();
2578 for (pathnode
= node
->list
; pathnode
; pathnode
= pathnode
->next
)
2580 VTOY_JSON_FMT_OBJ_BEGIN();
2581 VTOY_JSON_FMT_STRN("path", pathnode
->path
);
2583 valid
= ventoy_is_file_exist("%s%s", g_cur_dir
, pathnode
->path
);
2584 VTOY_JSON_FMT_SINT("valid", valid
);
2585 VTOY_JSON_FMT_OBJ_ENDEX();
2587 VTOY_JSON_FMT_ARY_ENDEX();
2590 VTOY_JSON_FMT_OBJ_ENDEX();
2593 VTOY_JSON_FMT_ARY_END();
2594 VTOY_JSON_FMT_END(pos
);
2599 static int ventoy_api_get_dud(struct mg_connection
*conn
, VTOY_JSON
*json
)
2601 api_get_func(conn
, json
, dud
);
2605 static int ventoy_api_save_dud(struct mg_connection
*conn
, VTOY_JSON
*json
)
2608 ret
= ventoy_data_save_all();
2610 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2615 static int ventoy_api_dud_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
2619 const char *path
= NULL
;
2620 dud_node
*node
= NULL
;
2621 dud_node
*cur
= NULL
;
2622 data_dud
*data
= NULL
;
2623 VTOY_JSON
*array
= NULL
;
2625 vtoy_json_get_int(json
, "index", &index
);
2626 data
= g_data_dud
+ index
;
2628 array
= vtoy_json_find_item(json
, JSON_TYPE_ARRAY
, "dud");
2629 path
= VTOY_JSON_STR_EX("path");
2632 node
= zalloc(sizeof(dud_node
));
2635 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
2636 node
->list
= ventoy_path_node_add_array(array
);
2638 vtoy_list_add(data
->list
, cur
, node
);
2642 ret
= ventoy_data_save_all();
2644 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2648 static int ventoy_api_dud_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
2652 const char *path
= NULL
;
2653 dud_node
*next
= NULL
;
2654 dud_node
*last
= NULL
;
2655 dud_node
*node
= NULL
;
2656 data_dud
*data
= NULL
;
2658 vtoy_json_get_int(json
, "index", &index
);
2659 data
= g_data_dud
+ index
;
2661 path
= VTOY_JSON_STR_EX("path");
2664 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
2666 for (node
= data
->list
; node
; node
= next
)
2669 ventoy_free_path_node_list(node
->list
);
2676 vtoy_list_del_ex(last
, node
, data
->list
, path
, ventoy_free_path_node_list
);
2680 ret
= ventoy_data_save_all();
2682 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2687 static int ventoy_api_dud_add_inner(struct mg_connection
*conn
, VTOY_JSON
*json
)
2691 const char *path
= NULL
;
2692 const char *outpath
= NULL
;
2693 path_node
*pcur
= NULL
;
2694 path_node
*pnode
= NULL
;
2695 dud_node
*node
= NULL
;
2696 data_dud
*data
= NULL
;
2698 vtoy_json_get_int(json
, "index", &index
);
2699 data
= g_data_dud
+ index
;
2701 path
= VTOY_JSON_STR_EX("path");
2702 outpath
= VTOY_JSON_STR_EX("outpath");
2703 if (path
&& outpath
)
2705 for (node
= data
->list
; node
; node
= node
->next
)
2707 if (strcmp(outpath
, node
->path
) == 0)
2709 pnode
= zalloc(sizeof(path_node
));
2712 scnprintf(pnode
->path
, sizeof(pnode
->path
), "%s", path
);
2713 vtoy_list_add(node
->list
, pcur
, pnode
);
2721 ret
= ventoy_data_save_all();
2723 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2727 static int ventoy_api_dud_del_inner(struct mg_connection
*conn
, VTOY_JSON
*json
)
2731 const char *path
= NULL
;
2732 const char *outpath
= NULL
;
2733 path_node
*plast
= NULL
;
2734 path_node
*pnode
= NULL
;
2735 dud_node
*node
= NULL
;
2736 data_dud
*data
= NULL
;
2738 vtoy_json_get_int(json
, "index", &index
);
2739 data
= g_data_dud
+ index
;
2741 path
= VTOY_JSON_STR_EX("path");
2742 outpath
= VTOY_JSON_STR_EX("outpath");
2743 if (path
&& outpath
)
2745 for (node
= data
->list
; node
; node
= node
->next
)
2747 if (strcmp(outpath
, node
->path
) == 0)
2749 vtoy_list_del(plast
, pnode
, node
->list
, path
);
2755 ret
= ventoy_data_save_all();
2757 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2765 void ventoy_data_default_auto_install(data_auto_install
*data
)
2767 memset(data
, 0, sizeof(data_auto_install
));
2770 int ventoy_data_cmp_auto_install(data_auto_install
*data1
, data_auto_install
*data2
)
2772 auto_install_node
*list1
= NULL
;
2773 auto_install_node
*list2
= NULL
;
2775 if (NULL
== data1
->list
&& NULL
== data2
->list
)
2779 else if (data1
->list
&& data2
->list
)
2781 list1
= data1
->list
;
2782 list2
= data2
->list
;
2784 while (list1
&& list2
)
2786 if (list1
->timeout
!= list2
->timeout
||
2787 list1
->autosel
!= list2
->autosel
||
2788 strcmp(list1
->path
, list2
->path
))
2793 /* no need to compare auto install list with default */
2794 list1
= list1
->next
;
2795 list2
= list2
->next
;
2798 if (list1
== NULL
&& list2
== NULL
)
2813 int ventoy_data_save_auto_install(data_auto_install
*data
, const char *title
, char *buf
, int buflen
)
2816 auto_install_node
*node
= NULL
;
2817 path_node
*pathnode
= NULL
;
2819 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2821 VTOY_JSON_FMT_KEY_L(L1
, title
);
2822 VTOY_JSON_FMT_ARY_BEGIN_N();
2824 for (node
= data
->list
; node
; node
= node
->next
)
2826 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
2827 if (node
->type
== 0)
2829 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "image", node
->path
);
2833 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "parent", node
->path
);
2837 VTOY_JSON_FMT_KEY_L(L3
, "template");
2838 VTOY_JSON_FMT_ARY_BEGIN_N();
2839 for (pathnode
= node
->list
; pathnode
; pathnode
= pathnode
->next
)
2841 VTOY_JSON_FMT_ITEM_PATH_LN(L4
, pathnode
->path
);
2843 VTOY_JSON_FMT_ARY_ENDEX_LN(L3
);
2845 if (node
->timeouten
)
2847 VTOY_JSON_FMT_SINT_LN(L3
, "timeout", node
->timeout
);
2850 if (node
->autoselen
)
2852 VTOY_JSON_FMT_SINT_LN(L3
, "autosel", node
->autosel
);
2855 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
2858 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
2859 VTOY_JSON_FMT_END(pos
);
2865 int ventoy_data_json_auto_install(data_auto_install
*data
, char *buf
, int buflen
)
2869 auto_install_node
*node
= NULL
;
2870 path_node
*pathnode
= NULL
;
2872 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2873 VTOY_JSON_FMT_ARY_BEGIN();
2875 for (node
= data
->list
; node
; node
= node
->next
)
2877 VTOY_JSON_FMT_OBJ_BEGIN();
2879 VTOY_JSON_FMT_STRN("path", node
->path
);
2881 if (node
->type
== 0)
2883 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
2887 valid
= ventoy_is_directory_exist("%s%s", g_cur_dir
, node
->path
);
2889 VTOY_JSON_FMT_SINT("valid", valid
);
2890 VTOY_JSON_FMT_SINT("type", node
->type
);
2892 VTOY_JSON_FMT_BOOL("timeouten", node
->timeouten
);
2893 VTOY_JSON_FMT_BOOL("autoselen", node
->autoselen
);
2895 VTOY_JSON_FMT_SINT("autosel", node
->autosel
);
2896 VTOY_JSON_FMT_SINT("timeout", node
->timeout
);
2898 VTOY_JSON_FMT_KEY("list");
2899 VTOY_JSON_FMT_ARY_BEGIN();
2900 for (pathnode
= node
->list
; pathnode
; pathnode
= pathnode
->next
)
2902 VTOY_JSON_FMT_OBJ_BEGIN();
2903 VTOY_JSON_FMT_STRN("path", pathnode
->path
);
2905 valid
= ventoy_is_file_exist("%s%s", g_cur_dir
, pathnode
->path
);
2906 VTOY_JSON_FMT_SINT("valid", valid
);
2907 VTOY_JSON_FMT_OBJ_ENDEX();
2909 VTOY_JSON_FMT_ARY_ENDEX();
2912 VTOY_JSON_FMT_OBJ_ENDEX();
2915 VTOY_JSON_FMT_ARY_END();
2916 VTOY_JSON_FMT_END(pos
);
2921 static int ventoy_api_get_auto_install(struct mg_connection
*conn
, VTOY_JSON
*json
)
2923 api_get_func(conn
, json
, auto_install
);
2927 static int ventoy_api_save_auto_install(struct mg_connection
*conn
, VTOY_JSON
*json
)
2933 uint8_t timeouten
= 0;
2934 uint8_t autoselen
= 0;
2935 auto_install_node
*node
= NULL
;
2936 data_auto_install
*data
= NULL
;
2938 vtoy_json_get_int(json
, "index", &index
);
2939 vtoy_json_get_int(json
, "id", &id
);
2941 vtoy_json_get_bool(json
, "timeouten", &timeouten
);
2942 vtoy_json_get_bool(json
, "autoselen", &autoselen
);
2944 data
= g_data_auto_install
+ index
;
2948 for (node
= data
->list
; node
; node
= node
->next
)
2952 node
->timeouten
= (int)timeouten
;
2953 node
->autoselen
= (int)autoselen
;
2954 VTOY_JSON_INT("timeout", node
->timeout
);
2955 VTOY_JSON_INT("autosel", node
->autosel
);
2961 ret
= ventoy_data_save_all();
2963 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2968 static int ventoy_api_auto_install_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
2973 const char *path
= NULL
;
2974 auto_install_node
*node
= NULL
;
2975 auto_install_node
*cur
= NULL
;
2976 data_auto_install
*data
= NULL
;
2977 VTOY_JSON
*array
= NULL
;
2979 vtoy_json_get_int(json
, "type", &type
);
2980 vtoy_json_get_int(json
, "index", &index
);
2981 data
= g_data_auto_install
+ index
;
2983 array
= vtoy_json_find_item(json
, JSON_TYPE_ARRAY
, "template");
2984 path
= VTOY_JSON_STR_EX("path");
2987 node
= zalloc(sizeof(auto_install_node
));
2991 node
->timeouten
= 0;
2992 node
->autoselen
= 0;
2995 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
2996 node
->list
= ventoy_path_node_add_array(array
);
2998 vtoy_list_add(data
->list
, cur
, node
);
3002 ret
= ventoy_data_save_all();
3004 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3008 static int ventoy_api_auto_install_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
3012 const char *path
= NULL
;
3013 auto_install_node
*last
= NULL
;
3014 auto_install_node
*next
= NULL
;
3015 auto_install_node
*node
= NULL
;
3016 data_auto_install
*data
= NULL
;
3018 vtoy_json_get_int(json
, "index", &index
);
3019 data
= g_data_auto_install
+ index
;
3021 path
= VTOY_JSON_STR_EX("path");
3024 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
3026 for (node
= data
->list
; node
; node
= next
)
3029 ventoy_free_path_node_list(node
->list
);
3036 vtoy_list_del_ex(last
, node
, data
->list
, path
, ventoy_free_path_node_list
);
3040 ret
= ventoy_data_save_all();
3042 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3046 static int ventoy_api_auto_install_add_inner(struct mg_connection
*conn
, VTOY_JSON
*json
)
3050 const char *path
= NULL
;
3051 const char *outpath
= NULL
;
3052 path_node
*pcur
= NULL
;
3053 path_node
*pnode
= NULL
;
3054 auto_install_node
*node
= NULL
;
3055 data_auto_install
*data
= NULL
;
3057 vtoy_json_get_int(json
, "index", &index
);
3058 data
= g_data_auto_install
+ index
;
3060 path
= VTOY_JSON_STR_EX("path");
3061 outpath
= VTOY_JSON_STR_EX("outpath");
3062 if (path
&& outpath
)
3064 for (node
= data
->list
; node
; node
= node
->next
)
3066 if (strcmp(outpath
, node
->path
) == 0)
3068 pnode
= zalloc(sizeof(path_node
));
3071 scnprintf(pnode
->path
, sizeof(pnode
->path
), "%s", path
);
3072 vtoy_list_add(node
->list
, pcur
, pnode
);
3080 ret
= ventoy_data_save_all();
3082 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3086 static int ventoy_api_auto_install_del_inner(struct mg_connection
*conn
, VTOY_JSON
*json
)
3090 const char *path
= NULL
;
3091 const char *outpath
= NULL
;
3092 path_node
*plast
= NULL
;
3093 path_node
*pnode
= NULL
;
3094 auto_install_node
*node
= NULL
;
3095 data_auto_install
*data
= NULL
;
3097 vtoy_json_get_int(json
, "index", &index
);
3098 data
= g_data_auto_install
+ index
;
3100 path
= VTOY_JSON_STR_EX("path");
3101 outpath
= VTOY_JSON_STR_EX("outpath");
3102 if (path
&& outpath
)
3104 for (node
= data
->list
; node
; node
= node
->next
)
3106 if (strcmp(outpath
, node
->path
) == 0)
3108 vtoy_list_del(plast
, pnode
, node
->list
, path
);
3114 ret
= ventoy_data_save_all();
3116 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3125 void ventoy_data_default_persistence(data_persistence
*data
)
3127 memset(data
, 0, sizeof(data_persistence
));
3130 int ventoy_data_cmp_persistence(data_persistence
*data1
, data_persistence
*data2
)
3132 persistence_node
*list1
= NULL
;
3133 persistence_node
*list2
= NULL
;
3135 if (NULL
== data1
->list
&& NULL
== data2
->list
)
3139 else if (data1
->list
&& data2
->list
)
3141 list1
= data1
->list
;
3142 list2
= data2
->list
;
3144 while (list1
&& list2
)
3146 if (list1
->timeout
!= list2
->timeout
||
3147 list1
->autosel
!= list2
->autosel
||
3148 strcmp(list1
->path
, list2
->path
))
3153 /* no need to compare auto install list with default */
3154 list1
= list1
->next
;
3155 list2
= list2
->next
;
3158 if (list1
== NULL
&& list2
== NULL
)
3173 int ventoy_data_save_persistence(data_persistence
*data
, const char *title
, char *buf
, int buflen
)
3176 persistence_node
*node
= NULL
;
3177 path_node
*pathnode
= NULL
;
3179 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
3181 VTOY_JSON_FMT_KEY_L(L1
, title
);
3182 VTOY_JSON_FMT_ARY_BEGIN_N();
3184 for (node
= data
->list
; node
; node
= node
->next
)
3186 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
3187 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "image", node
->path
);
3188 VTOY_JSON_FMT_KEY_L(L3
, "backend");
3189 VTOY_JSON_FMT_ARY_BEGIN_N();
3190 for (pathnode
= node
->list
; pathnode
; pathnode
= pathnode
->next
)
3192 VTOY_JSON_FMT_ITEM_PATH_LN(L4
, pathnode
->path
);
3194 VTOY_JSON_FMT_ARY_ENDEX_LN(L3
);
3196 if (node
->timeouten
)
3198 VTOY_JSON_FMT_SINT_LN(L3
, "timeout", node
->timeout
);
3201 if (node
->autoselen
)
3203 VTOY_JSON_FMT_SINT_LN(L3
, "autosel", node
->autosel
);
3206 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
3209 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
3210 VTOY_JSON_FMT_END(pos
);
3216 int ventoy_data_json_persistence(data_persistence
*data
, char *buf
, int buflen
)
3220 persistence_node
*node
= NULL
;
3221 path_node
*pathnode
= NULL
;
3223 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
3224 VTOY_JSON_FMT_ARY_BEGIN();
3226 for (node
= data
->list
; node
; node
= node
->next
)
3228 VTOY_JSON_FMT_OBJ_BEGIN();
3230 VTOY_JSON_FMT_STRN("path", node
->path
);
3232 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
3233 VTOY_JSON_FMT_SINT("valid", valid
);
3234 VTOY_JSON_FMT_SINT("type", node
->type
);
3236 VTOY_JSON_FMT_BOOL("timeouten", node
->timeouten
);
3237 VTOY_JSON_FMT_BOOL("autoselen", node
->autoselen
);
3239 VTOY_JSON_FMT_SINT("autosel", node
->autosel
);
3240 VTOY_JSON_FMT_SINT("timeout", node
->timeout
);
3242 VTOY_JSON_FMT_KEY("list");
3243 VTOY_JSON_FMT_ARY_BEGIN();
3244 for (pathnode
= node
->list
; pathnode
; pathnode
= pathnode
->next
)
3246 VTOY_JSON_FMT_OBJ_BEGIN();
3247 VTOY_JSON_FMT_STRN("path", pathnode
->path
);
3249 valid
= ventoy_is_file_exist("%s%s", g_cur_dir
, pathnode
->path
);
3250 VTOY_JSON_FMT_SINT("valid", valid
);
3251 VTOY_JSON_FMT_OBJ_ENDEX();
3253 VTOY_JSON_FMT_ARY_ENDEX();
3256 VTOY_JSON_FMT_OBJ_ENDEX();
3259 VTOY_JSON_FMT_ARY_END();
3260 VTOY_JSON_FMT_END(pos
);
3265 static int ventoy_api_get_persistence(struct mg_connection
*conn
, VTOY_JSON
*json
)
3267 api_get_func(conn
, json
, persistence
);
3271 static int ventoy_api_save_persistence(struct mg_connection
*conn
, VTOY_JSON
*json
)
3277 uint8_t timeouten
= 0;
3278 uint8_t autoselen
= 0;
3279 persistence_node
*node
= NULL
;
3280 data_persistence
*data
= NULL
;
3282 vtoy_json_get_int(json
, "index", &index
);
3283 vtoy_json_get_int(json
, "id", &id
);
3285 vtoy_json_get_bool(json
, "timeouten", &timeouten
);
3286 vtoy_json_get_bool(json
, "autoselen", &autoselen
);
3288 data
= g_data_persistence
+ index
;
3292 for (node
= data
->list
; node
; node
= node
->next
)
3296 node
->timeouten
= (int)timeouten
;
3297 node
->autoselen
= (int)autoselen
;
3298 VTOY_JSON_INT("timeout", node
->timeout
);
3299 VTOY_JSON_INT("autosel", node
->autosel
);
3305 ret
= ventoy_data_save_all();
3307 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3312 static int ventoy_api_persistence_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
3316 const char *path
= NULL
;
3317 persistence_node
*node
= NULL
;
3318 persistence_node
*cur
= NULL
;
3319 data_persistence
*data
= NULL
;
3320 VTOY_JSON
*array
= NULL
;
3322 vtoy_json_get_int(json
, "index", &index
);
3323 data
= g_data_persistence
+ index
;
3325 array
= vtoy_json_find_item(json
, JSON_TYPE_ARRAY
, "backend");
3326 path
= VTOY_JSON_STR_EX("path");
3329 node
= zalloc(sizeof(persistence_node
));
3332 node
->timeouten
= 0;
3333 node
->autoselen
= 0;
3336 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
3337 node
->list
= ventoy_path_node_add_array(array
);
3339 vtoy_list_add(data
->list
, cur
, node
);
3343 ret
= ventoy_data_save_all();
3345 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3349 static int ventoy_api_persistence_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
3353 const char *path
= NULL
;
3354 persistence_node
*last
= NULL
;
3355 persistence_node
*next
= NULL
;
3356 persistence_node
*node
= NULL
;
3357 data_persistence
*data
= NULL
;
3359 vtoy_json_get_int(json
, "index", &index
);
3360 data
= g_data_persistence
+ index
;
3362 path
= VTOY_JSON_STR_EX("path");
3365 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
3367 for (node
= data
->list
; node
; node
= next
)
3370 ventoy_free_path_node_list(node
->list
);
3377 vtoy_list_del_ex(last
, node
, data
->list
, path
, ventoy_free_path_node_list
);
3381 ret
= ventoy_data_save_all();
3383 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3387 static int ventoy_api_persistence_add_inner(struct mg_connection
*conn
, VTOY_JSON
*json
)
3391 const char *path
= NULL
;
3392 const char *outpath
= NULL
;
3393 path_node
*pcur
= NULL
;
3394 path_node
*pnode
= NULL
;
3395 persistence_node
*node
= NULL
;
3396 data_persistence
*data
= NULL
;
3398 vtoy_json_get_int(json
, "index", &index
);
3399 data
= g_data_persistence
+ index
;
3401 path
= VTOY_JSON_STR_EX("path");
3402 outpath
= VTOY_JSON_STR_EX("outpath");
3403 if (path
&& outpath
)
3405 for (node
= data
->list
; node
; node
= node
->next
)
3407 if (strcmp(outpath
, node
->path
) == 0)
3409 pnode
= zalloc(sizeof(path_node
));
3412 scnprintf(pnode
->path
, sizeof(pnode
->path
), "%s", path
);
3413 vtoy_list_add(node
->list
, pcur
, pnode
);
3421 ret
= ventoy_data_save_all();
3423 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3427 static int ventoy_api_persistence_del_inner(struct mg_connection
*conn
, VTOY_JSON
*json
)
3431 const char *path
= NULL
;
3432 const char *outpath
= NULL
;
3433 path_node
*plast
= NULL
;
3434 path_node
*pnode
= NULL
;
3435 persistence_node
*node
= NULL
;
3436 data_persistence
*data
= NULL
;
3438 vtoy_json_get_int(json
, "index", &index
);
3439 data
= g_data_persistence
+ index
;
3441 path
= VTOY_JSON_STR_EX("path");
3442 outpath
= VTOY_JSON_STR_EX("outpath");
3443 if (path
&& outpath
)
3445 for (node
= data
->list
; node
; node
= node
->next
)
3447 if (strcmp(outpath
, node
->path
) == 0)
3449 vtoy_list_del(plast
, pnode
, node
->list
, path
);
3455 ret
= ventoy_data_save_all();
3457 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3465 void ventoy_data_default_injection(data_injection
*data
)
3467 memset(data
, 0, sizeof(data_injection
));
3470 int ventoy_data_cmp_injection(data_injection
*data1
, data_injection
*data2
)
3472 injection_node
*list1
= NULL
;
3473 injection_node
*list2
= NULL
;
3475 if (NULL
== data1
->list
&& NULL
== data2
->list
)
3479 else if (data1
->list
&& data2
->list
)
3481 list1
= data1
->list
;
3482 list2
= data2
->list
;
3484 while (list1
&& list2
)
3486 if ((list1
->type
!= list2
->type
) ||
3487 strcmp(list1
->path
, list2
->path
) ||
3488 strcmp(list1
->archive
, list2
->archive
))
3493 list1
= list1
->next
;
3494 list2
= list2
->next
;
3497 if (list1
== NULL
&& list2
== NULL
)
3512 int ventoy_data_save_injection(data_injection
*data
, const char *title
, char *buf
, int buflen
)
3515 injection_node
*node
= NULL
;
3517 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
3519 VTOY_JSON_FMT_KEY_L(L1
, title
);
3520 VTOY_JSON_FMT_ARY_BEGIN_N();
3522 for (node
= data
->list
; node
; node
= node
->next
)
3524 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
3526 if (node
->type
== 0)
3528 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "image", node
->path
);
3532 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "parent", node
->path
);
3534 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "archive", node
->archive
);
3536 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
3539 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
3540 VTOY_JSON_FMT_END(pos
);
3546 int ventoy_data_json_injection(data_injection
*data
, char *buf
, int buflen
)
3550 injection_node
*node
= NULL
;
3552 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
3553 VTOY_JSON_FMT_ARY_BEGIN();
3555 for (node
= data
->list
; node
; node
= node
->next
)
3557 VTOY_JSON_FMT_OBJ_BEGIN();
3559 VTOY_JSON_FMT_UINT("type", node
->type
);
3560 VTOY_JSON_FMT_STRN("path", node
->path
);
3562 if (node
->type
== 0)
3564 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
3568 valid
= ventoy_is_directory_exist("%s%s", g_cur_dir
, node
->path
);
3570 VTOY_JSON_FMT_SINT("valid", valid
);
3572 VTOY_JSON_FMT_STRN("archive", node
->archive
);
3574 valid
= ventoy_is_file_exist("%s%s", g_cur_dir
, node
->archive
);
3575 VTOY_JSON_FMT_SINT("archive_valid", valid
);
3577 VTOY_JSON_FMT_OBJ_ENDEX();
3580 VTOY_JSON_FMT_ARY_END();
3581 VTOY_JSON_FMT_END(pos
);
3587 static int ventoy_api_get_injection(struct mg_connection
*conn
, VTOY_JSON
*json
)
3589 api_get_func(conn
, json
, injection
);
3593 static int ventoy_api_save_injection(struct mg_connection
*conn
, VTOY_JSON
*json
)
3596 ret
= ventoy_data_save_all();
3598 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3602 static int ventoy_api_injection_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
3607 const char *path
= NULL
;
3608 const char *archive
= NULL
;
3609 injection_node
*node
= NULL
;
3610 injection_node
*cur
= NULL
;
3611 data_injection
*data
= NULL
;
3613 vtoy_json_get_int(json
, "index", &index
);
3614 data
= g_data_injection
+ index
;
3616 vtoy_json_get_int(json
, "type", &type
);
3618 path
= VTOY_JSON_STR_EX("path");
3619 archive
= VTOY_JSON_STR_EX("archive");
3620 if (path
&& archive
)
3622 node
= zalloc(sizeof(injection_node
));
3627 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
3628 scnprintf(node
->archive
, sizeof(node
->archive
), "%s", archive
);
3630 vtoy_list_add(data
->list
, cur
, node
);
3634 ret
= ventoy_data_save_all();
3636 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3640 static int ventoy_api_injection_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
3644 const char *path
= NULL
;
3645 injection_node
*last
= NULL
;
3646 injection_node
*node
= NULL
;
3647 data_injection
*data
= NULL
;
3649 vtoy_json_get_int(json
, "index", &index
);
3650 data
= g_data_injection
+ index
;
3652 path
= VTOY_JSON_STR_EX("path");
3655 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
3657 vtoy_list_free(injection_node
, data
->list
);
3661 vtoy_list_del(last
, node
, data
->list
, path
);
3665 ret
= ventoy_data_save_all();
3667 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3674 static int ventoy_api_preview_json(struct mg_connection
*conn
, VTOY_JSON
*json
)
3679 char *encodebuf
= NULL
;
3683 len
= ventoy_data_real_save_all(0);
3684 encodebuf
= ventoy_base64_encode(JSON_SAVE_BUFFER
, len
, &encodelen
);
3685 encodebuf
[encodelen
] = 0;
3687 VTOY_JSON_FMT_BEGIN(pos
, JSON_BUFFER
, JSON_BUF_MAX
);
3688 VTOY_JSON_FMT_OBJ_BEGIN();
3689 VTOY_JSON_FMT_STRN("json", encodebuf
);
3690 VTOY_JSON_FMT_OBJ_END();
3691 VTOY_JSON_FMT_END(pos
);
3695 ventoy_json_buffer(conn
, JSON_BUFFER
, pos
);
3703 int ventoy_data_save_all(void)
3705 ventoy_set_writeback_event();
3709 int ventoy_data_real_save_all(int apilock
)
3717 pthread_mutex_lock(&g_api_mutex
);
3720 ssprintf(pos
, JSON_SAVE_BUFFER
, JSON_BUF_MAX
, "{\n");
3722 ventoy_save_plug(control
);
3723 ventoy_save_plug(theme
);
3724 ventoy_save_plug(menu_alias
);
3725 ventoy_save_plug(menu_tip
);
3726 ventoy_save_plug(menu_class
);
3727 ventoy_save_plug(auto_install
);
3728 ventoy_save_plug(persistence
);
3729 ventoy_save_plug(injection
);
3730 ventoy_save_plug(conf_replace
);
3731 ventoy_save_plug(password
);
3732 ventoy_save_plug(image_list
);
3733 ventoy_save_plug(auto_memdisk
);
3734 ventoy_save_plug(dud
);
3736 if (JSON_SAVE_BUFFER
[pos
- 1] == '\n' && JSON_SAVE_BUFFER
[pos
- 2] == ',')
3738 JSON_SAVE_BUFFER
[pos
- 2] = '\n';
3741 ssprintf(pos
, JSON_SAVE_BUFFER
, JSON_BUF_MAX
, "}\n");
3745 pthread_mutex_unlock(&g_api_mutex
);
3751 int ventoy_http_writeback(void)
3757 ventoy_get_json_path(filename
, NULL
);
3759 pos
= ventoy_data_real_save_all(1);
3762 printf("%s", JSON_SAVE_BUFFER
);
3765 ret
= ventoy_write_buf_to_file(filename
, JSON_SAVE_BUFFER
, pos
);
3768 vlog("Failed to write ventoy.json file.\n");
3769 g_sysinfo
.config_save_error
= 1;
3776 static JSON_CB g_ventoy_json_cb
[] =
3778 { "sysinfo", ventoy_api_sysinfo
},
3779 { "handshake", ventoy_api_handshake
},
3780 { "check_path", ventoy_api_check_exist
},
3781 { "check_path2", ventoy_api_check_exist2
},
3782 { "check_fuzzy", ventoy_api_check_fuzzy
},
3784 { "device_info", ventoy_api_device_info
},
3786 { "get_control", ventoy_api_get_control
},
3787 { "save_control", ventoy_api_save_control
},
3789 { "get_theme", ventoy_api_get_theme
},
3790 { "save_theme", ventoy_api_save_theme
},
3791 { "theme_add_file", ventoy_api_theme_add_file
},
3792 { "theme_del_file", ventoy_api_theme_del_file
},
3793 { "theme_add_font", ventoy_api_theme_add_font
},
3794 { "theme_del_font", ventoy_api_theme_del_font
},
3796 { "get_alias", ventoy_api_get_alias
},
3797 { "save_alias", ventoy_api_save_alias
},
3798 { "alias_add", ventoy_api_alias_add
},
3799 { "alias_del", ventoy_api_alias_del
},
3801 { "get_tip", ventoy_api_get_tip
},
3802 { "save_tip", ventoy_api_save_tip
},
3803 { "tip_add", ventoy_api_tip_add
},
3804 { "tip_del", ventoy_api_tip_del
},
3806 { "get_class", ventoy_api_get_class
},
3807 { "save_class", ventoy_api_save_class
},
3808 { "class_add", ventoy_api_class_add
},
3809 { "class_del", ventoy_api_class_del
},
3811 { "get_auto_memdisk", ventoy_api_get_auto_memdisk
},
3812 { "save_auto_memdisk", ventoy_api_save_auto_memdisk
},
3813 { "auto_memdisk_add", ventoy_api_auto_memdisk_add
},
3814 { "auto_memdisk_del", ventoy_api_auto_memdisk_del
},
3816 { "get_image_list", ventoy_api_get_image_list
},
3817 { "save_image_list", ventoy_api_save_image_list
},
3818 { "image_list_add", ventoy_api_image_list_add
},
3819 { "image_list_del", ventoy_api_image_list_del
},
3821 { "get_conf_replace", ventoy_api_get_conf_replace
},
3822 { "save_conf_replace", ventoy_api_save_conf_replace
},
3823 { "conf_replace_add", ventoy_api_conf_replace_add
},
3824 { "conf_replace_del", ventoy_api_conf_replace_del
},
3826 { "get_dud", ventoy_api_get_dud
},
3827 { "save_dud", ventoy_api_save_dud
},
3828 { "dud_add", ventoy_api_dud_add
},
3829 { "dud_del", ventoy_api_dud_del
},
3830 { "dud_add_inner", ventoy_api_dud_add_inner
},
3831 { "dud_del_inner", ventoy_api_dud_del_inner
},
3833 { "get_auto_install", ventoy_api_get_auto_install
},
3834 { "save_auto_install", ventoy_api_save_auto_install
},
3835 { "auto_install_add", ventoy_api_auto_install_add
},
3836 { "auto_install_del", ventoy_api_auto_install_del
},
3837 { "auto_install_add_inner", ventoy_api_auto_install_add_inner
},
3838 { "auto_install_del_inner", ventoy_api_auto_install_del_inner
},
3840 { "get_persistence", ventoy_api_get_persistence
},
3841 { "save_persistence", ventoy_api_save_persistence
},
3842 { "persistence_add", ventoy_api_persistence_add
},
3843 { "persistence_del", ventoy_api_persistence_del
},
3844 { "persistence_add_inner", ventoy_api_persistence_add_inner
},
3845 { "persistence_del_inner", ventoy_api_persistence_del_inner
},
3847 { "get_password", ventoy_api_get_password
},
3848 { "save_password", ventoy_api_save_password
},
3849 { "password_add", ventoy_api_password_add
},
3850 { "password_del", ventoy_api_password_del
},
3852 { "get_injection", ventoy_api_get_injection
},
3853 { "save_injection", ventoy_api_save_injection
},
3854 { "injection_add", ventoy_api_injection_add
},
3855 { "injection_del", ventoy_api_injection_del
},
3856 { "preview_json", ventoy_api_preview_json
},
3860 static int ventoy_json_handler(struct mg_connection
*conn
, VTOY_JSON
*json
, char *jsonstr
)
3863 const char *method
= NULL
;
3865 method
= vtoy_json_get_string_ex(json
, "method");
3868 ventoy_json_result(conn
, VTOY_JSON_SUCCESS_RET
);
3872 if (strcmp(method
, "handshake") == 0)
3874 ventoy_api_handshake(conn
, json
);
3878 for (i
= 0; i
< (int)(sizeof(g_ventoy_json_cb
) / sizeof(g_ventoy_json_cb
[0])); i
++)
3880 if (strcmp(method
, g_ventoy_json_cb
[i
].method
) == 0)
3882 g_ventoy_json_cb
[i
].callback(conn
, json
);
3890 static int ventoy_request_handler(struct mg_connection
*conn
)
3894 VTOY_JSON
*json
= NULL
;
3895 char *post_data_buf
= NULL
;
3896 const struct mg_request_info
*ri
= NULL
;
3897 char stack_buf
[512];
3899 ri
= mg_get_request_info(conn
);
3901 if (strcmp(ri
->uri
, "/vtoy/json") == 0)
3903 if (ri
->content_length
> 500)
3905 post_data_buf
= malloc((int)(ri
->content_length
+ 4));
3906 post_buf_len
= (int)(ri
->content_length
+ 1);
3910 post_data_buf
= stack_buf
;
3911 post_buf_len
= sizeof(stack_buf
);
3914 post_data_len
= mg_read(conn
, post_data_buf
, post_buf_len
);
3915 post_data_buf
[post_data_len
] = 0;
3917 json
= vtoy_json_create();
3918 if (JSON_SUCCESS
== vtoy_json_parse(json
, post_data_buf
))
3920 pthread_mutex_lock(&g_api_mutex
);
3921 ventoy_json_handler(conn
, json
->pstChild
, post_data_buf
);
3922 pthread_mutex_unlock(&g_api_mutex
);
3926 ventoy_json_result(conn
, VTOY_JSON_INVALID_RET
);
3929 vtoy_json_destroy(json
);
3931 if (post_data_buf
!= stack_buf
)
3933 free(post_data_buf
);
3943 const char *ventoy_web_openfile(const struct mg_connection
*conn
, const char *path
, size_t *data_len
)
3945 ventoy_file
*node
= NULL
;
3954 node
= ventoy_tar_find_file(path
);
3957 *data_len
= node
->size
;
3969 static int ventoy_parse_control(VTOY_JSON
*json
, void *p
)
3972 VTOY_JSON
*node
= NULL
;
3973 VTOY_JSON
*child
= NULL
;
3974 data_control
*data
= (data_control
*)p
;
3976 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
3981 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
3983 if (node
->enDataType
== JSON_TYPE_OBJECT
)
3985 child
= node
->pstChild
;
3987 if (strcmp(child
->pcName
, "VTOY_DEFAULT_MENU_MODE") == 0)
3989 CONTROL_PARSE_INT_DEF_0(child
, data
->default_menu_mode
);
3991 else if (strcmp(child
->pcName
, "VTOY_WIN11_BYPASS_CHECK") == 0)
3993 CONTROL_PARSE_INT_DEF_0(child
, data
->win11_bypass_check
);
3995 else if (strcmp(child
->pcName
, "VTOY_LINUX_REMOUNT") == 0)
3997 CONTROL_PARSE_INT_DEF_0(child
, data
->linux_remount
);
3999 else if (strcmp(child
->pcName
, "VTOY_SECONDARY_BOOT_MENU") == 0)
4001 CONTROL_PARSE_INT_DEF_1(child
, data
->secondary_menu
);
4003 else if (strcmp(child
->pcName
, "VTOY_SHOW_PASSWORD_ASTERISK") == 0)
4005 CONTROL_PARSE_INT_DEF_1(child
, data
->password_asterisk
);
4007 else if (strcmp(child
->pcName
, "VTOY_TREE_VIEW_MENU_STYLE") == 0)
4009 CONTROL_PARSE_INT_DEF_0(child
, data
->treeview_style
);
4011 else if (strcmp(child
->pcName
, "VTOY_FILT_DOT_UNDERSCORE_FILE") == 0)
4013 CONTROL_PARSE_INT_DEF_1(child
, data
->filter_dot_underscore
);
4015 else if (strcmp(child
->pcName
, "VTOY_SORT_CASE_SENSITIVE") == 0)
4017 CONTROL_PARSE_INT_DEF_0(child
, data
->sort_casesensitive
);
4019 else if (strcmp(child
->pcName
, "VTOY_MAX_SEARCH_LEVEL") == 0)
4021 if (strcmp(child
->unData
.pcStrVal
, "max") == 0)
4023 data
->max_search_level
= -1;
4027 data
->max_search_level
= (int)strtol(child
->unData
.pcStrVal
, NULL
, 10);
4030 else if (strcmp(child
->pcName
, "VTOY_DEFAULT_SEARCH_ROOT") == 0)
4032 strlcpy(data
->default_search_root
, child
->unData
.pcStrVal
);
4034 else if (strcmp(child
->pcName
, "VTOY_DEFAULT_IMAGE") == 0)
4036 strlcpy(data
->default_image
, child
->unData
.pcStrVal
);
4038 else if (strcmp(child
->pcName
, "VTOY_DEFAULT_KBD_LAYOUT") == 0)
4040 for (i
= 0; g_ventoy_kbd_layout
[i
]; i
++)
4042 if (strcmp(child
->unData
.pcStrVal
, g_ventoy_kbd_layout
[i
]) == 0)
4044 strlcpy(data
->default_kbd_layout
, child
->unData
.pcStrVal
);
4049 else if (strcmp(child
->pcName
, "VTOY_HELP_TXT_LANGUAGE") == 0)
4051 for (i
= 0; g_ventoy_help_lang
[i
][0]; i
++)
4053 if (strcmp(child
->unData
.pcStrVal
, g_ventoy_help_lang
[i
]) == 0)
4055 strlcpy(data
->help_text_language
, child
->unData
.pcStrVal
);
4060 else if (strcmp(child
->pcName
, "VTOY_MENU_TIMEOUT") == 0)
4062 data
->menu_timeout
= (int)strtol(child
->unData
.pcStrVal
, NULL
, 10);
4064 else if (strcmp(child
->pcName
, "VTOY_SECONDARY_TIMEOUT") == 0)
4066 data
->secondary_menu_timeout
= (int)strtol(child
->unData
.pcStrVal
, NULL
, 10);
4068 else if (strcmp(child
->pcName
, "VTOY_VHD_NO_WARNING") == 0)
4070 CONTROL_PARSE_INT_DEF_0(child
, data
->vhd_no_warning
);
4072 else if (strcmp(child
->pcName
, "VTOY_FILE_FLT_ISO") == 0)
4074 CONTROL_PARSE_INT_DEF_0(child
, data
->filter_iso
);
4076 else if (strcmp(child
->pcName
, "VTOY_FILE_FLT_IMG") == 0)
4078 CONTROL_PARSE_INT_DEF_0(child
, data
->filter_img
);
4080 else if (strcmp(child
->pcName
, "VTOY_FILE_FLT_EFI") == 0)
4082 CONTROL_PARSE_INT_DEF_0(child
, data
->filter_efi
);
4084 else if (strcmp(child
->pcName
, "VTOY_FILE_FLT_WIM") == 0)
4086 CONTROL_PARSE_INT_DEF_0(child
, data
->filter_wim
);
4088 else if (strcmp(child
->pcName
, "VTOY_FILE_FLT_VHD") == 0)
4090 CONTROL_PARSE_INT_DEF_0(child
, data
->filter_vhd
);
4092 else if (strcmp(child
->pcName
, "VTOY_FILE_FLT_VTOY") == 0)
4094 CONTROL_PARSE_INT_DEF_0(child
, data
->filter_vtoy
);
4102 static int ventoy_parse_theme(VTOY_JSON
*json
, void *p
)
4104 const char *dismode
= NULL
;
4105 VTOY_JSON
*child
= NULL
;
4106 VTOY_JSON
*node
= NULL
;
4107 path_node
*tail
= NULL
;
4108 path_node
*pnode
= NULL
;
4109 data_theme
*data
= (data_theme
*)p
;
4111 if (json
->enDataType
!= JSON_TYPE_OBJECT
)
4116 child
= json
->pstChild
;
4118 dismode
= vtoy_json_get_string_ex(child
, "display_mode");
4119 vtoy_json_get_string(child
, "ventoy_left", sizeof(data
->ventoy_left
), data
->ventoy_left
);
4120 vtoy_json_get_string(child
, "ventoy_top", sizeof(data
->ventoy_top
), data
->ventoy_top
);
4121 vtoy_json_get_string(child
, "ventoy_color", sizeof(data
->ventoy_color
), data
->ventoy_color
);
4123 vtoy_json_get_int(child
, "default_file", &(data
->default_file
));
4124 vtoy_json_get_string(child
, "gfxmode", sizeof(data
->gfxmode
), data
->gfxmode
);
4125 vtoy_json_get_string(child
, "serial_param", sizeof(data
->serial_param
), data
->serial_param
);
4129 if (strcmp(dismode
, "CLI") == 0)
4131 data
->display_mode
= display_mode_cli
;
4133 else if (strcmp(dismode
, "serial") == 0)
4135 data
->display_mode
= display_mode_serial
;
4137 else if (strcmp(dismode
, "serial_console") == 0)
4139 data
->display_mode
= display_mode_ser_console
;
4143 data
->display_mode
= display_mode_gui
;
4147 node
= vtoy_json_find_item(child
, JSON_TYPE_STRING
, "file");
4150 data
->default_file
= 0;
4152 pnode
= zalloc(sizeof(path_node
));
4155 strlcpy(pnode
->path
, node
->unData
.pcStrVal
);
4156 data
->filelist
= pnode
;
4161 node
= vtoy_json_find_item(child
, JSON_TYPE_ARRAY
, "file");
4164 for (node
= node
->pstChild
; node
; node
= node
->pstNext
)
4166 if (node
->enDataType
== JSON_TYPE_STRING
)
4168 pnode
= zalloc(sizeof(path_node
));
4171 strlcpy(pnode
->path
, node
->unData
.pcStrVal
);
4179 data
->filelist
= tail
= pnode
;
4188 node
= vtoy_json_find_item(child
, JSON_TYPE_ARRAY
, "fonts");
4191 for (node
= node
->pstChild
; node
; node
= node
->pstNext
)
4193 if (node
->enDataType
== JSON_TYPE_STRING
)
4195 pnode
= zalloc(sizeof(path_node
));
4198 strlcpy(pnode
->path
, node
->unData
.pcStrVal
);
4199 if (data
->fontslist
)
4206 data
->fontslist
= tail
= pnode
;
4215 static int ventoy_parse_menu_alias(VTOY_JSON
*json
, void *p
)
4218 const char *path
= NULL
;
4219 const char *alias
= NULL
;
4220 data_alias
*data
= (data_alias
*)p
;
4221 data_alias_node
*tail
= NULL
;
4222 data_alias_node
*pnode
= NULL
;
4223 VTOY_JSON
*node
= NULL
;
4225 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4230 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4232 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4237 type
= path_type_file
;
4238 path
= vtoy_json_get_string_ex(node
->pstChild
, "image");
4241 path
= vtoy_json_get_string_ex(node
->pstChild
, "dir");
4242 type
= path_type_dir
;
4244 alias
= vtoy_json_get_string_ex(node
->pstChild
, "alias");
4248 pnode
= zalloc(sizeof(data_alias_node
));
4252 strlcpy(pnode
->path
, path
);
4253 strlcpy(pnode
->alias
, alias
);
4262 data
->list
= tail
= pnode
;
4271 static int ventoy_parse_menu_tip(VTOY_JSON
*json
, void *p
)
4274 const char *path
= NULL
;
4275 const char *tip
= NULL
;
4276 data_tip
*data
= (data_tip
*)p
;
4277 data_tip_node
*tail
= NULL
;
4278 data_tip_node
*pnode
= NULL
;
4279 VTOY_JSON
*node
= NULL
;
4280 VTOY_JSON
*tips
= NULL
;
4282 if (json
->enDataType
!= JSON_TYPE_OBJECT
)
4287 vtoy_json_get_string(json
->pstChild
, "left", sizeof(data
->left
), data
->left
);
4288 vtoy_json_get_string(json
->pstChild
, "top", sizeof(data
->top
), data
->top
);
4289 vtoy_json_get_string(json
->pstChild
, "color", sizeof(data
->color
), data
->color
);
4291 tips
= vtoy_json_find_item(json
->pstChild
, JSON_TYPE_ARRAY
, "tips");
4297 for (node
= tips
->pstChild
; node
; node
= node
->pstNext
)
4299 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4304 type
= path_type_file
;
4305 path
= vtoy_json_get_string_ex(node
->pstChild
, "image");
4308 path
= vtoy_json_get_string_ex(node
->pstChild
, "dir");
4309 type
= path_type_dir
;
4311 tip
= vtoy_json_get_string_ex(node
->pstChild
, "tip");
4315 pnode
= zalloc(sizeof(data_tip_node
));
4319 strlcpy(pnode
->path
, path
);
4320 strlcpy(pnode
->tip
, tip
);
4329 data
->list
= tail
= pnode
;
4337 static int ventoy_parse_menu_class(VTOY_JSON
*json
, void *p
)
4340 const char *path
= NULL
;
4341 const char *class = NULL
;
4342 data_class
*data
= (data_class
*)p
;
4343 data_class_node
*tail
= NULL
;
4344 data_class_node
*pnode
= NULL
;
4345 VTOY_JSON
*node
= NULL
;
4347 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4352 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4354 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4359 type
= class_type_key
;
4360 path
= vtoy_json_get_string_ex(node
->pstChild
, "key");
4363 type
= class_type_dir
;
4364 path
= vtoy_json_get_string_ex(node
->pstChild
, "dir");
4367 type
= class_type_parent
;
4368 path
= vtoy_json_get_string_ex(node
->pstChild
, "parent");
4371 class = vtoy_json_get_string_ex(node
->pstChild
, "class");
4375 pnode
= zalloc(sizeof(data_class_node
));
4379 strlcpy(pnode
->path
, path
);
4380 strlcpy(pnode
->class, class);
4389 data
->list
= tail
= pnode
;
4397 static int ventoy_parse_auto_install(VTOY_JSON
*json
, void *p
)
4405 const char *path
= NULL
;
4406 const char *file
= NULL
;
4407 data_auto_install
*data
= (data_auto_install
*)p
;
4408 auto_install_node
*tail
= NULL
;
4409 auto_install_node
*pnode
= NULL
;
4410 path_node
*pathnode
= NULL
;
4411 path_node
*pathtail
= NULL
;
4412 VTOY_JSON
*node
= NULL
;
4413 VTOY_JSON
*filelist
= NULL
;
4414 VTOY_JSON
*filenode
= NULL
;
4416 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4421 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4423 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4429 path
= vtoy_json_get_string_ex(node
->pstChild
, "image");
4432 path
= vtoy_json_get_string_ex(node
->pstChild
, "parent");
4440 file
= vtoy_json_get_string_ex(node
->pstChild
, "template");
4443 pnode
= zalloc(sizeof(auto_install_node
));
4448 strlcpy(pnode
->path
, path
);
4450 pathnode
= zalloc(sizeof(path_node
));
4453 strlcpy(pathnode
->path
, file
);
4454 pnode
->list
= pathnode
;
4468 data
->list
= tail
= pnode
;
4476 timeouten
= autoselen
= 0;
4477 if (JSON_SUCCESS
== vtoy_json_get_int(node
->pstChild
, "timeout", &timeout
))
4481 if (JSON_SUCCESS
== vtoy_json_get_int(node
->pstChild
, "autosel", &autosel
))
4486 filelist
= vtoy_json_find_item(node
->pstChild
, JSON_TYPE_ARRAY
, "template");
4492 pnode
= zalloc(sizeof(auto_install_node
));
4499 pnode
->autoselen
= autoselen
;
4500 pnode
->timeouten
= timeouten
;
4501 pnode
->timeout
= timeout
;
4502 pnode
->autosel
= autosel
;
4503 strlcpy(pnode
->path
, path
);
4506 for (filenode
= filelist
->pstChild
; filenode
; filenode
= filenode
->pstNext
)
4508 if (filenode
->enDataType
!= JSON_TYPE_STRING
)
4513 pathnode
= zalloc(sizeof(path_node
));
4517 strlcpy(pathnode
->path
, filenode
->unData
.pcStrVal
);
4521 pathtail
->next
= pathnode
;
4522 pathtail
= pathnode
;
4526 pnode
->list
= pathtail
= pathnode
;
4537 if (pnode
->autoselen
&& pnode
->autosel
> count
)
4549 data
->list
= tail
= pnode
;
4556 static int ventoy_parse_persistence(VTOY_JSON
*json
, void *p
)
4563 const char *path
= NULL
;
4564 const char *file
= NULL
;
4565 data_persistence
*data
= (data_persistence
*)p
;
4566 persistence_node
*tail
= NULL
;
4567 persistence_node
*pnode
= NULL
;
4568 path_node
*pathnode
= NULL
;
4569 path_node
*pathtail
= NULL
;
4570 VTOY_JSON
*node
= NULL
;
4571 VTOY_JSON
*filelist
= NULL
;
4572 VTOY_JSON
*filenode
= NULL
;
4574 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4579 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4581 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4586 path
= vtoy_json_get_string_ex(node
->pstChild
, "image");
4592 file
= vtoy_json_get_string_ex(node
->pstChild
, "backend");
4595 pnode
= zalloc(sizeof(persistence_node
));
4600 strlcpy(pnode
->path
, path
);
4602 pathnode
= zalloc(sizeof(path_node
));
4605 strlcpy(pathnode
->path
, file
);
4606 pnode
->list
= pathnode
;
4620 data
->list
= tail
= pnode
;
4628 timeouten
= autoselen
= 0;
4629 if (JSON_SUCCESS
== vtoy_json_get_int(node
->pstChild
, "timeout", &timeout
))
4633 if (JSON_SUCCESS
== vtoy_json_get_int(node
->pstChild
, "autosel", &autosel
))
4638 filelist
= vtoy_json_find_item(node
->pstChild
, JSON_TYPE_ARRAY
, "backend");
4644 pnode
= zalloc(sizeof(persistence_node
));
4651 pnode
->autoselen
= autoselen
;
4652 pnode
->timeouten
= timeouten
;
4653 pnode
->timeout
= timeout
;
4654 pnode
->autosel
= autosel
;
4655 strlcpy(pnode
->path
, path
);
4658 for (filenode
= filelist
->pstChild
; filenode
; filenode
= filenode
->pstNext
)
4660 if (filenode
->enDataType
!= JSON_TYPE_STRING
)
4665 pathnode
= zalloc(sizeof(path_node
));
4669 strlcpy(pathnode
->path
, filenode
->unData
.pcStrVal
);
4673 pathtail
->next
= pathnode
;
4674 pathtail
= pathnode
;
4678 pnode
->list
= pathtail
= pathnode
;
4689 if (pnode
->autoselen
&& pnode
->autosel
> count
)
4701 data
->list
= tail
= pnode
;
4708 static int ventoy_parse_injection(VTOY_JSON
*json
, void *p
)
4711 const char *path
= NULL
;
4712 const char *archive
= NULL
;
4713 data_injection
*data
= (data_injection
*)p
;
4714 injection_node
*tail
= NULL
;
4715 injection_node
*pnode
= NULL
;
4716 VTOY_JSON
*node
= NULL
;
4718 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4723 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4725 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4731 path
= vtoy_json_get_string_ex(node
->pstChild
, "image");
4734 path
= vtoy_json_get_string_ex(node
->pstChild
, "parent");
4737 archive
= vtoy_json_get_string_ex(node
->pstChild
, "archive");
4739 if (path
&& archive
)
4741 pnode
= zalloc(sizeof(injection_node
));
4745 strlcpy(pnode
->path
, path
);
4746 strlcpy(pnode
->archive
, archive
);
4755 data
->list
= tail
= pnode
;
4763 static int ventoy_parse_conf_replace(VTOY_JSON
*json
, void *p
)
4766 const char *path
= NULL
;
4767 const char *org
= NULL
;
4768 const char *new = NULL
;
4769 data_conf_replace
*data
= (data_conf_replace
*)p
;
4770 conf_replace_node
*tail
= NULL
;
4771 conf_replace_node
*pnode
= NULL
;
4772 VTOY_JSON
*node
= NULL
;
4774 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4779 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4781 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4786 path
= vtoy_json_get_string_ex(node
->pstChild
, "iso");
4787 org
= vtoy_json_get_string_ex(node
->pstChild
, "org");
4788 new = vtoy_json_get_string_ex(node
->pstChild
, "new");
4791 vtoy_json_get_int(node
->pstChild
, "img", &img
);
4793 if (path
&& org
&& new)
4795 pnode
= zalloc(sizeof(conf_replace_node
));
4798 strlcpy(pnode
->path
, path
);
4799 strlcpy(pnode
->org
, org
);
4800 strlcpy(pnode
->new, new);
4813 data
->list
= tail
= pnode
;
4821 static int ventoy_parse_password(VTOY_JSON
*json
, void *p
)
4824 const char *bootpwd
= NULL
;
4825 const char *isopwd
= NULL
;
4826 const char *wimpwd
= NULL
;
4827 const char *imgpwd
= NULL
;
4828 const char *efipwd
= NULL
;
4829 const char *vhdpwd
= NULL
;
4830 const char *vtoypwd
= NULL
;
4831 const char *path
= NULL
;
4832 const char *pwd
= NULL
;
4833 data_password
*data
= (data_password
*)p
;
4834 menu_password
*tail
= NULL
;
4835 menu_password
*pnode
= NULL
;
4836 VTOY_JSON
*node
= NULL
;
4837 VTOY_JSON
*menupwd
= NULL
;
4839 if (json
->enDataType
!= JSON_TYPE_OBJECT
)
4844 bootpwd
= vtoy_json_get_string_ex(json
->pstChild
, "bootpwd");
4845 isopwd
= vtoy_json_get_string_ex(json
->pstChild
, "isopwd");
4846 wimpwd
= vtoy_json_get_string_ex(json
->pstChild
, "wimpwd");
4847 imgpwd
= vtoy_json_get_string_ex(json
->pstChild
, "imgpwd");
4848 efipwd
= vtoy_json_get_string_ex(json
->pstChild
, "efipwd");
4849 vhdpwd
= vtoy_json_get_string_ex(json
->pstChild
, "vhdpwd");
4850 vtoypwd
= vtoy_json_get_string_ex(json
->pstChild
, "vtoypwd");
4853 if (bootpwd
) strlcpy(data
->bootpwd
, bootpwd
);
4854 if (isopwd
) strlcpy(data
->isopwd
, isopwd
);
4855 if (wimpwd
) strlcpy(data
->wimpwd
, wimpwd
);
4856 if (imgpwd
) strlcpy(data
->imgpwd
, imgpwd
);
4857 if (efipwd
) strlcpy(data
->efipwd
, efipwd
);
4858 if (vhdpwd
) strlcpy(data
->vhdpwd
, vhdpwd
);
4859 if (vtoypwd
) strlcpy(data
->vtoypwd
, vtoypwd
);
4862 menupwd
= vtoy_json_find_item(json
->pstChild
, JSON_TYPE_ARRAY
, "menupwd");
4868 for (node
= menupwd
->pstChild
; node
; node
= node
->pstNext
)
4870 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4876 path
= vtoy_json_get_string_ex(node
->pstChild
, "file");
4879 path
= vtoy_json_get_string_ex(node
->pstChild
, "parent");
4882 pwd
= vtoy_json_get_string_ex(node
->pstChild
, "pwd");
4886 pnode
= zalloc(sizeof(menu_password
));
4890 strlcpy(pnode
->path
, path
);
4891 strlcpy(pnode
->pwd
, pwd
);
4900 data
->list
= tail
= pnode
;
4909 static int ventoy_parse_image_list_real(VTOY_JSON
*json
, int type
, void *p
)
4911 VTOY_JSON
*node
= NULL
;
4912 data_image_list
*data
= (data_image_list
*)p
;
4913 path_node
*tail
= NULL
;
4914 path_node
*pnode
= NULL
;
4916 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4923 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4925 if (node
->enDataType
== JSON_TYPE_STRING
)
4927 pnode
= zalloc(sizeof(path_node
));
4930 strlcpy(pnode
->path
, node
->unData
.pcStrVal
);
4938 data
->list
= tail
= pnode
;
4946 static int ventoy_parse_image_blacklist(VTOY_JSON
*json
, void *p
)
4948 return ventoy_parse_image_list_real(json
, 1, p
);
4950 static int ventoy_parse_image_list(VTOY_JSON
*json
, void *p
)
4952 return ventoy_parse_image_list_real(json
, 0, p
);
4955 static int ventoy_parse_auto_memdisk(VTOY_JSON
*json
, void *p
)
4957 VTOY_JSON
*node
= NULL
;
4958 data_auto_memdisk
*data
= (data_auto_memdisk
*)p
;
4959 path_node
*tail
= NULL
;
4960 path_node
*pnode
= NULL
;
4962 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4967 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4969 if (node
->enDataType
== JSON_TYPE_STRING
)
4971 pnode
= zalloc(sizeof(path_node
));
4974 strlcpy(pnode
->path
, node
->unData
.pcStrVal
);
4982 data
->list
= tail
= pnode
;
4990 static int ventoy_parse_dud(VTOY_JSON
*json
, void *p
)
4993 const char *path
= NULL
;
4994 const char *file
= NULL
;
4995 data_dud
*data
= (data_dud
*)p
;
4996 dud_node
*tail
= NULL
;
4997 dud_node
*pnode
= NULL
;
4998 path_node
*pathnode
= NULL
;
4999 path_node
*pathtail
= NULL
;
5000 VTOY_JSON
*node
= NULL
;
5001 VTOY_JSON
*filelist
= NULL
;
5002 VTOY_JSON
*filenode
= NULL
;
5004 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
5009 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
5011 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
5016 path
= vtoy_json_get_string_ex(node
->pstChild
, "image");
5022 file
= vtoy_json_get_string_ex(node
->pstChild
, "dud");
5025 pnode
= zalloc(sizeof(dud_node
));
5028 strlcpy(pnode
->path
, path
);
5030 pathnode
= zalloc(sizeof(path_node
));
5033 strlcpy(pathnode
->path
, file
);
5034 pnode
->list
= pathnode
;
5048 data
->list
= tail
= pnode
;
5055 filelist
= vtoy_json_find_item(node
->pstChild
, JSON_TYPE_ARRAY
, "dud");
5061 pnode
= zalloc(sizeof(dud_node
));
5067 strlcpy(pnode
->path
, path
);
5069 for (filenode
= filelist
->pstChild
; filenode
; filenode
= filenode
->pstNext
)
5071 if (filenode
->enDataType
!= JSON_TYPE_STRING
)
5076 pathnode
= zalloc(sizeof(path_node
));
5079 strlcpy(pathnode
->path
, filenode
->unData
.pcStrVal
);
5084 pathtail
->next
= pathnode
;
5085 pathtail
= pathnode
;
5089 pnode
->list
= pathtail
= pathnode
;
5107 data
->list
= tail
= pnode
;
5121 static int ventoy_load_old_json(const char *filename
)
5126 char *buffer
= NULL
;
5127 unsigned char *start
= NULL
;
5128 VTOY_JSON
*json
= NULL
;
5129 VTOY_JSON
*node
= NULL
;
5130 VTOY_JSON
*next
= NULL
;
5132 ret
= ventoy_read_file_to_buf(filename
, 4, (void **)&buffer
, &buflen
);
5135 vlog("Failed to read old ventoy.json file.\n");
5140 start
= (unsigned char *)buffer
;
5142 if (start
[0] == 0xef && start
[1] == 0xbb && start
[2] == 0xbf)
5146 else if ((start
[0] == 0xff && start
[1] == 0xfe) || (start
[0] == 0xfe && start
[1] == 0xff))
5148 vlog("ventoy.json is in UCS-2 encoding, ignore it.\n");
5153 json
= vtoy_json_create();
5160 if (vtoy_json_parse_ex(json
, buffer
+ offset
, buflen
- offset
) == JSON_SUCCESS
)
5162 vlog("parse ventoy.json success\n");
5164 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
5165 for (next
= node
->pstNext
; next
; next
= next
->pstNext
)
5167 if (node
->pcName
&& next
->pcName
&& strcmp(node
->pcName
, next
->pcName
) == 0)
5169 vlog("ventoy.json contains duplicate key <%s>.\n", node
->pcName
);
5170 g_sysinfo
.invalid_config
= 1;
5176 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
5178 ventoy_parse_json(control
);
5179 ventoy_parse_json(theme
);
5180 ventoy_parse_json(menu_alias
);
5181 ventoy_parse_json(menu_tip
);
5182 ventoy_parse_json(menu_class
);
5183 ventoy_parse_json(auto_install
);
5184 ventoy_parse_json(persistence
);
5185 ventoy_parse_json(injection
);
5186 ventoy_parse_json(conf_replace
);
5187 ventoy_parse_json(password
);
5188 ventoy_parse_json(image_list
);
5189 ventoy_parse_json(image_blacklist
);
5190 ventoy_parse_json(auto_memdisk
);
5191 ventoy_parse_json(dud
);
5196 vlog("ventoy.json has syntax error.\n");
5197 g_sysinfo
.syntax_error
= 1;
5202 vtoy_json_destroy(json
);
5209 int ventoy_http_start(const char *ip
, const char *port
)
5215 char backupname
[128];
5216 struct mg_callbacks callbacks
;
5217 const char *options
[] =
5219 "listening_ports", "24681",
5220 "document_root", "www",
5221 "index_files", "index.html",
5222 "num_threads", "16",
5223 "error_log_file", LOG_FILE
,
5224 "request_timeout_ms", "10000",
5228 for (i
= 0; i
<= bios_max
; i
++)
5230 ventoy_data_default_control(g_data_control
+ i
);
5231 ventoy_data_default_theme(g_data_theme
+ i
);
5232 ventoy_data_default_menu_alias(g_data_menu_alias
+ i
);
5233 ventoy_data_default_menu_class(g_data_menu_class
+ i
);
5234 ventoy_data_default_menu_tip(g_data_menu_tip
+ i
);
5235 ventoy_data_default_auto_install(g_data_auto_install
+ i
);
5236 ventoy_data_default_persistence(g_data_persistence
+ i
);
5237 ventoy_data_default_injection(g_data_injection
+ i
);
5238 ventoy_data_default_conf_replace(g_data_conf_replace
+ i
);
5239 ventoy_data_default_password(g_data_password
+ i
);
5240 ventoy_data_default_image_list(g_data_image_list
+ i
);
5241 ventoy_data_default_auto_memdisk(g_data_auto_memdisk
+ i
);
5242 ventoy_data_default_dud(g_data_dud
+ i
);
5245 ventoy_get_json_path(filename
, backupname
);
5246 if (ventoy_is_file_exist("%s", filename
))
5248 ventoy_copy_file(filename
, backupname
);
5249 ret
= ventoy_load_old_json(filename
);
5252 ventoy_data_real_save_all(0);
5257 scnprintf(addr
, sizeof(addr
), "%s:%s", ip
, port
);
5260 memset(&callbacks
, 0, sizeof(callbacks
));
5261 callbacks
.begin_request
= ventoy_request_handler
;
5263 callbacks
.open_file
= ventoy_web_openfile
;
5265 g_ventoy_http_ctx
= mg_start(&callbacks
, NULL
, options
);
5267 ventoy_start_writeback_thread(ventoy_http_writeback
);
5269 return g_ventoy_http_ctx
? 0 : 1;
5272 int ventoy_http_stop(void)
5274 if (g_ventoy_http_ctx
)
5276 mg_stop(g_ventoy_http_ctx
);
5279 ventoy_stop_writeback_thread();
5283 int ventoy_http_init(void)
5288 char *Buffer
= NULL
;
5291 ventoy_read_file_to_buf("www/helplist", 4, (void **)&Buffer
, &BufLen
);
5294 for (i
= 0; i
< BufLen
/ 5; i
++)
5296 memcpy(g_ventoy_help_lang
[i
], Buffer
+ i
* 5, 5);
5297 g_ventoy_help_lang
[i
][5] = 0;
5303 file
= ventoy_tar_find_file("www/helplist");
5306 for (i
= 0; i
< file
->size
/ 5; i
++)
5308 memcpy(g_ventoy_help_lang
[i
], (char *)(file
->addr
) + i
* 5, 5);
5309 g_ventoy_help_lang
[i
][5] = 0;
5314 if (!g_pub_json_buffer
)
5316 g_pub_json_buffer
= malloc(JSON_BUF_MAX
* 2);
5317 g_pub_save_buffer
= g_pub_json_buffer
+ JSON_BUF_MAX
;
5321 pthread_mutex_init(&g_api_mutex
, NULL
);
5325 void ventoy_http_exit(void)
5327 check_free(g_pub_json_buffer
);
5328 g_pub_json_buffer
= NULL
;
5329 g_pub_save_buffer
= NULL
;
5331 pthread_mutex_destroy(&g_api_mutex
);