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];
75 static char g_ventoy_menu_lang
[MAX_LANGUAGE
][8];
77 static char g_pub_path
[2 * MAX_PATH
];
78 static data_control g_data_control
[bios_max
+ 1];
79 static data_theme g_data_theme
[bios_max
+ 1];
80 static data_alias g_data_menu_alias
[bios_max
+ 1];
81 static data_tip g_data_menu_tip
[bios_max
+ 1];
82 static data_class g_data_menu_class
[bios_max
+ 1];
83 static data_image_list g_data_image_list
[bios_max
+ 1];
84 static data_image_list
*g_data_image_blacklist
= g_data_image_list
;
85 static data_auto_memdisk g_data_auto_memdisk
[bios_max
+ 1];
86 static data_password g_data_password
[bios_max
+ 1];
87 static data_conf_replace g_data_conf_replace
[bios_max
+ 1];
88 static data_injection g_data_injection
[bios_max
+ 1];
89 static data_auto_install g_data_auto_install
[bios_max
+ 1];
90 static data_persistence g_data_persistence
[bios_max
+ 1];
91 static data_dud g_data_dud
[bios_max
+ 1];
93 static char *g_pub_json_buffer
= NULL
;
94 static char *g_pub_save_buffer
= NULL
;
95 #define JSON_BUFFER g_pub_json_buffer
96 #define JSON_SAVE_BUFFER g_pub_save_buffer
98 static pthread_mutex_t g_api_mutex
;
99 static struct mg_context
*g_ventoy_http_ctx
= NULL
;
101 static int ventoy_is_kbd_valid(const char *key
)
105 for (i
= 0; g_ventoy_kbd_layout
[i
]; i
++)
107 if (strcmp(g_ventoy_kbd_layout
[i
], key
) == 0)
116 static const char * ventoy_real_path(const char *org
)
120 if (g_sysinfo
.pathcase
)
122 scnprintf(g_pub_path
, MAX_PATH
, "%s", org
);
123 count
= ventoy_path_case(g_pub_path
+ 1, 1);
137 static int ventoy_json_result(struct mg_connection
*conn
, const char *err
)
140 "HTTP/1.1 200 OK \r\n"
141 "Content-Type: application/json\r\n"
142 "Content-Length: %d\r\n"
144 (int)strlen(err
), err
);
149 static int ventoy_json_buffer(struct mg_connection
*conn
, const char *json_buf
, int json_len
)
152 "HTTP/1.1 200 OK \r\n"
153 "Content-Type: application/json\r\n"
154 "Content-Length: %d\r\n"
161 static void ventoy_free_path_node_list(path_node
*list
)
163 path_node
*next
= NULL
;
164 path_node
*node
= list
;
174 static path_node
* ventoy_path_node_add_array(VTOY_JSON
*array
)
176 path_node
*head
= NULL
;
177 path_node
*node
= NULL
;
178 path_node
*cur
= NULL
;
179 VTOY_JSON
*item
= NULL
;
181 for (item
= array
->pstChild
; item
; item
= item
->pstNext
)
183 node
= zalloc(sizeof(path_node
));
186 scnprintf(node
->path
, sizeof(node
->path
), "%s", item
->unData
.pcStrVal
);
187 vtoy_list_add(head
, cur
, node
);
194 static int ventoy_check_fuzzy_path(char *path
, int prefix
)
206 pos
= strchr(path
, '*');
209 for (cur
= pos
; *cur
; cur
++)
234 rc
= ventoy_is_directory_exist("%s%s", g_cur_dir
, path
);
238 rc
= ventoy_is_directory_exist("%s", path
);
259 return ventoy_is_file_exist("%s%s", g_cur_dir
, path
);
263 return ventoy_is_file_exist("%s", path
);
268 static int ventoy_path_list_cmp(path_node
*list1
, path_node
*list2
)
270 if (NULL
== list1
&& NULL
== list2
)
274 else if (list1
&& list2
)
276 while (list1
&& list2
)
278 if (strcmp(list1
->path
, list2
->path
))
287 if (list1
== NULL
&& list2
== NULL
)
300 static int ventoy_api_device_info(struct mg_connection
*conn
, VTOY_JSON
*json
)
306 VTOY_JSON_FMT_BEGIN(pos
, JSON_BUFFER
, JSON_BUF_MAX
);
307 VTOY_JSON_FMT_OBJ_BEGIN();
309 VTOY_JSON_FMT_STRN("dev_name", g_sysinfo
.cur_model
);
310 VTOY_JSON_FMT_STRN("dev_capacity", g_sysinfo
.cur_capacity
);
311 VTOY_JSON_FMT_STRN("dev_fs", g_sysinfo
.cur_fsname
);
312 VTOY_JSON_FMT_STRN("ventoy_ver", g_sysinfo
.cur_ventoy_ver
);
313 VTOY_JSON_FMT_SINT("part_style", g_sysinfo
.cur_part_style
);
314 VTOY_JSON_FMT_SINT("secure_boot", g_sysinfo
.cur_secureboot
);
316 VTOY_JSON_FMT_OBJ_END();
317 VTOY_JSON_FMT_END(pos
);
319 ventoy_json_buffer(conn
, JSON_BUFFER
, pos
);
323 static int ventoy_api_sysinfo(struct mg_connection
*conn
, VTOY_JSON
*json
)
329 VTOY_JSON_FMT_BEGIN(pos
, JSON_BUFFER
, JSON_BUF_MAX
);
330 VTOY_JSON_FMT_OBJ_BEGIN();
331 VTOY_JSON_FMT_STRN("language", ventoy_get_os_language());
332 VTOY_JSON_FMT_STRN("curdir", g_cur_dir
);
335 VTOY_JSON_FMT_SINT("syntax_error", g_sysinfo
.syntax_error
);
336 g_sysinfo
.syntax_error
= 0;
338 VTOY_JSON_FMT_SINT("invalid_config", g_sysinfo
.invalid_config
);
339 g_sysinfo
.invalid_config
= 0;
341 #if defined(_MSC_VER) || defined(WIN32)
342 VTOY_JSON_FMT_STRN("os", "windows");
344 VTOY_JSON_FMT_STRN("os", "linux");
347 VTOY_JSON_FMT_OBJ_END();
348 VTOY_JSON_FMT_END(pos
);
350 ventoy_json_buffer(conn
, JSON_BUFFER
, pos
);
354 static int ventoy_api_handshake(struct mg_connection
*conn
, VTOY_JSON
*json
)
363 VTOY_JSON_FMT_BEGIN(pos
, JSON_BUFFER
, JSON_BUF_MAX
);
364 VTOY_JSON_FMT_OBJ_BEGIN();
365 VTOY_JSON_FMT_SINT("status", 0);
366 VTOY_JSON_FMT_SINT("save_error", g_sysinfo
.config_save_error
);
367 g_sysinfo
.config_save_error
= 0;
369 for (i
= 0; i
< plugin_type_max
; i
++)
371 scnprintf(key
, sizeof(key
), "exist_%s", g_plugin_name
[i
]);
372 VTOY_JSON_FMT_KEY(key
);
373 VTOY_JSON_FMT_ARY_BEGIN();
374 for (j
= 0; j
< bios_max
; j
++)
376 VTOY_JSON_FMT_ITEM_INT(g_json_exist
[i
][j
]);
378 VTOY_JSON_FMT_ARY_ENDEX();
381 VTOY_JSON_FMT_OBJ_END();
382 VTOY_JSON_FMT_END(pos
);
384 ventoy_json_buffer(conn
, JSON_BUFFER
, pos
);
388 static int ventoy_api_check_exist(struct mg_connection
*conn
, VTOY_JSON
*json
)
393 const char *path
= NULL
;
395 path
= vtoy_json_get_string_ex(json
, "path");
396 vtoy_json_get_int(json
, "dir", &dir
);
402 exist
= ventoy_is_directory_exist("%s", path
);
406 exist
= ventoy_is_file_exist("%s", path
);
410 VTOY_JSON_FMT_BEGIN(pos
, JSON_BUFFER
, JSON_BUF_MAX
);
411 VTOY_JSON_FMT_OBJ_BEGIN();
412 VTOY_JSON_FMT_SINT("exist", exist
);
413 VTOY_JSON_FMT_OBJ_END();
414 VTOY_JSON_FMT_END(pos
);
416 ventoy_json_buffer(conn
, JSON_BUFFER
, pos
);
420 static int ventoy_api_check_exist2(struct mg_connection
*conn
, VTOY_JSON
*json
)
429 const char *path1
= NULL
;
430 const char *path2
= NULL
;
432 path1
= vtoy_json_get_string_ex(json
, "path1");
433 path2
= vtoy_json_get_string_ex(json
, "path2");
434 vtoy_json_get_int(json
, "dir1", &dir1
);
435 vtoy_json_get_int(json
, "dir2", &dir2
);
436 vtoy_json_get_int(json
, "fuzzy1", &fuzzy1
);
437 vtoy_json_get_int(json
, "fuzzy2", &fuzzy2
);
443 exist1
= ventoy_is_directory_exist("%s", path1
);
449 exist1
= ventoy_check_fuzzy_path((char *)path1
, 0);
453 exist1
= ventoy_is_file_exist("%s", path1
);
462 exist2
= ventoy_is_directory_exist("%s", path2
);
468 exist2
= ventoy_check_fuzzy_path((char *)path2
, 0);
472 exist2
= ventoy_is_file_exist("%s", path2
);
477 VTOY_JSON_FMT_BEGIN(pos
, JSON_BUFFER
, JSON_BUF_MAX
);
478 VTOY_JSON_FMT_OBJ_BEGIN();
479 VTOY_JSON_FMT_SINT("exist1", exist1
);
480 VTOY_JSON_FMT_SINT("exist2", exist2
);
481 VTOY_JSON_FMT_OBJ_END();
482 VTOY_JSON_FMT_END(pos
);
484 ventoy_json_buffer(conn
, JSON_BUFFER
, pos
);
488 static int ventoy_api_check_fuzzy(struct mg_connection
*conn
, VTOY_JSON
*json
)
492 const char *path
= NULL
;
494 path
= vtoy_json_get_string_ex(json
, "path");
497 exist
= ventoy_check_fuzzy_path((char *)path
, 0);
500 VTOY_JSON_FMT_BEGIN(pos
, JSON_BUFFER
, JSON_BUF_MAX
);
501 VTOY_JSON_FMT_OBJ_BEGIN();
502 VTOY_JSON_FMT_SINT("exist", exist
);
503 VTOY_JSON_FMT_OBJ_END();
504 VTOY_JSON_FMT_END(pos
);
506 ventoy_json_buffer(conn
, JSON_BUFFER
, pos
);
513 void ventoy_data_default_control(data_control
*data
)
515 memset(data
, 0, sizeof(data_control
));
517 data
->password_asterisk
= 1;
518 data
->secondary_menu
= 1;
519 data
->filter_dot_underscore
= 1;
520 data
->max_search_level
= -1;
521 data
->menu_timeout
= 0;
522 data
->secondary_menu_timeout
= 0;
524 strlcpy(data
->default_kbd_layout
, "QWERTY_USA");
525 strlcpy(data
->help_text_language
, "en_US");
526 strlcpy(data
->menu_language
, "en_US");
529 int ventoy_data_cmp_control(data_control
*data1
, data_control
*data2
)
531 if (data1
->default_menu_mode
!= data2
->default_menu_mode
||
532 data1
->treeview_style
!= data2
->treeview_style
||
533 data1
->filter_dot_underscore
!= data2
->filter_dot_underscore
||
534 data1
->sort_casesensitive
!= data2
->sort_casesensitive
||
535 data1
->max_search_level
!= data2
->max_search_level
||
536 data1
->vhd_no_warning
!= data2
->vhd_no_warning
||
537 data1
->filter_iso
!= data2
->filter_iso
||
538 data1
->filter_wim
!= data2
->filter_wim
||
539 data1
->filter_efi
!= data2
->filter_efi
||
540 data1
->filter_img
!= data2
->filter_img
||
541 data1
->filter_vhd
!= data2
->filter_vhd
||
542 data1
->filter_vtoy
!= data2
->filter_vtoy
||
543 data1
->win11_bypass_check
!= data2
->win11_bypass_check
||
544 data1
->linux_remount
!= data2
->linux_remount
||
545 data1
->password_asterisk
!= data2
->password_asterisk
||
546 data1
->secondary_menu
!= data2
->secondary_menu
||
547 data1
->menu_timeout
!= data2
->menu_timeout
||
548 data1
->secondary_menu_timeout
!= data2
->secondary_menu_timeout
)
553 if (strcmp(data1
->default_search_root
, data2
->default_search_root
) ||
554 strcmp(data1
->default_image
, data2
->default_image
) ||
555 strcmp(data1
->default_kbd_layout
, data2
->default_kbd_layout
) ||
556 strcmp(data1
->help_text_language
, data2
->help_text_language
) ||
557 strcmp(data1
->menu_language
, data2
->menu_language
))
565 int ventoy_data_save_control(data_control
*data
, const char *title
, char *buf
, int buflen
)
568 data_control
*def
= g_data_control
+ bios_max
;
570 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
572 VTOY_JSON_FMT_KEY_L(L1
, title
);
573 VTOY_JSON_FMT_ARY_BEGIN_N();
575 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_DEFAULT_MENU_MODE", default_menu_mode
);
576 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_TREE_VIEW_MENU_STYLE", treeview_style
);
577 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_FILT_DOT_UNDERSCORE_FILE", filter_dot_underscore
);
578 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_SORT_CASE_SENSITIVE", sort_casesensitive
);
580 if (data
->max_search_level
>= 0)
582 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_MAX_SEARCH_LEVEL", max_search_level
);
585 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_VHD_NO_WARNING", vhd_no_warning
);
586 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_FILE_FLT_ISO", filter_iso
);
587 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_FILE_FLT_WIM", filter_wim
);
588 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_FILE_FLT_EFI", filter_efi
);
589 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_FILE_FLT_IMG", filter_img
);
590 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_FILE_FLT_VHD", filter_vhd
);
591 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_FILE_FLT_VTOY", filter_vtoy
);
592 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_WIN11_BYPASS_CHECK", win11_bypass_check
);
593 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_LINUX_REMOUNT", linux_remount
);
594 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_SECONDARY_BOOT_MENU", secondary_menu
);
595 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_SHOW_PASSWORD_ASTERISK", password_asterisk
);
596 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_MENU_TIMEOUT", menu_timeout
);
597 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_SECONDARY_TIMEOUT", secondary_menu_timeout
);
599 VTOY_JSON_FMT_CTRL_STRN(L2
, "VTOY_DEFAULT_KBD_LAYOUT", default_kbd_layout
);
600 VTOY_JSON_FMT_CTRL_STRN(L2
, "VTOY_HELP_TXT_LANGUAGE", help_text_language
);
601 VTOY_JSON_FMT_CTRL_STRN(L2
, "VTOY_MENU_LANGUAGE", menu_language
);
603 if (strcmp(def
->default_search_root
, data
->default_search_root
))
605 VTOY_JSON_FMT_CTRL_STRN_STR(L2
, "VTOY_DEFAULT_SEARCH_ROOT", ventoy_real_path(data
->default_search_root
));
608 if (strcmp(def
->default_image
, data
->default_image
))
610 VTOY_JSON_FMT_CTRL_STRN_STR(L2
, "VTOY_DEFAULT_IMAGE", ventoy_real_path(data
->default_image
));
613 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
614 VTOY_JSON_FMT_END(pos
);
619 int ventoy_data_json_control(data_control
*ctrl
, char *buf
, int buflen
)
625 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
626 VTOY_JSON_FMT_OBJ_BEGIN();
628 VTOY_JSON_FMT_SINT("default_menu_mode", ctrl
->default_menu_mode
);
629 VTOY_JSON_FMT_SINT("treeview_style", ctrl
->treeview_style
);
630 VTOY_JSON_FMT_SINT("filter_dot_underscore", ctrl
->filter_dot_underscore
);
631 VTOY_JSON_FMT_SINT("sort_casesensitive", ctrl
->sort_casesensitive
);
632 VTOY_JSON_FMT_SINT("max_search_level", ctrl
->max_search_level
);
633 VTOY_JSON_FMT_SINT("vhd_no_warning", ctrl
->vhd_no_warning
);
635 VTOY_JSON_FMT_SINT("filter_iso", ctrl
->filter_iso
);
636 VTOY_JSON_FMT_SINT("filter_wim", ctrl
->filter_wim
);
637 VTOY_JSON_FMT_SINT("filter_efi", ctrl
->filter_efi
);
638 VTOY_JSON_FMT_SINT("filter_img", ctrl
->filter_img
);
639 VTOY_JSON_FMT_SINT("filter_vhd", ctrl
->filter_vhd
);
640 VTOY_JSON_FMT_SINT("filter_vtoy", ctrl
->filter_vtoy
);
641 VTOY_JSON_FMT_SINT("win11_bypass_check", ctrl
->win11_bypass_check
);
642 VTOY_JSON_FMT_SINT("linux_remount", ctrl
->linux_remount
);
643 VTOY_JSON_FMT_SINT("secondary_menu", ctrl
->secondary_menu
);
644 VTOY_JSON_FMT_SINT("password_asterisk", ctrl
->password_asterisk
);
645 VTOY_JSON_FMT_SINT("menu_timeout", ctrl
->menu_timeout
);
646 VTOY_JSON_FMT_SINT("secondary_menu_timeout", ctrl
->secondary_menu_timeout
);
647 VTOY_JSON_FMT_STRN("default_kbd_layout", ctrl
->default_kbd_layout
);
648 VTOY_JSON_FMT_STRN("help_text_language", ctrl
->help_text_language
);
649 VTOY_JSON_FMT_STRN("menu_language", ctrl
->menu_language
);
652 if (ctrl
->default_search_root
[0] && ventoy_is_directory_exist("%s%s", g_cur_dir
, ctrl
->default_search_root
))
656 VTOY_JSON_FMT_STRN("default_search_root", ctrl
->default_search_root
);
657 VTOY_JSON_FMT_SINT("default_search_root_valid", valid
);
661 if (ctrl
->default_image
[0] && ventoy_is_file_exist("%s%s", g_cur_dir
, ctrl
->default_image
))
665 VTOY_JSON_FMT_STRN("default_image", ctrl
->default_image
);
666 VTOY_JSON_FMT_SINT("default_image_valid", valid
);
668 VTOY_JSON_FMT_KEY("help_list");
669 VTOY_JSON_FMT_ARY_BEGIN();
671 for (i
= 0; g_ventoy_help_lang
[i
][0]; i
++)
673 VTOY_JSON_FMT_ITEM(g_ventoy_help_lang
[i
]);
675 VTOY_JSON_FMT_ARY_ENDEX();
677 VTOY_JSON_FMT_KEY("menu_list");
678 VTOY_JSON_FMT_ARY_BEGIN();
680 for (i
= 0; g_ventoy_menu_lang
[i
][0]; i
++)
682 VTOY_JSON_FMT_ITEM(g_ventoy_menu_lang
[i
]);
684 VTOY_JSON_FMT_ARY_ENDEX();
686 VTOY_JSON_FMT_OBJ_END();
687 VTOY_JSON_FMT_END(pos
);
692 static int ventoy_api_get_control(struct mg_connection
*conn
, VTOY_JSON
*json
)
694 api_get_func(conn
, json
, control
);
698 static int ventoy_api_save_control(struct mg_connection
*conn
, VTOY_JSON
*json
)
702 data_control
*ctrl
= NULL
;
704 vtoy_json_get_int(json
, "index", &index
);
705 ctrl
= g_data_control
+ index
;
707 VTOY_JSON_INT("default_menu_mode", ctrl
->default_menu_mode
);
708 VTOY_JSON_INT("treeview_style", ctrl
->treeview_style
);
709 VTOY_JSON_INT("filter_dot_underscore", ctrl
->filter_dot_underscore
);
710 VTOY_JSON_INT("sort_casesensitive", ctrl
->sort_casesensitive
);
711 VTOY_JSON_INT("max_search_level", ctrl
->max_search_level
);
712 VTOY_JSON_INT("vhd_no_warning", ctrl
->vhd_no_warning
);
713 VTOY_JSON_INT("filter_iso", ctrl
->filter_iso
);
714 VTOY_JSON_INT("filter_wim", ctrl
->filter_wim
);
715 VTOY_JSON_INT("filter_efi", ctrl
->filter_efi
);
716 VTOY_JSON_INT("filter_img", ctrl
->filter_img
);
717 VTOY_JSON_INT("filter_vhd", ctrl
->filter_vhd
);
718 VTOY_JSON_INT("filter_vtoy", ctrl
->filter_vtoy
);
719 VTOY_JSON_INT("win11_bypass_check", ctrl
->win11_bypass_check
);
720 VTOY_JSON_INT("linux_remount", ctrl
->linux_remount
);
721 VTOY_JSON_INT("secondary_menu", ctrl
->secondary_menu
);
722 VTOY_JSON_INT("password_asterisk", ctrl
->password_asterisk
);
723 VTOY_JSON_INT("menu_timeout", ctrl
->menu_timeout
);
724 VTOY_JSON_INT("secondary_menu_timeout", ctrl
->secondary_menu_timeout
);
726 VTOY_JSON_STR("default_image", ctrl
->default_image
);
727 VTOY_JSON_STR("default_search_root", ctrl
->default_search_root
);
728 VTOY_JSON_STR("help_text_language", ctrl
->help_text_language
);
729 VTOY_JSON_STR("menu_language", ctrl
->menu_language
);
730 VTOY_JSON_STR("default_kbd_layout", ctrl
->default_kbd_layout
);
732 ret
= ventoy_data_save_all();
734 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
742 void ventoy_data_default_theme(data_theme
*data
)
744 memset(data
, 0, sizeof(data_theme
));
745 strlcpy(data
->gfxmode
, "1024x768");
746 scnprintf(data
->ventoy_left
, sizeof(data
->ventoy_left
), "5%%");
747 scnprintf(data
->ventoy_top
, sizeof(data
->ventoy_top
), "95%%");
748 scnprintf(data
->ventoy_color
, sizeof(data
->ventoy_color
), "%s", "#0000ff");
751 int ventoy_data_cmp_theme(data_theme
*data1
, data_theme
*data2
)
753 if (data1
->display_mode
!= data2
->display_mode
||
754 strcmp(data1
->ventoy_left
, data2
->ventoy_left
) ||
755 strcmp(data1
->ventoy_top
, data2
->ventoy_top
) ||
756 strcmp(data1
->gfxmode
, data2
->gfxmode
) ||
757 strcmp(data1
->ventoy_color
, data2
->ventoy_color
)
763 if (ventoy_path_list_cmp(data1
->filelist
, data2
->filelist
))
768 if (ventoy_path_list_cmp(data1
->fontslist
, data2
->fontslist
))
777 int ventoy_data_save_theme(data_theme
*data
, const char *title
, char *buf
, int buflen
)
780 path_node
*node
= NULL
;
781 data_theme
*def
= g_data_theme
+ bios_max
;
783 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
785 VTOY_JSON_FMT_KEY_L(L1
, title
);
786 VTOY_JSON_FMT_OBJ_BEGIN_N();
790 if (data
->filelist
->next
)
792 VTOY_JSON_FMT_KEY_L(L2
, "file");
793 VTOY_JSON_FMT_ARY_BEGIN_N();
795 for (node
= data
->filelist
; node
; node
= node
->next
)
797 VTOY_JSON_FMT_ITEM_PATH_LN(L3
, node
->path
);
800 VTOY_JSON_FMT_ARY_ENDEX_LN(L2
);
802 if (def
->default_file
!= data
->default_file
)
804 VTOY_JSON_FMT_SINT_LN(L2
, "default_file", data
->default_file
);
809 VTOY_JSON_FMT_STRN_PATH_LN(L2
, "file", data
->filelist
->path
);
813 if (data
->display_mode
!= def
->display_mode
)
815 if (display_mode_cli
== data
->display_mode
)
817 VTOY_JSON_FMT_STRN_LN(L2
, "display_mode", "CLI");
819 else if (display_mode_serial
== data
->display_mode
)
821 VTOY_JSON_FMT_STRN_LN(L2
, "display_mode", "serial");
823 else if (display_mode_ser_console
== data
->display_mode
)
825 VTOY_JSON_FMT_STRN_LN(L2
, "display_mode", "serial_console");
829 VTOY_JSON_FMT_STRN_LN(L2
, "display_mode", "GUI");
833 VTOY_JSON_FMT_DIFF_STRN(L2
, "gfxmode", gfxmode
);
835 VTOY_JSON_FMT_DIFF_STRN(L2
, "ventoy_left", ventoy_left
);
836 VTOY_JSON_FMT_DIFF_STRN(L2
, "ventoy_top", ventoy_top
);
837 VTOY_JSON_FMT_DIFF_STRN(L2
, "ventoy_color", ventoy_color
);
841 VTOY_JSON_FMT_KEY_L(L2
, "fonts");
842 VTOY_JSON_FMT_ARY_BEGIN_N();
844 for (node
= data
->fontslist
; node
; node
= node
->next
)
846 VTOY_JSON_FMT_ITEM_PATH_LN(L3
, node
->path
);
849 VTOY_JSON_FMT_ARY_ENDEX_LN(L2
);
852 VTOY_JSON_FMT_OBJ_ENDEX_LN(L1
);
853 VTOY_JSON_FMT_END(pos
);
859 int ventoy_data_json_theme(data_theme
*data
, char *buf
, int buflen
)
862 path_node
*node
= NULL
;
864 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
865 VTOY_JSON_FMT_OBJ_BEGIN();
867 VTOY_JSON_FMT_SINT("default_file", data
->default_file
);
868 VTOY_JSON_FMT_SINT("display_mode", data
->display_mode
);
869 VTOY_JSON_FMT_STRN("gfxmode", data
->gfxmode
);
871 VTOY_JSON_FMT_STRN("ventoy_color", data
->ventoy_color
);
872 VTOY_JSON_FMT_STRN("ventoy_left", data
->ventoy_left
);
873 VTOY_JSON_FMT_STRN("ventoy_top", data
->ventoy_top
);
875 VTOY_JSON_FMT_KEY("filelist");
876 VTOY_JSON_FMT_ARY_BEGIN();
877 for (node
= data
->filelist
; node
; node
= node
->next
)
879 VTOY_JSON_FMT_OBJ_BEGIN();
880 VTOY_JSON_FMT_STRN("path", node
->path
);
881 VTOY_JSON_FMT_SINT("valid", ventoy_is_file_exist("%s%s", g_cur_dir
, node
->path
));
882 VTOY_JSON_FMT_OBJ_ENDEX();
884 VTOY_JSON_FMT_ARY_ENDEX();
886 VTOY_JSON_FMT_KEY("fontslist");
887 VTOY_JSON_FMT_ARY_BEGIN();
888 for (node
= data
->fontslist
; node
; node
= node
->next
)
890 VTOY_JSON_FMT_OBJ_BEGIN();
891 VTOY_JSON_FMT_STRN("path", node
->path
);
892 VTOY_JSON_FMT_SINT("valid", ventoy_is_file_exist("%s%s", g_cur_dir
, node
->path
));
893 VTOY_JSON_FMT_OBJ_ENDEX();
895 VTOY_JSON_FMT_ARY_ENDEX();
897 VTOY_JSON_FMT_OBJ_END();
898 VTOY_JSON_FMT_END(pos
);
903 static int ventoy_api_get_theme(struct mg_connection
*conn
, VTOY_JSON
*json
)
905 api_get_func(conn
, json
, theme
);
909 static int ventoy_api_save_theme(struct mg_connection
*conn
, VTOY_JSON
*json
)
913 data_theme
*data
= NULL
;
915 vtoy_json_get_int(json
, "index", &index
);
916 data
= g_data_theme
+ index
;
918 VTOY_JSON_INT("default_file", data
->default_file
);
919 VTOY_JSON_INT("display_mode", data
->display_mode
);
920 VTOY_JSON_STR("gfxmode", data
->gfxmode
);
921 VTOY_JSON_STR("ventoy_left", data
->ventoy_left
);
922 VTOY_JSON_STR("ventoy_top", data
->ventoy_top
);
923 VTOY_JSON_STR("ventoy_color", data
->ventoy_color
);
925 ret
= ventoy_data_save_all();
927 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
931 static int ventoy_api_theme_add_file(struct mg_connection
*conn
, VTOY_JSON
*json
)
935 const char *path
= NULL
;
936 path_node
*node
= NULL
;
937 path_node
*cur
= NULL
;
938 data_theme
*data
= NULL
;
940 vtoy_json_get_int(json
, "index", &index
);
941 data
= g_data_theme
+ index
;
943 path
= VTOY_JSON_STR_EX("path");
946 node
= zalloc(sizeof(path_node
));
949 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
951 vtoy_list_add(data
->filelist
, cur
, node
);
955 ret
= ventoy_data_save_all();
957 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
961 static int ventoy_api_theme_del_file(struct mg_connection
*conn
, VTOY_JSON
*json
)
965 const char *path
= NULL
;
966 path_node
*node
= NULL
;
967 path_node
*last
= NULL
;
968 data_theme
*data
= NULL
;
970 vtoy_json_get_int(json
, "index", &index
);
971 data
= g_data_theme
+ index
;
973 path
= VTOY_JSON_STR_EX("path");
976 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
978 vtoy_list_free(path_node
, data
->filelist
);
982 vtoy_list_del(last
, node
, data
->filelist
, path
);
986 ret
= ventoy_data_save_all();
988 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
993 static int ventoy_api_theme_add_font(struct mg_connection
*conn
, VTOY_JSON
*json
)
997 const char *path
= NULL
;
998 path_node
*node
= NULL
;
999 path_node
*cur
= NULL
;
1000 data_theme
*data
= NULL
;
1002 vtoy_json_get_int(json
, "index", &index
);
1003 data
= g_data_theme
+ index
;
1005 path
= VTOY_JSON_STR_EX("path");
1008 node
= zalloc(sizeof(path_node
));
1011 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
1012 vtoy_list_add(data
->fontslist
, cur
, node
);
1016 ret
= ventoy_data_save_all();
1018 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1023 static int ventoy_api_theme_del_font(struct mg_connection
*conn
, VTOY_JSON
*json
)
1027 const char *path
= NULL
;
1028 path_node
*node
= NULL
;
1029 path_node
*last
= NULL
;
1030 data_theme
*data
= NULL
;
1032 vtoy_json_get_int(json
, "index", &index
);
1033 data
= g_data_theme
+ index
;
1035 path
= VTOY_JSON_STR_EX("path");
1038 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
1040 vtoy_list_free(path_node
, data
->fontslist
);
1044 vtoy_list_del(last
, node
, data
->fontslist
, path
);
1048 ret
= ventoy_data_save_all();
1050 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1057 void ventoy_data_default_menu_alias(data_alias
*data
)
1059 memset(data
, 0, sizeof(data_alias
));
1062 int ventoy_data_cmp_menu_alias(data_alias
*data1
, data_alias
*data2
)
1064 data_alias_node
*list1
= NULL
;
1065 data_alias_node
*list2
= NULL
;
1067 if (NULL
== data1
->list
&& NULL
== data2
->list
)
1071 else if (data1
->list
&& data2
->list
)
1073 list1
= data1
->list
;
1074 list2
= data2
->list
;
1076 while (list1
&& list2
)
1078 if ((list1
->type
!= list2
->type
) ||
1079 strcmp(list1
->path
, list2
->path
) ||
1080 strcmp(list1
->alias
, list2
->alias
))
1085 list1
= list1
->next
;
1086 list2
= list2
->next
;
1089 if (list1
== NULL
&& list2
== NULL
)
1104 int ventoy_data_save_menu_alias(data_alias
*data
, const char *title
, char *buf
, int buflen
)
1107 data_alias_node
*node
= NULL
;
1109 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1111 VTOY_JSON_FMT_KEY_L(L1
, title
);
1112 VTOY_JSON_FMT_ARY_BEGIN_N();
1114 for (node
= data
->list
; node
; node
= node
->next
)
1116 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
1118 if (node
->type
== path_type_file
)
1120 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "image", node
->path
);
1124 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "dir", node
->path
);
1127 VTOY_JSON_FMT_STRN_EX_LN(L3
, "alias", node
->alias
);
1129 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
1132 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
1133 VTOY_JSON_FMT_END(pos
);
1139 int ventoy_data_json_menu_alias(data_alias
*data
, char *buf
, int buflen
)
1143 data_alias_node
*node
= NULL
;
1145 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1146 VTOY_JSON_FMT_ARY_BEGIN();
1148 for (node
= data
->list
; node
; node
= node
->next
)
1150 VTOY_JSON_FMT_OBJ_BEGIN();
1152 VTOY_JSON_FMT_UINT("type", node
->type
);
1153 VTOY_JSON_FMT_STRN("path", node
->path
);
1154 if (node
->type
== path_type_file
)
1156 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
1160 valid
= ventoy_is_directory_exist("%s%s", g_cur_dir
, node
->path
);
1163 VTOY_JSON_FMT_SINT("valid", valid
);
1164 VTOY_JSON_FMT_STRN("alias", node
->alias
);
1166 VTOY_JSON_FMT_OBJ_ENDEX();
1169 VTOY_JSON_FMT_ARY_END();
1170 VTOY_JSON_FMT_END(pos
);
1175 static int ventoy_api_get_alias(struct mg_connection
*conn
, VTOY_JSON
*json
)
1177 api_get_func(conn
, json
, menu_alias
);
1181 static int ventoy_api_save_alias(struct mg_connection
*conn
, VTOY_JSON
*json
)
1184 ret
= ventoy_data_save_all();
1186 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1190 static int ventoy_api_alias_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
1194 int type
= path_type_file
;
1195 const char *path
= NULL
;
1196 const char *alias
= NULL
;
1197 data_alias_node
*node
= NULL
;
1198 data_alias_node
*cur
= NULL
;
1199 data_alias
*data
= NULL
;
1201 vtoy_json_get_int(json
, "index", &index
);
1202 data
= g_data_menu_alias
+ index
;
1204 vtoy_json_get_int(json
, "type", &type
);
1206 path
= VTOY_JSON_STR_EX("path");
1207 alias
= VTOY_JSON_STR_EX("alias");
1210 node
= zalloc(sizeof(data_alias_node
));
1214 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
1215 scnprintf(node
->alias
, sizeof(node
->alias
), "%s", alias
);
1217 vtoy_list_add(data
->list
, cur
, node
);
1221 ret
= ventoy_data_save_all();
1223 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1227 static int ventoy_api_alias_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
1231 const char *path
= NULL
;
1232 data_alias_node
*last
= NULL
;
1233 data_alias_node
*node
= NULL
;
1234 data_alias
*data
= NULL
;
1236 vtoy_json_get_int(json
, "index", &index
);
1237 data
= g_data_menu_alias
+ index
;
1239 path
= VTOY_JSON_STR_EX("path");
1242 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
1244 vtoy_list_free(data_alias_node
, data
->list
);
1248 vtoy_list_del(last
, node
, data
->list
, path
);
1252 ret
= ventoy_data_save_all();
1254 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1261 void ventoy_data_default_menu_tip(data_tip
*data
)
1263 memset(data
, 0, sizeof(data_tip
));
1265 scnprintf(data
->left
, sizeof(data
->left
), "10%%");
1266 scnprintf(data
->top
, sizeof(data
->top
), "81%%");
1267 scnprintf(data
->color
, sizeof(data
->color
), "%s", "blue");
1270 int ventoy_data_cmp_menu_tip(data_tip
*data1
, data_tip
*data2
)
1272 data_tip_node
*list1
= NULL
;
1273 data_tip_node
*list2
= NULL
;
1275 if (strcmp(data1
->left
, data2
->left
) || strcmp(data1
->top
, data2
->top
) || strcmp(data1
->color
, data2
->color
))
1280 if (NULL
== data1
->list
&& NULL
== data2
->list
)
1284 else if (data1
->list
&& data2
->list
)
1286 list1
= data1
->list
;
1287 list2
= data2
->list
;
1289 while (list1
&& list2
)
1291 if ((list1
->type
!= list2
->type
) ||
1292 strcmp(list1
->path
, list2
->path
) ||
1293 strcmp(list1
->tip
, list2
->tip
))
1298 list1
= list1
->next
;
1299 list2
= list2
->next
;
1302 if (list1
== NULL
&& list2
== NULL
)
1317 int ventoy_data_save_menu_tip(data_tip
*data
, const char *title
, char *buf
, int buflen
)
1320 data_tip_node
*node
= NULL
;
1321 data_tip
*def
= g_data_menu_tip
+ bios_max
;
1323 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1324 VTOY_JSON_FMT_KEY_L(L1
, title
);
1325 VTOY_JSON_FMT_OBJ_BEGIN_N();
1327 VTOY_JSON_FMT_DIFF_STRN(L2
, "left", left
);
1328 VTOY_JSON_FMT_DIFF_STRN(L2
, "top", top
);
1329 VTOY_JSON_FMT_DIFF_STRN(L2
, "color", color
);
1333 VTOY_JSON_FMT_KEY_L(L2
, "tips");
1334 VTOY_JSON_FMT_ARY_BEGIN_N();
1336 for (node
= data
->list
; node
; node
= node
->next
)
1338 VTOY_JSON_FMT_OBJ_BEGIN_LN(L3
);
1340 if (node
->type
== path_type_file
)
1342 VTOY_JSON_FMT_STRN_PATH_LN(L4
, "image", node
->path
);
1346 VTOY_JSON_FMT_STRN_PATH_LN(L4
, "dir", node
->path
);
1348 VTOY_JSON_FMT_STRN_EX_LN(L4
, "tip", node
->tip
);
1350 VTOY_JSON_FMT_OBJ_ENDEX_LN(L3
);
1353 VTOY_JSON_FMT_ARY_ENDEX_LN(L2
);
1356 VTOY_JSON_FMT_OBJ_ENDEX_LN(L1
);
1358 VTOY_JSON_FMT_END(pos
);
1364 int ventoy_data_json_menu_tip(data_tip
*data
, char *buf
, int buflen
)
1368 data_tip_node
*node
= NULL
;
1370 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1372 VTOY_JSON_FMT_OBJ_BEGIN();
1374 VTOY_JSON_FMT_STRN("left", data
->left
);
1375 VTOY_JSON_FMT_STRN("top", data
->top
);
1376 VTOY_JSON_FMT_STRN("color", data
->color
);
1378 VTOY_JSON_FMT_KEY("tips");
1379 VTOY_JSON_FMT_ARY_BEGIN();
1381 for (node
= data
->list
; node
; node
= node
->next
)
1383 VTOY_JSON_FMT_OBJ_BEGIN();
1385 VTOY_JSON_FMT_UINT("type", node
->type
);
1386 VTOY_JSON_FMT_STRN("path", node
->path
);
1387 if (node
->type
== path_type_file
)
1389 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
1393 valid
= ventoy_is_directory_exist("%s%s", g_cur_dir
, node
->path
);
1396 VTOY_JSON_FMT_SINT("valid", valid
);
1397 VTOY_JSON_FMT_STRN("tip", node
->tip
);
1399 VTOY_JSON_FMT_OBJ_ENDEX();
1402 VTOY_JSON_FMT_ARY_ENDEX();
1404 VTOY_JSON_FMT_OBJ_END();
1405 VTOY_JSON_FMT_END(pos
);
1410 static int ventoy_api_get_tip(struct mg_connection
*conn
, VTOY_JSON
*json
)
1412 api_get_func(conn
, json
, menu_tip
);
1416 static int ventoy_api_save_tip(struct mg_connection
*conn
, VTOY_JSON
*json
)
1420 data_tip
*data
= NULL
;
1422 vtoy_json_get_int(json
, "index", &index
);
1423 data
= g_data_menu_tip
+ index
;
1425 VTOY_JSON_STR("left", data
->left
);
1426 VTOY_JSON_STR("top", data
->top
);
1427 VTOY_JSON_STR("color", data
->color
);
1429 ret
= ventoy_data_save_all();
1431 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1435 static int ventoy_api_tip_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
1439 int type
= path_type_file
;
1440 const char *path
= NULL
;
1441 const char *tip
= NULL
;
1442 data_tip_node
*node
= NULL
;
1443 data_tip_node
*cur
= NULL
;
1444 data_tip
*data
= NULL
;
1446 vtoy_json_get_int(json
, "index", &index
);
1447 data
= g_data_menu_tip
+ index
;
1449 vtoy_json_get_int(json
, "type", &type
);
1451 path
= VTOY_JSON_STR_EX("path");
1452 tip
= VTOY_JSON_STR_EX("tip");
1455 node
= zalloc(sizeof(data_tip_node
));
1459 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
1460 scnprintf(node
->tip
, sizeof(node
->tip
), "%s", tip
);
1462 vtoy_list_add(data
->list
, cur
, node
);
1466 ret
= ventoy_data_save_all();
1468 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1472 static int ventoy_api_tip_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
1476 const char *path
= NULL
;
1477 data_tip_node
*last
= NULL
;
1478 data_tip_node
*node
= NULL
;
1479 data_tip
*data
= NULL
;
1481 vtoy_json_get_int(json
, "index", &index
);
1482 data
= g_data_menu_tip
+ index
;
1484 path
= VTOY_JSON_STR_EX("path");
1487 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
1489 vtoy_list_free(data_tip_node
, data
->list
);
1493 vtoy_list_del(last
, node
, data
->list
, path
);
1497 ret
= ventoy_data_save_all();
1499 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1506 void ventoy_data_default_menu_class(data_class
*data
)
1508 memset(data
, 0, sizeof(data_class
));
1511 int ventoy_data_cmp_menu_class(data_class
*data1
, data_class
*data2
)
1513 data_class_node
*list1
= NULL
;
1514 data_class_node
*list2
= NULL
;
1516 if (NULL
== data1
->list
&& NULL
== data2
->list
)
1520 else if (data1
->list
&& data2
->list
)
1522 list1
= data1
->list
;
1523 list2
= data2
->list
;
1525 while (list1
&& list2
)
1527 if ((list1
->type
!= list2
->type
) ||
1528 strcmp(list1
->path
, list2
->path
) ||
1529 strcmp(list1
->class, list2
->class))
1534 list1
= list1
->next
;
1535 list2
= list2
->next
;
1538 if (list1
== NULL
&& list2
== NULL
)
1553 int ventoy_data_save_menu_class(data_class
*data
, const char *title
, char *buf
, int buflen
)
1556 data_class_node
*node
= NULL
;
1558 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1560 VTOY_JSON_FMT_KEY_L(L1
, title
);
1561 VTOY_JSON_FMT_ARY_BEGIN_N();
1563 for (node
= data
->list
; node
; node
= node
->next
)
1565 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
1567 if (node
->type
== class_type_key
)
1569 VTOY_JSON_FMT_STRN_LN(L3
, "key", node
->path
);
1571 else if (node
->type
== class_type_dir
)
1573 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "dir", node
->path
);
1577 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "parent", node
->path
);
1579 VTOY_JSON_FMT_STRN_LN(L3
, "class", node
->class);
1581 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
1584 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
1585 VTOY_JSON_FMT_END(pos
);
1591 int ventoy_data_json_menu_class(data_class
*data
, char *buf
, int buflen
)
1595 data_class_node
*node
= NULL
;
1597 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1598 VTOY_JSON_FMT_ARY_BEGIN();
1600 for (node
= data
->list
; node
; node
= node
->next
)
1602 VTOY_JSON_FMT_OBJ_BEGIN();
1604 VTOY_JSON_FMT_UINT("type", node
->type
);
1605 VTOY_JSON_FMT_STRN("path", node
->path
);
1607 if (node
->type
== class_type_key
)
1613 valid
= ventoy_is_directory_exist("%s%s", g_cur_dir
, node
->path
);
1615 VTOY_JSON_FMT_SINT("valid", valid
);
1617 VTOY_JSON_FMT_STRN("class", node
->class);
1619 VTOY_JSON_FMT_OBJ_ENDEX();
1622 VTOY_JSON_FMT_ARY_END();
1623 VTOY_JSON_FMT_END(pos
);
1629 static int ventoy_api_get_class(struct mg_connection
*conn
, VTOY_JSON
*json
)
1631 api_get_func(conn
, json
, menu_class
);
1635 static int ventoy_api_save_class(struct mg_connection
*conn
, VTOY_JSON
*json
)
1638 ret
= ventoy_data_save_all();
1640 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1644 static int ventoy_api_class_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
1648 int type
= class_type_key
;
1649 const char *path
= NULL
;
1650 const char *class = NULL
;
1651 data_class_node
*node
= NULL
;
1652 data_class_node
*cur
= NULL
;
1653 data_class
*data
= NULL
;
1655 vtoy_json_get_int(json
, "index", &index
);
1656 data
= g_data_menu_class
+ index
;
1658 vtoy_json_get_int(json
, "type", &type
);
1660 path
= VTOY_JSON_STR_EX("path");
1661 class = VTOY_JSON_STR_EX("class");
1664 node
= zalloc(sizeof(data_class_node
));
1669 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
1670 scnprintf(node
->class, sizeof(node
->class), "%s", class);
1672 vtoy_list_add(data
->list
, cur
, node
);
1676 ret
= ventoy_data_save_all();
1678 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1682 static int ventoy_api_class_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
1686 const char *path
= NULL
;
1687 data_class_node
*last
= NULL
;
1688 data_class_node
*node
= NULL
;
1689 data_class
*data
= NULL
;
1691 vtoy_json_get_int(json
, "index", &index
);
1692 data
= g_data_menu_class
+ index
;
1694 path
= VTOY_JSON_STR_EX("path");
1697 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
1699 vtoy_list_free(data_class_node
, data
->list
);
1703 vtoy_list_del(last
, node
, data
->list
, path
);
1707 ret
= ventoy_data_save_all();
1709 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1716 void ventoy_data_default_auto_memdisk(data_auto_memdisk
*data
)
1718 memset(data
, 0, sizeof(data_auto_memdisk
));
1721 int ventoy_data_cmp_auto_memdisk(data_auto_memdisk
*data1
, data_auto_memdisk
*data2
)
1723 return ventoy_path_list_cmp(data1
->list
, data2
->list
);
1726 int ventoy_data_save_auto_memdisk(data_auto_memdisk
*data
, const char *title
, char *buf
, int buflen
)
1729 path_node
*node
= NULL
;
1731 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1733 VTOY_JSON_FMT_KEY_L(L1
, title
);
1734 VTOY_JSON_FMT_ARY_BEGIN_N();
1736 for (node
= data
->list
; node
; node
= node
->next
)
1738 VTOY_JSON_FMT_ITEM_PATH_LN(L2
, node
->path
);
1741 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
1742 VTOY_JSON_FMT_END(pos
);
1747 int ventoy_data_json_auto_memdisk(data_auto_memdisk
*data
, char *buf
, int buflen
)
1751 path_node
*node
= NULL
;
1753 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1754 VTOY_JSON_FMT_ARY_BEGIN();
1756 for (node
= data
->list
; node
; node
= node
->next
)
1758 VTOY_JSON_FMT_OBJ_BEGIN();
1760 VTOY_JSON_FMT_STRN("path", node
->path
);
1761 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
1762 VTOY_JSON_FMT_SINT("valid", valid
);
1764 VTOY_JSON_FMT_OBJ_ENDEX();
1767 VTOY_JSON_FMT_ARY_END();
1768 VTOY_JSON_FMT_END(pos
);
1773 static int ventoy_api_get_auto_memdisk(struct mg_connection
*conn
, VTOY_JSON
*json
)
1775 api_get_func(conn
, json
, auto_memdisk
);
1779 static int ventoy_api_save_auto_memdisk(struct mg_connection
*conn
, VTOY_JSON
*json
)
1783 ret
= ventoy_data_save_all();
1785 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1789 static int ventoy_api_auto_memdisk_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
1793 const char *path
= NULL
;
1794 path_node
*node
= NULL
;
1795 path_node
*cur
= NULL
;
1796 data_auto_memdisk
*data
= NULL
;
1798 vtoy_json_get_int(json
, "index", &index
);
1799 data
= g_data_auto_memdisk
+ index
;
1801 path
= VTOY_JSON_STR_EX("path");
1804 node
= zalloc(sizeof(path_node
));
1807 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
1808 vtoy_list_add(data
->list
, cur
, node
);
1812 ret
= ventoy_data_save_all();
1814 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1818 static int ventoy_api_auto_memdisk_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
1822 const char *path
= NULL
;
1823 path_node
*last
= NULL
;
1824 path_node
*node
= NULL
;
1825 data_auto_memdisk
*data
= NULL
;
1827 vtoy_json_get_int(json
, "index", &index
);
1828 data
= g_data_auto_memdisk
+ index
;
1830 path
= VTOY_JSON_STR_EX("path");
1833 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
1835 vtoy_list_free(path_node
, data
->list
);
1839 vtoy_list_del(last
, node
, data
->list
, path
);
1843 ret
= ventoy_data_save_all();
1845 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1852 void ventoy_data_default_image_list(data_image_list
*data
)
1854 memset(data
, 0, sizeof(data_image_list
));
1857 int ventoy_data_cmp_image_list(data_image_list
*data1
, data_image_list
*data2
)
1859 if (data1
->type
!= data2
->type
)
1861 if (data1
->list
|| data2
->list
)
1871 return ventoy_path_list_cmp(data1
->list
, data2
->list
);
1874 int ventoy_data_save_image_list(data_image_list
*data
, const char *title
, char *buf
, int buflen
)
1878 path_node
*node
= NULL
;
1888 prelen
= (int)strlen("image_list");
1890 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1892 if (data
->type
== 0)
1894 scnprintf(newtitle
, sizeof(newtitle
), "image_list%s", title
+ prelen
);
1898 scnprintf(newtitle
, sizeof(newtitle
), "image_blacklist%s", title
+ prelen
);
1900 VTOY_JSON_FMT_KEY_L(L1
, newtitle
);
1902 VTOY_JSON_FMT_ARY_BEGIN_N();
1904 for (node
= data
->list
; node
; node
= node
->next
)
1906 VTOY_JSON_FMT_ITEM_PATH_LN(L2
, node
->path
);
1909 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
1910 VTOY_JSON_FMT_END(pos
);
1915 int ventoy_data_json_image_list(data_image_list
*data
, char *buf
, int buflen
)
1919 path_node
*node
= NULL
;
1921 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1923 VTOY_JSON_FMT_OBJ_BEGIN();
1924 VTOY_JSON_FMT_SINT("type", data
->type
);
1926 VTOY_JSON_FMT_KEY("list");
1927 VTOY_JSON_FMT_ARY_BEGIN();
1929 for (node
= data
->list
; node
; node
= node
->next
)
1931 VTOY_JSON_FMT_OBJ_BEGIN();
1933 VTOY_JSON_FMT_STRN("path", node
->path
);
1934 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
1935 VTOY_JSON_FMT_SINT("valid", valid
);
1937 VTOY_JSON_FMT_OBJ_ENDEX();
1940 VTOY_JSON_FMT_ARY_ENDEX();
1941 VTOY_JSON_FMT_OBJ_END();
1943 VTOY_JSON_FMT_END(pos
);
1948 static int ventoy_api_get_image_list(struct mg_connection
*conn
, VTOY_JSON
*json
)
1950 api_get_func(conn
, json
, image_list
);
1954 static int ventoy_api_save_image_list(struct mg_connection
*conn
, VTOY_JSON
*json
)
1958 data_image_list
*data
= NULL
;
1960 vtoy_json_get_int(json
, "index", &index
);
1961 data
= g_data_image_list
+ index
;
1963 VTOY_JSON_INT("type", data
->type
);
1965 ret
= ventoy_data_save_all();
1967 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1971 static int ventoy_api_image_list_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
1975 const char *path
= NULL
;
1976 path_node
*node
= NULL
;
1977 path_node
*cur
= NULL
;
1978 data_image_list
*data
= NULL
;
1980 vtoy_json_get_int(json
, "index", &index
);
1981 data
= g_data_image_list
+ index
;
1983 path
= VTOY_JSON_STR_EX("path");
1986 node
= zalloc(sizeof(path_node
));
1989 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
1990 vtoy_list_add(data
->list
, cur
, node
);
1994 ret
= ventoy_data_save_all();
1996 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2000 static int ventoy_api_image_list_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
2004 const char *path
= NULL
;
2005 path_node
*last
= NULL
;
2006 path_node
*node
= NULL
;
2007 data_image_list
*data
= NULL
;
2009 vtoy_json_get_int(json
, "index", &index
);
2010 data
= g_data_image_list
+ index
;
2012 path
= VTOY_JSON_STR_EX("path");
2015 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
2017 vtoy_list_free(path_node
, data
->list
);
2021 vtoy_list_del(last
, node
, data
->list
, path
);
2025 ret
= ventoy_data_save_all();
2027 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2034 void ventoy_data_default_password(data_password
*data
)
2036 memset(data
, 0, sizeof(data_password
));
2039 int ventoy_data_cmp_password(data_password
*data1
, data_password
*data2
)
2041 menu_password
*list1
= NULL
;
2042 menu_password
*list2
= NULL
;
2044 if (strcmp(data1
->bootpwd
, data2
->bootpwd
) ||
2045 strcmp(data1
->isopwd
, data2
->isopwd
) ||
2046 strcmp(data1
->wimpwd
, data2
->wimpwd
) ||
2047 strcmp(data1
->vhdpwd
, data2
->vhdpwd
) ||
2048 strcmp(data1
->imgpwd
, data2
->imgpwd
) ||
2049 strcmp(data1
->efipwd
, data2
->efipwd
) ||
2050 strcmp(data1
->vtoypwd
, data2
->vtoypwd
)
2056 if (NULL
== data1
->list
&& NULL
== data2
->list
)
2060 else if (data1
->list
&& data2
->list
)
2062 list1
= data1
->list
;
2063 list2
= data2
->list
;
2065 while (list1
&& list2
)
2067 if ((list1
->type
!= list2
->type
) || strcmp(list1
->path
, list2
->path
))
2072 list1
= list1
->next
;
2073 list2
= list2
->next
;
2076 if (list1
== NULL
&& list2
== NULL
)
2091 int ventoy_data_save_password(data_password
*data
, const char *title
, char *buf
, int buflen
)
2094 menu_password
*node
= NULL
;
2095 data_password
*def
= g_data_password
+ bios_max
;
2097 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2098 VTOY_JSON_FMT_KEY_L(L1
, title
);
2099 VTOY_JSON_FMT_OBJ_BEGIN_N();
2101 VTOY_JSON_FMT_DIFF_STRN(L2
, "bootpwd", bootpwd
);
2102 VTOY_JSON_FMT_DIFF_STRN(L2
, "isopwd", isopwd
);
2103 VTOY_JSON_FMT_DIFF_STRN(L2
, "wimpwd", wimpwd
);
2104 VTOY_JSON_FMT_DIFF_STRN(L2
, "vhdpwd", vhdpwd
);
2105 VTOY_JSON_FMT_DIFF_STRN(L2
, "imgpwd", imgpwd
);
2106 VTOY_JSON_FMT_DIFF_STRN(L2
, "efipwd", efipwd
);
2107 VTOY_JSON_FMT_DIFF_STRN(L2
, "vtoypwd", vtoypwd
);
2111 VTOY_JSON_FMT_KEY_L(L2
, "menupwd");
2112 VTOY_JSON_FMT_ARY_BEGIN_N();
2114 for (node
= data
->list
; node
; node
= node
->next
)
2116 VTOY_JSON_FMT_OBJ_BEGIN_LN(L3
);
2118 if (node
->type
== 0)
2120 VTOY_JSON_FMT_STRN_PATH_LN(L4
, "file", node
->path
);
2124 VTOY_JSON_FMT_STRN_PATH_LN(L4
, "parent", node
->path
);
2126 VTOY_JSON_FMT_STRN_LN(L4
, "pwd", node
->pwd
);
2128 VTOY_JSON_FMT_OBJ_ENDEX_LN(L3
);
2131 VTOY_JSON_FMT_ARY_ENDEX_LN(L2
);
2134 VTOY_JSON_FMT_OBJ_ENDEX_LN(L1
);
2136 VTOY_JSON_FMT_END(pos
);
2142 int ventoy_data_json_password(data_password
*data
, char *buf
, int buflen
)
2146 menu_password
*node
= NULL
;
2148 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2150 VTOY_JSON_FMT_OBJ_BEGIN();
2152 VTOY_JSON_FMT_STRN("bootpwd", data
->bootpwd
);
2153 VTOY_JSON_FMT_STRN("isopwd", data
->isopwd
);
2154 VTOY_JSON_FMT_STRN("wimpwd", data
->wimpwd
);
2155 VTOY_JSON_FMT_STRN("vhdpwd", data
->vhdpwd
);
2156 VTOY_JSON_FMT_STRN("imgpwd", data
->imgpwd
);
2157 VTOY_JSON_FMT_STRN("efipwd", data
->efipwd
);
2158 VTOY_JSON_FMT_STRN("vtoypwd", data
->vtoypwd
);
2160 VTOY_JSON_FMT_KEY("list");
2161 VTOY_JSON_FMT_ARY_BEGIN();
2163 for (node
= data
->list
; node
; node
= node
->next
)
2165 VTOY_JSON_FMT_OBJ_BEGIN();
2167 VTOY_JSON_FMT_SINT("type", node
->type
);
2168 VTOY_JSON_FMT_STRN("path", node
->path
);
2169 if (node
->type
== path_type_file
)
2171 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
2175 valid
= ventoy_is_directory_exist("%s%s", g_cur_dir
, node
->path
);
2178 VTOY_JSON_FMT_SINT("valid", valid
);
2179 VTOY_JSON_FMT_STRN("pwd", node
->pwd
);
2181 VTOY_JSON_FMT_OBJ_ENDEX();
2184 VTOY_JSON_FMT_ARY_ENDEX();
2186 VTOY_JSON_FMT_OBJ_END();
2187 VTOY_JSON_FMT_END(pos
);
2192 static int ventoy_api_get_password(struct mg_connection
*conn
, VTOY_JSON
*json
)
2194 api_get_func(conn
, json
, password
);
2198 static int ventoy_api_save_password(struct mg_connection
*conn
, VTOY_JSON
*json
)
2202 data_password
*data
= NULL
;
2204 vtoy_json_get_int(json
, "index", &index
);
2205 data
= g_data_password
+ index
;
2207 VTOY_JSON_STR("bootpwd", data
->bootpwd
);
2208 VTOY_JSON_STR("isopwd", data
->isopwd
);
2209 VTOY_JSON_STR("wimpwd", data
->wimpwd
);
2210 VTOY_JSON_STR("vhdpwd", data
->vhdpwd
);
2211 VTOY_JSON_STR("imgpwd", data
->imgpwd
);
2212 VTOY_JSON_STR("efipwd", data
->efipwd
);
2213 VTOY_JSON_STR("vtoypwd", data
->vtoypwd
);
2215 ret
= ventoy_data_save_all();
2217 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2221 static int ventoy_api_password_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
2226 const char *path
= NULL
;
2227 const char *pwd
= NULL
;
2228 menu_password
*node
= NULL
;
2229 menu_password
*cur
= NULL
;
2230 data_password
*data
= NULL
;
2232 vtoy_json_get_int(json
, "index", &index
);
2233 data
= g_data_password
+ index
;
2235 vtoy_json_get_int(json
, "type", &type
);
2237 path
= VTOY_JSON_STR_EX("path");
2238 pwd
= VTOY_JSON_STR_EX("pwd");
2241 node
= zalloc(sizeof(menu_password
));
2245 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
2246 scnprintf(node
->pwd
, sizeof(node
->pwd
), "%s", pwd
);
2248 vtoy_list_add(data
->list
, cur
, node
);
2252 ret
= ventoy_data_save_all();
2254 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2258 static int ventoy_api_password_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
2262 const char *path
= NULL
;
2263 menu_password
*last
= NULL
;
2264 menu_password
*node
= NULL
;
2265 data_password
*data
= NULL
;
2267 vtoy_json_get_int(json
, "index", &index
);
2268 data
= g_data_password
+ index
;
2270 path
= VTOY_JSON_STR_EX("path");
2273 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
2275 vtoy_list_free(menu_password
, data
->list
);
2279 vtoy_list_del(last
, node
, data
->list
, path
);
2283 ret
= ventoy_data_save_all();
2285 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2292 void ventoy_data_default_conf_replace(data_conf_replace
*data
)
2294 memset(data
, 0, sizeof(data_conf_replace
));
2297 int ventoy_data_cmp_conf_replace(data_conf_replace
*data1
, data_conf_replace
*data2
)
2299 conf_replace_node
*list1
= NULL
;
2300 conf_replace_node
*list2
= NULL
;
2302 if (NULL
== data1
->list
&& NULL
== data2
->list
)
2306 else if (data1
->list
&& data2
->list
)
2308 list1
= data1
->list
;
2309 list2
= data2
->list
;
2311 while (list1
&& list2
)
2313 if (list1
->image
!= list2
->image
||
2314 strcmp(list1
->path
, list2
->path
) ||
2315 strcmp(list1
->org
, list2
->org
) ||
2316 strcmp(list1
->new, list2
->new)
2322 list1
= list1
->next
;
2323 list2
= list2
->next
;
2326 if (list1
== NULL
&& list2
== NULL
)
2341 int ventoy_data_save_conf_replace(data_conf_replace
*data
, const char *title
, char *buf
, int buflen
)
2344 conf_replace_node
*node
= NULL
;
2346 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2348 VTOY_JSON_FMT_KEY_L(L1
, title
);
2349 VTOY_JSON_FMT_ARY_BEGIN_N();
2351 for (node
= data
->list
; node
; node
= node
->next
)
2353 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
2355 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "iso", node
->path
);
2356 VTOY_JSON_FMT_STRN_LN(L3
, "org", node
->org
);
2357 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "new", node
->new);
2360 VTOY_JSON_FMT_SINT_LN(L3
, "img", node
->image
);
2363 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
2366 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
2367 VTOY_JSON_FMT_END(pos
);
2373 int ventoy_data_json_conf_replace(data_conf_replace
*data
, char *buf
, int buflen
)
2376 conf_replace_node
*node
= NULL
;
2378 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2379 VTOY_JSON_FMT_ARY_BEGIN();
2381 for (node
= data
->list
; node
; node
= node
->next
)
2383 VTOY_JSON_FMT_OBJ_BEGIN();
2385 VTOY_JSON_FMT_STRN("path", node
->path
);
2386 VTOY_JSON_FMT_SINT("valid", ventoy_check_fuzzy_path(node
->path
, 1));
2387 VTOY_JSON_FMT_STRN("org", node
->org
);
2388 VTOY_JSON_FMT_STRN("new", node
->new);
2389 VTOY_JSON_FMT_SINT("new_valid", ventoy_is_file_exist("%s%s", g_cur_dir
, node
->new));
2390 VTOY_JSON_FMT_SINT("img", node
->image
);
2392 VTOY_JSON_FMT_OBJ_ENDEX();
2395 VTOY_JSON_FMT_ARY_END();
2396 VTOY_JSON_FMT_END(pos
);
2401 static int ventoy_api_get_conf_replace(struct mg_connection
*conn
, VTOY_JSON
*json
)
2403 api_get_func(conn
, json
, conf_replace
);
2407 static int ventoy_api_save_conf_replace(struct mg_connection
*conn
, VTOY_JSON
*json
)
2410 ret
= ventoy_data_save_all();
2412 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2416 static int ventoy_api_conf_replace_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
2421 const char *path
= NULL
;
2422 const char *org
= NULL
;
2423 const char *new = NULL
;
2424 conf_replace_node
*node
= NULL
;
2425 conf_replace_node
*cur
= NULL
;
2426 data_conf_replace
*data
= NULL
;
2428 vtoy_json_get_int(json
, "img", &image
);
2430 vtoy_json_get_int(json
, "index", &index
);
2431 data
= g_data_conf_replace
+ index
;
2433 path
= VTOY_JSON_STR_EX("path");
2434 org
= VTOY_JSON_STR_EX("org");
2435 new = VTOY_JSON_STR_EX("new");
2436 if (path
&& org
&& new)
2438 node
= zalloc(sizeof(conf_replace_node
));
2441 node
->image
= image
;
2442 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
2443 scnprintf(node
->org
, sizeof(node
->org
), "%s", org
);
2444 scnprintf(node
->new, sizeof(node
->new), "%s", new);
2446 vtoy_list_add(data
->list
, cur
, node
);
2450 ret
= ventoy_data_save_all();
2452 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2456 static int ventoy_api_conf_replace_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
2460 const char *path
= NULL
;
2461 conf_replace_node
*last
= NULL
;
2462 conf_replace_node
*node
= NULL
;
2463 data_conf_replace
*data
= NULL
;
2465 vtoy_json_get_int(json
, "index", &index
);
2466 data
= g_data_conf_replace
+ index
;
2468 path
= VTOY_JSON_STR_EX("path");
2471 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
2473 vtoy_list_free(conf_replace_node
, data
->list
);
2477 vtoy_list_del(last
, node
, data
->list
, path
);
2481 ret
= ventoy_data_save_all();
2483 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2491 void ventoy_data_default_dud(data_dud
*data
)
2493 memset(data
, 0, sizeof(data_dud
));
2496 int ventoy_data_cmp_dud(data_dud
*data1
, data_dud
*data2
)
2498 dud_node
*list1
= NULL
;
2499 dud_node
*list2
= NULL
;
2501 if (NULL
== data1
->list
&& NULL
== data2
->list
)
2505 else if (data1
->list
&& data2
->list
)
2507 list1
= data1
->list
;
2508 list2
= data2
->list
;
2510 while (list1
&& list2
)
2512 if (strcmp(list1
->path
, list2
->path
))
2517 /* no need to compare dud list with default */
2518 list1
= list1
->next
;
2519 list2
= list2
->next
;
2522 if (list1
== NULL
&& list2
== NULL
)
2537 int ventoy_data_save_dud(data_dud
*data
, const char *title
, char *buf
, int buflen
)
2540 dud_node
*node
= NULL
;
2541 path_node
*pathnode
= NULL
;
2543 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2545 VTOY_JSON_FMT_KEY_L(L1
, title
);
2546 VTOY_JSON_FMT_ARY_BEGIN_N();
2548 for (node
= data
->list
; node
; node
= node
->next
)
2550 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
2551 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "image", node
->path
);
2553 VTOY_JSON_FMT_KEY_L(L3
, "dud");
2554 VTOY_JSON_FMT_ARY_BEGIN_N();
2555 for (pathnode
= node
->list
; pathnode
; pathnode
= pathnode
->next
)
2557 VTOY_JSON_FMT_ITEM_PATH_LN(L4
, pathnode
->path
);
2559 VTOY_JSON_FMT_ARY_ENDEX_LN(L3
);
2561 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
2564 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
2565 VTOY_JSON_FMT_END(pos
);
2571 int ventoy_data_json_dud(data_dud
*data
, char *buf
, int buflen
)
2575 dud_node
*node
= NULL
;
2576 path_node
*pathnode
= NULL
;
2578 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2579 VTOY_JSON_FMT_ARY_BEGIN();
2581 for (node
= data
->list
; node
; node
= node
->next
)
2583 VTOY_JSON_FMT_OBJ_BEGIN();
2585 VTOY_JSON_FMT_STRN("path", node
->path
);
2586 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
2587 VTOY_JSON_FMT_SINT("valid", valid
);
2590 VTOY_JSON_FMT_KEY("list");
2591 VTOY_JSON_FMT_ARY_BEGIN();
2592 for (pathnode
= node
->list
; pathnode
; pathnode
= pathnode
->next
)
2594 VTOY_JSON_FMT_OBJ_BEGIN();
2595 VTOY_JSON_FMT_STRN("path", pathnode
->path
);
2597 valid
= ventoy_is_file_exist("%s%s", g_cur_dir
, pathnode
->path
);
2598 VTOY_JSON_FMT_SINT("valid", valid
);
2599 VTOY_JSON_FMT_OBJ_ENDEX();
2601 VTOY_JSON_FMT_ARY_ENDEX();
2604 VTOY_JSON_FMT_OBJ_ENDEX();
2607 VTOY_JSON_FMT_ARY_END();
2608 VTOY_JSON_FMT_END(pos
);
2613 static int ventoy_api_get_dud(struct mg_connection
*conn
, VTOY_JSON
*json
)
2615 api_get_func(conn
, json
, dud
);
2619 static int ventoy_api_save_dud(struct mg_connection
*conn
, VTOY_JSON
*json
)
2622 ret
= ventoy_data_save_all();
2624 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2629 static int ventoy_api_dud_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
2633 const char *path
= NULL
;
2634 dud_node
*node
= NULL
;
2635 dud_node
*cur
= NULL
;
2636 data_dud
*data
= NULL
;
2637 VTOY_JSON
*array
= NULL
;
2639 vtoy_json_get_int(json
, "index", &index
);
2640 data
= g_data_dud
+ index
;
2642 array
= vtoy_json_find_item(json
, JSON_TYPE_ARRAY
, "dud");
2643 path
= VTOY_JSON_STR_EX("path");
2646 node
= zalloc(sizeof(dud_node
));
2649 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
2650 node
->list
= ventoy_path_node_add_array(array
);
2652 vtoy_list_add(data
->list
, cur
, node
);
2656 ret
= ventoy_data_save_all();
2658 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2662 static int ventoy_api_dud_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
2666 const char *path
= NULL
;
2667 dud_node
*next
= NULL
;
2668 dud_node
*last
= NULL
;
2669 dud_node
*node
= NULL
;
2670 data_dud
*data
= NULL
;
2672 vtoy_json_get_int(json
, "index", &index
);
2673 data
= g_data_dud
+ index
;
2675 path
= VTOY_JSON_STR_EX("path");
2678 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
2680 for (node
= data
->list
; node
; node
= next
)
2683 ventoy_free_path_node_list(node
->list
);
2690 vtoy_list_del_ex(last
, node
, data
->list
, path
, ventoy_free_path_node_list
);
2694 ret
= ventoy_data_save_all();
2696 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2701 static int ventoy_api_dud_add_inner(struct mg_connection
*conn
, VTOY_JSON
*json
)
2705 const char *path
= NULL
;
2706 const char *outpath
= NULL
;
2707 path_node
*pcur
= NULL
;
2708 path_node
*pnode
= NULL
;
2709 dud_node
*node
= NULL
;
2710 data_dud
*data
= NULL
;
2712 vtoy_json_get_int(json
, "index", &index
);
2713 data
= g_data_dud
+ index
;
2715 path
= VTOY_JSON_STR_EX("path");
2716 outpath
= VTOY_JSON_STR_EX("outpath");
2717 if (path
&& outpath
)
2719 for (node
= data
->list
; node
; node
= node
->next
)
2721 if (strcmp(outpath
, node
->path
) == 0)
2723 pnode
= zalloc(sizeof(path_node
));
2726 scnprintf(pnode
->path
, sizeof(pnode
->path
), "%s", path
);
2727 vtoy_list_add(node
->list
, pcur
, pnode
);
2735 ret
= ventoy_data_save_all();
2737 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2741 static int ventoy_api_dud_del_inner(struct mg_connection
*conn
, VTOY_JSON
*json
)
2745 const char *path
= NULL
;
2746 const char *outpath
= NULL
;
2747 path_node
*plast
= NULL
;
2748 path_node
*pnode
= NULL
;
2749 dud_node
*node
= NULL
;
2750 data_dud
*data
= NULL
;
2752 vtoy_json_get_int(json
, "index", &index
);
2753 data
= g_data_dud
+ index
;
2755 path
= VTOY_JSON_STR_EX("path");
2756 outpath
= VTOY_JSON_STR_EX("outpath");
2757 if (path
&& outpath
)
2759 for (node
= data
->list
; node
; node
= node
->next
)
2761 if (strcmp(outpath
, node
->path
) == 0)
2763 vtoy_list_del(plast
, pnode
, node
->list
, path
);
2769 ret
= ventoy_data_save_all();
2771 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2779 void ventoy_data_default_auto_install(data_auto_install
*data
)
2781 memset(data
, 0, sizeof(data_auto_install
));
2784 int ventoy_data_cmp_auto_install(data_auto_install
*data1
, data_auto_install
*data2
)
2786 auto_install_node
*list1
= NULL
;
2787 auto_install_node
*list2
= NULL
;
2789 if (NULL
== data1
->list
&& NULL
== data2
->list
)
2793 else if (data1
->list
&& data2
->list
)
2795 list1
= data1
->list
;
2796 list2
= data2
->list
;
2798 while (list1
&& list2
)
2800 if (list1
->timeout
!= list2
->timeout
||
2801 list1
->autosel
!= list2
->autosel
||
2802 strcmp(list1
->path
, list2
->path
))
2807 /* no need to compare auto install list with default */
2808 list1
= list1
->next
;
2809 list2
= list2
->next
;
2812 if (list1
== NULL
&& list2
== NULL
)
2827 int ventoy_data_save_auto_install(data_auto_install
*data
, const char *title
, char *buf
, int buflen
)
2830 auto_install_node
*node
= NULL
;
2831 path_node
*pathnode
= NULL
;
2833 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2835 VTOY_JSON_FMT_KEY_L(L1
, title
);
2836 VTOY_JSON_FMT_ARY_BEGIN_N();
2838 for (node
= data
->list
; node
; node
= node
->next
)
2840 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
2841 if (node
->type
== 0)
2843 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "image", node
->path
);
2847 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "parent", node
->path
);
2851 VTOY_JSON_FMT_KEY_L(L3
, "template");
2852 VTOY_JSON_FMT_ARY_BEGIN_N();
2853 for (pathnode
= node
->list
; pathnode
; pathnode
= pathnode
->next
)
2855 VTOY_JSON_FMT_ITEM_PATH_LN(L4
, pathnode
->path
);
2857 VTOY_JSON_FMT_ARY_ENDEX_LN(L3
);
2859 if (node
->timeouten
)
2861 VTOY_JSON_FMT_SINT_LN(L3
, "timeout", node
->timeout
);
2864 if (node
->autoselen
)
2866 VTOY_JSON_FMT_SINT_LN(L3
, "autosel", node
->autosel
);
2869 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
2872 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
2873 VTOY_JSON_FMT_END(pos
);
2879 int ventoy_data_json_auto_install(data_auto_install
*data
, char *buf
, int buflen
)
2883 auto_install_node
*node
= NULL
;
2884 path_node
*pathnode
= NULL
;
2886 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2887 VTOY_JSON_FMT_ARY_BEGIN();
2889 for (node
= data
->list
; node
; node
= node
->next
)
2891 VTOY_JSON_FMT_OBJ_BEGIN();
2893 VTOY_JSON_FMT_STRN("path", node
->path
);
2895 if (node
->type
== 0)
2897 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
2901 valid
= ventoy_is_directory_exist("%s%s", g_cur_dir
, node
->path
);
2903 VTOY_JSON_FMT_SINT("valid", valid
);
2904 VTOY_JSON_FMT_SINT("type", node
->type
);
2906 VTOY_JSON_FMT_BOOL("timeouten", node
->timeouten
);
2907 VTOY_JSON_FMT_BOOL("autoselen", node
->autoselen
);
2909 VTOY_JSON_FMT_SINT("autosel", node
->autosel
);
2910 VTOY_JSON_FMT_SINT("timeout", node
->timeout
);
2912 VTOY_JSON_FMT_KEY("list");
2913 VTOY_JSON_FMT_ARY_BEGIN();
2914 for (pathnode
= node
->list
; pathnode
; pathnode
= pathnode
->next
)
2916 VTOY_JSON_FMT_OBJ_BEGIN();
2917 VTOY_JSON_FMT_STRN("path", pathnode
->path
);
2919 valid
= ventoy_is_file_exist("%s%s", g_cur_dir
, pathnode
->path
);
2920 VTOY_JSON_FMT_SINT("valid", valid
);
2921 VTOY_JSON_FMT_OBJ_ENDEX();
2923 VTOY_JSON_FMT_ARY_ENDEX();
2926 VTOY_JSON_FMT_OBJ_ENDEX();
2929 VTOY_JSON_FMT_ARY_END();
2930 VTOY_JSON_FMT_END(pos
);
2935 static int ventoy_api_get_auto_install(struct mg_connection
*conn
, VTOY_JSON
*json
)
2937 api_get_func(conn
, json
, auto_install
);
2941 static int ventoy_api_save_auto_install(struct mg_connection
*conn
, VTOY_JSON
*json
)
2947 uint8_t timeouten
= 0;
2948 uint8_t autoselen
= 0;
2949 auto_install_node
*node
= NULL
;
2950 data_auto_install
*data
= NULL
;
2952 vtoy_json_get_int(json
, "index", &index
);
2953 vtoy_json_get_int(json
, "id", &id
);
2955 vtoy_json_get_bool(json
, "timeouten", &timeouten
);
2956 vtoy_json_get_bool(json
, "autoselen", &autoselen
);
2958 data
= g_data_auto_install
+ index
;
2962 for (node
= data
->list
; node
; node
= node
->next
)
2966 node
->timeouten
= (int)timeouten
;
2967 node
->autoselen
= (int)autoselen
;
2968 VTOY_JSON_INT("timeout", node
->timeout
);
2969 VTOY_JSON_INT("autosel", node
->autosel
);
2975 ret
= ventoy_data_save_all();
2977 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2982 static int ventoy_api_auto_install_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
2987 const char *path
= NULL
;
2988 auto_install_node
*node
= NULL
;
2989 auto_install_node
*cur
= NULL
;
2990 data_auto_install
*data
= NULL
;
2991 VTOY_JSON
*array
= NULL
;
2993 vtoy_json_get_int(json
, "type", &type
);
2994 vtoy_json_get_int(json
, "index", &index
);
2995 data
= g_data_auto_install
+ index
;
2997 array
= vtoy_json_find_item(json
, JSON_TYPE_ARRAY
, "template");
2998 path
= VTOY_JSON_STR_EX("path");
3001 node
= zalloc(sizeof(auto_install_node
));
3005 node
->timeouten
= 0;
3006 node
->autoselen
= 0;
3009 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
3010 node
->list
= ventoy_path_node_add_array(array
);
3012 vtoy_list_add(data
->list
, cur
, node
);
3016 ret
= ventoy_data_save_all();
3018 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3022 static int ventoy_api_auto_install_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
3026 const char *path
= NULL
;
3027 auto_install_node
*last
= NULL
;
3028 auto_install_node
*next
= NULL
;
3029 auto_install_node
*node
= NULL
;
3030 data_auto_install
*data
= NULL
;
3032 vtoy_json_get_int(json
, "index", &index
);
3033 data
= g_data_auto_install
+ index
;
3035 path
= VTOY_JSON_STR_EX("path");
3038 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
3040 for (node
= data
->list
; node
; node
= next
)
3043 ventoy_free_path_node_list(node
->list
);
3050 vtoy_list_del_ex(last
, node
, data
->list
, path
, ventoy_free_path_node_list
);
3054 ret
= ventoy_data_save_all();
3056 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3060 static int ventoy_api_auto_install_add_inner(struct mg_connection
*conn
, VTOY_JSON
*json
)
3064 const char *path
= NULL
;
3065 const char *outpath
= NULL
;
3066 path_node
*pcur
= NULL
;
3067 path_node
*pnode
= NULL
;
3068 auto_install_node
*node
= NULL
;
3069 data_auto_install
*data
= NULL
;
3071 vtoy_json_get_int(json
, "index", &index
);
3072 data
= g_data_auto_install
+ index
;
3074 path
= VTOY_JSON_STR_EX("path");
3075 outpath
= VTOY_JSON_STR_EX("outpath");
3076 if (path
&& outpath
)
3078 for (node
= data
->list
; node
; node
= node
->next
)
3080 if (strcmp(outpath
, node
->path
) == 0)
3082 pnode
= zalloc(sizeof(path_node
));
3085 scnprintf(pnode
->path
, sizeof(pnode
->path
), "%s", path
);
3086 vtoy_list_add(node
->list
, pcur
, pnode
);
3094 ret
= ventoy_data_save_all();
3096 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3100 static int ventoy_api_auto_install_del_inner(struct mg_connection
*conn
, VTOY_JSON
*json
)
3104 const char *path
= NULL
;
3105 const char *outpath
= NULL
;
3106 path_node
*plast
= NULL
;
3107 path_node
*pnode
= NULL
;
3108 auto_install_node
*node
= NULL
;
3109 data_auto_install
*data
= NULL
;
3111 vtoy_json_get_int(json
, "index", &index
);
3112 data
= g_data_auto_install
+ index
;
3114 path
= VTOY_JSON_STR_EX("path");
3115 outpath
= VTOY_JSON_STR_EX("outpath");
3116 if (path
&& outpath
)
3118 for (node
= data
->list
; node
; node
= node
->next
)
3120 if (strcmp(outpath
, node
->path
) == 0)
3122 vtoy_list_del(plast
, pnode
, node
->list
, path
);
3128 ret
= ventoy_data_save_all();
3130 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3139 void ventoy_data_default_persistence(data_persistence
*data
)
3141 memset(data
, 0, sizeof(data_persistence
));
3144 int ventoy_data_cmp_persistence(data_persistence
*data1
, data_persistence
*data2
)
3146 persistence_node
*list1
= NULL
;
3147 persistence_node
*list2
= NULL
;
3149 if (NULL
== data1
->list
&& NULL
== data2
->list
)
3153 else if (data1
->list
&& data2
->list
)
3155 list1
= data1
->list
;
3156 list2
= data2
->list
;
3158 while (list1
&& list2
)
3160 if (list1
->timeout
!= list2
->timeout
||
3161 list1
->autosel
!= list2
->autosel
||
3162 strcmp(list1
->path
, list2
->path
))
3167 /* no need to compare auto install list with default */
3168 list1
= list1
->next
;
3169 list2
= list2
->next
;
3172 if (list1
== NULL
&& list2
== NULL
)
3187 int ventoy_data_save_persistence(data_persistence
*data
, const char *title
, char *buf
, int buflen
)
3190 persistence_node
*node
= NULL
;
3191 path_node
*pathnode
= NULL
;
3193 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
3195 VTOY_JSON_FMT_KEY_L(L1
, title
);
3196 VTOY_JSON_FMT_ARY_BEGIN_N();
3198 for (node
= data
->list
; node
; node
= node
->next
)
3200 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
3201 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "image", node
->path
);
3202 VTOY_JSON_FMT_KEY_L(L3
, "backend");
3203 VTOY_JSON_FMT_ARY_BEGIN_N();
3204 for (pathnode
= node
->list
; pathnode
; pathnode
= pathnode
->next
)
3206 VTOY_JSON_FMT_ITEM_PATH_LN(L4
, pathnode
->path
);
3208 VTOY_JSON_FMT_ARY_ENDEX_LN(L3
);
3210 if (node
->timeouten
)
3212 VTOY_JSON_FMT_SINT_LN(L3
, "timeout", node
->timeout
);
3215 if (node
->autoselen
)
3217 VTOY_JSON_FMT_SINT_LN(L3
, "autosel", node
->autosel
);
3220 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
3223 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
3224 VTOY_JSON_FMT_END(pos
);
3230 int ventoy_data_json_persistence(data_persistence
*data
, char *buf
, int buflen
)
3234 persistence_node
*node
= NULL
;
3235 path_node
*pathnode
= NULL
;
3237 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
3238 VTOY_JSON_FMT_ARY_BEGIN();
3240 for (node
= data
->list
; node
; node
= node
->next
)
3242 VTOY_JSON_FMT_OBJ_BEGIN();
3244 VTOY_JSON_FMT_STRN("path", node
->path
);
3246 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
3247 VTOY_JSON_FMT_SINT("valid", valid
);
3248 VTOY_JSON_FMT_SINT("type", node
->type
);
3250 VTOY_JSON_FMT_BOOL("timeouten", node
->timeouten
);
3251 VTOY_JSON_FMT_BOOL("autoselen", node
->autoselen
);
3253 VTOY_JSON_FMT_SINT("autosel", node
->autosel
);
3254 VTOY_JSON_FMT_SINT("timeout", node
->timeout
);
3256 VTOY_JSON_FMT_KEY("list");
3257 VTOY_JSON_FMT_ARY_BEGIN();
3258 for (pathnode
= node
->list
; pathnode
; pathnode
= pathnode
->next
)
3260 VTOY_JSON_FMT_OBJ_BEGIN();
3261 VTOY_JSON_FMT_STRN("path", pathnode
->path
);
3263 valid
= ventoy_is_file_exist("%s%s", g_cur_dir
, pathnode
->path
);
3264 VTOY_JSON_FMT_SINT("valid", valid
);
3265 VTOY_JSON_FMT_OBJ_ENDEX();
3267 VTOY_JSON_FMT_ARY_ENDEX();
3270 VTOY_JSON_FMT_OBJ_ENDEX();
3273 VTOY_JSON_FMT_ARY_END();
3274 VTOY_JSON_FMT_END(pos
);
3279 static int ventoy_api_get_persistence(struct mg_connection
*conn
, VTOY_JSON
*json
)
3281 api_get_func(conn
, json
, persistence
);
3285 static int ventoy_api_save_persistence(struct mg_connection
*conn
, VTOY_JSON
*json
)
3291 uint8_t timeouten
= 0;
3292 uint8_t autoselen
= 0;
3293 persistence_node
*node
= NULL
;
3294 data_persistence
*data
= NULL
;
3296 vtoy_json_get_int(json
, "index", &index
);
3297 vtoy_json_get_int(json
, "id", &id
);
3299 vtoy_json_get_bool(json
, "timeouten", &timeouten
);
3300 vtoy_json_get_bool(json
, "autoselen", &autoselen
);
3302 data
= g_data_persistence
+ index
;
3306 for (node
= data
->list
; node
; node
= node
->next
)
3310 node
->timeouten
= (int)timeouten
;
3311 node
->autoselen
= (int)autoselen
;
3312 VTOY_JSON_INT("timeout", node
->timeout
);
3313 VTOY_JSON_INT("autosel", node
->autosel
);
3319 ret
= ventoy_data_save_all();
3321 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3326 static int ventoy_api_persistence_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
3330 const char *path
= NULL
;
3331 persistence_node
*node
= NULL
;
3332 persistence_node
*cur
= NULL
;
3333 data_persistence
*data
= NULL
;
3334 VTOY_JSON
*array
= NULL
;
3336 vtoy_json_get_int(json
, "index", &index
);
3337 data
= g_data_persistence
+ index
;
3339 array
= vtoy_json_find_item(json
, JSON_TYPE_ARRAY
, "backend");
3340 path
= VTOY_JSON_STR_EX("path");
3343 node
= zalloc(sizeof(persistence_node
));
3346 node
->timeouten
= 0;
3347 node
->autoselen
= 0;
3350 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
3351 node
->list
= ventoy_path_node_add_array(array
);
3353 vtoy_list_add(data
->list
, cur
, node
);
3357 ret
= ventoy_data_save_all();
3359 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3363 static int ventoy_api_persistence_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
3367 const char *path
= NULL
;
3368 persistence_node
*last
= NULL
;
3369 persistence_node
*next
= NULL
;
3370 persistence_node
*node
= NULL
;
3371 data_persistence
*data
= NULL
;
3373 vtoy_json_get_int(json
, "index", &index
);
3374 data
= g_data_persistence
+ index
;
3376 path
= VTOY_JSON_STR_EX("path");
3379 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
3381 for (node
= data
->list
; node
; node
= next
)
3384 ventoy_free_path_node_list(node
->list
);
3391 vtoy_list_del_ex(last
, node
, data
->list
, path
, ventoy_free_path_node_list
);
3395 ret
= ventoy_data_save_all();
3397 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3401 static int ventoy_api_persistence_add_inner(struct mg_connection
*conn
, VTOY_JSON
*json
)
3405 const char *path
= NULL
;
3406 const char *outpath
= NULL
;
3407 path_node
*pcur
= NULL
;
3408 path_node
*pnode
= NULL
;
3409 persistence_node
*node
= NULL
;
3410 data_persistence
*data
= NULL
;
3412 vtoy_json_get_int(json
, "index", &index
);
3413 data
= g_data_persistence
+ index
;
3415 path
= VTOY_JSON_STR_EX("path");
3416 outpath
= VTOY_JSON_STR_EX("outpath");
3417 if (path
&& outpath
)
3419 for (node
= data
->list
; node
; node
= node
->next
)
3421 if (strcmp(outpath
, node
->path
) == 0)
3423 pnode
= zalloc(sizeof(path_node
));
3426 scnprintf(pnode
->path
, sizeof(pnode
->path
), "%s", path
);
3427 vtoy_list_add(node
->list
, pcur
, pnode
);
3435 ret
= ventoy_data_save_all();
3437 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3441 static int ventoy_api_persistence_del_inner(struct mg_connection
*conn
, VTOY_JSON
*json
)
3445 const char *path
= NULL
;
3446 const char *outpath
= NULL
;
3447 path_node
*plast
= NULL
;
3448 path_node
*pnode
= NULL
;
3449 persistence_node
*node
= NULL
;
3450 data_persistence
*data
= NULL
;
3452 vtoy_json_get_int(json
, "index", &index
);
3453 data
= g_data_persistence
+ index
;
3455 path
= VTOY_JSON_STR_EX("path");
3456 outpath
= VTOY_JSON_STR_EX("outpath");
3457 if (path
&& outpath
)
3459 for (node
= data
->list
; node
; node
= node
->next
)
3461 if (strcmp(outpath
, node
->path
) == 0)
3463 vtoy_list_del(plast
, pnode
, node
->list
, path
);
3469 ret
= ventoy_data_save_all();
3471 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3479 void ventoy_data_default_injection(data_injection
*data
)
3481 memset(data
, 0, sizeof(data_injection
));
3484 int ventoy_data_cmp_injection(data_injection
*data1
, data_injection
*data2
)
3486 injection_node
*list1
= NULL
;
3487 injection_node
*list2
= NULL
;
3489 if (NULL
== data1
->list
&& NULL
== data2
->list
)
3493 else if (data1
->list
&& data2
->list
)
3495 list1
= data1
->list
;
3496 list2
= data2
->list
;
3498 while (list1
&& list2
)
3500 if ((list1
->type
!= list2
->type
) ||
3501 strcmp(list1
->path
, list2
->path
) ||
3502 strcmp(list1
->archive
, list2
->archive
))
3507 list1
= list1
->next
;
3508 list2
= list2
->next
;
3511 if (list1
== NULL
&& list2
== NULL
)
3526 int ventoy_data_save_injection(data_injection
*data
, const char *title
, char *buf
, int buflen
)
3529 injection_node
*node
= NULL
;
3531 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
3533 VTOY_JSON_FMT_KEY_L(L1
, title
);
3534 VTOY_JSON_FMT_ARY_BEGIN_N();
3536 for (node
= data
->list
; node
; node
= node
->next
)
3538 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
3540 if (node
->type
== 0)
3542 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "image", node
->path
);
3546 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "parent", node
->path
);
3548 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "archive", node
->archive
);
3550 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
3553 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
3554 VTOY_JSON_FMT_END(pos
);
3560 int ventoy_data_json_injection(data_injection
*data
, char *buf
, int buflen
)
3564 injection_node
*node
= NULL
;
3566 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
3567 VTOY_JSON_FMT_ARY_BEGIN();
3569 for (node
= data
->list
; node
; node
= node
->next
)
3571 VTOY_JSON_FMT_OBJ_BEGIN();
3573 VTOY_JSON_FMT_UINT("type", node
->type
);
3574 VTOY_JSON_FMT_STRN("path", node
->path
);
3576 if (node
->type
== 0)
3578 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
3582 valid
= ventoy_is_directory_exist("%s%s", g_cur_dir
, node
->path
);
3584 VTOY_JSON_FMT_SINT("valid", valid
);
3586 VTOY_JSON_FMT_STRN("archive", node
->archive
);
3588 valid
= ventoy_is_file_exist("%s%s", g_cur_dir
, node
->archive
);
3589 VTOY_JSON_FMT_SINT("archive_valid", valid
);
3591 VTOY_JSON_FMT_OBJ_ENDEX();
3594 VTOY_JSON_FMT_ARY_END();
3595 VTOY_JSON_FMT_END(pos
);
3601 static int ventoy_api_get_injection(struct mg_connection
*conn
, VTOY_JSON
*json
)
3603 api_get_func(conn
, json
, injection
);
3607 static int ventoy_api_save_injection(struct mg_connection
*conn
, VTOY_JSON
*json
)
3610 ret
= ventoy_data_save_all();
3612 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3616 static int ventoy_api_injection_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
3621 const char *path
= NULL
;
3622 const char *archive
= NULL
;
3623 injection_node
*node
= NULL
;
3624 injection_node
*cur
= NULL
;
3625 data_injection
*data
= NULL
;
3627 vtoy_json_get_int(json
, "index", &index
);
3628 data
= g_data_injection
+ index
;
3630 vtoy_json_get_int(json
, "type", &type
);
3632 path
= VTOY_JSON_STR_EX("path");
3633 archive
= VTOY_JSON_STR_EX("archive");
3634 if (path
&& archive
)
3636 node
= zalloc(sizeof(injection_node
));
3641 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
3642 scnprintf(node
->archive
, sizeof(node
->archive
), "%s", archive
);
3644 vtoy_list_add(data
->list
, cur
, node
);
3648 ret
= ventoy_data_save_all();
3650 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3654 static int ventoy_api_injection_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
3658 const char *path
= NULL
;
3659 injection_node
*last
= NULL
;
3660 injection_node
*node
= NULL
;
3661 data_injection
*data
= NULL
;
3663 vtoy_json_get_int(json
, "index", &index
);
3664 data
= g_data_injection
+ index
;
3666 path
= VTOY_JSON_STR_EX("path");
3669 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
3671 vtoy_list_free(injection_node
, data
->list
);
3675 vtoy_list_del(last
, node
, data
->list
, path
);
3679 ret
= ventoy_data_save_all();
3681 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3688 static int ventoy_api_preview_json(struct mg_connection
*conn
, VTOY_JSON
*json
)
3693 int utf16enclen
= 0;
3694 char *encodebuf
= NULL
;
3695 unsigned short *utf16buf
= NULL
;
3699 /* We can not use json directly, because it will be formated in the JS. */
3701 len
= ventoy_data_real_save_all(0);
3703 utf16buf
= (unsigned short *)malloc(2 * len
+ 16);
3709 utf16enclen
= utf8_to_utf16((unsigned char *)JSON_SAVE_BUFFER
, len
, utf16buf
, len
+ 2);
3711 encodebuf
= (char *)malloc(utf16enclen
* 4 + 16);
3717 for (i
= 0; i
< utf16enclen
; i
++)
3719 scnprintf(encodebuf
+ i
* 4, 5, "%04X", utf16buf
[i
]);
3723 VTOY_JSON_FMT_BEGIN(pos
, JSON_BUFFER
, JSON_BUF_MAX
);
3724 VTOY_JSON_FMT_OBJ_BEGIN();
3725 VTOY_JSON_FMT_STRN("json", (encodebuf
? encodebuf
: ""));
3726 VTOY_JSON_FMT_OBJ_END();
3727 VTOY_JSON_FMT_END(pos
);
3729 CHECK_FREE(encodebuf
);
3730 CHECK_FREE(utf16buf
);
3732 ventoy_json_buffer(conn
, JSON_BUFFER
, pos
);
3740 int ventoy_data_save_all(void)
3742 ventoy_set_writeback_event();
3746 int ventoy_data_real_save_all(int apilock
)
3754 pthread_mutex_lock(&g_api_mutex
);
3757 ssprintf(pos
, JSON_SAVE_BUFFER
, JSON_BUF_MAX
, "{\n");
3759 ventoy_save_plug(control
);
3760 ventoy_save_plug(theme
);
3761 ventoy_save_plug(menu_alias
);
3762 ventoy_save_plug(menu_tip
);
3763 ventoy_save_plug(menu_class
);
3764 ventoy_save_plug(auto_install
);
3765 ventoy_save_plug(persistence
);
3766 ventoy_save_plug(injection
);
3767 ventoy_save_plug(conf_replace
);
3768 ventoy_save_plug(password
);
3769 ventoy_save_plug(image_list
);
3770 ventoy_save_plug(auto_memdisk
);
3771 ventoy_save_plug(dud
);
3773 if (JSON_SAVE_BUFFER
[pos
- 1] == '\n' && JSON_SAVE_BUFFER
[pos
- 2] == ',')
3775 JSON_SAVE_BUFFER
[pos
- 2] = '\n';
3778 ssprintf(pos
, JSON_SAVE_BUFFER
, JSON_BUF_MAX
, "}\n");
3782 pthread_mutex_unlock(&g_api_mutex
);
3788 int ventoy_http_writeback(void)
3794 ventoy_get_json_path(filename
, NULL
);
3796 pos
= ventoy_data_real_save_all(1);
3799 printf("%s", JSON_SAVE_BUFFER
);
3802 ret
= ventoy_write_buf_to_file(filename
, JSON_SAVE_BUFFER
, pos
);
3805 vlog("Failed to write ventoy.json file.\n");
3806 g_sysinfo
.config_save_error
= 1;
3813 static JSON_CB g_ventoy_json_cb
[] =
3815 { "sysinfo", ventoy_api_sysinfo
},
3816 { "handshake", ventoy_api_handshake
},
3817 { "check_path", ventoy_api_check_exist
},
3818 { "check_path2", ventoy_api_check_exist2
},
3819 { "check_fuzzy", ventoy_api_check_fuzzy
},
3821 { "device_info", ventoy_api_device_info
},
3823 { "get_control", ventoy_api_get_control
},
3824 { "save_control", ventoy_api_save_control
},
3826 { "get_theme", ventoy_api_get_theme
},
3827 { "save_theme", ventoy_api_save_theme
},
3828 { "theme_add_file", ventoy_api_theme_add_file
},
3829 { "theme_del_file", ventoy_api_theme_del_file
},
3830 { "theme_add_font", ventoy_api_theme_add_font
},
3831 { "theme_del_font", ventoy_api_theme_del_font
},
3833 { "get_alias", ventoy_api_get_alias
},
3834 { "save_alias", ventoy_api_save_alias
},
3835 { "alias_add", ventoy_api_alias_add
},
3836 { "alias_del", ventoy_api_alias_del
},
3838 { "get_tip", ventoy_api_get_tip
},
3839 { "save_tip", ventoy_api_save_tip
},
3840 { "tip_add", ventoy_api_tip_add
},
3841 { "tip_del", ventoy_api_tip_del
},
3843 { "get_class", ventoy_api_get_class
},
3844 { "save_class", ventoy_api_save_class
},
3845 { "class_add", ventoy_api_class_add
},
3846 { "class_del", ventoy_api_class_del
},
3848 { "get_auto_memdisk", ventoy_api_get_auto_memdisk
},
3849 { "save_auto_memdisk", ventoy_api_save_auto_memdisk
},
3850 { "auto_memdisk_add", ventoy_api_auto_memdisk_add
},
3851 { "auto_memdisk_del", ventoy_api_auto_memdisk_del
},
3853 { "get_image_list", ventoy_api_get_image_list
},
3854 { "save_image_list", ventoy_api_save_image_list
},
3855 { "image_list_add", ventoy_api_image_list_add
},
3856 { "image_list_del", ventoy_api_image_list_del
},
3858 { "get_conf_replace", ventoy_api_get_conf_replace
},
3859 { "save_conf_replace", ventoy_api_save_conf_replace
},
3860 { "conf_replace_add", ventoy_api_conf_replace_add
},
3861 { "conf_replace_del", ventoy_api_conf_replace_del
},
3863 { "get_dud", ventoy_api_get_dud
},
3864 { "save_dud", ventoy_api_save_dud
},
3865 { "dud_add", ventoy_api_dud_add
},
3866 { "dud_del", ventoy_api_dud_del
},
3867 { "dud_add_inner", ventoy_api_dud_add_inner
},
3868 { "dud_del_inner", ventoy_api_dud_del_inner
},
3870 { "get_auto_install", ventoy_api_get_auto_install
},
3871 { "save_auto_install", ventoy_api_save_auto_install
},
3872 { "auto_install_add", ventoy_api_auto_install_add
},
3873 { "auto_install_del", ventoy_api_auto_install_del
},
3874 { "auto_install_add_inner", ventoy_api_auto_install_add_inner
},
3875 { "auto_install_del_inner", ventoy_api_auto_install_del_inner
},
3877 { "get_persistence", ventoy_api_get_persistence
},
3878 { "save_persistence", ventoy_api_save_persistence
},
3879 { "persistence_add", ventoy_api_persistence_add
},
3880 { "persistence_del", ventoy_api_persistence_del
},
3881 { "persistence_add_inner", ventoy_api_persistence_add_inner
},
3882 { "persistence_del_inner", ventoy_api_persistence_del_inner
},
3884 { "get_password", ventoy_api_get_password
},
3885 { "save_password", ventoy_api_save_password
},
3886 { "password_add", ventoy_api_password_add
},
3887 { "password_del", ventoy_api_password_del
},
3889 { "get_injection", ventoy_api_get_injection
},
3890 { "save_injection", ventoy_api_save_injection
},
3891 { "injection_add", ventoy_api_injection_add
},
3892 { "injection_del", ventoy_api_injection_del
},
3893 { "preview_json", ventoy_api_preview_json
},
3897 static int ventoy_json_handler(struct mg_connection
*conn
, VTOY_JSON
*json
, char *jsonstr
)
3900 const char *method
= NULL
;
3902 method
= vtoy_json_get_string_ex(json
, "method");
3905 ventoy_json_result(conn
, VTOY_JSON_SUCCESS_RET
);
3909 if (strcmp(method
, "handshake") == 0)
3911 ventoy_api_handshake(conn
, json
);
3915 for (i
= 0; i
< (int)(sizeof(g_ventoy_json_cb
) / sizeof(g_ventoy_json_cb
[0])); i
++)
3917 if (strcmp(method
, g_ventoy_json_cb
[i
].method
) == 0)
3919 g_ventoy_json_cb
[i
].callback(conn
, json
);
3927 static int ventoy_request_handler(struct mg_connection
*conn
)
3931 VTOY_JSON
*json
= NULL
;
3932 char *post_data_buf
= NULL
;
3933 const struct mg_request_info
*ri
= NULL
;
3934 char stack_buf
[512];
3936 ri
= mg_get_request_info(conn
);
3938 if (strcmp(ri
->uri
, "/vtoy/json") == 0)
3940 if (ri
->content_length
> 500)
3942 post_data_buf
= malloc((int)(ri
->content_length
+ 4));
3943 post_buf_len
= (int)(ri
->content_length
+ 1);
3947 post_data_buf
= stack_buf
;
3948 post_buf_len
= sizeof(stack_buf
);
3951 post_data_len
= mg_read(conn
, post_data_buf
, post_buf_len
);
3952 post_data_buf
[post_data_len
] = 0;
3954 json
= vtoy_json_create();
3955 if (JSON_SUCCESS
== vtoy_json_parse(json
, post_data_buf
))
3957 pthread_mutex_lock(&g_api_mutex
);
3958 ventoy_json_handler(conn
, json
->pstChild
, post_data_buf
);
3959 pthread_mutex_unlock(&g_api_mutex
);
3963 ventoy_json_result(conn
, VTOY_JSON_INVALID_RET
);
3966 vtoy_json_destroy(json
);
3968 if (post_data_buf
!= stack_buf
)
3970 free(post_data_buf
);
3980 const char *ventoy_web_openfile(const struct mg_connection
*conn
, const char *path
, size_t *data_len
)
3982 ventoy_file
*node
= NULL
;
3991 node
= ventoy_tar_find_file(path
);
3994 *data_len
= node
->size
;
4006 static int ventoy_parse_control(VTOY_JSON
*json
, void *p
)
4009 VTOY_JSON
*node
= NULL
;
4010 VTOY_JSON
*child
= NULL
;
4011 data_control
*data
= (data_control
*)p
;
4013 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4018 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4020 if (node
->enDataType
== JSON_TYPE_OBJECT
)
4022 child
= node
->pstChild
;
4024 if (child
->enDataType
!= JSON_TYPE_STRING
)
4029 if (strcmp(child
->pcName
, "VTOY_DEFAULT_MENU_MODE") == 0)
4031 CONTROL_PARSE_INT_DEF_0(child
, data
->default_menu_mode
);
4033 else if (strcmp(child
->pcName
, "VTOY_WIN11_BYPASS_CHECK") == 0)
4035 CONTROL_PARSE_INT_DEF_0(child
, data
->win11_bypass_check
);
4037 else if (strcmp(child
->pcName
, "VTOY_LINUX_REMOUNT") == 0)
4039 CONTROL_PARSE_INT_DEF_0(child
, data
->linux_remount
);
4041 else if (strcmp(child
->pcName
, "VTOY_SECONDARY_BOOT_MENU") == 0)
4043 CONTROL_PARSE_INT_DEF_1(child
, data
->secondary_menu
);
4045 else if (strcmp(child
->pcName
, "VTOY_SHOW_PASSWORD_ASTERISK") == 0)
4047 CONTROL_PARSE_INT_DEF_1(child
, data
->password_asterisk
);
4049 else if (strcmp(child
->pcName
, "VTOY_TREE_VIEW_MENU_STYLE") == 0)
4051 CONTROL_PARSE_INT_DEF_0(child
, data
->treeview_style
);
4053 else if (strcmp(child
->pcName
, "VTOY_FILT_DOT_UNDERSCORE_FILE") == 0)
4055 CONTROL_PARSE_INT_DEF_1(child
, data
->filter_dot_underscore
);
4057 else if (strcmp(child
->pcName
, "VTOY_SORT_CASE_SENSITIVE") == 0)
4059 CONTROL_PARSE_INT_DEF_0(child
, data
->sort_casesensitive
);
4061 else if (strcmp(child
->pcName
, "VTOY_MAX_SEARCH_LEVEL") == 0)
4063 if (strcmp(child
->unData
.pcStrVal
, "max") == 0)
4065 data
->max_search_level
= -1;
4069 data
->max_search_level
= (int)strtol(child
->unData
.pcStrVal
, NULL
, 10);
4072 else if (strcmp(child
->pcName
, "VTOY_DEFAULT_SEARCH_ROOT") == 0)
4074 strlcpy(data
->default_search_root
, child
->unData
.pcStrVal
);
4076 else if (strcmp(child
->pcName
, "VTOY_DEFAULT_IMAGE") == 0)
4078 strlcpy(data
->default_image
, child
->unData
.pcStrVal
);
4080 else if (strcmp(child
->pcName
, "VTOY_DEFAULT_KBD_LAYOUT") == 0)
4082 for (i
= 0; g_ventoy_kbd_layout
[i
]; i
++)
4084 if (strcmp(child
->unData
.pcStrVal
, g_ventoy_kbd_layout
[i
]) == 0)
4086 strlcpy(data
->default_kbd_layout
, child
->unData
.pcStrVal
);
4091 else if (strcmp(child
->pcName
, "VTOY_HELP_TXT_LANGUAGE") == 0)
4093 for (i
= 0; g_ventoy_help_lang
[i
][0]; i
++)
4095 if (strcmp(child
->unData
.pcStrVal
, g_ventoy_help_lang
[i
]) == 0)
4097 strlcpy(data
->help_text_language
, child
->unData
.pcStrVal
);
4102 else if (strcmp(child
->pcName
, "VTOY_MENU_LANGUAGE") == 0)
4104 for (i
= 0; g_ventoy_menu_lang
[i
][0]; i
++)
4106 if (strcmp(child
->unData
.pcStrVal
, g_ventoy_menu_lang
[i
]) == 0)
4108 strlcpy(data
->menu_language
, child
->unData
.pcStrVal
);
4113 else if (strcmp(child
->pcName
, "VTOY_MENU_TIMEOUT") == 0)
4115 data
->menu_timeout
= (int)strtol(child
->unData
.pcStrVal
, NULL
, 10);
4117 else if (strcmp(child
->pcName
, "VTOY_SECONDARY_TIMEOUT") == 0)
4119 data
->secondary_menu_timeout
= (int)strtol(child
->unData
.pcStrVal
, NULL
, 10);
4121 else if (strcmp(child
->pcName
, "VTOY_VHD_NO_WARNING") == 0)
4123 CONTROL_PARSE_INT_DEF_0(child
, data
->vhd_no_warning
);
4125 else if (strcmp(child
->pcName
, "VTOY_FILE_FLT_ISO") == 0)
4127 CONTROL_PARSE_INT_DEF_0(child
, data
->filter_iso
);
4129 else if (strcmp(child
->pcName
, "VTOY_FILE_FLT_IMG") == 0)
4131 CONTROL_PARSE_INT_DEF_0(child
, data
->filter_img
);
4133 else if (strcmp(child
->pcName
, "VTOY_FILE_FLT_EFI") == 0)
4135 CONTROL_PARSE_INT_DEF_0(child
, data
->filter_efi
);
4137 else if (strcmp(child
->pcName
, "VTOY_FILE_FLT_WIM") == 0)
4139 CONTROL_PARSE_INT_DEF_0(child
, data
->filter_wim
);
4141 else if (strcmp(child
->pcName
, "VTOY_FILE_FLT_VHD") == 0)
4143 CONTROL_PARSE_INT_DEF_0(child
, data
->filter_vhd
);
4145 else if (strcmp(child
->pcName
, "VTOY_FILE_FLT_VTOY") == 0)
4147 CONTROL_PARSE_INT_DEF_0(child
, data
->filter_vtoy
);
4155 static int ventoy_parse_theme(VTOY_JSON
*json
, void *p
)
4157 const char *dismode
= NULL
;
4158 VTOY_JSON
*child
= NULL
;
4159 VTOY_JSON
*node
= NULL
;
4160 path_node
*tail
= NULL
;
4161 path_node
*pnode
= NULL
;
4162 data_theme
*data
= (data_theme
*)p
;
4164 if (json
->enDataType
!= JSON_TYPE_OBJECT
)
4169 child
= json
->pstChild
;
4171 dismode
= vtoy_json_get_string_ex(child
, "display_mode");
4172 vtoy_json_get_string(child
, "ventoy_left", sizeof(data
->ventoy_left
), data
->ventoy_left
);
4173 vtoy_json_get_string(child
, "ventoy_top", sizeof(data
->ventoy_top
), data
->ventoy_top
);
4174 vtoy_json_get_string(child
, "ventoy_color", sizeof(data
->ventoy_color
), data
->ventoy_color
);
4176 vtoy_json_get_int(child
, "default_file", &(data
->default_file
));
4177 vtoy_json_get_string(child
, "gfxmode", sizeof(data
->gfxmode
), data
->gfxmode
);
4178 vtoy_json_get_string(child
, "serial_param", sizeof(data
->serial_param
), data
->serial_param
);
4182 if (strcmp(dismode
, "CLI") == 0)
4184 data
->display_mode
= display_mode_cli
;
4186 else if (strcmp(dismode
, "serial") == 0)
4188 data
->display_mode
= display_mode_serial
;
4190 else if (strcmp(dismode
, "serial_console") == 0)
4192 data
->display_mode
= display_mode_ser_console
;
4196 data
->display_mode
= display_mode_gui
;
4200 node
= vtoy_json_find_item(child
, JSON_TYPE_STRING
, "file");
4203 data
->default_file
= 0;
4205 pnode
= zalloc(sizeof(path_node
));
4208 strlcpy(pnode
->path
, node
->unData
.pcStrVal
);
4209 data
->filelist
= pnode
;
4214 node
= vtoy_json_find_item(child
, JSON_TYPE_ARRAY
, "file");
4217 for (node
= node
->pstChild
; node
; node
= node
->pstNext
)
4219 if (node
->enDataType
== JSON_TYPE_STRING
)
4221 pnode
= zalloc(sizeof(path_node
));
4224 strlcpy(pnode
->path
, node
->unData
.pcStrVal
);
4232 data
->filelist
= tail
= pnode
;
4241 node
= vtoy_json_find_item(child
, JSON_TYPE_ARRAY
, "fonts");
4244 for (node
= node
->pstChild
; node
; node
= node
->pstNext
)
4246 if (node
->enDataType
== JSON_TYPE_STRING
)
4248 pnode
= zalloc(sizeof(path_node
));
4251 strlcpy(pnode
->path
, node
->unData
.pcStrVal
);
4252 if (data
->fontslist
)
4259 data
->fontslist
= tail
= pnode
;
4268 static int ventoy_parse_menu_alias(VTOY_JSON
*json
, void *p
)
4271 const char *path
= NULL
;
4272 const char *alias
= NULL
;
4273 data_alias
*data
= (data_alias
*)p
;
4274 data_alias_node
*tail
= NULL
;
4275 data_alias_node
*pnode
= NULL
;
4276 VTOY_JSON
*node
= NULL
;
4278 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4283 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4285 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4290 type
= path_type_file
;
4291 path
= vtoy_json_get_string_ex(node
->pstChild
, "image");
4294 path
= vtoy_json_get_string_ex(node
->pstChild
, "dir");
4295 type
= path_type_dir
;
4297 alias
= vtoy_json_get_string_ex(node
->pstChild
, "alias");
4301 pnode
= zalloc(sizeof(data_alias_node
));
4305 strlcpy(pnode
->path
, path
);
4306 strlcpy(pnode
->alias
, alias
);
4315 data
->list
= tail
= pnode
;
4324 static int ventoy_parse_menu_tip(VTOY_JSON
*json
, void *p
)
4327 const char *path
= NULL
;
4328 const char *tip
= NULL
;
4329 data_tip
*data
= (data_tip
*)p
;
4330 data_tip_node
*tail
= NULL
;
4331 data_tip_node
*pnode
= NULL
;
4332 VTOY_JSON
*node
= NULL
;
4333 VTOY_JSON
*tips
= NULL
;
4335 if (json
->enDataType
!= JSON_TYPE_OBJECT
)
4340 vtoy_json_get_string(json
->pstChild
, "left", sizeof(data
->left
), data
->left
);
4341 vtoy_json_get_string(json
->pstChild
, "top", sizeof(data
->top
), data
->top
);
4342 vtoy_json_get_string(json
->pstChild
, "color", sizeof(data
->color
), data
->color
);
4344 tips
= vtoy_json_find_item(json
->pstChild
, JSON_TYPE_ARRAY
, "tips");
4350 for (node
= tips
->pstChild
; node
; node
= node
->pstNext
)
4352 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4357 type
= path_type_file
;
4358 path
= vtoy_json_get_string_ex(node
->pstChild
, "image");
4361 path
= vtoy_json_get_string_ex(node
->pstChild
, "dir");
4362 type
= path_type_dir
;
4364 tip
= vtoy_json_get_string_ex(node
->pstChild
, "tip");
4368 pnode
= zalloc(sizeof(data_tip_node
));
4372 strlcpy(pnode
->path
, path
);
4373 strlcpy(pnode
->tip
, tip
);
4382 data
->list
= tail
= pnode
;
4390 static int ventoy_parse_menu_class(VTOY_JSON
*json
, void *p
)
4393 const char *path
= NULL
;
4394 const char *class = NULL
;
4395 data_class
*data
= (data_class
*)p
;
4396 data_class_node
*tail
= NULL
;
4397 data_class_node
*pnode
= NULL
;
4398 VTOY_JSON
*node
= NULL
;
4400 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4405 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4407 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4412 type
= class_type_key
;
4413 path
= vtoy_json_get_string_ex(node
->pstChild
, "key");
4416 type
= class_type_dir
;
4417 path
= vtoy_json_get_string_ex(node
->pstChild
, "dir");
4420 type
= class_type_parent
;
4421 path
= vtoy_json_get_string_ex(node
->pstChild
, "parent");
4424 class = vtoy_json_get_string_ex(node
->pstChild
, "class");
4428 pnode
= zalloc(sizeof(data_class_node
));
4432 strlcpy(pnode
->path
, path
);
4433 strlcpy(pnode
->class, class);
4442 data
->list
= tail
= pnode
;
4450 static int ventoy_parse_auto_install(VTOY_JSON
*json
, void *p
)
4458 const char *path
= NULL
;
4459 const char *file
= NULL
;
4460 data_auto_install
*data
= (data_auto_install
*)p
;
4461 auto_install_node
*tail
= NULL
;
4462 auto_install_node
*pnode
= NULL
;
4463 path_node
*pathnode
= NULL
;
4464 path_node
*pathtail
= NULL
;
4465 VTOY_JSON
*node
= NULL
;
4466 VTOY_JSON
*filelist
= NULL
;
4467 VTOY_JSON
*filenode
= NULL
;
4469 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4474 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4476 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4482 path
= vtoy_json_get_string_ex(node
->pstChild
, "image");
4485 path
= vtoy_json_get_string_ex(node
->pstChild
, "parent");
4493 file
= vtoy_json_get_string_ex(node
->pstChild
, "template");
4496 pnode
= zalloc(sizeof(auto_install_node
));
4501 strlcpy(pnode
->path
, path
);
4503 pathnode
= zalloc(sizeof(path_node
));
4506 strlcpy(pathnode
->path
, file
);
4507 pnode
->list
= pathnode
;
4521 data
->list
= tail
= pnode
;
4529 timeouten
= autoselen
= 0;
4530 if (JSON_SUCCESS
== vtoy_json_get_int(node
->pstChild
, "timeout", &timeout
))
4534 if (JSON_SUCCESS
== vtoy_json_get_int(node
->pstChild
, "autosel", &autosel
))
4539 filelist
= vtoy_json_find_item(node
->pstChild
, JSON_TYPE_ARRAY
, "template");
4545 pnode
= zalloc(sizeof(auto_install_node
));
4552 pnode
->autoselen
= autoselen
;
4553 pnode
->timeouten
= timeouten
;
4554 pnode
->timeout
= timeout
;
4555 pnode
->autosel
= autosel
;
4556 strlcpy(pnode
->path
, path
);
4559 for (filenode
= filelist
->pstChild
; filenode
; filenode
= filenode
->pstNext
)
4561 if (filenode
->enDataType
!= JSON_TYPE_STRING
)
4566 pathnode
= zalloc(sizeof(path_node
));
4570 strlcpy(pathnode
->path
, filenode
->unData
.pcStrVal
);
4574 pathtail
->next
= pathnode
;
4575 pathtail
= pathnode
;
4579 pnode
->list
= pathtail
= pathnode
;
4590 if (pnode
->autoselen
&& pnode
->autosel
> count
)
4602 data
->list
= tail
= pnode
;
4609 static int ventoy_parse_persistence(VTOY_JSON
*json
, void *p
)
4616 const char *path
= NULL
;
4617 const char *file
= NULL
;
4618 data_persistence
*data
= (data_persistence
*)p
;
4619 persistence_node
*tail
= NULL
;
4620 persistence_node
*pnode
= NULL
;
4621 path_node
*pathnode
= NULL
;
4622 path_node
*pathtail
= NULL
;
4623 VTOY_JSON
*node
= NULL
;
4624 VTOY_JSON
*filelist
= NULL
;
4625 VTOY_JSON
*filenode
= NULL
;
4627 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4632 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4634 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4639 path
= vtoy_json_get_string_ex(node
->pstChild
, "image");
4645 file
= vtoy_json_get_string_ex(node
->pstChild
, "backend");
4648 pnode
= zalloc(sizeof(persistence_node
));
4653 strlcpy(pnode
->path
, path
);
4655 pathnode
= zalloc(sizeof(path_node
));
4658 strlcpy(pathnode
->path
, file
);
4659 pnode
->list
= pathnode
;
4673 data
->list
= tail
= pnode
;
4681 timeouten
= autoselen
= 0;
4682 if (JSON_SUCCESS
== vtoy_json_get_int(node
->pstChild
, "timeout", &timeout
))
4686 if (JSON_SUCCESS
== vtoy_json_get_int(node
->pstChild
, "autosel", &autosel
))
4691 filelist
= vtoy_json_find_item(node
->pstChild
, JSON_TYPE_ARRAY
, "backend");
4697 pnode
= zalloc(sizeof(persistence_node
));
4704 pnode
->autoselen
= autoselen
;
4705 pnode
->timeouten
= timeouten
;
4706 pnode
->timeout
= timeout
;
4707 pnode
->autosel
= autosel
;
4708 strlcpy(pnode
->path
, path
);
4711 for (filenode
= filelist
->pstChild
; filenode
; filenode
= filenode
->pstNext
)
4713 if (filenode
->enDataType
!= JSON_TYPE_STRING
)
4718 pathnode
= zalloc(sizeof(path_node
));
4722 strlcpy(pathnode
->path
, filenode
->unData
.pcStrVal
);
4726 pathtail
->next
= pathnode
;
4727 pathtail
= pathnode
;
4731 pnode
->list
= pathtail
= pathnode
;
4742 if (pnode
->autoselen
&& pnode
->autosel
> count
)
4754 data
->list
= tail
= pnode
;
4761 static int ventoy_parse_injection(VTOY_JSON
*json
, void *p
)
4764 const char *path
= NULL
;
4765 const char *archive
= NULL
;
4766 data_injection
*data
= (data_injection
*)p
;
4767 injection_node
*tail
= NULL
;
4768 injection_node
*pnode
= NULL
;
4769 VTOY_JSON
*node
= NULL
;
4771 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4776 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4778 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4784 path
= vtoy_json_get_string_ex(node
->pstChild
, "image");
4787 path
= vtoy_json_get_string_ex(node
->pstChild
, "parent");
4790 archive
= vtoy_json_get_string_ex(node
->pstChild
, "archive");
4792 if (path
&& archive
)
4794 pnode
= zalloc(sizeof(injection_node
));
4798 strlcpy(pnode
->path
, path
);
4799 strlcpy(pnode
->archive
, archive
);
4808 data
->list
= tail
= pnode
;
4816 static int ventoy_parse_conf_replace(VTOY_JSON
*json
, void *p
)
4819 const char *path
= NULL
;
4820 const char *org
= NULL
;
4821 const char *new = NULL
;
4822 data_conf_replace
*data
= (data_conf_replace
*)p
;
4823 conf_replace_node
*tail
= NULL
;
4824 conf_replace_node
*pnode
= NULL
;
4825 VTOY_JSON
*node
= NULL
;
4827 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4832 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4834 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4839 path
= vtoy_json_get_string_ex(node
->pstChild
, "iso");
4840 org
= vtoy_json_get_string_ex(node
->pstChild
, "org");
4841 new = vtoy_json_get_string_ex(node
->pstChild
, "new");
4844 vtoy_json_get_int(node
->pstChild
, "img", &img
);
4846 if (path
&& org
&& new)
4848 pnode
= zalloc(sizeof(conf_replace_node
));
4851 strlcpy(pnode
->path
, path
);
4852 strlcpy(pnode
->org
, org
);
4853 strlcpy(pnode
->new, new);
4866 data
->list
= tail
= pnode
;
4874 static int ventoy_parse_password(VTOY_JSON
*json
, void *p
)
4877 const char *bootpwd
= NULL
;
4878 const char *isopwd
= NULL
;
4879 const char *wimpwd
= NULL
;
4880 const char *imgpwd
= NULL
;
4881 const char *efipwd
= NULL
;
4882 const char *vhdpwd
= NULL
;
4883 const char *vtoypwd
= NULL
;
4884 const char *path
= NULL
;
4885 const char *pwd
= NULL
;
4886 data_password
*data
= (data_password
*)p
;
4887 menu_password
*tail
= NULL
;
4888 menu_password
*pnode
= NULL
;
4889 VTOY_JSON
*node
= NULL
;
4890 VTOY_JSON
*menupwd
= NULL
;
4892 if (json
->enDataType
!= JSON_TYPE_OBJECT
)
4897 bootpwd
= vtoy_json_get_string_ex(json
->pstChild
, "bootpwd");
4898 isopwd
= vtoy_json_get_string_ex(json
->pstChild
, "isopwd");
4899 wimpwd
= vtoy_json_get_string_ex(json
->pstChild
, "wimpwd");
4900 imgpwd
= vtoy_json_get_string_ex(json
->pstChild
, "imgpwd");
4901 efipwd
= vtoy_json_get_string_ex(json
->pstChild
, "efipwd");
4902 vhdpwd
= vtoy_json_get_string_ex(json
->pstChild
, "vhdpwd");
4903 vtoypwd
= vtoy_json_get_string_ex(json
->pstChild
, "vtoypwd");
4906 if (bootpwd
) strlcpy(data
->bootpwd
, bootpwd
);
4907 if (isopwd
) strlcpy(data
->isopwd
, isopwd
);
4908 if (wimpwd
) strlcpy(data
->wimpwd
, wimpwd
);
4909 if (imgpwd
) strlcpy(data
->imgpwd
, imgpwd
);
4910 if (efipwd
) strlcpy(data
->efipwd
, efipwd
);
4911 if (vhdpwd
) strlcpy(data
->vhdpwd
, vhdpwd
);
4912 if (vtoypwd
) strlcpy(data
->vtoypwd
, vtoypwd
);
4915 menupwd
= vtoy_json_find_item(json
->pstChild
, JSON_TYPE_ARRAY
, "menupwd");
4921 for (node
= menupwd
->pstChild
; node
; node
= node
->pstNext
)
4923 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4929 path
= vtoy_json_get_string_ex(node
->pstChild
, "file");
4932 path
= vtoy_json_get_string_ex(node
->pstChild
, "parent");
4935 pwd
= vtoy_json_get_string_ex(node
->pstChild
, "pwd");
4939 pnode
= zalloc(sizeof(menu_password
));
4943 strlcpy(pnode
->path
, path
);
4944 strlcpy(pnode
->pwd
, pwd
);
4953 data
->list
= tail
= pnode
;
4962 static int ventoy_parse_image_list_real(VTOY_JSON
*json
, int type
, void *p
)
4964 VTOY_JSON
*node
= NULL
;
4965 data_image_list
*data
= (data_image_list
*)p
;
4966 path_node
*tail
= NULL
;
4967 path_node
*pnode
= NULL
;
4969 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4976 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4978 if (node
->enDataType
== JSON_TYPE_STRING
)
4980 pnode
= zalloc(sizeof(path_node
));
4983 strlcpy(pnode
->path
, node
->unData
.pcStrVal
);
4991 data
->list
= tail
= pnode
;
4999 static int ventoy_parse_image_blacklist(VTOY_JSON
*json
, void *p
)
5001 return ventoy_parse_image_list_real(json
, 1, p
);
5003 static int ventoy_parse_image_list(VTOY_JSON
*json
, void *p
)
5005 return ventoy_parse_image_list_real(json
, 0, p
);
5008 static int ventoy_parse_auto_memdisk(VTOY_JSON
*json
, void *p
)
5010 VTOY_JSON
*node
= NULL
;
5011 data_auto_memdisk
*data
= (data_auto_memdisk
*)p
;
5012 path_node
*tail
= NULL
;
5013 path_node
*pnode
= NULL
;
5015 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
5020 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
5022 if (node
->enDataType
== JSON_TYPE_STRING
)
5024 pnode
= zalloc(sizeof(path_node
));
5027 strlcpy(pnode
->path
, node
->unData
.pcStrVal
);
5035 data
->list
= tail
= pnode
;
5043 static int ventoy_parse_dud(VTOY_JSON
*json
, void *p
)
5046 const char *path
= NULL
;
5047 const char *file
= NULL
;
5048 data_dud
*data
= (data_dud
*)p
;
5049 dud_node
*tail
= NULL
;
5050 dud_node
*pnode
= NULL
;
5051 path_node
*pathnode
= NULL
;
5052 path_node
*pathtail
= NULL
;
5053 VTOY_JSON
*node
= NULL
;
5054 VTOY_JSON
*filelist
= NULL
;
5055 VTOY_JSON
*filenode
= NULL
;
5057 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
5062 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
5064 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
5069 path
= vtoy_json_get_string_ex(node
->pstChild
, "image");
5075 file
= vtoy_json_get_string_ex(node
->pstChild
, "dud");
5078 pnode
= zalloc(sizeof(dud_node
));
5081 strlcpy(pnode
->path
, path
);
5083 pathnode
= zalloc(sizeof(path_node
));
5086 strlcpy(pathnode
->path
, file
);
5087 pnode
->list
= pathnode
;
5101 data
->list
= tail
= pnode
;
5108 filelist
= vtoy_json_find_item(node
->pstChild
, JSON_TYPE_ARRAY
, "dud");
5114 pnode
= zalloc(sizeof(dud_node
));
5120 strlcpy(pnode
->path
, path
);
5122 for (filenode
= filelist
->pstChild
; filenode
; filenode
= filenode
->pstNext
)
5124 if (filenode
->enDataType
!= JSON_TYPE_STRING
)
5129 pathnode
= zalloc(sizeof(path_node
));
5132 strlcpy(pathnode
->path
, filenode
->unData
.pcStrVal
);
5137 pathtail
->next
= pathnode
;
5138 pathtail
= pathnode
;
5142 pnode
->list
= pathtail
= pathnode
;
5160 data
->list
= tail
= pnode
;
5174 static int ventoy_load_old_json(const char *filename
)
5179 char *buffer
= NULL
;
5180 unsigned char *start
= NULL
;
5181 VTOY_JSON
*json
= NULL
;
5182 VTOY_JSON
*node
= NULL
;
5183 VTOY_JSON
*next
= NULL
;
5185 ret
= ventoy_read_file_to_buf(filename
, 4, (void **)&buffer
, &buflen
);
5188 vlog("Failed to read old ventoy.json file.\n");
5193 start
= (unsigned char *)buffer
;
5195 if (start
[0] == 0xef && start
[1] == 0xbb && start
[2] == 0xbf)
5199 else if ((start
[0] == 0xff && start
[1] == 0xfe) || (start
[0] == 0xfe && start
[1] == 0xff))
5201 vlog("ventoy.json is in UCS-2 encoding, ignore it.\n");
5206 json
= vtoy_json_create();
5213 if (vtoy_json_parse_ex(json
, buffer
+ offset
, buflen
- offset
) == JSON_SUCCESS
)
5215 vlog("parse ventoy.json success\n");
5217 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
5218 for (next
= node
->pstNext
; next
; next
= next
->pstNext
)
5220 if (node
->pcName
&& next
->pcName
&& strcmp(node
->pcName
, next
->pcName
) == 0)
5222 vlog("ventoy.json contains duplicate key <%s>.\n", node
->pcName
);
5223 g_sysinfo
.invalid_config
= 1;
5229 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
5231 ventoy_parse_json(control
);
5232 ventoy_parse_json(theme
);
5233 ventoy_parse_json(menu_alias
);
5234 ventoy_parse_json(menu_tip
);
5235 ventoy_parse_json(menu_class
);
5236 ventoy_parse_json(auto_install
);
5237 ventoy_parse_json(persistence
);
5238 ventoy_parse_json(injection
);
5239 ventoy_parse_json(conf_replace
);
5240 ventoy_parse_json(password
);
5241 ventoy_parse_json(image_list
);
5242 ventoy_parse_json(image_blacklist
);
5243 ventoy_parse_json(auto_memdisk
);
5244 ventoy_parse_json(dud
);
5249 vlog("ventoy.json has syntax error.\n");
5250 g_sysinfo
.syntax_error
= 1;
5255 vtoy_json_destroy(json
);
5262 int ventoy_http_start(const char *ip
, const char *port
)
5268 char backupname
[128];
5269 struct mg_callbacks callbacks
;
5270 const char *options
[] =
5272 "listening_ports", "24681",
5273 "document_root", "www",
5274 "index_files", "index.html",
5275 "num_threads", "16",
5276 "error_log_file", LOG_FILE
,
5277 "request_timeout_ms", "10000",
5281 for (i
= 0; i
<= bios_max
; i
++)
5283 ventoy_data_default_control(g_data_control
+ i
);
5284 ventoy_data_default_theme(g_data_theme
+ i
);
5285 ventoy_data_default_menu_alias(g_data_menu_alias
+ i
);
5286 ventoy_data_default_menu_class(g_data_menu_class
+ i
);
5287 ventoy_data_default_menu_tip(g_data_menu_tip
+ i
);
5288 ventoy_data_default_auto_install(g_data_auto_install
+ i
);
5289 ventoy_data_default_persistence(g_data_persistence
+ i
);
5290 ventoy_data_default_injection(g_data_injection
+ i
);
5291 ventoy_data_default_conf_replace(g_data_conf_replace
+ i
);
5292 ventoy_data_default_password(g_data_password
+ i
);
5293 ventoy_data_default_image_list(g_data_image_list
+ i
);
5294 ventoy_data_default_auto_memdisk(g_data_auto_memdisk
+ i
);
5295 ventoy_data_default_dud(g_data_dud
+ i
);
5298 ventoy_get_json_path(filename
, backupname
);
5299 if (ventoy_is_file_exist("%s", filename
))
5301 ventoy_copy_file(filename
, backupname
);
5302 ret
= ventoy_load_old_json(filename
);
5305 ventoy_data_real_save_all(0);
5310 scnprintf(addr
, sizeof(addr
), "%s:%s", ip
, port
);
5313 memset(&callbacks
, 0, sizeof(callbacks
));
5314 callbacks
.begin_request
= ventoy_request_handler
;
5316 callbacks
.open_file
= ventoy_web_openfile
;
5318 g_ventoy_http_ctx
= mg_start(&callbacks
, NULL
, options
);
5320 ventoy_start_writeback_thread(ventoy_http_writeback
);
5322 return g_ventoy_http_ctx
? 0 : 1;
5325 int ventoy_http_stop(void)
5327 if (g_ventoy_http_ctx
)
5329 mg_stop(g_ventoy_http_ctx
);
5332 ventoy_stop_writeback_thread();
5336 int ventoy_http_init(void)
5341 char *Buffer
= NULL
;
5344 ventoy_read_file_to_buf("www/helplist", 4, (void **)&Buffer
, &BufLen
);
5347 for (i
= 0; i
< BufLen
/ 5; i
++)
5349 memcpy(g_ventoy_help_lang
[i
], Buffer
+ i
* 5, 5);
5350 g_ventoy_help_lang
[i
][5] = 0;
5357 ventoy_read_file_to_buf("www/menulist", 4, (void **)&Buffer
, &BufLen
);
5360 for (i
= 0; i
< BufLen
/ 5; i
++)
5362 memcpy(g_ventoy_menu_lang
[i
], Buffer
+ i
* 5, 5);
5363 g_ventoy_menu_lang
[i
][5] = 0;
5369 file
= ventoy_tar_find_file("www/helplist");
5372 for (i
= 0; i
< file
->size
/ 5; i
++)
5374 memcpy(g_ventoy_help_lang
[i
], (char *)(file
->addr
) + i
* 5, 5);
5375 g_ventoy_help_lang
[i
][5] = 0;
5379 file
= ventoy_tar_find_file("www/menulist");
5382 for (i
= 0; i
< file
->size
/ 5; i
++)
5384 memcpy(g_ventoy_menu_lang
[i
], (char *)(file
->addr
) + i
* 5, 5);
5385 g_ventoy_menu_lang
[i
][5] = 0;
5390 if (!g_pub_json_buffer
)
5392 g_pub_json_buffer
= malloc(JSON_BUF_MAX
* 2);
5393 g_pub_save_buffer
= g_pub_json_buffer
+ JSON_BUF_MAX
;
5397 pthread_mutex_init(&g_api_mutex
, NULL
);
5401 void ventoy_http_exit(void)
5403 check_free(g_pub_json_buffer
);
5404 g_pub_json_buffer
= NULL
;
5405 g_pub_save_buffer
= NULL
;
5407 pthread_mutex_destroy(&g_api_mutex
);