1 /******************************************************************************
2 * ventoy_http.c ---- ventoy http
3 * Copyright (c) 2021, longpanda <admin@ventoy.net>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 3 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
28 #if defined(_MSC_VER) || defined(WIN32)
32 #include <sys/types.h>
33 #include <sys/ioctl.h>
35 #include <sys/types.h>
36 #include <sys/mount.h>
38 #include <linux/limits.h>
43 #include <ventoy_define.h>
44 #include <ventoy_json.h>
45 #include <ventoy_util.h>
46 #include <ventoy_disk.h>
47 #include <ventoy_http.h>
48 #include "fat_filelib.h"
50 static const char *g_json_title_postfix
[bios_max
+ 1] =
52 "", "_legacy", "_uefi", "_ia32", "_aa64", "_mips", ""
55 static const char *g_ventoy_kbd_layout
[] =
57 "QWERTY_USA", "AZERTY", "CZECH_QWERTY", "CZECH_QWERTZ", "DANISH",
58 "DVORAK_USA", "FRENCH", "GERMAN", "ITALIANO", "JAPAN_106", "LATIN_USA",
59 "PORTU_BRAZIL", "QWERTY_UK", "QWERTZ", "QWERTZ_HUN", "QWERTZ_SLOV_CROAT",
60 "SPANISH", "SWEDISH", "TURKISH_Q", "VIETNAMESE",
64 #define VTOY_DEL_ALL_PATH "4119ae33-98ea-448e-b9c0-569aafcf1fb4"
66 static int g_json_exist
[plugin_type_max
][bios_max
];
67 static const char *g_plugin_name
[plugin_type_max
] =
69 "control", "theme", "menu_alias", "menu_tip",
70 "menu_class", "auto_install", "persistence", "injection",
71 "conf_replace", "password", "image_list",
75 static char g_ventoy_menu_lang
[MAX_LANGUAGE
][8];
77 static char g_pub_path
[2 * MAX_PATH
];
78 static data_control g_data_control
[bios_max
+ 1];
79 static data_theme g_data_theme
[bios_max
+ 1];
80 static data_alias g_data_menu_alias
[bios_max
+ 1];
81 static data_tip g_data_menu_tip
[bios_max
+ 1];
82 static data_class g_data_menu_class
[bios_max
+ 1];
83 static data_image_list g_data_image_list
[bios_max
+ 1];
84 static data_image_list
*g_data_image_blacklist
= g_data_image_list
;
85 static data_auto_memdisk g_data_auto_memdisk
[bios_max
+ 1];
86 static data_password g_data_password
[bios_max
+ 1];
87 static data_conf_replace g_data_conf_replace
[bios_max
+ 1];
88 static data_injection g_data_injection
[bios_max
+ 1];
89 static data_auto_install g_data_auto_install
[bios_max
+ 1];
90 static data_persistence g_data_persistence
[bios_max
+ 1];
91 static data_dud g_data_dud
[bios_max
+ 1];
93 static char *g_pub_json_buffer
= NULL
;
94 static char *g_pub_save_buffer
= NULL
;
95 #define JSON_BUFFER g_pub_json_buffer
96 #define JSON_SAVE_BUFFER g_pub_save_buffer
98 static pthread_mutex_t g_api_mutex
;
99 static struct mg_context
*g_ventoy_http_ctx
= NULL
;
101 #define ventoy_is_real_exist_common(xpath, xnode, xtype) \
102 ventoy_path_is_real_exist(xpath, xnode, offsetof(xtype, path), offsetof(xtype, next))
104 static int ventoy_is_kbd_valid(const char *key
)
108 for (i
= 0; g_ventoy_kbd_layout
[i
]; i
++)
110 if (strcmp(g_ventoy_kbd_layout
[i
], key
) == 0)
119 static const char * ventoy_real_path(const char *org
)
123 if (g_sysinfo
.pathcase
)
125 scnprintf(g_pub_path
, MAX_PATH
, "%s", org
);
126 count
= ventoy_path_case(g_pub_path
+ 1, 1);
140 static int ventoy_json_result(struct mg_connection
*conn
, const char *err
)
143 "HTTP/1.1 200 OK \r\n"
144 "Content-Type: application/json\r\n"
145 "Content-Length: %d\r\n"
147 (int)strlen(err
), err
);
152 static int ventoy_json_buffer(struct mg_connection
*conn
, const char *json_buf
, int json_len
)
155 "HTTP/1.1 200 OK \r\n"
156 "Content-Type: application/json\r\n"
157 "Content-Length: %d\r\n"
164 static void ventoy_free_path_node_list(path_node
*list
)
166 path_node
*next
= NULL
;
167 path_node
*node
= list
;
177 static int ventoy_path_is_real_exist(const char *path
, void *head
, size_t pathoff
, size_t nextoff
)
180 const char *nodepath
= NULL
;
181 const char *realpath
= NULL
;
182 char pathbuf
[MAX_PATH
];
184 if (strchr(path
, '*'))
189 realpath
= ventoy_real_path(path
);
190 scnprintf(pathbuf
, sizeof(pathbuf
), "%s", realpath
);
195 nodepath
= node
+ pathoff
;
196 if (NULL
== strchr(nodepath
, '*'))
198 realpath
= ventoy_real_path(nodepath
);
199 if (strcmp(pathbuf
, realpath
) == 0)
205 memcpy(&node
, node
+ nextoff
, sizeof(node
));
211 static path_node
* ventoy_path_node_add_array(VTOY_JSON
*array
)
213 path_node
*head
= NULL
;
214 path_node
*node
= NULL
;
215 path_node
*cur
= NULL
;
216 VTOY_JSON
*item
= NULL
;
218 for (item
= array
->pstChild
; item
; item
= item
->pstNext
)
220 node
= zalloc(sizeof(path_node
));
223 scnprintf(node
->path
, sizeof(node
->path
), "%s", item
->unData
.pcStrVal
);
224 vtoy_list_add(head
, cur
, node
);
231 static int ventoy_check_fuzzy_path(char *path
, int prefix
)
243 pos
= strchr(path
, '*');
246 for (cur
= pos
; *cur
; cur
++)
271 rc
= ventoy_is_directory_exist("%s%s", g_cur_dir
, path
);
275 rc
= ventoy_is_directory_exist("%s", path
);
296 return ventoy_is_file_exist("%s%s", g_cur_dir
, path
);
300 return ventoy_is_file_exist("%s", path
);
305 static int ventoy_path_list_cmp(path_node
*list1
, path_node
*list2
)
307 if (NULL
== list1
&& NULL
== list2
)
311 else if (list1
&& list2
)
313 while (list1
&& list2
)
315 if (strcmp(list1
->path
, list2
->path
))
324 if (list1
== NULL
&& list2
== NULL
)
337 static int ventoy_api_device_info(struct mg_connection
*conn
, VTOY_JSON
*json
)
343 VTOY_JSON_FMT_BEGIN(pos
, JSON_BUFFER
, JSON_BUF_MAX
);
344 VTOY_JSON_FMT_OBJ_BEGIN();
346 VTOY_JSON_FMT_STRN("dev_name", g_sysinfo
.cur_model
);
347 VTOY_JSON_FMT_STRN("dev_capacity", g_sysinfo
.cur_capacity
);
348 VTOY_JSON_FMT_STRN("dev_fs", g_sysinfo
.cur_fsname
);
349 VTOY_JSON_FMT_STRN("ventoy_ver", g_sysinfo
.cur_ventoy_ver
);
350 VTOY_JSON_FMT_SINT("part_style", g_sysinfo
.cur_part_style
);
351 VTOY_JSON_FMT_SINT("secure_boot", g_sysinfo
.cur_secureboot
);
353 VTOY_JSON_FMT_OBJ_END();
354 VTOY_JSON_FMT_END(pos
);
356 ventoy_json_buffer(conn
, JSON_BUFFER
, pos
);
360 static int ventoy_api_sysinfo(struct mg_connection
*conn
, VTOY_JSON
*json
)
366 VTOY_JSON_FMT_BEGIN(pos
, JSON_BUFFER
, JSON_BUF_MAX
);
367 VTOY_JSON_FMT_OBJ_BEGIN();
368 VTOY_JSON_FMT_STRN("language", ventoy_get_os_language());
369 VTOY_JSON_FMT_STRN("curdir", g_cur_dir
);
372 VTOY_JSON_FMT_SINT("syntax_error", g_sysinfo
.syntax_error
);
373 g_sysinfo
.syntax_error
= 0;
375 VTOY_JSON_FMT_SINT("invalid_config", g_sysinfo
.invalid_config
);
376 g_sysinfo
.invalid_config
= 0;
378 #if defined(_MSC_VER) || defined(WIN32)
379 VTOY_JSON_FMT_STRN("os", "windows");
381 VTOY_JSON_FMT_STRN("os", "linux");
384 VTOY_JSON_FMT_OBJ_END();
385 VTOY_JSON_FMT_END(pos
);
387 ventoy_json_buffer(conn
, JSON_BUFFER
, pos
);
391 static int ventoy_api_handshake(struct mg_connection
*conn
, VTOY_JSON
*json
)
400 VTOY_JSON_FMT_BEGIN(pos
, JSON_BUFFER
, JSON_BUF_MAX
);
401 VTOY_JSON_FMT_OBJ_BEGIN();
402 VTOY_JSON_FMT_SINT("status", 0);
403 VTOY_JSON_FMT_SINT("save_error", g_sysinfo
.config_save_error
);
404 g_sysinfo
.config_save_error
= 0;
406 for (i
= 0; i
< plugin_type_max
; i
++)
408 scnprintf(key
, sizeof(key
), "exist_%s", g_plugin_name
[i
]);
409 VTOY_JSON_FMT_KEY(key
);
410 VTOY_JSON_FMT_ARY_BEGIN();
411 for (j
= 0; j
< bios_max
; j
++)
413 VTOY_JSON_FMT_ITEM_INT(g_json_exist
[i
][j
]);
415 VTOY_JSON_FMT_ARY_ENDEX();
418 VTOY_JSON_FMT_OBJ_END();
419 VTOY_JSON_FMT_END(pos
);
421 ventoy_json_buffer(conn
, JSON_BUFFER
, pos
);
425 static int ventoy_api_check_exist(struct mg_connection
*conn
, VTOY_JSON
*json
)
430 const char *path
= NULL
;
432 path
= vtoy_json_get_string_ex(json
, "path");
433 vtoy_json_get_int(json
, "dir", &dir
);
439 exist
= ventoy_is_directory_exist("%s", path
);
443 exist
= ventoy_is_file_exist("%s", path
);
447 VTOY_JSON_FMT_BEGIN(pos
, JSON_BUFFER
, JSON_BUF_MAX
);
448 VTOY_JSON_FMT_OBJ_BEGIN();
449 VTOY_JSON_FMT_SINT("exist", exist
);
450 VTOY_JSON_FMT_OBJ_END();
451 VTOY_JSON_FMT_END(pos
);
453 ventoy_json_buffer(conn
, JSON_BUFFER
, pos
);
457 static int ventoy_api_check_exist2(struct mg_connection
*conn
, VTOY_JSON
*json
)
466 const char *path1
= NULL
;
467 const char *path2
= NULL
;
469 path1
= vtoy_json_get_string_ex(json
, "path1");
470 path2
= vtoy_json_get_string_ex(json
, "path2");
471 vtoy_json_get_int(json
, "dir1", &dir1
);
472 vtoy_json_get_int(json
, "dir2", &dir2
);
473 vtoy_json_get_int(json
, "fuzzy1", &fuzzy1
);
474 vtoy_json_get_int(json
, "fuzzy2", &fuzzy2
);
480 exist1
= ventoy_is_directory_exist("%s", path1
);
486 exist1
= ventoy_check_fuzzy_path((char *)path1
, 0);
490 exist1
= ventoy_is_file_exist("%s", path1
);
499 exist2
= ventoy_is_directory_exist("%s", path2
);
505 exist2
= ventoy_check_fuzzy_path((char *)path2
, 0);
509 exist2
= ventoy_is_file_exist("%s", path2
);
514 VTOY_JSON_FMT_BEGIN(pos
, JSON_BUFFER
, JSON_BUF_MAX
);
515 VTOY_JSON_FMT_OBJ_BEGIN();
516 VTOY_JSON_FMT_SINT("exist1", exist1
);
517 VTOY_JSON_FMT_SINT("exist2", exist2
);
518 VTOY_JSON_FMT_OBJ_END();
519 VTOY_JSON_FMT_END(pos
);
521 ventoy_json_buffer(conn
, JSON_BUFFER
, pos
);
525 static int ventoy_api_check_fuzzy(struct mg_connection
*conn
, VTOY_JSON
*json
)
529 const char *path
= NULL
;
531 path
= vtoy_json_get_string_ex(json
, "path");
534 exist
= ventoy_check_fuzzy_path((char *)path
, 0);
537 VTOY_JSON_FMT_BEGIN(pos
, JSON_BUFFER
, JSON_BUF_MAX
);
538 VTOY_JSON_FMT_OBJ_BEGIN();
539 VTOY_JSON_FMT_SINT("exist", exist
);
540 VTOY_JSON_FMT_OBJ_END();
541 VTOY_JSON_FMT_END(pos
);
543 ventoy_json_buffer(conn
, JSON_BUFFER
, pos
);
550 void ventoy_data_default_control(data_control
*data
)
552 memset(data
, 0, sizeof(data_control
));
554 data
->password_asterisk
= 1;
555 data
->secondary_menu
= 1;
556 data
->filter_dot_underscore
= 1;
557 data
->max_search_level
= -1;
558 data
->menu_timeout
= 0;
559 data
->secondary_menu_timeout
= 0;
561 strlcpy(data
->default_kbd_layout
, "QWERTY_USA");
562 strlcpy(data
->menu_language
, "en_US");
565 int ventoy_data_cmp_control(data_control
*data1
, data_control
*data2
)
567 if (data1
->default_menu_mode
!= data2
->default_menu_mode
||
568 data1
->treeview_style
!= data2
->treeview_style
||
569 data1
->filter_dot_underscore
!= data2
->filter_dot_underscore
||
570 data1
->sort_casesensitive
!= data2
->sort_casesensitive
||
571 data1
->max_search_level
!= data2
->max_search_level
||
572 data1
->vhd_no_warning
!= data2
->vhd_no_warning
||
573 data1
->filter_iso
!= data2
->filter_iso
||
574 data1
->filter_wim
!= data2
->filter_wim
||
575 data1
->filter_efi
!= data2
->filter_efi
||
576 data1
->filter_img
!= data2
->filter_img
||
577 data1
->filter_vhd
!= data2
->filter_vhd
||
578 data1
->filter_vtoy
!= data2
->filter_vtoy
||
579 data1
->win11_bypass_check
!= data2
->win11_bypass_check
||
580 data1
->linux_remount
!= data2
->linux_remount
||
581 data1
->password_asterisk
!= data2
->password_asterisk
||
582 data1
->secondary_menu
!= data2
->secondary_menu
||
583 data1
->menu_timeout
!= data2
->menu_timeout
||
584 data1
->secondary_menu_timeout
!= data2
->secondary_menu_timeout
)
589 if (strcmp(data1
->default_search_root
, data2
->default_search_root
) ||
590 strcmp(data1
->default_image
, data2
->default_image
) ||
591 strcmp(data1
->default_kbd_layout
, data2
->default_kbd_layout
) ||
592 strcmp(data1
->menu_language
, data2
->menu_language
))
600 int ventoy_data_save_control(data_control
*data
, const char *title
, char *buf
, int buflen
)
603 data_control
*def
= g_data_control
+ bios_max
;
605 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
607 VTOY_JSON_FMT_KEY_L(L1
, title
);
608 VTOY_JSON_FMT_ARY_BEGIN_N();
610 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_DEFAULT_MENU_MODE", default_menu_mode
);
611 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_TREE_VIEW_MENU_STYLE", treeview_style
);
612 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_FILT_DOT_UNDERSCORE_FILE", filter_dot_underscore
);
613 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_SORT_CASE_SENSITIVE", sort_casesensitive
);
615 if (data
->max_search_level
>= 0)
617 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_MAX_SEARCH_LEVEL", max_search_level
);
620 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_VHD_NO_WARNING", vhd_no_warning
);
621 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_FILE_FLT_ISO", filter_iso
);
622 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_FILE_FLT_WIM", filter_wim
);
623 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_FILE_FLT_EFI", filter_efi
);
624 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_FILE_FLT_IMG", filter_img
);
625 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_FILE_FLT_VHD", filter_vhd
);
626 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_FILE_FLT_VTOY", filter_vtoy
);
627 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_WIN11_BYPASS_CHECK", win11_bypass_check
);
628 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_LINUX_REMOUNT", linux_remount
);
629 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_SECONDARY_BOOT_MENU", secondary_menu
);
630 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_SHOW_PASSWORD_ASTERISK", password_asterisk
);
631 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_MENU_TIMEOUT", menu_timeout
);
632 VTOY_JSON_FMT_CTRL_INT(L2
, "VTOY_SECONDARY_TIMEOUT", secondary_menu_timeout
);
634 VTOY_JSON_FMT_CTRL_STRN(L2
, "VTOY_DEFAULT_KBD_LAYOUT", default_kbd_layout
);
635 VTOY_JSON_FMT_CTRL_STRN(L2
, "VTOY_MENU_LANGUAGE", menu_language
);
637 if (strcmp(def
->default_search_root
, data
->default_search_root
))
639 VTOY_JSON_FMT_CTRL_STRN_STR(L2
, "VTOY_DEFAULT_SEARCH_ROOT", ventoy_real_path(data
->default_search_root
));
642 if (strcmp(def
->default_image
, data
->default_image
))
644 VTOY_JSON_FMT_CTRL_STRN_STR(L2
, "VTOY_DEFAULT_IMAGE", ventoy_real_path(data
->default_image
));
647 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
648 VTOY_JSON_FMT_END(pos
);
653 int ventoy_data_json_control(data_control
*ctrl
, char *buf
, int buflen
)
659 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
660 VTOY_JSON_FMT_OBJ_BEGIN();
662 VTOY_JSON_FMT_SINT("default_menu_mode", ctrl
->default_menu_mode
);
663 VTOY_JSON_FMT_SINT("treeview_style", ctrl
->treeview_style
);
664 VTOY_JSON_FMT_SINT("filter_dot_underscore", ctrl
->filter_dot_underscore
);
665 VTOY_JSON_FMT_SINT("sort_casesensitive", ctrl
->sort_casesensitive
);
666 VTOY_JSON_FMT_SINT("max_search_level", ctrl
->max_search_level
);
667 VTOY_JSON_FMT_SINT("vhd_no_warning", ctrl
->vhd_no_warning
);
669 VTOY_JSON_FMT_SINT("filter_iso", ctrl
->filter_iso
);
670 VTOY_JSON_FMT_SINT("filter_wim", ctrl
->filter_wim
);
671 VTOY_JSON_FMT_SINT("filter_efi", ctrl
->filter_efi
);
672 VTOY_JSON_FMT_SINT("filter_img", ctrl
->filter_img
);
673 VTOY_JSON_FMT_SINT("filter_vhd", ctrl
->filter_vhd
);
674 VTOY_JSON_FMT_SINT("filter_vtoy", ctrl
->filter_vtoy
);
675 VTOY_JSON_FMT_SINT("win11_bypass_check", ctrl
->win11_bypass_check
);
676 VTOY_JSON_FMT_SINT("linux_remount", ctrl
->linux_remount
);
677 VTOY_JSON_FMT_SINT("secondary_menu", ctrl
->secondary_menu
);
678 VTOY_JSON_FMT_SINT("password_asterisk", ctrl
->password_asterisk
);
679 VTOY_JSON_FMT_SINT("menu_timeout", ctrl
->menu_timeout
);
680 VTOY_JSON_FMT_SINT("secondary_menu_timeout", ctrl
->secondary_menu_timeout
);
681 VTOY_JSON_FMT_STRN("default_kbd_layout", ctrl
->default_kbd_layout
);
682 VTOY_JSON_FMT_STRN("menu_language", ctrl
->menu_language
);
685 if (ctrl
->default_search_root
[0] && ventoy_is_directory_exist("%s%s", g_cur_dir
, ctrl
->default_search_root
))
689 VTOY_JSON_FMT_STRN("default_search_root", ctrl
->default_search_root
);
690 VTOY_JSON_FMT_SINT("default_search_root_valid", valid
);
694 if (ctrl
->default_image
[0] && ventoy_is_file_exist("%s%s", g_cur_dir
, ctrl
->default_image
))
698 VTOY_JSON_FMT_STRN("default_image", ctrl
->default_image
);
699 VTOY_JSON_FMT_SINT("default_image_valid", valid
);
701 VTOY_JSON_FMT_KEY("menu_list");
702 VTOY_JSON_FMT_ARY_BEGIN();
704 for (i
= 0; g_ventoy_menu_lang
[i
][0]; i
++)
706 VTOY_JSON_FMT_ITEM(g_ventoy_menu_lang
[i
]);
708 VTOY_JSON_FMT_ARY_ENDEX();
710 VTOY_JSON_FMT_OBJ_END();
711 VTOY_JSON_FMT_END(pos
);
716 static int ventoy_api_get_control(struct mg_connection
*conn
, VTOY_JSON
*json
)
718 api_get_func(conn
, json
, control
);
722 static int ventoy_api_save_control(struct mg_connection
*conn
, VTOY_JSON
*json
)
726 data_control
*ctrl
= NULL
;
728 vtoy_json_get_int(json
, "index", &index
);
729 ctrl
= g_data_control
+ index
;
731 VTOY_JSON_INT("default_menu_mode", ctrl
->default_menu_mode
);
732 VTOY_JSON_INT("treeview_style", ctrl
->treeview_style
);
733 VTOY_JSON_INT("filter_dot_underscore", ctrl
->filter_dot_underscore
);
734 VTOY_JSON_INT("sort_casesensitive", ctrl
->sort_casesensitive
);
735 VTOY_JSON_INT("max_search_level", ctrl
->max_search_level
);
736 VTOY_JSON_INT("vhd_no_warning", ctrl
->vhd_no_warning
);
737 VTOY_JSON_INT("filter_iso", ctrl
->filter_iso
);
738 VTOY_JSON_INT("filter_wim", ctrl
->filter_wim
);
739 VTOY_JSON_INT("filter_efi", ctrl
->filter_efi
);
740 VTOY_JSON_INT("filter_img", ctrl
->filter_img
);
741 VTOY_JSON_INT("filter_vhd", ctrl
->filter_vhd
);
742 VTOY_JSON_INT("filter_vtoy", ctrl
->filter_vtoy
);
743 VTOY_JSON_INT("win11_bypass_check", ctrl
->win11_bypass_check
);
744 VTOY_JSON_INT("linux_remount", ctrl
->linux_remount
);
745 VTOY_JSON_INT("secondary_menu", ctrl
->secondary_menu
);
746 VTOY_JSON_INT("password_asterisk", ctrl
->password_asterisk
);
747 VTOY_JSON_INT("menu_timeout", ctrl
->menu_timeout
);
748 VTOY_JSON_INT("secondary_menu_timeout", ctrl
->secondary_menu_timeout
);
750 VTOY_JSON_STR("default_image", ctrl
->default_image
);
751 VTOY_JSON_STR("default_search_root", ctrl
->default_search_root
);
752 VTOY_JSON_STR("menu_language", ctrl
->menu_language
);
753 VTOY_JSON_STR("default_kbd_layout", ctrl
->default_kbd_layout
);
755 ret
= ventoy_data_save_all();
757 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
765 void ventoy_data_default_theme(data_theme
*data
)
767 memset(data
, 0, sizeof(data_theme
));
768 strlcpy(data
->gfxmode
, "1024x768");
769 scnprintf(data
->ventoy_left
, sizeof(data
->ventoy_left
), "5%%");
770 scnprintf(data
->ventoy_top
, sizeof(data
->ventoy_top
), "95%%");
771 scnprintf(data
->ventoy_color
, sizeof(data
->ventoy_color
), "%s", "#0000ff");
774 int ventoy_data_cmp_theme(data_theme
*data1
, data_theme
*data2
)
776 if (data1
->display_mode
!= data2
->display_mode
||
777 strcmp(data1
->ventoy_left
, data2
->ventoy_left
) ||
778 strcmp(data1
->ventoy_top
, data2
->ventoy_top
) ||
779 strcmp(data1
->gfxmode
, data2
->gfxmode
) ||
780 strcmp(data1
->ventoy_color
, data2
->ventoy_color
)
786 if (ventoy_path_list_cmp(data1
->filelist
, data2
->filelist
))
791 if (ventoy_path_list_cmp(data1
->fontslist
, data2
->fontslist
))
800 int ventoy_data_save_theme(data_theme
*data
, const char *title
, char *buf
, int buflen
)
803 path_node
*node
= NULL
;
804 data_theme
*def
= g_data_theme
+ bios_max
;
806 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
808 VTOY_JSON_FMT_KEY_L(L1
, title
);
809 VTOY_JSON_FMT_OBJ_BEGIN_N();
813 if (data
->filelist
->next
)
815 VTOY_JSON_FMT_KEY_L(L2
, "file");
816 VTOY_JSON_FMT_ARY_BEGIN_N();
818 for (node
= data
->filelist
; node
; node
= node
->next
)
820 VTOY_JSON_FMT_ITEM_PATH_LN(L3
, node
->path
);
823 VTOY_JSON_FMT_ARY_ENDEX_LN(L2
);
825 if (def
->default_file
!= data
->default_file
)
827 VTOY_JSON_FMT_SINT_LN(L2
, "default_file", data
->default_file
);
832 VTOY_JSON_FMT_STRN_PATH_LN(L2
, "file", data
->filelist
->path
);
836 if (data
->display_mode
!= def
->display_mode
)
838 if (display_mode_cli
== data
->display_mode
)
840 VTOY_JSON_FMT_STRN_LN(L2
, "display_mode", "CLI");
842 else if (display_mode_serial
== data
->display_mode
)
844 VTOY_JSON_FMT_STRN_LN(L2
, "display_mode", "serial");
846 else if (display_mode_ser_console
== data
->display_mode
)
848 VTOY_JSON_FMT_STRN_LN(L2
, "display_mode", "serial_console");
852 VTOY_JSON_FMT_STRN_LN(L2
, "display_mode", "GUI");
856 VTOY_JSON_FMT_DIFF_STRN(L2
, "gfxmode", gfxmode
);
858 VTOY_JSON_FMT_DIFF_STRN(L2
, "ventoy_left", ventoy_left
);
859 VTOY_JSON_FMT_DIFF_STRN(L2
, "ventoy_top", ventoy_top
);
860 VTOY_JSON_FMT_DIFF_STRN(L2
, "ventoy_color", ventoy_color
);
864 VTOY_JSON_FMT_KEY_L(L2
, "fonts");
865 VTOY_JSON_FMT_ARY_BEGIN_N();
867 for (node
= data
->fontslist
; node
; node
= node
->next
)
869 VTOY_JSON_FMT_ITEM_PATH_LN(L3
, node
->path
);
872 VTOY_JSON_FMT_ARY_ENDEX_LN(L2
);
875 VTOY_JSON_FMT_OBJ_ENDEX_LN(L1
);
876 VTOY_JSON_FMT_END(pos
);
882 int ventoy_data_json_theme(data_theme
*data
, char *buf
, int buflen
)
885 path_node
*node
= NULL
;
887 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
888 VTOY_JSON_FMT_OBJ_BEGIN();
890 VTOY_JSON_FMT_SINT("default_file", data
->default_file
);
891 VTOY_JSON_FMT_SINT("display_mode", data
->display_mode
);
892 VTOY_JSON_FMT_STRN("gfxmode", data
->gfxmode
);
894 VTOY_JSON_FMT_STRN("ventoy_color", data
->ventoy_color
);
895 VTOY_JSON_FMT_STRN("ventoy_left", data
->ventoy_left
);
896 VTOY_JSON_FMT_STRN("ventoy_top", data
->ventoy_top
);
898 VTOY_JSON_FMT_KEY("filelist");
899 VTOY_JSON_FMT_ARY_BEGIN();
900 for (node
= data
->filelist
; node
; node
= node
->next
)
902 VTOY_JSON_FMT_OBJ_BEGIN();
903 VTOY_JSON_FMT_STRN("path", node
->path
);
904 VTOY_JSON_FMT_SINT("valid", ventoy_is_file_exist("%s%s", g_cur_dir
, node
->path
));
905 VTOY_JSON_FMT_OBJ_ENDEX();
907 VTOY_JSON_FMT_ARY_ENDEX();
909 VTOY_JSON_FMT_KEY("fontslist");
910 VTOY_JSON_FMT_ARY_BEGIN();
911 for (node
= data
->fontslist
; node
; node
= node
->next
)
913 VTOY_JSON_FMT_OBJ_BEGIN();
914 VTOY_JSON_FMT_STRN("path", node
->path
);
915 VTOY_JSON_FMT_SINT("valid", ventoy_is_file_exist("%s%s", g_cur_dir
, node
->path
));
916 VTOY_JSON_FMT_OBJ_ENDEX();
918 VTOY_JSON_FMT_ARY_ENDEX();
920 VTOY_JSON_FMT_OBJ_END();
921 VTOY_JSON_FMT_END(pos
);
926 static int ventoy_api_get_theme(struct mg_connection
*conn
, VTOY_JSON
*json
)
928 api_get_func(conn
, json
, theme
);
932 static int ventoy_api_save_theme(struct mg_connection
*conn
, VTOY_JSON
*json
)
936 data_theme
*data
= NULL
;
938 vtoy_json_get_int(json
, "index", &index
);
939 data
= g_data_theme
+ index
;
941 VTOY_JSON_INT("default_file", data
->default_file
);
942 VTOY_JSON_INT("display_mode", data
->display_mode
);
943 VTOY_JSON_STR("gfxmode", data
->gfxmode
);
944 VTOY_JSON_STR("ventoy_left", data
->ventoy_left
);
945 VTOY_JSON_STR("ventoy_top", data
->ventoy_top
);
946 VTOY_JSON_STR("ventoy_color", data
->ventoy_color
);
948 ret
= ventoy_data_save_all();
950 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
955 static int ventoy_api_theme_add_file(struct mg_connection
*conn
, VTOY_JSON
*json
)
959 const char *path
= NULL
;
960 path_node
*node
= NULL
;
961 path_node
*cur
= NULL
;
962 data_theme
*data
= NULL
;
964 vtoy_json_get_int(json
, "index", &index
);
965 data
= g_data_theme
+ index
;
967 path
= VTOY_JSON_STR_EX("path");
970 if (ventoy_is_real_exist_common(path
, data
->filelist
, path_node
))
972 ventoy_json_result(conn
, VTOY_JSON_DUPLICATE
);
976 node
= zalloc(sizeof(path_node
));
979 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
981 vtoy_list_add(data
->filelist
, cur
, node
);
985 ret
= ventoy_data_save_all();
987 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
991 static int ventoy_api_theme_del_file(struct mg_connection
*conn
, VTOY_JSON
*json
)
995 const char *path
= NULL
;
996 path_node
*node
= NULL
;
997 path_node
*last
= NULL
;
998 data_theme
*data
= NULL
;
1000 vtoy_json_get_int(json
, "index", &index
);
1001 data
= g_data_theme
+ index
;
1003 path
= VTOY_JSON_STR_EX("path");
1006 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
1008 vtoy_list_free(path_node
, data
->filelist
);
1012 vtoy_list_del(last
, node
, data
->filelist
, path
);
1016 ret
= ventoy_data_save_all();
1018 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1022 static int ventoy_api_theme_add_font(struct mg_connection
*conn
, VTOY_JSON
*json
)
1026 const char *path
= NULL
;
1027 path_node
*node
= NULL
;
1028 path_node
*cur
= NULL
;
1029 data_theme
*data
= NULL
;
1031 vtoy_json_get_int(json
, "index", &index
);
1032 data
= g_data_theme
+ index
;
1034 path
= VTOY_JSON_STR_EX("path");
1037 if (ventoy_is_real_exist_common(path
, data
->fontslist
, path_node
))
1039 ventoy_json_result(conn
, VTOY_JSON_DUPLICATE
);
1043 node
= zalloc(sizeof(path_node
));
1046 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
1047 vtoy_list_add(data
->fontslist
, cur
, node
);
1051 ret
= ventoy_data_save_all();
1053 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1058 static int ventoy_api_theme_del_font(struct mg_connection
*conn
, VTOY_JSON
*json
)
1062 const char *path
= NULL
;
1063 path_node
*node
= NULL
;
1064 path_node
*last
= NULL
;
1065 data_theme
*data
= NULL
;
1067 vtoy_json_get_int(json
, "index", &index
);
1068 data
= g_data_theme
+ index
;
1070 path
= VTOY_JSON_STR_EX("path");
1073 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
1075 vtoy_list_free(path_node
, data
->fontslist
);
1079 vtoy_list_del(last
, node
, data
->fontslist
, path
);
1083 ret
= ventoy_data_save_all();
1085 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1092 void ventoy_data_default_menu_alias(data_alias
*data
)
1094 memset(data
, 0, sizeof(data_alias
));
1097 int ventoy_data_cmp_menu_alias(data_alias
*data1
, data_alias
*data2
)
1099 data_alias_node
*list1
= NULL
;
1100 data_alias_node
*list2
= NULL
;
1102 if (NULL
== data1
->list
&& NULL
== data2
->list
)
1106 else if (data1
->list
&& data2
->list
)
1108 list1
= data1
->list
;
1109 list2
= data2
->list
;
1111 while (list1
&& list2
)
1113 if ((list1
->type
!= list2
->type
) ||
1114 strcmp(list1
->path
, list2
->path
) ||
1115 strcmp(list1
->alias
, list2
->alias
))
1120 list1
= list1
->next
;
1121 list2
= list2
->next
;
1124 if (list1
== NULL
&& list2
== NULL
)
1139 int ventoy_data_save_menu_alias(data_alias
*data
, const char *title
, char *buf
, int buflen
)
1142 data_alias_node
*node
= NULL
;
1144 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1146 VTOY_JSON_FMT_KEY_L(L1
, title
);
1147 VTOY_JSON_FMT_ARY_BEGIN_N();
1149 for (node
= data
->list
; node
; node
= node
->next
)
1151 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
1153 if (node
->type
== path_type_file
)
1155 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "image", node
->path
);
1159 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "dir", node
->path
);
1162 VTOY_JSON_FMT_STRN_EX_LN(L3
, "alias", node
->alias
);
1164 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
1167 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
1168 VTOY_JSON_FMT_END(pos
);
1174 int ventoy_data_json_menu_alias(data_alias
*data
, char *buf
, int buflen
)
1178 data_alias_node
*node
= NULL
;
1180 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1181 VTOY_JSON_FMT_ARY_BEGIN();
1183 for (node
= data
->list
; node
; node
= node
->next
)
1185 VTOY_JSON_FMT_OBJ_BEGIN();
1187 VTOY_JSON_FMT_UINT("type", node
->type
);
1188 VTOY_JSON_FMT_STRN("path", node
->path
);
1189 if (node
->type
== path_type_file
)
1191 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
1195 valid
= ventoy_is_directory_exist("%s%s", g_cur_dir
, node
->path
);
1198 VTOY_JSON_FMT_SINT("valid", valid
);
1199 VTOY_JSON_FMT_STRN("alias", node
->alias
);
1201 VTOY_JSON_FMT_OBJ_ENDEX();
1204 VTOY_JSON_FMT_ARY_END();
1205 VTOY_JSON_FMT_END(pos
);
1210 static int ventoy_api_get_alias(struct mg_connection
*conn
, VTOY_JSON
*json
)
1212 api_get_func(conn
, json
, menu_alias
);
1216 static int ventoy_api_save_alias(struct mg_connection
*conn
, VTOY_JSON
*json
)
1219 ret
= ventoy_data_save_all();
1221 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1225 static int ventoy_api_alias_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
1229 int type
= path_type_file
;
1230 const char *path
= NULL
;
1231 const char *alias
= NULL
;
1232 data_alias_node
*node
= NULL
;
1233 data_alias_node
*cur
= NULL
;
1234 data_alias
*data
= NULL
;
1236 vtoy_json_get_int(json
, "index", &index
);
1237 data
= g_data_menu_alias
+ index
;
1239 vtoy_json_get_int(json
, "type", &type
);
1241 path
= VTOY_JSON_STR_EX("path");
1242 alias
= VTOY_JSON_STR_EX("alias");
1245 if (ventoy_is_real_exist_common(path
, data
->list
, data_alias_node
))
1247 ventoy_json_result(conn
, VTOY_JSON_DUPLICATE
);
1251 node
= zalloc(sizeof(data_alias_node
));
1255 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
1256 scnprintf(node
->alias
, sizeof(node
->alias
), "%s", alias
);
1258 vtoy_list_add(data
->list
, cur
, node
);
1262 ret
= ventoy_data_save_all();
1264 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1268 static int ventoy_api_alias_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
1272 const char *path
= NULL
;
1273 data_alias_node
*last
= NULL
;
1274 data_alias_node
*node
= NULL
;
1275 data_alias
*data
= NULL
;
1277 vtoy_json_get_int(json
, "index", &index
);
1278 data
= g_data_menu_alias
+ index
;
1280 path
= VTOY_JSON_STR_EX("path");
1283 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
1285 vtoy_list_free(data_alias_node
, data
->list
);
1289 vtoy_list_del(last
, node
, data
->list
, path
);
1293 ret
= ventoy_data_save_all();
1295 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1302 void ventoy_data_default_menu_tip(data_tip
*data
)
1304 memset(data
, 0, sizeof(data_tip
));
1306 scnprintf(data
->left
, sizeof(data
->left
), "10%%");
1307 scnprintf(data
->top
, sizeof(data
->top
), "81%%");
1308 scnprintf(data
->color
, sizeof(data
->color
), "%s", "blue");
1311 int ventoy_data_cmp_menu_tip(data_tip
*data1
, data_tip
*data2
)
1313 data_tip_node
*list1
= NULL
;
1314 data_tip_node
*list2
= NULL
;
1316 if (strcmp(data1
->left
, data2
->left
) || strcmp(data1
->top
, data2
->top
) || strcmp(data1
->color
, data2
->color
))
1321 if (NULL
== data1
->list
&& NULL
== data2
->list
)
1325 else if (data1
->list
&& data2
->list
)
1327 list1
= data1
->list
;
1328 list2
= data2
->list
;
1330 while (list1
&& list2
)
1332 if ((list1
->type
!= list2
->type
) ||
1333 strcmp(list1
->path
, list2
->path
) ||
1334 strcmp(list1
->tip
, list2
->tip
))
1339 list1
= list1
->next
;
1340 list2
= list2
->next
;
1343 if (list1
== NULL
&& list2
== NULL
)
1358 int ventoy_data_save_menu_tip(data_tip
*data
, const char *title
, char *buf
, int buflen
)
1361 data_tip_node
*node
= NULL
;
1362 data_tip
*def
= g_data_menu_tip
+ bios_max
;
1364 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1365 VTOY_JSON_FMT_KEY_L(L1
, title
);
1366 VTOY_JSON_FMT_OBJ_BEGIN_N();
1368 VTOY_JSON_FMT_DIFF_STRN(L2
, "left", left
);
1369 VTOY_JSON_FMT_DIFF_STRN(L2
, "top", top
);
1370 VTOY_JSON_FMT_DIFF_STRN(L2
, "color", color
);
1374 VTOY_JSON_FMT_KEY_L(L2
, "tips");
1375 VTOY_JSON_FMT_ARY_BEGIN_N();
1377 for (node
= data
->list
; node
; node
= node
->next
)
1379 VTOY_JSON_FMT_OBJ_BEGIN_LN(L3
);
1381 if (node
->type
== path_type_file
)
1383 VTOY_JSON_FMT_STRN_PATH_LN(L4
, "image", node
->path
);
1387 VTOY_JSON_FMT_STRN_PATH_LN(L4
, "dir", node
->path
);
1389 VTOY_JSON_FMT_STRN_EX_LN(L4
, "tip", node
->tip
);
1391 VTOY_JSON_FMT_OBJ_ENDEX_LN(L3
);
1394 VTOY_JSON_FMT_ARY_ENDEX_LN(L2
);
1397 VTOY_JSON_FMT_OBJ_ENDEX_LN(L1
);
1399 VTOY_JSON_FMT_END(pos
);
1405 int ventoy_data_json_menu_tip(data_tip
*data
, char *buf
, int buflen
)
1409 data_tip_node
*node
= NULL
;
1411 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1413 VTOY_JSON_FMT_OBJ_BEGIN();
1415 VTOY_JSON_FMT_STRN("left", data
->left
);
1416 VTOY_JSON_FMT_STRN("top", data
->top
);
1417 VTOY_JSON_FMT_STRN("color", data
->color
);
1419 VTOY_JSON_FMT_KEY("tips");
1420 VTOY_JSON_FMT_ARY_BEGIN();
1422 for (node
= data
->list
; node
; node
= node
->next
)
1424 VTOY_JSON_FMT_OBJ_BEGIN();
1426 VTOY_JSON_FMT_UINT("type", node
->type
);
1427 VTOY_JSON_FMT_STRN("path", node
->path
);
1428 if (node
->type
== path_type_file
)
1430 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
1434 valid
= ventoy_is_directory_exist("%s%s", g_cur_dir
, node
->path
);
1437 VTOY_JSON_FMT_SINT("valid", valid
);
1438 VTOY_JSON_FMT_STRN("tip", node
->tip
);
1440 VTOY_JSON_FMT_OBJ_ENDEX();
1443 VTOY_JSON_FMT_ARY_ENDEX();
1445 VTOY_JSON_FMT_OBJ_END();
1446 VTOY_JSON_FMT_END(pos
);
1451 static int ventoy_api_get_tip(struct mg_connection
*conn
, VTOY_JSON
*json
)
1453 api_get_func(conn
, json
, menu_tip
);
1457 static int ventoy_api_save_tip(struct mg_connection
*conn
, VTOY_JSON
*json
)
1461 data_tip
*data
= NULL
;
1463 vtoy_json_get_int(json
, "index", &index
);
1464 data
= g_data_menu_tip
+ index
;
1466 VTOY_JSON_STR("left", data
->left
);
1467 VTOY_JSON_STR("top", data
->top
);
1468 VTOY_JSON_STR("color", data
->color
);
1470 ret
= ventoy_data_save_all();
1472 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1476 static int ventoy_api_tip_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
1480 int type
= path_type_file
;
1481 const char *path
= NULL
;
1482 const char *tip
= NULL
;
1483 data_tip_node
*node
= NULL
;
1484 data_tip_node
*cur
= NULL
;
1485 data_tip
*data
= NULL
;
1487 vtoy_json_get_int(json
, "index", &index
);
1488 data
= g_data_menu_tip
+ index
;
1490 vtoy_json_get_int(json
, "type", &type
);
1492 path
= VTOY_JSON_STR_EX("path");
1493 tip
= VTOY_JSON_STR_EX("tip");
1496 if (ventoy_is_real_exist_common(path
, data
->list
, data_tip_node
))
1498 ventoy_json_result(conn
, VTOY_JSON_DUPLICATE
);
1502 node
= zalloc(sizeof(data_tip_node
));
1506 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
1507 scnprintf(node
->tip
, sizeof(node
->tip
), "%s", tip
);
1509 vtoy_list_add(data
->list
, cur
, node
);
1513 ret
= ventoy_data_save_all();
1515 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1519 static int ventoy_api_tip_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
1523 const char *path
= NULL
;
1524 data_tip_node
*last
= NULL
;
1525 data_tip_node
*node
= NULL
;
1526 data_tip
*data
= NULL
;
1528 vtoy_json_get_int(json
, "index", &index
);
1529 data
= g_data_menu_tip
+ index
;
1531 path
= VTOY_JSON_STR_EX("path");
1534 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
1536 vtoy_list_free(data_tip_node
, data
->list
);
1540 vtoy_list_del(last
, node
, data
->list
, path
);
1544 ret
= ventoy_data_save_all();
1546 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1553 void ventoy_data_default_menu_class(data_class
*data
)
1555 memset(data
, 0, sizeof(data_class
));
1558 int ventoy_data_cmp_menu_class(data_class
*data1
, data_class
*data2
)
1560 data_class_node
*list1
= NULL
;
1561 data_class_node
*list2
= NULL
;
1563 if (NULL
== data1
->list
&& NULL
== data2
->list
)
1567 else if (data1
->list
&& data2
->list
)
1569 list1
= data1
->list
;
1570 list2
= data2
->list
;
1572 while (list1
&& list2
)
1574 if ((list1
->type
!= list2
->type
) ||
1575 strcmp(list1
->path
, list2
->path
) ||
1576 strcmp(list1
->class, list2
->class))
1581 list1
= list1
->next
;
1582 list2
= list2
->next
;
1585 if (list1
== NULL
&& list2
== NULL
)
1600 int ventoy_data_save_menu_class(data_class
*data
, const char *title
, char *buf
, int buflen
)
1603 data_class_node
*node
= NULL
;
1605 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1607 VTOY_JSON_FMT_KEY_L(L1
, title
);
1608 VTOY_JSON_FMT_ARY_BEGIN_N();
1610 for (node
= data
->list
; node
; node
= node
->next
)
1612 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
1614 if (node
->type
== class_type_key
)
1616 VTOY_JSON_FMT_STRN_LN(L3
, "key", node
->path
);
1618 else if (node
->type
== class_type_dir
)
1620 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "dir", node
->path
);
1624 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "parent", node
->path
);
1626 VTOY_JSON_FMT_STRN_LN(L3
, "class", node
->class);
1628 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
1631 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
1632 VTOY_JSON_FMT_END(pos
);
1638 int ventoy_data_json_menu_class(data_class
*data
, char *buf
, int buflen
)
1642 data_class_node
*node
= NULL
;
1644 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1645 VTOY_JSON_FMT_ARY_BEGIN();
1647 for (node
= data
->list
; node
; node
= node
->next
)
1649 VTOY_JSON_FMT_OBJ_BEGIN();
1651 VTOY_JSON_FMT_UINT("type", node
->type
);
1652 VTOY_JSON_FMT_STRN("path", node
->path
);
1654 if (node
->type
== class_type_key
)
1660 valid
= ventoy_is_directory_exist("%s%s", g_cur_dir
, node
->path
);
1662 VTOY_JSON_FMT_SINT("valid", valid
);
1664 VTOY_JSON_FMT_STRN("class", node
->class);
1666 VTOY_JSON_FMT_OBJ_ENDEX();
1669 VTOY_JSON_FMT_ARY_END();
1670 VTOY_JSON_FMT_END(pos
);
1676 static int ventoy_api_get_class(struct mg_connection
*conn
, VTOY_JSON
*json
)
1678 api_get_func(conn
, json
, menu_class
);
1682 static int ventoy_api_save_class(struct mg_connection
*conn
, VTOY_JSON
*json
)
1685 ret
= ventoy_data_save_all();
1687 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1691 static int ventoy_api_class_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
1695 int type
= class_type_key
;
1696 const char *path
= NULL
;
1697 const char *class = NULL
;
1698 data_class_node
*node
= NULL
;
1699 data_class_node
*cur
= NULL
;
1700 data_class
*data
= NULL
;
1702 vtoy_json_get_int(json
, "index", &index
);
1703 data
= g_data_menu_class
+ index
;
1705 vtoy_json_get_int(json
, "type", &type
);
1707 path
= VTOY_JSON_STR_EX("path");
1708 class = VTOY_JSON_STR_EX("class");
1711 node
= zalloc(sizeof(data_class_node
));
1716 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
1717 scnprintf(node
->class, sizeof(node
->class), "%s", class);
1719 vtoy_list_add(data
->list
, cur
, node
);
1723 ret
= ventoy_data_save_all();
1725 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1729 static int ventoy_api_class_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
1733 const char *path
= NULL
;
1734 data_class_node
*last
= NULL
;
1735 data_class_node
*node
= NULL
;
1736 data_class
*data
= NULL
;
1738 vtoy_json_get_int(json
, "index", &index
);
1739 data
= g_data_menu_class
+ index
;
1741 path
= VTOY_JSON_STR_EX("path");
1744 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
1746 vtoy_list_free(data_class_node
, data
->list
);
1750 vtoy_list_del(last
, node
, data
->list
, path
);
1754 ret
= ventoy_data_save_all();
1756 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1763 void ventoy_data_default_auto_memdisk(data_auto_memdisk
*data
)
1765 memset(data
, 0, sizeof(data_auto_memdisk
));
1768 int ventoy_data_cmp_auto_memdisk(data_auto_memdisk
*data1
, data_auto_memdisk
*data2
)
1770 return ventoy_path_list_cmp(data1
->list
, data2
->list
);
1773 int ventoy_data_save_auto_memdisk(data_auto_memdisk
*data
, const char *title
, char *buf
, int buflen
)
1776 path_node
*node
= NULL
;
1778 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1780 VTOY_JSON_FMT_KEY_L(L1
, title
);
1781 VTOY_JSON_FMT_ARY_BEGIN_N();
1783 for (node
= data
->list
; node
; node
= node
->next
)
1785 VTOY_JSON_FMT_ITEM_PATH_LN(L2
, node
->path
);
1788 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
1789 VTOY_JSON_FMT_END(pos
);
1794 int ventoy_data_json_auto_memdisk(data_auto_memdisk
*data
, char *buf
, int buflen
)
1798 path_node
*node
= NULL
;
1800 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1801 VTOY_JSON_FMT_ARY_BEGIN();
1803 for (node
= data
->list
; node
; node
= node
->next
)
1805 VTOY_JSON_FMT_OBJ_BEGIN();
1807 VTOY_JSON_FMT_STRN("path", node
->path
);
1808 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
1809 VTOY_JSON_FMT_SINT("valid", valid
);
1811 VTOY_JSON_FMT_OBJ_ENDEX();
1814 VTOY_JSON_FMT_ARY_END();
1815 VTOY_JSON_FMT_END(pos
);
1820 static int ventoy_api_get_auto_memdisk(struct mg_connection
*conn
, VTOY_JSON
*json
)
1822 api_get_func(conn
, json
, auto_memdisk
);
1826 static int ventoy_api_save_auto_memdisk(struct mg_connection
*conn
, VTOY_JSON
*json
)
1830 ret
= ventoy_data_save_all();
1832 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1836 static int ventoy_api_auto_memdisk_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
1840 const char *path
= NULL
;
1841 path_node
*node
= NULL
;
1842 path_node
*cur
= NULL
;
1843 data_auto_memdisk
*data
= NULL
;
1845 vtoy_json_get_int(json
, "index", &index
);
1846 data
= g_data_auto_memdisk
+ index
;
1848 path
= VTOY_JSON_STR_EX("path");
1851 if (ventoy_is_real_exist_common(path
, data
->list
, path_node
))
1853 ventoy_json_result(conn
, VTOY_JSON_DUPLICATE
);
1857 node
= zalloc(sizeof(path_node
));
1860 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
1861 vtoy_list_add(data
->list
, cur
, node
);
1865 ret
= ventoy_data_save_all();
1867 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1871 static int ventoy_api_auto_memdisk_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
1875 const char *path
= NULL
;
1876 path_node
*last
= NULL
;
1877 path_node
*node
= NULL
;
1878 data_auto_memdisk
*data
= NULL
;
1880 vtoy_json_get_int(json
, "index", &index
);
1881 data
= g_data_auto_memdisk
+ index
;
1883 path
= VTOY_JSON_STR_EX("path");
1886 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
1888 vtoy_list_free(path_node
, data
->list
);
1892 vtoy_list_del(last
, node
, data
->list
, path
);
1896 ret
= ventoy_data_save_all();
1898 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
1905 void ventoy_data_default_image_list(data_image_list
*data
)
1907 memset(data
, 0, sizeof(data_image_list
));
1910 int ventoy_data_cmp_image_list(data_image_list
*data1
, data_image_list
*data2
)
1912 if (data1
->type
!= data2
->type
)
1914 if (data1
->list
|| data2
->list
)
1924 return ventoy_path_list_cmp(data1
->list
, data2
->list
);
1927 int ventoy_data_save_image_list(data_image_list
*data
, const char *title
, char *buf
, int buflen
)
1931 path_node
*node
= NULL
;
1941 prelen
= (int)strlen("image_list");
1943 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1945 if (data
->type
== 0)
1947 scnprintf(newtitle
, sizeof(newtitle
), "image_list%s", title
+ prelen
);
1951 scnprintf(newtitle
, sizeof(newtitle
), "image_blacklist%s", title
+ prelen
);
1953 VTOY_JSON_FMT_KEY_L(L1
, newtitle
);
1955 VTOY_JSON_FMT_ARY_BEGIN_N();
1957 for (node
= data
->list
; node
; node
= node
->next
)
1959 VTOY_JSON_FMT_ITEM_PATH_LN(L2
, node
->path
);
1962 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
1963 VTOY_JSON_FMT_END(pos
);
1968 int ventoy_data_json_image_list(data_image_list
*data
, char *buf
, int buflen
)
1972 path_node
*node
= NULL
;
1974 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
1976 VTOY_JSON_FMT_OBJ_BEGIN();
1977 VTOY_JSON_FMT_SINT("type", data
->type
);
1979 VTOY_JSON_FMT_KEY("list");
1980 VTOY_JSON_FMT_ARY_BEGIN();
1982 for (node
= data
->list
; node
; node
= node
->next
)
1984 VTOY_JSON_FMT_OBJ_BEGIN();
1986 VTOY_JSON_FMT_STRN("path", node
->path
);
1987 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
1988 VTOY_JSON_FMT_SINT("valid", valid
);
1990 VTOY_JSON_FMT_OBJ_ENDEX();
1993 VTOY_JSON_FMT_ARY_ENDEX();
1994 VTOY_JSON_FMT_OBJ_END();
1996 VTOY_JSON_FMT_END(pos
);
2001 static int ventoy_api_get_image_list(struct mg_connection
*conn
, VTOY_JSON
*json
)
2003 api_get_func(conn
, json
, image_list
);
2007 static int ventoy_api_save_image_list(struct mg_connection
*conn
, VTOY_JSON
*json
)
2011 data_image_list
*data
= NULL
;
2013 vtoy_json_get_int(json
, "index", &index
);
2014 data
= g_data_image_list
+ index
;
2016 VTOY_JSON_INT("type", data
->type
);
2018 ret
= ventoy_data_save_all();
2020 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2024 static int ventoy_api_image_list_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
2028 const char *path
= NULL
;
2029 path_node
*node
= NULL
;
2030 path_node
*cur
= NULL
;
2031 data_image_list
*data
= NULL
;
2033 vtoy_json_get_int(json
, "index", &index
);
2034 data
= g_data_image_list
+ index
;
2036 path
= VTOY_JSON_STR_EX("path");
2039 if (ventoy_is_real_exist_common(path
, data
->list
, path_node
))
2041 ventoy_json_result(conn
, VTOY_JSON_DUPLICATE
);
2045 node
= zalloc(sizeof(path_node
));
2048 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
2049 vtoy_list_add(data
->list
, cur
, node
);
2053 ret
= ventoy_data_save_all();
2055 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2059 static int ventoy_api_image_list_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
2063 const char *path
= NULL
;
2064 path_node
*last
= NULL
;
2065 path_node
*node
= NULL
;
2066 data_image_list
*data
= NULL
;
2068 vtoy_json_get_int(json
, "index", &index
);
2069 data
= g_data_image_list
+ index
;
2071 path
= VTOY_JSON_STR_EX("path");
2074 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
2076 vtoy_list_free(path_node
, data
->list
);
2080 vtoy_list_del(last
, node
, data
->list
, path
);
2084 ret
= ventoy_data_save_all();
2086 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2093 void ventoy_data_default_password(data_password
*data
)
2095 memset(data
, 0, sizeof(data_password
));
2098 int ventoy_data_cmp_password(data_password
*data1
, data_password
*data2
)
2100 menu_password
*list1
= NULL
;
2101 menu_password
*list2
= NULL
;
2103 if (strcmp(data1
->bootpwd
, data2
->bootpwd
) ||
2104 strcmp(data1
->isopwd
, data2
->isopwd
) ||
2105 strcmp(data1
->wimpwd
, data2
->wimpwd
) ||
2106 strcmp(data1
->vhdpwd
, data2
->vhdpwd
) ||
2107 strcmp(data1
->imgpwd
, data2
->imgpwd
) ||
2108 strcmp(data1
->efipwd
, data2
->efipwd
) ||
2109 strcmp(data1
->vtoypwd
, data2
->vtoypwd
)
2115 if (NULL
== data1
->list
&& NULL
== data2
->list
)
2119 else if (data1
->list
&& data2
->list
)
2121 list1
= data1
->list
;
2122 list2
= data2
->list
;
2124 while (list1
&& list2
)
2126 if ((list1
->type
!= list2
->type
) || strcmp(list1
->path
, list2
->path
))
2131 list1
= list1
->next
;
2132 list2
= list2
->next
;
2135 if (list1
== NULL
&& list2
== NULL
)
2150 int ventoy_data_save_password(data_password
*data
, const char *title
, char *buf
, int buflen
)
2153 menu_password
*node
= NULL
;
2154 data_password
*def
= g_data_password
+ bios_max
;
2156 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2157 VTOY_JSON_FMT_KEY_L(L1
, title
);
2158 VTOY_JSON_FMT_OBJ_BEGIN_N();
2160 VTOY_JSON_FMT_DIFF_STRN(L2
, "bootpwd", bootpwd
);
2161 VTOY_JSON_FMT_DIFF_STRN(L2
, "isopwd", isopwd
);
2162 VTOY_JSON_FMT_DIFF_STRN(L2
, "wimpwd", wimpwd
);
2163 VTOY_JSON_FMT_DIFF_STRN(L2
, "vhdpwd", vhdpwd
);
2164 VTOY_JSON_FMT_DIFF_STRN(L2
, "imgpwd", imgpwd
);
2165 VTOY_JSON_FMT_DIFF_STRN(L2
, "efipwd", efipwd
);
2166 VTOY_JSON_FMT_DIFF_STRN(L2
, "vtoypwd", vtoypwd
);
2170 VTOY_JSON_FMT_KEY_L(L2
, "menupwd");
2171 VTOY_JSON_FMT_ARY_BEGIN_N();
2173 for (node
= data
->list
; node
; node
= node
->next
)
2175 VTOY_JSON_FMT_OBJ_BEGIN_LN(L3
);
2177 if (node
->type
== 0)
2179 VTOY_JSON_FMT_STRN_PATH_LN(L4
, "file", node
->path
);
2183 VTOY_JSON_FMT_STRN_PATH_LN(L4
, "parent", node
->path
);
2185 VTOY_JSON_FMT_STRN_LN(L4
, "pwd", node
->pwd
);
2187 VTOY_JSON_FMT_OBJ_ENDEX_LN(L3
);
2190 VTOY_JSON_FMT_ARY_ENDEX_LN(L2
);
2193 VTOY_JSON_FMT_OBJ_ENDEX_LN(L1
);
2195 VTOY_JSON_FMT_END(pos
);
2201 int ventoy_data_json_password(data_password
*data
, char *buf
, int buflen
)
2205 menu_password
*node
= NULL
;
2207 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2209 VTOY_JSON_FMT_OBJ_BEGIN();
2211 VTOY_JSON_FMT_STRN("bootpwd", data
->bootpwd
);
2212 VTOY_JSON_FMT_STRN("isopwd", data
->isopwd
);
2213 VTOY_JSON_FMT_STRN("wimpwd", data
->wimpwd
);
2214 VTOY_JSON_FMT_STRN("vhdpwd", data
->vhdpwd
);
2215 VTOY_JSON_FMT_STRN("imgpwd", data
->imgpwd
);
2216 VTOY_JSON_FMT_STRN("efipwd", data
->efipwd
);
2217 VTOY_JSON_FMT_STRN("vtoypwd", data
->vtoypwd
);
2219 VTOY_JSON_FMT_KEY("list");
2220 VTOY_JSON_FMT_ARY_BEGIN();
2222 for (node
= data
->list
; node
; node
= node
->next
)
2224 VTOY_JSON_FMT_OBJ_BEGIN();
2226 VTOY_JSON_FMT_SINT("type", node
->type
);
2227 VTOY_JSON_FMT_STRN("path", node
->path
);
2228 if (node
->type
== path_type_file
)
2230 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
2234 valid
= ventoy_is_directory_exist("%s%s", g_cur_dir
, node
->path
);
2237 VTOY_JSON_FMT_SINT("valid", valid
);
2238 VTOY_JSON_FMT_STRN("pwd", node
->pwd
);
2240 VTOY_JSON_FMT_OBJ_ENDEX();
2243 VTOY_JSON_FMT_ARY_ENDEX();
2245 VTOY_JSON_FMT_OBJ_END();
2246 VTOY_JSON_FMT_END(pos
);
2251 static int ventoy_api_get_password(struct mg_connection
*conn
, VTOY_JSON
*json
)
2253 api_get_func(conn
, json
, password
);
2257 static int ventoy_api_save_password(struct mg_connection
*conn
, VTOY_JSON
*json
)
2261 data_password
*data
= NULL
;
2263 vtoy_json_get_int(json
, "index", &index
);
2264 data
= g_data_password
+ index
;
2266 VTOY_JSON_STR("bootpwd", data
->bootpwd
);
2267 VTOY_JSON_STR("isopwd", data
->isopwd
);
2268 VTOY_JSON_STR("wimpwd", data
->wimpwd
);
2269 VTOY_JSON_STR("vhdpwd", data
->vhdpwd
);
2270 VTOY_JSON_STR("imgpwd", data
->imgpwd
);
2271 VTOY_JSON_STR("efipwd", data
->efipwd
);
2272 VTOY_JSON_STR("vtoypwd", data
->vtoypwd
);
2274 ret
= ventoy_data_save_all();
2276 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2280 static int ventoy_api_password_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
2285 const char *path
= NULL
;
2286 const char *pwd
= NULL
;
2287 menu_password
*node
= NULL
;
2288 menu_password
*cur
= NULL
;
2289 data_password
*data
= NULL
;
2291 vtoy_json_get_int(json
, "index", &index
);
2292 data
= g_data_password
+ index
;
2294 vtoy_json_get_int(json
, "type", &type
);
2296 path
= VTOY_JSON_STR_EX("path");
2297 pwd
= VTOY_JSON_STR_EX("pwd");
2300 if (ventoy_is_real_exist_common(path
, data
->list
, menu_password
))
2302 ventoy_json_result(conn
, VTOY_JSON_DUPLICATE
);
2306 node
= zalloc(sizeof(menu_password
));
2310 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
2311 scnprintf(node
->pwd
, sizeof(node
->pwd
), "%s", pwd
);
2313 vtoy_list_add(data
->list
, cur
, node
);
2317 ret
= ventoy_data_save_all();
2319 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2323 static int ventoy_api_password_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
2327 const char *path
= NULL
;
2328 menu_password
*last
= NULL
;
2329 menu_password
*node
= NULL
;
2330 data_password
*data
= NULL
;
2332 vtoy_json_get_int(json
, "index", &index
);
2333 data
= g_data_password
+ index
;
2335 path
= VTOY_JSON_STR_EX("path");
2338 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
2340 vtoy_list_free(menu_password
, data
->list
);
2344 vtoy_list_del(last
, node
, data
->list
, path
);
2348 ret
= ventoy_data_save_all();
2350 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2357 void ventoy_data_default_conf_replace(data_conf_replace
*data
)
2359 memset(data
, 0, sizeof(data_conf_replace
));
2362 int ventoy_data_cmp_conf_replace(data_conf_replace
*data1
, data_conf_replace
*data2
)
2364 conf_replace_node
*list1
= NULL
;
2365 conf_replace_node
*list2
= NULL
;
2367 if (NULL
== data1
->list
&& NULL
== data2
->list
)
2371 else if (data1
->list
&& data2
->list
)
2373 list1
= data1
->list
;
2374 list2
= data2
->list
;
2376 while (list1
&& list2
)
2378 if (list1
->image
!= list2
->image
||
2379 strcmp(list1
->path
, list2
->path
) ||
2380 strcmp(list1
->org
, list2
->org
) ||
2381 strcmp(list1
->new, list2
->new)
2387 list1
= list1
->next
;
2388 list2
= list2
->next
;
2391 if (list1
== NULL
&& list2
== NULL
)
2406 int ventoy_data_save_conf_replace(data_conf_replace
*data
, const char *title
, char *buf
, int buflen
)
2409 conf_replace_node
*node
= NULL
;
2411 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2413 VTOY_JSON_FMT_KEY_L(L1
, title
);
2414 VTOY_JSON_FMT_ARY_BEGIN_N();
2416 for (node
= data
->list
; node
; node
= node
->next
)
2418 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
2420 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "iso", node
->path
);
2421 VTOY_JSON_FMT_STRN_LN(L3
, "org", node
->org
);
2422 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "new", node
->new);
2425 VTOY_JSON_FMT_SINT_LN(L3
, "img", node
->image
);
2428 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
2431 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
2432 VTOY_JSON_FMT_END(pos
);
2438 int ventoy_data_json_conf_replace(data_conf_replace
*data
, char *buf
, int buflen
)
2441 conf_replace_node
*node
= NULL
;
2443 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2444 VTOY_JSON_FMT_ARY_BEGIN();
2446 for (node
= data
->list
; node
; node
= node
->next
)
2448 VTOY_JSON_FMT_OBJ_BEGIN();
2450 VTOY_JSON_FMT_STRN("path", node
->path
);
2451 VTOY_JSON_FMT_SINT("valid", ventoy_check_fuzzy_path(node
->path
, 1));
2452 VTOY_JSON_FMT_STRN("org", node
->org
);
2453 VTOY_JSON_FMT_STRN("new", node
->new);
2454 VTOY_JSON_FMT_SINT("new_valid", ventoy_is_file_exist("%s%s", g_cur_dir
, node
->new));
2455 VTOY_JSON_FMT_SINT("img", node
->image
);
2457 VTOY_JSON_FMT_OBJ_ENDEX();
2460 VTOY_JSON_FMT_ARY_END();
2461 VTOY_JSON_FMT_END(pos
);
2466 static int ventoy_api_get_conf_replace(struct mg_connection
*conn
, VTOY_JSON
*json
)
2468 api_get_func(conn
, json
, conf_replace
);
2472 static int ventoy_api_save_conf_replace(struct mg_connection
*conn
, VTOY_JSON
*json
)
2475 ret
= ventoy_data_save_all();
2477 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2481 static int ventoy_api_conf_replace_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
2486 const char *path
= NULL
;
2487 const char *org
= NULL
;
2488 const char *new = NULL
;
2489 conf_replace_node
*node
= NULL
;
2490 conf_replace_node
*cur
= NULL
;
2491 data_conf_replace
*data
= NULL
;
2493 vtoy_json_get_int(json
, "img", &image
);
2495 vtoy_json_get_int(json
, "index", &index
);
2496 data
= g_data_conf_replace
+ index
;
2498 path
= VTOY_JSON_STR_EX("path");
2499 org
= VTOY_JSON_STR_EX("org");
2500 new = VTOY_JSON_STR_EX("new");
2501 if (path
&& org
&& new)
2503 node
= zalloc(sizeof(conf_replace_node
));
2506 node
->image
= image
;
2507 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
2508 scnprintf(node
->org
, sizeof(node
->org
), "%s", org
);
2509 scnprintf(node
->new, sizeof(node
->new), "%s", new);
2511 vtoy_list_add(data
->list
, cur
, node
);
2515 ret
= ventoy_data_save_all();
2517 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2521 static int ventoy_api_conf_replace_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
2525 const char *path
= NULL
;
2526 conf_replace_node
*last
= NULL
;
2527 conf_replace_node
*node
= NULL
;
2528 data_conf_replace
*data
= NULL
;
2530 vtoy_json_get_int(json
, "index", &index
);
2531 data
= g_data_conf_replace
+ index
;
2533 path
= VTOY_JSON_STR_EX("path");
2536 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
2538 vtoy_list_free(conf_replace_node
, data
->list
);
2542 vtoy_list_del(last
, node
, data
->list
, path
);
2546 ret
= ventoy_data_save_all();
2548 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2556 void ventoy_data_default_dud(data_dud
*data
)
2558 memset(data
, 0, sizeof(data_dud
));
2561 int ventoy_data_cmp_dud(data_dud
*data1
, data_dud
*data2
)
2563 dud_node
*list1
= NULL
;
2564 dud_node
*list2
= NULL
;
2566 if (NULL
== data1
->list
&& NULL
== data2
->list
)
2570 else if (data1
->list
&& data2
->list
)
2572 list1
= data1
->list
;
2573 list2
= data2
->list
;
2575 while (list1
&& list2
)
2577 if (strcmp(list1
->path
, list2
->path
))
2582 /* no need to compare dud list with default */
2583 list1
= list1
->next
;
2584 list2
= list2
->next
;
2587 if (list1
== NULL
&& list2
== NULL
)
2602 int ventoy_data_save_dud(data_dud
*data
, const char *title
, char *buf
, int buflen
)
2605 dud_node
*node
= NULL
;
2606 path_node
*pathnode
= NULL
;
2608 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2610 VTOY_JSON_FMT_KEY_L(L1
, title
);
2611 VTOY_JSON_FMT_ARY_BEGIN_N();
2613 for (node
= data
->list
; node
; node
= node
->next
)
2615 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
2616 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "image", node
->path
);
2618 VTOY_JSON_FMT_KEY_L(L3
, "dud");
2619 VTOY_JSON_FMT_ARY_BEGIN_N();
2620 for (pathnode
= node
->list
; pathnode
; pathnode
= pathnode
->next
)
2622 VTOY_JSON_FMT_ITEM_PATH_LN(L4
, pathnode
->path
);
2624 VTOY_JSON_FMT_ARY_ENDEX_LN(L3
);
2626 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
2629 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
2630 VTOY_JSON_FMT_END(pos
);
2636 int ventoy_data_json_dud(data_dud
*data
, char *buf
, int buflen
)
2640 dud_node
*node
= NULL
;
2641 path_node
*pathnode
= NULL
;
2643 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2644 VTOY_JSON_FMT_ARY_BEGIN();
2646 for (node
= data
->list
; node
; node
= node
->next
)
2648 VTOY_JSON_FMT_OBJ_BEGIN();
2650 VTOY_JSON_FMT_STRN("path", node
->path
);
2651 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
2652 VTOY_JSON_FMT_SINT("valid", valid
);
2655 VTOY_JSON_FMT_KEY("list");
2656 VTOY_JSON_FMT_ARY_BEGIN();
2657 for (pathnode
= node
->list
; pathnode
; pathnode
= pathnode
->next
)
2659 VTOY_JSON_FMT_OBJ_BEGIN();
2660 VTOY_JSON_FMT_STRN("path", pathnode
->path
);
2662 valid
= ventoy_is_file_exist("%s%s", g_cur_dir
, pathnode
->path
);
2663 VTOY_JSON_FMT_SINT("valid", valid
);
2664 VTOY_JSON_FMT_OBJ_ENDEX();
2666 VTOY_JSON_FMT_ARY_ENDEX();
2669 VTOY_JSON_FMT_OBJ_ENDEX();
2672 VTOY_JSON_FMT_ARY_END();
2673 VTOY_JSON_FMT_END(pos
);
2678 static int ventoy_api_get_dud(struct mg_connection
*conn
, VTOY_JSON
*json
)
2680 api_get_func(conn
, json
, dud
);
2684 static int ventoy_api_save_dud(struct mg_connection
*conn
, VTOY_JSON
*json
)
2687 ret
= ventoy_data_save_all();
2689 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2694 static int ventoy_api_dud_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
2698 const char *path
= NULL
;
2699 dud_node
*node
= NULL
;
2700 dud_node
*cur
= NULL
;
2701 data_dud
*data
= NULL
;
2702 VTOY_JSON
*array
= NULL
;
2704 vtoy_json_get_int(json
, "index", &index
);
2705 data
= g_data_dud
+ index
;
2707 array
= vtoy_json_find_item(json
, JSON_TYPE_ARRAY
, "dud");
2708 path
= VTOY_JSON_STR_EX("path");
2711 if (ventoy_is_real_exist_common(path
, data
->list
, dud_node
))
2713 ventoy_json_result(conn
, VTOY_JSON_DUPLICATE
);
2717 node
= zalloc(sizeof(dud_node
));
2720 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
2721 node
->list
= ventoy_path_node_add_array(array
);
2723 vtoy_list_add(data
->list
, cur
, node
);
2727 ret
= ventoy_data_save_all();
2729 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2733 static int ventoy_api_dud_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
2737 const char *path
= NULL
;
2738 dud_node
*next
= NULL
;
2739 dud_node
*last
= NULL
;
2740 dud_node
*node
= NULL
;
2741 data_dud
*data
= NULL
;
2743 vtoy_json_get_int(json
, "index", &index
);
2744 data
= g_data_dud
+ index
;
2746 path
= VTOY_JSON_STR_EX("path");
2749 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
2751 for (node
= data
->list
; node
; node
= next
)
2754 ventoy_free_path_node_list(node
->list
);
2761 vtoy_list_del_ex(last
, node
, data
->list
, path
, ventoy_free_path_node_list
);
2765 ret
= ventoy_data_save_all();
2767 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2772 static int ventoy_api_dud_add_inner(struct mg_connection
*conn
, VTOY_JSON
*json
)
2776 const char *path
= NULL
;
2777 const char *outpath
= NULL
;
2778 path_node
*pcur
= NULL
;
2779 path_node
*pnode
= NULL
;
2780 dud_node
*node
= NULL
;
2781 data_dud
*data
= NULL
;
2783 vtoy_json_get_int(json
, "index", &index
);
2784 data
= g_data_dud
+ index
;
2786 path
= VTOY_JSON_STR_EX("path");
2787 outpath
= VTOY_JSON_STR_EX("outpath");
2788 if (path
&& outpath
)
2790 for (node
= data
->list
; node
; node
= node
->next
)
2792 if (strcmp(outpath
, node
->path
) == 0)
2794 pnode
= zalloc(sizeof(path_node
));
2797 scnprintf(pnode
->path
, sizeof(pnode
->path
), "%s", path
);
2798 vtoy_list_add(node
->list
, pcur
, pnode
);
2806 ret
= ventoy_data_save_all();
2808 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2812 static int ventoy_api_dud_del_inner(struct mg_connection
*conn
, VTOY_JSON
*json
)
2816 const char *path
= NULL
;
2817 const char *outpath
= NULL
;
2818 path_node
*plast
= NULL
;
2819 path_node
*pnode
= NULL
;
2820 dud_node
*node
= NULL
;
2821 data_dud
*data
= NULL
;
2823 vtoy_json_get_int(json
, "index", &index
);
2824 data
= g_data_dud
+ index
;
2826 path
= VTOY_JSON_STR_EX("path");
2827 outpath
= VTOY_JSON_STR_EX("outpath");
2828 if (path
&& outpath
)
2830 for (node
= data
->list
; node
; node
= node
->next
)
2832 if (strcmp(outpath
, node
->path
) == 0)
2834 vtoy_list_del(plast
, pnode
, node
->list
, path
);
2840 ret
= ventoy_data_save_all();
2842 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
2850 void ventoy_data_default_auto_install(data_auto_install
*data
)
2852 memset(data
, 0, sizeof(data_auto_install
));
2855 int ventoy_data_cmp_auto_install(data_auto_install
*data1
, data_auto_install
*data2
)
2857 auto_install_node
*list1
= NULL
;
2858 auto_install_node
*list2
= NULL
;
2860 if (NULL
== data1
->list
&& NULL
== data2
->list
)
2864 else if (data1
->list
&& data2
->list
)
2866 list1
= data1
->list
;
2867 list2
= data2
->list
;
2869 while (list1
&& list2
)
2871 if (list1
->timeout
!= list2
->timeout
||
2872 list1
->autosel
!= list2
->autosel
||
2873 strcmp(list1
->path
, list2
->path
))
2878 /* no need to compare auto install list with default */
2879 list1
= list1
->next
;
2880 list2
= list2
->next
;
2883 if (list1
== NULL
&& list2
== NULL
)
2898 int ventoy_data_save_auto_install(data_auto_install
*data
, const char *title
, char *buf
, int buflen
)
2901 auto_install_node
*node
= NULL
;
2902 path_node
*pathnode
= NULL
;
2904 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2906 VTOY_JSON_FMT_KEY_L(L1
, title
);
2907 VTOY_JSON_FMT_ARY_BEGIN_N();
2909 for (node
= data
->list
; node
; node
= node
->next
)
2911 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
2912 if (node
->type
== 0)
2914 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "image", node
->path
);
2918 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "parent", node
->path
);
2922 VTOY_JSON_FMT_KEY_L(L3
, "template");
2923 VTOY_JSON_FMT_ARY_BEGIN_N();
2924 for (pathnode
= node
->list
; pathnode
; pathnode
= pathnode
->next
)
2926 VTOY_JSON_FMT_ITEM_PATH_LN(L4
, pathnode
->path
);
2928 VTOY_JSON_FMT_ARY_ENDEX_LN(L3
);
2930 if (node
->timeouten
)
2932 VTOY_JSON_FMT_SINT_LN(L3
, "timeout", node
->timeout
);
2935 if (node
->autoselen
)
2937 VTOY_JSON_FMT_SINT_LN(L3
, "autosel", node
->autosel
);
2940 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
2943 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
2944 VTOY_JSON_FMT_END(pos
);
2950 int ventoy_data_json_auto_install(data_auto_install
*data
, char *buf
, int buflen
)
2954 auto_install_node
*node
= NULL
;
2955 path_node
*pathnode
= NULL
;
2957 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
2958 VTOY_JSON_FMT_ARY_BEGIN();
2960 for (node
= data
->list
; node
; node
= node
->next
)
2962 VTOY_JSON_FMT_OBJ_BEGIN();
2964 VTOY_JSON_FMT_STRN("path", node
->path
);
2966 if (node
->type
== 0)
2968 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
2972 valid
= ventoy_is_directory_exist("%s%s", g_cur_dir
, node
->path
);
2974 VTOY_JSON_FMT_SINT("valid", valid
);
2975 VTOY_JSON_FMT_SINT("type", node
->type
);
2977 VTOY_JSON_FMT_BOOL("timeouten", node
->timeouten
);
2978 VTOY_JSON_FMT_BOOL("autoselen", node
->autoselen
);
2980 VTOY_JSON_FMT_SINT("autosel", node
->autosel
);
2981 VTOY_JSON_FMT_SINT("timeout", node
->timeout
);
2983 VTOY_JSON_FMT_KEY("list");
2984 VTOY_JSON_FMT_ARY_BEGIN();
2985 for (pathnode
= node
->list
; pathnode
; pathnode
= pathnode
->next
)
2987 VTOY_JSON_FMT_OBJ_BEGIN();
2988 VTOY_JSON_FMT_STRN("path", pathnode
->path
);
2990 valid
= ventoy_is_file_exist("%s%s", g_cur_dir
, pathnode
->path
);
2991 VTOY_JSON_FMT_SINT("valid", valid
);
2992 VTOY_JSON_FMT_OBJ_ENDEX();
2994 VTOY_JSON_FMT_ARY_ENDEX();
2997 VTOY_JSON_FMT_OBJ_ENDEX();
3000 VTOY_JSON_FMT_ARY_END();
3001 VTOY_JSON_FMT_END(pos
);
3006 static int ventoy_api_get_auto_install(struct mg_connection
*conn
, VTOY_JSON
*json
)
3008 api_get_func(conn
, json
, auto_install
);
3012 static int ventoy_api_save_auto_install(struct mg_connection
*conn
, VTOY_JSON
*json
)
3018 uint8_t timeouten
= 0;
3019 uint8_t autoselen
= 0;
3020 auto_install_node
*node
= NULL
;
3021 data_auto_install
*data
= NULL
;
3023 vtoy_json_get_int(json
, "index", &index
);
3024 vtoy_json_get_int(json
, "id", &id
);
3026 vtoy_json_get_bool(json
, "timeouten", &timeouten
);
3027 vtoy_json_get_bool(json
, "autoselen", &autoselen
);
3029 data
= g_data_auto_install
+ index
;
3033 for (node
= data
->list
; node
; node
= node
->next
)
3037 node
->timeouten
= (int)timeouten
;
3038 node
->autoselen
= (int)autoselen
;
3039 VTOY_JSON_INT("timeout", node
->timeout
);
3040 VTOY_JSON_INT("autosel", node
->autosel
);
3046 ret
= ventoy_data_save_all();
3048 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3053 static int ventoy_api_auto_install_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
3058 const char *path
= NULL
;
3059 auto_install_node
*node
= NULL
;
3060 auto_install_node
*cur
= NULL
;
3061 data_auto_install
*data
= NULL
;
3062 VTOY_JSON
*array
= NULL
;
3064 vtoy_json_get_int(json
, "type", &type
);
3065 vtoy_json_get_int(json
, "index", &index
);
3066 data
= g_data_auto_install
+ index
;
3068 array
= vtoy_json_find_item(json
, JSON_TYPE_ARRAY
, "template");
3069 path
= VTOY_JSON_STR_EX("path");
3072 if (ventoy_is_real_exist_common(path
, data
->list
, auto_install_node
))
3074 ventoy_json_result(conn
, VTOY_JSON_DUPLICATE
);
3078 node
= zalloc(sizeof(auto_install_node
));
3082 node
->timeouten
= 0;
3083 node
->autoselen
= 0;
3086 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
3087 node
->list
= ventoy_path_node_add_array(array
);
3089 vtoy_list_add(data
->list
, cur
, node
);
3093 ret
= ventoy_data_save_all();
3095 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3099 static int ventoy_api_auto_install_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
3103 const char *path
= NULL
;
3104 auto_install_node
*last
= NULL
;
3105 auto_install_node
*next
= NULL
;
3106 auto_install_node
*node
= NULL
;
3107 data_auto_install
*data
= NULL
;
3109 vtoy_json_get_int(json
, "index", &index
);
3110 data
= g_data_auto_install
+ index
;
3112 path
= VTOY_JSON_STR_EX("path");
3115 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
3117 for (node
= data
->list
; node
; node
= next
)
3120 ventoy_free_path_node_list(node
->list
);
3127 vtoy_list_del_ex(last
, node
, data
->list
, path
, ventoy_free_path_node_list
);
3131 ret
= ventoy_data_save_all();
3133 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3137 static int ventoy_api_auto_install_add_inner(struct mg_connection
*conn
, VTOY_JSON
*json
)
3141 const char *path
= NULL
;
3142 const char *outpath
= NULL
;
3143 path_node
*pcur
= NULL
;
3144 path_node
*pnode
= NULL
;
3145 auto_install_node
*node
= NULL
;
3146 data_auto_install
*data
= NULL
;
3148 vtoy_json_get_int(json
, "index", &index
);
3149 data
= g_data_auto_install
+ index
;
3151 path
= VTOY_JSON_STR_EX("path");
3152 outpath
= VTOY_JSON_STR_EX("outpath");
3153 if (path
&& outpath
)
3155 for (node
= data
->list
; node
; node
= node
->next
)
3157 if (strcmp(outpath
, node
->path
) == 0)
3159 pnode
= zalloc(sizeof(path_node
));
3162 scnprintf(pnode
->path
, sizeof(pnode
->path
), "%s", path
);
3163 vtoy_list_add(node
->list
, pcur
, pnode
);
3171 ret
= ventoy_data_save_all();
3173 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3177 static int ventoy_api_auto_install_del_inner(struct mg_connection
*conn
, VTOY_JSON
*json
)
3181 const char *path
= NULL
;
3182 const char *outpath
= NULL
;
3183 path_node
*plast
= NULL
;
3184 path_node
*pnode
= NULL
;
3185 auto_install_node
*node
= NULL
;
3186 data_auto_install
*data
= NULL
;
3188 vtoy_json_get_int(json
, "index", &index
);
3189 data
= g_data_auto_install
+ index
;
3191 path
= VTOY_JSON_STR_EX("path");
3192 outpath
= VTOY_JSON_STR_EX("outpath");
3193 if (path
&& outpath
)
3195 for (node
= data
->list
; node
; node
= node
->next
)
3197 if (strcmp(outpath
, node
->path
) == 0)
3199 vtoy_list_del(plast
, pnode
, node
->list
, path
);
3205 ret
= ventoy_data_save_all();
3207 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3216 void ventoy_data_default_persistence(data_persistence
*data
)
3218 memset(data
, 0, sizeof(data_persistence
));
3221 int ventoy_data_cmp_persistence(data_persistence
*data1
, data_persistence
*data2
)
3223 persistence_node
*list1
= NULL
;
3224 persistence_node
*list2
= NULL
;
3226 if (NULL
== data1
->list
&& NULL
== data2
->list
)
3230 else if (data1
->list
&& data2
->list
)
3232 list1
= data1
->list
;
3233 list2
= data2
->list
;
3235 while (list1
&& list2
)
3237 if (list1
->timeout
!= list2
->timeout
||
3238 list1
->autosel
!= list2
->autosel
||
3239 strcmp(list1
->path
, list2
->path
))
3244 /* no need to compare auto install list with default */
3245 list1
= list1
->next
;
3246 list2
= list2
->next
;
3249 if (list1
== NULL
&& list2
== NULL
)
3264 int ventoy_data_save_persistence(data_persistence
*data
, const char *title
, char *buf
, int buflen
)
3267 persistence_node
*node
= NULL
;
3268 path_node
*pathnode
= NULL
;
3270 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
3272 VTOY_JSON_FMT_KEY_L(L1
, title
);
3273 VTOY_JSON_FMT_ARY_BEGIN_N();
3275 for (node
= data
->list
; node
; node
= node
->next
)
3277 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
3278 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "image", node
->path
);
3279 VTOY_JSON_FMT_KEY_L(L3
, "backend");
3280 VTOY_JSON_FMT_ARY_BEGIN_N();
3281 for (pathnode
= node
->list
; pathnode
; pathnode
= pathnode
->next
)
3283 VTOY_JSON_FMT_ITEM_PATH_LN(L4
, pathnode
->path
);
3285 VTOY_JSON_FMT_ARY_ENDEX_LN(L3
);
3287 if (node
->timeouten
)
3289 VTOY_JSON_FMT_SINT_LN(L3
, "timeout", node
->timeout
);
3292 if (node
->autoselen
)
3294 VTOY_JSON_FMT_SINT_LN(L3
, "autosel", node
->autosel
);
3297 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
3300 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
3301 VTOY_JSON_FMT_END(pos
);
3307 int ventoy_data_json_persistence(data_persistence
*data
, char *buf
, int buflen
)
3311 persistence_node
*node
= NULL
;
3312 path_node
*pathnode
= NULL
;
3314 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
3315 VTOY_JSON_FMT_ARY_BEGIN();
3317 for (node
= data
->list
; node
; node
= node
->next
)
3319 VTOY_JSON_FMT_OBJ_BEGIN();
3321 VTOY_JSON_FMT_STRN("path", node
->path
);
3323 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
3324 VTOY_JSON_FMT_SINT("valid", valid
);
3325 VTOY_JSON_FMT_SINT("type", node
->type
);
3327 VTOY_JSON_FMT_BOOL("timeouten", node
->timeouten
);
3328 VTOY_JSON_FMT_BOOL("autoselen", node
->autoselen
);
3330 VTOY_JSON_FMT_SINT("autosel", node
->autosel
);
3331 VTOY_JSON_FMT_SINT("timeout", node
->timeout
);
3333 VTOY_JSON_FMT_KEY("list");
3334 VTOY_JSON_FMT_ARY_BEGIN();
3335 for (pathnode
= node
->list
; pathnode
; pathnode
= pathnode
->next
)
3337 VTOY_JSON_FMT_OBJ_BEGIN();
3338 VTOY_JSON_FMT_STRN("path", pathnode
->path
);
3340 valid
= ventoy_is_file_exist("%s%s", g_cur_dir
, pathnode
->path
);
3341 VTOY_JSON_FMT_SINT("valid", valid
);
3342 VTOY_JSON_FMT_OBJ_ENDEX();
3344 VTOY_JSON_FMT_ARY_ENDEX();
3347 VTOY_JSON_FMT_OBJ_ENDEX();
3350 VTOY_JSON_FMT_ARY_END();
3351 VTOY_JSON_FMT_END(pos
);
3356 static int ventoy_api_get_persistence(struct mg_connection
*conn
, VTOY_JSON
*json
)
3358 api_get_func(conn
, json
, persistence
);
3362 static int ventoy_api_save_persistence(struct mg_connection
*conn
, VTOY_JSON
*json
)
3368 uint8_t timeouten
= 0;
3369 uint8_t autoselen
= 0;
3370 persistence_node
*node
= NULL
;
3371 data_persistence
*data
= NULL
;
3373 vtoy_json_get_int(json
, "index", &index
);
3374 vtoy_json_get_int(json
, "id", &id
);
3376 vtoy_json_get_bool(json
, "timeouten", &timeouten
);
3377 vtoy_json_get_bool(json
, "autoselen", &autoselen
);
3379 data
= g_data_persistence
+ index
;
3383 for (node
= data
->list
; node
; node
= node
->next
)
3387 node
->timeouten
= (int)timeouten
;
3388 node
->autoselen
= (int)autoselen
;
3389 VTOY_JSON_INT("timeout", node
->timeout
);
3390 VTOY_JSON_INT("autosel", node
->autosel
);
3396 ret
= ventoy_data_save_all();
3398 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3403 static int ventoy_api_persistence_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
3407 const char *path
= NULL
;
3408 persistence_node
*node
= NULL
;
3409 persistence_node
*cur
= NULL
;
3410 data_persistence
*data
= NULL
;
3411 VTOY_JSON
*array
= NULL
;
3413 vtoy_json_get_int(json
, "index", &index
);
3414 data
= g_data_persistence
+ index
;
3416 array
= vtoy_json_find_item(json
, JSON_TYPE_ARRAY
, "backend");
3417 path
= VTOY_JSON_STR_EX("path");
3420 if (ventoy_is_real_exist_common(path
, data
->list
, persistence_node
))
3422 ventoy_json_result(conn
, VTOY_JSON_DUPLICATE
);
3426 node
= zalloc(sizeof(persistence_node
));
3429 node
->timeouten
= 0;
3430 node
->autoselen
= 0;
3433 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
3434 node
->list
= ventoy_path_node_add_array(array
);
3436 vtoy_list_add(data
->list
, cur
, node
);
3440 ret
= ventoy_data_save_all();
3442 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3446 static int ventoy_api_persistence_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
3450 const char *path
= NULL
;
3451 persistence_node
*last
= NULL
;
3452 persistence_node
*next
= NULL
;
3453 persistence_node
*node
= NULL
;
3454 data_persistence
*data
= NULL
;
3456 vtoy_json_get_int(json
, "index", &index
);
3457 data
= g_data_persistence
+ index
;
3459 path
= VTOY_JSON_STR_EX("path");
3462 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
3464 for (node
= data
->list
; node
; node
= next
)
3467 ventoy_free_path_node_list(node
->list
);
3474 vtoy_list_del_ex(last
, node
, data
->list
, path
, ventoy_free_path_node_list
);
3478 ret
= ventoy_data_save_all();
3480 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3484 static int ventoy_api_persistence_add_inner(struct mg_connection
*conn
, VTOY_JSON
*json
)
3488 const char *path
= NULL
;
3489 const char *outpath
= NULL
;
3490 path_node
*pcur
= NULL
;
3491 path_node
*pnode
= NULL
;
3492 persistence_node
*node
= NULL
;
3493 data_persistence
*data
= NULL
;
3495 vtoy_json_get_int(json
, "index", &index
);
3496 data
= g_data_persistence
+ index
;
3498 path
= VTOY_JSON_STR_EX("path");
3499 outpath
= VTOY_JSON_STR_EX("outpath");
3500 if (path
&& outpath
)
3502 for (node
= data
->list
; node
; node
= node
->next
)
3504 if (strcmp(outpath
, node
->path
) == 0)
3506 pnode
= zalloc(sizeof(path_node
));
3509 scnprintf(pnode
->path
, sizeof(pnode
->path
), "%s", path
);
3510 vtoy_list_add(node
->list
, pcur
, pnode
);
3518 ret
= ventoy_data_save_all();
3520 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3524 static int ventoy_api_persistence_del_inner(struct mg_connection
*conn
, VTOY_JSON
*json
)
3528 const char *path
= NULL
;
3529 const char *outpath
= NULL
;
3530 path_node
*plast
= NULL
;
3531 path_node
*pnode
= NULL
;
3532 persistence_node
*node
= NULL
;
3533 data_persistence
*data
= NULL
;
3535 vtoy_json_get_int(json
, "index", &index
);
3536 data
= g_data_persistence
+ index
;
3538 path
= VTOY_JSON_STR_EX("path");
3539 outpath
= VTOY_JSON_STR_EX("outpath");
3540 if (path
&& outpath
)
3542 for (node
= data
->list
; node
; node
= node
->next
)
3544 if (strcmp(outpath
, node
->path
) == 0)
3546 vtoy_list_del(plast
, pnode
, node
->list
, path
);
3552 ret
= ventoy_data_save_all();
3554 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3562 void ventoy_data_default_injection(data_injection
*data
)
3564 memset(data
, 0, sizeof(data_injection
));
3567 int ventoy_data_cmp_injection(data_injection
*data1
, data_injection
*data2
)
3569 injection_node
*list1
= NULL
;
3570 injection_node
*list2
= NULL
;
3572 if (NULL
== data1
->list
&& NULL
== data2
->list
)
3576 else if (data1
->list
&& data2
->list
)
3578 list1
= data1
->list
;
3579 list2
= data2
->list
;
3581 while (list1
&& list2
)
3583 if ((list1
->type
!= list2
->type
) ||
3584 strcmp(list1
->path
, list2
->path
) ||
3585 strcmp(list1
->archive
, list2
->archive
))
3590 list1
= list1
->next
;
3591 list2
= list2
->next
;
3594 if (list1
== NULL
&& list2
== NULL
)
3609 int ventoy_data_save_injection(data_injection
*data
, const char *title
, char *buf
, int buflen
)
3612 injection_node
*node
= NULL
;
3614 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
3616 VTOY_JSON_FMT_KEY_L(L1
, title
);
3617 VTOY_JSON_FMT_ARY_BEGIN_N();
3619 for (node
= data
->list
; node
; node
= node
->next
)
3621 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2
);
3623 if (node
->type
== 0)
3625 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "image", node
->path
);
3629 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "parent", node
->path
);
3631 VTOY_JSON_FMT_STRN_PATH_LN(L3
, "archive", node
->archive
);
3633 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2
);
3636 VTOY_JSON_FMT_ARY_ENDEX_LN(L1
);
3637 VTOY_JSON_FMT_END(pos
);
3643 int ventoy_data_json_injection(data_injection
*data
, char *buf
, int buflen
)
3647 injection_node
*node
= NULL
;
3649 VTOY_JSON_FMT_BEGIN(pos
, buf
, buflen
);
3650 VTOY_JSON_FMT_ARY_BEGIN();
3652 for (node
= data
->list
; node
; node
= node
->next
)
3654 VTOY_JSON_FMT_OBJ_BEGIN();
3656 VTOY_JSON_FMT_UINT("type", node
->type
);
3657 VTOY_JSON_FMT_STRN("path", node
->path
);
3659 if (node
->type
== 0)
3661 valid
= ventoy_check_fuzzy_path(node
->path
, 1);
3665 valid
= ventoy_is_directory_exist("%s%s", g_cur_dir
, node
->path
);
3667 VTOY_JSON_FMT_SINT("valid", valid
);
3669 VTOY_JSON_FMT_STRN("archive", node
->archive
);
3671 valid
= ventoy_is_file_exist("%s%s", g_cur_dir
, node
->archive
);
3672 VTOY_JSON_FMT_SINT("archive_valid", valid
);
3674 VTOY_JSON_FMT_OBJ_ENDEX();
3677 VTOY_JSON_FMT_ARY_END();
3678 VTOY_JSON_FMT_END(pos
);
3684 static int ventoy_api_get_injection(struct mg_connection
*conn
, VTOY_JSON
*json
)
3686 api_get_func(conn
, json
, injection
);
3690 static int ventoy_api_save_injection(struct mg_connection
*conn
, VTOY_JSON
*json
)
3693 ret
= ventoy_data_save_all();
3695 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3699 static int ventoy_api_injection_add(struct mg_connection
*conn
, VTOY_JSON
*json
)
3704 const char *path
= NULL
;
3705 const char *archive
= NULL
;
3706 injection_node
*node
= NULL
;
3707 injection_node
*cur
= NULL
;
3708 data_injection
*data
= NULL
;
3710 vtoy_json_get_int(json
, "index", &index
);
3711 data
= g_data_injection
+ index
;
3713 vtoy_json_get_int(json
, "type", &type
);
3715 path
= VTOY_JSON_STR_EX("path");
3716 archive
= VTOY_JSON_STR_EX("archive");
3717 if (path
&& archive
)
3719 if (ventoy_is_real_exist_common(path
, data
->list
, injection_node
))
3721 ventoy_json_result(conn
, VTOY_JSON_DUPLICATE
);
3725 node
= zalloc(sizeof(injection_node
));
3730 scnprintf(node
->path
, sizeof(node
->path
), "%s", path
);
3731 scnprintf(node
->archive
, sizeof(node
->archive
), "%s", archive
);
3733 vtoy_list_add(data
->list
, cur
, node
);
3737 ret
= ventoy_data_save_all();
3739 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3743 static int ventoy_api_injection_del(struct mg_connection
*conn
, VTOY_JSON
*json
)
3747 const char *path
= NULL
;
3748 injection_node
*last
= NULL
;
3749 injection_node
*node
= NULL
;
3750 data_injection
*data
= NULL
;
3752 vtoy_json_get_int(json
, "index", &index
);
3753 data
= g_data_injection
+ index
;
3755 path
= VTOY_JSON_STR_EX("path");
3758 if (strcmp(path
, VTOY_DEL_ALL_PATH
) == 0)
3760 vtoy_list_free(injection_node
, data
->list
);
3764 vtoy_list_del(last
, node
, data
->list
, path
);
3768 ret
= ventoy_data_save_all();
3770 ventoy_json_result(conn
, ret
== 0 ? VTOY_JSON_SUCCESS_RET
: VTOY_JSON_FAILED_RET
);
3777 static int ventoy_api_preview_json(struct mg_connection
*conn
, VTOY_JSON
*json
)
3782 int utf16enclen
= 0;
3783 char *encodebuf
= NULL
;
3784 unsigned short *utf16buf
= NULL
;
3788 /* We can not use json directly, because it will be formated in the JS. */
3790 len
= ventoy_data_real_save_all(0);
3792 utf16buf
= (unsigned short *)malloc(2 * len
+ 16);
3798 utf16enclen
= (int)utf8_to_utf16((unsigned char *)JSON_SAVE_BUFFER
, len
, utf16buf
, len
+ 2);
3800 encodebuf
= (char *)malloc(utf16enclen
* 4 + 16);
3806 for (i
= 0; i
< utf16enclen
; i
++)
3808 scnprintf(encodebuf
+ i
* 4, 5, "%04X", utf16buf
[i
]);
3812 VTOY_JSON_FMT_BEGIN(pos
, JSON_BUFFER
, JSON_BUF_MAX
);
3813 VTOY_JSON_FMT_OBJ_BEGIN();
3814 VTOY_JSON_FMT_STRN("json", (encodebuf
? encodebuf
: ""));
3815 VTOY_JSON_FMT_OBJ_END();
3816 VTOY_JSON_FMT_END(pos
);
3818 CHECK_FREE(encodebuf
);
3819 CHECK_FREE(utf16buf
);
3821 ventoy_json_buffer(conn
, JSON_BUFFER
, pos
);
3829 int ventoy_data_save_all(void)
3831 ventoy_set_writeback_event();
3835 int ventoy_data_real_save_all(int apilock
)
3843 pthread_mutex_lock(&g_api_mutex
);
3846 ssprintf(pos
, JSON_SAVE_BUFFER
, JSON_BUF_MAX
, "{\n");
3848 ventoy_save_plug(control
);
3849 ventoy_save_plug(theme
);
3850 ventoy_save_plug(menu_alias
);
3851 ventoy_save_plug(menu_tip
);
3852 ventoy_save_plug(menu_class
);
3853 ventoy_save_plug(auto_install
);
3854 ventoy_save_plug(persistence
);
3855 ventoy_save_plug(injection
);
3856 ventoy_save_plug(conf_replace
);
3857 ventoy_save_plug(password
);
3858 ventoy_save_plug(image_list
);
3859 ventoy_save_plug(auto_memdisk
);
3860 ventoy_save_plug(dud
);
3862 if (JSON_SAVE_BUFFER
[pos
- 1] == '\n' && JSON_SAVE_BUFFER
[pos
- 2] == ',')
3864 JSON_SAVE_BUFFER
[pos
- 2] = '\n';
3867 ssprintf(pos
, JSON_SAVE_BUFFER
, JSON_BUF_MAX
, "}\n");
3871 pthread_mutex_unlock(&g_api_mutex
);
3877 int ventoy_http_writeback(void)
3883 ventoy_get_json_path(filename
, NULL
);
3885 pos
= ventoy_data_real_save_all(1);
3888 printf("%s", JSON_SAVE_BUFFER
);
3891 ret
= ventoy_write_buf_to_file(filename
, JSON_SAVE_BUFFER
, pos
);
3894 vlog("Failed to write ventoy.json file.\n");
3895 g_sysinfo
.config_save_error
= 1;
3902 static JSON_CB g_ventoy_json_cb
[] =
3904 { "sysinfo", ventoy_api_sysinfo
},
3905 { "handshake", ventoy_api_handshake
},
3906 { "check_path", ventoy_api_check_exist
},
3907 { "check_path2", ventoy_api_check_exist2
},
3908 { "check_fuzzy", ventoy_api_check_fuzzy
},
3910 { "device_info", ventoy_api_device_info
},
3912 { "get_control", ventoy_api_get_control
},
3913 { "save_control", ventoy_api_save_control
},
3915 { "get_theme", ventoy_api_get_theme
},
3916 { "save_theme", ventoy_api_save_theme
},
3917 { "theme_add_file", ventoy_api_theme_add_file
},
3918 { "theme_del_file", ventoy_api_theme_del_file
},
3919 { "theme_add_font", ventoy_api_theme_add_font
},
3920 { "theme_del_font", ventoy_api_theme_del_font
},
3922 { "get_alias", ventoy_api_get_alias
},
3923 { "save_alias", ventoy_api_save_alias
},
3924 { "alias_add", ventoy_api_alias_add
},
3925 { "alias_del", ventoy_api_alias_del
},
3927 { "get_tip", ventoy_api_get_tip
},
3928 { "save_tip", ventoy_api_save_tip
},
3929 { "tip_add", ventoy_api_tip_add
},
3930 { "tip_del", ventoy_api_tip_del
},
3932 { "get_class", ventoy_api_get_class
},
3933 { "save_class", ventoy_api_save_class
},
3934 { "class_add", ventoy_api_class_add
},
3935 { "class_del", ventoy_api_class_del
},
3937 { "get_auto_memdisk", ventoy_api_get_auto_memdisk
},
3938 { "save_auto_memdisk", ventoy_api_save_auto_memdisk
},
3939 { "auto_memdisk_add", ventoy_api_auto_memdisk_add
},
3940 { "auto_memdisk_del", ventoy_api_auto_memdisk_del
},
3942 { "get_image_list", ventoy_api_get_image_list
},
3943 { "save_image_list", ventoy_api_save_image_list
},
3944 { "image_list_add", ventoy_api_image_list_add
},
3945 { "image_list_del", ventoy_api_image_list_del
},
3947 { "get_conf_replace", ventoy_api_get_conf_replace
},
3948 { "save_conf_replace", ventoy_api_save_conf_replace
},
3949 { "conf_replace_add", ventoy_api_conf_replace_add
},
3950 { "conf_replace_del", ventoy_api_conf_replace_del
},
3952 { "get_dud", ventoy_api_get_dud
},
3953 { "save_dud", ventoy_api_save_dud
},
3954 { "dud_add", ventoy_api_dud_add
},
3955 { "dud_del", ventoy_api_dud_del
},
3956 { "dud_add_inner", ventoy_api_dud_add_inner
},
3957 { "dud_del_inner", ventoy_api_dud_del_inner
},
3959 { "get_auto_install", ventoy_api_get_auto_install
},
3960 { "save_auto_install", ventoy_api_save_auto_install
},
3961 { "auto_install_add", ventoy_api_auto_install_add
},
3962 { "auto_install_del", ventoy_api_auto_install_del
},
3963 { "auto_install_add_inner", ventoy_api_auto_install_add_inner
},
3964 { "auto_install_del_inner", ventoy_api_auto_install_del_inner
},
3966 { "get_persistence", ventoy_api_get_persistence
},
3967 { "save_persistence", ventoy_api_save_persistence
},
3968 { "persistence_add", ventoy_api_persistence_add
},
3969 { "persistence_del", ventoy_api_persistence_del
},
3970 { "persistence_add_inner", ventoy_api_persistence_add_inner
},
3971 { "persistence_del_inner", ventoy_api_persistence_del_inner
},
3973 { "get_password", ventoy_api_get_password
},
3974 { "save_password", ventoy_api_save_password
},
3975 { "password_add", ventoy_api_password_add
},
3976 { "password_del", ventoy_api_password_del
},
3978 { "get_injection", ventoy_api_get_injection
},
3979 { "save_injection", ventoy_api_save_injection
},
3980 { "injection_add", ventoy_api_injection_add
},
3981 { "injection_del", ventoy_api_injection_del
},
3982 { "preview_json", ventoy_api_preview_json
},
3986 static int ventoy_json_handler(struct mg_connection
*conn
, VTOY_JSON
*json
, char *jsonstr
)
3989 const char *method
= NULL
;
3991 method
= vtoy_json_get_string_ex(json
, "method");
3994 ventoy_json_result(conn
, VTOY_JSON_SUCCESS_RET
);
3998 if (strcmp(method
, "handshake") == 0)
4000 ventoy_api_handshake(conn
, json
);
4004 for (i
= 0; i
< (int)(sizeof(g_ventoy_json_cb
) / sizeof(g_ventoy_json_cb
[0])); i
++)
4006 if (strcmp(method
, g_ventoy_json_cb
[i
].method
) == 0)
4008 g_ventoy_json_cb
[i
].callback(conn
, json
);
4016 static int ventoy_request_handler(struct mg_connection
*conn
)
4020 VTOY_JSON
*json
= NULL
;
4021 char *post_data_buf
= NULL
;
4022 const struct mg_request_info
*ri
= NULL
;
4023 char stack_buf
[512];
4025 ri
= mg_get_request_info(conn
);
4027 if (strcmp(ri
->uri
, "/vtoy/json") == 0)
4029 if (ri
->content_length
> 500)
4031 post_data_buf
= malloc((int)(ri
->content_length
+ 4));
4032 post_buf_len
= (int)(ri
->content_length
+ 1);
4036 post_data_buf
= stack_buf
;
4037 post_buf_len
= sizeof(stack_buf
);
4040 post_data_len
= mg_read(conn
, post_data_buf
, post_buf_len
);
4041 post_data_buf
[post_data_len
] = 0;
4043 json
= vtoy_json_create();
4044 if (JSON_SUCCESS
== vtoy_json_parse(json
, post_data_buf
))
4046 pthread_mutex_lock(&g_api_mutex
);
4047 ventoy_json_handler(conn
, json
->pstChild
, post_data_buf
);
4048 pthread_mutex_unlock(&g_api_mutex
);
4052 ventoy_json_result(conn
, VTOY_JSON_INVALID_RET
);
4055 vtoy_json_destroy(json
);
4057 if (post_data_buf
!= stack_buf
)
4059 free(post_data_buf
);
4069 const char *ventoy_web_openfile(const struct mg_connection
*conn
, const char *path
, size_t *data_len
)
4071 ventoy_file
*node
= NULL
;
4080 node
= ventoy_tar_find_file(path
);
4083 *data_len
= node
->size
;
4095 static int ventoy_parse_control(VTOY_JSON
*json
, void *p
)
4098 VTOY_JSON
*node
= NULL
;
4099 VTOY_JSON
*child
= NULL
;
4100 data_control
*data
= (data_control
*)p
;
4102 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4107 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4109 if (node
->enDataType
== JSON_TYPE_OBJECT
)
4111 child
= node
->pstChild
;
4113 if (child
->enDataType
!= JSON_TYPE_STRING
)
4118 if (strcmp(child
->pcName
, "VTOY_DEFAULT_MENU_MODE") == 0)
4120 CONTROL_PARSE_INT_DEF_0(child
, data
->default_menu_mode
);
4122 else if (strcmp(child
->pcName
, "VTOY_WIN11_BYPASS_CHECK") == 0)
4124 CONTROL_PARSE_INT_DEF_0(child
, data
->win11_bypass_check
);
4126 else if (strcmp(child
->pcName
, "VTOY_LINUX_REMOUNT") == 0)
4128 CONTROL_PARSE_INT_DEF_0(child
, data
->linux_remount
);
4130 else if (strcmp(child
->pcName
, "VTOY_SECONDARY_BOOT_MENU") == 0)
4132 CONTROL_PARSE_INT_DEF_1(child
, data
->secondary_menu
);
4134 else if (strcmp(child
->pcName
, "VTOY_SHOW_PASSWORD_ASTERISK") == 0)
4136 CONTROL_PARSE_INT_DEF_1(child
, data
->password_asterisk
);
4138 else if (strcmp(child
->pcName
, "VTOY_TREE_VIEW_MENU_STYLE") == 0)
4140 CONTROL_PARSE_INT_DEF_0(child
, data
->treeview_style
);
4142 else if (strcmp(child
->pcName
, "VTOY_FILT_DOT_UNDERSCORE_FILE") == 0)
4144 CONTROL_PARSE_INT_DEF_1(child
, data
->filter_dot_underscore
);
4146 else if (strcmp(child
->pcName
, "VTOY_SORT_CASE_SENSITIVE") == 0)
4148 CONTROL_PARSE_INT_DEF_0(child
, data
->sort_casesensitive
);
4150 else if (strcmp(child
->pcName
, "VTOY_MAX_SEARCH_LEVEL") == 0)
4152 if (strcmp(child
->unData
.pcStrVal
, "max") == 0)
4154 data
->max_search_level
= -1;
4158 data
->max_search_level
= (int)strtol(child
->unData
.pcStrVal
, NULL
, 10);
4161 else if (strcmp(child
->pcName
, "VTOY_DEFAULT_SEARCH_ROOT") == 0)
4163 strlcpy(data
->default_search_root
, child
->unData
.pcStrVal
);
4165 else if (strcmp(child
->pcName
, "VTOY_DEFAULT_IMAGE") == 0)
4167 strlcpy(data
->default_image
, child
->unData
.pcStrVal
);
4169 else if (strcmp(child
->pcName
, "VTOY_DEFAULT_KBD_LAYOUT") == 0)
4171 for (i
= 0; g_ventoy_kbd_layout
[i
]; i
++)
4173 if (strcmp(child
->unData
.pcStrVal
, g_ventoy_kbd_layout
[i
]) == 0)
4175 strlcpy(data
->default_kbd_layout
, child
->unData
.pcStrVal
);
4180 else if (strcmp(child
->pcName
, "VTOY_MENU_LANGUAGE") == 0)
4182 for (i
= 0; g_ventoy_menu_lang
[i
][0]; i
++)
4184 if (strcmp(child
->unData
.pcStrVal
, g_ventoy_menu_lang
[i
]) == 0)
4186 strlcpy(data
->menu_language
, child
->unData
.pcStrVal
);
4191 else if (strcmp(child
->pcName
, "VTOY_MENU_TIMEOUT") == 0)
4193 data
->menu_timeout
= (int)strtol(child
->unData
.pcStrVal
, NULL
, 10);
4195 else if (strcmp(child
->pcName
, "VTOY_SECONDARY_TIMEOUT") == 0)
4197 data
->secondary_menu_timeout
= (int)strtol(child
->unData
.pcStrVal
, NULL
, 10);
4199 else if (strcmp(child
->pcName
, "VTOY_VHD_NO_WARNING") == 0)
4201 CONTROL_PARSE_INT_DEF_0(child
, data
->vhd_no_warning
);
4203 else if (strcmp(child
->pcName
, "VTOY_FILE_FLT_ISO") == 0)
4205 CONTROL_PARSE_INT_DEF_0(child
, data
->filter_iso
);
4207 else if (strcmp(child
->pcName
, "VTOY_FILE_FLT_IMG") == 0)
4209 CONTROL_PARSE_INT_DEF_0(child
, data
->filter_img
);
4211 else if (strcmp(child
->pcName
, "VTOY_FILE_FLT_EFI") == 0)
4213 CONTROL_PARSE_INT_DEF_0(child
, data
->filter_efi
);
4215 else if (strcmp(child
->pcName
, "VTOY_FILE_FLT_WIM") == 0)
4217 CONTROL_PARSE_INT_DEF_0(child
, data
->filter_wim
);
4219 else if (strcmp(child
->pcName
, "VTOY_FILE_FLT_VHD") == 0)
4221 CONTROL_PARSE_INT_DEF_0(child
, data
->filter_vhd
);
4223 else if (strcmp(child
->pcName
, "VTOY_FILE_FLT_VTOY") == 0)
4225 CONTROL_PARSE_INT_DEF_0(child
, data
->filter_vtoy
);
4233 static int ventoy_parse_theme(VTOY_JSON
*json
, void *p
)
4235 const char *dismode
= NULL
;
4236 VTOY_JSON
*child
= NULL
;
4237 VTOY_JSON
*node
= NULL
;
4238 path_node
*tail
= NULL
;
4239 path_node
*pnode
= NULL
;
4240 data_theme
*data
= (data_theme
*)p
;
4242 if (json
->enDataType
!= JSON_TYPE_OBJECT
)
4247 child
= json
->pstChild
;
4249 dismode
= vtoy_json_get_string_ex(child
, "display_mode");
4250 vtoy_json_get_string(child
, "ventoy_left", sizeof(data
->ventoy_left
), data
->ventoy_left
);
4251 vtoy_json_get_string(child
, "ventoy_top", sizeof(data
->ventoy_top
), data
->ventoy_top
);
4252 vtoy_json_get_string(child
, "ventoy_color", sizeof(data
->ventoy_color
), data
->ventoy_color
);
4254 vtoy_json_get_int(child
, "default_file", &(data
->default_file
));
4255 vtoy_json_get_string(child
, "gfxmode", sizeof(data
->gfxmode
), data
->gfxmode
);
4256 vtoy_json_get_string(child
, "serial_param", sizeof(data
->serial_param
), data
->serial_param
);
4260 if (strcmp(dismode
, "CLI") == 0)
4262 data
->display_mode
= display_mode_cli
;
4264 else if (strcmp(dismode
, "serial") == 0)
4266 data
->display_mode
= display_mode_serial
;
4268 else if (strcmp(dismode
, "serial_console") == 0)
4270 data
->display_mode
= display_mode_ser_console
;
4274 data
->display_mode
= display_mode_gui
;
4278 node
= vtoy_json_find_item(child
, JSON_TYPE_STRING
, "file");
4281 data
->default_file
= 0;
4283 pnode
= zalloc(sizeof(path_node
));
4286 strlcpy(pnode
->path
, node
->unData
.pcStrVal
);
4287 data
->filelist
= pnode
;
4292 node
= vtoy_json_find_item(child
, JSON_TYPE_ARRAY
, "file");
4295 for (node
= node
->pstChild
; node
; node
= node
->pstNext
)
4297 if (node
->enDataType
== JSON_TYPE_STRING
)
4299 pnode
= zalloc(sizeof(path_node
));
4302 strlcpy(pnode
->path
, node
->unData
.pcStrVal
);
4310 data
->filelist
= tail
= pnode
;
4319 node
= vtoy_json_find_item(child
, JSON_TYPE_ARRAY
, "fonts");
4322 for (node
= node
->pstChild
; node
; node
= node
->pstNext
)
4324 if (node
->enDataType
== JSON_TYPE_STRING
)
4326 pnode
= zalloc(sizeof(path_node
));
4329 strlcpy(pnode
->path
, node
->unData
.pcStrVal
);
4330 if (data
->fontslist
)
4337 data
->fontslist
= tail
= pnode
;
4346 static int ventoy_parse_menu_alias(VTOY_JSON
*json
, void *p
)
4349 const char *path
= NULL
;
4350 const char *alias
= NULL
;
4351 data_alias
*data
= (data_alias
*)p
;
4352 data_alias_node
*tail
= NULL
;
4353 data_alias_node
*pnode
= NULL
;
4354 VTOY_JSON
*node
= NULL
;
4356 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4361 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4363 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4368 type
= path_type_file
;
4369 path
= vtoy_json_get_string_ex(node
->pstChild
, "image");
4372 path
= vtoy_json_get_string_ex(node
->pstChild
, "dir");
4373 type
= path_type_dir
;
4375 alias
= vtoy_json_get_string_ex(node
->pstChild
, "alias");
4379 pnode
= zalloc(sizeof(data_alias_node
));
4383 strlcpy(pnode
->path
, path
);
4384 strlcpy(pnode
->alias
, alias
);
4393 data
->list
= tail
= pnode
;
4402 static int ventoy_parse_menu_tip(VTOY_JSON
*json
, void *p
)
4405 const char *path
= NULL
;
4406 const char *tip
= NULL
;
4407 data_tip
*data
= (data_tip
*)p
;
4408 data_tip_node
*tail
= NULL
;
4409 data_tip_node
*pnode
= NULL
;
4410 VTOY_JSON
*node
= NULL
;
4411 VTOY_JSON
*tips
= NULL
;
4413 if (json
->enDataType
!= JSON_TYPE_OBJECT
)
4418 vtoy_json_get_string(json
->pstChild
, "left", sizeof(data
->left
), data
->left
);
4419 vtoy_json_get_string(json
->pstChild
, "top", sizeof(data
->top
), data
->top
);
4420 vtoy_json_get_string(json
->pstChild
, "color", sizeof(data
->color
), data
->color
);
4422 tips
= vtoy_json_find_item(json
->pstChild
, JSON_TYPE_ARRAY
, "tips");
4428 for (node
= tips
->pstChild
; node
; node
= node
->pstNext
)
4430 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4435 type
= path_type_file
;
4436 path
= vtoy_json_get_string_ex(node
->pstChild
, "image");
4439 path
= vtoy_json_get_string_ex(node
->pstChild
, "dir");
4440 type
= path_type_dir
;
4442 tip
= vtoy_json_get_string_ex(node
->pstChild
, "tip");
4446 pnode
= zalloc(sizeof(data_tip_node
));
4450 strlcpy(pnode
->path
, path
);
4451 strlcpy(pnode
->tip
, tip
);
4460 data
->list
= tail
= pnode
;
4468 static int ventoy_parse_menu_class(VTOY_JSON
*json
, void *p
)
4471 const char *path
= NULL
;
4472 const char *class = NULL
;
4473 data_class
*data
= (data_class
*)p
;
4474 data_class_node
*tail
= NULL
;
4475 data_class_node
*pnode
= NULL
;
4476 VTOY_JSON
*node
= NULL
;
4478 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4483 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4485 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4490 type
= class_type_key
;
4491 path
= vtoy_json_get_string_ex(node
->pstChild
, "key");
4494 type
= class_type_dir
;
4495 path
= vtoy_json_get_string_ex(node
->pstChild
, "dir");
4498 type
= class_type_parent
;
4499 path
= vtoy_json_get_string_ex(node
->pstChild
, "parent");
4502 class = vtoy_json_get_string_ex(node
->pstChild
, "class");
4506 pnode
= zalloc(sizeof(data_class_node
));
4510 strlcpy(pnode
->path
, path
);
4511 strlcpy(pnode
->class, class);
4520 data
->list
= tail
= pnode
;
4528 static int ventoy_parse_auto_install(VTOY_JSON
*json
, void *p
)
4536 const char *path
= NULL
;
4537 const char *file
= NULL
;
4538 data_auto_install
*data
= (data_auto_install
*)p
;
4539 auto_install_node
*tail
= NULL
;
4540 auto_install_node
*pnode
= NULL
;
4541 path_node
*pathnode
= NULL
;
4542 path_node
*pathtail
= NULL
;
4543 VTOY_JSON
*node
= NULL
;
4544 VTOY_JSON
*filelist
= NULL
;
4545 VTOY_JSON
*filenode
= NULL
;
4547 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4552 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4554 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4560 path
= vtoy_json_get_string_ex(node
->pstChild
, "image");
4563 path
= vtoy_json_get_string_ex(node
->pstChild
, "parent");
4571 file
= vtoy_json_get_string_ex(node
->pstChild
, "template");
4574 pnode
= zalloc(sizeof(auto_install_node
));
4579 strlcpy(pnode
->path
, path
);
4581 pathnode
= zalloc(sizeof(path_node
));
4584 strlcpy(pathnode
->path
, file
);
4585 pnode
->list
= pathnode
;
4599 data
->list
= tail
= pnode
;
4607 timeouten
= autoselen
= 0;
4608 if (JSON_SUCCESS
== vtoy_json_get_int(node
->pstChild
, "timeout", &timeout
))
4612 if (JSON_SUCCESS
== vtoy_json_get_int(node
->pstChild
, "autosel", &autosel
))
4617 filelist
= vtoy_json_find_item(node
->pstChild
, JSON_TYPE_ARRAY
, "template");
4623 pnode
= zalloc(sizeof(auto_install_node
));
4630 pnode
->autoselen
= autoselen
;
4631 pnode
->timeouten
= timeouten
;
4632 pnode
->timeout
= timeout
;
4633 pnode
->autosel
= autosel
;
4634 strlcpy(pnode
->path
, path
);
4637 for (filenode
= filelist
->pstChild
; filenode
; filenode
= filenode
->pstNext
)
4639 if (filenode
->enDataType
!= JSON_TYPE_STRING
)
4644 pathnode
= zalloc(sizeof(path_node
));
4648 strlcpy(pathnode
->path
, filenode
->unData
.pcStrVal
);
4652 pathtail
->next
= pathnode
;
4653 pathtail
= pathnode
;
4657 pnode
->list
= pathtail
= pathnode
;
4668 if (pnode
->autoselen
&& pnode
->autosel
> count
)
4680 data
->list
= tail
= pnode
;
4687 static int ventoy_parse_persistence(VTOY_JSON
*json
, void *p
)
4694 const char *path
= NULL
;
4695 const char *file
= NULL
;
4696 data_persistence
*data
= (data_persistence
*)p
;
4697 persistence_node
*tail
= NULL
;
4698 persistence_node
*pnode
= NULL
;
4699 path_node
*pathnode
= NULL
;
4700 path_node
*pathtail
= NULL
;
4701 VTOY_JSON
*node
= NULL
;
4702 VTOY_JSON
*filelist
= NULL
;
4703 VTOY_JSON
*filenode
= NULL
;
4705 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4710 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4712 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4717 path
= vtoy_json_get_string_ex(node
->pstChild
, "image");
4723 file
= vtoy_json_get_string_ex(node
->pstChild
, "backend");
4726 pnode
= zalloc(sizeof(persistence_node
));
4731 strlcpy(pnode
->path
, path
);
4733 pathnode
= zalloc(sizeof(path_node
));
4736 strlcpy(pathnode
->path
, file
);
4737 pnode
->list
= pathnode
;
4751 data
->list
= tail
= pnode
;
4759 timeouten
= autoselen
= 0;
4760 if (JSON_SUCCESS
== vtoy_json_get_int(node
->pstChild
, "timeout", &timeout
))
4764 if (JSON_SUCCESS
== vtoy_json_get_int(node
->pstChild
, "autosel", &autosel
))
4769 filelist
= vtoy_json_find_item(node
->pstChild
, JSON_TYPE_ARRAY
, "backend");
4775 pnode
= zalloc(sizeof(persistence_node
));
4782 pnode
->autoselen
= autoselen
;
4783 pnode
->timeouten
= timeouten
;
4784 pnode
->timeout
= timeout
;
4785 pnode
->autosel
= autosel
;
4786 strlcpy(pnode
->path
, path
);
4789 for (filenode
= filelist
->pstChild
; filenode
; filenode
= filenode
->pstNext
)
4791 if (filenode
->enDataType
!= JSON_TYPE_STRING
)
4796 pathnode
= zalloc(sizeof(path_node
));
4800 strlcpy(pathnode
->path
, filenode
->unData
.pcStrVal
);
4804 pathtail
->next
= pathnode
;
4805 pathtail
= pathnode
;
4809 pnode
->list
= pathtail
= pathnode
;
4820 if (pnode
->autoselen
&& pnode
->autosel
> count
)
4832 data
->list
= tail
= pnode
;
4839 static int ventoy_parse_injection(VTOY_JSON
*json
, void *p
)
4842 const char *path
= NULL
;
4843 const char *archive
= NULL
;
4844 data_injection
*data
= (data_injection
*)p
;
4845 injection_node
*tail
= NULL
;
4846 injection_node
*pnode
= NULL
;
4847 VTOY_JSON
*node
= NULL
;
4849 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4854 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4856 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4862 path
= vtoy_json_get_string_ex(node
->pstChild
, "image");
4865 path
= vtoy_json_get_string_ex(node
->pstChild
, "parent");
4868 archive
= vtoy_json_get_string_ex(node
->pstChild
, "archive");
4870 if (path
&& archive
)
4872 pnode
= zalloc(sizeof(injection_node
));
4876 strlcpy(pnode
->path
, path
);
4877 strlcpy(pnode
->archive
, archive
);
4886 data
->list
= tail
= pnode
;
4894 static int ventoy_parse_conf_replace(VTOY_JSON
*json
, void *p
)
4897 const char *path
= NULL
;
4898 const char *org
= NULL
;
4899 const char *new = NULL
;
4900 data_conf_replace
*data
= (data_conf_replace
*)p
;
4901 conf_replace_node
*tail
= NULL
;
4902 conf_replace_node
*pnode
= NULL
;
4903 VTOY_JSON
*node
= NULL
;
4905 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
4910 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
4912 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
4917 path
= vtoy_json_get_string_ex(node
->pstChild
, "iso");
4918 org
= vtoy_json_get_string_ex(node
->pstChild
, "org");
4919 new = vtoy_json_get_string_ex(node
->pstChild
, "new");
4922 vtoy_json_get_int(node
->pstChild
, "img", &img
);
4924 if (path
&& org
&& new)
4926 pnode
= zalloc(sizeof(conf_replace_node
));
4929 strlcpy(pnode
->path
, path
);
4930 strlcpy(pnode
->org
, org
);
4931 strlcpy(pnode
->new, new);
4944 data
->list
= tail
= pnode
;
4952 static int ventoy_parse_password(VTOY_JSON
*json
, void *p
)
4955 const char *bootpwd
= NULL
;
4956 const char *isopwd
= NULL
;
4957 const char *wimpwd
= NULL
;
4958 const char *imgpwd
= NULL
;
4959 const char *efipwd
= NULL
;
4960 const char *vhdpwd
= NULL
;
4961 const char *vtoypwd
= NULL
;
4962 const char *path
= NULL
;
4963 const char *pwd
= NULL
;
4964 data_password
*data
= (data_password
*)p
;
4965 menu_password
*tail
= NULL
;
4966 menu_password
*pnode
= NULL
;
4967 VTOY_JSON
*node
= NULL
;
4968 VTOY_JSON
*menupwd
= NULL
;
4970 if (json
->enDataType
!= JSON_TYPE_OBJECT
)
4975 bootpwd
= vtoy_json_get_string_ex(json
->pstChild
, "bootpwd");
4976 isopwd
= vtoy_json_get_string_ex(json
->pstChild
, "isopwd");
4977 wimpwd
= vtoy_json_get_string_ex(json
->pstChild
, "wimpwd");
4978 imgpwd
= vtoy_json_get_string_ex(json
->pstChild
, "imgpwd");
4979 efipwd
= vtoy_json_get_string_ex(json
->pstChild
, "efipwd");
4980 vhdpwd
= vtoy_json_get_string_ex(json
->pstChild
, "vhdpwd");
4981 vtoypwd
= vtoy_json_get_string_ex(json
->pstChild
, "vtoypwd");
4984 if (bootpwd
) strlcpy(data
->bootpwd
, bootpwd
);
4985 if (isopwd
) strlcpy(data
->isopwd
, isopwd
);
4986 if (wimpwd
) strlcpy(data
->wimpwd
, wimpwd
);
4987 if (imgpwd
) strlcpy(data
->imgpwd
, imgpwd
);
4988 if (efipwd
) strlcpy(data
->efipwd
, efipwd
);
4989 if (vhdpwd
) strlcpy(data
->vhdpwd
, vhdpwd
);
4990 if (vtoypwd
) strlcpy(data
->vtoypwd
, vtoypwd
);
4993 menupwd
= vtoy_json_find_item(json
->pstChild
, JSON_TYPE_ARRAY
, "menupwd");
4999 for (node
= menupwd
->pstChild
; node
; node
= node
->pstNext
)
5001 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
5007 path
= vtoy_json_get_string_ex(node
->pstChild
, "file");
5010 path
= vtoy_json_get_string_ex(node
->pstChild
, "parent");
5013 pwd
= vtoy_json_get_string_ex(node
->pstChild
, "pwd");
5017 pnode
= zalloc(sizeof(menu_password
));
5021 strlcpy(pnode
->path
, path
);
5022 strlcpy(pnode
->pwd
, pwd
);
5031 data
->list
= tail
= pnode
;
5040 static int ventoy_parse_image_list_real(VTOY_JSON
*json
, int type
, void *p
)
5042 VTOY_JSON
*node
= NULL
;
5043 data_image_list
*data
= (data_image_list
*)p
;
5044 path_node
*tail
= NULL
;
5045 path_node
*pnode
= NULL
;
5047 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
5054 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
5056 if (node
->enDataType
== JSON_TYPE_STRING
)
5058 pnode
= zalloc(sizeof(path_node
));
5061 strlcpy(pnode
->path
, node
->unData
.pcStrVal
);
5069 data
->list
= tail
= pnode
;
5077 static int ventoy_parse_image_blacklist(VTOY_JSON
*json
, void *p
)
5079 return ventoy_parse_image_list_real(json
, 1, p
);
5081 static int ventoy_parse_image_list(VTOY_JSON
*json
, void *p
)
5083 return ventoy_parse_image_list_real(json
, 0, p
);
5086 static int ventoy_parse_auto_memdisk(VTOY_JSON
*json
, void *p
)
5088 VTOY_JSON
*node
= NULL
;
5089 data_auto_memdisk
*data
= (data_auto_memdisk
*)p
;
5090 path_node
*tail
= NULL
;
5091 path_node
*pnode
= NULL
;
5093 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
5098 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
5100 if (node
->enDataType
== JSON_TYPE_STRING
)
5102 pnode
= zalloc(sizeof(path_node
));
5105 strlcpy(pnode
->path
, node
->unData
.pcStrVal
);
5113 data
->list
= tail
= pnode
;
5121 static int ventoy_parse_dud(VTOY_JSON
*json
, void *p
)
5124 const char *path
= NULL
;
5125 const char *file
= NULL
;
5126 data_dud
*data
= (data_dud
*)p
;
5127 dud_node
*tail
= NULL
;
5128 dud_node
*pnode
= NULL
;
5129 path_node
*pathnode
= NULL
;
5130 path_node
*pathtail
= NULL
;
5131 VTOY_JSON
*node
= NULL
;
5132 VTOY_JSON
*filelist
= NULL
;
5133 VTOY_JSON
*filenode
= NULL
;
5135 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
5140 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
5142 if (node
->enDataType
!= JSON_TYPE_OBJECT
)
5147 path
= vtoy_json_get_string_ex(node
->pstChild
, "image");
5153 file
= vtoy_json_get_string_ex(node
->pstChild
, "dud");
5156 pnode
= zalloc(sizeof(dud_node
));
5159 strlcpy(pnode
->path
, path
);
5161 pathnode
= zalloc(sizeof(path_node
));
5164 strlcpy(pathnode
->path
, file
);
5165 pnode
->list
= pathnode
;
5179 data
->list
= tail
= pnode
;
5186 filelist
= vtoy_json_find_item(node
->pstChild
, JSON_TYPE_ARRAY
, "dud");
5192 pnode
= zalloc(sizeof(dud_node
));
5198 strlcpy(pnode
->path
, path
);
5200 for (filenode
= filelist
->pstChild
; filenode
; filenode
= filenode
->pstNext
)
5202 if (filenode
->enDataType
!= JSON_TYPE_STRING
)
5207 pathnode
= zalloc(sizeof(path_node
));
5210 strlcpy(pathnode
->path
, filenode
->unData
.pcStrVal
);
5215 pathtail
->next
= pathnode
;
5216 pathtail
= pathnode
;
5220 pnode
->list
= pathtail
= pathnode
;
5238 data
->list
= tail
= pnode
;
5252 static int ventoy_load_old_json(const char *filename
)
5257 char *buffer
= NULL
;
5258 unsigned char *start
= NULL
;
5259 VTOY_JSON
*json
= NULL
;
5260 VTOY_JSON
*node
= NULL
;
5261 VTOY_JSON
*next
= NULL
;
5263 ret
= ventoy_read_file_to_buf(filename
, 4, (void **)&buffer
, &buflen
);
5266 vlog("Failed to read old ventoy.json file.\n");
5271 start
= (unsigned char *)buffer
;
5273 if (start
[0] == 0xef && start
[1] == 0xbb && start
[2] == 0xbf)
5277 else if ((start
[0] == 0xff && start
[1] == 0xfe) || (start
[0] == 0xfe && start
[1] == 0xff))
5279 vlog("ventoy.json is in UCS-2 encoding, ignore it.\n");
5284 json
= vtoy_json_create();
5291 if (vtoy_json_parse_ex(json
, buffer
+ offset
, buflen
- offset
) == JSON_SUCCESS
)
5293 vlog("parse ventoy.json success\n");
5295 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
5296 for (next
= node
->pstNext
; next
; next
= next
->pstNext
)
5298 if (node
->pcName
&& next
->pcName
&& strcmp(node
->pcName
, next
->pcName
) == 0)
5300 vlog("ventoy.json contains duplicate key <%s>.\n", node
->pcName
);
5301 g_sysinfo
.invalid_config
= 1;
5307 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
5309 ventoy_parse_json(control
);
5310 ventoy_parse_json(theme
);
5311 ventoy_parse_json(menu_alias
);
5312 ventoy_parse_json(menu_tip
);
5313 ventoy_parse_json(menu_class
);
5314 ventoy_parse_json(auto_install
);
5315 ventoy_parse_json(persistence
);
5316 ventoy_parse_json(injection
);
5317 ventoy_parse_json(conf_replace
);
5318 ventoy_parse_json(password
);
5319 ventoy_parse_json(image_list
);
5320 ventoy_parse_json(image_blacklist
);
5321 ventoy_parse_json(auto_memdisk
);
5322 ventoy_parse_json(dud
);
5327 vlog("ventoy.json has syntax error.\n");
5328 g_sysinfo
.syntax_error
= 1;
5333 vtoy_json_destroy(json
);
5340 int ventoy_http_start(const char *ip
, const char *port
)
5346 char backupname
[128];
5347 struct mg_callbacks callbacks
;
5348 const char *options
[] =
5350 "listening_ports", "24681",
5351 "document_root", "www",
5352 "index_files", "index.html",
5353 "num_threads", "16",
5354 "error_log_file", LOG_FILE
,
5355 "request_timeout_ms", "10000",
5359 for (i
= 0; i
<= bios_max
; i
++)
5361 ventoy_data_default_control(g_data_control
+ i
);
5362 ventoy_data_default_theme(g_data_theme
+ i
);
5363 ventoy_data_default_menu_alias(g_data_menu_alias
+ i
);
5364 ventoy_data_default_menu_class(g_data_menu_class
+ i
);
5365 ventoy_data_default_menu_tip(g_data_menu_tip
+ i
);
5366 ventoy_data_default_auto_install(g_data_auto_install
+ i
);
5367 ventoy_data_default_persistence(g_data_persistence
+ i
);
5368 ventoy_data_default_injection(g_data_injection
+ i
);
5369 ventoy_data_default_conf_replace(g_data_conf_replace
+ i
);
5370 ventoy_data_default_password(g_data_password
+ i
);
5371 ventoy_data_default_image_list(g_data_image_list
+ i
);
5372 ventoy_data_default_auto_memdisk(g_data_auto_memdisk
+ i
);
5373 ventoy_data_default_dud(g_data_dud
+ i
);
5376 ventoy_get_json_path(filename
, backupname
);
5377 if (ventoy_is_file_exist("%s", filename
))
5379 ventoy_copy_file(filename
, backupname
);
5380 ret
= ventoy_load_old_json(filename
);
5383 ventoy_data_real_save_all(0);
5388 scnprintf(addr
, sizeof(addr
), "%s:%s", ip
, port
);
5391 memset(&callbacks
, 0, sizeof(callbacks
));
5392 callbacks
.begin_request
= ventoy_request_handler
;
5394 callbacks
.open_file
= ventoy_web_openfile
;
5396 g_ventoy_http_ctx
= mg_start(&callbacks
, NULL
, options
);
5398 ventoy_start_writeback_thread(ventoy_http_writeback
);
5400 return g_ventoy_http_ctx
? 0 : 1;
5403 int ventoy_http_stop(void)
5405 if (g_ventoy_http_ctx
)
5407 mg_stop(g_ventoy_http_ctx
);
5410 ventoy_stop_writeback_thread();
5414 int ventoy_http_init(void)
5419 char *Buffer
= NULL
;
5422 ventoy_read_file_to_buf("www/menulist", 4, (void **)&Buffer
, &BufLen
);
5425 for (i
= 0; i
< BufLen
/ 5; i
++)
5427 memcpy(g_ventoy_menu_lang
[i
], Buffer
+ i
* 5, 5);
5428 g_ventoy_menu_lang
[i
][5] = 0;
5435 file
= ventoy_tar_find_file("www/menulist");
5438 for (i
= 0; i
< file
->size
/ 5; i
++)
5440 memcpy(g_ventoy_menu_lang
[i
], (char *)(file
->addr
) + i
* 5, 5);
5441 g_ventoy_menu_lang
[i
][5] = 0;
5446 if (!g_pub_json_buffer
)
5448 g_pub_json_buffer
= malloc(JSON_BUF_MAX
* 2);
5449 g_pub_save_buffer
= g_pub_json_buffer
+ JSON_BUF_MAX
;
5453 pthread_mutex_init(&g_api_mutex
, NULL
);
5457 void ventoy_http_exit(void)
5459 check_free(g_pub_json_buffer
);
5460 g_pub_json_buffer
= NULL
;
5461 g_pub_save_buffer
= NULL
;
5463 pthread_mutex_destroy(&g_api_mutex
);