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_menu_lang
[MAX_LANGUAGE
][8];
76 static char g_pub_path
[2 * MAX_PATH
];
77 static data_control g_data_control
[bios_max
+ 1];
78 static data_theme g_data_theme
[bios_max
+ 1];
79 static data_alias g_data_menu_alias
[bios_max
+ 1];
80 static data_tip g_data_menu_tip
[bios_max
+ 1];
81 static data_class g_data_menu_class
[bios_max
+ 1];
82 static data_image_list g_data_image_list
[bios_max
+ 1];
83 static data_image_list
*g_data_image_blacklist
= g_data_image_list
;
84 static data_auto_memdisk g_data_auto_memdisk
[bios_max
+ 1];
85 static data_password g_data_password
[bios_max
+ 1];
86 static data_conf_replace g_data_conf_replace
[bios_max
+ 1];
87 static data_injection g_data_injection
[bios_max
+ 1];
88 static data_auto_install g_data_auto_install
[bios_max
+ 1];
89 static data_persistence g_data_persistence
[bios_max
+ 1];
90 static data_dud g_data_dud
[bios_max
+ 1];
92 static char *g_pub_json_buffer
= NULL
;
93 static char *g_pub_save_buffer
= NULL
;
94 #define JSON_BUFFER g_pub_json_buffer
95 #define JSON_SAVE_BUFFER g_pub_save_buffer
97 static pthread_mutex_t g_api_mutex
;
98 static struct mg_context
*g_ventoy_http_ctx
= NULL
;
100 static int ventoy_is_kbd_valid(const char *key
)
104 for (i
= 0; g_ventoy_kbd_layout
[i
]; i
++)
106 if (strcmp(g_ventoy_kbd_layout
[i
], key
) == 0)
115 static const char * ventoy_real_path(const char *org
)
119 if (g_sysinfo
.pathcase
)
121 scnprintf(g_pub_path
, MAX_PATH
, "%s", org
);
122 count
= ventoy_path_case(g_pub_path
+ 1, 1);
136 static int ventoy_json_result(struct mg_connection
*conn
, const char *err
)
139 "HTTP/1.1 200 OK \r\n"
140 "Content-Type: application/json\r\n"
141 "Content-Length: %d\r\n"
143 (int)strlen(err
), err
);
148 static int ventoy_json_buffer(struct mg_connection
*conn
, const char *json_buf
, int json_len
)
151 "HTTP/1.1 200 OK \r\n"
152 "Content-Type: application/json\r\n"
153 "Content-Length: %d\r\n"
160 static void ventoy_free_path_node_list(path_node
*list
)
162 path_node
*next
= NULL
;
163 path_node
*node
= list
;
173 static path_node
* ventoy_path_node_add_array(VTOY_JSON
*array
)
175 path_node
*head
= NULL
;
176 path_node
*node
= NULL
;
177 path_node
*cur
= NULL
;
178 VTOY_JSON
*item
= NULL
;
180 for (item
= array
->pstChild
; item
; item
= item
->pstNext
)
182 node
= zalloc(sizeof(path_node
));
185 scnprintf(node
->path
, sizeof(node
->path
), "%s", item
->unData
.pcStrVal
);
186 vtoy_list_add(head
, cur
, node
);
193 static int ventoy_check_fuzzy_path(char *path
, int prefix
)
205 pos
= strchr(path
, '*');
208 for (cur
= pos
; *cur
; cur
++)
233 rc
= ventoy_is_directory_exist("%s%s", g_cur_dir
, path
);
237 rc
= ventoy_is_directory_exist("%s", path
);
258 return ventoy_is_file_exist("%s%s", g_cur_dir
, path
);
262 return ventoy_is_file_exist("%s", path
);
267 static int ventoy_path_list_cmp(path_node
*list1
, path_node
*list2
)
269 if (NULL
== list1
&& NULL
== list2
)
273 else if (list1
&& list2
)
275 while (list1
&& list2
)
277 if (strcmp(list1
->path
, list2
->path
))
286 if (list1
== NULL
&& list2
== NULL
)
299 static int ventoy_api_device_info(struct mg_connection
*conn
, VTOY_JSON
*json
)
305 VTOY_JSON_FMT_BEGIN(pos
, JSON_BUFFER
, JSON_BUF_MAX
);
306 VTOY_JSON_FMT_OBJ_BEGIN();
308 VTOY_JSON_FMT_STRN("dev_name", g_sysinfo
.cur_model
);
309 VTOY_JSON_FMT_STRN("dev_capacity", g_sysinfo
.cur_capacity
);
310 VTOY_JSON_FMT_STRN("dev_fs", g_sysinfo
.cur_fsname
);
311 VTOY_JSON_FMT_STRN("ventoy_ver", g_sysinfo
.cur_ventoy_ver
);
312 VTOY_JSON_FMT_SINT("part_style", g_sysinfo
.cur_part_style
);
313 VTOY_JSON_FMT_SINT("secure_boot", g_sysinfo
.cur_secureboot
);
315 VTOY_JSON_FMT_OBJ_END();
316 VTOY_JSON_FMT_END(pos
);
318 ventoy_json_buffer(conn
, JSON_BUFFER
, pos
);
322 static int ventoy_api_sysinfo(struct mg_connection
*conn
, VTOY_JSON
*json
)
328 VTOY_JSON_FMT_BEGIN(pos
, JSON_BUFFER
, JSON_BUF_MAX
);
329 VTOY_JSON_FMT_OBJ_BEGIN();
330 VTOY_JSON_FMT_STRN("language", ventoy_get_os_language());
331 VTOY_JSON_FMT_STRN("curdir", g_cur_dir
);
334 VTOY_JSON_FMT_SINT("syntax_error", g_sysinfo
.syntax_error
);
335 g_sysinfo
.syntax_error
= 0;
337 VTOY_JSON_FMT_SINT("invalid_config", g_sysinfo
.invalid_config
);
338 g_sysinfo
.invalid_config
= 0;
340 #if defined(_MSC_VER) || defined(WIN32)
341 VTOY_JSON_FMT_STRN("os", "windows");
343 VTOY_JSON_FMT_STRN("os", "linux");
346 VTOY_JSON_FMT_OBJ_END();
347 VTOY_JSON_FMT_END(pos
);
349 ventoy_json_buffer(conn
, JSON_BUFFER
, pos
);
353 static int ventoy_api_handshake(struct mg_connection
*conn
, VTOY_JSON
*json
)
362 VTOY_JSON_FMT_BEGIN(pos
, JSON_BUFFER
, JSON_BUF_MAX
);
363 VTOY_JSON_FMT_OBJ_BEGIN();
364 VTOY_JSON_FMT_SINT("status", 0);
365 VTOY_JSON_FMT_SINT("save_error", g_sysinfo
.config_save_error
);
366 g_sysinfo
.config_save_error
= 0;
368 for (i
= 0; i
< plugin_type_max
; i
++)
370 scnprintf(key
, sizeof(key
), "exist_%s", g_plugin_name
[i
]);
371 VTOY_JSON_FMT_KEY(key
);
372 VTOY_JSON_FMT_ARY_BEGIN();
373 for (j
= 0; j
< bios_max
; j
++)
375 VTOY_JSON_FMT_ITEM_INT(g_json_exist
[i
][j
]);
377 VTOY_JSON_FMT_ARY_ENDEX();
380 VTOY_JSON_FMT_OBJ_END();
381 VTOY_JSON_FMT_END(pos
);
383 ventoy_json_buffer(conn
, JSON_BUFFER
, pos
);
387 static int ventoy_api_check_exist(struct mg_connection
*conn
, VTOY_JSON
*json
)
392 const char *path
= NULL
;
394 path
= vtoy_json_get_string_ex(json
, "path");
395 vtoy_json_get_int(json
, "dir", &dir
);
401 exist
= ventoy_is_directory_exist("%s", path
);
405 exist
= ventoy_is_file_exist("%s", path
);
409 VTOY_JSON_FMT_BEGIN(pos
, JSON_BUFFER
, JSON_BUF_MAX
);
410 VTOY_JSON_FMT_OBJ_BEGIN();
411 VTOY_JSON_FMT_SINT("exist", exist
);
412 VTOY_JSON_FMT_OBJ_END();
413 VTOY_JSON_FMT_END(pos
);
415 ventoy_json_buffer(conn
, JSON_BUFFER
, pos
);
419 static int ventoy_api_check_exist2(struct mg_connection
*conn
, VTOY_JSON
*json
)
428 const char *path1
= NULL
;
429 const char *path2
= NULL
;
431 path1
= vtoy_json_get_string_ex(json
, "path1");
432 path2
= vtoy_json_get_string_ex(json
, "path2");
433 vtoy_json_get_int(json
, "dir1", &dir1
);
434 vtoy_json_get_int(json
, "dir2", &dir2
);
435 vtoy_json_get_int(json
, "fuzzy1", &fuzzy1
);
436 vtoy_json_get_int(json
, "fuzzy2", &fuzzy2
);
442 exist1
= ventoy_is_directory_exist("%s", path1
);
448 exist1
= ventoy_check_fuzzy_path((char *)path1
, 0);
452 exist1
= ventoy_is_file_exist("%s", path1
);
461 exist2
= ventoy_is_directory_exist("%s", path2
);
467 exist2
= ventoy_check_fuzzy_path((char *)path2
, 0);
471 exist2
= ventoy_is_file_exist("%s", path2
);
476 VTOY_JSON_FMT_BEGIN(pos
, JSON_BUFFER
, JSON_BUF_MAX
);
477 VTOY_JSON_FMT_OBJ_BEGIN();
478 VTOY_JSON_FMT_SINT("exist1", exist1
);
479 VTOY_JSON_FMT_SINT("exist2", exist2
);
480 VTOY_JSON_FMT_OBJ_END();
481 VTOY_JSON_FMT_END(pos
);
483 ventoy_json_buffer(conn
, JSON_BUFFER
, pos
);
487 static int ventoy_api_check_fuzzy(struct mg_connection
*conn
, VTOY_JSON
*json
)
491 const char *path
= NULL
;
493 path
= vtoy_json_get_string_ex(json
, "path");
496 exist
= ventoy_check_fuzzy_path((char *)path
, 0);
499 VTOY_JSON_FMT_BEGIN(pos
, JSON_BUFFER
, JSON_BUF_MAX
);
500 VTOY_JSON_FMT_OBJ_BEGIN();
501 VTOY_JSON_FMT_SINT("exist", exist
);
502 VTOY_JSON_FMT_OBJ_END();
503 VTOY_JSON_FMT_END(pos
);
505 ventoy_json_buffer(conn
, JSON_BUFFER
, pos
);
512 void ventoy_data_default_control(data_control
*data
)
514 memset(data
, 0, sizeof(data_control
));
516 data
->password_asterisk
= 1;
517 data
->secondary_menu
= 1;
518 data
->filter_dot_underscore
= 1;
519 data
->max_search_level
= -1;
520 data
->menu_timeout
= 0;
521 data
->secondary_menu_timeout
= 0;
523 strlcpy(data
->default_kbd_layout
, "QWERTY_USA");
524 strlcpy(data
->menu_language
, "en_US");
527 int ventoy_data_cmp_control(data_control
*data1
, data_control
*data2
)
529 if (data1
->default_menu_mode
!= data2
->default_menu_mode
||
530 data1
->treeview_style
!= data2
->treeview_style
||
531 data1
->filter_dot_underscore
!= data2
->filter_dot_underscore
||
532 data1
->sort_casesensitive
!= data2
->sort_casesensitive
||
533 data1
->max_search_level
!= data2
->max_search_level
||
534 data1
->vhd_no_warning
!= data2
->vhd_no_warning
||
535 data1
->filter_iso
!= data2
->filter_iso
||
536 data1
->filter_wim
!= data2
->filter_wim
||
537 data1
->filter_efi
!= data2
->filter_efi
||
538 data1
->filter_img
!= data2
->filter_img
||
539 data1
->filter_vhd
!= data2
->filter_vhd
||
540 data1
->filter_vtoy
!= data2
->filter_vtoy
||
541 data1
->win11_bypass_check
!= data2
->win11_bypass_check
||
542 data1
->linux_remount
!= data2
->linux_remount
||
543 data1
->password_asterisk
!= data2
->password_asterisk
||
544 data1
->secondary_menu
!= data2
->secondary_menu
||
545 data1
->menu_timeout
!= data2
->menu_timeout
||
546 data1
->secondary_menu_timeout
!= data2
->secondary_menu_timeout
)
551 if (strcmp(data1
->default_search_root
, data2
->default_search_root
) ||
552 strcmp(data1
->default_image
, data2
->default_image
) ||
553 strcmp(data1
->default_kbd_layout
, data2
->default_kbd_layout
) ||
554 strcmp(data1
->menu_language
, data2
->menu_language
))
562 int ventoy_data_save_control(data_control
*data
, const char *title
, char *buf
, int buflen
)
565 data_control
*def
= g_data_control
+ bios_max
;
567 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
569 VTOY_JSON_FMT_KEY_L(L1
, title
);
570 VTOY_JSON_FMT_ARY_BEGIN_N();
572 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_DEFAULT_MENU_MODE", default_menu_mode
);
573 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_TREE_VIEW_MENU_STYLE", treeview_style
);
574 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_FILT_DOT_UNDERSCORE_FILE", filter_dot_underscore
);
575 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_SORT_CASE_SENSITIVE", sort_casesensitive
);
577 if (data
->max_search_level
>= 0)
579 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_MAX_SEARCH_LEVEL", max_search_level
);
582 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_VHD_NO_WARNING", vhd_no_warning
);
583 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_FILE_FLT_ISO", filter_iso
);
584 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_FILE_FLT_WIM", filter_wim
);
585 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_FILE_FLT_EFI", filter_efi
);
586 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_FILE_FLT_IMG", filter_img
);
587 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_FILE_FLT_VHD", filter_vhd
);
588 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_FILE_FLT_VTOY", filter_vtoy
);
589 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_WIN11_BYPASS_CHECK", win11_bypass_check
);
590 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_LINUX_REMOUNT", linux_remount
);
591 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_SECONDARY_BOOT_MENU", secondary_menu
);
592 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_SHOW_PASSWORD_ASTERISK", password_asterisk
);
593 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_MENU_TIMEOUT", menu_timeout
);
594 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_SECONDARY_TIMEOUT", secondary_menu_timeout
);
596 VTOY_JSON_FMT_CTRL_STRN(L2
, "VTOY_DEFAULT_KBD_LAYOUT", default_kbd_layout
);
597 VTOY_JSON_FMT_CTRL_STRN(L2
, "VTOY_MENU_LANGUAGE", menu_language
);
599 if (strcmp(def
->default_search_root
, data
->default_search_root
))
601 VTOY_JSON_FMT_CTRL_STRN_STR(L2
, "VTOY_DEFAULT_SEARCH_ROOT", ventoy_real_path(data
->default_search_root
));
604 if (strcmp(def
->default_image
, data
->default_image
))
606 VTOY_JSON_FMT_CTRL_STRN_STR(L2
, "VTOY_DEFAULT_IMAGE", ventoy_real_path(data
->default_image
));
609 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
610 VTOY_JSON_FMT_END(pos
);
615 int ventoy_data_json_control(data_control
*ctrl
, char *buf
, int buflen
)
621 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
622 VTOY_JSON_FMT_OBJ_BEGIN();
624 VTOY_JSON_FMT_SINT("default_menu_mode", ctrl
->default_menu_mode
);
625 VTOY_JSON_FMT_SINT("treeview_style", ctrl
->treeview_style
);
626 VTOY_JSON_FMT_SINT("filter_dot_underscore", ctrl
->filter_dot_underscore
);
627 VTOY_JSON_FMT_SINT("sort_casesensitive", ctrl
->sort_casesensitive
);
628 VTOY_JSON_FMT_SINT("max_search_level", ctrl
->max_search_level
);
629 VTOY_JSON_FMT_SINT("vhd_no_warning", ctrl
->vhd_no_warning
);
631 VTOY_JSON_FMT_SINT("filter_iso", ctrl
->filter_iso
);
632 VTOY_JSON_FMT_SINT("filter_wim", ctrl
->filter_wim
);
633 VTOY_JSON_FMT_SINT("filter_efi", ctrl
->filter_efi
);
634 VTOY_JSON_FMT_SINT("filter_img", ctrl
->filter_img
);
635 VTOY_JSON_FMT_SINT("filter_vhd", ctrl
->filter_vhd
);
636 VTOY_JSON_FMT_SINT("filter_vtoy", ctrl
->filter_vtoy
);
637 VTOY_JSON_FMT_SINT("win11_bypass_check", ctrl
->win11_bypass_check
);
638 VTOY_JSON_FMT_SINT("linux_remount", ctrl
->linux_remount
);
639 VTOY_JSON_FMT_SINT("secondary_menu", ctrl
->secondary_menu
);
640 VTOY_JSON_FMT_SINT("password_asterisk", ctrl
->password_asterisk
);
641 VTOY_JSON_FMT_SINT("menu_timeout", ctrl
->menu_timeout
);
642 VTOY_JSON_FMT_SINT("secondary_menu_timeout", ctrl
->secondary_menu_timeout
);
643 VTOY_JSON_FMT_STRN("default_kbd_layout", ctrl
->default_kbd_layout
);
644 VTOY_JSON_FMT_STRN("menu_language", ctrl
->menu_language
);
647 if (ctrl
->default_search_root
[0] && ventoy_is_directory_exist("%s%s", g_cur_dir
, ctrl
->default_search_root
))
651 VTOY_JSON_FMT_STRN("default_search_root", ctrl
->default_search_root
);
652 VTOY_JSON_FMT_SINT("default_search_root_valid", valid
);
656 if (ctrl
->default_image
[0] && ventoy_is_file_exist("%s%s", g_cur_dir
, ctrl
->default_image
))
660 VTOY_JSON_FMT_STRN("default_image", ctrl
->default_image
);
661 VTOY_JSON_FMT_SINT("default_image_valid", valid
);
663 VTOY_JSON_FMT_KEY("menu_list");
664 VTOY_JSON_FMT_ARY_BEGIN();
666 for (i
= 0; g_ventoy_menu_lang
[i
][0]; i
++)
668 VTOY_JSON_FMT_ITEM(g_ventoy_menu_lang
[i
]);
670 VTOY_JSON_FMT_ARY_ENDEX();
672 VTOY_JSON_FMT_OBJ_END();
673 VTOY_JSON_FMT_END(pos
);
678 static int ventoy_api_get_control(struct mg_connection
*conn
, VTOY_JSON
*json
)
680 api_get_func(conn
, json
, control
);
684 static int ventoy_api_save_control(struct mg_connection
*conn
, VTOY_JSON
*json
)
688 data_control
*ctrl
= NULL
;
690 vtoy_json_get_int(json
, "index", &index
);
691 ctrl
= g_data_control
+ index
;
693 VTOY_JSON_INT("default_menu_mode", ctrl
->default_menu_mode
);
694 VTOY_JSON_INT("treeview_style", ctrl
->treeview_style
);
695 VTOY_JSON_INT("filter_dot_underscore", ctrl
->filter_dot_underscore
);
696 VTOY_JSON_INT("sort_casesensitive", ctrl
->sort_casesensitive
);
697 VTOY_JSON_INT("max_search_level", ctrl
->max_search_level
);
698 VTOY_JSON_INT("vhd_no_warning", ctrl
->vhd_no_warning
);
699 VTOY_JSON_INT("filter_iso", ctrl
->filter_iso
);
700 VTOY_JSON_INT("filter_wim", ctrl
->filter_wim
);
701 VTOY_JSON_INT("filter_efi", ctrl
->filter_efi
);
702 VTOY_JSON_INT("filter_img", ctrl
->filter_img
);
703 VTOY_JSON_INT("filter_vhd", ctrl
->filter_vhd
);
704 VTOY_JSON_INT("filter_vtoy", ctrl
->filter_vtoy
);
705 VTOY_JSON_INT("win11_bypass_check", ctrl
->win11_bypass_check
);
706 VTOY_JSON_INT("linux_remount", ctrl
->linux_remount
);
707 VTOY_JSON_INT("secondary_menu", ctrl
->secondary_menu
);
708 VTOY_JSON_INT("password_asterisk", ctrl
->password_asterisk
);
709 VTOY_JSON_INT("menu_timeout", ctrl
->menu_timeout
);
710 VTOY_JSON_INT("secondary_menu_timeout", ctrl
->secondary_menu_timeout
);
712 VTOY_JSON_STR("default_image", ctrl
->default_image
);
713 VTOY_JSON_STR("default_search_root", ctrl
->default_search_root
);
714 VTOY_JSON_STR("menu_language", ctrl
->menu_language
);
715 VTOY_JSON_STR("default_kbd_layout", ctrl
->default_kbd_layout
);
717 ret
= ventoy_data_save_all();
719 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
727 void ventoy_data_default_theme(data_theme
*data
)
729 memset(data
, 0, sizeof(data_theme
));
730 strlcpy(data
->gfxmode
, "1024x768");
731 scnprintf(data
->ventoy_left
, sizeof(data
->ventoy_left
), "5%%");
732 scnprintf(data
->ventoy_top
, sizeof(data
->ventoy_top
), "95%%");
733 scnprintf(data
->ventoy_color
, sizeof(data
->ventoy_color
), "%s", "#0000ff");
736 int ventoy_data_cmp_theme(data_theme
*data1
, data_theme
*data2
)
738 if (data1
->display_mode
!= data2
->display_mode
||
739 strcmp(data1
->ventoy_left
, data2
->ventoy_left
) ||
740 strcmp(data1
->ventoy_top
, data2
->ventoy_top
) ||
741 strcmp(data1
->gfxmode
, data2
->gfxmode
) ||
742 strcmp(data1
->ventoy_color
, data2
->ventoy_color
)
748 if (ventoy_path_list_cmp(data1
->filelist
, data2
->filelist
))
753 if (ventoy_path_list_cmp(data1
->fontslist
, data2
->fontslist
))
762 int ventoy_data_save_theme(data_theme
*data
, const char *title
, char *buf
, int buflen
)
765 path_node
*node
= NULL
;
766 data_theme
*def
= g_data_theme
+ bios_max
;
768 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
770 VTOY_JSON_FMT_KEY_L(L1
, title
);
771 VTOY_JSON_FMT_OBJ_BEGIN_N();
775 if (data
->filelist
->next
)
777 VTOY_JSON_FMT_KEY_L(L2
, "file");
778 VTOY_JSON_FMT_ARY_BEGIN_N();
780 for (node
= data
->filelist
; node
; node
= node
->next
)
782 VTOY_JSON_FMT_ITEM_PATH_LN(L3
, node
->path
);
785 VTOY_JSON_FMT_ARY_ENDEX_LN(L2
);
787 if (def
->default_file
!= data
->default_file
)
789 VTOY_JSON_FMT_SINT_LN(L2
, "default_file", data
->default_file
);
794 VTOY_JSON_FMT_STRN_PATH_LN(L2
, "file", data
->filelist
->path
);
798 if (data
->display_mode
!= def
->display_mode
)
800 if (display_mode_cli
== data
->display_mode
)
802 VTOY_JSON_FMT_STRN_LN(L2
, "display_mode", "CLI");
804 else if (display_mode_serial
== data
->display_mode
)
806 VTOY_JSON_FMT_STRN_LN(L2
, "display_mode", "serial");
808 else if (display_mode_ser_console
== data
->display_mode
)
810 VTOY_JSON_FMT_STRN_LN(L2
, "display_mode", "serial_console");
814 VTOY_JSON_FMT_STRN_LN(L2
, "display_mode", "GUI");
818 VTOY_JSON_FMT_DIFF_STRN(L2
, "gfxmode", gfxmode
);
820 VTOY_JSON_FMT_DIFF_STRN(L2
, "ventoy_left", ventoy_left
);
821 VTOY_JSON_FMT_DIFF_STRN(L2
, "ventoy_top", ventoy_top
);
822 VTOY_JSON_FMT_DIFF_STRN(L2
, "ventoy_color", ventoy_color
);
826 VTOY_JSON_FMT_KEY_L(L2
, "fonts");
827 VTOY_JSON_FMT_ARY_BEGIN_N();
829 for (node
= data
->fontslist
; node
; node
= node
->next
)
831 VTOY_JSON_FMT_ITEM_PATH_LN(L3
, node
->path
);
834 VTOY_JSON_FMT_ARY_ENDEX_LN(L2
);
837 VTOY_JSON_FMT_OBJ_ENDEX_LN(L1
);
838 VTOY_JSON_FMT_END(pos
);
844 int ventoy_data_json_theme(data_theme
*data
, char *buf
, int buflen
)
847 path_node
*node
= NULL
;
849 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
850 VTOY_JSON_FMT_OBJ_BEGIN();
852 VTOY_JSON_FMT_SINT("default_file", data
->default_file
);
853 VTOY_JSON_FMT_SINT("display_mode", data
->display_mode
);
854 VTOY_JSON_FMT_STRN("gfxmode", data
->gfxmode
);
856 VTOY_JSON_FMT_STRN("ventoy_color", data
->ventoy_color
);
857 VTOY_JSON_FMT_STRN("ventoy_left", data
->ventoy_left
);
858 VTOY_JSON_FMT_STRN("ventoy_top", data
->ventoy_top
);
860 VTOY_JSON_FMT_KEY("filelist");
861 VTOY_JSON_FMT_ARY_BEGIN();
862 for (node
= data
->filelist
; node
; node
= node
->next
)
864 VTOY_JSON_FMT_OBJ_BEGIN();
865 VTOY_JSON_FMT_STRN("path", node
->path
);
866 VTOY_JSON_FMT_SINT("valid", ventoy_is_file_exist("%s%s", g_cur_dir
, node
->path
));
867 VTOY_JSON_FMT_OBJ_ENDEX();
869 VTOY_JSON_FMT_ARY_ENDEX();
871 VTOY_JSON_FMT_KEY("fontslist");
872 VTOY_JSON_FMT_ARY_BEGIN();
873 for (node
= data
->fontslist
; node
; node
= node
->next
)
875 VTOY_JSON_FMT_OBJ_BEGIN();
876 VTOY_JSON_FMT_STRN("path", node
->path
);
877 VTOY_JSON_FMT_SINT("valid", ventoy_is_file_exist("%s%s", g_cur_dir
, node
->path
));
878 VTOY_JSON_FMT_OBJ_ENDEX();
880 VTOY_JSON_FMT_ARY_ENDEX();
882 VTOY_JSON_FMT_OBJ_END();
883 VTOY_JSON_FMT_END(pos
);
888 static int ventoy_api_get_theme(struct mg_connection
*conn
, VTOY_JSON
*json
)
890 api_get_func(conn
, json
, theme
);
894 static int ventoy_api_save_theme(struct mg_connection
*conn
, VTOY_JSON
*json
)
898 data_theme
*data
= NULL
;
900 vtoy_json_get_int(json
, "index", &index
);
901 data
= g_data_theme
+ index
;
903 VTOY_JSON_INT("default_file", data
->default_file
);
904 VTOY_JSON_INT("display_mode", data
->display_mode
);
905 VTOY_JSON_STR("gfxmode", data
->gfxmode
);
906 VTOY_JSON_STR("ventoy_left", data
->ventoy_left
);
907 VTOY_JSON_STR("ventoy_top", data
->ventoy_top
);
908 VTOY_JSON_STR("ventoy_color", data
->ventoy_color
);
910 ret
= ventoy_data_save_all();
912 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
916 static int ventoy_api_theme_add_file(struct mg_connection
*conn
, VTOY_JSON
*json
)
920 const char *path
= NULL
;
921 path_node
*node
= NULL
;
922 path_node
*cur
= NULL
;
923 data_theme
*data
= NULL
;
925 vtoy_json_get_int(json
, "index", &index
);
926 data
= g_data_theme
+ index
;
928 path
= VTOY_JSON_STR_EX("path");
931 node
= zalloc(sizeof(path_node
));
934 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
936 vtoy_list_add(data
->filelist
, cur
, node
);
940 ret
= ventoy_data_save_all();
942 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
946 static int ventoy_api_theme_del_file(struct mg_connection
*conn
, VTOY_JSON
*json
)
950 const char *path
= NULL
;
951 path_node
*node
= NULL
;
952 path_node
*last
= NULL
;
953 data_theme
*data
= NULL
;
955 vtoy_json_get_int(json
, "index", &index
);
956 data
= g_data_theme
+ index
;
958 path
= VTOY_JSON_STR_EX("path");
961 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
963 vtoy_list_free(path_node
, data
->filelist
);
967 vtoy_list_del(last
, node
, data
->filelist
, path
);
971 ret
= ventoy_data_save_all();
973 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
978 static int ventoy_api_theme_add_font(struct mg_connection
*conn
, VTOY_JSON
*json
)
982 const char *path
= NULL
;
983 path_node
*node
= NULL
;
984 path_node
*cur
= NULL
;
985 data_theme
*data
= NULL
;
987 vtoy_json_get_int(json
, "index", &index
);
988 data
= g_data_theme
+ index
;
990 path
= VTOY_JSON_STR_EX("path");
993 node
= zalloc(sizeof(path_node
));
996 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
997 vtoy_list_add(data
->fontslist
, cur
, node
);
1001 ret
= ventoy_data_save_all();
1003 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1008 static int ventoy_api_theme_del_font(struct mg_connection
*conn
, VTOY_JSON
*json
)
1012 const char *path
= NULL
;
1013 path_node
*node
= NULL
;
1014 path_node
*last
= NULL
;
1015 data_theme
*data
= NULL
;
1017 vtoy_json_get_int(json
, "index", &index
);
1018 data
= g_data_theme
+ index
;
1020 path
= VTOY_JSON_STR_EX("path");
1023 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
1025 vtoy_list_free(path_node
, data
->fontslist
);
1029 vtoy_list_del(last
, node
, data
->fontslist
, path
);
1033 ret
= ventoy_data_save_all();
1035 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1042 void ventoy_data_default_menu_alias(data_alias
*data
)
1044 memset(data
, 0, sizeof(data_alias
));
1047 int ventoy_data_cmp_menu_alias(data_alias
*data1
, data_alias
*data2
)
1049 data_alias_node
*list1
= NULL
;
1050 data_alias_node
*list2
= NULL
;
1052 if (NULL
== data1
->list
&& NULL
== data2
->list
)
1056 else if (data1
->list
&& data2
->list
)
1058 list1
= data1
->list
;
1059 list2
= data2
->list
;
1061 while (list1
&& list2
)
1063 if ((list1
->type
!= list2
->type
) ||
1064 strcmp(list1
->path
, list2
->path
) ||
1065 strcmp(list1
->alias
, list2
->alias
))
1070 list1
= list1
->next
;
1071 list2
= list2
->next
;
1074 if (list1
== NULL
&& list2
== NULL
)
1089 int ventoy_data_save_menu_alias(data_alias
*data
, const char *title
, char *buf
, int buflen
)
1092 data_alias_node
*node
= NULL
;
1094 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1096 VTOY_JSON_FMT_KEY_L(L1
, title
);
1097 VTOY_JSON_FMT_ARY_BEGIN_N();
1099 for (node
= data
->list
; node
; node
= node
->next
)
1101 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
1103 if (node
->type
== path_type_file
)
1105 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "image", node
->path
);
1109 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "dir", node
->path
);
1112 VTOY_JSON_FMT_STRN_EX_LN(L3
, "alias", node
->alias
);
1114 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
1117 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
1118 VTOY_JSON_FMT_END(pos
);
1124 int ventoy_data_json_menu_alias(data_alias
*data
, char *buf
, int buflen
)
1128 data_alias_node
*node
= NULL
;
1130 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1131 VTOY_JSON_FMT_ARY_BEGIN();
1133 for (node
= data
->list
; node
; node
= node
->next
)
1135 VTOY_JSON_FMT_OBJ_BEGIN();
1137 VTOY_JSON_FMT_UINT("type", node
->type
);
1138 VTOY_JSON_FMT_STRN("path", node
->path
);
1139 if (node
->type
== path_type_file
)
1141 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
1145 valid
= ventoy_is_directory_exist("%s%s", g_cur_dir
, node
->path
);
1148 VTOY_JSON_FMT_SINT("valid", valid
);
1149 VTOY_JSON_FMT_STRN("alias", node
->alias
);
1151 VTOY_JSON_FMT_OBJ_ENDEX();
1154 VTOY_JSON_FMT_ARY_END();
1155 VTOY_JSON_FMT_END(pos
);
1160 static int ventoy_api_get_alias(struct mg_connection
*conn
, VTOY_JSON
*json
)
1162 api_get_func(conn
, json
, menu_alias
);
1166 static int ventoy_api_save_alias(struct mg_connection
*conn
, VTOY_JSON
*json
)
1169 ret
= ventoy_data_save_all();
1171 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1175 static int ventoy_api_alias_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
1179 int type
= path_type_file
;
1180 const char *path
= NULL
;
1181 const char *alias
= NULL
;
1182 data_alias_node
*node
= NULL
;
1183 data_alias_node
*cur
= NULL
;
1184 data_alias
*data
= NULL
;
1186 vtoy_json_get_int(json
, "index", &index
);
1187 data
= g_data_menu_alias
+ index
;
1189 vtoy_json_get_int(json
, "type", &type
);
1191 path
= VTOY_JSON_STR_EX("path");
1192 alias
= VTOY_JSON_STR_EX("alias");
1195 node
= zalloc(sizeof(data_alias_node
));
1199 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
1200 scnprintf(node
->alias
, sizeof(node
->alias
), "%s", alias
);
1202 vtoy_list_add(data
->list
, cur
, node
);
1206 ret
= ventoy_data_save_all();
1208 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1212 static int ventoy_api_alias_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
1216 const char *path
= NULL
;
1217 data_alias_node
*last
= NULL
;
1218 data_alias_node
*node
= NULL
;
1219 data_alias
*data
= NULL
;
1221 vtoy_json_get_int(json
, "index", &index
);
1222 data
= g_data_menu_alias
+ index
;
1224 path
= VTOY_JSON_STR_EX("path");
1227 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
1229 vtoy_list_free(data_alias_node
, data
->list
);
1233 vtoy_list_del(last
, node
, data
->list
, path
);
1237 ret
= ventoy_data_save_all();
1239 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1246 void ventoy_data_default_menu_tip(data_tip
*data
)
1248 memset(data
, 0, sizeof(data_tip
));
1250 scnprintf(data
->left
, sizeof(data
->left
), "10%%");
1251 scnprintf(data
->top
, sizeof(data
->top
), "81%%");
1252 scnprintf(data
->color
, sizeof(data
->color
), "%s", "blue");
1255 int ventoy_data_cmp_menu_tip(data_tip
*data1
, data_tip
*data2
)
1257 data_tip_node
*list1
= NULL
;
1258 data_tip_node
*list2
= NULL
;
1260 if (strcmp(data1
->left
, data2
->left
) || strcmp(data1
->top
, data2
->top
) || strcmp(data1
->color
, data2
->color
))
1265 if (NULL
== data1
->list
&& NULL
== data2
->list
)
1269 else if (data1
->list
&& data2
->list
)
1271 list1
= data1
->list
;
1272 list2
= data2
->list
;
1274 while (list1
&& list2
)
1276 if ((list1
->type
!= list2
->type
) ||
1277 strcmp(list1
->path
, list2
->path
) ||
1278 strcmp(list1
->tip
, list2
->tip
))
1283 list1
= list1
->next
;
1284 list2
= list2
->next
;
1287 if (list1
== NULL
&& list2
== NULL
)
1302 int ventoy_data_save_menu_tip(data_tip
*data
, const char *title
, char *buf
, int buflen
)
1305 data_tip_node
*node
= NULL
;
1306 data_tip
*def
= g_data_menu_tip
+ bios_max
;
1308 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1309 VTOY_JSON_FMT_KEY_L(L1
, title
);
1310 VTOY_JSON_FMT_OBJ_BEGIN_N();
1312 VTOY_JSON_FMT_DIFF_STRN(L2
, "left", left
);
1313 VTOY_JSON_FMT_DIFF_STRN(L2
, "top", top
);
1314 VTOY_JSON_FMT_DIFF_STRN(L2
, "color", color
);
1318 VTOY_JSON_FMT_KEY_L(L2
, "tips");
1319 VTOY_JSON_FMT_ARY_BEGIN_N();
1321 for (node
= data
->list
; node
; node
= node
->next
)
1323 VTOY_JSON_FMT_OBJ_BEGIN_LN(L3
);
1325 if (node
->type
== path_type_file
)
1327 VTOY_JSON_FMT_STRN_PATH_LN(L4
, "image", node
->path
);
1331 VTOY_JSON_FMT_STRN_PATH_LN(L4
, "dir", node
->path
);
1333 VTOY_JSON_FMT_STRN_EX_LN(L4
, "tip", node
->tip
);
1335 VTOY_JSON_FMT_OBJ_ENDEX_LN(L3
);
1338 VTOY_JSON_FMT_ARY_ENDEX_LN(L2
);
1341 VTOY_JSON_FMT_OBJ_ENDEX_LN(L1
);
1343 VTOY_JSON_FMT_END(pos
);
1349 int ventoy_data_json_menu_tip(data_tip
*data
, char *buf
, int buflen
)
1353 data_tip_node
*node
= NULL
;
1355 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1357 VTOY_JSON_FMT_OBJ_BEGIN();
1359 VTOY_JSON_FMT_STRN("left", data
->left
);
1360 VTOY_JSON_FMT_STRN("top", data
->top
);
1361 VTOY_JSON_FMT_STRN("color", data
->color
);
1363 VTOY_JSON_FMT_KEY("tips");
1364 VTOY_JSON_FMT_ARY_BEGIN();
1366 for (node
= data
->list
; node
; node
= node
->next
)
1368 VTOY_JSON_FMT_OBJ_BEGIN();
1370 VTOY_JSON_FMT_UINT("type", node
->type
);
1371 VTOY_JSON_FMT_STRN("path", node
->path
);
1372 if (node
->type
== path_type_file
)
1374 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
1378 valid
= ventoy_is_directory_exist("%s%s", g_cur_dir
, node
->path
);
1381 VTOY_JSON_FMT_SINT("valid", valid
);
1382 VTOY_JSON_FMT_STRN("tip", node
->tip
);
1384 VTOY_JSON_FMT_OBJ_ENDEX();
1387 VTOY_JSON_FMT_ARY_ENDEX();
1389 VTOY_JSON_FMT_OBJ_END();
1390 VTOY_JSON_FMT_END(pos
);
1395 static int ventoy_api_get_tip(struct mg_connection
*conn
, VTOY_JSON
*json
)
1397 api_get_func(conn
, json
, menu_tip
);
1401 static int ventoy_api_save_tip(struct mg_connection
*conn
, VTOY_JSON
*json
)
1405 data_tip
*data
= NULL
;
1407 vtoy_json_get_int(json
, "index", &index
);
1408 data
= g_data_menu_tip
+ index
;
1410 VTOY_JSON_STR("left", data
->left
);
1411 VTOY_JSON_STR("top", data
->top
);
1412 VTOY_JSON_STR("color", data
->color
);
1414 ret
= ventoy_data_save_all();
1416 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1420 static int ventoy_api_tip_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
1424 int type
= path_type_file
;
1425 const char *path
= NULL
;
1426 const char *tip
= NULL
;
1427 data_tip_node
*node
= NULL
;
1428 data_tip_node
*cur
= NULL
;
1429 data_tip
*data
= NULL
;
1431 vtoy_json_get_int(json
, "index", &index
);
1432 data
= g_data_menu_tip
+ index
;
1434 vtoy_json_get_int(json
, "type", &type
);
1436 path
= VTOY_JSON_STR_EX("path");
1437 tip
= VTOY_JSON_STR_EX("tip");
1440 node
= zalloc(sizeof(data_tip_node
));
1444 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
1445 scnprintf(node
->tip
, sizeof(node
->tip
), "%s", tip
);
1447 vtoy_list_add(data
->list
, cur
, node
);
1451 ret
= ventoy_data_save_all();
1453 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1457 static int ventoy_api_tip_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
1461 const char *path
= NULL
;
1462 data_tip_node
*last
= NULL
;
1463 data_tip_node
*node
= NULL
;
1464 data_tip
*data
= NULL
;
1466 vtoy_json_get_int(json
, "index", &index
);
1467 data
= g_data_menu_tip
+ index
;
1469 path
= VTOY_JSON_STR_EX("path");
1472 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
1474 vtoy_list_free(data_tip_node
, data
->list
);
1478 vtoy_list_del(last
, node
, data
->list
, path
);
1482 ret
= ventoy_data_save_all();
1484 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1491 void ventoy_data_default_menu_class(data_class
*data
)
1493 memset(data
, 0, sizeof(data_class
));
1496 int ventoy_data_cmp_menu_class(data_class
*data1
, data_class
*data2
)
1498 data_class_node
*list1
= NULL
;
1499 data_class_node
*list2
= NULL
;
1501 if (NULL
== data1
->list
&& NULL
== data2
->list
)
1505 else if (data1
->list
&& data2
->list
)
1507 list1
= data1
->list
;
1508 list2
= data2
->list
;
1510 while (list1
&& list2
)
1512 if ((list1
->type
!= list2
->type
) ||
1513 strcmp(list1
->path
, list2
->path
) ||
1514 strcmp(list1
->class, list2
->class))
1519 list1
= list1
->next
;
1520 list2
= list2
->next
;
1523 if (list1
== NULL
&& list2
== NULL
)
1538 int ventoy_data_save_menu_class(data_class
*data
, const char *title
, char *buf
, int buflen
)
1541 data_class_node
*node
= NULL
;
1543 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1545 VTOY_JSON_FMT_KEY_L(L1
, title
);
1546 VTOY_JSON_FMT_ARY_BEGIN_N();
1548 for (node
= data
->list
; node
; node
= node
->next
)
1550 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
1552 if (node
->type
== class_type_key
)
1554 VTOY_JSON_FMT_STRN_LN(L3
, "key", node
->path
);
1556 else if (node
->type
== class_type_dir
)
1558 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "dir", node
->path
);
1562 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "parent", node
->path
);
1564 VTOY_JSON_FMT_STRN_LN(L3
, "class", node
->class);
1566 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
1569 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
1570 VTOY_JSON_FMT_END(pos
);
1576 int ventoy_data_json_menu_class(data_class
*data
, char *buf
, int buflen
)
1580 data_class_node
*node
= NULL
;
1582 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1583 VTOY_JSON_FMT_ARY_BEGIN();
1585 for (node
= data
->list
; node
; node
= node
->next
)
1587 VTOY_JSON_FMT_OBJ_BEGIN();
1589 VTOY_JSON_FMT_UINT("type", node
->type
);
1590 VTOY_JSON_FMT_STRN("path", node
->path
);
1592 if (node
->type
== class_type_key
)
1598 valid
= ventoy_is_directory_exist("%s%s", g_cur_dir
, node
->path
);
1600 VTOY_JSON_FMT_SINT("valid", valid
);
1602 VTOY_JSON_FMT_STRN("class", node
->class);
1604 VTOY_JSON_FMT_OBJ_ENDEX();
1607 VTOY_JSON_FMT_ARY_END();
1608 VTOY_JSON_FMT_END(pos
);
1614 static int ventoy_api_get_class(struct mg_connection
*conn
, VTOY_JSON
*json
)
1616 api_get_func(conn
, json
, menu_class
);
1620 static int ventoy_api_save_class(struct mg_connection
*conn
, VTOY_JSON
*json
)
1623 ret
= ventoy_data_save_all();
1625 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1629 static int ventoy_api_class_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
1633 int type
= class_type_key
;
1634 const char *path
= NULL
;
1635 const char *class = NULL
;
1636 data_class_node
*node
= NULL
;
1637 data_class_node
*cur
= NULL
;
1638 data_class
*data
= NULL
;
1640 vtoy_json_get_int(json
, "index", &index
);
1641 data
= g_data_menu_class
+ index
;
1643 vtoy_json_get_int(json
, "type", &type
);
1645 path
= VTOY_JSON_STR_EX("path");
1646 class = VTOY_JSON_STR_EX("class");
1649 node
= zalloc(sizeof(data_class_node
));
1654 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
1655 scnprintf(node
->class, sizeof(node
->class), "%s", class);
1657 vtoy_list_add(data
->list
, cur
, node
);
1661 ret
= ventoy_data_save_all();
1663 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1667 static int ventoy_api_class_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
1671 const char *path
= NULL
;
1672 data_class_node
*last
= NULL
;
1673 data_class_node
*node
= NULL
;
1674 data_class
*data
= NULL
;
1676 vtoy_json_get_int(json
, "index", &index
);
1677 data
= g_data_menu_class
+ index
;
1679 path
= VTOY_JSON_STR_EX("path");
1682 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
1684 vtoy_list_free(data_class_node
, data
->list
);
1688 vtoy_list_del(last
, node
, data
->list
, path
);
1692 ret
= ventoy_data_save_all();
1694 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1701 void ventoy_data_default_auto_memdisk(data_auto_memdisk
*data
)
1703 memset(data
, 0, sizeof(data_auto_memdisk
));
1706 int ventoy_data_cmp_auto_memdisk(data_auto_memdisk
*data1
, data_auto_memdisk
*data2
)
1708 return ventoy_path_list_cmp(data1
->list
, data2
->list
);
1711 int ventoy_data_save_auto_memdisk(data_auto_memdisk
*data
, const char *title
, char *buf
, int buflen
)
1714 path_node
*node
= NULL
;
1716 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1718 VTOY_JSON_FMT_KEY_L(L1
, title
);
1719 VTOY_JSON_FMT_ARY_BEGIN_N();
1721 for (node
= data
->list
; node
; node
= node
->next
)
1723 VTOY_JSON_FMT_ITEM_PATH_LN(L2
, node
->path
);
1726 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
1727 VTOY_JSON_FMT_END(pos
);
1732 int ventoy_data_json_auto_memdisk(data_auto_memdisk
*data
, char *buf
, int buflen
)
1736 path_node
*node
= NULL
;
1738 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1739 VTOY_JSON_FMT_ARY_BEGIN();
1741 for (node
= data
->list
; node
; node
= node
->next
)
1743 VTOY_JSON_FMT_OBJ_BEGIN();
1745 VTOY_JSON_FMT_STRN("path", node
->path
);
1746 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
1747 VTOY_JSON_FMT_SINT("valid", valid
);
1749 VTOY_JSON_FMT_OBJ_ENDEX();
1752 VTOY_JSON_FMT_ARY_END();
1753 VTOY_JSON_FMT_END(pos
);
1758 static int ventoy_api_get_auto_memdisk(struct mg_connection
*conn
, VTOY_JSON
*json
)
1760 api_get_func(conn
, json
, auto_memdisk
);
1764 static int ventoy_api_save_auto_memdisk(struct mg_connection
*conn
, VTOY_JSON
*json
)
1768 ret
= ventoy_data_save_all();
1770 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1774 static int ventoy_api_auto_memdisk_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
1778 const char *path
= NULL
;
1779 path_node
*node
= NULL
;
1780 path_node
*cur
= NULL
;
1781 data_auto_memdisk
*data
= NULL
;
1783 vtoy_json_get_int(json
, "index", &index
);
1784 data
= g_data_auto_memdisk
+ index
;
1786 path
= VTOY_JSON_STR_EX("path");
1789 node
= zalloc(sizeof(path_node
));
1792 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
1793 vtoy_list_add(data
->list
, cur
, node
);
1797 ret
= ventoy_data_save_all();
1799 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1803 static int ventoy_api_auto_memdisk_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
1807 const char *path
= NULL
;
1808 path_node
*last
= NULL
;
1809 path_node
*node
= NULL
;
1810 data_auto_memdisk
*data
= NULL
;
1812 vtoy_json_get_int(json
, "index", &index
);
1813 data
= g_data_auto_memdisk
+ index
;
1815 path
= VTOY_JSON_STR_EX("path");
1818 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
1820 vtoy_list_free(path_node
, data
->list
);
1824 vtoy_list_del(last
, node
, data
->list
, path
);
1828 ret
= ventoy_data_save_all();
1830 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1837 void ventoy_data_default_image_list(data_image_list
*data
)
1839 memset(data
, 0, sizeof(data_image_list
));
1842 int ventoy_data_cmp_image_list(data_image_list
*data1
, data_image_list
*data2
)
1844 if (data1
->type
!= data2
->type
)
1846 if (data1
->list
|| data2
->list
)
1856 return ventoy_path_list_cmp(data1
->list
, data2
->list
);
1859 int ventoy_data_save_image_list(data_image_list
*data
, const char *title
, char *buf
, int buflen
)
1863 path_node
*node
= NULL
;
1873 prelen
= (int)strlen("image_list");
1875 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1877 if (data
->type
== 0)
1879 scnprintf(newtitle
, sizeof(newtitle
), "image_list%s", title
+ prelen
);
1883 scnprintf(newtitle
, sizeof(newtitle
), "image_blacklist%s", title
+ prelen
);
1885 VTOY_JSON_FMT_KEY_L(L1
, newtitle
);
1887 VTOY_JSON_FMT_ARY_BEGIN_N();
1889 for (node
= data
->list
; node
; node
= node
->next
)
1891 VTOY_JSON_FMT_ITEM_PATH_LN(L2
, node
->path
);
1894 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
1895 VTOY_JSON_FMT_END(pos
);
1900 int ventoy_data_json_image_list(data_image_list
*data
, char *buf
, int buflen
)
1904 path_node
*node
= NULL
;
1906 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1908 VTOY_JSON_FMT_OBJ_BEGIN();
1909 VTOY_JSON_FMT_SINT("type", data
->type
);
1911 VTOY_JSON_FMT_KEY("list");
1912 VTOY_JSON_FMT_ARY_BEGIN();
1914 for (node
= data
->list
; node
; node
= node
->next
)
1916 VTOY_JSON_FMT_OBJ_BEGIN();
1918 VTOY_JSON_FMT_STRN("path", node
->path
);
1919 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
1920 VTOY_JSON_FMT_SINT("valid", valid
);
1922 VTOY_JSON_FMT_OBJ_ENDEX();
1925 VTOY_JSON_FMT_ARY_ENDEX();
1926 VTOY_JSON_FMT_OBJ_END();
1928 VTOY_JSON_FMT_END(pos
);
1933 static int ventoy_api_get_image_list(struct mg_connection
*conn
, VTOY_JSON
*json
)
1935 api_get_func(conn
, json
, image_list
);
1939 static int ventoy_api_save_image_list(struct mg_connection
*conn
, VTOY_JSON
*json
)
1943 data_image_list
*data
= NULL
;
1945 vtoy_json_get_int(json
, "index", &index
);
1946 data
= g_data_image_list
+ index
;
1948 VTOY_JSON_INT("type", data
->type
);
1950 ret
= ventoy_data_save_all();
1952 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1956 static int ventoy_api_image_list_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
1960 const char *path
= NULL
;
1961 path_node
*node
= NULL
;
1962 path_node
*cur
= NULL
;
1963 data_image_list
*data
= NULL
;
1965 vtoy_json_get_int(json
, "index", &index
);
1966 data
= g_data_image_list
+ index
;
1968 path
= VTOY_JSON_STR_EX("path");
1971 node
= zalloc(sizeof(path_node
));
1974 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
1975 vtoy_list_add(data
->list
, cur
, node
);
1979 ret
= ventoy_data_save_all();
1981 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1985 static int ventoy_api_image_list_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
1989 const char *path
= NULL
;
1990 path_node
*last
= NULL
;
1991 path_node
*node
= NULL
;
1992 data_image_list
*data
= NULL
;
1994 vtoy_json_get_int(json
, "index", &index
);
1995 data
= g_data_image_list
+ index
;
1997 path
= VTOY_JSON_STR_EX("path");
2000 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
2002 vtoy_list_free(path_node
, data
->list
);
2006 vtoy_list_del(last
, node
, data
->list
, path
);
2010 ret
= ventoy_data_save_all();
2012 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2019 void ventoy_data_default_password(data_password
*data
)
2021 memset(data
, 0, sizeof(data_password
));
2024 int ventoy_data_cmp_password(data_password
*data1
, data_password
*data2
)
2026 menu_password
*list1
= NULL
;
2027 menu_password
*list2
= NULL
;
2029 if (strcmp(data1
->bootpwd
, data2
->bootpwd
) ||
2030 strcmp(data1
->isopwd
, data2
->isopwd
) ||
2031 strcmp(data1
->wimpwd
, data2
->wimpwd
) ||
2032 strcmp(data1
->vhdpwd
, data2
->vhdpwd
) ||
2033 strcmp(data1
->imgpwd
, data2
->imgpwd
) ||
2034 strcmp(data1
->efipwd
, data2
->efipwd
) ||
2035 strcmp(data1
->vtoypwd
, data2
->vtoypwd
)
2041 if (NULL
== data1
->list
&& NULL
== data2
->list
)
2045 else if (data1
->list
&& data2
->list
)
2047 list1
= data1
->list
;
2048 list2
= data2
->list
;
2050 while (list1
&& list2
)
2052 if ((list1
->type
!= list2
->type
) || strcmp(list1
->path
, list2
->path
))
2057 list1
= list1
->next
;
2058 list2
= list2
->next
;
2061 if (list1
== NULL
&& list2
== NULL
)
2076 int ventoy_data_save_password(data_password
*data
, const char *title
, char *buf
, int buflen
)
2079 menu_password
*node
= NULL
;
2080 data_password
*def
= g_data_password
+ bios_max
;
2082 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2083 VTOY_JSON_FMT_KEY_L(L1
, title
);
2084 VTOY_JSON_FMT_OBJ_BEGIN_N();
2086 VTOY_JSON_FMT_DIFF_STRN(L2
, "bootpwd", bootpwd
);
2087 VTOY_JSON_FMT_DIFF_STRN(L2
, "isopwd", isopwd
);
2088 VTOY_JSON_FMT_DIFF_STRN(L2
, "wimpwd", wimpwd
);
2089 VTOY_JSON_FMT_DIFF_STRN(L2
, "vhdpwd", vhdpwd
);
2090 VTOY_JSON_FMT_DIFF_STRN(L2
, "imgpwd", imgpwd
);
2091 VTOY_JSON_FMT_DIFF_STRN(L2
, "efipwd", efipwd
);
2092 VTOY_JSON_FMT_DIFF_STRN(L2
, "vtoypwd", vtoypwd
);
2096 VTOY_JSON_FMT_KEY_L(L2
, "menupwd");
2097 VTOY_JSON_FMT_ARY_BEGIN_N();
2099 for (node
= data
->list
; node
; node
= node
->next
)
2101 VTOY_JSON_FMT_OBJ_BEGIN_LN(L3
);
2103 if (node
->type
== 0)
2105 VTOY_JSON_FMT_STRN_PATH_LN(L4
, "file", node
->path
);
2109 VTOY_JSON_FMT_STRN_PATH_LN(L4
, "parent", node
->path
);
2111 VTOY_JSON_FMT_STRN_LN(L4
, "pwd", node
->pwd
);
2113 VTOY_JSON_FMT_OBJ_ENDEX_LN(L3
);
2116 VTOY_JSON_FMT_ARY_ENDEX_LN(L2
);
2119 VTOY_JSON_FMT_OBJ_ENDEX_LN(L1
);
2121 VTOY_JSON_FMT_END(pos
);
2127 int ventoy_data_json_password(data_password
*data
, char *buf
, int buflen
)
2131 menu_password
*node
= NULL
;
2133 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2135 VTOY_JSON_FMT_OBJ_BEGIN();
2137 VTOY_JSON_FMT_STRN("bootpwd", data
->bootpwd
);
2138 VTOY_JSON_FMT_STRN("isopwd", data
->isopwd
);
2139 VTOY_JSON_FMT_STRN("wimpwd", data
->wimpwd
);
2140 VTOY_JSON_FMT_STRN("vhdpwd", data
->vhdpwd
);
2141 VTOY_JSON_FMT_STRN("imgpwd", data
->imgpwd
);
2142 VTOY_JSON_FMT_STRN("efipwd", data
->efipwd
);
2143 VTOY_JSON_FMT_STRN("vtoypwd", data
->vtoypwd
);
2145 VTOY_JSON_FMT_KEY("list");
2146 VTOY_JSON_FMT_ARY_BEGIN();
2148 for (node
= data
->list
; node
; node
= node
->next
)
2150 VTOY_JSON_FMT_OBJ_BEGIN();
2152 VTOY_JSON_FMT_SINT("type", node
->type
);
2153 VTOY_JSON_FMT_STRN("path", node
->path
);
2154 if (node
->type
== path_type_file
)
2156 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
2160 valid
= ventoy_is_directory_exist("%s%s", g_cur_dir
, node
->path
);
2163 VTOY_JSON_FMT_SINT("valid", valid
);
2164 VTOY_JSON_FMT_STRN("pwd", node
->pwd
);
2166 VTOY_JSON_FMT_OBJ_ENDEX();
2169 VTOY_JSON_FMT_ARY_ENDEX();
2171 VTOY_JSON_FMT_OBJ_END();
2172 VTOY_JSON_FMT_END(pos
);
2177 static int ventoy_api_get_password(struct mg_connection
*conn
, VTOY_JSON
*json
)
2179 api_get_func(conn
, json
, password
);
2183 static int ventoy_api_save_password(struct mg_connection
*conn
, VTOY_JSON
*json
)
2187 data_password
*data
= NULL
;
2189 vtoy_json_get_int(json
, "index", &index
);
2190 data
= g_data_password
+ index
;
2192 VTOY_JSON_STR("bootpwd", data
->bootpwd
);
2193 VTOY_JSON_STR("isopwd", data
->isopwd
);
2194 VTOY_JSON_STR("wimpwd", data
->wimpwd
);
2195 VTOY_JSON_STR("vhdpwd", data
->vhdpwd
);
2196 VTOY_JSON_STR("imgpwd", data
->imgpwd
);
2197 VTOY_JSON_STR("efipwd", data
->efipwd
);
2198 VTOY_JSON_STR("vtoypwd", data
->vtoypwd
);
2200 ret
= ventoy_data_save_all();
2202 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2206 static int ventoy_api_password_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
2211 const char *path
= NULL
;
2212 const char *pwd
= NULL
;
2213 menu_password
*node
= NULL
;
2214 menu_password
*cur
= NULL
;
2215 data_password
*data
= NULL
;
2217 vtoy_json_get_int(json
, "index", &index
);
2218 data
= g_data_password
+ index
;
2220 vtoy_json_get_int(json
, "type", &type
);
2222 path
= VTOY_JSON_STR_EX("path");
2223 pwd
= VTOY_JSON_STR_EX("pwd");
2226 node
= zalloc(sizeof(menu_password
));
2230 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
2231 scnprintf(node
->pwd
, sizeof(node
->pwd
), "%s", pwd
);
2233 vtoy_list_add(data
->list
, cur
, node
);
2237 ret
= ventoy_data_save_all();
2239 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2243 static int ventoy_api_password_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
2247 const char *path
= NULL
;
2248 menu_password
*last
= NULL
;
2249 menu_password
*node
= NULL
;
2250 data_password
*data
= NULL
;
2252 vtoy_json_get_int(json
, "index", &index
);
2253 data
= g_data_password
+ index
;
2255 path
= VTOY_JSON_STR_EX("path");
2258 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
2260 vtoy_list_free(menu_password
, data
->list
);
2264 vtoy_list_del(last
, node
, data
->list
, path
);
2268 ret
= ventoy_data_save_all();
2270 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2277 void ventoy_data_default_conf_replace(data_conf_replace
*data
)
2279 memset(data
, 0, sizeof(data_conf_replace
));
2282 int ventoy_data_cmp_conf_replace(data_conf_replace
*data1
, data_conf_replace
*data2
)
2284 conf_replace_node
*list1
= NULL
;
2285 conf_replace_node
*list2
= NULL
;
2287 if (NULL
== data1
->list
&& NULL
== data2
->list
)
2291 else if (data1
->list
&& data2
->list
)
2293 list1
= data1
->list
;
2294 list2
= data2
->list
;
2296 while (list1
&& list2
)
2298 if (list1
->image
!= list2
->image
||
2299 strcmp(list1
->path
, list2
->path
) ||
2300 strcmp(list1
->org
, list2
->org
) ||
2301 strcmp(list1
->new, list2
->new)
2307 list1
= list1
->next
;
2308 list2
= list2
->next
;
2311 if (list1
== NULL
&& list2
== NULL
)
2326 int ventoy_data_save_conf_replace(data_conf_replace
*data
, const char *title
, char *buf
, int buflen
)
2329 conf_replace_node
*node
= NULL
;
2331 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2333 VTOY_JSON_FMT_KEY_L(L1
, title
);
2334 VTOY_JSON_FMT_ARY_BEGIN_N();
2336 for (node
= data
->list
; node
; node
= node
->next
)
2338 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
2340 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "iso", node
->path
);
2341 VTOY_JSON_FMT_STRN_LN(L3
, "org", node
->org
);
2342 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "new", node
->new);
2345 VTOY_JSON_FMT_SINT_LN(L3
, "img", node
->image
);
2348 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
2351 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
2352 VTOY_JSON_FMT_END(pos
);
2358 int ventoy_data_json_conf_replace(data_conf_replace
*data
, char *buf
, int buflen
)
2361 conf_replace_node
*node
= NULL
;
2363 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2364 VTOY_JSON_FMT_ARY_BEGIN();
2366 for (node
= data
->list
; node
; node
= node
->next
)
2368 VTOY_JSON_FMT_OBJ_BEGIN();
2370 VTOY_JSON_FMT_STRN("path", node
->path
);
2371 VTOY_JSON_FMT_SINT("valid", ventoy_check_fuzzy_path(node
->path
, 1));
2372 VTOY_JSON_FMT_STRN("org", node
->org
);
2373 VTOY_JSON_FMT_STRN("new", node
->new);
2374 VTOY_JSON_FMT_SINT("new_valid", ventoy_is_file_exist("%s%s", g_cur_dir
, node
->new));
2375 VTOY_JSON_FMT_SINT("img", node
->image
);
2377 VTOY_JSON_FMT_OBJ_ENDEX();
2380 VTOY_JSON_FMT_ARY_END();
2381 VTOY_JSON_FMT_END(pos
);
2386 static int ventoy_api_get_conf_replace(struct mg_connection
*conn
, VTOY_JSON
*json
)
2388 api_get_func(conn
, json
, conf_replace
);
2392 static int ventoy_api_save_conf_replace(struct mg_connection
*conn
, VTOY_JSON
*json
)
2395 ret
= ventoy_data_save_all();
2397 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2401 static int ventoy_api_conf_replace_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
2406 const char *path
= NULL
;
2407 const char *org
= NULL
;
2408 const char *new = NULL
;
2409 conf_replace_node
*node
= NULL
;
2410 conf_replace_node
*cur
= NULL
;
2411 data_conf_replace
*data
= NULL
;
2413 vtoy_json_get_int(json
, "img", &image
);
2415 vtoy_json_get_int(json
, "index", &index
);
2416 data
= g_data_conf_replace
+ index
;
2418 path
= VTOY_JSON_STR_EX("path");
2419 org
= VTOY_JSON_STR_EX("org");
2420 new = VTOY_JSON_STR_EX("new");
2421 if (path
&& org
&& new)
2423 node
= zalloc(sizeof(conf_replace_node
));
2426 node
->image
= image
;
2427 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
2428 scnprintf(node
->org
, sizeof(node
->org
), "%s", org
);
2429 scnprintf(node
->new, sizeof(node
->new), "%s", new);
2431 vtoy_list_add(data
->list
, cur
, node
);
2435 ret
= ventoy_data_save_all();
2437 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2441 static int ventoy_api_conf_replace_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
2445 const char *path
= NULL
;
2446 conf_replace_node
*last
= NULL
;
2447 conf_replace_node
*node
= NULL
;
2448 data_conf_replace
*data
= NULL
;
2450 vtoy_json_get_int(json
, "index", &index
);
2451 data
= g_data_conf_replace
+ index
;
2453 path
= VTOY_JSON_STR_EX("path");
2456 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
2458 vtoy_list_free(conf_replace_node
, data
->list
);
2462 vtoy_list_del(last
, node
, data
->list
, path
);
2466 ret
= ventoy_data_save_all();
2468 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2476 void ventoy_data_default_dud(data_dud
*data
)
2478 memset(data
, 0, sizeof(data_dud
));
2481 int ventoy_data_cmp_dud(data_dud
*data1
, data_dud
*data2
)
2483 dud_node
*list1
= NULL
;
2484 dud_node
*list2
= NULL
;
2486 if (NULL
== data1
->list
&& NULL
== data2
->list
)
2490 else if (data1
->list
&& data2
->list
)
2492 list1
= data1
->list
;
2493 list2
= data2
->list
;
2495 while (list1
&& list2
)
2497 if (strcmp(list1
->path
, list2
->path
))
2502 /* no need to compare dud list with default */
2503 list1
= list1
->next
;
2504 list2
= list2
->next
;
2507 if (list1
== NULL
&& list2
== NULL
)
2522 int ventoy_data_save_dud(data_dud
*data
, const char *title
, char *buf
, int buflen
)
2525 dud_node
*node
= NULL
;
2526 path_node
*pathnode
= NULL
;
2528 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2530 VTOY_JSON_FMT_KEY_L(L1
, title
);
2531 VTOY_JSON_FMT_ARY_BEGIN_N();
2533 for (node
= data
->list
; node
; node
= node
->next
)
2535 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
2536 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "image", node
->path
);
2538 VTOY_JSON_FMT_KEY_L(L3
, "dud");
2539 VTOY_JSON_FMT_ARY_BEGIN_N();
2540 for (pathnode
= node
->list
; pathnode
; pathnode
= pathnode
->next
)
2542 VTOY_JSON_FMT_ITEM_PATH_LN(L4
, pathnode
->path
);
2544 VTOY_JSON_FMT_ARY_ENDEX_LN(L3
);
2546 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
2549 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
2550 VTOY_JSON_FMT_END(pos
);
2556 int ventoy_data_json_dud(data_dud
*data
, char *buf
, int buflen
)
2560 dud_node
*node
= NULL
;
2561 path_node
*pathnode
= NULL
;
2563 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2564 VTOY_JSON_FMT_ARY_BEGIN();
2566 for (node
= data
->list
; node
; node
= node
->next
)
2568 VTOY_JSON_FMT_OBJ_BEGIN();
2570 VTOY_JSON_FMT_STRN("path", node
->path
);
2571 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
2572 VTOY_JSON_FMT_SINT("valid", valid
);
2575 VTOY_JSON_FMT_KEY("list");
2576 VTOY_JSON_FMT_ARY_BEGIN();
2577 for (pathnode
= node
->list
; pathnode
; pathnode
= pathnode
->next
)
2579 VTOY_JSON_FMT_OBJ_BEGIN();
2580 VTOY_JSON_FMT_STRN("path", pathnode
->path
);
2582 valid
= ventoy_is_file_exist("%s%s", g_cur_dir
, pathnode
->path
);
2583 VTOY_JSON_FMT_SINT("valid", valid
);
2584 VTOY_JSON_FMT_OBJ_ENDEX();
2586 VTOY_JSON_FMT_ARY_ENDEX();
2589 VTOY_JSON_FMT_OBJ_ENDEX();
2592 VTOY_JSON_FMT_ARY_END();
2593 VTOY_JSON_FMT_END(pos
);
2598 static int ventoy_api_get_dud(struct mg_connection
*conn
, VTOY_JSON
*json
)
2600 api_get_func(conn
, json
, dud
);
2604 static int ventoy_api_save_dud(struct mg_connection
*conn
, VTOY_JSON
*json
)
2607 ret
= ventoy_data_save_all();
2609 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2614 static int ventoy_api_dud_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
2618 const char *path
= NULL
;
2619 dud_node
*node
= NULL
;
2620 dud_node
*cur
= NULL
;
2621 data_dud
*data
= NULL
;
2622 VTOY_JSON
*array
= NULL
;
2624 vtoy_json_get_int(json
, "index", &index
);
2625 data
= g_data_dud
+ index
;
2627 array
= vtoy_json_find_item(json
, JSON_TYPE_ARRAY
, "dud");
2628 path
= VTOY_JSON_STR_EX("path");
2631 node
= zalloc(sizeof(dud_node
));
2634 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
2635 node
->list
= ventoy_path_node_add_array(array
);
2637 vtoy_list_add(data
->list
, cur
, node
);
2641 ret
= ventoy_data_save_all();
2643 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2647 static int ventoy_api_dud_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
2651 const char *path
= NULL
;
2652 dud_node
*next
= NULL
;
2653 dud_node
*last
= NULL
;
2654 dud_node
*node
= NULL
;
2655 data_dud
*data
= NULL
;
2657 vtoy_json_get_int(json
, "index", &index
);
2658 data
= g_data_dud
+ index
;
2660 path
= VTOY_JSON_STR_EX("path");
2663 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
2665 for (node
= data
->list
; node
; node
= next
)
2668 ventoy_free_path_node_list(node
->list
);
2675 vtoy_list_del_ex(last
, node
, data
->list
, path
, ventoy_free_path_node_list
);
2679 ret
= ventoy_data_save_all();
2681 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2686 static int ventoy_api_dud_add_inner(struct mg_connection
*conn
, VTOY_JSON
*json
)
2690 const char *path
= NULL
;
2691 const char *outpath
= NULL
;
2692 path_node
*pcur
= NULL
;
2693 path_node
*pnode
= NULL
;
2694 dud_node
*node
= NULL
;
2695 data_dud
*data
= NULL
;
2697 vtoy_json_get_int(json
, "index", &index
);
2698 data
= g_data_dud
+ index
;
2700 path
= VTOY_JSON_STR_EX("path");
2701 outpath
= VTOY_JSON_STR_EX("outpath");
2702 if (path
&& outpath
)
2704 for (node
= data
->list
; node
; node
= node
->next
)
2706 if (strcmp(outpath
, node
->path
) == 0)
2708 pnode
= zalloc(sizeof(path_node
));
2711 scnprintf(pnode
->path
, sizeof(pnode
->path
), "%s", path
);
2712 vtoy_list_add(node
->list
, pcur
, pnode
);
2720 ret
= ventoy_data_save_all();
2722 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2726 static int ventoy_api_dud_del_inner(struct mg_connection
*conn
, VTOY_JSON
*json
)
2730 const char *path
= NULL
;
2731 const char *outpath
= NULL
;
2732 path_node
*plast
= NULL
;
2733 path_node
*pnode
= NULL
;
2734 dud_node
*node
= NULL
;
2735 data_dud
*data
= NULL
;
2737 vtoy_json_get_int(json
, "index", &index
);
2738 data
= g_data_dud
+ index
;
2740 path
= VTOY_JSON_STR_EX("path");
2741 outpath
= VTOY_JSON_STR_EX("outpath");
2742 if (path
&& outpath
)
2744 for (node
= data
->list
; node
; node
= node
->next
)
2746 if (strcmp(outpath
, node
->path
) == 0)
2748 vtoy_list_del(plast
, pnode
, node
->list
, path
);
2754 ret
= ventoy_data_save_all();
2756 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2764 void ventoy_data_default_auto_install(data_auto_install
*data
)
2766 memset(data
, 0, sizeof(data_auto_install
));
2769 int ventoy_data_cmp_auto_install(data_auto_install
*data1
, data_auto_install
*data2
)
2771 auto_install_node
*list1
= NULL
;
2772 auto_install_node
*list2
= NULL
;
2774 if (NULL
== data1
->list
&& NULL
== data2
->list
)
2778 else if (data1
->list
&& data2
->list
)
2780 list1
= data1
->list
;
2781 list2
= data2
->list
;
2783 while (list1
&& list2
)
2785 if (list1
->timeout
!= list2
->timeout
||
2786 list1
->autosel
!= list2
->autosel
||
2787 strcmp(list1
->path
, list2
->path
))
2792 /* no need to compare auto install list with default */
2793 list1
= list1
->next
;
2794 list2
= list2
->next
;
2797 if (list1
== NULL
&& list2
== NULL
)
2812 int ventoy_data_save_auto_install(data_auto_install
*data
, const char *title
, char *buf
, int buflen
)
2815 auto_install_node
*node
= NULL
;
2816 path_node
*pathnode
= NULL
;
2818 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2820 VTOY_JSON_FMT_KEY_L(L1
, title
);
2821 VTOY_JSON_FMT_ARY_BEGIN_N();
2823 for (node
= data
->list
; node
; node
= node
->next
)
2825 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
2826 if (node
->type
== 0)
2828 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "image", node
->path
);
2832 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "parent", node
->path
);
2836 VTOY_JSON_FMT_KEY_L(L3
, "template");
2837 VTOY_JSON_FMT_ARY_BEGIN_N();
2838 for (pathnode
= node
->list
; pathnode
; pathnode
= pathnode
->next
)
2840 VTOY_JSON_FMT_ITEM_PATH_LN(L4
, pathnode
->path
);
2842 VTOY_JSON_FMT_ARY_ENDEX_LN(L3
);
2844 if (node
->timeouten
)
2846 VTOY_JSON_FMT_SINT_LN(L3
, "timeout", node
->timeout
);
2849 if (node
->autoselen
)
2851 VTOY_JSON_FMT_SINT_LN(L3
, "autosel", node
->autosel
);
2854 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
2857 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
2858 VTOY_JSON_FMT_END(pos
);
2864 int ventoy_data_json_auto_install(data_auto_install
*data
, char *buf
, int buflen
)
2868 auto_install_node
*node
= NULL
;
2869 path_node
*pathnode
= NULL
;
2871 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2872 VTOY_JSON_FMT_ARY_BEGIN();
2874 for (node
= data
->list
; node
; node
= node
->next
)
2876 VTOY_JSON_FMT_OBJ_BEGIN();
2878 VTOY_JSON_FMT_STRN("path", node
->path
);
2880 if (node
->type
== 0)
2882 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
2886 valid
= ventoy_is_directory_exist("%s%s", g_cur_dir
, node
->path
);
2888 VTOY_JSON_FMT_SINT("valid", valid
);
2889 VTOY_JSON_FMT_SINT("type", node
->type
);
2891 VTOY_JSON_FMT_BOOL("timeouten", node
->timeouten
);
2892 VTOY_JSON_FMT_BOOL("autoselen", node
->autoselen
);
2894 VTOY_JSON_FMT_SINT("autosel", node
->autosel
);
2895 VTOY_JSON_FMT_SINT("timeout", node
->timeout
);
2897 VTOY_JSON_FMT_KEY("list");
2898 VTOY_JSON_FMT_ARY_BEGIN();
2899 for (pathnode
= node
->list
; pathnode
; pathnode
= pathnode
->next
)
2901 VTOY_JSON_FMT_OBJ_BEGIN();
2902 VTOY_JSON_FMT_STRN("path", pathnode
->path
);
2904 valid
= ventoy_is_file_exist("%s%s", g_cur_dir
, pathnode
->path
);
2905 VTOY_JSON_FMT_SINT("valid", valid
);
2906 VTOY_JSON_FMT_OBJ_ENDEX();
2908 VTOY_JSON_FMT_ARY_ENDEX();
2911 VTOY_JSON_FMT_OBJ_ENDEX();
2914 VTOY_JSON_FMT_ARY_END();
2915 VTOY_JSON_FMT_END(pos
);
2920 static int ventoy_api_get_auto_install(struct mg_connection
*conn
, VTOY_JSON
*json
)
2922 api_get_func(conn
, json
, auto_install
);
2926 static int ventoy_api_save_auto_install(struct mg_connection
*conn
, VTOY_JSON
*json
)
2932 uint8_t timeouten
= 0;
2933 uint8_t autoselen
= 0;
2934 auto_install_node
*node
= NULL
;
2935 data_auto_install
*data
= NULL
;
2937 vtoy_json_get_int(json
, "index", &index
);
2938 vtoy_json_get_int(json
, "id", &id
);
2940 vtoy_json_get_bool(json
, "timeouten", &timeouten
);
2941 vtoy_json_get_bool(json
, "autoselen", &autoselen
);
2943 data
= g_data_auto_install
+ index
;
2947 for (node
= data
->list
; node
; node
= node
->next
)
2951 node
->timeouten
= (int)timeouten
;
2952 node
->autoselen
= (int)autoselen
;
2953 VTOY_JSON_INT("timeout", node
->timeout
);
2954 VTOY_JSON_INT("autosel", node
->autosel
);
2960 ret
= ventoy_data_save_all();
2962 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2967 static int ventoy_api_auto_install_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
2972 const char *path
= NULL
;
2973 auto_install_node
*node
= NULL
;
2974 auto_install_node
*cur
= NULL
;
2975 data_auto_install
*data
= NULL
;
2976 VTOY_JSON
*array
= NULL
;
2978 vtoy_json_get_int(json
, "type", &type
);
2979 vtoy_json_get_int(json
, "index", &index
);
2980 data
= g_data_auto_install
+ index
;
2982 array
= vtoy_json_find_item(json
, JSON_TYPE_ARRAY
, "template");
2983 path
= VTOY_JSON_STR_EX("path");
2986 node
= zalloc(sizeof(auto_install_node
));
2990 node
->timeouten
= 0;
2991 node
->autoselen
= 0;
2994 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
2995 node
->list
= ventoy_path_node_add_array(array
);
2997 vtoy_list_add(data
->list
, cur
, node
);
3001 ret
= ventoy_data_save_all();
3003 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3007 static int ventoy_api_auto_install_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
3011 const char *path
= NULL
;
3012 auto_install_node
*last
= NULL
;
3013 auto_install_node
*next
= NULL
;
3014 auto_install_node
*node
= NULL
;
3015 data_auto_install
*data
= NULL
;
3017 vtoy_json_get_int(json
, "index", &index
);
3018 data
= g_data_auto_install
+ index
;
3020 path
= VTOY_JSON_STR_EX("path");
3023 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
3025 for (node
= data
->list
; node
; node
= next
)
3028 ventoy_free_path_node_list(node
->list
);
3035 vtoy_list_del_ex(last
, node
, data
->list
, path
, ventoy_free_path_node_list
);
3039 ret
= ventoy_data_save_all();
3041 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3045 static int ventoy_api_auto_install_add_inner(struct mg_connection
*conn
, VTOY_JSON
*json
)
3049 const char *path
= NULL
;
3050 const char *outpath
= NULL
;
3051 path_node
*pcur
= NULL
;
3052 path_node
*pnode
= NULL
;
3053 auto_install_node
*node
= NULL
;
3054 data_auto_install
*data
= NULL
;
3056 vtoy_json_get_int(json
, "index", &index
);
3057 data
= g_data_auto_install
+ index
;
3059 path
= VTOY_JSON_STR_EX("path");
3060 outpath
= VTOY_JSON_STR_EX("outpath");
3061 if (path
&& outpath
)
3063 for (node
= data
->list
; node
; node
= node
->next
)
3065 if (strcmp(outpath
, node
->path
) == 0)
3067 pnode
= zalloc(sizeof(path_node
));
3070 scnprintf(pnode
->path
, sizeof(pnode
->path
), "%s", path
);
3071 vtoy_list_add(node
->list
, pcur
, pnode
);
3079 ret
= ventoy_data_save_all();
3081 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3085 static int ventoy_api_auto_install_del_inner(struct mg_connection
*conn
, VTOY_JSON
*json
)
3089 const char *path
= NULL
;
3090 const char *outpath
= NULL
;
3091 path_node
*plast
= NULL
;
3092 path_node
*pnode
= NULL
;
3093 auto_install_node
*node
= NULL
;
3094 data_auto_install
*data
= NULL
;
3096 vtoy_json_get_int(json
, "index", &index
);
3097 data
= g_data_auto_install
+ index
;
3099 path
= VTOY_JSON_STR_EX("path");
3100 outpath
= VTOY_JSON_STR_EX("outpath");
3101 if (path
&& outpath
)
3103 for (node
= data
->list
; node
; node
= node
->next
)
3105 if (strcmp(outpath
, node
->path
) == 0)
3107 vtoy_list_del(plast
, pnode
, node
->list
, path
);
3113 ret
= ventoy_data_save_all();
3115 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3124 void ventoy_data_default_persistence(data_persistence
*data
)
3126 memset(data
, 0, sizeof(data_persistence
));
3129 int ventoy_data_cmp_persistence(data_persistence
*data1
, data_persistence
*data2
)
3131 persistence_node
*list1
= NULL
;
3132 persistence_node
*list2
= NULL
;
3134 if (NULL
== data1
->list
&& NULL
== data2
->list
)
3138 else if (data1
->list
&& data2
->list
)
3140 list1
= data1
->list
;
3141 list2
= data2
->list
;
3143 while (list1
&& list2
)
3145 if (list1
->timeout
!= list2
->timeout
||
3146 list1
->autosel
!= list2
->autosel
||
3147 strcmp(list1
->path
, list2
->path
))
3152 /* no need to compare auto install list with default */
3153 list1
= list1
->next
;
3154 list2
= list2
->next
;
3157 if (list1
== NULL
&& list2
== NULL
)
3172 int ventoy_data_save_persistence(data_persistence
*data
, const char *title
, char *buf
, int buflen
)
3175 persistence_node
*node
= NULL
;
3176 path_node
*pathnode
= NULL
;
3178 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
3180 VTOY_JSON_FMT_KEY_L(L1
, title
);
3181 VTOY_JSON_FMT_ARY_BEGIN_N();
3183 for (node
= data
->list
; node
; node
= node
->next
)
3185 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
3186 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "image", node
->path
);
3187 VTOY_JSON_FMT_KEY_L(L3
, "backend");
3188 VTOY_JSON_FMT_ARY_BEGIN_N();
3189 for (pathnode
= node
->list
; pathnode
; pathnode
= pathnode
->next
)
3191 VTOY_JSON_FMT_ITEM_PATH_LN(L4
, pathnode
->path
);
3193 VTOY_JSON_FMT_ARY_ENDEX_LN(L3
);
3195 if (node
->timeouten
)
3197 VTOY_JSON_FMT_SINT_LN(L3
, "timeout", node
->timeout
);
3200 if (node
->autoselen
)
3202 VTOY_JSON_FMT_SINT_LN(L3
, "autosel", node
->autosel
);
3205 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
3208 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
3209 VTOY_JSON_FMT_END(pos
);
3215 int ventoy_data_json_persistence(data_persistence
*data
, char *buf
, int buflen
)
3219 persistence_node
*node
= NULL
;
3220 path_node
*pathnode
= NULL
;
3222 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
3223 VTOY_JSON_FMT_ARY_BEGIN();
3225 for (node
= data
->list
; node
; node
= node
->next
)
3227 VTOY_JSON_FMT_OBJ_BEGIN();
3229 VTOY_JSON_FMT_STRN("path", node
->path
);
3231 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
3232 VTOY_JSON_FMT_SINT("valid", valid
);
3233 VTOY_JSON_FMT_SINT("type", node
->type
);
3235 VTOY_JSON_FMT_BOOL("timeouten", node
->timeouten
);
3236 VTOY_JSON_FMT_BOOL("autoselen", node
->autoselen
);
3238 VTOY_JSON_FMT_SINT("autosel", node
->autosel
);
3239 VTOY_JSON_FMT_SINT("timeout", node
->timeout
);
3241 VTOY_JSON_FMT_KEY("list");
3242 VTOY_JSON_FMT_ARY_BEGIN();
3243 for (pathnode
= node
->list
; pathnode
; pathnode
= pathnode
->next
)
3245 VTOY_JSON_FMT_OBJ_BEGIN();
3246 VTOY_JSON_FMT_STRN("path", pathnode
->path
);
3248 valid
= ventoy_is_file_exist("%s%s", g_cur_dir
, pathnode
->path
);
3249 VTOY_JSON_FMT_SINT("valid", valid
);
3250 VTOY_JSON_FMT_OBJ_ENDEX();
3252 VTOY_JSON_FMT_ARY_ENDEX();
3255 VTOY_JSON_FMT_OBJ_ENDEX();
3258 VTOY_JSON_FMT_ARY_END();
3259 VTOY_JSON_FMT_END(pos
);
3264 static int ventoy_api_get_persistence(struct mg_connection
*conn
, VTOY_JSON
*json
)
3266 api_get_func(conn
, json
, persistence
);
3270 static int ventoy_api_save_persistence(struct mg_connection
*conn
, VTOY_JSON
*json
)
3276 uint8_t timeouten
= 0;
3277 uint8_t autoselen
= 0;
3278 persistence_node
*node
= NULL
;
3279 data_persistence
*data
= NULL
;
3281 vtoy_json_get_int(json
, "index", &index
);
3282 vtoy_json_get_int(json
, "id", &id
);
3284 vtoy_json_get_bool(json
, "timeouten", &timeouten
);
3285 vtoy_json_get_bool(json
, "autoselen", &autoselen
);
3287 data
= g_data_persistence
+ index
;
3291 for (node
= data
->list
; node
; node
= node
->next
)
3295 node
->timeouten
= (int)timeouten
;
3296 node
->autoselen
= (int)autoselen
;
3297 VTOY_JSON_INT("timeout", node
->timeout
);
3298 VTOY_JSON_INT("autosel", node
->autosel
);
3304 ret
= ventoy_data_save_all();
3306 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3311 static int ventoy_api_persistence_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
3315 const char *path
= NULL
;
3316 persistence_node
*node
= NULL
;
3317 persistence_node
*cur
= NULL
;
3318 data_persistence
*data
= NULL
;
3319 VTOY_JSON
*array
= NULL
;
3321 vtoy_json_get_int(json
, "index", &index
);
3322 data
= g_data_persistence
+ index
;
3324 array
= vtoy_json_find_item(json
, JSON_TYPE_ARRAY
, "backend");
3325 path
= VTOY_JSON_STR_EX("path");
3328 node
= zalloc(sizeof(persistence_node
));
3331 node
->timeouten
= 0;
3332 node
->autoselen
= 0;
3335 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
3336 node
->list
= ventoy_path_node_add_array(array
);
3338 vtoy_list_add(data
->list
, cur
, node
);
3342 ret
= ventoy_data_save_all();
3344 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3348 static int ventoy_api_persistence_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
3352 const char *path
= NULL
;
3353 persistence_node
*last
= NULL
;
3354 persistence_node
*next
= NULL
;
3355 persistence_node
*node
= NULL
;
3356 data_persistence
*data
= NULL
;
3358 vtoy_json_get_int(json
, "index", &index
);
3359 data
= g_data_persistence
+ index
;
3361 path
= VTOY_JSON_STR_EX("path");
3364 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
3366 for (node
= data
->list
; node
; node
= next
)
3369 ventoy_free_path_node_list(node
->list
);
3376 vtoy_list_del_ex(last
, node
, data
->list
, path
, ventoy_free_path_node_list
);
3380 ret
= ventoy_data_save_all();
3382 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3386 static int ventoy_api_persistence_add_inner(struct mg_connection
*conn
, VTOY_JSON
*json
)
3390 const char *path
= NULL
;
3391 const char *outpath
= NULL
;
3392 path_node
*pcur
= NULL
;
3393 path_node
*pnode
= NULL
;
3394 persistence_node
*node
= NULL
;
3395 data_persistence
*data
= NULL
;
3397 vtoy_json_get_int(json
, "index", &index
);
3398 data
= g_data_persistence
+ index
;
3400 path
= VTOY_JSON_STR_EX("path");
3401 outpath
= VTOY_JSON_STR_EX("outpath");
3402 if (path
&& outpath
)
3404 for (node
= data
->list
; node
; node
= node
->next
)
3406 if (strcmp(outpath
, node
->path
) == 0)
3408 pnode
= zalloc(sizeof(path_node
));
3411 scnprintf(pnode
->path
, sizeof(pnode
->path
), "%s", path
);
3412 vtoy_list_add(node
->list
, pcur
, pnode
);
3420 ret
= ventoy_data_save_all();
3422 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3426 static int ventoy_api_persistence_del_inner(struct mg_connection
*conn
, VTOY_JSON
*json
)
3430 const char *path
= NULL
;
3431 const char *outpath
= NULL
;
3432 path_node
*plast
= NULL
;
3433 path_node
*pnode
= NULL
;
3434 persistence_node
*node
= NULL
;
3435 data_persistence
*data
= NULL
;
3437 vtoy_json_get_int(json
, "index", &index
);
3438 data
= g_data_persistence
+ index
;
3440 path
= VTOY_JSON_STR_EX("path");
3441 outpath
= VTOY_JSON_STR_EX("outpath");
3442 if (path
&& outpath
)
3444 for (node
= data
->list
; node
; node
= node
->next
)
3446 if (strcmp(outpath
, node
->path
) == 0)
3448 vtoy_list_del(plast
, pnode
, node
->list
, path
);
3454 ret
= ventoy_data_save_all();
3456 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3464 void ventoy_data_default_injection(data_injection
*data
)
3466 memset(data
, 0, sizeof(data_injection
));
3469 int ventoy_data_cmp_injection(data_injection
*data1
, data_injection
*data2
)
3471 injection_node
*list1
= NULL
;
3472 injection_node
*list2
= NULL
;
3474 if (NULL
== data1
->list
&& NULL
== data2
->list
)
3478 else if (data1
->list
&& data2
->list
)
3480 list1
= data1
->list
;
3481 list2
= data2
->list
;
3483 while (list1
&& list2
)
3485 if ((list1
->type
!= list2
->type
) ||
3486 strcmp(list1
->path
, list2
->path
) ||
3487 strcmp(list1
->archive
, list2
->archive
))
3492 list1
= list1
->next
;
3493 list2
= list2
->next
;
3496 if (list1
== NULL
&& list2
== NULL
)
3511 int ventoy_data_save_injection(data_injection
*data
, const char *title
, char *buf
, int buflen
)
3514 injection_node
*node
= NULL
;
3516 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
3518 VTOY_JSON_FMT_KEY_L(L1
, title
);
3519 VTOY_JSON_FMT_ARY_BEGIN_N();
3521 for (node
= data
->list
; node
; node
= node
->next
)
3523 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
3525 if (node
->type
== 0)
3527 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "image", node
->path
);
3531 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "parent", node
->path
);
3533 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "archive", node
->archive
);
3535 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
3538 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
3539 VTOY_JSON_FMT_END(pos
);
3545 int ventoy_data_json_injection(data_injection
*data
, char *buf
, int buflen
)
3549 injection_node
*node
= NULL
;
3551 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
3552 VTOY_JSON_FMT_ARY_BEGIN();
3554 for (node
= data
->list
; node
; node
= node
->next
)
3556 VTOY_JSON_FMT_OBJ_BEGIN();
3558 VTOY_JSON_FMT_UINT("type", node
->type
);
3559 VTOY_JSON_FMT_STRN("path", node
->path
);
3561 if (node
->type
== 0)
3563 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
3567 valid
= ventoy_is_directory_exist("%s%s", g_cur_dir
, node
->path
);
3569 VTOY_JSON_FMT_SINT("valid", valid
);
3571 VTOY_JSON_FMT_STRN("archive", node
->archive
);
3573 valid
= ventoy_is_file_exist("%s%s", g_cur_dir
, node
->archive
);
3574 VTOY_JSON_FMT_SINT("archive_valid", valid
);
3576 VTOY_JSON_FMT_OBJ_ENDEX();
3579 VTOY_JSON_FMT_ARY_END();
3580 VTOY_JSON_FMT_END(pos
);
3586 static int ventoy_api_get_injection(struct mg_connection
*conn
, VTOY_JSON
*json
)
3588 api_get_func(conn
, json
, injection
);
3592 static int ventoy_api_save_injection(struct mg_connection
*conn
, VTOY_JSON
*json
)
3595 ret
= ventoy_data_save_all();
3597 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3601 static int ventoy_api_injection_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
3606 const char *path
= NULL
;
3607 const char *archive
= NULL
;
3608 injection_node
*node
= NULL
;
3609 injection_node
*cur
= NULL
;
3610 data_injection
*data
= NULL
;
3612 vtoy_json_get_int(json
, "index", &index
);
3613 data
= g_data_injection
+ index
;
3615 vtoy_json_get_int(json
, "type", &type
);
3617 path
= VTOY_JSON_STR_EX("path");
3618 archive
= VTOY_JSON_STR_EX("archive");
3619 if (path
&& archive
)
3621 node
= zalloc(sizeof(injection_node
));
3626 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
3627 scnprintf(node
->archive
, sizeof(node
->archive
), "%s", archive
);
3629 vtoy_list_add(data
->list
, cur
, node
);
3633 ret
= ventoy_data_save_all();
3635 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3639 static int ventoy_api_injection_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
3643 const char *path
= NULL
;
3644 injection_node
*last
= NULL
;
3645 injection_node
*node
= NULL
;
3646 data_injection
*data
= NULL
;
3648 vtoy_json_get_int(json
, "index", &index
);
3649 data
= g_data_injection
+ index
;
3651 path
= VTOY_JSON_STR_EX("path");
3654 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
3656 vtoy_list_free(injection_node
, data
->list
);
3660 vtoy_list_del(last
, node
, data
->list
, path
);
3664 ret
= ventoy_data_save_all();
3666 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3673 static int ventoy_api_preview_json(struct mg_connection
*conn
, VTOY_JSON
*json
)
3678 int utf16enclen
= 0;
3679 char *encodebuf
= NULL
;
3680 unsigned short *utf16buf
= NULL
;
3684 /* We can not use json directly, because it will be formated in the JS. */
3686 len
= ventoy_data_real_save_all(0);
3688 utf16buf
= (unsigned short *)malloc(2 * len
+ 16);
3694 utf16enclen
= (int)utf8_to_utf16((unsigned char *)JSON_SAVE_BUFFER
, len
, utf16buf
, len
+ 2);
3696 encodebuf
= (char *)malloc(utf16enclen
* 4 + 16);
3702 for (i
= 0; i
< utf16enclen
; i
++)
3704 scnprintf(encodebuf
+ i
* 4, 5, "%04X", utf16buf
[i
]);
3708 VTOY_JSON_FMT_BEGIN(pos
, JSON_BUFFER
, JSON_BUF_MAX
);
3709 VTOY_JSON_FMT_OBJ_BEGIN();
3710 VTOY_JSON_FMT_STRN("json", (encodebuf
? encodebuf
: ""));
3711 VTOY_JSON_FMT_OBJ_END();
3712 VTOY_JSON_FMT_END(pos
);
3714 CHECK_FREE(encodebuf
);
3715 CHECK_FREE(utf16buf
);
3717 ventoy_json_buffer(conn
, JSON_BUFFER
, pos
);
3725 int ventoy_data_save_all(void)
3727 ventoy_set_writeback_event();
3731 int ventoy_data_real_save_all(int apilock
)
3739 pthread_mutex_lock(&g_api_mutex
);
3742 ssprintf(pos
, JSON_SAVE_BUFFER
, JSON_BUF_MAX
, "{\n");
3744 ventoy_save_plug(control
);
3745 ventoy_save_plug(theme
);
3746 ventoy_save_plug(menu_alias
);
3747 ventoy_save_plug(menu_tip
);
3748 ventoy_save_plug(menu_class
);
3749 ventoy_save_plug(auto_install
);
3750 ventoy_save_plug(persistence
);
3751 ventoy_save_plug(injection
);
3752 ventoy_save_plug(conf_replace
);
3753 ventoy_save_plug(password
);
3754 ventoy_save_plug(image_list
);
3755 ventoy_save_plug(auto_memdisk
);
3756 ventoy_save_plug(dud
);
3758 if (JSON_SAVE_BUFFER
[pos
- 1] == '\n' && JSON_SAVE_BUFFER
[pos
- 2] == ',')
3760 JSON_SAVE_BUFFER
[pos
- 2] = '\n';
3763 ssprintf(pos
, JSON_SAVE_BUFFER
, JSON_BUF_MAX
, "}\n");
3767 pthread_mutex_unlock(&g_api_mutex
);
3773 int ventoy_http_writeback(void)
3779 ventoy_get_json_path(filename
, NULL
);
3781 pos
= ventoy_data_real_save_all(1);
3784 printf("%s", JSON_SAVE_BUFFER
);
3787 ret
= ventoy_write_buf_to_file(filename
, JSON_SAVE_BUFFER
, pos
);
3790 vlog("Failed to write ventoy.json file.\n");
3791 g_sysinfo
.config_save_error
= 1;
3798 static JSON_CB g_ventoy_json_cb
[] =
3800 { "sysinfo", ventoy_api_sysinfo
},
3801 { "handshake", ventoy_api_handshake
},
3802 { "check_path", ventoy_api_check_exist
},
3803 { "check_path2", ventoy_api_check_exist2
},
3804 { "check_fuzzy", ventoy_api_check_fuzzy
},
3806 { "device_info", ventoy_api_device_info
},
3808 { "get_control", ventoy_api_get_control
},
3809 { "save_control", ventoy_api_save_control
},
3811 { "get_theme", ventoy_api_get_theme
},
3812 { "save_theme", ventoy_api_save_theme
},
3813 { "theme_add_file", ventoy_api_theme_add_file
},
3814 { "theme_del_file", ventoy_api_theme_del_file
},
3815 { "theme_add_font", ventoy_api_theme_add_font
},
3816 { "theme_del_font", ventoy_api_theme_del_font
},
3818 { "get_alias", ventoy_api_get_alias
},
3819 { "save_alias", ventoy_api_save_alias
},
3820 { "alias_add", ventoy_api_alias_add
},
3821 { "alias_del", ventoy_api_alias_del
},
3823 { "get_tip", ventoy_api_get_tip
},
3824 { "save_tip", ventoy_api_save_tip
},
3825 { "tip_add", ventoy_api_tip_add
},
3826 { "tip_del", ventoy_api_tip_del
},
3828 { "get_class", ventoy_api_get_class
},
3829 { "save_class", ventoy_api_save_class
},
3830 { "class_add", ventoy_api_class_add
},
3831 { "class_del", ventoy_api_class_del
},
3833 { "get_auto_memdisk", ventoy_api_get_auto_memdisk
},
3834 { "save_auto_memdisk", ventoy_api_save_auto_memdisk
},
3835 { "auto_memdisk_add", ventoy_api_auto_memdisk_add
},
3836 { "auto_memdisk_del", ventoy_api_auto_memdisk_del
},
3838 { "get_image_list", ventoy_api_get_image_list
},
3839 { "save_image_list", ventoy_api_save_image_list
},
3840 { "image_list_add", ventoy_api_image_list_add
},
3841 { "image_list_del", ventoy_api_image_list_del
},
3843 { "get_conf_replace", ventoy_api_get_conf_replace
},
3844 { "save_conf_replace", ventoy_api_save_conf_replace
},
3845 { "conf_replace_add", ventoy_api_conf_replace_add
},
3846 { "conf_replace_del", ventoy_api_conf_replace_del
},
3848 { "get_dud", ventoy_api_get_dud
},
3849 { "save_dud", ventoy_api_save_dud
},
3850 { "dud_add", ventoy_api_dud_add
},
3851 { "dud_del", ventoy_api_dud_del
},
3852 { "dud_add_inner", ventoy_api_dud_add_inner
},
3853 { "dud_del_inner", ventoy_api_dud_del_inner
},
3855 { "get_auto_install", ventoy_api_get_auto_install
},
3856 { "save_auto_install", ventoy_api_save_auto_install
},
3857 { "auto_install_add", ventoy_api_auto_install_add
},
3858 { "auto_install_del", ventoy_api_auto_install_del
},
3859 { "auto_install_add_inner", ventoy_api_auto_install_add_inner
},
3860 { "auto_install_del_inner", ventoy_api_auto_install_del_inner
},
3862 { "get_persistence", ventoy_api_get_persistence
},
3863 { "save_persistence", ventoy_api_save_persistence
},
3864 { "persistence_add", ventoy_api_persistence_add
},
3865 { "persistence_del", ventoy_api_persistence_del
},
3866 { "persistence_add_inner", ventoy_api_persistence_add_inner
},
3867 { "persistence_del_inner", ventoy_api_persistence_del_inner
},
3869 { "get_password", ventoy_api_get_password
},
3870 { "save_password", ventoy_api_save_password
},
3871 { "password_add", ventoy_api_password_add
},
3872 { "password_del", ventoy_api_password_del
},
3874 { "get_injection", ventoy_api_get_injection
},
3875 { "save_injection", ventoy_api_save_injection
},
3876 { "injection_add", ventoy_api_injection_add
},
3877 { "injection_del", ventoy_api_injection_del
},
3878 { "preview_json", ventoy_api_preview_json
},
3882 static int ventoy_json_handler(struct mg_connection
*conn
, VTOY_JSON
*json
, char *jsonstr
)
3885 const char *method
= NULL
;
3887 method
= vtoy_json_get_string_ex(json
, "method");
3890 ventoy_json_result(conn
, VTOY_JSON_SUCCESS_RET
);
3894 if (strcmp(method
, "handshake") == 0)
3896 ventoy_api_handshake(conn
, json
);
3900 for (i
= 0; i
< (int)(sizeof(g_ventoy_json_cb
) / sizeof(g_ventoy_json_cb
[0])); i
++)
3902 if (strcmp(method
, g_ventoy_json_cb
[i
].method
) == 0)
3904 g_ventoy_json_cb
[i
].callback(conn
, json
);
3912 static int ventoy_request_handler(struct mg_connection
*conn
)
3916 VTOY_JSON
*json
= NULL
;
3917 char *post_data_buf
= NULL
;
3918 const struct mg_request_info
*ri
= NULL
;
3919 char stack_buf
[512];
3921 ri
= mg_get_request_info(conn
);
3923 if (strcmp(ri
->uri
, "/vtoy/json") == 0)
3925 if (ri
->content_length
> 500)
3927 post_data_buf
= malloc((int)(ri
->content_length
+ 4));
3928 post_buf_len
= (int)(ri
->content_length
+ 1);
3932 post_data_buf
= stack_buf
;
3933 post_buf_len
= sizeof(stack_buf
);
3936 post_data_len
= mg_read(conn
, post_data_buf
, post_buf_len
);
3937 post_data_buf
[post_data_len
] = 0;
3939 json
= vtoy_json_create();
3940 if (JSON_SUCCESS
== vtoy_json_parse(json
, post_data_buf
))
3942 pthread_mutex_lock(&g_api_mutex
);
3943 ventoy_json_handler(conn
, json
->pstChild
, post_data_buf
);
3944 pthread_mutex_unlock(&g_api_mutex
);
3948 ventoy_json_result(conn
, VTOY_JSON_INVALID_RET
);
3951 vtoy_json_destroy(json
);
3953 if (post_data_buf
!= stack_buf
)
3955 free(post_data_buf
);
3965 const char *ventoy_web_openfile(const struct mg_connection
*conn
, const char *path
, size_t *data_len
)
3967 ventoy_file
*node
= NULL
;
3976 node
= ventoy_tar_find_file(path
);
3979 *data_len
= node
->size
;
3991 static int ventoy_parse_control(VTOY_JSON
*json
, void *p
)
3994 VTOY_JSON
*node
= NULL
;
3995 VTOY_JSON
*child
= NULL
;
3996 data_control
*data
= (data_control
*)p
;
3998 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4003 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4005 if (node
->enDataType
== JSON_TYPE_OBJECT
)
4007 child
= node
->pstChild
;
4009 if (child
->enDataType
!= JSON_TYPE_STRING
)
4014 if (strcmp(child
->pcName
, "VTOY_DEFAULT_MENU_MODE") == 0)
4016 CONTROL_PARSE_INT_DEF_0(child
, data
->default_menu_mode
);
4018 else if (strcmp(child
->pcName
, "VTOY_WIN11_BYPASS_CHECK") == 0)
4020 CONTROL_PARSE_INT_DEF_0(child
, data
->win11_bypass_check
);
4022 else if (strcmp(child
->pcName
, "VTOY_LINUX_REMOUNT") == 0)
4024 CONTROL_PARSE_INT_DEF_0(child
, data
->linux_remount
);
4026 else if (strcmp(child
->pcName
, "VTOY_SECONDARY_BOOT_MENU") == 0)
4028 CONTROL_PARSE_INT_DEF_1(child
, data
->secondary_menu
);
4030 else if (strcmp(child
->pcName
, "VTOY_SHOW_PASSWORD_ASTERISK") == 0)
4032 CONTROL_PARSE_INT_DEF_1(child
, data
->password_asterisk
);
4034 else if (strcmp(child
->pcName
, "VTOY_TREE_VIEW_MENU_STYLE") == 0)
4036 CONTROL_PARSE_INT_DEF_0(child
, data
->treeview_style
);
4038 else if (strcmp(child
->pcName
, "VTOY_FILT_DOT_UNDERSCORE_FILE") == 0)
4040 CONTROL_PARSE_INT_DEF_1(child
, data
->filter_dot_underscore
);
4042 else if (strcmp(child
->pcName
, "VTOY_SORT_CASE_SENSITIVE") == 0)
4044 CONTROL_PARSE_INT_DEF_0(child
, data
->sort_casesensitive
);
4046 else if (strcmp(child
->pcName
, "VTOY_MAX_SEARCH_LEVEL") == 0)
4048 if (strcmp(child
->unData
.pcStrVal
, "max") == 0)
4050 data
->max_search_level
= -1;
4054 data
->max_search_level
= (int)strtol(child
->unData
.pcStrVal
, NULL
, 10);
4057 else if (strcmp(child
->pcName
, "VTOY_DEFAULT_SEARCH_ROOT") == 0)
4059 strlcpy(data
->default_search_root
, child
->unData
.pcStrVal
);
4061 else if (strcmp(child
->pcName
, "VTOY_DEFAULT_IMAGE") == 0)
4063 strlcpy(data
->default_image
, child
->unData
.pcStrVal
);
4065 else if (strcmp(child
->pcName
, "VTOY_DEFAULT_KBD_LAYOUT") == 0)
4067 for (i
= 0; g_ventoy_kbd_layout
[i
]; i
++)
4069 if (strcmp(child
->unData
.pcStrVal
, g_ventoy_kbd_layout
[i
]) == 0)
4071 strlcpy(data
->default_kbd_layout
, child
->unData
.pcStrVal
);
4076 else if (strcmp(child
->pcName
, "VTOY_MENU_LANGUAGE") == 0)
4078 for (i
= 0; g_ventoy_menu_lang
[i
][0]; i
++)
4080 if (strcmp(child
->unData
.pcStrVal
, g_ventoy_menu_lang
[i
]) == 0)
4082 strlcpy(data
->menu_language
, child
->unData
.pcStrVal
);
4087 else if (strcmp(child
->pcName
, "VTOY_MENU_TIMEOUT") == 0)
4089 data
->menu_timeout
= (int)strtol(child
->unData
.pcStrVal
, NULL
, 10);
4091 else if (strcmp(child
->pcName
, "VTOY_SECONDARY_TIMEOUT") == 0)
4093 data
->secondary_menu_timeout
= (int)strtol(child
->unData
.pcStrVal
, NULL
, 10);
4095 else if (strcmp(child
->pcName
, "VTOY_VHD_NO_WARNING") == 0)
4097 CONTROL_PARSE_INT_DEF_0(child
, data
->vhd_no_warning
);
4099 else if (strcmp(child
->pcName
, "VTOY_FILE_FLT_ISO") == 0)
4101 CONTROL_PARSE_INT_DEF_0(child
, data
->filter_iso
);
4103 else if (strcmp(child
->pcName
, "VTOY_FILE_FLT_IMG") == 0)
4105 CONTROL_PARSE_INT_DEF_0(child
, data
->filter_img
);
4107 else if (strcmp(child
->pcName
, "VTOY_FILE_FLT_EFI") == 0)
4109 CONTROL_PARSE_INT_DEF_0(child
, data
->filter_efi
);
4111 else if (strcmp(child
->pcName
, "VTOY_FILE_FLT_WIM") == 0)
4113 CONTROL_PARSE_INT_DEF_0(child
, data
->filter_wim
);
4115 else if (strcmp(child
->pcName
, "VTOY_FILE_FLT_VHD") == 0)
4117 CONTROL_PARSE_INT_DEF_0(child
, data
->filter_vhd
);
4119 else if (strcmp(child
->pcName
, "VTOY_FILE_FLT_VTOY") == 0)
4121 CONTROL_PARSE_INT_DEF_0(child
, data
->filter_vtoy
);
4129 static int ventoy_parse_theme(VTOY_JSON
*json
, void *p
)
4131 const char *dismode
= NULL
;
4132 VTOY_JSON
*child
= NULL
;
4133 VTOY_JSON
*node
= NULL
;
4134 path_node
*tail
= NULL
;
4135 path_node
*pnode
= NULL
;
4136 data_theme
*data
= (data_theme
*)p
;
4138 if (json
->enDataType
!= JSON_TYPE_OBJECT
)
4143 child
= json
->pstChild
;
4145 dismode
= vtoy_json_get_string_ex(child
, "display_mode");
4146 vtoy_json_get_string(child
, "ventoy_left", sizeof(data
->ventoy_left
), data
->ventoy_left
);
4147 vtoy_json_get_string(child
, "ventoy_top", sizeof(data
->ventoy_top
), data
->ventoy_top
);
4148 vtoy_json_get_string(child
, "ventoy_color", sizeof(data
->ventoy_color
), data
->ventoy_color
);
4150 vtoy_json_get_int(child
, "default_file", &(data
->default_file
));
4151 vtoy_json_get_string(child
, "gfxmode", sizeof(data
->gfxmode
), data
->gfxmode
);
4152 vtoy_json_get_string(child
, "serial_param", sizeof(data
->serial_param
), data
->serial_param
);
4156 if (strcmp(dismode
, "CLI") == 0)
4158 data
->display_mode
= display_mode_cli
;
4160 else if (strcmp(dismode
, "serial") == 0)
4162 data
->display_mode
= display_mode_serial
;
4164 else if (strcmp(dismode
, "serial_console") == 0)
4166 data
->display_mode
= display_mode_ser_console
;
4170 data
->display_mode
= display_mode_gui
;
4174 node
= vtoy_json_find_item(child
, JSON_TYPE_STRING
, "file");
4177 data
->default_file
= 0;
4179 pnode
= zalloc(sizeof(path_node
));
4182 strlcpy(pnode
->path
, node
->unData
.pcStrVal
);
4183 data
->filelist
= pnode
;
4188 node
= vtoy_json_find_item(child
, JSON_TYPE_ARRAY
, "file");
4191 for (node
= node
->pstChild
; node
; node
= node
->pstNext
)
4193 if (node
->enDataType
== JSON_TYPE_STRING
)
4195 pnode
= zalloc(sizeof(path_node
));
4198 strlcpy(pnode
->path
, node
->unData
.pcStrVal
);
4206 data
->filelist
= tail
= pnode
;
4215 node
= vtoy_json_find_item(child
, JSON_TYPE_ARRAY
, "fonts");
4218 for (node
= node
->pstChild
; node
; node
= node
->pstNext
)
4220 if (node
->enDataType
== JSON_TYPE_STRING
)
4222 pnode
= zalloc(sizeof(path_node
));
4225 strlcpy(pnode
->path
, node
->unData
.pcStrVal
);
4226 if (data
->fontslist
)
4233 data
->fontslist
= tail
= pnode
;
4242 static int ventoy_parse_menu_alias(VTOY_JSON
*json
, void *p
)
4245 const char *path
= NULL
;
4246 const char *alias
= NULL
;
4247 data_alias
*data
= (data_alias
*)p
;
4248 data_alias_node
*tail
= NULL
;
4249 data_alias_node
*pnode
= NULL
;
4250 VTOY_JSON
*node
= NULL
;
4252 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4257 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4259 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4264 type
= path_type_file
;
4265 path
= vtoy_json_get_string_ex(node
->pstChild
, "image");
4268 path
= vtoy_json_get_string_ex(node
->pstChild
, "dir");
4269 type
= path_type_dir
;
4271 alias
= vtoy_json_get_string_ex(node
->pstChild
, "alias");
4275 pnode
= zalloc(sizeof(data_alias_node
));
4279 strlcpy(pnode
->path
, path
);
4280 strlcpy(pnode
->alias
, alias
);
4289 data
->list
= tail
= pnode
;
4298 static int ventoy_parse_menu_tip(VTOY_JSON
*json
, void *p
)
4301 const char *path
= NULL
;
4302 const char *tip
= NULL
;
4303 data_tip
*data
= (data_tip
*)p
;
4304 data_tip_node
*tail
= NULL
;
4305 data_tip_node
*pnode
= NULL
;
4306 VTOY_JSON
*node
= NULL
;
4307 VTOY_JSON
*tips
= NULL
;
4309 if (json
->enDataType
!= JSON_TYPE_OBJECT
)
4314 vtoy_json_get_string(json
->pstChild
, "left", sizeof(data
->left
), data
->left
);
4315 vtoy_json_get_string(json
->pstChild
, "top", sizeof(data
->top
), data
->top
);
4316 vtoy_json_get_string(json
->pstChild
, "color", sizeof(data
->color
), data
->color
);
4318 tips
= vtoy_json_find_item(json
->pstChild
, JSON_TYPE_ARRAY
, "tips");
4324 for (node
= tips
->pstChild
; node
; node
= node
->pstNext
)
4326 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4331 type
= path_type_file
;
4332 path
= vtoy_json_get_string_ex(node
->pstChild
, "image");
4335 path
= vtoy_json_get_string_ex(node
->pstChild
, "dir");
4336 type
= path_type_dir
;
4338 tip
= vtoy_json_get_string_ex(node
->pstChild
, "tip");
4342 pnode
= zalloc(sizeof(data_tip_node
));
4346 strlcpy(pnode
->path
, path
);
4347 strlcpy(pnode
->tip
, tip
);
4356 data
->list
= tail
= pnode
;
4364 static int ventoy_parse_menu_class(VTOY_JSON
*json
, void *p
)
4367 const char *path
= NULL
;
4368 const char *class = NULL
;
4369 data_class
*data
= (data_class
*)p
;
4370 data_class_node
*tail
= NULL
;
4371 data_class_node
*pnode
= NULL
;
4372 VTOY_JSON
*node
= NULL
;
4374 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4379 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4381 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4386 type
= class_type_key
;
4387 path
= vtoy_json_get_string_ex(node
->pstChild
, "key");
4390 type
= class_type_dir
;
4391 path
= vtoy_json_get_string_ex(node
->pstChild
, "dir");
4394 type
= class_type_parent
;
4395 path
= vtoy_json_get_string_ex(node
->pstChild
, "parent");
4398 class = vtoy_json_get_string_ex(node
->pstChild
, "class");
4402 pnode
= zalloc(sizeof(data_class_node
));
4406 strlcpy(pnode
->path
, path
);
4407 strlcpy(pnode
->class, class);
4416 data
->list
= tail
= pnode
;
4424 static int ventoy_parse_auto_install(VTOY_JSON
*json
, void *p
)
4432 const char *path
= NULL
;
4433 const char *file
= NULL
;
4434 data_auto_install
*data
= (data_auto_install
*)p
;
4435 auto_install_node
*tail
= NULL
;
4436 auto_install_node
*pnode
= NULL
;
4437 path_node
*pathnode
= NULL
;
4438 path_node
*pathtail
= NULL
;
4439 VTOY_JSON
*node
= NULL
;
4440 VTOY_JSON
*filelist
= NULL
;
4441 VTOY_JSON
*filenode
= NULL
;
4443 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4448 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4450 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4456 path
= vtoy_json_get_string_ex(node
->pstChild
, "image");
4459 path
= vtoy_json_get_string_ex(node
->pstChild
, "parent");
4467 file
= vtoy_json_get_string_ex(node
->pstChild
, "template");
4470 pnode
= zalloc(sizeof(auto_install_node
));
4475 strlcpy(pnode
->path
, path
);
4477 pathnode
= zalloc(sizeof(path_node
));
4480 strlcpy(pathnode
->path
, file
);
4481 pnode
->list
= pathnode
;
4495 data
->list
= tail
= pnode
;
4503 timeouten
= autoselen
= 0;
4504 if (JSON_SUCCESS
== vtoy_json_get_int(node
->pstChild
, "timeout", &timeout
))
4508 if (JSON_SUCCESS
== vtoy_json_get_int(node
->pstChild
, "autosel", &autosel
))
4513 filelist
= vtoy_json_find_item(node
->pstChild
, JSON_TYPE_ARRAY
, "template");
4519 pnode
= zalloc(sizeof(auto_install_node
));
4526 pnode
->autoselen
= autoselen
;
4527 pnode
->timeouten
= timeouten
;
4528 pnode
->timeout
= timeout
;
4529 pnode
->autosel
= autosel
;
4530 strlcpy(pnode
->path
, path
);
4533 for (filenode
= filelist
->pstChild
; filenode
; filenode
= filenode
->pstNext
)
4535 if (filenode
->enDataType
!= JSON_TYPE_STRING
)
4540 pathnode
= zalloc(sizeof(path_node
));
4544 strlcpy(pathnode
->path
, filenode
->unData
.pcStrVal
);
4548 pathtail
->next
= pathnode
;
4549 pathtail
= pathnode
;
4553 pnode
->list
= pathtail
= pathnode
;
4564 if (pnode
->autoselen
&& pnode
->autosel
> count
)
4576 data
->list
= tail
= pnode
;
4583 static int ventoy_parse_persistence(VTOY_JSON
*json
, void *p
)
4590 const char *path
= NULL
;
4591 const char *file
= NULL
;
4592 data_persistence
*data
= (data_persistence
*)p
;
4593 persistence_node
*tail
= NULL
;
4594 persistence_node
*pnode
= NULL
;
4595 path_node
*pathnode
= NULL
;
4596 path_node
*pathtail
= NULL
;
4597 VTOY_JSON
*node
= NULL
;
4598 VTOY_JSON
*filelist
= NULL
;
4599 VTOY_JSON
*filenode
= NULL
;
4601 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4606 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4608 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4613 path
= vtoy_json_get_string_ex(node
->pstChild
, "image");
4619 file
= vtoy_json_get_string_ex(node
->pstChild
, "backend");
4622 pnode
= zalloc(sizeof(persistence_node
));
4627 strlcpy(pnode
->path
, path
);
4629 pathnode
= zalloc(sizeof(path_node
));
4632 strlcpy(pathnode
->path
, file
);
4633 pnode
->list
= pathnode
;
4647 data
->list
= tail
= pnode
;
4655 timeouten
= autoselen
= 0;
4656 if (JSON_SUCCESS
== vtoy_json_get_int(node
->pstChild
, "timeout", &timeout
))
4660 if (JSON_SUCCESS
== vtoy_json_get_int(node
->pstChild
, "autosel", &autosel
))
4665 filelist
= vtoy_json_find_item(node
->pstChild
, JSON_TYPE_ARRAY
, "backend");
4671 pnode
= zalloc(sizeof(persistence_node
));
4678 pnode
->autoselen
= autoselen
;
4679 pnode
->timeouten
= timeouten
;
4680 pnode
->timeout
= timeout
;
4681 pnode
->autosel
= autosel
;
4682 strlcpy(pnode
->path
, path
);
4685 for (filenode
= filelist
->pstChild
; filenode
; filenode
= filenode
->pstNext
)
4687 if (filenode
->enDataType
!= JSON_TYPE_STRING
)
4692 pathnode
= zalloc(sizeof(path_node
));
4696 strlcpy(pathnode
->path
, filenode
->unData
.pcStrVal
);
4700 pathtail
->next
= pathnode
;
4701 pathtail
= pathnode
;
4705 pnode
->list
= pathtail
= pathnode
;
4716 if (pnode
->autoselen
&& pnode
->autosel
> count
)
4728 data
->list
= tail
= pnode
;
4735 static int ventoy_parse_injection(VTOY_JSON
*json
, void *p
)
4738 const char *path
= NULL
;
4739 const char *archive
= NULL
;
4740 data_injection
*data
= (data_injection
*)p
;
4741 injection_node
*tail
= NULL
;
4742 injection_node
*pnode
= NULL
;
4743 VTOY_JSON
*node
= NULL
;
4745 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4750 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4752 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4758 path
= vtoy_json_get_string_ex(node
->pstChild
, "image");
4761 path
= vtoy_json_get_string_ex(node
->pstChild
, "parent");
4764 archive
= vtoy_json_get_string_ex(node
->pstChild
, "archive");
4766 if (path
&& archive
)
4768 pnode
= zalloc(sizeof(injection_node
));
4772 strlcpy(pnode
->path
, path
);
4773 strlcpy(pnode
->archive
, archive
);
4782 data
->list
= tail
= pnode
;
4790 static int ventoy_parse_conf_replace(VTOY_JSON
*json
, void *p
)
4793 const char *path
= NULL
;
4794 const char *org
= NULL
;
4795 const char *new = NULL
;
4796 data_conf_replace
*data
= (data_conf_replace
*)p
;
4797 conf_replace_node
*tail
= NULL
;
4798 conf_replace_node
*pnode
= NULL
;
4799 VTOY_JSON
*node
= NULL
;
4801 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4806 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4808 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4813 path
= vtoy_json_get_string_ex(node
->pstChild
, "iso");
4814 org
= vtoy_json_get_string_ex(node
->pstChild
, "org");
4815 new = vtoy_json_get_string_ex(node
->pstChild
, "new");
4818 vtoy_json_get_int(node
->pstChild
, "img", &img
);
4820 if (path
&& org
&& new)
4822 pnode
= zalloc(sizeof(conf_replace_node
));
4825 strlcpy(pnode
->path
, path
);
4826 strlcpy(pnode
->org
, org
);
4827 strlcpy(pnode
->new, new);
4840 data
->list
= tail
= pnode
;
4848 static int ventoy_parse_password(VTOY_JSON
*json
, void *p
)
4851 const char *bootpwd
= NULL
;
4852 const char *isopwd
= NULL
;
4853 const char *wimpwd
= NULL
;
4854 const char *imgpwd
= NULL
;
4855 const char *efipwd
= NULL
;
4856 const char *vhdpwd
= NULL
;
4857 const char *vtoypwd
= NULL
;
4858 const char *path
= NULL
;
4859 const char *pwd
= NULL
;
4860 data_password
*data
= (data_password
*)p
;
4861 menu_password
*tail
= NULL
;
4862 menu_password
*pnode
= NULL
;
4863 VTOY_JSON
*node
= NULL
;
4864 VTOY_JSON
*menupwd
= NULL
;
4866 if (json
->enDataType
!= JSON_TYPE_OBJECT
)
4871 bootpwd
= vtoy_json_get_string_ex(json
->pstChild
, "bootpwd");
4872 isopwd
= vtoy_json_get_string_ex(json
->pstChild
, "isopwd");
4873 wimpwd
= vtoy_json_get_string_ex(json
->pstChild
, "wimpwd");
4874 imgpwd
= vtoy_json_get_string_ex(json
->pstChild
, "imgpwd");
4875 efipwd
= vtoy_json_get_string_ex(json
->pstChild
, "efipwd");
4876 vhdpwd
= vtoy_json_get_string_ex(json
->pstChild
, "vhdpwd");
4877 vtoypwd
= vtoy_json_get_string_ex(json
->pstChild
, "vtoypwd");
4880 if (bootpwd
) strlcpy(data
->bootpwd
, bootpwd
);
4881 if (isopwd
) strlcpy(data
->isopwd
, isopwd
);
4882 if (wimpwd
) strlcpy(data
->wimpwd
, wimpwd
);
4883 if (imgpwd
) strlcpy(data
->imgpwd
, imgpwd
);
4884 if (efipwd
) strlcpy(data
->efipwd
, efipwd
);
4885 if (vhdpwd
) strlcpy(data
->vhdpwd
, vhdpwd
);
4886 if (vtoypwd
) strlcpy(data
->vtoypwd
, vtoypwd
);
4889 menupwd
= vtoy_json_find_item(json
->pstChild
, JSON_TYPE_ARRAY
, "menupwd");
4895 for (node
= menupwd
->pstChild
; node
; node
= node
->pstNext
)
4897 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4903 path
= vtoy_json_get_string_ex(node
->pstChild
, "file");
4906 path
= vtoy_json_get_string_ex(node
->pstChild
, "parent");
4909 pwd
= vtoy_json_get_string_ex(node
->pstChild
, "pwd");
4913 pnode
= zalloc(sizeof(menu_password
));
4917 strlcpy(pnode
->path
, path
);
4918 strlcpy(pnode
->pwd
, pwd
);
4927 data
->list
= tail
= pnode
;
4936 static int ventoy_parse_image_list_real(VTOY_JSON
*json
, int type
, void *p
)
4938 VTOY_JSON
*node
= NULL
;
4939 data_image_list
*data
= (data_image_list
*)p
;
4940 path_node
*tail
= NULL
;
4941 path_node
*pnode
= NULL
;
4943 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4950 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4952 if (node
->enDataType
== JSON_TYPE_STRING
)
4954 pnode
= zalloc(sizeof(path_node
));
4957 strlcpy(pnode
->path
, node
->unData
.pcStrVal
);
4965 data
->list
= tail
= pnode
;
4973 static int ventoy_parse_image_blacklist(VTOY_JSON
*json
, void *p
)
4975 return ventoy_parse_image_list_real(json
, 1, p
);
4977 static int ventoy_parse_image_list(VTOY_JSON
*json
, void *p
)
4979 return ventoy_parse_image_list_real(json
, 0, p
);
4982 static int ventoy_parse_auto_memdisk(VTOY_JSON
*json
, void *p
)
4984 VTOY_JSON
*node
= NULL
;
4985 data_auto_memdisk
*data
= (data_auto_memdisk
*)p
;
4986 path_node
*tail
= NULL
;
4987 path_node
*pnode
= NULL
;
4989 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4994 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4996 if (node
->enDataType
== JSON_TYPE_STRING
)
4998 pnode
= zalloc(sizeof(path_node
));
5001 strlcpy(pnode
->path
, node
->unData
.pcStrVal
);
5009 data
->list
= tail
= pnode
;
5017 static int ventoy_parse_dud(VTOY_JSON
*json
, void *p
)
5020 const char *path
= NULL
;
5021 const char *file
= NULL
;
5022 data_dud
*data
= (data_dud
*)p
;
5023 dud_node
*tail
= NULL
;
5024 dud_node
*pnode
= NULL
;
5025 path_node
*pathnode
= NULL
;
5026 path_node
*pathtail
= NULL
;
5027 VTOY_JSON
*node
= NULL
;
5028 VTOY_JSON
*filelist
= NULL
;
5029 VTOY_JSON
*filenode
= NULL
;
5031 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
5036 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
5038 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
5043 path
= vtoy_json_get_string_ex(node
->pstChild
, "image");
5049 file
= vtoy_json_get_string_ex(node
->pstChild
, "dud");
5052 pnode
= zalloc(sizeof(dud_node
));
5055 strlcpy(pnode
->path
, path
);
5057 pathnode
= zalloc(sizeof(path_node
));
5060 strlcpy(pathnode
->path
, file
);
5061 pnode
->list
= pathnode
;
5075 data
->list
= tail
= pnode
;
5082 filelist
= vtoy_json_find_item(node
->pstChild
, JSON_TYPE_ARRAY
, "dud");
5088 pnode
= zalloc(sizeof(dud_node
));
5094 strlcpy(pnode
->path
, path
);
5096 for (filenode
= filelist
->pstChild
; filenode
; filenode
= filenode
->pstNext
)
5098 if (filenode
->enDataType
!= JSON_TYPE_STRING
)
5103 pathnode
= zalloc(sizeof(path_node
));
5106 strlcpy(pathnode
->path
, filenode
->unData
.pcStrVal
);
5111 pathtail
->next
= pathnode
;
5112 pathtail
= pathnode
;
5116 pnode
->list
= pathtail
= pathnode
;
5134 data
->list
= tail
= pnode
;
5148 static int ventoy_load_old_json(const char *filename
)
5153 char *buffer
= NULL
;
5154 unsigned char *start
= NULL
;
5155 VTOY_JSON
*json
= NULL
;
5156 VTOY_JSON
*node
= NULL
;
5157 VTOY_JSON
*next
= NULL
;
5159 ret
= ventoy_read_file_to_buf(filename
, 4, (void **)&buffer
, &buflen
);
5162 vlog("Failed to read old ventoy.json file.\n");
5167 start
= (unsigned char *)buffer
;
5169 if (start
[0] == 0xef && start
[1] == 0xbb && start
[2] == 0xbf)
5173 else if ((start
[0] == 0xff && start
[1] == 0xfe) || (start
[0] == 0xfe && start
[1] == 0xff))
5175 vlog("ventoy.json is in UCS-2 encoding, ignore it.\n");
5180 json
= vtoy_json_create();
5187 if (vtoy_json_parse_ex(json
, buffer
+ offset
, buflen
- offset
) == JSON_SUCCESS
)
5189 vlog("parse ventoy.json success\n");
5191 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
5192 for (next
= node
->pstNext
; next
; next
= next
->pstNext
)
5194 if (node
->pcName
&& next
->pcName
&& strcmp(node
->pcName
, next
->pcName
) == 0)
5196 vlog("ventoy.json contains duplicate key <%s>.\n", node
->pcName
);
5197 g_sysinfo
.invalid_config
= 1;
5203 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
5205 ventoy_parse_json(control
);
5206 ventoy_parse_json(theme
);
5207 ventoy_parse_json(menu_alias
);
5208 ventoy_parse_json(menu_tip
);
5209 ventoy_parse_json(menu_class
);
5210 ventoy_parse_json(auto_install
);
5211 ventoy_parse_json(persistence
);
5212 ventoy_parse_json(injection
);
5213 ventoy_parse_json(conf_replace
);
5214 ventoy_parse_json(password
);
5215 ventoy_parse_json(image_list
);
5216 ventoy_parse_json(image_blacklist
);
5217 ventoy_parse_json(auto_memdisk
);
5218 ventoy_parse_json(dud
);
5223 vlog("ventoy.json has syntax error.\n");
5224 g_sysinfo
.syntax_error
= 1;
5229 vtoy_json_destroy(json
);
5236 int ventoy_http_start(const char *ip
, const char *port
)
5242 char backupname
[128];
5243 struct mg_callbacks callbacks
;
5244 const char *options
[] =
5246 "listening_ports", "24681",
5247 "document_root", "www",
5248 "index_files", "index.html",
5249 "num_threads", "16",
5250 "error_log_file", LOG_FILE
,
5251 "request_timeout_ms", "10000",
5255 for (i
= 0; i
<= bios_max
; i
++)
5257 ventoy_data_default_control(g_data_control
+ i
);
5258 ventoy_data_default_theme(g_data_theme
+ i
);
5259 ventoy_data_default_menu_alias(g_data_menu_alias
+ i
);
5260 ventoy_data_default_menu_class(g_data_menu_class
+ i
);
5261 ventoy_data_default_menu_tip(g_data_menu_tip
+ i
);
5262 ventoy_data_default_auto_install(g_data_auto_install
+ i
);
5263 ventoy_data_default_persistence(g_data_persistence
+ i
);
5264 ventoy_data_default_injection(g_data_injection
+ i
);
5265 ventoy_data_default_conf_replace(g_data_conf_replace
+ i
);
5266 ventoy_data_default_password(g_data_password
+ i
);
5267 ventoy_data_default_image_list(g_data_image_list
+ i
);
5268 ventoy_data_default_auto_memdisk(g_data_auto_memdisk
+ i
);
5269 ventoy_data_default_dud(g_data_dud
+ i
);
5272 ventoy_get_json_path(filename
, backupname
);
5273 if (ventoy_is_file_exist("%s", filename
))
5275 ventoy_copy_file(filename
, backupname
);
5276 ret
= ventoy_load_old_json(filename
);
5279 ventoy_data_real_save_all(0);
5284 scnprintf(addr
, sizeof(addr
), "%s:%s", ip
, port
);
5287 memset(&callbacks
, 0, sizeof(callbacks
));
5288 callbacks
.begin_request
= ventoy_request_handler
;
5290 callbacks
.open_file
= ventoy_web_openfile
;
5292 g_ventoy_http_ctx
= mg_start(&callbacks
, NULL
, options
);
5294 ventoy_start_writeback_thread(ventoy_http_writeback
);
5296 return g_ventoy_http_ctx
? 0 : 1;
5299 int ventoy_http_stop(void)
5301 if (g_ventoy_http_ctx
)
5303 mg_stop(g_ventoy_http_ctx
);
5306 ventoy_stop_writeback_thread();
5310 int ventoy_http_init(void)
5315 char *Buffer
= NULL
;
5318 ventoy_read_file_to_buf("www/menulist", 4, (void **)&Buffer
, &BufLen
);
5321 for (i
= 0; i
< BufLen
/ 5; i
++)
5323 memcpy(g_ventoy_menu_lang
[i
], Buffer
+ i
* 5, 5);
5324 g_ventoy_menu_lang
[i
][5] = 0;
5331 file
= ventoy_tar_find_file("www/menulist");
5334 for (i
= 0; i
< file
->size
/ 5; i
++)
5336 memcpy(g_ventoy_menu_lang
[i
], (char *)(file
->addr
) + i
* 5, 5);
5337 g_ventoy_menu_lang
[i
][5] = 0;
5342 if (!g_pub_json_buffer
)
5344 g_pub_json_buffer
= malloc(JSON_BUF_MAX
* 2);
5345 g_pub_save_buffer
= g_pub_json_buffer
+ JSON_BUF_MAX
;
5349 pthread_mutex_init(&g_api_mutex
, NULL
);
5353 void ventoy_http_exit(void)
5355 check_free(g_pub_json_buffer
);
5356 g_pub_json_buffer
= NULL
;
5357 g_pub_save_buffer
= NULL
;
5359 pthread_mutex_destroy(&g_api_mutex
);