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 const char *realpath
= NULL
;
922 path_node
*node
= NULL
;
923 path_node
*cur
= NULL
;
924 data_theme
*data
= NULL
;
925 char pathbuf
[MAX_PATH
];
927 vtoy_json_get_int(json
, "index", &index
);
928 data
= g_data_theme
+ index
;
930 path
= VTOY_JSON_STR_EX("path");
933 realpath
= ventoy_real_path(path
);
934 scnprintf(pathbuf
, sizeof(pathbuf
), "%s", realpath
);
936 for (node
= data
->filelist
; node
; node
= node
->next
)
938 realpath
= ventoy_real_path(node
->path
);
939 if (strcmp(pathbuf
, realpath
) == 0)
941 ventoy_json_result(conn
, VTOY_JSON_DUPLICATE
);
946 node
= zalloc(sizeof(path_node
));
949 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
951 vtoy_list_add(data
->filelist
, cur
, node
);
955 ret
= ventoy_data_save_all();
957 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
961 static int ventoy_api_theme_del_file(struct mg_connection
*conn
, VTOY_JSON
*json
)
965 const char *path
= NULL
;
966 path_node
*node
= NULL
;
967 path_node
*last
= NULL
;
968 data_theme
*data
= NULL
;
970 vtoy_json_get_int(json
, "index", &index
);
971 data
= g_data_theme
+ index
;
973 path
= VTOY_JSON_STR_EX("path");
976 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
978 vtoy_list_free(path_node
, data
->filelist
);
982 vtoy_list_del(last
, node
, data
->filelist
, path
);
986 ret
= ventoy_data_save_all();
988 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
993 static int ventoy_api_theme_add_font(struct mg_connection
*conn
, VTOY_JSON
*json
)
997 const char *path
= NULL
;
998 const char *realpath
= NULL
;
999 path_node
*node
= NULL
;
1000 path_node
*cur
= NULL
;
1001 data_theme
*data
= NULL
;
1002 char pathbuf
[MAX_PATH
];
1004 vtoy_json_get_int(json
, "index", &index
);
1005 data
= g_data_theme
+ index
;
1007 path
= VTOY_JSON_STR_EX("path");
1010 realpath
= ventoy_real_path(path
);
1011 scnprintf(pathbuf
, sizeof(pathbuf
), "%s", realpath
);
1013 for (node
= data
->fontslist
; node
; node
= node
->next
)
1015 realpath
= ventoy_real_path(node
->path
);
1016 if (strcmp(pathbuf
, realpath
) == 0)
1018 ventoy_json_result(conn
, VTOY_JSON_DUPLICATE
);
1023 node
= zalloc(sizeof(path_node
));
1026 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
1027 vtoy_list_add(data
->fontslist
, cur
, node
);
1031 ret
= ventoy_data_save_all();
1033 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1038 static int ventoy_api_theme_del_font(struct mg_connection
*conn
, VTOY_JSON
*json
)
1042 const char *path
= NULL
;
1043 path_node
*node
= NULL
;
1044 path_node
*last
= NULL
;
1045 data_theme
*data
= NULL
;
1047 vtoy_json_get_int(json
, "index", &index
);
1048 data
= g_data_theme
+ index
;
1050 path
= VTOY_JSON_STR_EX("path");
1053 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
1055 vtoy_list_free(path_node
, data
->fontslist
);
1059 vtoy_list_del(last
, node
, data
->fontslist
, path
);
1063 ret
= ventoy_data_save_all();
1065 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1072 void ventoy_data_default_menu_alias(data_alias
*data
)
1074 memset(data
, 0, sizeof(data_alias
));
1077 int ventoy_data_cmp_menu_alias(data_alias
*data1
, data_alias
*data2
)
1079 data_alias_node
*list1
= NULL
;
1080 data_alias_node
*list2
= NULL
;
1082 if (NULL
== data1
->list
&& NULL
== data2
->list
)
1086 else if (data1
->list
&& data2
->list
)
1088 list1
= data1
->list
;
1089 list2
= data2
->list
;
1091 while (list1
&& list2
)
1093 if ((list1
->type
!= list2
->type
) ||
1094 strcmp(list1
->path
, list2
->path
) ||
1095 strcmp(list1
->alias
, list2
->alias
))
1100 list1
= list1
->next
;
1101 list2
= list2
->next
;
1104 if (list1
== NULL
&& list2
== NULL
)
1119 int ventoy_data_save_menu_alias(data_alias
*data
, const char *title
, char *buf
, int buflen
)
1122 data_alias_node
*node
= NULL
;
1124 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1126 VTOY_JSON_FMT_KEY_L(L1
, title
);
1127 VTOY_JSON_FMT_ARY_BEGIN_N();
1129 for (node
= data
->list
; node
; node
= node
->next
)
1131 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
1133 if (node
->type
== path_type_file
)
1135 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "image", node
->path
);
1139 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "dir", node
->path
);
1142 VTOY_JSON_FMT_STRN_EX_LN(L3
, "alias", node
->alias
);
1144 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
1147 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
1148 VTOY_JSON_FMT_END(pos
);
1154 int ventoy_data_json_menu_alias(data_alias
*data
, char *buf
, int buflen
)
1158 data_alias_node
*node
= NULL
;
1160 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1161 VTOY_JSON_FMT_ARY_BEGIN();
1163 for (node
= data
->list
; node
; node
= node
->next
)
1165 VTOY_JSON_FMT_OBJ_BEGIN();
1167 VTOY_JSON_FMT_UINT("type", node
->type
);
1168 VTOY_JSON_FMT_STRN("path", node
->path
);
1169 if (node
->type
== path_type_file
)
1171 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
1175 valid
= ventoy_is_directory_exist("%s%s", g_cur_dir
, node
->path
);
1178 VTOY_JSON_FMT_SINT("valid", valid
);
1179 VTOY_JSON_FMT_STRN("alias", node
->alias
);
1181 VTOY_JSON_FMT_OBJ_ENDEX();
1184 VTOY_JSON_FMT_ARY_END();
1185 VTOY_JSON_FMT_END(pos
);
1190 static int ventoy_api_get_alias(struct mg_connection
*conn
, VTOY_JSON
*json
)
1192 api_get_func(conn
, json
, menu_alias
);
1196 static int ventoy_api_save_alias(struct mg_connection
*conn
, VTOY_JSON
*json
)
1199 ret
= ventoy_data_save_all();
1201 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1205 static int ventoy_api_alias_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
1209 int type
= path_type_file
;
1210 const char *path
= NULL
;
1211 const char *alias
= NULL
;
1212 data_alias_node
*node
= NULL
;
1213 data_alias_node
*cur
= NULL
;
1214 data_alias
*data
= NULL
;
1216 vtoy_json_get_int(json
, "index", &index
);
1217 data
= g_data_menu_alias
+ index
;
1219 vtoy_json_get_int(json
, "type", &type
);
1221 path
= VTOY_JSON_STR_EX("path");
1222 alias
= VTOY_JSON_STR_EX("alias");
1225 node
= zalloc(sizeof(data_alias_node
));
1229 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
1230 scnprintf(node
->alias
, sizeof(node
->alias
), "%s", alias
);
1232 vtoy_list_add(data
->list
, cur
, node
);
1236 ret
= ventoy_data_save_all();
1238 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1242 static int ventoy_api_alias_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
1246 const char *path
= NULL
;
1247 data_alias_node
*last
= NULL
;
1248 data_alias_node
*node
= NULL
;
1249 data_alias
*data
= NULL
;
1251 vtoy_json_get_int(json
, "index", &index
);
1252 data
= g_data_menu_alias
+ index
;
1254 path
= VTOY_JSON_STR_EX("path");
1257 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
1259 vtoy_list_free(data_alias_node
, data
->list
);
1263 vtoy_list_del(last
, node
, data
->list
, path
);
1267 ret
= ventoy_data_save_all();
1269 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1276 void ventoy_data_default_menu_tip(data_tip
*data
)
1278 memset(data
, 0, sizeof(data_tip
));
1280 scnprintf(data
->left
, sizeof(data
->left
), "10%%");
1281 scnprintf(data
->top
, sizeof(data
->top
), "81%%");
1282 scnprintf(data
->color
, sizeof(data
->color
), "%s", "blue");
1285 int ventoy_data_cmp_menu_tip(data_tip
*data1
, data_tip
*data2
)
1287 data_tip_node
*list1
= NULL
;
1288 data_tip_node
*list2
= NULL
;
1290 if (strcmp(data1
->left
, data2
->left
) || strcmp(data1
->top
, data2
->top
) || strcmp(data1
->color
, data2
->color
))
1295 if (NULL
== data1
->list
&& NULL
== data2
->list
)
1299 else if (data1
->list
&& data2
->list
)
1301 list1
= data1
->list
;
1302 list2
= data2
->list
;
1304 while (list1
&& list2
)
1306 if ((list1
->type
!= list2
->type
) ||
1307 strcmp(list1
->path
, list2
->path
) ||
1308 strcmp(list1
->tip
, list2
->tip
))
1313 list1
= list1
->next
;
1314 list2
= list2
->next
;
1317 if (list1
== NULL
&& list2
== NULL
)
1332 int ventoy_data_save_menu_tip(data_tip
*data
, const char *title
, char *buf
, int buflen
)
1335 data_tip_node
*node
= NULL
;
1336 data_tip
*def
= g_data_menu_tip
+ bios_max
;
1338 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1339 VTOY_JSON_FMT_KEY_L(L1
, title
);
1340 VTOY_JSON_FMT_OBJ_BEGIN_N();
1342 VTOY_JSON_FMT_DIFF_STRN(L2
, "left", left
);
1343 VTOY_JSON_FMT_DIFF_STRN(L2
, "top", top
);
1344 VTOY_JSON_FMT_DIFF_STRN(L2
, "color", color
);
1348 VTOY_JSON_FMT_KEY_L(L2
, "tips");
1349 VTOY_JSON_FMT_ARY_BEGIN_N();
1351 for (node
= data
->list
; node
; node
= node
->next
)
1353 VTOY_JSON_FMT_OBJ_BEGIN_LN(L3
);
1355 if (node
->type
== path_type_file
)
1357 VTOY_JSON_FMT_STRN_PATH_LN(L4
, "image", node
->path
);
1361 VTOY_JSON_FMT_STRN_PATH_LN(L4
, "dir", node
->path
);
1363 VTOY_JSON_FMT_STRN_EX_LN(L4
, "tip", node
->tip
);
1365 VTOY_JSON_FMT_OBJ_ENDEX_LN(L3
);
1368 VTOY_JSON_FMT_ARY_ENDEX_LN(L2
);
1371 VTOY_JSON_FMT_OBJ_ENDEX_LN(L1
);
1373 VTOY_JSON_FMT_END(pos
);
1379 int ventoy_data_json_menu_tip(data_tip
*data
, char *buf
, int buflen
)
1383 data_tip_node
*node
= NULL
;
1385 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1387 VTOY_JSON_FMT_OBJ_BEGIN();
1389 VTOY_JSON_FMT_STRN("left", data
->left
);
1390 VTOY_JSON_FMT_STRN("top", data
->top
);
1391 VTOY_JSON_FMT_STRN("color", data
->color
);
1393 VTOY_JSON_FMT_KEY("tips");
1394 VTOY_JSON_FMT_ARY_BEGIN();
1396 for (node
= data
->list
; node
; node
= node
->next
)
1398 VTOY_JSON_FMT_OBJ_BEGIN();
1400 VTOY_JSON_FMT_UINT("type", node
->type
);
1401 VTOY_JSON_FMT_STRN("path", node
->path
);
1402 if (node
->type
== path_type_file
)
1404 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
1408 valid
= ventoy_is_directory_exist("%s%s", g_cur_dir
, node
->path
);
1411 VTOY_JSON_FMT_SINT("valid", valid
);
1412 VTOY_JSON_FMT_STRN("tip", node
->tip
);
1414 VTOY_JSON_FMT_OBJ_ENDEX();
1417 VTOY_JSON_FMT_ARY_ENDEX();
1419 VTOY_JSON_FMT_OBJ_END();
1420 VTOY_JSON_FMT_END(pos
);
1425 static int ventoy_api_get_tip(struct mg_connection
*conn
, VTOY_JSON
*json
)
1427 api_get_func(conn
, json
, menu_tip
);
1431 static int ventoy_api_save_tip(struct mg_connection
*conn
, VTOY_JSON
*json
)
1435 data_tip
*data
= NULL
;
1437 vtoy_json_get_int(json
, "index", &index
);
1438 data
= g_data_menu_tip
+ index
;
1440 VTOY_JSON_STR("left", data
->left
);
1441 VTOY_JSON_STR("top", data
->top
);
1442 VTOY_JSON_STR("color", data
->color
);
1444 ret
= ventoy_data_save_all();
1446 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1450 static int ventoy_api_tip_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
1454 int type
= path_type_file
;
1455 const char *path
= NULL
;
1456 const char *tip
= NULL
;
1457 data_tip_node
*node
= NULL
;
1458 data_tip_node
*cur
= NULL
;
1459 data_tip
*data
= NULL
;
1461 vtoy_json_get_int(json
, "index", &index
);
1462 data
= g_data_menu_tip
+ index
;
1464 vtoy_json_get_int(json
, "type", &type
);
1466 path
= VTOY_JSON_STR_EX("path");
1467 tip
= VTOY_JSON_STR_EX("tip");
1470 node
= zalloc(sizeof(data_tip_node
));
1474 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
1475 scnprintf(node
->tip
, sizeof(node
->tip
), "%s", tip
);
1477 vtoy_list_add(data
->list
, cur
, node
);
1481 ret
= ventoy_data_save_all();
1483 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1487 static int ventoy_api_tip_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
1491 const char *path
= NULL
;
1492 data_tip_node
*last
= NULL
;
1493 data_tip_node
*node
= NULL
;
1494 data_tip
*data
= NULL
;
1496 vtoy_json_get_int(json
, "index", &index
);
1497 data
= g_data_menu_tip
+ index
;
1499 path
= VTOY_JSON_STR_EX("path");
1502 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
1504 vtoy_list_free(data_tip_node
, data
->list
);
1508 vtoy_list_del(last
, node
, data
->list
, path
);
1512 ret
= ventoy_data_save_all();
1514 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1521 void ventoy_data_default_menu_class(data_class
*data
)
1523 memset(data
, 0, sizeof(data_class
));
1526 int ventoy_data_cmp_menu_class(data_class
*data1
, data_class
*data2
)
1528 data_class_node
*list1
= NULL
;
1529 data_class_node
*list2
= NULL
;
1531 if (NULL
== data1
->list
&& NULL
== data2
->list
)
1535 else if (data1
->list
&& data2
->list
)
1537 list1
= data1
->list
;
1538 list2
= data2
->list
;
1540 while (list1
&& list2
)
1542 if ((list1
->type
!= list2
->type
) ||
1543 strcmp(list1
->path
, list2
->path
) ||
1544 strcmp(list1
->class, list2
->class))
1549 list1
= list1
->next
;
1550 list2
= list2
->next
;
1553 if (list1
== NULL
&& list2
== NULL
)
1568 int ventoy_data_save_menu_class(data_class
*data
, const char *title
, char *buf
, int buflen
)
1571 data_class_node
*node
= NULL
;
1573 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1575 VTOY_JSON_FMT_KEY_L(L1
, title
);
1576 VTOY_JSON_FMT_ARY_BEGIN_N();
1578 for (node
= data
->list
; node
; node
= node
->next
)
1580 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
1582 if (node
->type
== class_type_key
)
1584 VTOY_JSON_FMT_STRN_LN(L3
, "key", node
->path
);
1586 else if (node
->type
== class_type_dir
)
1588 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "dir", node
->path
);
1592 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "parent", node
->path
);
1594 VTOY_JSON_FMT_STRN_LN(L3
, "class", node
->class);
1596 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
1599 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
1600 VTOY_JSON_FMT_END(pos
);
1606 int ventoy_data_json_menu_class(data_class
*data
, char *buf
, int buflen
)
1610 data_class_node
*node
= NULL
;
1612 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1613 VTOY_JSON_FMT_ARY_BEGIN();
1615 for (node
= data
->list
; node
; node
= node
->next
)
1617 VTOY_JSON_FMT_OBJ_BEGIN();
1619 VTOY_JSON_FMT_UINT("type", node
->type
);
1620 VTOY_JSON_FMT_STRN("path", node
->path
);
1622 if (node
->type
== class_type_key
)
1628 valid
= ventoy_is_directory_exist("%s%s", g_cur_dir
, node
->path
);
1630 VTOY_JSON_FMT_SINT("valid", valid
);
1632 VTOY_JSON_FMT_STRN("class", node
->class);
1634 VTOY_JSON_FMT_OBJ_ENDEX();
1637 VTOY_JSON_FMT_ARY_END();
1638 VTOY_JSON_FMT_END(pos
);
1644 static int ventoy_api_get_class(struct mg_connection
*conn
, VTOY_JSON
*json
)
1646 api_get_func(conn
, json
, menu_class
);
1650 static int ventoy_api_save_class(struct mg_connection
*conn
, VTOY_JSON
*json
)
1653 ret
= ventoy_data_save_all();
1655 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1659 static int ventoy_api_class_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
1663 int type
= class_type_key
;
1664 const char *path
= NULL
;
1665 const char *class = NULL
;
1666 data_class_node
*node
= NULL
;
1667 data_class_node
*cur
= NULL
;
1668 data_class
*data
= NULL
;
1670 vtoy_json_get_int(json
, "index", &index
);
1671 data
= g_data_menu_class
+ index
;
1673 vtoy_json_get_int(json
, "type", &type
);
1675 path
= VTOY_JSON_STR_EX("path");
1676 class = VTOY_JSON_STR_EX("class");
1679 node
= zalloc(sizeof(data_class_node
));
1684 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
1685 scnprintf(node
->class, sizeof(node
->class), "%s", class);
1687 vtoy_list_add(data
->list
, cur
, node
);
1691 ret
= ventoy_data_save_all();
1693 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1697 static int ventoy_api_class_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
1701 const char *path
= NULL
;
1702 data_class_node
*last
= NULL
;
1703 data_class_node
*node
= NULL
;
1704 data_class
*data
= NULL
;
1706 vtoy_json_get_int(json
, "index", &index
);
1707 data
= g_data_menu_class
+ index
;
1709 path
= VTOY_JSON_STR_EX("path");
1712 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
1714 vtoy_list_free(data_class_node
, data
->list
);
1718 vtoy_list_del(last
, node
, data
->list
, path
);
1722 ret
= ventoy_data_save_all();
1724 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1731 void ventoy_data_default_auto_memdisk(data_auto_memdisk
*data
)
1733 memset(data
, 0, sizeof(data_auto_memdisk
));
1736 int ventoy_data_cmp_auto_memdisk(data_auto_memdisk
*data1
, data_auto_memdisk
*data2
)
1738 return ventoy_path_list_cmp(data1
->list
, data2
->list
);
1741 int ventoy_data_save_auto_memdisk(data_auto_memdisk
*data
, const char *title
, char *buf
, int buflen
)
1744 path_node
*node
= NULL
;
1746 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1748 VTOY_JSON_FMT_KEY_L(L1
, title
);
1749 VTOY_JSON_FMT_ARY_BEGIN_N();
1751 for (node
= data
->list
; node
; node
= node
->next
)
1753 VTOY_JSON_FMT_ITEM_PATH_LN(L2
, node
->path
);
1756 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
1757 VTOY_JSON_FMT_END(pos
);
1762 int ventoy_data_json_auto_memdisk(data_auto_memdisk
*data
, char *buf
, int buflen
)
1766 path_node
*node
= NULL
;
1768 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1769 VTOY_JSON_FMT_ARY_BEGIN();
1771 for (node
= data
->list
; node
; node
= node
->next
)
1773 VTOY_JSON_FMT_OBJ_BEGIN();
1775 VTOY_JSON_FMT_STRN("path", node
->path
);
1776 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
1777 VTOY_JSON_FMT_SINT("valid", valid
);
1779 VTOY_JSON_FMT_OBJ_ENDEX();
1782 VTOY_JSON_FMT_ARY_END();
1783 VTOY_JSON_FMT_END(pos
);
1788 static int ventoy_api_get_auto_memdisk(struct mg_connection
*conn
, VTOY_JSON
*json
)
1790 api_get_func(conn
, json
, auto_memdisk
);
1794 static int ventoy_api_save_auto_memdisk(struct mg_connection
*conn
, VTOY_JSON
*json
)
1798 ret
= ventoy_data_save_all();
1800 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1804 static int ventoy_api_auto_memdisk_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
1808 const char *path
= NULL
;
1809 path_node
*node
= NULL
;
1810 path_node
*cur
= NULL
;
1811 data_auto_memdisk
*data
= NULL
;
1813 vtoy_json_get_int(json
, "index", &index
);
1814 data
= g_data_auto_memdisk
+ index
;
1816 path
= VTOY_JSON_STR_EX("path");
1819 node
= zalloc(sizeof(path_node
));
1822 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
1823 vtoy_list_add(data
->list
, cur
, node
);
1827 ret
= ventoy_data_save_all();
1829 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1833 static int ventoy_api_auto_memdisk_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
1837 const char *path
= NULL
;
1838 path_node
*last
= NULL
;
1839 path_node
*node
= NULL
;
1840 data_auto_memdisk
*data
= NULL
;
1842 vtoy_json_get_int(json
, "index", &index
);
1843 data
= g_data_auto_memdisk
+ index
;
1845 path
= VTOY_JSON_STR_EX("path");
1848 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
1850 vtoy_list_free(path_node
, data
->list
);
1854 vtoy_list_del(last
, node
, data
->list
, path
);
1858 ret
= ventoy_data_save_all();
1860 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1867 void ventoy_data_default_image_list(data_image_list
*data
)
1869 memset(data
, 0, sizeof(data_image_list
));
1872 int ventoy_data_cmp_image_list(data_image_list
*data1
, data_image_list
*data2
)
1874 if (data1
->type
!= data2
->type
)
1876 if (data1
->list
|| data2
->list
)
1886 return ventoy_path_list_cmp(data1
->list
, data2
->list
);
1889 int ventoy_data_save_image_list(data_image_list
*data
, const char *title
, char *buf
, int buflen
)
1893 path_node
*node
= NULL
;
1903 prelen
= (int)strlen("image_list");
1905 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1907 if (data
->type
== 0)
1909 scnprintf(newtitle
, sizeof(newtitle
), "image_list%s", title
+ prelen
);
1913 scnprintf(newtitle
, sizeof(newtitle
), "image_blacklist%s", title
+ prelen
);
1915 VTOY_JSON_FMT_KEY_L(L1
, newtitle
);
1917 VTOY_JSON_FMT_ARY_BEGIN_N();
1919 for (node
= data
->list
; node
; node
= node
->next
)
1921 VTOY_JSON_FMT_ITEM_PATH_LN(L2
, node
->path
);
1924 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
1925 VTOY_JSON_FMT_END(pos
);
1930 int ventoy_data_json_image_list(data_image_list
*data
, char *buf
, int buflen
)
1934 path_node
*node
= NULL
;
1936 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1938 VTOY_JSON_FMT_OBJ_BEGIN();
1939 VTOY_JSON_FMT_SINT("type", data
->type
);
1941 VTOY_JSON_FMT_KEY("list");
1942 VTOY_JSON_FMT_ARY_BEGIN();
1944 for (node
= data
->list
; node
; node
= node
->next
)
1946 VTOY_JSON_FMT_OBJ_BEGIN();
1948 VTOY_JSON_FMT_STRN("path", node
->path
);
1949 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
1950 VTOY_JSON_FMT_SINT("valid", valid
);
1952 VTOY_JSON_FMT_OBJ_ENDEX();
1955 VTOY_JSON_FMT_ARY_ENDEX();
1956 VTOY_JSON_FMT_OBJ_END();
1958 VTOY_JSON_FMT_END(pos
);
1963 static int ventoy_api_get_image_list(struct mg_connection
*conn
, VTOY_JSON
*json
)
1965 api_get_func(conn
, json
, image_list
);
1969 static int ventoy_api_save_image_list(struct mg_connection
*conn
, VTOY_JSON
*json
)
1973 data_image_list
*data
= NULL
;
1975 vtoy_json_get_int(json
, "index", &index
);
1976 data
= g_data_image_list
+ index
;
1978 VTOY_JSON_INT("type", data
->type
);
1980 ret
= ventoy_data_save_all();
1982 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1986 static int ventoy_api_image_list_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
1990 const char *path
= NULL
;
1991 path_node
*node
= NULL
;
1992 path_node
*cur
= NULL
;
1993 data_image_list
*data
= NULL
;
1995 vtoy_json_get_int(json
, "index", &index
);
1996 data
= g_data_image_list
+ index
;
1998 path
= VTOY_JSON_STR_EX("path");
2001 node
= zalloc(sizeof(path_node
));
2004 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
2005 vtoy_list_add(data
->list
, cur
, node
);
2009 ret
= ventoy_data_save_all();
2011 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2015 static int ventoy_api_image_list_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
2019 const char *path
= NULL
;
2020 path_node
*last
= NULL
;
2021 path_node
*node
= NULL
;
2022 data_image_list
*data
= NULL
;
2024 vtoy_json_get_int(json
, "index", &index
);
2025 data
= g_data_image_list
+ index
;
2027 path
= VTOY_JSON_STR_EX("path");
2030 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
2032 vtoy_list_free(path_node
, data
->list
);
2036 vtoy_list_del(last
, node
, data
->list
, path
);
2040 ret
= ventoy_data_save_all();
2042 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2049 void ventoy_data_default_password(data_password
*data
)
2051 memset(data
, 0, sizeof(data_password
));
2054 int ventoy_data_cmp_password(data_password
*data1
, data_password
*data2
)
2056 menu_password
*list1
= NULL
;
2057 menu_password
*list2
= NULL
;
2059 if (strcmp(data1
->bootpwd
, data2
->bootpwd
) ||
2060 strcmp(data1
->isopwd
, data2
->isopwd
) ||
2061 strcmp(data1
->wimpwd
, data2
->wimpwd
) ||
2062 strcmp(data1
->vhdpwd
, data2
->vhdpwd
) ||
2063 strcmp(data1
->imgpwd
, data2
->imgpwd
) ||
2064 strcmp(data1
->efipwd
, data2
->efipwd
) ||
2065 strcmp(data1
->vtoypwd
, data2
->vtoypwd
)
2071 if (NULL
== data1
->list
&& NULL
== data2
->list
)
2075 else if (data1
->list
&& data2
->list
)
2077 list1
= data1
->list
;
2078 list2
= data2
->list
;
2080 while (list1
&& list2
)
2082 if ((list1
->type
!= list2
->type
) || strcmp(list1
->path
, list2
->path
))
2087 list1
= list1
->next
;
2088 list2
= list2
->next
;
2091 if (list1
== NULL
&& list2
== NULL
)
2106 int ventoy_data_save_password(data_password
*data
, const char *title
, char *buf
, int buflen
)
2109 menu_password
*node
= NULL
;
2110 data_password
*def
= g_data_password
+ bios_max
;
2112 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2113 VTOY_JSON_FMT_KEY_L(L1
, title
);
2114 VTOY_JSON_FMT_OBJ_BEGIN_N();
2116 VTOY_JSON_FMT_DIFF_STRN(L2
, "bootpwd", bootpwd
);
2117 VTOY_JSON_FMT_DIFF_STRN(L2
, "isopwd", isopwd
);
2118 VTOY_JSON_FMT_DIFF_STRN(L2
, "wimpwd", wimpwd
);
2119 VTOY_JSON_FMT_DIFF_STRN(L2
, "vhdpwd", vhdpwd
);
2120 VTOY_JSON_FMT_DIFF_STRN(L2
, "imgpwd", imgpwd
);
2121 VTOY_JSON_FMT_DIFF_STRN(L2
, "efipwd", efipwd
);
2122 VTOY_JSON_FMT_DIFF_STRN(L2
, "vtoypwd", vtoypwd
);
2126 VTOY_JSON_FMT_KEY_L(L2
, "menupwd");
2127 VTOY_JSON_FMT_ARY_BEGIN_N();
2129 for (node
= data
->list
; node
; node
= node
->next
)
2131 VTOY_JSON_FMT_OBJ_BEGIN_LN(L3
);
2133 if (node
->type
== 0)
2135 VTOY_JSON_FMT_STRN_PATH_LN(L4
, "file", node
->path
);
2139 VTOY_JSON_FMT_STRN_PATH_LN(L4
, "parent", node
->path
);
2141 VTOY_JSON_FMT_STRN_LN(L4
, "pwd", node
->pwd
);
2143 VTOY_JSON_FMT_OBJ_ENDEX_LN(L3
);
2146 VTOY_JSON_FMT_ARY_ENDEX_LN(L2
);
2149 VTOY_JSON_FMT_OBJ_ENDEX_LN(L1
);
2151 VTOY_JSON_FMT_END(pos
);
2157 int ventoy_data_json_password(data_password
*data
, char *buf
, int buflen
)
2161 menu_password
*node
= NULL
;
2163 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2165 VTOY_JSON_FMT_OBJ_BEGIN();
2167 VTOY_JSON_FMT_STRN("bootpwd", data
->bootpwd
);
2168 VTOY_JSON_FMT_STRN("isopwd", data
->isopwd
);
2169 VTOY_JSON_FMT_STRN("wimpwd", data
->wimpwd
);
2170 VTOY_JSON_FMT_STRN("vhdpwd", data
->vhdpwd
);
2171 VTOY_JSON_FMT_STRN("imgpwd", data
->imgpwd
);
2172 VTOY_JSON_FMT_STRN("efipwd", data
->efipwd
);
2173 VTOY_JSON_FMT_STRN("vtoypwd", data
->vtoypwd
);
2175 VTOY_JSON_FMT_KEY("list");
2176 VTOY_JSON_FMT_ARY_BEGIN();
2178 for (node
= data
->list
; node
; node
= node
->next
)
2180 VTOY_JSON_FMT_OBJ_BEGIN();
2182 VTOY_JSON_FMT_SINT("type", node
->type
);
2183 VTOY_JSON_FMT_STRN("path", node
->path
);
2184 if (node
->type
== path_type_file
)
2186 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
2190 valid
= ventoy_is_directory_exist("%s%s", g_cur_dir
, node
->path
);
2193 VTOY_JSON_FMT_SINT("valid", valid
);
2194 VTOY_JSON_FMT_STRN("pwd", node
->pwd
);
2196 VTOY_JSON_FMT_OBJ_ENDEX();
2199 VTOY_JSON_FMT_ARY_ENDEX();
2201 VTOY_JSON_FMT_OBJ_END();
2202 VTOY_JSON_FMT_END(pos
);
2207 static int ventoy_api_get_password(struct mg_connection
*conn
, VTOY_JSON
*json
)
2209 api_get_func(conn
, json
, password
);
2213 static int ventoy_api_save_password(struct mg_connection
*conn
, VTOY_JSON
*json
)
2217 data_password
*data
= NULL
;
2219 vtoy_json_get_int(json
, "index", &index
);
2220 data
= g_data_password
+ index
;
2222 VTOY_JSON_STR("bootpwd", data
->bootpwd
);
2223 VTOY_JSON_STR("isopwd", data
->isopwd
);
2224 VTOY_JSON_STR("wimpwd", data
->wimpwd
);
2225 VTOY_JSON_STR("vhdpwd", data
->vhdpwd
);
2226 VTOY_JSON_STR("imgpwd", data
->imgpwd
);
2227 VTOY_JSON_STR("efipwd", data
->efipwd
);
2228 VTOY_JSON_STR("vtoypwd", data
->vtoypwd
);
2230 ret
= ventoy_data_save_all();
2232 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2236 static int ventoy_api_password_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
2241 const char *path
= NULL
;
2242 const char *pwd
= NULL
;
2243 menu_password
*node
= NULL
;
2244 menu_password
*cur
= NULL
;
2245 data_password
*data
= NULL
;
2247 vtoy_json_get_int(json
, "index", &index
);
2248 data
= g_data_password
+ index
;
2250 vtoy_json_get_int(json
, "type", &type
);
2252 path
= VTOY_JSON_STR_EX("path");
2253 pwd
= VTOY_JSON_STR_EX("pwd");
2256 node
= zalloc(sizeof(menu_password
));
2260 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
2261 scnprintf(node
->pwd
, sizeof(node
->pwd
), "%s", pwd
);
2263 vtoy_list_add(data
->list
, cur
, node
);
2267 ret
= ventoy_data_save_all();
2269 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2273 static int ventoy_api_password_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
2277 const char *path
= NULL
;
2278 menu_password
*last
= NULL
;
2279 menu_password
*node
= NULL
;
2280 data_password
*data
= NULL
;
2282 vtoy_json_get_int(json
, "index", &index
);
2283 data
= g_data_password
+ index
;
2285 path
= VTOY_JSON_STR_EX("path");
2288 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
2290 vtoy_list_free(menu_password
, data
->list
);
2294 vtoy_list_del(last
, node
, data
->list
, path
);
2298 ret
= ventoy_data_save_all();
2300 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2307 void ventoy_data_default_conf_replace(data_conf_replace
*data
)
2309 memset(data
, 0, sizeof(data_conf_replace
));
2312 int ventoy_data_cmp_conf_replace(data_conf_replace
*data1
, data_conf_replace
*data2
)
2314 conf_replace_node
*list1
= NULL
;
2315 conf_replace_node
*list2
= NULL
;
2317 if (NULL
== data1
->list
&& NULL
== data2
->list
)
2321 else if (data1
->list
&& data2
->list
)
2323 list1
= data1
->list
;
2324 list2
= data2
->list
;
2326 while (list1
&& list2
)
2328 if (list1
->image
!= list2
->image
||
2329 strcmp(list1
->path
, list2
->path
) ||
2330 strcmp(list1
->org
, list2
->org
) ||
2331 strcmp(list1
->new, list2
->new)
2337 list1
= list1
->next
;
2338 list2
= list2
->next
;
2341 if (list1
== NULL
&& list2
== NULL
)
2356 int ventoy_data_save_conf_replace(data_conf_replace
*data
, const char *title
, char *buf
, int buflen
)
2359 conf_replace_node
*node
= NULL
;
2361 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2363 VTOY_JSON_FMT_KEY_L(L1
, title
);
2364 VTOY_JSON_FMT_ARY_BEGIN_N();
2366 for (node
= data
->list
; node
; node
= node
->next
)
2368 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
2370 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "iso", node
->path
);
2371 VTOY_JSON_FMT_STRN_LN(L3
, "org", node
->org
);
2372 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "new", node
->new);
2375 VTOY_JSON_FMT_SINT_LN(L3
, "img", node
->image
);
2378 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
2381 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
2382 VTOY_JSON_FMT_END(pos
);
2388 int ventoy_data_json_conf_replace(data_conf_replace
*data
, char *buf
, int buflen
)
2391 conf_replace_node
*node
= NULL
;
2393 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2394 VTOY_JSON_FMT_ARY_BEGIN();
2396 for (node
= data
->list
; node
; node
= node
->next
)
2398 VTOY_JSON_FMT_OBJ_BEGIN();
2400 VTOY_JSON_FMT_STRN("path", node
->path
);
2401 VTOY_JSON_FMT_SINT("valid", ventoy_check_fuzzy_path(node
->path
, 1));
2402 VTOY_JSON_FMT_STRN("org", node
->org
);
2403 VTOY_JSON_FMT_STRN("new", node
->new);
2404 VTOY_JSON_FMT_SINT("new_valid", ventoy_is_file_exist("%s%s", g_cur_dir
, node
->new));
2405 VTOY_JSON_FMT_SINT("img", node
->image
);
2407 VTOY_JSON_FMT_OBJ_ENDEX();
2410 VTOY_JSON_FMT_ARY_END();
2411 VTOY_JSON_FMT_END(pos
);
2416 static int ventoy_api_get_conf_replace(struct mg_connection
*conn
, VTOY_JSON
*json
)
2418 api_get_func(conn
, json
, conf_replace
);
2422 static int ventoy_api_save_conf_replace(struct mg_connection
*conn
, VTOY_JSON
*json
)
2425 ret
= ventoy_data_save_all();
2427 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2431 static int ventoy_api_conf_replace_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
2436 const char *path
= NULL
;
2437 const char *org
= NULL
;
2438 const char *new = NULL
;
2439 conf_replace_node
*node
= NULL
;
2440 conf_replace_node
*cur
= NULL
;
2441 data_conf_replace
*data
= NULL
;
2443 vtoy_json_get_int(json
, "img", &image
);
2445 vtoy_json_get_int(json
, "index", &index
);
2446 data
= g_data_conf_replace
+ index
;
2448 path
= VTOY_JSON_STR_EX("path");
2449 org
= VTOY_JSON_STR_EX("org");
2450 new = VTOY_JSON_STR_EX("new");
2451 if (path
&& org
&& new)
2453 node
= zalloc(sizeof(conf_replace_node
));
2456 node
->image
= image
;
2457 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
2458 scnprintf(node
->org
, sizeof(node
->org
), "%s", org
);
2459 scnprintf(node
->new, sizeof(node
->new), "%s", new);
2461 vtoy_list_add(data
->list
, cur
, node
);
2465 ret
= ventoy_data_save_all();
2467 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2471 static int ventoy_api_conf_replace_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
2475 const char *path
= NULL
;
2476 conf_replace_node
*last
= NULL
;
2477 conf_replace_node
*node
= NULL
;
2478 data_conf_replace
*data
= NULL
;
2480 vtoy_json_get_int(json
, "index", &index
);
2481 data
= g_data_conf_replace
+ index
;
2483 path
= VTOY_JSON_STR_EX("path");
2486 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
2488 vtoy_list_free(conf_replace_node
, data
->list
);
2492 vtoy_list_del(last
, node
, data
->list
, path
);
2496 ret
= ventoy_data_save_all();
2498 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2506 void ventoy_data_default_dud(data_dud
*data
)
2508 memset(data
, 0, sizeof(data_dud
));
2511 int ventoy_data_cmp_dud(data_dud
*data1
, data_dud
*data2
)
2513 dud_node
*list1
= NULL
;
2514 dud_node
*list2
= NULL
;
2516 if (NULL
== data1
->list
&& NULL
== data2
->list
)
2520 else if (data1
->list
&& data2
->list
)
2522 list1
= data1
->list
;
2523 list2
= data2
->list
;
2525 while (list1
&& list2
)
2527 if (strcmp(list1
->path
, list2
->path
))
2532 /* no need to compare dud list with default */
2533 list1
= list1
->next
;
2534 list2
= list2
->next
;
2537 if (list1
== NULL
&& list2
== NULL
)
2552 int ventoy_data_save_dud(data_dud
*data
, const char *title
, char *buf
, int buflen
)
2555 dud_node
*node
= NULL
;
2556 path_node
*pathnode
= NULL
;
2558 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2560 VTOY_JSON_FMT_KEY_L(L1
, title
);
2561 VTOY_JSON_FMT_ARY_BEGIN_N();
2563 for (node
= data
->list
; node
; node
= node
->next
)
2565 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
2566 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "image", node
->path
);
2568 VTOY_JSON_FMT_KEY_L(L3
, "dud");
2569 VTOY_JSON_FMT_ARY_BEGIN_N();
2570 for (pathnode
= node
->list
; pathnode
; pathnode
= pathnode
->next
)
2572 VTOY_JSON_FMT_ITEM_PATH_LN(L4
, pathnode
->path
);
2574 VTOY_JSON_FMT_ARY_ENDEX_LN(L3
);
2576 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
2579 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
2580 VTOY_JSON_FMT_END(pos
);
2586 int ventoy_data_json_dud(data_dud
*data
, char *buf
, int buflen
)
2590 dud_node
*node
= NULL
;
2591 path_node
*pathnode
= NULL
;
2593 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2594 VTOY_JSON_FMT_ARY_BEGIN();
2596 for (node
= data
->list
; node
; node
= node
->next
)
2598 VTOY_JSON_FMT_OBJ_BEGIN();
2600 VTOY_JSON_FMT_STRN("path", node
->path
);
2601 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
2602 VTOY_JSON_FMT_SINT("valid", valid
);
2605 VTOY_JSON_FMT_KEY("list");
2606 VTOY_JSON_FMT_ARY_BEGIN();
2607 for (pathnode
= node
->list
; pathnode
; pathnode
= pathnode
->next
)
2609 VTOY_JSON_FMT_OBJ_BEGIN();
2610 VTOY_JSON_FMT_STRN("path", pathnode
->path
);
2612 valid
= ventoy_is_file_exist("%s%s", g_cur_dir
, pathnode
->path
);
2613 VTOY_JSON_FMT_SINT("valid", valid
);
2614 VTOY_JSON_FMT_OBJ_ENDEX();
2616 VTOY_JSON_FMT_ARY_ENDEX();
2619 VTOY_JSON_FMT_OBJ_ENDEX();
2622 VTOY_JSON_FMT_ARY_END();
2623 VTOY_JSON_FMT_END(pos
);
2628 static int ventoy_api_get_dud(struct mg_connection
*conn
, VTOY_JSON
*json
)
2630 api_get_func(conn
, json
, dud
);
2634 static int ventoy_api_save_dud(struct mg_connection
*conn
, VTOY_JSON
*json
)
2637 ret
= ventoy_data_save_all();
2639 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2644 static int ventoy_api_dud_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
2648 const char *path
= NULL
;
2649 dud_node
*node
= NULL
;
2650 dud_node
*cur
= NULL
;
2651 data_dud
*data
= NULL
;
2652 VTOY_JSON
*array
= NULL
;
2654 vtoy_json_get_int(json
, "index", &index
);
2655 data
= g_data_dud
+ index
;
2657 array
= vtoy_json_find_item(json
, JSON_TYPE_ARRAY
, "dud");
2658 path
= VTOY_JSON_STR_EX("path");
2661 node
= zalloc(sizeof(dud_node
));
2664 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
2665 node
->list
= ventoy_path_node_add_array(array
);
2667 vtoy_list_add(data
->list
, cur
, node
);
2671 ret
= ventoy_data_save_all();
2673 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2677 static int ventoy_api_dud_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
2681 const char *path
= NULL
;
2682 dud_node
*next
= NULL
;
2683 dud_node
*last
= NULL
;
2684 dud_node
*node
= NULL
;
2685 data_dud
*data
= NULL
;
2687 vtoy_json_get_int(json
, "index", &index
);
2688 data
= g_data_dud
+ index
;
2690 path
= VTOY_JSON_STR_EX("path");
2693 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
2695 for (node
= data
->list
; node
; node
= next
)
2698 ventoy_free_path_node_list(node
->list
);
2705 vtoy_list_del_ex(last
, node
, data
->list
, path
, ventoy_free_path_node_list
);
2709 ret
= ventoy_data_save_all();
2711 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2716 static int ventoy_api_dud_add_inner(struct mg_connection
*conn
, VTOY_JSON
*json
)
2720 const char *path
= NULL
;
2721 const char *outpath
= NULL
;
2722 path_node
*pcur
= NULL
;
2723 path_node
*pnode
= NULL
;
2724 dud_node
*node
= NULL
;
2725 data_dud
*data
= NULL
;
2727 vtoy_json_get_int(json
, "index", &index
);
2728 data
= g_data_dud
+ index
;
2730 path
= VTOY_JSON_STR_EX("path");
2731 outpath
= VTOY_JSON_STR_EX("outpath");
2732 if (path
&& outpath
)
2734 for (node
= data
->list
; node
; node
= node
->next
)
2736 if (strcmp(outpath
, node
->path
) == 0)
2738 pnode
= zalloc(sizeof(path_node
));
2741 scnprintf(pnode
->path
, sizeof(pnode
->path
), "%s", path
);
2742 vtoy_list_add(node
->list
, pcur
, pnode
);
2750 ret
= ventoy_data_save_all();
2752 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2756 static int ventoy_api_dud_del_inner(struct mg_connection
*conn
, VTOY_JSON
*json
)
2760 const char *path
= NULL
;
2761 const char *outpath
= NULL
;
2762 path_node
*plast
= NULL
;
2763 path_node
*pnode
= NULL
;
2764 dud_node
*node
= NULL
;
2765 data_dud
*data
= NULL
;
2767 vtoy_json_get_int(json
, "index", &index
);
2768 data
= g_data_dud
+ index
;
2770 path
= VTOY_JSON_STR_EX("path");
2771 outpath
= VTOY_JSON_STR_EX("outpath");
2772 if (path
&& outpath
)
2774 for (node
= data
->list
; node
; node
= node
->next
)
2776 if (strcmp(outpath
, node
->path
) == 0)
2778 vtoy_list_del(plast
, pnode
, node
->list
, path
);
2784 ret
= ventoy_data_save_all();
2786 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2794 void ventoy_data_default_auto_install(data_auto_install
*data
)
2796 memset(data
, 0, sizeof(data_auto_install
));
2799 int ventoy_data_cmp_auto_install(data_auto_install
*data1
, data_auto_install
*data2
)
2801 auto_install_node
*list1
= NULL
;
2802 auto_install_node
*list2
= NULL
;
2804 if (NULL
== data1
->list
&& NULL
== data2
->list
)
2808 else if (data1
->list
&& data2
->list
)
2810 list1
= data1
->list
;
2811 list2
= data2
->list
;
2813 while (list1
&& list2
)
2815 if (list1
->timeout
!= list2
->timeout
||
2816 list1
->autosel
!= list2
->autosel
||
2817 strcmp(list1
->path
, list2
->path
))
2822 /* no need to compare auto install list with default */
2823 list1
= list1
->next
;
2824 list2
= list2
->next
;
2827 if (list1
== NULL
&& list2
== NULL
)
2842 int ventoy_data_save_auto_install(data_auto_install
*data
, const char *title
, char *buf
, int buflen
)
2845 auto_install_node
*node
= NULL
;
2846 path_node
*pathnode
= NULL
;
2848 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2850 VTOY_JSON_FMT_KEY_L(L1
, title
);
2851 VTOY_JSON_FMT_ARY_BEGIN_N();
2853 for (node
= data
->list
; node
; node
= node
->next
)
2855 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
2856 if (node
->type
== 0)
2858 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "image", node
->path
);
2862 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "parent", node
->path
);
2866 VTOY_JSON_FMT_KEY_L(L3
, "template");
2867 VTOY_JSON_FMT_ARY_BEGIN_N();
2868 for (pathnode
= node
->list
; pathnode
; pathnode
= pathnode
->next
)
2870 VTOY_JSON_FMT_ITEM_PATH_LN(L4
, pathnode
->path
);
2872 VTOY_JSON_FMT_ARY_ENDEX_LN(L3
);
2874 if (node
->timeouten
)
2876 VTOY_JSON_FMT_SINT_LN(L3
, "timeout", node
->timeout
);
2879 if (node
->autoselen
)
2881 VTOY_JSON_FMT_SINT_LN(L3
, "autosel", node
->autosel
);
2884 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
2887 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
2888 VTOY_JSON_FMT_END(pos
);
2894 int ventoy_data_json_auto_install(data_auto_install
*data
, char *buf
, int buflen
)
2898 auto_install_node
*node
= NULL
;
2899 path_node
*pathnode
= NULL
;
2901 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2902 VTOY_JSON_FMT_ARY_BEGIN();
2904 for (node
= data
->list
; node
; node
= node
->next
)
2906 VTOY_JSON_FMT_OBJ_BEGIN();
2908 VTOY_JSON_FMT_STRN("path", node
->path
);
2910 if (node
->type
== 0)
2912 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
2916 valid
= ventoy_is_directory_exist("%s%s", g_cur_dir
, node
->path
);
2918 VTOY_JSON_FMT_SINT("valid", valid
);
2919 VTOY_JSON_FMT_SINT("type", node
->type
);
2921 VTOY_JSON_FMT_BOOL("timeouten", node
->timeouten
);
2922 VTOY_JSON_FMT_BOOL("autoselen", node
->autoselen
);
2924 VTOY_JSON_FMT_SINT("autosel", node
->autosel
);
2925 VTOY_JSON_FMT_SINT("timeout", node
->timeout
);
2927 VTOY_JSON_FMT_KEY("list");
2928 VTOY_JSON_FMT_ARY_BEGIN();
2929 for (pathnode
= node
->list
; pathnode
; pathnode
= pathnode
->next
)
2931 VTOY_JSON_FMT_OBJ_BEGIN();
2932 VTOY_JSON_FMT_STRN("path", pathnode
->path
);
2934 valid
= ventoy_is_file_exist("%s%s", g_cur_dir
, pathnode
->path
);
2935 VTOY_JSON_FMT_SINT("valid", valid
);
2936 VTOY_JSON_FMT_OBJ_ENDEX();
2938 VTOY_JSON_FMT_ARY_ENDEX();
2941 VTOY_JSON_FMT_OBJ_ENDEX();
2944 VTOY_JSON_FMT_ARY_END();
2945 VTOY_JSON_FMT_END(pos
);
2950 static int ventoy_api_get_auto_install(struct mg_connection
*conn
, VTOY_JSON
*json
)
2952 api_get_func(conn
, json
, auto_install
);
2956 static int ventoy_api_save_auto_install(struct mg_connection
*conn
, VTOY_JSON
*json
)
2962 uint8_t timeouten
= 0;
2963 uint8_t autoselen
= 0;
2964 auto_install_node
*node
= NULL
;
2965 data_auto_install
*data
= NULL
;
2967 vtoy_json_get_int(json
, "index", &index
);
2968 vtoy_json_get_int(json
, "id", &id
);
2970 vtoy_json_get_bool(json
, "timeouten", &timeouten
);
2971 vtoy_json_get_bool(json
, "autoselen", &autoselen
);
2973 data
= g_data_auto_install
+ index
;
2977 for (node
= data
->list
; node
; node
= node
->next
)
2981 node
->timeouten
= (int)timeouten
;
2982 node
->autoselen
= (int)autoselen
;
2983 VTOY_JSON_INT("timeout", node
->timeout
);
2984 VTOY_JSON_INT("autosel", node
->autosel
);
2990 ret
= ventoy_data_save_all();
2992 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2997 static int ventoy_api_auto_install_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
3002 const char *path
= NULL
;
3003 auto_install_node
*node
= NULL
;
3004 auto_install_node
*cur
= NULL
;
3005 data_auto_install
*data
= NULL
;
3006 VTOY_JSON
*array
= NULL
;
3008 vtoy_json_get_int(json
, "type", &type
);
3009 vtoy_json_get_int(json
, "index", &index
);
3010 data
= g_data_auto_install
+ index
;
3012 array
= vtoy_json_find_item(json
, JSON_TYPE_ARRAY
, "template");
3013 path
= VTOY_JSON_STR_EX("path");
3016 node
= zalloc(sizeof(auto_install_node
));
3020 node
->timeouten
= 0;
3021 node
->autoselen
= 0;
3024 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
3025 node
->list
= ventoy_path_node_add_array(array
);
3027 vtoy_list_add(data
->list
, cur
, node
);
3031 ret
= ventoy_data_save_all();
3033 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3037 static int ventoy_api_auto_install_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
3041 const char *path
= NULL
;
3042 auto_install_node
*last
= NULL
;
3043 auto_install_node
*next
= NULL
;
3044 auto_install_node
*node
= NULL
;
3045 data_auto_install
*data
= NULL
;
3047 vtoy_json_get_int(json
, "index", &index
);
3048 data
= g_data_auto_install
+ index
;
3050 path
= VTOY_JSON_STR_EX("path");
3053 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
3055 for (node
= data
->list
; node
; node
= next
)
3058 ventoy_free_path_node_list(node
->list
);
3065 vtoy_list_del_ex(last
, node
, data
->list
, path
, ventoy_free_path_node_list
);
3069 ret
= ventoy_data_save_all();
3071 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3075 static int ventoy_api_auto_install_add_inner(struct mg_connection
*conn
, VTOY_JSON
*json
)
3079 const char *path
= NULL
;
3080 const char *outpath
= NULL
;
3081 path_node
*pcur
= NULL
;
3082 path_node
*pnode
= NULL
;
3083 auto_install_node
*node
= NULL
;
3084 data_auto_install
*data
= NULL
;
3086 vtoy_json_get_int(json
, "index", &index
);
3087 data
= g_data_auto_install
+ index
;
3089 path
= VTOY_JSON_STR_EX("path");
3090 outpath
= VTOY_JSON_STR_EX("outpath");
3091 if (path
&& outpath
)
3093 for (node
= data
->list
; node
; node
= node
->next
)
3095 if (strcmp(outpath
, node
->path
) == 0)
3097 pnode
= zalloc(sizeof(path_node
));
3100 scnprintf(pnode
->path
, sizeof(pnode
->path
), "%s", path
);
3101 vtoy_list_add(node
->list
, pcur
, pnode
);
3109 ret
= ventoy_data_save_all();
3111 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3115 static int ventoy_api_auto_install_del_inner(struct mg_connection
*conn
, VTOY_JSON
*json
)
3119 const char *path
= NULL
;
3120 const char *outpath
= NULL
;
3121 path_node
*plast
= NULL
;
3122 path_node
*pnode
= NULL
;
3123 auto_install_node
*node
= NULL
;
3124 data_auto_install
*data
= NULL
;
3126 vtoy_json_get_int(json
, "index", &index
);
3127 data
= g_data_auto_install
+ index
;
3129 path
= VTOY_JSON_STR_EX("path");
3130 outpath
= VTOY_JSON_STR_EX("outpath");
3131 if (path
&& outpath
)
3133 for (node
= data
->list
; node
; node
= node
->next
)
3135 if (strcmp(outpath
, node
->path
) == 0)
3137 vtoy_list_del(plast
, pnode
, node
->list
, path
);
3143 ret
= ventoy_data_save_all();
3145 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3154 void ventoy_data_default_persistence(data_persistence
*data
)
3156 memset(data
, 0, sizeof(data_persistence
));
3159 int ventoy_data_cmp_persistence(data_persistence
*data1
, data_persistence
*data2
)
3161 persistence_node
*list1
= NULL
;
3162 persistence_node
*list2
= NULL
;
3164 if (NULL
== data1
->list
&& NULL
== data2
->list
)
3168 else if (data1
->list
&& data2
->list
)
3170 list1
= data1
->list
;
3171 list2
= data2
->list
;
3173 while (list1
&& list2
)
3175 if (list1
->timeout
!= list2
->timeout
||
3176 list1
->autosel
!= list2
->autosel
||
3177 strcmp(list1
->path
, list2
->path
))
3182 /* no need to compare auto install list with default */
3183 list1
= list1
->next
;
3184 list2
= list2
->next
;
3187 if (list1
== NULL
&& list2
== NULL
)
3202 int ventoy_data_save_persistence(data_persistence
*data
, const char *title
, char *buf
, int buflen
)
3205 persistence_node
*node
= NULL
;
3206 path_node
*pathnode
= NULL
;
3208 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
3210 VTOY_JSON_FMT_KEY_L(L1
, title
);
3211 VTOY_JSON_FMT_ARY_BEGIN_N();
3213 for (node
= data
->list
; node
; node
= node
->next
)
3215 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
3216 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "image", node
->path
);
3217 VTOY_JSON_FMT_KEY_L(L3
, "backend");
3218 VTOY_JSON_FMT_ARY_BEGIN_N();
3219 for (pathnode
= node
->list
; pathnode
; pathnode
= pathnode
->next
)
3221 VTOY_JSON_FMT_ITEM_PATH_LN(L4
, pathnode
->path
);
3223 VTOY_JSON_FMT_ARY_ENDEX_LN(L3
);
3225 if (node
->timeouten
)
3227 VTOY_JSON_FMT_SINT_LN(L3
, "timeout", node
->timeout
);
3230 if (node
->autoselen
)
3232 VTOY_JSON_FMT_SINT_LN(L3
, "autosel", node
->autosel
);
3235 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
3238 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
3239 VTOY_JSON_FMT_END(pos
);
3245 int ventoy_data_json_persistence(data_persistence
*data
, char *buf
, int buflen
)
3249 persistence_node
*node
= NULL
;
3250 path_node
*pathnode
= NULL
;
3252 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
3253 VTOY_JSON_FMT_ARY_BEGIN();
3255 for (node
= data
->list
; node
; node
= node
->next
)
3257 VTOY_JSON_FMT_OBJ_BEGIN();
3259 VTOY_JSON_FMT_STRN("path", node
->path
);
3261 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
3262 VTOY_JSON_FMT_SINT("valid", valid
);
3263 VTOY_JSON_FMT_SINT("type", node
->type
);
3265 VTOY_JSON_FMT_BOOL("timeouten", node
->timeouten
);
3266 VTOY_JSON_FMT_BOOL("autoselen", node
->autoselen
);
3268 VTOY_JSON_FMT_SINT("autosel", node
->autosel
);
3269 VTOY_JSON_FMT_SINT("timeout", node
->timeout
);
3271 VTOY_JSON_FMT_KEY("list");
3272 VTOY_JSON_FMT_ARY_BEGIN();
3273 for (pathnode
= node
->list
; pathnode
; pathnode
= pathnode
->next
)
3275 VTOY_JSON_FMT_OBJ_BEGIN();
3276 VTOY_JSON_FMT_STRN("path", pathnode
->path
);
3278 valid
= ventoy_is_file_exist("%s%s", g_cur_dir
, pathnode
->path
);
3279 VTOY_JSON_FMT_SINT("valid", valid
);
3280 VTOY_JSON_FMT_OBJ_ENDEX();
3282 VTOY_JSON_FMT_ARY_ENDEX();
3285 VTOY_JSON_FMT_OBJ_ENDEX();
3288 VTOY_JSON_FMT_ARY_END();
3289 VTOY_JSON_FMT_END(pos
);
3294 static int ventoy_api_get_persistence(struct mg_connection
*conn
, VTOY_JSON
*json
)
3296 api_get_func(conn
, json
, persistence
);
3300 static int ventoy_api_save_persistence(struct mg_connection
*conn
, VTOY_JSON
*json
)
3306 uint8_t timeouten
= 0;
3307 uint8_t autoselen
= 0;
3308 persistence_node
*node
= NULL
;
3309 data_persistence
*data
= NULL
;
3311 vtoy_json_get_int(json
, "index", &index
);
3312 vtoy_json_get_int(json
, "id", &id
);
3314 vtoy_json_get_bool(json
, "timeouten", &timeouten
);
3315 vtoy_json_get_bool(json
, "autoselen", &autoselen
);
3317 data
= g_data_persistence
+ index
;
3321 for (node
= data
->list
; node
; node
= node
->next
)
3325 node
->timeouten
= (int)timeouten
;
3326 node
->autoselen
= (int)autoselen
;
3327 VTOY_JSON_INT("timeout", node
->timeout
);
3328 VTOY_JSON_INT("autosel", node
->autosel
);
3334 ret
= ventoy_data_save_all();
3336 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3341 static int ventoy_api_persistence_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
3345 const char *path
= NULL
;
3346 persistence_node
*node
= NULL
;
3347 persistence_node
*cur
= NULL
;
3348 data_persistence
*data
= NULL
;
3349 VTOY_JSON
*array
= NULL
;
3351 vtoy_json_get_int(json
, "index", &index
);
3352 data
= g_data_persistence
+ index
;
3354 array
= vtoy_json_find_item(json
, JSON_TYPE_ARRAY
, "backend");
3355 path
= VTOY_JSON_STR_EX("path");
3358 node
= zalloc(sizeof(persistence_node
));
3361 node
->timeouten
= 0;
3362 node
->autoselen
= 0;
3365 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
3366 node
->list
= ventoy_path_node_add_array(array
);
3368 vtoy_list_add(data
->list
, cur
, node
);
3372 ret
= ventoy_data_save_all();
3374 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3378 static int ventoy_api_persistence_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
3382 const char *path
= NULL
;
3383 persistence_node
*last
= NULL
;
3384 persistence_node
*next
= NULL
;
3385 persistence_node
*node
= NULL
;
3386 data_persistence
*data
= NULL
;
3388 vtoy_json_get_int(json
, "index", &index
);
3389 data
= g_data_persistence
+ index
;
3391 path
= VTOY_JSON_STR_EX("path");
3394 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
3396 for (node
= data
->list
; node
; node
= next
)
3399 ventoy_free_path_node_list(node
->list
);
3406 vtoy_list_del_ex(last
, node
, data
->list
, path
, ventoy_free_path_node_list
);
3410 ret
= ventoy_data_save_all();
3412 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3416 static int ventoy_api_persistence_add_inner(struct mg_connection
*conn
, VTOY_JSON
*json
)
3420 const char *path
= NULL
;
3421 const char *outpath
= NULL
;
3422 path_node
*pcur
= NULL
;
3423 path_node
*pnode
= NULL
;
3424 persistence_node
*node
= NULL
;
3425 data_persistence
*data
= NULL
;
3427 vtoy_json_get_int(json
, "index", &index
);
3428 data
= g_data_persistence
+ index
;
3430 path
= VTOY_JSON_STR_EX("path");
3431 outpath
= VTOY_JSON_STR_EX("outpath");
3432 if (path
&& outpath
)
3434 for (node
= data
->list
; node
; node
= node
->next
)
3436 if (strcmp(outpath
, node
->path
) == 0)
3438 pnode
= zalloc(sizeof(path_node
));
3441 scnprintf(pnode
->path
, sizeof(pnode
->path
), "%s", path
);
3442 vtoy_list_add(node
->list
, pcur
, pnode
);
3450 ret
= ventoy_data_save_all();
3452 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3456 static int ventoy_api_persistence_del_inner(struct mg_connection
*conn
, VTOY_JSON
*json
)
3460 const char *path
= NULL
;
3461 const char *outpath
= NULL
;
3462 path_node
*plast
= NULL
;
3463 path_node
*pnode
= NULL
;
3464 persistence_node
*node
= NULL
;
3465 data_persistence
*data
= NULL
;
3467 vtoy_json_get_int(json
, "index", &index
);
3468 data
= g_data_persistence
+ index
;
3470 path
= VTOY_JSON_STR_EX("path");
3471 outpath
= VTOY_JSON_STR_EX("outpath");
3472 if (path
&& outpath
)
3474 for (node
= data
->list
; node
; node
= node
->next
)
3476 if (strcmp(outpath
, node
->path
) == 0)
3478 vtoy_list_del(plast
, pnode
, node
->list
, path
);
3484 ret
= ventoy_data_save_all();
3486 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3494 void ventoy_data_default_injection(data_injection
*data
)
3496 memset(data
, 0, sizeof(data_injection
));
3499 int ventoy_data_cmp_injection(data_injection
*data1
, data_injection
*data2
)
3501 injection_node
*list1
= NULL
;
3502 injection_node
*list2
= NULL
;
3504 if (NULL
== data1
->list
&& NULL
== data2
->list
)
3508 else if (data1
->list
&& data2
->list
)
3510 list1
= data1
->list
;
3511 list2
= data2
->list
;
3513 while (list1
&& list2
)
3515 if ((list1
->type
!= list2
->type
) ||
3516 strcmp(list1
->path
, list2
->path
) ||
3517 strcmp(list1
->archive
, list2
->archive
))
3522 list1
= list1
->next
;
3523 list2
= list2
->next
;
3526 if (list1
== NULL
&& list2
== NULL
)
3541 int ventoy_data_save_injection(data_injection
*data
, const char *title
, char *buf
, int buflen
)
3544 injection_node
*node
= NULL
;
3546 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
3548 VTOY_JSON_FMT_KEY_L(L1
, title
);
3549 VTOY_JSON_FMT_ARY_BEGIN_N();
3551 for (node
= data
->list
; node
; node
= node
->next
)
3553 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
3555 if (node
->type
== 0)
3557 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "image", node
->path
);
3561 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "parent", node
->path
);
3563 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "archive", node
->archive
);
3565 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
3568 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
3569 VTOY_JSON_FMT_END(pos
);
3575 int ventoy_data_json_injection(data_injection
*data
, char *buf
, int buflen
)
3579 injection_node
*node
= NULL
;
3581 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
3582 VTOY_JSON_FMT_ARY_BEGIN();
3584 for (node
= data
->list
; node
; node
= node
->next
)
3586 VTOY_JSON_FMT_OBJ_BEGIN();
3588 VTOY_JSON_FMT_UINT("type", node
->type
);
3589 VTOY_JSON_FMT_STRN("path", node
->path
);
3591 if (node
->type
== 0)
3593 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
3597 valid
= ventoy_is_directory_exist("%s%s", g_cur_dir
, node
->path
);
3599 VTOY_JSON_FMT_SINT("valid", valid
);
3601 VTOY_JSON_FMT_STRN("archive", node
->archive
);
3603 valid
= ventoy_is_file_exist("%s%s", g_cur_dir
, node
->archive
);
3604 VTOY_JSON_FMT_SINT("archive_valid", valid
);
3606 VTOY_JSON_FMT_OBJ_ENDEX();
3609 VTOY_JSON_FMT_ARY_END();
3610 VTOY_JSON_FMT_END(pos
);
3616 static int ventoy_api_get_injection(struct mg_connection
*conn
, VTOY_JSON
*json
)
3618 api_get_func(conn
, json
, injection
);
3622 static int ventoy_api_save_injection(struct mg_connection
*conn
, VTOY_JSON
*json
)
3625 ret
= ventoy_data_save_all();
3627 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3631 static int ventoy_api_injection_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
3636 const char *path
= NULL
;
3637 const char *archive
= NULL
;
3638 injection_node
*node
= NULL
;
3639 injection_node
*cur
= NULL
;
3640 data_injection
*data
= NULL
;
3642 vtoy_json_get_int(json
, "index", &index
);
3643 data
= g_data_injection
+ index
;
3645 vtoy_json_get_int(json
, "type", &type
);
3647 path
= VTOY_JSON_STR_EX("path");
3648 archive
= VTOY_JSON_STR_EX("archive");
3649 if (path
&& archive
)
3651 node
= zalloc(sizeof(injection_node
));
3656 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
3657 scnprintf(node
->archive
, sizeof(node
->archive
), "%s", archive
);
3659 vtoy_list_add(data
->list
, cur
, node
);
3663 ret
= ventoy_data_save_all();
3665 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3669 static int ventoy_api_injection_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
3673 const char *path
= NULL
;
3674 injection_node
*last
= NULL
;
3675 injection_node
*node
= NULL
;
3676 data_injection
*data
= NULL
;
3678 vtoy_json_get_int(json
, "index", &index
);
3679 data
= g_data_injection
+ index
;
3681 path
= VTOY_JSON_STR_EX("path");
3684 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
3686 vtoy_list_free(injection_node
, data
->list
);
3690 vtoy_list_del(last
, node
, data
->list
, path
);
3694 ret
= ventoy_data_save_all();
3696 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3703 static int ventoy_api_preview_json(struct mg_connection
*conn
, VTOY_JSON
*json
)
3708 int utf16enclen
= 0;
3709 char *encodebuf
= NULL
;
3710 unsigned short *utf16buf
= NULL
;
3714 /* We can not use json directly, because it will be formated in the JS. */
3716 len
= ventoy_data_real_save_all(0);
3718 utf16buf
= (unsigned short *)malloc(2 * len
+ 16);
3724 utf16enclen
= (int)utf8_to_utf16((unsigned char *)JSON_SAVE_BUFFER
, len
, utf16buf
, len
+ 2);
3726 encodebuf
= (char *)malloc(utf16enclen
* 4 + 16);
3732 for (i
= 0; i
< utf16enclen
; i
++)
3734 scnprintf(encodebuf
+ i
* 4, 5, "%04X", utf16buf
[i
]);
3738 VTOY_JSON_FMT_BEGIN(pos
, JSON_BUFFER
, JSON_BUF_MAX
);
3739 VTOY_JSON_FMT_OBJ_BEGIN();
3740 VTOY_JSON_FMT_STRN("json", (encodebuf
? encodebuf
: ""));
3741 VTOY_JSON_FMT_OBJ_END();
3742 VTOY_JSON_FMT_END(pos
);
3744 CHECK_FREE(encodebuf
);
3745 CHECK_FREE(utf16buf
);
3747 ventoy_json_buffer(conn
, JSON_BUFFER
, pos
);
3755 int ventoy_data_save_all(void)
3757 ventoy_set_writeback_event();
3761 int ventoy_data_real_save_all(int apilock
)
3769 pthread_mutex_lock(&g_api_mutex
);
3772 ssprintf(pos
, JSON_SAVE_BUFFER
, JSON_BUF_MAX
, "{\n");
3774 ventoy_save_plug(control
);
3775 ventoy_save_plug(theme
);
3776 ventoy_save_plug(menu_alias
);
3777 ventoy_save_plug(menu_tip
);
3778 ventoy_save_plug(menu_class
);
3779 ventoy_save_plug(auto_install
);
3780 ventoy_save_plug(persistence
);
3781 ventoy_save_plug(injection
);
3782 ventoy_save_plug(conf_replace
);
3783 ventoy_save_plug(password
);
3784 ventoy_save_plug(image_list
);
3785 ventoy_save_plug(auto_memdisk
);
3786 ventoy_save_plug(dud
);
3788 if (JSON_SAVE_BUFFER
[pos
- 1] == '\n' && JSON_SAVE_BUFFER
[pos
- 2] == ',')
3790 JSON_SAVE_BUFFER
[pos
- 2] = '\n';
3793 ssprintf(pos
, JSON_SAVE_BUFFER
, JSON_BUF_MAX
, "}\n");
3797 pthread_mutex_unlock(&g_api_mutex
);
3803 int ventoy_http_writeback(void)
3809 ventoy_get_json_path(filename
, NULL
);
3811 pos
= ventoy_data_real_save_all(1);
3814 printf("%s", JSON_SAVE_BUFFER
);
3817 ret
= ventoy_write_buf_to_file(filename
, JSON_SAVE_BUFFER
, pos
);
3820 vlog("Failed to write ventoy.json file.\n");
3821 g_sysinfo
.config_save_error
= 1;
3828 static JSON_CB g_ventoy_json_cb
[] =
3830 { "sysinfo", ventoy_api_sysinfo
},
3831 { "handshake", ventoy_api_handshake
},
3832 { "check_path", ventoy_api_check_exist
},
3833 { "check_path2", ventoy_api_check_exist2
},
3834 { "check_fuzzy", ventoy_api_check_fuzzy
},
3836 { "device_info", ventoy_api_device_info
},
3838 { "get_control", ventoy_api_get_control
},
3839 { "save_control", ventoy_api_save_control
},
3841 { "get_theme", ventoy_api_get_theme
},
3842 { "save_theme", ventoy_api_save_theme
},
3843 { "theme_add_file", ventoy_api_theme_add_file
},
3844 { "theme_del_file", ventoy_api_theme_del_file
},
3845 { "theme_add_font", ventoy_api_theme_add_font
},
3846 { "theme_del_font", ventoy_api_theme_del_font
},
3848 { "get_alias", ventoy_api_get_alias
},
3849 { "save_alias", ventoy_api_save_alias
},
3850 { "alias_add", ventoy_api_alias_add
},
3851 { "alias_del", ventoy_api_alias_del
},
3853 { "get_tip", ventoy_api_get_tip
},
3854 { "save_tip", ventoy_api_save_tip
},
3855 { "tip_add", ventoy_api_tip_add
},
3856 { "tip_del", ventoy_api_tip_del
},
3858 { "get_class", ventoy_api_get_class
},
3859 { "save_class", ventoy_api_save_class
},
3860 { "class_add", ventoy_api_class_add
},
3861 { "class_del", ventoy_api_class_del
},
3863 { "get_auto_memdisk", ventoy_api_get_auto_memdisk
},
3864 { "save_auto_memdisk", ventoy_api_save_auto_memdisk
},
3865 { "auto_memdisk_add", ventoy_api_auto_memdisk_add
},
3866 { "auto_memdisk_del", ventoy_api_auto_memdisk_del
},
3868 { "get_image_list", ventoy_api_get_image_list
},
3869 { "save_image_list", ventoy_api_save_image_list
},
3870 { "image_list_add", ventoy_api_image_list_add
},
3871 { "image_list_del", ventoy_api_image_list_del
},
3873 { "get_conf_replace", ventoy_api_get_conf_replace
},
3874 { "save_conf_replace", ventoy_api_save_conf_replace
},
3875 { "conf_replace_add", ventoy_api_conf_replace_add
},
3876 { "conf_replace_del", ventoy_api_conf_replace_del
},
3878 { "get_dud", ventoy_api_get_dud
},
3879 { "save_dud", ventoy_api_save_dud
},
3880 { "dud_add", ventoy_api_dud_add
},
3881 { "dud_del", ventoy_api_dud_del
},
3882 { "dud_add_inner", ventoy_api_dud_add_inner
},
3883 { "dud_del_inner", ventoy_api_dud_del_inner
},
3885 { "get_auto_install", ventoy_api_get_auto_install
},
3886 { "save_auto_install", ventoy_api_save_auto_install
},
3887 { "auto_install_add", ventoy_api_auto_install_add
},
3888 { "auto_install_del", ventoy_api_auto_install_del
},
3889 { "auto_install_add_inner", ventoy_api_auto_install_add_inner
},
3890 { "auto_install_del_inner", ventoy_api_auto_install_del_inner
},
3892 { "get_persistence", ventoy_api_get_persistence
},
3893 { "save_persistence", ventoy_api_save_persistence
},
3894 { "persistence_add", ventoy_api_persistence_add
},
3895 { "persistence_del", ventoy_api_persistence_del
},
3896 { "persistence_add_inner", ventoy_api_persistence_add_inner
},
3897 { "persistence_del_inner", ventoy_api_persistence_del_inner
},
3899 { "get_password", ventoy_api_get_password
},
3900 { "save_password", ventoy_api_save_password
},
3901 { "password_add", ventoy_api_password_add
},
3902 { "password_del", ventoy_api_password_del
},
3904 { "get_injection", ventoy_api_get_injection
},
3905 { "save_injection", ventoy_api_save_injection
},
3906 { "injection_add", ventoy_api_injection_add
},
3907 { "injection_del", ventoy_api_injection_del
},
3908 { "preview_json", ventoy_api_preview_json
},
3912 static int ventoy_json_handler(struct mg_connection
*conn
, VTOY_JSON
*json
, char *jsonstr
)
3915 const char *method
= NULL
;
3917 method
= vtoy_json_get_string_ex(json
, "method");
3920 ventoy_json_result(conn
, VTOY_JSON_SUCCESS_RET
);
3924 if (strcmp(method
, "handshake") == 0)
3926 ventoy_api_handshake(conn
, json
);
3930 for (i
= 0; i
< (int)(sizeof(g_ventoy_json_cb
) / sizeof(g_ventoy_json_cb
[0])); i
++)
3932 if (strcmp(method
, g_ventoy_json_cb
[i
].method
) == 0)
3934 g_ventoy_json_cb
[i
].callback(conn
, json
);
3942 static int ventoy_request_handler(struct mg_connection
*conn
)
3946 VTOY_JSON
*json
= NULL
;
3947 char *post_data_buf
= NULL
;
3948 const struct mg_request_info
*ri
= NULL
;
3949 char stack_buf
[512];
3951 ri
= mg_get_request_info(conn
);
3953 if (strcmp(ri
->uri
, "/vtoy/json") == 0)
3955 if (ri
->content_length
> 500)
3957 post_data_buf
= malloc((int)(ri
->content_length
+ 4));
3958 post_buf_len
= (int)(ri
->content_length
+ 1);
3962 post_data_buf
= stack_buf
;
3963 post_buf_len
= sizeof(stack_buf
);
3966 post_data_len
= mg_read(conn
, post_data_buf
, post_buf_len
);
3967 post_data_buf
[post_data_len
] = 0;
3969 json
= vtoy_json_create();
3970 if (JSON_SUCCESS
== vtoy_json_parse(json
, post_data_buf
))
3972 pthread_mutex_lock(&g_api_mutex
);
3973 ventoy_json_handler(conn
, json
->pstChild
, post_data_buf
);
3974 pthread_mutex_unlock(&g_api_mutex
);
3978 ventoy_json_result(conn
, VTOY_JSON_INVALID_RET
);
3981 vtoy_json_destroy(json
);
3983 if (post_data_buf
!= stack_buf
)
3985 free(post_data_buf
);
3995 const char *ventoy_web_openfile(const struct mg_connection
*conn
, const char *path
, size_t *data_len
)
3997 ventoy_file
*node
= NULL
;
4006 node
= ventoy_tar_find_file(path
);
4009 *data_len
= node
->size
;
4021 static int ventoy_parse_control(VTOY_JSON
*json
, void *p
)
4024 VTOY_JSON
*node
= NULL
;
4025 VTOY_JSON
*child
= NULL
;
4026 data_control
*data
= (data_control
*)p
;
4028 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4033 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4035 if (node
->enDataType
== JSON_TYPE_OBJECT
)
4037 child
= node
->pstChild
;
4039 if (child
->enDataType
!= JSON_TYPE_STRING
)
4044 if (strcmp(child
->pcName
, "VTOY_DEFAULT_MENU_MODE") == 0)
4046 CONTROL_PARSE_INT_DEF_0(child
, data
->default_menu_mode
);
4048 else if (strcmp(child
->pcName
, "VTOY_WIN11_BYPASS_CHECK") == 0)
4050 CONTROL_PARSE_INT_DEF_0(child
, data
->win11_bypass_check
);
4052 else if (strcmp(child
->pcName
, "VTOY_LINUX_REMOUNT") == 0)
4054 CONTROL_PARSE_INT_DEF_0(child
, data
->linux_remount
);
4056 else if (strcmp(child
->pcName
, "VTOY_SECONDARY_BOOT_MENU") == 0)
4058 CONTROL_PARSE_INT_DEF_1(child
, data
->secondary_menu
);
4060 else if (strcmp(child
->pcName
, "VTOY_SHOW_PASSWORD_ASTERISK") == 0)
4062 CONTROL_PARSE_INT_DEF_1(child
, data
->password_asterisk
);
4064 else if (strcmp(child
->pcName
, "VTOY_TREE_VIEW_MENU_STYLE") == 0)
4066 CONTROL_PARSE_INT_DEF_0(child
, data
->treeview_style
);
4068 else if (strcmp(child
->pcName
, "VTOY_FILT_DOT_UNDERSCORE_FILE") == 0)
4070 CONTROL_PARSE_INT_DEF_1(child
, data
->filter_dot_underscore
);
4072 else if (strcmp(child
->pcName
, "VTOY_SORT_CASE_SENSITIVE") == 0)
4074 CONTROL_PARSE_INT_DEF_0(child
, data
->sort_casesensitive
);
4076 else if (strcmp(child
->pcName
, "VTOY_MAX_SEARCH_LEVEL") == 0)
4078 if (strcmp(child
->unData
.pcStrVal
, "max") == 0)
4080 data
->max_search_level
= -1;
4084 data
->max_search_level
= (int)strtol(child
->unData
.pcStrVal
, NULL
, 10);
4087 else if (strcmp(child
->pcName
, "VTOY_DEFAULT_SEARCH_ROOT") == 0)
4089 strlcpy(data
->default_search_root
, child
->unData
.pcStrVal
);
4091 else if (strcmp(child
->pcName
, "VTOY_DEFAULT_IMAGE") == 0)
4093 strlcpy(data
->default_image
, child
->unData
.pcStrVal
);
4095 else if (strcmp(child
->pcName
, "VTOY_DEFAULT_KBD_LAYOUT") == 0)
4097 for (i
= 0; g_ventoy_kbd_layout
[i
]; i
++)
4099 if (strcmp(child
->unData
.pcStrVal
, g_ventoy_kbd_layout
[i
]) == 0)
4101 strlcpy(data
->default_kbd_layout
, child
->unData
.pcStrVal
);
4106 else if (strcmp(child
->pcName
, "VTOY_MENU_LANGUAGE") == 0)
4108 for (i
= 0; g_ventoy_menu_lang
[i
][0]; i
++)
4110 if (strcmp(child
->unData
.pcStrVal
, g_ventoy_menu_lang
[i
]) == 0)
4112 strlcpy(data
->menu_language
, child
->unData
.pcStrVal
);
4117 else if (strcmp(child
->pcName
, "VTOY_MENU_TIMEOUT") == 0)
4119 data
->menu_timeout
= (int)strtol(child
->unData
.pcStrVal
, NULL
, 10);
4121 else if (strcmp(child
->pcName
, "VTOY_SECONDARY_TIMEOUT") == 0)
4123 data
->secondary_menu_timeout
= (int)strtol(child
->unData
.pcStrVal
, NULL
, 10);
4125 else if (strcmp(child
->pcName
, "VTOY_VHD_NO_WARNING") == 0)
4127 CONTROL_PARSE_INT_DEF_0(child
, data
->vhd_no_warning
);
4129 else if (strcmp(child
->pcName
, "VTOY_FILE_FLT_ISO") == 0)
4131 CONTROL_PARSE_INT_DEF_0(child
, data
->filter_iso
);
4133 else if (strcmp(child
->pcName
, "VTOY_FILE_FLT_IMG") == 0)
4135 CONTROL_PARSE_INT_DEF_0(child
, data
->filter_img
);
4137 else if (strcmp(child
->pcName
, "VTOY_FILE_FLT_EFI") == 0)
4139 CONTROL_PARSE_INT_DEF_0(child
, data
->filter_efi
);
4141 else if (strcmp(child
->pcName
, "VTOY_FILE_FLT_WIM") == 0)
4143 CONTROL_PARSE_INT_DEF_0(child
, data
->filter_wim
);
4145 else if (strcmp(child
->pcName
, "VTOY_FILE_FLT_VHD") == 0)
4147 CONTROL_PARSE_INT_DEF_0(child
, data
->filter_vhd
);
4149 else if (strcmp(child
->pcName
, "VTOY_FILE_FLT_VTOY") == 0)
4151 CONTROL_PARSE_INT_DEF_0(child
, data
->filter_vtoy
);
4159 static int ventoy_parse_theme(VTOY_JSON
*json
, void *p
)
4161 const char *dismode
= NULL
;
4162 VTOY_JSON
*child
= NULL
;
4163 VTOY_JSON
*node
= NULL
;
4164 path_node
*tail
= NULL
;
4165 path_node
*pnode
= NULL
;
4166 data_theme
*data
= (data_theme
*)p
;
4168 if (json
->enDataType
!= JSON_TYPE_OBJECT
)
4173 child
= json
->pstChild
;
4175 dismode
= vtoy_json_get_string_ex(child
, "display_mode");
4176 vtoy_json_get_string(child
, "ventoy_left", sizeof(data
->ventoy_left
), data
->ventoy_left
);
4177 vtoy_json_get_string(child
, "ventoy_top", sizeof(data
->ventoy_top
), data
->ventoy_top
);
4178 vtoy_json_get_string(child
, "ventoy_color", sizeof(data
->ventoy_color
), data
->ventoy_color
);
4180 vtoy_json_get_int(child
, "default_file", &(data
->default_file
));
4181 vtoy_json_get_string(child
, "gfxmode", sizeof(data
->gfxmode
), data
->gfxmode
);
4182 vtoy_json_get_string(child
, "serial_param", sizeof(data
->serial_param
), data
->serial_param
);
4186 if (strcmp(dismode
, "CLI") == 0)
4188 data
->display_mode
= display_mode_cli
;
4190 else if (strcmp(dismode
, "serial") == 0)
4192 data
->display_mode
= display_mode_serial
;
4194 else if (strcmp(dismode
, "serial_console") == 0)
4196 data
->display_mode
= display_mode_ser_console
;
4200 data
->display_mode
= display_mode_gui
;
4204 node
= vtoy_json_find_item(child
, JSON_TYPE_STRING
, "file");
4207 data
->default_file
= 0;
4209 pnode
= zalloc(sizeof(path_node
));
4212 strlcpy(pnode
->path
, node
->unData
.pcStrVal
);
4213 data
->filelist
= pnode
;
4218 node
= vtoy_json_find_item(child
, JSON_TYPE_ARRAY
, "file");
4221 for (node
= node
->pstChild
; node
; node
= node
->pstNext
)
4223 if (node
->enDataType
== JSON_TYPE_STRING
)
4225 pnode
= zalloc(sizeof(path_node
));
4228 strlcpy(pnode
->path
, node
->unData
.pcStrVal
);
4236 data
->filelist
= tail
= pnode
;
4245 node
= vtoy_json_find_item(child
, JSON_TYPE_ARRAY
, "fonts");
4248 for (node
= node
->pstChild
; node
; node
= node
->pstNext
)
4250 if (node
->enDataType
== JSON_TYPE_STRING
)
4252 pnode
= zalloc(sizeof(path_node
));
4255 strlcpy(pnode
->path
, node
->unData
.pcStrVal
);
4256 if (data
->fontslist
)
4263 data
->fontslist
= tail
= pnode
;
4272 static int ventoy_parse_menu_alias(VTOY_JSON
*json
, void *p
)
4275 const char *path
= NULL
;
4276 const char *alias
= NULL
;
4277 data_alias
*data
= (data_alias
*)p
;
4278 data_alias_node
*tail
= NULL
;
4279 data_alias_node
*pnode
= NULL
;
4280 VTOY_JSON
*node
= NULL
;
4282 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4287 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4289 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4294 type
= path_type_file
;
4295 path
= vtoy_json_get_string_ex(node
->pstChild
, "image");
4298 path
= vtoy_json_get_string_ex(node
->pstChild
, "dir");
4299 type
= path_type_dir
;
4301 alias
= vtoy_json_get_string_ex(node
->pstChild
, "alias");
4305 pnode
= zalloc(sizeof(data_alias_node
));
4309 strlcpy(pnode
->path
, path
);
4310 strlcpy(pnode
->alias
, alias
);
4319 data
->list
= tail
= pnode
;
4328 static int ventoy_parse_menu_tip(VTOY_JSON
*json
, void *p
)
4331 const char *path
= NULL
;
4332 const char *tip
= NULL
;
4333 data_tip
*data
= (data_tip
*)p
;
4334 data_tip_node
*tail
= NULL
;
4335 data_tip_node
*pnode
= NULL
;
4336 VTOY_JSON
*node
= NULL
;
4337 VTOY_JSON
*tips
= NULL
;
4339 if (json
->enDataType
!= JSON_TYPE_OBJECT
)
4344 vtoy_json_get_string(json
->pstChild
, "left", sizeof(data
->left
), data
->left
);
4345 vtoy_json_get_string(json
->pstChild
, "top", sizeof(data
->top
), data
->top
);
4346 vtoy_json_get_string(json
->pstChild
, "color", sizeof(data
->color
), data
->color
);
4348 tips
= vtoy_json_find_item(json
->pstChild
, JSON_TYPE_ARRAY
, "tips");
4354 for (node
= tips
->pstChild
; node
; node
= node
->pstNext
)
4356 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4361 type
= path_type_file
;
4362 path
= vtoy_json_get_string_ex(node
->pstChild
, "image");
4365 path
= vtoy_json_get_string_ex(node
->pstChild
, "dir");
4366 type
= path_type_dir
;
4368 tip
= vtoy_json_get_string_ex(node
->pstChild
, "tip");
4372 pnode
= zalloc(sizeof(data_tip_node
));
4376 strlcpy(pnode
->path
, path
);
4377 strlcpy(pnode
->tip
, tip
);
4386 data
->list
= tail
= pnode
;
4394 static int ventoy_parse_menu_class(VTOY_JSON
*json
, void *p
)
4397 const char *path
= NULL
;
4398 const char *class = NULL
;
4399 data_class
*data
= (data_class
*)p
;
4400 data_class_node
*tail
= NULL
;
4401 data_class_node
*pnode
= NULL
;
4402 VTOY_JSON
*node
= NULL
;
4404 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4409 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4411 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4416 type
= class_type_key
;
4417 path
= vtoy_json_get_string_ex(node
->pstChild
, "key");
4420 type
= class_type_dir
;
4421 path
= vtoy_json_get_string_ex(node
->pstChild
, "dir");
4424 type
= class_type_parent
;
4425 path
= vtoy_json_get_string_ex(node
->pstChild
, "parent");
4428 class = vtoy_json_get_string_ex(node
->pstChild
, "class");
4432 pnode
= zalloc(sizeof(data_class_node
));
4436 strlcpy(pnode
->path
, path
);
4437 strlcpy(pnode
->class, class);
4446 data
->list
= tail
= pnode
;
4454 static int ventoy_parse_auto_install(VTOY_JSON
*json
, void *p
)
4462 const char *path
= NULL
;
4463 const char *file
= NULL
;
4464 data_auto_install
*data
= (data_auto_install
*)p
;
4465 auto_install_node
*tail
= NULL
;
4466 auto_install_node
*pnode
= NULL
;
4467 path_node
*pathnode
= NULL
;
4468 path_node
*pathtail
= NULL
;
4469 VTOY_JSON
*node
= NULL
;
4470 VTOY_JSON
*filelist
= NULL
;
4471 VTOY_JSON
*filenode
= NULL
;
4473 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4478 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4480 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4486 path
= vtoy_json_get_string_ex(node
->pstChild
, "image");
4489 path
= vtoy_json_get_string_ex(node
->pstChild
, "parent");
4497 file
= vtoy_json_get_string_ex(node
->pstChild
, "template");
4500 pnode
= zalloc(sizeof(auto_install_node
));
4505 strlcpy(pnode
->path
, path
);
4507 pathnode
= zalloc(sizeof(path_node
));
4510 strlcpy(pathnode
->path
, file
);
4511 pnode
->list
= pathnode
;
4525 data
->list
= tail
= pnode
;
4533 timeouten
= autoselen
= 0;
4534 if (JSON_SUCCESS
== vtoy_json_get_int(node
->pstChild
, "timeout", &timeout
))
4538 if (JSON_SUCCESS
== vtoy_json_get_int(node
->pstChild
, "autosel", &autosel
))
4543 filelist
= vtoy_json_find_item(node
->pstChild
, JSON_TYPE_ARRAY
, "template");
4549 pnode
= zalloc(sizeof(auto_install_node
));
4556 pnode
->autoselen
= autoselen
;
4557 pnode
->timeouten
= timeouten
;
4558 pnode
->timeout
= timeout
;
4559 pnode
->autosel
= autosel
;
4560 strlcpy(pnode
->path
, path
);
4563 for (filenode
= filelist
->pstChild
; filenode
; filenode
= filenode
->pstNext
)
4565 if (filenode
->enDataType
!= JSON_TYPE_STRING
)
4570 pathnode
= zalloc(sizeof(path_node
));
4574 strlcpy(pathnode
->path
, filenode
->unData
.pcStrVal
);
4578 pathtail
->next
= pathnode
;
4579 pathtail
= pathnode
;
4583 pnode
->list
= pathtail
= pathnode
;
4594 if (pnode
->autoselen
&& pnode
->autosel
> count
)
4606 data
->list
= tail
= pnode
;
4613 static int ventoy_parse_persistence(VTOY_JSON
*json
, void *p
)
4620 const char *path
= NULL
;
4621 const char *file
= NULL
;
4622 data_persistence
*data
= (data_persistence
*)p
;
4623 persistence_node
*tail
= NULL
;
4624 persistence_node
*pnode
= NULL
;
4625 path_node
*pathnode
= NULL
;
4626 path_node
*pathtail
= NULL
;
4627 VTOY_JSON
*node
= NULL
;
4628 VTOY_JSON
*filelist
= NULL
;
4629 VTOY_JSON
*filenode
= NULL
;
4631 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4636 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4638 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4643 path
= vtoy_json_get_string_ex(node
->pstChild
, "image");
4649 file
= vtoy_json_get_string_ex(node
->pstChild
, "backend");
4652 pnode
= zalloc(sizeof(persistence_node
));
4657 strlcpy(pnode
->path
, path
);
4659 pathnode
= zalloc(sizeof(path_node
));
4662 strlcpy(pathnode
->path
, file
);
4663 pnode
->list
= pathnode
;
4677 data
->list
= tail
= pnode
;
4685 timeouten
= autoselen
= 0;
4686 if (JSON_SUCCESS
== vtoy_json_get_int(node
->pstChild
, "timeout", &timeout
))
4690 if (JSON_SUCCESS
== vtoy_json_get_int(node
->pstChild
, "autosel", &autosel
))
4695 filelist
= vtoy_json_find_item(node
->pstChild
, JSON_TYPE_ARRAY
, "backend");
4701 pnode
= zalloc(sizeof(persistence_node
));
4708 pnode
->autoselen
= autoselen
;
4709 pnode
->timeouten
= timeouten
;
4710 pnode
->timeout
= timeout
;
4711 pnode
->autosel
= autosel
;
4712 strlcpy(pnode
->path
, path
);
4715 for (filenode
= filelist
->pstChild
; filenode
; filenode
= filenode
->pstNext
)
4717 if (filenode
->enDataType
!= JSON_TYPE_STRING
)
4722 pathnode
= zalloc(sizeof(path_node
));
4726 strlcpy(pathnode
->path
, filenode
->unData
.pcStrVal
);
4730 pathtail
->next
= pathnode
;
4731 pathtail
= pathnode
;
4735 pnode
->list
= pathtail
= pathnode
;
4746 if (pnode
->autoselen
&& pnode
->autosel
> count
)
4758 data
->list
= tail
= pnode
;
4765 static int ventoy_parse_injection(VTOY_JSON
*json
, void *p
)
4768 const char *path
= NULL
;
4769 const char *archive
= NULL
;
4770 data_injection
*data
= (data_injection
*)p
;
4771 injection_node
*tail
= NULL
;
4772 injection_node
*pnode
= NULL
;
4773 VTOY_JSON
*node
= NULL
;
4775 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4780 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4782 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4788 path
= vtoy_json_get_string_ex(node
->pstChild
, "image");
4791 path
= vtoy_json_get_string_ex(node
->pstChild
, "parent");
4794 archive
= vtoy_json_get_string_ex(node
->pstChild
, "archive");
4796 if (path
&& archive
)
4798 pnode
= zalloc(sizeof(injection_node
));
4802 strlcpy(pnode
->path
, path
);
4803 strlcpy(pnode
->archive
, archive
);
4812 data
->list
= tail
= pnode
;
4820 static int ventoy_parse_conf_replace(VTOY_JSON
*json
, void *p
)
4823 const char *path
= NULL
;
4824 const char *org
= NULL
;
4825 const char *new = NULL
;
4826 data_conf_replace
*data
= (data_conf_replace
*)p
;
4827 conf_replace_node
*tail
= NULL
;
4828 conf_replace_node
*pnode
= NULL
;
4829 VTOY_JSON
*node
= NULL
;
4831 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4836 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4838 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4843 path
= vtoy_json_get_string_ex(node
->pstChild
, "iso");
4844 org
= vtoy_json_get_string_ex(node
->pstChild
, "org");
4845 new = vtoy_json_get_string_ex(node
->pstChild
, "new");
4848 vtoy_json_get_int(node
->pstChild
, "img", &img
);
4850 if (path
&& org
&& new)
4852 pnode
= zalloc(sizeof(conf_replace_node
));
4855 strlcpy(pnode
->path
, path
);
4856 strlcpy(pnode
->org
, org
);
4857 strlcpy(pnode
->new, new);
4870 data
->list
= tail
= pnode
;
4878 static int ventoy_parse_password(VTOY_JSON
*json
, void *p
)
4881 const char *bootpwd
= NULL
;
4882 const char *isopwd
= NULL
;
4883 const char *wimpwd
= NULL
;
4884 const char *imgpwd
= NULL
;
4885 const char *efipwd
= NULL
;
4886 const char *vhdpwd
= NULL
;
4887 const char *vtoypwd
= NULL
;
4888 const char *path
= NULL
;
4889 const char *pwd
= NULL
;
4890 data_password
*data
= (data_password
*)p
;
4891 menu_password
*tail
= NULL
;
4892 menu_password
*pnode
= NULL
;
4893 VTOY_JSON
*node
= NULL
;
4894 VTOY_JSON
*menupwd
= NULL
;
4896 if (json
->enDataType
!= JSON_TYPE_OBJECT
)
4901 bootpwd
= vtoy_json_get_string_ex(json
->pstChild
, "bootpwd");
4902 isopwd
= vtoy_json_get_string_ex(json
->pstChild
, "isopwd");
4903 wimpwd
= vtoy_json_get_string_ex(json
->pstChild
, "wimpwd");
4904 imgpwd
= vtoy_json_get_string_ex(json
->pstChild
, "imgpwd");
4905 efipwd
= vtoy_json_get_string_ex(json
->pstChild
, "efipwd");
4906 vhdpwd
= vtoy_json_get_string_ex(json
->pstChild
, "vhdpwd");
4907 vtoypwd
= vtoy_json_get_string_ex(json
->pstChild
, "vtoypwd");
4910 if (bootpwd
) strlcpy(data
->bootpwd
, bootpwd
);
4911 if (isopwd
) strlcpy(data
->isopwd
, isopwd
);
4912 if (wimpwd
) strlcpy(data
->wimpwd
, wimpwd
);
4913 if (imgpwd
) strlcpy(data
->imgpwd
, imgpwd
);
4914 if (efipwd
) strlcpy(data
->efipwd
, efipwd
);
4915 if (vhdpwd
) strlcpy(data
->vhdpwd
, vhdpwd
);
4916 if (vtoypwd
) strlcpy(data
->vtoypwd
, vtoypwd
);
4919 menupwd
= vtoy_json_find_item(json
->pstChild
, JSON_TYPE_ARRAY
, "menupwd");
4925 for (node
= menupwd
->pstChild
; node
; node
= node
->pstNext
)
4927 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4933 path
= vtoy_json_get_string_ex(node
->pstChild
, "file");
4936 path
= vtoy_json_get_string_ex(node
->pstChild
, "parent");
4939 pwd
= vtoy_json_get_string_ex(node
->pstChild
, "pwd");
4943 pnode
= zalloc(sizeof(menu_password
));
4947 strlcpy(pnode
->path
, path
);
4948 strlcpy(pnode
->pwd
, pwd
);
4957 data
->list
= tail
= pnode
;
4966 static int ventoy_parse_image_list_real(VTOY_JSON
*json
, int type
, void *p
)
4968 VTOY_JSON
*node
= NULL
;
4969 data_image_list
*data
= (data_image_list
*)p
;
4970 path_node
*tail
= NULL
;
4971 path_node
*pnode
= NULL
;
4973 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4980 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4982 if (node
->enDataType
== JSON_TYPE_STRING
)
4984 pnode
= zalloc(sizeof(path_node
));
4987 strlcpy(pnode
->path
, node
->unData
.pcStrVal
);
4995 data
->list
= tail
= pnode
;
5003 static int ventoy_parse_image_blacklist(VTOY_JSON
*json
, void *p
)
5005 return ventoy_parse_image_list_real(json
, 1, p
);
5007 static int ventoy_parse_image_list(VTOY_JSON
*json
, void *p
)
5009 return ventoy_parse_image_list_real(json
, 0, p
);
5012 static int ventoy_parse_auto_memdisk(VTOY_JSON
*json
, void *p
)
5014 VTOY_JSON
*node
= NULL
;
5015 data_auto_memdisk
*data
= (data_auto_memdisk
*)p
;
5016 path_node
*tail
= NULL
;
5017 path_node
*pnode
= NULL
;
5019 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
5024 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
5026 if (node
->enDataType
== JSON_TYPE_STRING
)
5028 pnode
= zalloc(sizeof(path_node
));
5031 strlcpy(pnode
->path
, node
->unData
.pcStrVal
);
5039 data
->list
= tail
= pnode
;
5047 static int ventoy_parse_dud(VTOY_JSON
*json
, void *p
)
5050 const char *path
= NULL
;
5051 const char *file
= NULL
;
5052 data_dud
*data
= (data_dud
*)p
;
5053 dud_node
*tail
= NULL
;
5054 dud_node
*pnode
= NULL
;
5055 path_node
*pathnode
= NULL
;
5056 path_node
*pathtail
= NULL
;
5057 VTOY_JSON
*node
= NULL
;
5058 VTOY_JSON
*filelist
= NULL
;
5059 VTOY_JSON
*filenode
= NULL
;
5061 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
5066 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
5068 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
5073 path
= vtoy_json_get_string_ex(node
->pstChild
, "image");
5079 file
= vtoy_json_get_string_ex(node
->pstChild
, "dud");
5082 pnode
= zalloc(sizeof(dud_node
));
5085 strlcpy(pnode
->path
, path
);
5087 pathnode
= zalloc(sizeof(path_node
));
5090 strlcpy(pathnode
->path
, file
);
5091 pnode
->list
= pathnode
;
5105 data
->list
= tail
= pnode
;
5112 filelist
= vtoy_json_find_item(node
->pstChild
, JSON_TYPE_ARRAY
, "dud");
5118 pnode
= zalloc(sizeof(dud_node
));
5124 strlcpy(pnode
->path
, path
);
5126 for (filenode
= filelist
->pstChild
; filenode
; filenode
= filenode
->pstNext
)
5128 if (filenode
->enDataType
!= JSON_TYPE_STRING
)
5133 pathnode
= zalloc(sizeof(path_node
));
5136 strlcpy(pathnode
->path
, filenode
->unData
.pcStrVal
);
5141 pathtail
->next
= pathnode
;
5142 pathtail
= pathnode
;
5146 pnode
->list
= pathtail
= pathnode
;
5164 data
->list
= tail
= pnode
;
5178 static int ventoy_load_old_json(const char *filename
)
5183 char *buffer
= NULL
;
5184 unsigned char *start
= NULL
;
5185 VTOY_JSON
*json
= NULL
;
5186 VTOY_JSON
*node
= NULL
;
5187 VTOY_JSON
*next
= NULL
;
5189 ret
= ventoy_read_file_to_buf(filename
, 4, (void **)&buffer
, &buflen
);
5192 vlog("Failed to read old ventoy.json file.\n");
5197 start
= (unsigned char *)buffer
;
5199 if (start
[0] == 0xef && start
[1] == 0xbb && start
[2] == 0xbf)
5203 else if ((start
[0] == 0xff && start
[1] == 0xfe) || (start
[0] == 0xfe && start
[1] == 0xff))
5205 vlog("ventoy.json is in UCS-2 encoding, ignore it.\n");
5210 json
= vtoy_json_create();
5217 if (vtoy_json_parse_ex(json
, buffer
+ offset
, buflen
- offset
) == JSON_SUCCESS
)
5219 vlog("parse ventoy.json success\n");
5221 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
5222 for (next
= node
->pstNext
; next
; next
= next
->pstNext
)
5224 if (node
->pcName
&& next
->pcName
&& strcmp(node
->pcName
, next
->pcName
) == 0)
5226 vlog("ventoy.json contains duplicate key <%s>.\n", node
->pcName
);
5227 g_sysinfo
.invalid_config
= 1;
5233 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
5235 ventoy_parse_json(control
);
5236 ventoy_parse_json(theme
);
5237 ventoy_parse_json(menu_alias
);
5238 ventoy_parse_json(menu_tip
);
5239 ventoy_parse_json(menu_class
);
5240 ventoy_parse_json(auto_install
);
5241 ventoy_parse_json(persistence
);
5242 ventoy_parse_json(injection
);
5243 ventoy_parse_json(conf_replace
);
5244 ventoy_parse_json(password
);
5245 ventoy_parse_json(image_list
);
5246 ventoy_parse_json(image_blacklist
);
5247 ventoy_parse_json(auto_memdisk
);
5248 ventoy_parse_json(dud
);
5253 vlog("ventoy.json has syntax error.\n");
5254 g_sysinfo
.syntax_error
= 1;
5259 vtoy_json_destroy(json
);
5266 int ventoy_http_start(const char *ip
, const char *port
)
5272 char backupname
[128];
5273 struct mg_callbacks callbacks
;
5274 const char *options
[] =
5276 "listening_ports", "24681",
5277 "document_root", "www",
5278 "index_files", "index.html",
5279 "num_threads", "16",
5280 "error_log_file", LOG_FILE
,
5281 "request_timeout_ms", "10000",
5285 for (i
= 0; i
<= bios_max
; i
++)
5287 ventoy_data_default_control(g_data_control
+ i
);
5288 ventoy_data_default_theme(g_data_theme
+ i
);
5289 ventoy_data_default_menu_alias(g_data_menu_alias
+ i
);
5290 ventoy_data_default_menu_class(g_data_menu_class
+ i
);
5291 ventoy_data_default_menu_tip(g_data_menu_tip
+ i
);
5292 ventoy_data_default_auto_install(g_data_auto_install
+ i
);
5293 ventoy_data_default_persistence(g_data_persistence
+ i
);
5294 ventoy_data_default_injection(g_data_injection
+ i
);
5295 ventoy_data_default_conf_replace(g_data_conf_replace
+ i
);
5296 ventoy_data_default_password(g_data_password
+ i
);
5297 ventoy_data_default_image_list(g_data_image_list
+ i
);
5298 ventoy_data_default_auto_memdisk(g_data_auto_memdisk
+ i
);
5299 ventoy_data_default_dud(g_data_dud
+ i
);
5302 ventoy_get_json_path(filename
, backupname
);
5303 if (ventoy_is_file_exist("%s", filename
))
5305 ventoy_copy_file(filename
, backupname
);
5306 ret
= ventoy_load_old_json(filename
);
5309 ventoy_data_real_save_all(0);
5314 scnprintf(addr
, sizeof(addr
), "%s:%s", ip
, port
);
5317 memset(&callbacks
, 0, sizeof(callbacks
));
5318 callbacks
.begin_request
= ventoy_request_handler
;
5320 callbacks
.open_file
= ventoy_web_openfile
;
5322 g_ventoy_http_ctx
= mg_start(&callbacks
, NULL
, options
);
5324 ventoy_start_writeback_thread(ventoy_http_writeback
);
5326 return g_ventoy_http_ctx
? 0 : 1;
5329 int ventoy_http_stop(void)
5331 if (g_ventoy_http_ctx
)
5333 mg_stop(g_ventoy_http_ctx
);
5336 ventoy_stop_writeback_thread();
5340 int ventoy_http_init(void)
5345 char *Buffer
= NULL
;
5348 ventoy_read_file_to_buf("www/menulist", 4, (void **)&Buffer
, &BufLen
);
5351 for (i
= 0; i
< BufLen
/ 5; i
++)
5353 memcpy(g_ventoy_menu_lang
[i
], Buffer
+ i
* 5, 5);
5354 g_ventoy_menu_lang
[i
][5] = 0;
5361 file
= ventoy_tar_find_file("www/menulist");
5364 for (i
= 0; i
< file
->size
/ 5; i
++)
5366 memcpy(g_ventoy_menu_lang
[i
], (char *)(file
->addr
) + i
* 5, 5);
5367 g_ventoy_menu_lang
[i
][5] = 0;
5372 if (!g_pub_json_buffer
)
5374 g_pub_json_buffer
= malloc(JSON_BUF_MAX
* 2);
5375 g_pub_save_buffer
= g_pub_json_buffer
+ JSON_BUF_MAX
;
5379 pthread_mutex_init(&g_api_mutex
, NULL
);
5383 void ventoy_http_exit(void)
5385 check_free(g_pub_json_buffer
);
5386 g_pub_json_buffer
= NULL
;
5387 g_pub_save_buffer
= NULL
;
5389 pthread_mutex_destroy(&g_api_mutex
);