]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - Plugson/src/Web/ventoy_http.c
Add muli-languages support for Ventoy menu.
[Ventoy.git] / Plugson / src / Web / ventoy_http.c
1 /******************************************************************************
2 * ventoy_http.c ---- ventoy http
3 * Copyright (c) 2021, longpanda <admin@ventoy.net>
4 *
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.
9 *
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.
14 *
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/>.
17 *
18 */
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <stdint.h>
22 #include <string.h>
23 #include <stdarg.h>
24 #include <errno.h>
25 #include <time.h>
26
27 #if defined(_MSC_VER) || defined(WIN32)
28 #else
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <sys/types.h>
32 #include <sys/ioctl.h>
33 #include <sys/stat.h>
34 #include <sys/types.h>
35 #include <sys/mount.h>
36 #include <linux/fs.h>
37 #include <linux/limits.h>
38 #include <dirent.h>
39 #include <pthread.h>
40 #endif
41
42 #include <ventoy_define.h>
43 #include <ventoy_json.h>
44 #include <ventoy_util.h>
45 #include <ventoy_disk.h>
46 #include <ventoy_http.h>
47 #include "fat_filelib.h"
48
49 static const char *g_json_title_postfix[bios_max + 1] =
50 {
51 "", "_legacy", "_uefi", "_ia32", "_aa64", "_mips", ""
52 };
53
54 static const char *g_ventoy_kbd_layout[] =
55 {
56 "QWERTY_USA", "AZERTY", "CZECH_QWERTY", "CZECH_QWERTZ", "DANISH",
57 "DVORAK_USA", "FRENCH", "GERMAN", "ITALIANO", "JAPAN_106", "LATIN_USA",
58 "PORTU_BRAZIL", "QWERTY_UK", "QWERTZ", "QWERTZ_HUN", "QWERTZ_SLOV_CROAT",
59 "SPANISH", "SWEDISH", "TURKISH_Q", "VIETNAMESE",
60 NULL
61 };
62
63 #define VTOY_DEL_ALL_PATH "4119ae33-98ea-448e-b9c0-569aafcf1fb4"
64
65 static int g_json_exist[plugin_type_max][bios_max];
66 static const char *g_plugin_name[plugin_type_max] =
67 {
68 "control", "theme", "menu_alias", "menu_tip",
69 "menu_class", "auto_install", "persistence", "injection",
70 "conf_replace", "password", "image_list",
71 "auto_memdisk", "dud"
72 };
73
74 static char g_ventoy_help_lang[MAX_LANGUAGE][8];
75 static char g_ventoy_menu_lang[MAX_LANGUAGE][8];
76
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];
92
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
97
98 static pthread_mutex_t g_api_mutex;
99 static struct mg_context *g_ventoy_http_ctx = NULL;
100
101 static int ventoy_is_kbd_valid(const char *key)
102 {
103 int i = 0;
104
105 for (i = 0; g_ventoy_kbd_layout[i]; i++)
106 {
107 if (strcmp(g_ventoy_kbd_layout[i], key) == 0)
108 {
109 return 1;
110 }
111 }
112
113 return 0;
114 }
115
116 static const char * ventoy_real_path(const char *org)
117 {
118 int count = 0;
119
120 if (g_sysinfo.pathcase)
121 {
122 scnprintf(g_pub_path, MAX_PATH, "%s", org);
123 count = ventoy_path_case(g_pub_path + 1, 1);
124 if (count > 0)
125 {
126 return g_pub_path;
127 }
128 return org;
129 }
130 else
131 {
132 return org;
133 }
134 }
135
136
137 static int ventoy_json_result(struct mg_connection *conn, const char *err)
138 {
139 mg_printf(conn,
140 "HTTP/1.1 200 OK \r\n"
141 "Content-Type: application/json\r\n"
142 "Content-Length: %d\r\n"
143 "\r\n%s",
144 (int)strlen(err), err);
145
146 return 0;
147 }
148
149 static int ventoy_json_buffer(struct mg_connection *conn, const char *json_buf, int json_len)
150 {
151 mg_printf(conn,
152 "HTTP/1.1 200 OK \r\n"
153 "Content-Type: application/json\r\n"
154 "Content-Length: %d\r\n"
155 "\r\n%s",
156 json_len, json_buf);
157
158 return 0;
159 }
160
161 static void ventoy_free_path_node_list(path_node *list)
162 {
163 path_node *next = NULL;
164 path_node *node = list;
165
166 while (node)
167 {
168 next = node->next;
169 free(node);
170 node = next;
171 }
172 }
173
174 static path_node * ventoy_path_node_add_array(VTOY_JSON *array)
175 {
176 path_node *head = NULL;
177 path_node *node = NULL;
178 path_node *cur = NULL;
179 VTOY_JSON *item = NULL;
180
181 for (item = array->pstChild; item; item = item->pstNext)
182 {
183 node = zalloc(sizeof(path_node));
184 if (node)
185 {
186 scnprintf(node->path, sizeof(node->path), "%s", item->unData.pcStrVal);
187 vtoy_list_add(head, cur, node);
188 }
189 }
190
191 return head;
192 }
193
194 static int ventoy_check_fuzzy_path(char *path, int prefix)
195 {
196 int rc;
197 char c;
198 char *cur = NULL;
199 char *pos = NULL;
200
201 if (!path)
202 {
203 return 0;
204 }
205
206 pos = strchr(path, '*');
207 if (pos)
208 {
209 for (cur = pos; *cur; cur++)
210 {
211 if (*cur == '/')
212 {
213 return 0;
214 }
215 }
216
217 while (pos != path)
218 {
219 if (*pos == '/')
220 {
221 break;
222 }
223 pos--;
224 }
225
226 if (*pos == '/')
227 {
228 if (pos != path)
229 {
230 c = *pos;
231 *pos = 0;
232 if (prefix)
233 {
234 rc = ventoy_is_directory_exist("%s%s", g_cur_dir, path);
235 }
236 else
237 {
238 rc = ventoy_is_directory_exist("%s", path);
239 }
240 *pos = c;
241
242 if (rc == 0)
243 {
244 return 0;
245 }
246 }
247
248 return -1;
249 }
250 else
251 {
252 return 0;
253 }
254 }
255 else
256 {
257 if (prefix)
258 {
259 return ventoy_is_file_exist("%s%s", g_cur_dir, path);
260 }
261 else
262 {
263 return ventoy_is_file_exist("%s", path);
264 }
265 }
266 }
267
268 static int ventoy_path_list_cmp(path_node *list1, path_node *list2)
269 {
270 if (NULL == list1 && NULL == list2)
271 {
272 return 0;
273 }
274 else if (list1 && list2)
275 {
276 while (list1 && list2)
277 {
278 if (strcmp(list1->path, list2->path))
279 {
280 return 1;
281 }
282
283 list1 = list1->next;
284 list2 = list2->next;
285 }
286
287 if (list1 == NULL && list2 == NULL)
288 {
289 return 0;
290 }
291 return 1;
292 }
293 else
294 {
295 return 1;
296 }
297 }
298
299
300 static int ventoy_api_device_info(struct mg_connection *conn, VTOY_JSON *json)
301 {
302 int pos = 0;
303
304 (void)json;
305
306 VTOY_JSON_FMT_BEGIN(pos, JSON_BUFFER, JSON_BUF_MAX);
307 VTOY_JSON_FMT_OBJ_BEGIN();
308
309 VTOY_JSON_FMT_STRN("dev_name", g_sysinfo.cur_model);
310 VTOY_JSON_FMT_STRN("dev_capacity", g_sysinfo.cur_capacity);
311 VTOY_JSON_FMT_STRN("dev_fs", g_sysinfo.cur_fsname);
312 VTOY_JSON_FMT_STRN("ventoy_ver", g_sysinfo.cur_ventoy_ver);
313 VTOY_JSON_FMT_SINT("part_style", g_sysinfo.cur_part_style);
314 VTOY_JSON_FMT_SINT("secure_boot", g_sysinfo.cur_secureboot);
315
316 VTOY_JSON_FMT_OBJ_END();
317 VTOY_JSON_FMT_END(pos);
318
319 ventoy_json_buffer(conn, JSON_BUFFER, pos);
320 return 0;
321 }
322
323 static int ventoy_api_sysinfo(struct mg_connection *conn, VTOY_JSON *json)
324 {
325 int pos = 0;
326
327 (void)json;
328
329 VTOY_JSON_FMT_BEGIN(pos, JSON_BUFFER, JSON_BUF_MAX);
330 VTOY_JSON_FMT_OBJ_BEGIN();
331 VTOY_JSON_FMT_STRN("language", ventoy_get_os_language());
332 VTOY_JSON_FMT_STRN("curdir", g_cur_dir);
333
334 //read clear
335 VTOY_JSON_FMT_SINT("syntax_error", g_sysinfo.syntax_error);
336 g_sysinfo.syntax_error = 0;
337
338 VTOY_JSON_FMT_SINT("invalid_config", g_sysinfo.invalid_config);
339 g_sysinfo.invalid_config = 0;
340
341 #if defined(_MSC_VER) || defined(WIN32)
342 VTOY_JSON_FMT_STRN("os", "windows");
343 #else
344 VTOY_JSON_FMT_STRN("os", "linux");
345 #endif
346
347 VTOY_JSON_FMT_OBJ_END();
348 VTOY_JSON_FMT_END(pos);
349
350 ventoy_json_buffer(conn, JSON_BUFFER, pos);
351 return 0;
352 }
353
354 static int ventoy_api_handshake(struct mg_connection *conn, VTOY_JSON *json)
355 {
356 int i = 0;
357 int j = 0;
358 int pos = 0;
359 char key[128];
360
361 (void)json;
362
363 VTOY_JSON_FMT_BEGIN(pos, JSON_BUFFER, JSON_BUF_MAX);
364 VTOY_JSON_FMT_OBJ_BEGIN();
365 VTOY_JSON_FMT_SINT("status", 0);
366 VTOY_JSON_FMT_SINT("save_error", g_sysinfo.config_save_error);
367 g_sysinfo.config_save_error = 0;
368
369 for (i = 0; i < plugin_type_max; i++)
370 {
371 scnprintf(key, sizeof(key), "exist_%s", g_plugin_name[i]);
372 VTOY_JSON_FMT_KEY(key);
373 VTOY_JSON_FMT_ARY_BEGIN();
374 for (j = 0; j < bios_max; j++)
375 {
376 VTOY_JSON_FMT_ITEM_INT(g_json_exist[i][j]);
377 }
378 VTOY_JSON_FMT_ARY_ENDEX();
379 }
380
381 VTOY_JSON_FMT_OBJ_END();
382 VTOY_JSON_FMT_END(pos);
383
384 ventoy_json_buffer(conn, JSON_BUFFER, pos);
385 return 0;
386 }
387
388 static int ventoy_api_check_exist(struct mg_connection *conn, VTOY_JSON *json)
389 {
390 int dir = 0;
391 int pos = 0;
392 int exist = 0;
393 const char *path = NULL;
394
395 path = vtoy_json_get_string_ex(json, "path");
396 vtoy_json_get_int(json, "dir", &dir);
397
398 if (path)
399 {
400 if (dir)
401 {
402 exist = ventoy_is_directory_exist("%s", path);
403 }
404 else
405 {
406 exist = ventoy_is_file_exist("%s", path);
407 }
408 }
409
410 VTOY_JSON_FMT_BEGIN(pos, JSON_BUFFER, JSON_BUF_MAX);
411 VTOY_JSON_FMT_OBJ_BEGIN();
412 VTOY_JSON_FMT_SINT("exist", exist);
413 VTOY_JSON_FMT_OBJ_END();
414 VTOY_JSON_FMT_END(pos);
415
416 ventoy_json_buffer(conn, JSON_BUFFER, pos);
417 return 0;
418 }
419
420 static int ventoy_api_check_exist2(struct mg_connection *conn, VTOY_JSON *json)
421 {
422 int dir1 = 0;
423 int dir2 = 0;
424 int fuzzy1 = 0;
425 int fuzzy2 = 0;
426 int pos = 0;
427 int exist1 = 0;
428 int exist2 = 0;
429 const char *path1 = NULL;
430 const char *path2 = NULL;
431
432 path1 = vtoy_json_get_string_ex(json, "path1");
433 path2 = vtoy_json_get_string_ex(json, "path2");
434 vtoy_json_get_int(json, "dir1", &dir1);
435 vtoy_json_get_int(json, "dir2", &dir2);
436 vtoy_json_get_int(json, "fuzzy1", &fuzzy1);
437 vtoy_json_get_int(json, "fuzzy2", &fuzzy2);
438
439 if (path1)
440 {
441 if (dir1)
442 {
443 exist1 = ventoy_is_directory_exist("%s", path1);
444 }
445 else
446 {
447 if (fuzzy1)
448 {
449 exist1 = ventoy_check_fuzzy_path((char *)path1, 0);
450 }
451 else
452 {
453 exist1 = ventoy_is_file_exist("%s", path1);
454 }
455 }
456 }
457
458 if (path2)
459 {
460 if (dir2)
461 {
462 exist2 = ventoy_is_directory_exist("%s", path2);
463 }
464 else
465 {
466 if (fuzzy2)
467 {
468 exist2 = ventoy_check_fuzzy_path((char *)path2, 0);
469 }
470 else
471 {
472 exist2 = ventoy_is_file_exist("%s", path2);
473 }
474 }
475 }
476
477 VTOY_JSON_FMT_BEGIN(pos, JSON_BUFFER, JSON_BUF_MAX);
478 VTOY_JSON_FMT_OBJ_BEGIN();
479 VTOY_JSON_FMT_SINT("exist1", exist1);
480 VTOY_JSON_FMT_SINT("exist2", exist2);
481 VTOY_JSON_FMT_OBJ_END();
482 VTOY_JSON_FMT_END(pos);
483
484 ventoy_json_buffer(conn, JSON_BUFFER, pos);
485 return 0;
486 }
487
488 static int ventoy_api_check_fuzzy(struct mg_connection *conn, VTOY_JSON *json)
489 {
490 int pos = 0;
491 int exist = 0;
492 const char *path = NULL;
493
494 path = vtoy_json_get_string_ex(json, "path");
495 if (path)
496 {
497 exist = ventoy_check_fuzzy_path((char *)path, 0);
498 }
499
500 VTOY_JSON_FMT_BEGIN(pos, JSON_BUFFER, JSON_BUF_MAX);
501 VTOY_JSON_FMT_OBJ_BEGIN();
502 VTOY_JSON_FMT_SINT("exist", exist);
503 VTOY_JSON_FMT_OBJ_END();
504 VTOY_JSON_FMT_END(pos);
505
506 ventoy_json_buffer(conn, JSON_BUFFER, pos);
507 return 0;
508 }
509
510
511 #if 0
512 #endif
513 void ventoy_data_default_control(data_control *data)
514 {
515 memset(data, 0, sizeof(data_control));
516
517 data->password_asterisk = 1;
518 data->secondary_menu = 1;
519 data->filter_dot_underscore = 1;
520 data->max_search_level = -1;
521 data->menu_timeout = 0;
522 data->secondary_menu_timeout = 0;
523
524 strlcpy(data->default_kbd_layout, "QWERTY_USA");
525 strlcpy(data->help_text_language, "en_US");
526 strlcpy(data->menu_language, "en_US");
527 }
528
529 int ventoy_data_cmp_control(data_control *data1, data_control *data2)
530 {
531 if (data1->default_menu_mode != data2->default_menu_mode ||
532 data1->treeview_style != data2->treeview_style ||
533 data1->filter_dot_underscore != data2->filter_dot_underscore ||
534 data1->sort_casesensitive != data2->sort_casesensitive ||
535 data1->max_search_level != data2->max_search_level ||
536 data1->vhd_no_warning != data2->vhd_no_warning ||
537 data1->filter_iso != data2->filter_iso ||
538 data1->filter_wim != data2->filter_wim ||
539 data1->filter_efi != data2->filter_efi ||
540 data1->filter_img != data2->filter_img ||
541 data1->filter_vhd != data2->filter_vhd ||
542 data1->filter_vtoy != data2->filter_vtoy ||
543 data1->win11_bypass_check != data2->win11_bypass_check ||
544 data1->linux_remount != data2->linux_remount ||
545 data1->password_asterisk != data2->password_asterisk ||
546 data1->secondary_menu != data2->secondary_menu ||
547 data1->menu_timeout != data2->menu_timeout ||
548 data1->secondary_menu_timeout != data2->secondary_menu_timeout)
549 {
550 return 1;
551 }
552
553 if (strcmp(data1->default_search_root, data2->default_search_root) ||
554 strcmp(data1->default_image, data2->default_image) ||
555 strcmp(data1->default_kbd_layout, data2->default_kbd_layout) ||
556 strcmp(data1->help_text_language, data2->help_text_language) ||
557 strcmp(data1->menu_language, data2->menu_language))
558 {
559 return 1;
560 }
561
562 return 0;
563 }
564
565 int ventoy_data_save_control(data_control *data, const char *title, char *buf, int buflen)
566 {
567 int pos = 0;
568 data_control *def = g_data_control + bios_max;
569
570 VTOY_JSON_FMT_BEGIN(pos, buf, buflen);
571
572 VTOY_JSON_FMT_KEY_L(L1, title);
573 VTOY_JSON_FMT_ARY_BEGIN_N();
574
575 VTOY_JSON_FMT_CTRL_INT(L2, "VTOY_DEFAULT_MENU_MODE", default_menu_mode);
576 VTOY_JSON_FMT_CTRL_INT(L2, "VTOY_TREE_VIEW_MENU_STYLE", treeview_style);
577 VTOY_JSON_FMT_CTRL_INT(L2, "VTOY_FILT_DOT_UNDERSCORE_FILE", filter_dot_underscore);
578 VTOY_JSON_FMT_CTRL_INT(L2, "VTOY_SORT_CASE_SENSITIVE", sort_casesensitive);
579
580 if (data->max_search_level >= 0)
581 {
582 VTOY_JSON_FMT_CTRL_INT(L2, "VTOY_MAX_SEARCH_LEVEL", max_search_level);
583 }
584
585 VTOY_JSON_FMT_CTRL_INT(L2, "VTOY_VHD_NO_WARNING", vhd_no_warning);
586 VTOY_JSON_FMT_CTRL_INT(L2, "VTOY_FILE_FLT_ISO", filter_iso);
587 VTOY_JSON_FMT_CTRL_INT(L2, "VTOY_FILE_FLT_WIM", filter_wim);
588 VTOY_JSON_FMT_CTRL_INT(L2, "VTOY_FILE_FLT_EFI", filter_efi);
589 VTOY_JSON_FMT_CTRL_INT(L2, "VTOY_FILE_FLT_IMG", filter_img);
590 VTOY_JSON_FMT_CTRL_INT(L2, "VTOY_FILE_FLT_VHD", filter_vhd);
591 VTOY_JSON_FMT_CTRL_INT(L2, "VTOY_FILE_FLT_VTOY", filter_vtoy);
592 VTOY_JSON_FMT_CTRL_INT(L2, "VTOY_WIN11_BYPASS_CHECK", win11_bypass_check);
593 VTOY_JSON_FMT_CTRL_INT(L2, "VTOY_LINUX_REMOUNT", linux_remount);
594 VTOY_JSON_FMT_CTRL_INT(L2, "VTOY_SECONDARY_BOOT_MENU", secondary_menu);
595 VTOY_JSON_FMT_CTRL_INT(L2, "VTOY_SHOW_PASSWORD_ASTERISK", password_asterisk);
596 VTOY_JSON_FMT_CTRL_INT(L2, "VTOY_MENU_TIMEOUT", menu_timeout);
597 VTOY_JSON_FMT_CTRL_INT(L2, "VTOY_SECONDARY_TIMEOUT", secondary_menu_timeout);
598
599 VTOY_JSON_FMT_CTRL_STRN(L2, "VTOY_DEFAULT_KBD_LAYOUT", default_kbd_layout);
600 VTOY_JSON_FMT_CTRL_STRN(L2, "VTOY_HELP_TXT_LANGUAGE", help_text_language);
601 VTOY_JSON_FMT_CTRL_STRN(L2, "VTOY_MENU_LANGUAGE", menu_language);
602
603 if (strcmp(def->default_search_root, data->default_search_root))
604 {
605 VTOY_JSON_FMT_CTRL_STRN_STR(L2, "VTOY_DEFAULT_SEARCH_ROOT", ventoy_real_path(data->default_search_root));
606 }
607
608 if (strcmp(def->default_image, data->default_image))
609 {
610 VTOY_JSON_FMT_CTRL_STRN_STR(L2, "VTOY_DEFAULT_IMAGE", ventoy_real_path(data->default_image));
611 }
612
613 VTOY_JSON_FMT_ARY_ENDEX_LN(L1);
614 VTOY_JSON_FMT_END(pos);
615
616 return pos;
617 }
618
619 int ventoy_data_json_control(data_control *ctrl, char *buf, int buflen)
620 {
621 int i = 0;
622 int pos = 0;
623 int valid = 0;
624
625 VTOY_JSON_FMT_BEGIN(pos, buf, buflen);
626 VTOY_JSON_FMT_OBJ_BEGIN();
627
628 VTOY_JSON_FMT_SINT("default_menu_mode", ctrl->default_menu_mode);
629 VTOY_JSON_FMT_SINT("treeview_style", ctrl->treeview_style);
630 VTOY_JSON_FMT_SINT("filter_dot_underscore", ctrl->filter_dot_underscore);
631 VTOY_JSON_FMT_SINT("sort_casesensitive", ctrl->sort_casesensitive);
632 VTOY_JSON_FMT_SINT("max_search_level", ctrl->max_search_level);
633 VTOY_JSON_FMT_SINT("vhd_no_warning", ctrl->vhd_no_warning);
634
635 VTOY_JSON_FMT_SINT("filter_iso", ctrl->filter_iso);
636 VTOY_JSON_FMT_SINT("filter_wim", ctrl->filter_wim);
637 VTOY_JSON_FMT_SINT("filter_efi", ctrl->filter_efi);
638 VTOY_JSON_FMT_SINT("filter_img", ctrl->filter_img);
639 VTOY_JSON_FMT_SINT("filter_vhd", ctrl->filter_vhd);
640 VTOY_JSON_FMT_SINT("filter_vtoy", ctrl->filter_vtoy);
641 VTOY_JSON_FMT_SINT("win11_bypass_check", ctrl->win11_bypass_check);
642 VTOY_JSON_FMT_SINT("linux_remount", ctrl->linux_remount);
643 VTOY_JSON_FMT_SINT("secondary_menu", ctrl->secondary_menu);
644 VTOY_JSON_FMT_SINT("password_asterisk", ctrl->password_asterisk);
645 VTOY_JSON_FMT_SINT("menu_timeout", ctrl->menu_timeout);
646 VTOY_JSON_FMT_SINT("secondary_menu_timeout", ctrl->secondary_menu_timeout);
647 VTOY_JSON_FMT_STRN("default_kbd_layout", ctrl->default_kbd_layout);
648 VTOY_JSON_FMT_STRN("help_text_language", ctrl->help_text_language);
649 VTOY_JSON_FMT_STRN("menu_language", ctrl->menu_language);
650
651 valid = 0;
652 if (ctrl->default_search_root[0] && ventoy_is_directory_exist("%s%s", g_cur_dir, ctrl->default_search_root))
653 {
654 valid = 1;
655 }
656 VTOY_JSON_FMT_STRN("default_search_root", ctrl->default_search_root);
657 VTOY_JSON_FMT_SINT("default_search_root_valid", valid);
658
659
660 valid = 0;
661 if (ctrl->default_image[0] && ventoy_is_file_exist("%s%s", g_cur_dir, ctrl->default_image))
662 {
663 valid = 1;
664 }
665 VTOY_JSON_FMT_STRN("default_image", ctrl->default_image);
666 VTOY_JSON_FMT_SINT("default_image_valid", valid);
667
668 VTOY_JSON_FMT_KEY("help_list");
669 VTOY_JSON_FMT_ARY_BEGIN();
670
671 for (i = 0; g_ventoy_help_lang[i][0]; i++)
672 {
673 VTOY_JSON_FMT_ITEM(g_ventoy_help_lang[i]);
674 }
675 VTOY_JSON_FMT_ARY_ENDEX();
676
677 VTOY_JSON_FMT_KEY("menu_list");
678 VTOY_JSON_FMT_ARY_BEGIN();
679
680 for (i = 0; g_ventoy_menu_lang[i][0]; i++)
681 {
682 VTOY_JSON_FMT_ITEM(g_ventoy_menu_lang[i]);
683 }
684 VTOY_JSON_FMT_ARY_ENDEX();
685
686 VTOY_JSON_FMT_OBJ_END();
687 VTOY_JSON_FMT_END(pos);
688
689 return pos;
690 }
691
692 static int ventoy_api_get_control(struct mg_connection *conn, VTOY_JSON *json)
693 {
694 api_get_func(conn, json, control);
695 return 0;
696 }
697
698 static int ventoy_api_save_control(struct mg_connection *conn, VTOY_JSON *json)
699 {
700 int ret;
701 int index = 0;
702 data_control *ctrl = NULL;
703
704 vtoy_json_get_int(json, "index", &index);
705 ctrl = g_data_control + index;
706
707 VTOY_JSON_INT("default_menu_mode", ctrl->default_menu_mode);
708 VTOY_JSON_INT("treeview_style", ctrl->treeview_style);
709 VTOY_JSON_INT("filter_dot_underscore", ctrl->filter_dot_underscore);
710 VTOY_JSON_INT("sort_casesensitive", ctrl->sort_casesensitive);
711 VTOY_JSON_INT("max_search_level", ctrl->max_search_level);
712 VTOY_JSON_INT("vhd_no_warning", ctrl->vhd_no_warning);
713 VTOY_JSON_INT("filter_iso", ctrl->filter_iso);
714 VTOY_JSON_INT("filter_wim", ctrl->filter_wim);
715 VTOY_JSON_INT("filter_efi", ctrl->filter_efi);
716 VTOY_JSON_INT("filter_img", ctrl->filter_img);
717 VTOY_JSON_INT("filter_vhd", ctrl->filter_vhd);
718 VTOY_JSON_INT("filter_vtoy", ctrl->filter_vtoy);
719 VTOY_JSON_INT("win11_bypass_check", ctrl->win11_bypass_check);
720 VTOY_JSON_INT("linux_remount", ctrl->linux_remount);
721 VTOY_JSON_INT("secondary_menu", ctrl->secondary_menu);
722 VTOY_JSON_INT("password_asterisk", ctrl->password_asterisk);
723 VTOY_JSON_INT("menu_timeout", ctrl->menu_timeout);
724 VTOY_JSON_INT("secondary_menu_timeout", ctrl->secondary_menu_timeout);
725
726 VTOY_JSON_STR("default_image", ctrl->default_image);
727 VTOY_JSON_STR("default_search_root", ctrl->default_search_root);
728 VTOY_JSON_STR("help_text_language", ctrl->help_text_language);
729 VTOY_JSON_STR("menu_language", ctrl->menu_language);
730 VTOY_JSON_STR("default_kbd_layout", ctrl->default_kbd_layout);
731
732 ret = ventoy_data_save_all();
733
734 ventoy_json_result(conn, ret == 0 ? VTOY_JSON_SUCCESS_RET : VTOY_JSON_FAILED_RET);
735 return 0;
736 }
737
738
739 #if 0
740 #endif
741
742 void ventoy_data_default_theme(data_theme *data)
743 {
744 memset(data, 0, sizeof(data_theme));
745 strlcpy(data->gfxmode, "1024x768");
746 scnprintf(data->ventoy_left, sizeof(data->ventoy_left), "5%%");
747 scnprintf(data->ventoy_top, sizeof(data->ventoy_top), "95%%");
748 scnprintf(data->ventoy_color, sizeof(data->ventoy_color), "%s", "#0000ff");
749 }
750
751 int ventoy_data_cmp_theme(data_theme *data1, data_theme *data2)
752 {
753 if (data1->display_mode != data2->display_mode ||
754 strcmp(data1->ventoy_left, data2->ventoy_left) ||
755 strcmp(data1->ventoy_top, data2->ventoy_top) ||
756 strcmp(data1->gfxmode, data2->gfxmode) ||
757 strcmp(data1->ventoy_color, data2->ventoy_color)
758 )
759 {
760 return 1;
761 }
762
763 if (ventoy_path_list_cmp(data1->filelist, data2->filelist))
764 {
765 return 1;
766 }
767
768 if (ventoy_path_list_cmp(data1->fontslist, data2->fontslist))
769 {
770 return 1;
771 }
772
773 return 0;
774 }
775
776
777 int ventoy_data_save_theme(data_theme *data, const char *title, char *buf, int buflen)
778 {
779 int pos = 0;
780 path_node *node = NULL;
781 data_theme *def = g_data_theme + bios_max;
782
783 VTOY_JSON_FMT_BEGIN(pos, buf, buflen);
784
785 VTOY_JSON_FMT_KEY_L(L1, title);
786 VTOY_JSON_FMT_OBJ_BEGIN_N();
787
788 if (data->filelist)
789 {
790 if (data->filelist->next)
791 {
792 VTOY_JSON_FMT_KEY_L(L2, "file");
793 VTOY_JSON_FMT_ARY_BEGIN_N();
794
795 for (node = data->filelist; node; node = node->next)
796 {
797 VTOY_JSON_FMT_ITEM_PATH_LN(L3, node->path);
798 }
799
800 VTOY_JSON_FMT_ARY_ENDEX_LN(L2);
801
802 if (def->default_file != data->default_file)
803 {
804 VTOY_JSON_FMT_SINT_LN(L2, "default_file", data->default_file);
805 }
806 }
807 else
808 {
809 VTOY_JSON_FMT_STRN_PATH_LN(L2, "file", data->filelist->path);
810 }
811 }
812
813 if (data->display_mode != def->display_mode)
814 {
815 if (display_mode_cli == data->display_mode)
816 {
817 VTOY_JSON_FMT_STRN_LN(L2, "display_mode", "CLI");
818 }
819 else if (display_mode_serial == data->display_mode)
820 {
821 VTOY_JSON_FMT_STRN_LN(L2, "display_mode", "serial");
822 }
823 else if (display_mode_ser_console == data->display_mode)
824 {
825 VTOY_JSON_FMT_STRN_LN(L2, "display_mode", "serial_console");
826 }
827 else
828 {
829 VTOY_JSON_FMT_STRN_LN(L2, "display_mode", "GUI");
830 }
831 }
832
833 VTOY_JSON_FMT_DIFF_STRN(L2, "gfxmode", gfxmode);
834
835 VTOY_JSON_FMT_DIFF_STRN(L2, "ventoy_left", ventoy_left);
836 VTOY_JSON_FMT_DIFF_STRN(L2, "ventoy_top", ventoy_top);
837 VTOY_JSON_FMT_DIFF_STRN(L2, "ventoy_color", ventoy_color);
838
839 if (data->fontslist)
840 {
841 VTOY_JSON_FMT_KEY_L(L2, "fonts");
842 VTOY_JSON_FMT_ARY_BEGIN_N();
843
844 for (node = data->fontslist; node; node = node->next)
845 {
846 VTOY_JSON_FMT_ITEM_PATH_LN(L3, node->path);
847 }
848
849 VTOY_JSON_FMT_ARY_ENDEX_LN(L2);
850 }
851
852 VTOY_JSON_FMT_OBJ_ENDEX_LN(L1);
853 VTOY_JSON_FMT_END(pos);
854
855 return pos;
856 }
857
858
859 int ventoy_data_json_theme(data_theme *data, char *buf, int buflen)
860 {
861 int pos = 0;
862 path_node *node = NULL;
863
864 VTOY_JSON_FMT_BEGIN(pos, buf, buflen);
865 VTOY_JSON_FMT_OBJ_BEGIN();
866
867 VTOY_JSON_FMT_SINT("default_file", data->default_file);
868 VTOY_JSON_FMT_SINT("display_mode", data->display_mode);
869 VTOY_JSON_FMT_STRN("gfxmode", data->gfxmode);
870
871 VTOY_JSON_FMT_STRN("ventoy_color", data->ventoy_color);
872 VTOY_JSON_FMT_STRN("ventoy_left", data->ventoy_left);
873 VTOY_JSON_FMT_STRN("ventoy_top", data->ventoy_top);
874
875 VTOY_JSON_FMT_KEY("filelist");
876 VTOY_JSON_FMT_ARY_BEGIN();
877 for (node = data->filelist; node; node = node->next)
878 {
879 VTOY_JSON_FMT_OBJ_BEGIN();
880 VTOY_JSON_FMT_STRN("path", node->path);
881 VTOY_JSON_FMT_SINT("valid", ventoy_is_file_exist("%s%s", g_cur_dir, node->path));
882 VTOY_JSON_FMT_OBJ_ENDEX();
883 }
884 VTOY_JSON_FMT_ARY_ENDEX();
885
886 VTOY_JSON_FMT_KEY("fontslist");
887 VTOY_JSON_FMT_ARY_BEGIN();
888 for (node = data->fontslist; node; node = node->next)
889 {
890 VTOY_JSON_FMT_OBJ_BEGIN();
891 VTOY_JSON_FMT_STRN("path", node->path);
892 VTOY_JSON_FMT_SINT("valid", ventoy_is_file_exist("%s%s", g_cur_dir, node->path));
893 VTOY_JSON_FMT_OBJ_ENDEX();
894 }
895 VTOY_JSON_FMT_ARY_ENDEX();
896
897 VTOY_JSON_FMT_OBJ_END();
898 VTOY_JSON_FMT_END(pos);
899
900 return pos;
901 }
902
903 static int ventoy_api_get_theme(struct mg_connection *conn, VTOY_JSON *json)
904 {
905 api_get_func(conn, json, theme);
906 return 0;
907 }
908
909 static int ventoy_api_save_theme(struct mg_connection *conn, VTOY_JSON *json)
910 {
911 int ret;
912 int index = 0;
913 data_theme *data = NULL;
914
915 vtoy_json_get_int(json, "index", &index);
916 data = g_data_theme + index;
917
918 VTOY_JSON_INT("default_file", data->default_file);
919 VTOY_JSON_INT("display_mode", data->display_mode);
920 VTOY_JSON_STR("gfxmode", data->gfxmode);
921 VTOY_JSON_STR("ventoy_left", data->ventoy_left);
922 VTOY_JSON_STR("ventoy_top", data->ventoy_top);
923 VTOY_JSON_STR("ventoy_color", data->ventoy_color);
924
925 ret = ventoy_data_save_all();
926
927 ventoy_json_result(conn, ret == 0 ? VTOY_JSON_SUCCESS_RET : VTOY_JSON_FAILED_RET);
928 return 0;
929 }
930
931 static int ventoy_api_theme_add_file(struct mg_connection *conn, VTOY_JSON *json)
932 {
933 int ret;
934 int index = 0;
935 const char *path = NULL;
936 path_node *node = NULL;
937 path_node *cur = NULL;
938 data_theme *data = NULL;
939
940 vtoy_json_get_int(json, "index", &index);
941 data = g_data_theme + index;
942
943 path = VTOY_JSON_STR_EX("path");
944 if (path)
945 {
946 node = zalloc(sizeof(path_node));
947 if (node)
948 {
949 scnprintf(node->path, sizeof(node->path), "%s", path);
950
951 vtoy_list_add(data->filelist, cur, node);
952 }
953 }
954
955 ret = ventoy_data_save_all();
956
957 ventoy_json_result(conn, ret == 0 ? VTOY_JSON_SUCCESS_RET : VTOY_JSON_FAILED_RET);
958 return 0;
959 }
960
961 static int ventoy_api_theme_del_file(struct mg_connection *conn, VTOY_JSON *json)
962 {
963 int ret;
964 int index = 0;
965 const char *path = NULL;
966 path_node *node = NULL;
967 path_node *last = NULL;
968 data_theme *data = NULL;
969
970 vtoy_json_get_int(json, "index", &index);
971 data = g_data_theme + index;
972
973 path = VTOY_JSON_STR_EX("path");
974 if (path)
975 {
976 if (strcmp(path, VTOY_DEL_ALL_PATH) == 0)
977 {
978 vtoy_list_free(path_node, data->filelist);
979 }
980 else
981 {
982 vtoy_list_del(last, node, data->filelist, path);
983 }
984 }
985
986 ret = ventoy_data_save_all();
987
988 ventoy_json_result(conn, ret == 0 ? VTOY_JSON_SUCCESS_RET : VTOY_JSON_FAILED_RET);
989 return 0;
990 }
991
992
993 static int ventoy_api_theme_add_font(struct mg_connection *conn, VTOY_JSON *json)
994 {
995 int ret;
996 int index = 0;
997 const char *path = NULL;
998 path_node *node = NULL;
999 path_node *cur = NULL;
1000 data_theme *data = NULL;
1001
1002 vtoy_json_get_int(json, "index", &index);
1003 data = g_data_theme + index;
1004
1005 path = VTOY_JSON_STR_EX("path");
1006 if (path)
1007 {
1008 node = zalloc(sizeof(path_node));
1009 if (node)
1010 {
1011 scnprintf(node->path, sizeof(node->path), "%s", path);
1012 vtoy_list_add(data->fontslist, cur, node);
1013 }
1014 }
1015
1016 ret = ventoy_data_save_all();
1017
1018 ventoy_json_result(conn, ret == 0 ? VTOY_JSON_SUCCESS_RET : VTOY_JSON_FAILED_RET);
1019 return 0;
1020 }
1021
1022
1023 static int ventoy_api_theme_del_font(struct mg_connection *conn, VTOY_JSON *json)
1024 {
1025 int ret;
1026 int index = 0;
1027 const char *path = NULL;
1028 path_node *node = NULL;
1029 path_node *last = NULL;
1030 data_theme *data = NULL;
1031
1032 vtoy_json_get_int(json, "index", &index);
1033 data = g_data_theme + index;
1034
1035 path = VTOY_JSON_STR_EX("path");
1036 if (path)
1037 {
1038 if (strcmp(path, VTOY_DEL_ALL_PATH) == 0)
1039 {
1040 vtoy_list_free(path_node, data->fontslist);
1041 }
1042 else
1043 {
1044 vtoy_list_del(last, node, data->fontslist, path);
1045 }
1046 }
1047
1048 ret = ventoy_data_save_all();
1049
1050 ventoy_json_result(conn, ret == 0 ? VTOY_JSON_SUCCESS_RET : VTOY_JSON_FAILED_RET);
1051 return 0;
1052 }
1053
1054 #if 0
1055 #endif
1056
1057 void ventoy_data_default_menu_alias(data_alias *data)
1058 {
1059 memset(data, 0, sizeof(data_alias));
1060 }
1061
1062 int ventoy_data_cmp_menu_alias(data_alias *data1, data_alias *data2)
1063 {
1064 data_alias_node *list1 = NULL;
1065 data_alias_node *list2 = NULL;
1066
1067 if (NULL == data1->list && NULL == data2->list)
1068 {
1069 return 0;
1070 }
1071 else if (data1->list && data2->list)
1072 {
1073 list1 = data1->list;
1074 list2 = data2->list;
1075
1076 while (list1 && list2)
1077 {
1078 if ((list1->type != list2->type) ||
1079 strcmp(list1->path, list2->path) ||
1080 strcmp(list1->alias, list2->alias))
1081 {
1082 return 1;
1083 }
1084
1085 list1 = list1->next;
1086 list2 = list2->next;
1087 }
1088
1089 if (list1 == NULL && list2 == NULL)
1090 {
1091 return 0;
1092 }
1093 return 1;
1094 }
1095 else
1096 {
1097 return 1;
1098 }
1099
1100 return 0;
1101 }
1102
1103
1104 int ventoy_data_save_menu_alias(data_alias *data, const char *title, char *buf, int buflen)
1105 {
1106 int pos = 0;
1107 data_alias_node *node = NULL;
1108
1109 VTOY_JSON_FMT_BEGIN(pos, buf, buflen);
1110
1111 VTOY_JSON_FMT_KEY_L(L1, title);
1112 VTOY_JSON_FMT_ARY_BEGIN_N();
1113
1114 for (node = data->list; node; node = node->next)
1115 {
1116 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2);
1117
1118 if (node->type == path_type_file)
1119 {
1120 VTOY_JSON_FMT_STRN_PATH_LN(L3, "image", node->path);
1121 }
1122 else
1123 {
1124 VTOY_JSON_FMT_STRN_PATH_LN(L3, "dir", node->path);
1125 }
1126
1127 VTOY_JSON_FMT_STRN_EX_LN(L3, "alias", node->alias);
1128
1129 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2);
1130 }
1131
1132 VTOY_JSON_FMT_ARY_ENDEX_LN(L1);
1133 VTOY_JSON_FMT_END(pos);
1134
1135 return pos;
1136 }
1137
1138
1139 int ventoy_data_json_menu_alias(data_alias *data, char *buf, int buflen)
1140 {
1141 int pos = 0;
1142 int valid = 0;
1143 data_alias_node *node = NULL;
1144
1145 VTOY_JSON_FMT_BEGIN(pos, buf, buflen);
1146 VTOY_JSON_FMT_ARY_BEGIN();
1147
1148 for (node = data->list; node; node = node->next)
1149 {
1150 VTOY_JSON_FMT_OBJ_BEGIN();
1151
1152 VTOY_JSON_FMT_UINT("type", node->type);
1153 VTOY_JSON_FMT_STRN("path", node->path);
1154 if (node->type == path_type_file)
1155 {
1156 valid = ventoy_check_fuzzy_path(node->path, 1);
1157 }
1158 else
1159 {
1160 valid = ventoy_is_directory_exist("%s%s", g_cur_dir, node->path);
1161 }
1162
1163 VTOY_JSON_FMT_SINT("valid", valid);
1164 VTOY_JSON_FMT_STRN("alias", node->alias);
1165
1166 VTOY_JSON_FMT_OBJ_ENDEX();
1167 }
1168
1169 VTOY_JSON_FMT_ARY_END();
1170 VTOY_JSON_FMT_END(pos);
1171
1172 return pos;
1173 }
1174
1175 static int ventoy_api_get_alias(struct mg_connection *conn, VTOY_JSON *json)
1176 {
1177 api_get_func(conn, json, menu_alias);
1178 return 0;
1179 }
1180
1181 static int ventoy_api_save_alias(struct mg_connection *conn, VTOY_JSON *json)
1182 {
1183 int ret;
1184 ret = ventoy_data_save_all();
1185
1186 ventoy_json_result(conn, ret == 0 ? VTOY_JSON_SUCCESS_RET : VTOY_JSON_FAILED_RET);
1187 return 0;
1188 }
1189
1190 static int ventoy_api_alias_add(struct mg_connection *conn, VTOY_JSON *json)
1191 {
1192 int ret;
1193 int index = 0;
1194 int type = path_type_file;
1195 const char *path = NULL;
1196 const char *alias = NULL;
1197 data_alias_node *node = NULL;
1198 data_alias_node *cur = NULL;
1199 data_alias *data = NULL;
1200
1201 vtoy_json_get_int(json, "index", &index);
1202 data = g_data_menu_alias + index;
1203
1204 vtoy_json_get_int(json, "type", &type);
1205
1206 path = VTOY_JSON_STR_EX("path");
1207 alias = VTOY_JSON_STR_EX("alias");
1208 if (path && alias)
1209 {
1210 node = zalloc(sizeof(data_alias_node));
1211 if (node)
1212 {
1213 node->type = type;
1214 scnprintf(node->path, sizeof(node->path), "%s", path);
1215 scnprintf(node->alias, sizeof(node->alias), "%s", alias);
1216
1217 vtoy_list_add(data->list, cur, node);
1218 }
1219 }
1220
1221 ret = ventoy_data_save_all();
1222
1223 ventoy_json_result(conn, ret == 0 ? VTOY_JSON_SUCCESS_RET : VTOY_JSON_FAILED_RET);
1224 return 0;
1225 }
1226
1227 static int ventoy_api_alias_del(struct mg_connection *conn, VTOY_JSON *json)
1228 {
1229 int ret;
1230 int index = 0;
1231 const char *path = NULL;
1232 data_alias_node *last = NULL;
1233 data_alias_node *node = NULL;
1234 data_alias *data = NULL;
1235
1236 vtoy_json_get_int(json, "index", &index);
1237 data = g_data_menu_alias + index;
1238
1239 path = VTOY_JSON_STR_EX("path");
1240 if (path)
1241 {
1242 if (strcmp(path, VTOY_DEL_ALL_PATH) == 0)
1243 {
1244 vtoy_list_free(data_alias_node, data->list);
1245 }
1246 else
1247 {
1248 vtoy_list_del(last, node, data->list, path);
1249 }
1250 }
1251
1252 ret = ventoy_data_save_all();
1253
1254 ventoy_json_result(conn, ret == 0 ? VTOY_JSON_SUCCESS_RET : VTOY_JSON_FAILED_RET);
1255 return 0;
1256 }
1257
1258 #if 0
1259 #endif
1260
1261 void ventoy_data_default_menu_tip(data_tip *data)
1262 {
1263 memset(data, 0, sizeof(data_tip));
1264
1265 scnprintf(data->left, sizeof(data->left), "10%%");
1266 scnprintf(data->top, sizeof(data->top), "81%%");
1267 scnprintf(data->color, sizeof(data->color), "%s", "blue");
1268 }
1269
1270 int ventoy_data_cmp_menu_tip(data_tip *data1, data_tip *data2)
1271 {
1272 data_tip_node *list1 = NULL;
1273 data_tip_node *list2 = NULL;
1274
1275 if (strcmp(data1->left, data2->left) || strcmp(data1->top, data2->top) || strcmp(data1->color, data2->color))
1276 {
1277 return 1;
1278 }
1279
1280 if (NULL == data1->list && NULL == data2->list)
1281 {
1282 return 0;
1283 }
1284 else if (data1->list && data2->list)
1285 {
1286 list1 = data1->list;
1287 list2 = data2->list;
1288
1289 while (list1 && list2)
1290 {
1291 if ((list1->type != list2->type) ||
1292 strcmp(list1->path, list2->path) ||
1293 strcmp(list1->tip, list2->tip))
1294 {
1295 return 1;
1296 }
1297
1298 list1 = list1->next;
1299 list2 = list2->next;
1300 }
1301
1302 if (list1 == NULL && list2 == NULL)
1303 {
1304 return 0;
1305 }
1306 return 1;
1307 }
1308 else
1309 {
1310 return 1;
1311 }
1312
1313 return 0;
1314 }
1315
1316
1317 int ventoy_data_save_menu_tip(data_tip *data, const char *title, char *buf, int buflen)
1318 {
1319 int pos = 0;
1320 data_tip_node *node = NULL;
1321 data_tip *def = g_data_menu_tip + bios_max;
1322
1323 VTOY_JSON_FMT_BEGIN(pos, buf, buflen);
1324 VTOY_JSON_FMT_KEY_L(L1, title);
1325 VTOY_JSON_FMT_OBJ_BEGIN_N();
1326
1327 VTOY_JSON_FMT_DIFF_STRN(L2, "left", left);
1328 VTOY_JSON_FMT_DIFF_STRN(L2, "top", top);
1329 VTOY_JSON_FMT_DIFF_STRN(L2, "color", color);
1330
1331 if (data->list)
1332 {
1333 VTOY_JSON_FMT_KEY_L(L2, "tips");
1334 VTOY_JSON_FMT_ARY_BEGIN_N();
1335
1336 for (node = data->list; node; node = node->next)
1337 {
1338 VTOY_JSON_FMT_OBJ_BEGIN_LN(L3);
1339
1340 if (node->type == path_type_file)
1341 {
1342 VTOY_JSON_FMT_STRN_PATH_LN(L4, "image", node->path);
1343 }
1344 else
1345 {
1346 VTOY_JSON_FMT_STRN_PATH_LN(L4, "dir", node->path);
1347 }
1348 VTOY_JSON_FMT_STRN_EX_LN(L4, "tip", node->tip);
1349
1350 VTOY_JSON_FMT_OBJ_ENDEX_LN(L3);
1351 }
1352
1353 VTOY_JSON_FMT_ARY_ENDEX_LN(L2);
1354 }
1355
1356 VTOY_JSON_FMT_OBJ_ENDEX_LN(L1);
1357
1358 VTOY_JSON_FMT_END(pos);
1359
1360 return pos;
1361 }
1362
1363
1364 int ventoy_data_json_menu_tip(data_tip *data, char *buf, int buflen)
1365 {
1366 int pos = 0;
1367 int valid = 0;
1368 data_tip_node *node = NULL;
1369
1370 VTOY_JSON_FMT_BEGIN(pos, buf, buflen);
1371
1372 VTOY_JSON_FMT_OBJ_BEGIN();
1373
1374 VTOY_JSON_FMT_STRN("left", data->left);
1375 VTOY_JSON_FMT_STRN("top", data->top);
1376 VTOY_JSON_FMT_STRN("color", data->color);
1377
1378 VTOY_JSON_FMT_KEY("tips");
1379 VTOY_JSON_FMT_ARY_BEGIN();
1380
1381 for (node = data->list; node; node = node->next)
1382 {
1383 VTOY_JSON_FMT_OBJ_BEGIN();
1384
1385 VTOY_JSON_FMT_UINT("type", node->type);
1386 VTOY_JSON_FMT_STRN("path", node->path);
1387 if (node->type == path_type_file)
1388 {
1389 valid = ventoy_check_fuzzy_path(node->path, 1);
1390 }
1391 else
1392 {
1393 valid = ventoy_is_directory_exist("%s%s", g_cur_dir, node->path);
1394 }
1395
1396 VTOY_JSON_FMT_SINT("valid", valid);
1397 VTOY_JSON_FMT_STRN("tip", node->tip);
1398
1399 VTOY_JSON_FMT_OBJ_ENDEX();
1400 }
1401
1402 VTOY_JSON_FMT_ARY_ENDEX();
1403
1404 VTOY_JSON_FMT_OBJ_END();
1405 VTOY_JSON_FMT_END(pos);
1406
1407 return pos;
1408 }
1409
1410 static int ventoy_api_get_tip(struct mg_connection *conn, VTOY_JSON *json)
1411 {
1412 api_get_func(conn, json, menu_tip);
1413 return 0;
1414 }
1415
1416 static int ventoy_api_save_tip(struct mg_connection *conn, VTOY_JSON *json)
1417 {
1418 int ret;
1419 int index = 0;
1420 data_tip *data = NULL;
1421
1422 vtoy_json_get_int(json, "index", &index);
1423 data = g_data_menu_tip + index;
1424
1425 VTOY_JSON_STR("left", data->left);
1426 VTOY_JSON_STR("top", data->top);
1427 VTOY_JSON_STR("color", data->color);
1428
1429 ret = ventoy_data_save_all();
1430
1431 ventoy_json_result(conn, ret == 0 ? VTOY_JSON_SUCCESS_RET : VTOY_JSON_FAILED_RET);
1432 return 0;
1433 }
1434
1435 static int ventoy_api_tip_add(struct mg_connection *conn, VTOY_JSON *json)
1436 {
1437 int ret;
1438 int index = 0;
1439 int type = path_type_file;
1440 const char *path = NULL;
1441 const char *tip = NULL;
1442 data_tip_node *node = NULL;
1443 data_tip_node *cur = NULL;
1444 data_tip *data = NULL;
1445
1446 vtoy_json_get_int(json, "index", &index);
1447 data = g_data_menu_tip + index;
1448
1449 vtoy_json_get_int(json, "type", &type);
1450
1451 path = VTOY_JSON_STR_EX("path");
1452 tip = VTOY_JSON_STR_EX("tip");
1453 if (path && tip)
1454 {
1455 node = zalloc(sizeof(data_tip_node));
1456 if (node)
1457 {
1458 node->type = type;
1459 scnprintf(node->path, sizeof(node->path), "%s", path);
1460 scnprintf(node->tip, sizeof(node->tip), "%s", tip);
1461
1462 vtoy_list_add(data->list, cur, node);
1463 }
1464 }
1465
1466 ret = ventoy_data_save_all();
1467
1468 ventoy_json_result(conn, ret == 0 ? VTOY_JSON_SUCCESS_RET : VTOY_JSON_FAILED_RET);
1469 return 0;
1470 }
1471
1472 static int ventoy_api_tip_del(struct mg_connection *conn, VTOY_JSON *json)
1473 {
1474 int ret;
1475 int index = 0;
1476 const char *path = NULL;
1477 data_tip_node *last = NULL;
1478 data_tip_node *node = NULL;
1479 data_tip *data = NULL;
1480
1481 vtoy_json_get_int(json, "index", &index);
1482 data = g_data_menu_tip + index;
1483
1484 path = VTOY_JSON_STR_EX("path");
1485 if (path)
1486 {
1487 if (strcmp(path, VTOY_DEL_ALL_PATH) == 0)
1488 {
1489 vtoy_list_free(data_tip_node, data->list);
1490 }
1491 else
1492 {
1493 vtoy_list_del(last, node, data->list, path);
1494 }
1495 }
1496
1497 ret = ventoy_data_save_all();
1498
1499 ventoy_json_result(conn, ret == 0 ? VTOY_JSON_SUCCESS_RET : VTOY_JSON_FAILED_RET);
1500 return 0;
1501 }
1502
1503 #if 0
1504 #endif
1505
1506 void ventoy_data_default_menu_class(data_class *data)
1507 {
1508 memset(data, 0, sizeof(data_class));
1509 }
1510
1511 int ventoy_data_cmp_menu_class(data_class *data1, data_class *data2)
1512 {
1513 data_class_node *list1 = NULL;
1514 data_class_node *list2 = NULL;
1515
1516 if (NULL == data1->list && NULL == data2->list)
1517 {
1518 return 0;
1519 }
1520 else if (data1->list && data2->list)
1521 {
1522 list1 = data1->list;
1523 list2 = data2->list;
1524
1525 while (list1 && list2)
1526 {
1527 if ((list1->type != list2->type) ||
1528 strcmp(list1->path, list2->path) ||
1529 strcmp(list1->class, list2->class))
1530 {
1531 return 1;
1532 }
1533
1534 list1 = list1->next;
1535 list2 = list2->next;
1536 }
1537
1538 if (list1 == NULL && list2 == NULL)
1539 {
1540 return 0;
1541 }
1542 return 1;
1543 }
1544 else
1545 {
1546 return 1;
1547 }
1548
1549 return 0;
1550 }
1551
1552
1553 int ventoy_data_save_menu_class(data_class *data, const char *title, char *buf, int buflen)
1554 {
1555 int pos = 0;
1556 data_class_node *node = NULL;
1557
1558 VTOY_JSON_FMT_BEGIN(pos, buf, buflen);
1559
1560 VTOY_JSON_FMT_KEY_L(L1, title);
1561 VTOY_JSON_FMT_ARY_BEGIN_N();
1562
1563 for (node = data->list; node; node = node->next)
1564 {
1565 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2);
1566
1567 if (node->type == class_type_key)
1568 {
1569 VTOY_JSON_FMT_STRN_LN(L3, "key", node->path);
1570 }
1571 else if (node->type == class_type_dir)
1572 {
1573 VTOY_JSON_FMT_STRN_PATH_LN(L3, "dir", node->path);
1574 }
1575 else
1576 {
1577 VTOY_JSON_FMT_STRN_PATH_LN(L3, "parent", node->path);
1578 }
1579 VTOY_JSON_FMT_STRN_LN(L3, "class", node->class);
1580
1581 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2);
1582 }
1583
1584 VTOY_JSON_FMT_ARY_ENDEX_LN(L1);
1585 VTOY_JSON_FMT_END(pos);
1586
1587 return pos;
1588 }
1589
1590
1591 int ventoy_data_json_menu_class(data_class *data, char *buf, int buflen)
1592 {
1593 int pos = 0;
1594 int valid = 0;
1595 data_class_node *node = NULL;
1596
1597 VTOY_JSON_FMT_BEGIN(pos, buf, buflen);
1598 VTOY_JSON_FMT_ARY_BEGIN();
1599
1600 for (node = data->list; node; node = node->next)
1601 {
1602 VTOY_JSON_FMT_OBJ_BEGIN();
1603
1604 VTOY_JSON_FMT_UINT("type", node->type);
1605 VTOY_JSON_FMT_STRN("path", node->path);
1606
1607 if (node->type == class_type_key)
1608 {
1609 valid = 1;
1610 }
1611 else
1612 {
1613 valid = ventoy_is_directory_exist("%s%s", g_cur_dir, node->path);
1614 }
1615 VTOY_JSON_FMT_SINT("valid", valid);
1616
1617 VTOY_JSON_FMT_STRN("class", node->class);
1618
1619 VTOY_JSON_FMT_OBJ_ENDEX();
1620 }
1621
1622 VTOY_JSON_FMT_ARY_END();
1623 VTOY_JSON_FMT_END(pos);
1624
1625 return pos;
1626 }
1627
1628
1629 static int ventoy_api_get_class(struct mg_connection *conn, VTOY_JSON *json)
1630 {
1631 api_get_func(conn, json, menu_class);
1632 return 0;
1633 }
1634
1635 static int ventoy_api_save_class(struct mg_connection *conn, VTOY_JSON *json)
1636 {
1637 int ret;
1638 ret = ventoy_data_save_all();
1639
1640 ventoy_json_result(conn, ret == 0 ? VTOY_JSON_SUCCESS_RET : VTOY_JSON_FAILED_RET);
1641 return 0;
1642 }
1643
1644 static int ventoy_api_class_add(struct mg_connection *conn, VTOY_JSON *json)
1645 {
1646 int ret;
1647 int index = 0;
1648 int type = class_type_key;
1649 const char *path = NULL;
1650 const char *class = NULL;
1651 data_class_node *node = NULL;
1652 data_class_node *cur = NULL;
1653 data_class *data = NULL;
1654
1655 vtoy_json_get_int(json, "index", &index);
1656 data = g_data_menu_class + index;
1657
1658 vtoy_json_get_int(json, "type", &type);
1659
1660 path = VTOY_JSON_STR_EX("path");
1661 class = VTOY_JSON_STR_EX("class");
1662 if (path && class)
1663 {
1664 node = zalloc(sizeof(data_class_node));
1665 if (node)
1666 {
1667 node->type = type;
1668
1669 scnprintf(node->path, sizeof(node->path), "%s", path);
1670 scnprintf(node->class, sizeof(node->class), "%s", class);
1671
1672 vtoy_list_add(data->list, cur, node);
1673 }
1674 }
1675
1676 ret = ventoy_data_save_all();
1677
1678 ventoy_json_result(conn, ret == 0 ? VTOY_JSON_SUCCESS_RET : VTOY_JSON_FAILED_RET);
1679 return 0;
1680 }
1681
1682 static int ventoy_api_class_del(struct mg_connection *conn, VTOY_JSON *json)
1683 {
1684 int ret;
1685 int index = 0;
1686 const char *path = NULL;
1687 data_class_node *last = NULL;
1688 data_class_node *node = NULL;
1689 data_class *data = NULL;
1690
1691 vtoy_json_get_int(json, "index", &index);
1692 data = g_data_menu_class + index;
1693
1694 path = VTOY_JSON_STR_EX("path");
1695 if (path)
1696 {
1697 if (strcmp(path, VTOY_DEL_ALL_PATH) == 0)
1698 {
1699 vtoy_list_free(data_class_node, data->list);
1700 }
1701 else
1702 {
1703 vtoy_list_del(last, node, data->list, path);
1704 }
1705 }
1706
1707 ret = ventoy_data_save_all();
1708
1709 ventoy_json_result(conn, ret == 0 ? VTOY_JSON_SUCCESS_RET : VTOY_JSON_FAILED_RET);
1710 return 0;
1711 }
1712
1713 #if 0
1714 #endif
1715
1716 void ventoy_data_default_auto_memdisk(data_auto_memdisk *data)
1717 {
1718 memset(data, 0, sizeof(data_auto_memdisk));
1719 }
1720
1721 int ventoy_data_cmp_auto_memdisk(data_auto_memdisk *data1, data_auto_memdisk *data2)
1722 {
1723 return ventoy_path_list_cmp(data1->list, data2->list);
1724 }
1725
1726 int ventoy_data_save_auto_memdisk(data_auto_memdisk *data, const char *title, char *buf, int buflen)
1727 {
1728 int pos = 0;
1729 path_node *node = NULL;
1730
1731 VTOY_JSON_FMT_BEGIN(pos, buf, buflen);
1732
1733 VTOY_JSON_FMT_KEY_L(L1, title);
1734 VTOY_JSON_FMT_ARY_BEGIN_N();
1735
1736 for (node = data->list; node; node = node->next)
1737 {
1738 VTOY_JSON_FMT_ITEM_PATH_LN(L2, node->path);
1739 }
1740
1741 VTOY_JSON_FMT_ARY_ENDEX_LN(L1);
1742 VTOY_JSON_FMT_END(pos);
1743
1744 return pos;
1745 }
1746
1747 int ventoy_data_json_auto_memdisk(data_auto_memdisk *data, char *buf, int buflen)
1748 {
1749 int pos = 0;
1750 int valid = 0;
1751 path_node *node = NULL;
1752
1753 VTOY_JSON_FMT_BEGIN(pos, buf, buflen);
1754 VTOY_JSON_FMT_ARY_BEGIN();
1755
1756 for (node = data->list; node; node = node->next)
1757 {
1758 VTOY_JSON_FMT_OBJ_BEGIN();
1759
1760 VTOY_JSON_FMT_STRN("path", node->path);
1761 valid = ventoy_check_fuzzy_path(node->path, 1);
1762 VTOY_JSON_FMT_SINT("valid", valid);
1763
1764 VTOY_JSON_FMT_OBJ_ENDEX();
1765 }
1766
1767 VTOY_JSON_FMT_ARY_END();
1768 VTOY_JSON_FMT_END(pos);
1769
1770 return pos;
1771 }
1772
1773 static int ventoy_api_get_auto_memdisk(struct mg_connection *conn, VTOY_JSON *json)
1774 {
1775 api_get_func(conn, json, auto_memdisk);
1776 return 0;
1777 }
1778
1779 static int ventoy_api_save_auto_memdisk(struct mg_connection *conn, VTOY_JSON *json)
1780 {
1781 int ret;
1782
1783 ret = ventoy_data_save_all();
1784
1785 ventoy_json_result(conn, ret == 0 ? VTOY_JSON_SUCCESS_RET : VTOY_JSON_FAILED_RET);
1786 return 0;
1787 }
1788
1789 static int ventoy_api_auto_memdisk_add(struct mg_connection *conn, VTOY_JSON *json)
1790 {
1791 int ret;
1792 int index = 0;
1793 const char *path = NULL;
1794 path_node *node = NULL;
1795 path_node *cur = NULL;
1796 data_auto_memdisk *data = NULL;
1797
1798 vtoy_json_get_int(json, "index", &index);
1799 data = g_data_auto_memdisk + index;
1800
1801 path = VTOY_JSON_STR_EX("path");
1802 if (path)
1803 {
1804 node = zalloc(sizeof(path_node));
1805 if (node)
1806 {
1807 scnprintf(node->path, sizeof(node->path), "%s", path);
1808 vtoy_list_add(data->list, cur, node);
1809 }
1810 }
1811
1812 ret = ventoy_data_save_all();
1813
1814 ventoy_json_result(conn, ret == 0 ? VTOY_JSON_SUCCESS_RET : VTOY_JSON_FAILED_RET);
1815 return 0;
1816 }
1817
1818 static int ventoy_api_auto_memdisk_del(struct mg_connection *conn, VTOY_JSON *json)
1819 {
1820 int ret;
1821 int index = 0;
1822 const char *path = NULL;
1823 path_node *last = NULL;
1824 path_node *node = NULL;
1825 data_auto_memdisk *data = NULL;
1826
1827 vtoy_json_get_int(json, "index", &index);
1828 data = g_data_auto_memdisk + index;
1829
1830 path = VTOY_JSON_STR_EX("path");
1831 if (path)
1832 {
1833 if (strcmp(path, VTOY_DEL_ALL_PATH) == 0)
1834 {
1835 vtoy_list_free(path_node, data->list);
1836 }
1837 else
1838 {
1839 vtoy_list_del(last, node, data->list, path);
1840 }
1841 }
1842
1843 ret = ventoy_data_save_all();
1844
1845 ventoy_json_result(conn, ret == 0 ? VTOY_JSON_SUCCESS_RET : VTOY_JSON_FAILED_RET);
1846 return 0;
1847 }
1848
1849 #if 0
1850 #endif
1851
1852 void ventoy_data_default_image_list(data_image_list *data)
1853 {
1854 memset(data, 0, sizeof(data_image_list));
1855 }
1856
1857 int ventoy_data_cmp_image_list(data_image_list *data1, data_image_list *data2)
1858 {
1859 if (data1->type != data2->type)
1860 {
1861 if (data1->list || data2->list)
1862 {
1863 return 1;
1864 }
1865 else
1866 {
1867 return 0;
1868 }
1869 }
1870
1871 return ventoy_path_list_cmp(data1->list, data2->list);
1872 }
1873
1874 int ventoy_data_save_image_list(data_image_list *data, const char *title, char *buf, int buflen)
1875 {
1876 int pos = 0;
1877 int prelen;
1878 path_node *node = NULL;
1879 char newtitle[64];
1880
1881 (void)title;
1882
1883 if (!(data->list))
1884 {
1885 return 0;
1886 }
1887
1888 prelen = (int)strlen("image_list");
1889
1890 VTOY_JSON_FMT_BEGIN(pos, buf, buflen);
1891
1892 if (data->type == 0)
1893 {
1894 scnprintf(newtitle, sizeof(newtitle), "image_list%s", title + prelen);
1895 }
1896 else
1897 {
1898 scnprintf(newtitle, sizeof(newtitle), "image_blacklist%s", title + prelen);
1899 }
1900 VTOY_JSON_FMT_KEY_L(L1, newtitle);
1901
1902 VTOY_JSON_FMT_ARY_BEGIN_N();
1903
1904 for (node = data->list; node; node = node->next)
1905 {
1906 VTOY_JSON_FMT_ITEM_PATH_LN(L2, node->path);
1907 }
1908
1909 VTOY_JSON_FMT_ARY_ENDEX_LN(L1);
1910 VTOY_JSON_FMT_END(pos);
1911
1912 return pos;
1913 }
1914
1915 int ventoy_data_json_image_list(data_image_list *data, char *buf, int buflen)
1916 {
1917 int pos = 0;
1918 int valid = 0;
1919 path_node *node = NULL;
1920
1921 VTOY_JSON_FMT_BEGIN(pos, buf, buflen);
1922
1923 VTOY_JSON_FMT_OBJ_BEGIN();
1924 VTOY_JSON_FMT_SINT("type", data->type);
1925
1926 VTOY_JSON_FMT_KEY("list");
1927 VTOY_JSON_FMT_ARY_BEGIN();
1928
1929 for (node = data->list; node; node = node->next)
1930 {
1931 VTOY_JSON_FMT_OBJ_BEGIN();
1932
1933 VTOY_JSON_FMT_STRN("path", node->path);
1934 valid = ventoy_check_fuzzy_path(node->path, 1);
1935 VTOY_JSON_FMT_SINT("valid", valid);
1936
1937 VTOY_JSON_FMT_OBJ_ENDEX();
1938 }
1939
1940 VTOY_JSON_FMT_ARY_ENDEX();
1941 VTOY_JSON_FMT_OBJ_END();
1942
1943 VTOY_JSON_FMT_END(pos);
1944
1945 return pos;
1946 }
1947
1948 static int ventoy_api_get_image_list(struct mg_connection *conn, VTOY_JSON *json)
1949 {
1950 api_get_func(conn, json, image_list);
1951 return 0;
1952 }
1953
1954 static int ventoy_api_save_image_list(struct mg_connection *conn, VTOY_JSON *json)
1955 {
1956 int ret;
1957 int index = 0;
1958 data_image_list *data = NULL;
1959
1960 vtoy_json_get_int(json, "index", &index);
1961 data = g_data_image_list + index;
1962
1963 VTOY_JSON_INT("type", data->type);
1964
1965 ret = ventoy_data_save_all();
1966
1967 ventoy_json_result(conn, ret == 0 ? VTOY_JSON_SUCCESS_RET : VTOY_JSON_FAILED_RET);
1968 return 0;
1969 }
1970
1971 static int ventoy_api_image_list_add(struct mg_connection *conn, VTOY_JSON *json)
1972 {
1973 int ret;
1974 int index = 0;
1975 const char *path = NULL;
1976 path_node *node = NULL;
1977 path_node *cur = NULL;
1978 data_image_list *data = NULL;
1979
1980 vtoy_json_get_int(json, "index", &index);
1981 data = g_data_image_list + index;
1982
1983 path = VTOY_JSON_STR_EX("path");
1984 if (path)
1985 {
1986 node = zalloc(sizeof(path_node));
1987 if (node)
1988 {
1989 scnprintf(node->path, sizeof(node->path), "%s", path);
1990 vtoy_list_add(data->list, cur, node);
1991 }
1992 }
1993
1994 ret = ventoy_data_save_all();
1995
1996 ventoy_json_result(conn, ret == 0 ? VTOY_JSON_SUCCESS_RET : VTOY_JSON_FAILED_RET);
1997 return 0;
1998 }
1999
2000 static int ventoy_api_image_list_del(struct mg_connection *conn, VTOY_JSON *json)
2001 {
2002 int ret;
2003 int index = 0;
2004 const char *path = NULL;
2005 path_node *last = NULL;
2006 path_node *node = NULL;
2007 data_image_list *data = NULL;
2008
2009 vtoy_json_get_int(json, "index", &index);
2010 data = g_data_image_list + index;
2011
2012 path = VTOY_JSON_STR_EX("path");
2013 if (path)
2014 {
2015 if (strcmp(path, VTOY_DEL_ALL_PATH) == 0)
2016 {
2017 vtoy_list_free(path_node, data->list);
2018 }
2019 else
2020 {
2021 vtoy_list_del(last, node, data->list, path);
2022 }
2023 }
2024
2025 ret = ventoy_data_save_all();
2026
2027 ventoy_json_result(conn, ret == 0 ? VTOY_JSON_SUCCESS_RET : VTOY_JSON_FAILED_RET);
2028 return 0;
2029 }
2030
2031 #if 0
2032 #endif
2033
2034 void ventoy_data_default_password(data_password *data)
2035 {
2036 memset(data, 0, sizeof(data_password));
2037 }
2038
2039 int ventoy_data_cmp_password(data_password *data1, data_password *data2)
2040 {
2041 menu_password *list1 = NULL;
2042 menu_password *list2 = NULL;
2043
2044 if (strcmp(data1->bootpwd, data2->bootpwd) ||
2045 strcmp(data1->isopwd, data2->isopwd) ||
2046 strcmp(data1->wimpwd, data2->wimpwd) ||
2047 strcmp(data1->vhdpwd, data2->vhdpwd) ||
2048 strcmp(data1->imgpwd, data2->imgpwd) ||
2049 strcmp(data1->efipwd, data2->efipwd) ||
2050 strcmp(data1->vtoypwd, data2->vtoypwd)
2051 )
2052 {
2053 return 1;
2054 }
2055
2056 if (NULL == data1->list && NULL == data2->list)
2057 {
2058 return 0;
2059 }
2060 else if (data1->list && data2->list)
2061 {
2062 list1 = data1->list;
2063 list2 = data2->list;
2064
2065 while (list1 && list2)
2066 {
2067 if ((list1->type != list2->type) || strcmp(list1->path, list2->path))
2068 {
2069 return 1;
2070 }
2071
2072 list1 = list1->next;
2073 list2 = list2->next;
2074 }
2075
2076 if (list1 == NULL && list2 == NULL)
2077 {
2078 return 0;
2079 }
2080 return 1;
2081 }
2082 else
2083 {
2084 return 1;
2085 }
2086
2087 return 0;
2088 }
2089
2090
2091 int ventoy_data_save_password(data_password *data, const char *title, char *buf, int buflen)
2092 {
2093 int pos = 0;
2094 menu_password *node = NULL;
2095 data_password *def = g_data_password + bios_max;
2096
2097 VTOY_JSON_FMT_BEGIN(pos, buf, buflen);
2098 VTOY_JSON_FMT_KEY_L(L1, title);
2099 VTOY_JSON_FMT_OBJ_BEGIN_N();
2100
2101 VTOY_JSON_FMT_DIFF_STRN(L2, "bootpwd", bootpwd);
2102 VTOY_JSON_FMT_DIFF_STRN(L2, "isopwd", isopwd);
2103 VTOY_JSON_FMT_DIFF_STRN(L2, "wimpwd", wimpwd);
2104 VTOY_JSON_FMT_DIFF_STRN(L2, "vhdpwd", vhdpwd);
2105 VTOY_JSON_FMT_DIFF_STRN(L2, "imgpwd", imgpwd);
2106 VTOY_JSON_FMT_DIFF_STRN(L2, "efipwd", efipwd);
2107 VTOY_JSON_FMT_DIFF_STRN(L2, "vtoypwd", vtoypwd);
2108
2109 if (data->list)
2110 {
2111 VTOY_JSON_FMT_KEY_L(L2, "menupwd");
2112 VTOY_JSON_FMT_ARY_BEGIN_N();
2113
2114 for (node = data->list; node; node = node->next)
2115 {
2116 VTOY_JSON_FMT_OBJ_BEGIN_LN(L3);
2117
2118 if (node->type == 0)
2119 {
2120 VTOY_JSON_FMT_STRN_PATH_LN(L4, "file", node->path);
2121 }
2122 else
2123 {
2124 VTOY_JSON_FMT_STRN_PATH_LN(L4, "parent", node->path);
2125 }
2126 VTOY_JSON_FMT_STRN_LN(L4, "pwd", node->pwd);
2127
2128 VTOY_JSON_FMT_OBJ_ENDEX_LN(L3);
2129 }
2130
2131 VTOY_JSON_FMT_ARY_ENDEX_LN(L2);
2132 }
2133
2134 VTOY_JSON_FMT_OBJ_ENDEX_LN(L1);
2135
2136 VTOY_JSON_FMT_END(pos);
2137
2138 return pos;
2139 }
2140
2141
2142 int ventoy_data_json_password(data_password *data, char *buf, int buflen)
2143 {
2144 int pos = 0;
2145 int valid = 0;
2146 menu_password *node = NULL;
2147
2148 VTOY_JSON_FMT_BEGIN(pos, buf, buflen);
2149
2150 VTOY_JSON_FMT_OBJ_BEGIN();
2151
2152 VTOY_JSON_FMT_STRN("bootpwd", data->bootpwd);
2153 VTOY_JSON_FMT_STRN("isopwd", data->isopwd);
2154 VTOY_JSON_FMT_STRN("wimpwd", data->wimpwd);
2155 VTOY_JSON_FMT_STRN("vhdpwd", data->vhdpwd);
2156 VTOY_JSON_FMT_STRN("imgpwd", data->imgpwd);
2157 VTOY_JSON_FMT_STRN("efipwd", data->efipwd);
2158 VTOY_JSON_FMT_STRN("vtoypwd", data->vtoypwd);
2159
2160 VTOY_JSON_FMT_KEY("list");
2161 VTOY_JSON_FMT_ARY_BEGIN();
2162
2163 for (node = data->list; node; node = node->next)
2164 {
2165 VTOY_JSON_FMT_OBJ_BEGIN();
2166
2167 VTOY_JSON_FMT_SINT("type", node->type);
2168 VTOY_JSON_FMT_STRN("path", node->path);
2169 if (node->type == path_type_file)
2170 {
2171 valid = ventoy_check_fuzzy_path(node->path, 1);
2172 }
2173 else
2174 {
2175 valid = ventoy_is_directory_exist("%s%s", g_cur_dir, node->path);
2176 }
2177
2178 VTOY_JSON_FMT_SINT("valid", valid);
2179 VTOY_JSON_FMT_STRN("pwd", node->pwd);
2180
2181 VTOY_JSON_FMT_OBJ_ENDEX();
2182 }
2183
2184 VTOY_JSON_FMT_ARY_ENDEX();
2185
2186 VTOY_JSON_FMT_OBJ_END();
2187 VTOY_JSON_FMT_END(pos);
2188
2189 return pos;
2190 }
2191
2192 static int ventoy_api_get_password(struct mg_connection *conn, VTOY_JSON *json)
2193 {
2194 api_get_func(conn, json, password);
2195 return 0;
2196 }
2197
2198 static int ventoy_api_save_password(struct mg_connection *conn, VTOY_JSON *json)
2199 {
2200 int ret;
2201 int index = 0;
2202 data_password *data = NULL;
2203
2204 vtoy_json_get_int(json, "index", &index);
2205 data = g_data_password + index;
2206
2207 VTOY_JSON_STR("bootpwd", data->bootpwd);
2208 VTOY_JSON_STR("isopwd", data->isopwd);
2209 VTOY_JSON_STR("wimpwd", data->wimpwd);
2210 VTOY_JSON_STR("vhdpwd", data->vhdpwd);
2211 VTOY_JSON_STR("imgpwd", data->imgpwd);
2212 VTOY_JSON_STR("efipwd", data->efipwd);
2213 VTOY_JSON_STR("vtoypwd", data->vtoypwd);
2214
2215 ret = ventoy_data_save_all();
2216
2217 ventoy_json_result(conn, ret == 0 ? VTOY_JSON_SUCCESS_RET : VTOY_JSON_FAILED_RET);
2218 return 0;
2219 }
2220
2221 static int ventoy_api_password_add(struct mg_connection *conn, VTOY_JSON *json)
2222 {
2223 int ret;
2224 int index = 0;
2225 int type = 0;
2226 const char *path = NULL;
2227 const char *pwd = NULL;
2228 menu_password *node = NULL;
2229 menu_password *cur = NULL;
2230 data_password *data = NULL;
2231
2232 vtoy_json_get_int(json, "index", &index);
2233 data = g_data_password + index;
2234
2235 vtoy_json_get_int(json, "type", &type);
2236
2237 path = VTOY_JSON_STR_EX("path");
2238 pwd = VTOY_JSON_STR_EX("pwd");
2239 if (path && pwd)
2240 {
2241 node = zalloc(sizeof(menu_password));
2242 if (node)
2243 {
2244 node->type = type;
2245 scnprintf(node->path, sizeof(node->path), "%s", path);
2246 scnprintf(node->pwd, sizeof(node->pwd), "%s", pwd);
2247
2248 vtoy_list_add(data->list, cur, node);
2249 }
2250 }
2251
2252 ret = ventoy_data_save_all();
2253
2254 ventoy_json_result(conn, ret == 0 ? VTOY_JSON_SUCCESS_RET : VTOY_JSON_FAILED_RET);
2255 return 0;
2256 }
2257
2258 static int ventoy_api_password_del(struct mg_connection *conn, VTOY_JSON *json)
2259 {
2260 int ret;
2261 int index = 0;
2262 const char *path = NULL;
2263 menu_password *last = NULL;
2264 menu_password *node = NULL;
2265 data_password *data = NULL;
2266
2267 vtoy_json_get_int(json, "index", &index);
2268 data = g_data_password + index;
2269
2270 path = VTOY_JSON_STR_EX("path");
2271 if (path)
2272 {
2273 if (strcmp(path, VTOY_DEL_ALL_PATH) == 0)
2274 {
2275 vtoy_list_free(menu_password, data->list);
2276 }
2277 else
2278 {
2279 vtoy_list_del(last, node, data->list, path);
2280 }
2281 }
2282
2283 ret = ventoy_data_save_all();
2284
2285 ventoy_json_result(conn, ret == 0 ? VTOY_JSON_SUCCESS_RET : VTOY_JSON_FAILED_RET);
2286 return 0;
2287 }
2288
2289 #if 0
2290 #endif
2291
2292 void ventoy_data_default_conf_replace(data_conf_replace *data)
2293 {
2294 memset(data, 0, sizeof(data_conf_replace));
2295 }
2296
2297 int ventoy_data_cmp_conf_replace(data_conf_replace *data1, data_conf_replace *data2)
2298 {
2299 conf_replace_node *list1 = NULL;
2300 conf_replace_node *list2 = NULL;
2301
2302 if (NULL == data1->list && NULL == data2->list)
2303 {
2304 return 0;
2305 }
2306 else if (data1->list && data2->list)
2307 {
2308 list1 = data1->list;
2309 list2 = data2->list;
2310
2311 while (list1 && list2)
2312 {
2313 if (list1->image != list2->image ||
2314 strcmp(list1->path, list2->path) ||
2315 strcmp(list1->org, list2->org) ||
2316 strcmp(list1->new, list2->new)
2317 )
2318 {
2319 return 1;
2320 }
2321
2322 list1 = list1->next;
2323 list2 = list2->next;
2324 }
2325
2326 if (list1 == NULL && list2 == NULL)
2327 {
2328 return 0;
2329 }
2330 return 1;
2331 }
2332 else
2333 {
2334 return 1;
2335 }
2336
2337 return 0;
2338 }
2339
2340
2341 int ventoy_data_save_conf_replace(data_conf_replace *data, const char *title, char *buf, int buflen)
2342 {
2343 int pos = 0;
2344 conf_replace_node *node = NULL;
2345
2346 VTOY_JSON_FMT_BEGIN(pos, buf, buflen);
2347
2348 VTOY_JSON_FMT_KEY_L(L1, title);
2349 VTOY_JSON_FMT_ARY_BEGIN_N();
2350
2351 for (node = data->list; node; node = node->next)
2352 {
2353 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2);
2354
2355 VTOY_JSON_FMT_STRN_PATH_LN(L3, "iso", node->path);
2356 VTOY_JSON_FMT_STRN_LN(L3, "org", node->org);
2357 VTOY_JSON_FMT_STRN_PATH_LN(L3, "new", node->new);
2358 if (node->image)
2359 {
2360 VTOY_JSON_FMT_SINT_LN(L3, "img", node->image);
2361 }
2362
2363 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2);
2364 }
2365
2366 VTOY_JSON_FMT_ARY_ENDEX_LN(L1);
2367 VTOY_JSON_FMT_END(pos);
2368
2369 return pos;
2370 }
2371
2372
2373 int ventoy_data_json_conf_replace(data_conf_replace *data, char *buf, int buflen)
2374 {
2375 int pos = 0;
2376 conf_replace_node *node = NULL;
2377
2378 VTOY_JSON_FMT_BEGIN(pos, buf, buflen);
2379 VTOY_JSON_FMT_ARY_BEGIN();
2380
2381 for (node = data->list; node; node = node->next)
2382 {
2383 VTOY_JSON_FMT_OBJ_BEGIN();
2384
2385 VTOY_JSON_FMT_STRN("path", node->path);
2386 VTOY_JSON_FMT_SINT("valid", ventoy_check_fuzzy_path(node->path, 1));
2387 VTOY_JSON_FMT_STRN("org", node->org);
2388 VTOY_JSON_FMT_STRN("new", node->new);
2389 VTOY_JSON_FMT_SINT("new_valid", ventoy_is_file_exist("%s%s", g_cur_dir, node->new));
2390 VTOY_JSON_FMT_SINT("img", node->image);
2391
2392 VTOY_JSON_FMT_OBJ_ENDEX();
2393 }
2394
2395 VTOY_JSON_FMT_ARY_END();
2396 VTOY_JSON_FMT_END(pos);
2397
2398 return pos;
2399 }
2400
2401 static int ventoy_api_get_conf_replace(struct mg_connection *conn, VTOY_JSON *json)
2402 {
2403 api_get_func(conn, json, conf_replace);
2404 return 0;
2405 }
2406
2407 static int ventoy_api_save_conf_replace(struct mg_connection *conn, VTOY_JSON *json)
2408 {
2409 int ret;
2410 ret = ventoy_data_save_all();
2411
2412 ventoy_json_result(conn, ret == 0 ? VTOY_JSON_SUCCESS_RET : VTOY_JSON_FAILED_RET);
2413 return 0;
2414 }
2415
2416 static int ventoy_api_conf_replace_add(struct mg_connection *conn, VTOY_JSON *json)
2417 {
2418 int ret;
2419 int image = 0;
2420 int index = 0;
2421 const char *path = NULL;
2422 const char *org = NULL;
2423 const char *new = NULL;
2424 conf_replace_node *node = NULL;
2425 conf_replace_node *cur = NULL;
2426 data_conf_replace *data = NULL;
2427
2428 vtoy_json_get_int(json, "img", &image);
2429
2430 vtoy_json_get_int(json, "index", &index);
2431 data = g_data_conf_replace + index;
2432
2433 path = VTOY_JSON_STR_EX("path");
2434 org = VTOY_JSON_STR_EX("org");
2435 new = VTOY_JSON_STR_EX("new");
2436 if (path && org && new)
2437 {
2438 node = zalloc(sizeof(conf_replace_node));
2439 if (node)
2440 {
2441 node->image = image;
2442 scnprintf(node->path, sizeof(node->path), "%s", path);
2443 scnprintf(node->org, sizeof(node->org), "%s", org);
2444 scnprintf(node->new, sizeof(node->new), "%s", new);
2445
2446 vtoy_list_add(data->list, cur, node);
2447 }
2448 }
2449
2450 ret = ventoy_data_save_all();
2451
2452 ventoy_json_result(conn, ret == 0 ? VTOY_JSON_SUCCESS_RET : VTOY_JSON_FAILED_RET);
2453 return 0;
2454 }
2455
2456 static int ventoy_api_conf_replace_del(struct mg_connection *conn, VTOY_JSON *json)
2457 {
2458 int ret;
2459 int index = 0;
2460 const char *path = NULL;
2461 conf_replace_node *last = NULL;
2462 conf_replace_node *node = NULL;
2463 data_conf_replace *data = NULL;
2464
2465 vtoy_json_get_int(json, "index", &index);
2466 data = g_data_conf_replace + index;
2467
2468 path = VTOY_JSON_STR_EX("path");
2469 if (path)
2470 {
2471 if (strcmp(path, VTOY_DEL_ALL_PATH) == 0)
2472 {
2473 vtoy_list_free(conf_replace_node, data->list);
2474 }
2475 else
2476 {
2477 vtoy_list_del(last, node, data->list, path);
2478 }
2479 }
2480
2481 ret = ventoy_data_save_all();
2482
2483 ventoy_json_result(conn, ret == 0 ? VTOY_JSON_SUCCESS_RET : VTOY_JSON_FAILED_RET);
2484 return 0;
2485 }
2486
2487
2488 #if 0
2489 #endif
2490
2491 void ventoy_data_default_dud(data_dud *data)
2492 {
2493 memset(data, 0, sizeof(data_dud));
2494 }
2495
2496 int ventoy_data_cmp_dud(data_dud *data1, data_dud *data2)
2497 {
2498 dud_node *list1 = NULL;
2499 dud_node *list2 = NULL;
2500
2501 if (NULL == data1->list && NULL == data2->list)
2502 {
2503 return 0;
2504 }
2505 else if (data1->list && data2->list)
2506 {
2507 list1 = data1->list;
2508 list2 = data2->list;
2509
2510 while (list1 && list2)
2511 {
2512 if (strcmp(list1->path, list2->path))
2513 {
2514 return 1;
2515 }
2516
2517 /* no need to compare dud list with default */
2518 list1 = list1->next;
2519 list2 = list2->next;
2520 }
2521
2522 if (list1 == NULL && list2 == NULL)
2523 {
2524 return 0;
2525 }
2526 return 1;
2527 }
2528 else
2529 {
2530 return 1;
2531 }
2532
2533 return 0;
2534 }
2535
2536
2537 int ventoy_data_save_dud(data_dud *data, const char *title, char *buf, int buflen)
2538 {
2539 int pos = 0;
2540 dud_node *node = NULL;
2541 path_node *pathnode = NULL;
2542
2543 VTOY_JSON_FMT_BEGIN(pos, buf, buflen);
2544
2545 VTOY_JSON_FMT_KEY_L(L1, title);
2546 VTOY_JSON_FMT_ARY_BEGIN_N();
2547
2548 for (node = data->list; node; node = node->next)
2549 {
2550 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2);
2551 VTOY_JSON_FMT_STRN_PATH_LN(L3, "image", node->path);
2552
2553 VTOY_JSON_FMT_KEY_L(L3, "dud");
2554 VTOY_JSON_FMT_ARY_BEGIN_N();
2555 for (pathnode = node->list; pathnode; pathnode = pathnode->next)
2556 {
2557 VTOY_JSON_FMT_ITEM_PATH_LN(L4, pathnode->path);
2558 }
2559 VTOY_JSON_FMT_ARY_ENDEX_LN(L3);
2560
2561 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2);
2562 }
2563
2564 VTOY_JSON_FMT_ARY_ENDEX_LN(L1);
2565 VTOY_JSON_FMT_END(pos);
2566
2567 return pos;
2568 }
2569
2570
2571 int ventoy_data_json_dud(data_dud *data, char *buf, int buflen)
2572 {
2573 int pos = 0;
2574 int valid = 0;
2575 dud_node *node = NULL;
2576 path_node *pathnode = NULL;
2577
2578 VTOY_JSON_FMT_BEGIN(pos, buf, buflen);
2579 VTOY_JSON_FMT_ARY_BEGIN();
2580
2581 for (node = data->list; node; node = node->next)
2582 {
2583 VTOY_JSON_FMT_OBJ_BEGIN();
2584
2585 VTOY_JSON_FMT_STRN("path", node->path);
2586 valid = ventoy_check_fuzzy_path(node->path, 1);
2587 VTOY_JSON_FMT_SINT("valid", valid);
2588
2589
2590 VTOY_JSON_FMT_KEY("list");
2591 VTOY_JSON_FMT_ARY_BEGIN();
2592 for (pathnode = node->list; pathnode; pathnode = pathnode->next)
2593 {
2594 VTOY_JSON_FMT_OBJ_BEGIN();
2595 VTOY_JSON_FMT_STRN("path", pathnode->path);
2596
2597 valid = ventoy_is_file_exist("%s%s", g_cur_dir, pathnode->path);
2598 VTOY_JSON_FMT_SINT("valid", valid);
2599 VTOY_JSON_FMT_OBJ_ENDEX();
2600 }
2601 VTOY_JSON_FMT_ARY_ENDEX();
2602
2603
2604 VTOY_JSON_FMT_OBJ_ENDEX();
2605 }
2606
2607 VTOY_JSON_FMT_ARY_END();
2608 VTOY_JSON_FMT_END(pos);
2609
2610 return pos;
2611 }
2612
2613 static int ventoy_api_get_dud(struct mg_connection *conn, VTOY_JSON *json)
2614 {
2615 api_get_func(conn, json, dud);
2616 return 0;
2617 }
2618
2619 static int ventoy_api_save_dud(struct mg_connection *conn, VTOY_JSON *json)
2620 {
2621 int ret;
2622 ret = ventoy_data_save_all();
2623
2624 ventoy_json_result(conn, ret == 0 ? VTOY_JSON_SUCCESS_RET : VTOY_JSON_FAILED_RET);
2625 return 0;
2626 }
2627
2628
2629 static int ventoy_api_dud_add(struct mg_connection *conn, VTOY_JSON *json)
2630 {
2631 int ret;
2632 int index = 0;
2633 const char *path = NULL;
2634 dud_node *node = NULL;
2635 dud_node *cur = NULL;
2636 data_dud *data = NULL;
2637 VTOY_JSON *array = NULL;
2638
2639 vtoy_json_get_int(json, "index", &index);
2640 data = g_data_dud + index;
2641
2642 array = vtoy_json_find_item(json, JSON_TYPE_ARRAY, "dud");
2643 path = VTOY_JSON_STR_EX("path");
2644 if (path && array)
2645 {
2646 node = zalloc(sizeof(dud_node));
2647 if (node)
2648 {
2649 scnprintf(node->path, sizeof(node->path), "%s", path);
2650 node->list = ventoy_path_node_add_array(array);
2651
2652 vtoy_list_add(data->list, cur, node);
2653 }
2654 }
2655
2656 ret = ventoy_data_save_all();
2657
2658 ventoy_json_result(conn, ret == 0 ? VTOY_JSON_SUCCESS_RET : VTOY_JSON_FAILED_RET);
2659 return 0;
2660 }
2661
2662 static int ventoy_api_dud_del(struct mg_connection *conn, VTOY_JSON *json)
2663 {
2664 int ret;
2665 int index = 0;
2666 const char *path = NULL;
2667 dud_node *next = NULL;
2668 dud_node *last = NULL;
2669 dud_node *node = NULL;
2670 data_dud *data = NULL;
2671
2672 vtoy_json_get_int(json, "index", &index);
2673 data = g_data_dud + index;
2674
2675 path = VTOY_JSON_STR_EX("path");
2676 if (path)
2677 {
2678 if (strcmp(path, VTOY_DEL_ALL_PATH) == 0)
2679 {
2680 for (node = data->list; node; node = next)
2681 {
2682 next = node->next;
2683 ventoy_free_path_node_list(node->list);
2684 free(node);
2685 }
2686 data->list = NULL;
2687 }
2688 else
2689 {
2690 vtoy_list_del_ex(last, node, data->list, path, ventoy_free_path_node_list);
2691 }
2692 }
2693
2694 ret = ventoy_data_save_all();
2695
2696 ventoy_json_result(conn, ret == 0 ? VTOY_JSON_SUCCESS_RET : VTOY_JSON_FAILED_RET);
2697 return 0;
2698 }
2699
2700
2701 static int ventoy_api_dud_add_inner(struct mg_connection *conn, VTOY_JSON *json)
2702 {
2703 int ret;
2704 int index = 0;
2705 const char *path = NULL;
2706 const char *outpath = NULL;
2707 path_node *pcur = NULL;
2708 path_node *pnode = NULL;
2709 dud_node *node = NULL;
2710 data_dud *data = NULL;
2711
2712 vtoy_json_get_int(json, "index", &index);
2713 data = g_data_dud + index;
2714
2715 path = VTOY_JSON_STR_EX("path");
2716 outpath = VTOY_JSON_STR_EX("outpath");
2717 if (path && outpath)
2718 {
2719 for (node = data->list; node; node = node->next)
2720 {
2721 if (strcmp(outpath, node->path) == 0)
2722 {
2723 pnode = zalloc(sizeof(path_node));
2724 if (pnode)
2725 {
2726 scnprintf(pnode->path, sizeof(pnode->path), "%s", path);
2727 vtoy_list_add(node->list, pcur, pnode);
2728 }
2729
2730 break;
2731 }
2732 }
2733 }
2734
2735 ret = ventoy_data_save_all();
2736
2737 ventoy_json_result(conn, ret == 0 ? VTOY_JSON_SUCCESS_RET : VTOY_JSON_FAILED_RET);
2738 return 0;
2739 }
2740
2741 static int ventoy_api_dud_del_inner(struct mg_connection *conn, VTOY_JSON *json)
2742 {
2743 int ret;
2744 int index = 0;
2745 const char *path = NULL;
2746 const char *outpath = NULL;
2747 path_node *plast = NULL;
2748 path_node *pnode = NULL;
2749 dud_node *node = NULL;
2750 data_dud *data = NULL;
2751
2752 vtoy_json_get_int(json, "index", &index);
2753 data = g_data_dud + index;
2754
2755 path = VTOY_JSON_STR_EX("path");
2756 outpath = VTOY_JSON_STR_EX("outpath");
2757 if (path && outpath)
2758 {
2759 for (node = data->list; node; node = node->next)
2760 {
2761 if (strcmp(outpath, node->path) == 0)
2762 {
2763 vtoy_list_del(plast, pnode, node->list, path);
2764 break;
2765 }
2766 }
2767 }
2768
2769 ret = ventoy_data_save_all();
2770
2771 ventoy_json_result(conn, ret == 0 ? VTOY_JSON_SUCCESS_RET : VTOY_JSON_FAILED_RET);
2772 return 0;
2773 }
2774
2775
2776 #if 0
2777 #endif
2778
2779 void ventoy_data_default_auto_install(data_auto_install *data)
2780 {
2781 memset(data, 0, sizeof(data_auto_install));
2782 }
2783
2784 int ventoy_data_cmp_auto_install(data_auto_install *data1, data_auto_install *data2)
2785 {
2786 auto_install_node *list1 = NULL;
2787 auto_install_node *list2 = NULL;
2788
2789 if (NULL == data1->list && NULL == data2->list)
2790 {
2791 return 0;
2792 }
2793 else if (data1->list && data2->list)
2794 {
2795 list1 = data1->list;
2796 list2 = data2->list;
2797
2798 while (list1 && list2)
2799 {
2800 if (list1->timeout != list2->timeout ||
2801 list1->autosel != list2->autosel ||
2802 strcmp(list1->path, list2->path))
2803 {
2804 return 1;
2805 }
2806
2807 /* no need to compare auto install list with default */
2808 list1 = list1->next;
2809 list2 = list2->next;
2810 }
2811
2812 if (list1 == NULL && list2 == NULL)
2813 {
2814 return 0;
2815 }
2816 return 1;
2817 }
2818 else
2819 {
2820 return 1;
2821 }
2822
2823 return 0;
2824 }
2825
2826
2827 int ventoy_data_save_auto_install(data_auto_install *data, const char *title, char *buf, int buflen)
2828 {
2829 int pos = 0;
2830 auto_install_node *node = NULL;
2831 path_node *pathnode = NULL;
2832
2833 VTOY_JSON_FMT_BEGIN(pos, buf, buflen);
2834
2835 VTOY_JSON_FMT_KEY_L(L1, title);
2836 VTOY_JSON_FMT_ARY_BEGIN_N();
2837
2838 for (node = data->list; node; node = node->next)
2839 {
2840 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2);
2841 if (node->type == 0)
2842 {
2843 VTOY_JSON_FMT_STRN_PATH_LN(L3, "image", node->path);
2844 }
2845 else
2846 {
2847 VTOY_JSON_FMT_STRN_PATH_LN(L3, "parent", node->path);
2848 }
2849
2850
2851 VTOY_JSON_FMT_KEY_L(L3, "template");
2852 VTOY_JSON_FMT_ARY_BEGIN_N();
2853 for (pathnode = node->list; pathnode; pathnode = pathnode->next)
2854 {
2855 VTOY_JSON_FMT_ITEM_PATH_LN(L4, pathnode->path);
2856 }
2857 VTOY_JSON_FMT_ARY_ENDEX_LN(L3);
2858
2859 if (node->timeouten)
2860 {
2861 VTOY_JSON_FMT_SINT_LN(L3, "timeout", node->timeout);
2862 }
2863
2864 if (node->autoselen)
2865 {
2866 VTOY_JSON_FMT_SINT_LN(L3, "autosel", node->autosel);
2867 }
2868
2869 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2);
2870 }
2871
2872 VTOY_JSON_FMT_ARY_ENDEX_LN(L1);
2873 VTOY_JSON_FMT_END(pos);
2874
2875 return pos;
2876 }
2877
2878
2879 int ventoy_data_json_auto_install(data_auto_install *data, char *buf, int buflen)
2880 {
2881 int pos = 0;
2882 int valid = 0;
2883 auto_install_node *node = NULL;
2884 path_node *pathnode = NULL;
2885
2886 VTOY_JSON_FMT_BEGIN(pos, buf, buflen);
2887 VTOY_JSON_FMT_ARY_BEGIN();
2888
2889 for (node = data->list; node; node = node->next)
2890 {
2891 VTOY_JSON_FMT_OBJ_BEGIN();
2892
2893 VTOY_JSON_FMT_STRN("path", node->path);
2894
2895 if (node->type == 0)
2896 {
2897 valid = ventoy_check_fuzzy_path(node->path, 1);
2898 }
2899 else
2900 {
2901 valid = ventoy_is_directory_exist("%s%s", g_cur_dir, node->path);
2902 }
2903 VTOY_JSON_FMT_SINT("valid", valid);
2904 VTOY_JSON_FMT_SINT("type", node->type);
2905
2906 VTOY_JSON_FMT_BOOL("timeouten", node->timeouten);
2907 VTOY_JSON_FMT_BOOL("autoselen", node->autoselen);
2908
2909 VTOY_JSON_FMT_SINT("autosel", node->autosel);
2910 VTOY_JSON_FMT_SINT("timeout", node->timeout);
2911
2912 VTOY_JSON_FMT_KEY("list");
2913 VTOY_JSON_FMT_ARY_BEGIN();
2914 for (pathnode = node->list; pathnode; pathnode = pathnode->next)
2915 {
2916 VTOY_JSON_FMT_OBJ_BEGIN();
2917 VTOY_JSON_FMT_STRN("path", pathnode->path);
2918
2919 valid = ventoy_is_file_exist("%s%s", g_cur_dir, pathnode->path);
2920 VTOY_JSON_FMT_SINT("valid", valid);
2921 VTOY_JSON_FMT_OBJ_ENDEX();
2922 }
2923 VTOY_JSON_FMT_ARY_ENDEX();
2924
2925
2926 VTOY_JSON_FMT_OBJ_ENDEX();
2927 }
2928
2929 VTOY_JSON_FMT_ARY_END();
2930 VTOY_JSON_FMT_END(pos);
2931
2932 return pos;
2933 }
2934
2935 static int ventoy_api_get_auto_install(struct mg_connection *conn, VTOY_JSON *json)
2936 {
2937 api_get_func(conn, json, auto_install);
2938 return 0;
2939 }
2940
2941 static int ventoy_api_save_auto_install(struct mg_connection *conn, VTOY_JSON *json)
2942 {
2943 int ret;
2944 int id = -1;
2945 int cnt = 0;
2946 int index = 0;
2947 uint8_t timeouten = 0;
2948 uint8_t autoselen = 0;
2949 auto_install_node *node = NULL;
2950 data_auto_install *data = NULL;
2951
2952 vtoy_json_get_int(json, "index", &index);
2953 vtoy_json_get_int(json, "id", &id);
2954
2955 vtoy_json_get_bool(json, "timeouten", &timeouten);
2956 vtoy_json_get_bool(json, "autoselen", &autoselen);
2957
2958 data = g_data_auto_install + index;
2959
2960 if (id >= 0)
2961 {
2962 for (node = data->list; node; node = node->next)
2963 {
2964 if (cnt == id)
2965 {
2966 node->timeouten = (int)timeouten;
2967 node->autoselen = (int)autoselen;
2968 VTOY_JSON_INT("timeout", node->timeout);
2969 VTOY_JSON_INT("autosel", node->autosel);
2970 break;
2971 }
2972 }
2973 }
2974
2975 ret = ventoy_data_save_all();
2976
2977 ventoy_json_result(conn, ret == 0 ? VTOY_JSON_SUCCESS_RET : VTOY_JSON_FAILED_RET);
2978 return 0;
2979 }
2980
2981
2982 static int ventoy_api_auto_install_add(struct mg_connection *conn, VTOY_JSON *json)
2983 {
2984 int ret;
2985 int index = 0;
2986 int type = 0;
2987 const char *path = NULL;
2988 auto_install_node *node = NULL;
2989 auto_install_node *cur = NULL;
2990 data_auto_install *data = NULL;
2991 VTOY_JSON *array = NULL;
2992
2993 vtoy_json_get_int(json, "type", &type);
2994 vtoy_json_get_int(json, "index", &index);
2995 data = g_data_auto_install + index;
2996
2997 array = vtoy_json_find_item(json, JSON_TYPE_ARRAY, "template");
2998 path = VTOY_JSON_STR_EX("path");
2999 if (path && array)
3000 {
3001 node = zalloc(sizeof(auto_install_node));
3002 if (node)
3003 {
3004 node->type = type;
3005 node->timeouten = 0;
3006 node->autoselen = 0;
3007 node->autosel = 1;
3008 node->timeout = 0;
3009 scnprintf(node->path, sizeof(node->path), "%s", path);
3010 node->list = ventoy_path_node_add_array(array);
3011
3012 vtoy_list_add(data->list, cur, node);
3013 }
3014 }
3015
3016 ret = ventoy_data_save_all();
3017
3018 ventoy_json_result(conn, ret == 0 ? VTOY_JSON_SUCCESS_RET : VTOY_JSON_FAILED_RET);
3019 return 0;
3020 }
3021
3022 static int ventoy_api_auto_install_del(struct mg_connection *conn, VTOY_JSON *json)
3023 {
3024 int ret;
3025 int index = 0;
3026 const char *path = NULL;
3027 auto_install_node *last = NULL;
3028 auto_install_node *next = NULL;
3029 auto_install_node *node = NULL;
3030 data_auto_install *data = NULL;
3031
3032 vtoy_json_get_int(json, "index", &index);
3033 data = g_data_auto_install + index;
3034
3035 path = VTOY_JSON_STR_EX("path");
3036 if (path)
3037 {
3038 if (strcmp(path, VTOY_DEL_ALL_PATH) == 0)
3039 {
3040 for (node = data->list; node; node = next)
3041 {
3042 next = node->next;
3043 ventoy_free_path_node_list(node->list);
3044 free(node);
3045 }
3046 data->list = NULL;
3047 }
3048 else
3049 {
3050 vtoy_list_del_ex(last, node, data->list, path, ventoy_free_path_node_list);
3051 }
3052 }
3053
3054 ret = ventoy_data_save_all();
3055
3056 ventoy_json_result(conn, ret == 0 ? VTOY_JSON_SUCCESS_RET : VTOY_JSON_FAILED_RET);
3057 return 0;
3058 }
3059
3060 static int ventoy_api_auto_install_add_inner(struct mg_connection *conn, VTOY_JSON *json)
3061 {
3062 int ret;
3063 int index = 0;
3064 const char *path = NULL;
3065 const char *outpath = NULL;
3066 path_node *pcur = NULL;
3067 path_node *pnode = NULL;
3068 auto_install_node *node = NULL;
3069 data_auto_install *data = NULL;
3070
3071 vtoy_json_get_int(json, "index", &index);
3072 data = g_data_auto_install + index;
3073
3074 path = VTOY_JSON_STR_EX("path");
3075 outpath = VTOY_JSON_STR_EX("outpath");
3076 if (path && outpath)
3077 {
3078 for (node = data->list; node; node = node->next)
3079 {
3080 if (strcmp(outpath, node->path) == 0)
3081 {
3082 pnode = zalloc(sizeof(path_node));
3083 if (pnode)
3084 {
3085 scnprintf(pnode->path, sizeof(pnode->path), "%s", path);
3086 vtoy_list_add(node->list, pcur, pnode);
3087 }
3088
3089 break;
3090 }
3091 }
3092 }
3093
3094 ret = ventoy_data_save_all();
3095
3096 ventoy_json_result(conn, ret == 0 ? VTOY_JSON_SUCCESS_RET : VTOY_JSON_FAILED_RET);
3097 return 0;
3098 }
3099
3100 static int ventoy_api_auto_install_del_inner(struct mg_connection *conn, VTOY_JSON *json)
3101 {
3102 int ret;
3103 int index = 0;
3104 const char *path = NULL;
3105 const char *outpath = NULL;
3106 path_node *plast = NULL;
3107 path_node *pnode = NULL;
3108 auto_install_node *node = NULL;
3109 data_auto_install *data = NULL;
3110
3111 vtoy_json_get_int(json, "index", &index);
3112 data = g_data_auto_install + index;
3113
3114 path = VTOY_JSON_STR_EX("path");
3115 outpath = VTOY_JSON_STR_EX("outpath");
3116 if (path && outpath)
3117 {
3118 for (node = data->list; node; node = node->next)
3119 {
3120 if (strcmp(outpath, node->path) == 0)
3121 {
3122 vtoy_list_del(plast, pnode, node->list, path);
3123 break;
3124 }
3125 }
3126 }
3127
3128 ret = ventoy_data_save_all();
3129
3130 ventoy_json_result(conn, ret == 0 ? VTOY_JSON_SUCCESS_RET : VTOY_JSON_FAILED_RET);
3131 return 0;
3132 }
3133
3134
3135 #if 0
3136 #endif
3137
3138
3139 void ventoy_data_default_persistence(data_persistence *data)
3140 {
3141 memset(data, 0, sizeof(data_persistence));
3142 }
3143
3144 int ventoy_data_cmp_persistence(data_persistence *data1, data_persistence *data2)
3145 {
3146 persistence_node *list1 = NULL;
3147 persistence_node *list2 = NULL;
3148
3149 if (NULL == data1->list && NULL == data2->list)
3150 {
3151 return 0;
3152 }
3153 else if (data1->list && data2->list)
3154 {
3155 list1 = data1->list;
3156 list2 = data2->list;
3157
3158 while (list1 && list2)
3159 {
3160 if (list1->timeout != list2->timeout ||
3161 list1->autosel != list2->autosel ||
3162 strcmp(list1->path, list2->path))
3163 {
3164 return 1;
3165 }
3166
3167 /* no need to compare auto install list with default */
3168 list1 = list1->next;
3169 list2 = list2->next;
3170 }
3171
3172 if (list1 == NULL && list2 == NULL)
3173 {
3174 return 0;
3175 }
3176 return 1;
3177 }
3178 else
3179 {
3180 return 1;
3181 }
3182
3183 return 0;
3184 }
3185
3186
3187 int ventoy_data_save_persistence(data_persistence *data, const char *title, char *buf, int buflen)
3188 {
3189 int pos = 0;
3190 persistence_node *node = NULL;
3191 path_node *pathnode = NULL;
3192
3193 VTOY_JSON_FMT_BEGIN(pos, buf, buflen);
3194
3195 VTOY_JSON_FMT_KEY_L(L1, title);
3196 VTOY_JSON_FMT_ARY_BEGIN_N();
3197
3198 for (node = data->list; node; node = node->next)
3199 {
3200 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2);
3201 VTOY_JSON_FMT_STRN_PATH_LN(L3, "image", node->path);
3202 VTOY_JSON_FMT_KEY_L(L3, "backend");
3203 VTOY_JSON_FMT_ARY_BEGIN_N();
3204 for (pathnode = node->list; pathnode; pathnode = pathnode->next)
3205 {
3206 VTOY_JSON_FMT_ITEM_PATH_LN(L4, pathnode->path);
3207 }
3208 VTOY_JSON_FMT_ARY_ENDEX_LN(L3);
3209
3210 if (node->timeouten)
3211 {
3212 VTOY_JSON_FMT_SINT_LN(L3, "timeout", node->timeout);
3213 }
3214
3215 if (node->autoselen)
3216 {
3217 VTOY_JSON_FMT_SINT_LN(L3, "autosel", node->autosel);
3218 }
3219
3220 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2);
3221 }
3222
3223 VTOY_JSON_FMT_ARY_ENDEX_LN(L1);
3224 VTOY_JSON_FMT_END(pos);
3225
3226 return pos;
3227 }
3228
3229
3230 int ventoy_data_json_persistence(data_persistence *data, char *buf, int buflen)
3231 {
3232 int pos = 0;
3233 int valid = 0;
3234 persistence_node *node = NULL;
3235 path_node *pathnode = NULL;
3236
3237 VTOY_JSON_FMT_BEGIN(pos, buf, buflen);
3238 VTOY_JSON_FMT_ARY_BEGIN();
3239
3240 for (node = data->list; node; node = node->next)
3241 {
3242 VTOY_JSON_FMT_OBJ_BEGIN();
3243
3244 VTOY_JSON_FMT_STRN("path", node->path);
3245
3246 valid = ventoy_check_fuzzy_path(node->path, 1);
3247 VTOY_JSON_FMT_SINT("valid", valid);
3248 VTOY_JSON_FMT_SINT("type", node->type);
3249
3250 VTOY_JSON_FMT_BOOL("timeouten", node->timeouten);
3251 VTOY_JSON_FMT_BOOL("autoselen", node->autoselen);
3252
3253 VTOY_JSON_FMT_SINT("autosel", node->autosel);
3254 VTOY_JSON_FMT_SINT("timeout", node->timeout);
3255
3256 VTOY_JSON_FMT_KEY("list");
3257 VTOY_JSON_FMT_ARY_BEGIN();
3258 for (pathnode = node->list; pathnode; pathnode = pathnode->next)
3259 {
3260 VTOY_JSON_FMT_OBJ_BEGIN();
3261 VTOY_JSON_FMT_STRN("path", pathnode->path);
3262
3263 valid = ventoy_is_file_exist("%s%s", g_cur_dir, pathnode->path);
3264 VTOY_JSON_FMT_SINT("valid", valid);
3265 VTOY_JSON_FMT_OBJ_ENDEX();
3266 }
3267 VTOY_JSON_FMT_ARY_ENDEX();
3268
3269
3270 VTOY_JSON_FMT_OBJ_ENDEX();
3271 }
3272
3273 VTOY_JSON_FMT_ARY_END();
3274 VTOY_JSON_FMT_END(pos);
3275
3276 return pos;
3277 }
3278
3279 static int ventoy_api_get_persistence(struct mg_connection *conn, VTOY_JSON *json)
3280 {
3281 api_get_func(conn, json, persistence);
3282 return 0;
3283 }
3284
3285 static int ventoy_api_save_persistence(struct mg_connection *conn, VTOY_JSON *json)
3286 {
3287 int ret;
3288 int id = -1;
3289 int cnt = 0;
3290 int index = 0;
3291 uint8_t timeouten = 0;
3292 uint8_t autoselen = 0;
3293 persistence_node *node = NULL;
3294 data_persistence *data = NULL;
3295
3296 vtoy_json_get_int(json, "index", &index);
3297 vtoy_json_get_int(json, "id", &id);
3298
3299 vtoy_json_get_bool(json, "timeouten", &timeouten);
3300 vtoy_json_get_bool(json, "autoselen", &autoselen);
3301
3302 data = g_data_persistence + index;
3303
3304 if (id >= 0)
3305 {
3306 for (node = data->list; node; node = node->next)
3307 {
3308 if (cnt == id)
3309 {
3310 node->timeouten = (int)timeouten;
3311 node->autoselen = (int)autoselen;
3312 VTOY_JSON_INT("timeout", node->timeout);
3313 VTOY_JSON_INT("autosel", node->autosel);
3314 break;
3315 }
3316 }
3317 }
3318
3319 ret = ventoy_data_save_all();
3320
3321 ventoy_json_result(conn, ret == 0 ? VTOY_JSON_SUCCESS_RET : VTOY_JSON_FAILED_RET);
3322 return 0;
3323 }
3324
3325
3326 static int ventoy_api_persistence_add(struct mg_connection *conn, VTOY_JSON *json)
3327 {
3328 int ret;
3329 int index = 0;
3330 const char *path = NULL;
3331 persistence_node *node = NULL;
3332 persistence_node *cur = NULL;
3333 data_persistence *data = NULL;
3334 VTOY_JSON *array = NULL;
3335
3336 vtoy_json_get_int(json, "index", &index);
3337 data = g_data_persistence + index;
3338
3339 array = vtoy_json_find_item(json, JSON_TYPE_ARRAY, "backend");
3340 path = VTOY_JSON_STR_EX("path");
3341 if (path && array)
3342 {
3343 node = zalloc(sizeof(persistence_node));
3344 if (node)
3345 {
3346 node->timeouten = 0;
3347 node->autoselen = 0;
3348 node->autosel = 1;
3349 node->timeout = 0;
3350 scnprintf(node->path, sizeof(node->path), "%s", path);
3351 node->list = ventoy_path_node_add_array(array);
3352
3353 vtoy_list_add(data->list, cur, node);
3354 }
3355 }
3356
3357 ret = ventoy_data_save_all();
3358
3359 ventoy_json_result(conn, ret == 0 ? VTOY_JSON_SUCCESS_RET : VTOY_JSON_FAILED_RET);
3360 return 0;
3361 }
3362
3363 static int ventoy_api_persistence_del(struct mg_connection *conn, VTOY_JSON *json)
3364 {
3365 int ret;
3366 int index = 0;
3367 const char *path = NULL;
3368 persistence_node *last = NULL;
3369 persistence_node *next = NULL;
3370 persistence_node *node = NULL;
3371 data_persistence *data = NULL;
3372
3373 vtoy_json_get_int(json, "index", &index);
3374 data = g_data_persistence + index;
3375
3376 path = VTOY_JSON_STR_EX("path");
3377 if (path)
3378 {
3379 if (strcmp(path, VTOY_DEL_ALL_PATH) == 0)
3380 {
3381 for (node = data->list; node; node = next)
3382 {
3383 next = node->next;
3384 ventoy_free_path_node_list(node->list);
3385 free(node);
3386 }
3387 data->list = NULL;
3388 }
3389 else
3390 {
3391 vtoy_list_del_ex(last, node, data->list, path, ventoy_free_path_node_list);
3392 }
3393 }
3394
3395 ret = ventoy_data_save_all();
3396
3397 ventoy_json_result(conn, ret == 0 ? VTOY_JSON_SUCCESS_RET : VTOY_JSON_FAILED_RET);
3398 return 0;
3399 }
3400
3401 static int ventoy_api_persistence_add_inner(struct mg_connection *conn, VTOY_JSON *json)
3402 {
3403 int ret;
3404 int index = 0;
3405 const char *path = NULL;
3406 const char *outpath = NULL;
3407 path_node *pcur = NULL;
3408 path_node *pnode = NULL;
3409 persistence_node *node = NULL;
3410 data_persistence *data = NULL;
3411
3412 vtoy_json_get_int(json, "index", &index);
3413 data = g_data_persistence + index;
3414
3415 path = VTOY_JSON_STR_EX("path");
3416 outpath = VTOY_JSON_STR_EX("outpath");
3417 if (path && outpath)
3418 {
3419 for (node = data->list; node; node = node->next)
3420 {
3421 if (strcmp(outpath, node->path) == 0)
3422 {
3423 pnode = zalloc(sizeof(path_node));
3424 if (pnode)
3425 {
3426 scnprintf(pnode->path, sizeof(pnode->path), "%s", path);
3427 vtoy_list_add(node->list, pcur, pnode);
3428 }
3429
3430 break;
3431 }
3432 }
3433 }
3434
3435 ret = ventoy_data_save_all();
3436
3437 ventoy_json_result(conn, ret == 0 ? VTOY_JSON_SUCCESS_RET : VTOY_JSON_FAILED_RET);
3438 return 0;
3439 }
3440
3441 static int ventoy_api_persistence_del_inner(struct mg_connection *conn, VTOY_JSON *json)
3442 {
3443 int ret;
3444 int index = 0;
3445 const char *path = NULL;
3446 const char *outpath = NULL;
3447 path_node *plast = NULL;
3448 path_node *pnode = NULL;
3449 persistence_node *node = NULL;
3450 data_persistence *data = NULL;
3451
3452 vtoy_json_get_int(json, "index", &index);
3453 data = g_data_persistence + index;
3454
3455 path = VTOY_JSON_STR_EX("path");
3456 outpath = VTOY_JSON_STR_EX("outpath");
3457 if (path && outpath)
3458 {
3459 for (node = data->list; node; node = node->next)
3460 {
3461 if (strcmp(outpath, node->path) == 0)
3462 {
3463 vtoy_list_del(plast, pnode, node->list, path);
3464 break;
3465 }
3466 }
3467 }
3468
3469 ret = ventoy_data_save_all();
3470
3471 ventoy_json_result(conn, ret == 0 ? VTOY_JSON_SUCCESS_RET : VTOY_JSON_FAILED_RET);
3472 return 0;
3473 }
3474
3475
3476 #if 0
3477 #endif
3478
3479 void ventoy_data_default_injection(data_injection *data)
3480 {
3481 memset(data, 0, sizeof(data_injection));
3482 }
3483
3484 int ventoy_data_cmp_injection(data_injection *data1, data_injection *data2)
3485 {
3486 injection_node *list1 = NULL;
3487 injection_node *list2 = NULL;
3488
3489 if (NULL == data1->list && NULL == data2->list)
3490 {
3491 return 0;
3492 }
3493 else if (data1->list && data2->list)
3494 {
3495 list1 = data1->list;
3496 list2 = data2->list;
3497
3498 while (list1 && list2)
3499 {
3500 if ((list1->type != list2->type) ||
3501 strcmp(list1->path, list2->path) ||
3502 strcmp(list1->archive, list2->archive))
3503 {
3504 return 1;
3505 }
3506
3507 list1 = list1->next;
3508 list2 = list2->next;
3509 }
3510
3511 if (list1 == NULL && list2 == NULL)
3512 {
3513 return 0;
3514 }
3515 return 1;
3516 }
3517 else
3518 {
3519 return 1;
3520 }
3521
3522 return 0;
3523 }
3524
3525
3526 int ventoy_data_save_injection(data_injection *data, const char *title, char *buf, int buflen)
3527 {
3528 int pos = 0;
3529 injection_node *node = NULL;
3530
3531 VTOY_JSON_FMT_BEGIN(pos, buf, buflen);
3532
3533 VTOY_JSON_FMT_KEY_L(L1, title);
3534 VTOY_JSON_FMT_ARY_BEGIN_N();
3535
3536 for (node = data->list; node; node = node->next)
3537 {
3538 VTOY_JSON_FMT_OBJ_BEGIN_LN(L2);
3539
3540 if (node->type == 0)
3541 {
3542 VTOY_JSON_FMT_STRN_PATH_LN(L3, "image", node->path);
3543 }
3544 else
3545 {
3546 VTOY_JSON_FMT_STRN_PATH_LN(L3, "parent", node->path);
3547 }
3548 VTOY_JSON_FMT_STRN_PATH_LN(L3, "archive", node->archive);
3549
3550 VTOY_JSON_FMT_OBJ_ENDEX_LN(L2);
3551 }
3552
3553 VTOY_JSON_FMT_ARY_ENDEX_LN(L1);
3554 VTOY_JSON_FMT_END(pos);
3555
3556 return pos;
3557 }
3558
3559
3560 int ventoy_data_json_injection(data_injection *data, char *buf, int buflen)
3561 {
3562 int pos = 0;
3563 int valid = 0;
3564 injection_node *node = NULL;
3565
3566 VTOY_JSON_FMT_BEGIN(pos, buf, buflen);
3567 VTOY_JSON_FMT_ARY_BEGIN();
3568
3569 for (node = data->list; node; node = node->next)
3570 {
3571 VTOY_JSON_FMT_OBJ_BEGIN();
3572
3573 VTOY_JSON_FMT_UINT("type", node->type);
3574 VTOY_JSON_FMT_STRN("path", node->path);
3575
3576 if (node->type == 0)
3577 {
3578 valid = ventoy_check_fuzzy_path(node->path, 1);
3579 }
3580 else
3581 {
3582 valid = ventoy_is_directory_exist("%s%s", g_cur_dir, node->path);
3583 }
3584 VTOY_JSON_FMT_SINT("valid", valid);
3585
3586 VTOY_JSON_FMT_STRN("archive", node->archive);
3587
3588 valid = ventoy_is_file_exist("%s%s", g_cur_dir, node->archive);
3589 VTOY_JSON_FMT_SINT("archive_valid", valid);
3590
3591 VTOY_JSON_FMT_OBJ_ENDEX();
3592 }
3593
3594 VTOY_JSON_FMT_ARY_END();
3595 VTOY_JSON_FMT_END(pos);
3596
3597 return pos;
3598 }
3599
3600
3601 static int ventoy_api_get_injection(struct mg_connection *conn, VTOY_JSON *json)
3602 {
3603 api_get_func(conn, json, injection);
3604 return 0;
3605 }
3606
3607 static int ventoy_api_save_injection(struct mg_connection *conn, VTOY_JSON *json)
3608 {
3609 int ret;
3610 ret = ventoy_data_save_all();
3611
3612 ventoy_json_result(conn, ret == 0 ? VTOY_JSON_SUCCESS_RET : VTOY_JSON_FAILED_RET);
3613 return 0;
3614 }
3615
3616 static int ventoy_api_injection_add(struct mg_connection *conn, VTOY_JSON *json)
3617 {
3618 int ret;
3619 int index = 0;
3620 int type = 0;
3621 const char *path = NULL;
3622 const char *archive = NULL;
3623 injection_node *node = NULL;
3624 injection_node *cur = NULL;
3625 data_injection *data = NULL;
3626
3627 vtoy_json_get_int(json, "index", &index);
3628 data = g_data_injection + index;
3629
3630 vtoy_json_get_int(json, "type", &type);
3631
3632 path = VTOY_JSON_STR_EX("path");
3633 archive = VTOY_JSON_STR_EX("archive");
3634 if (path && archive)
3635 {
3636 node = zalloc(sizeof(injection_node));
3637 if (node)
3638 {
3639 node->type = type;
3640
3641 scnprintf(node->path, sizeof(node->path), "%s", path);
3642 scnprintf(node->archive, sizeof(node->archive), "%s", archive);
3643
3644 vtoy_list_add(data->list, cur, node);
3645 }
3646 }
3647
3648 ret = ventoy_data_save_all();
3649
3650 ventoy_json_result(conn, ret == 0 ? VTOY_JSON_SUCCESS_RET : VTOY_JSON_FAILED_RET);
3651 return 0;
3652 }
3653
3654 static int ventoy_api_injection_del(struct mg_connection *conn, VTOY_JSON *json)
3655 {
3656 int ret;
3657 int index = 0;
3658 const char *path = NULL;
3659 injection_node *last = NULL;
3660 injection_node *node = NULL;
3661 data_injection *data = NULL;
3662
3663 vtoy_json_get_int(json, "index", &index);
3664 data = g_data_injection + index;
3665
3666 path = VTOY_JSON_STR_EX("path");
3667 if (path)
3668 {
3669 if (strcmp(path, VTOY_DEL_ALL_PATH) == 0)
3670 {
3671 vtoy_list_free(injection_node, data->list);
3672 }
3673 else
3674 {
3675 vtoy_list_del(last, node, data->list, path);
3676 }
3677 }
3678
3679 ret = ventoy_data_save_all();
3680
3681 ventoy_json_result(conn, ret == 0 ? VTOY_JSON_SUCCESS_RET : VTOY_JSON_FAILED_RET);
3682 return 0;
3683 }
3684
3685 #if 0
3686 #endif
3687
3688 static int ventoy_api_preview_json(struct mg_connection *conn, VTOY_JSON *json)
3689 {
3690 int i = 0;
3691 int pos = 0;
3692 int len = 0;
3693 int utf16enclen = 0;
3694 char *encodebuf = NULL;
3695 unsigned short *utf16buf = NULL;
3696
3697 (void)json;
3698
3699 /* We can not use json directly, because it will be formated in the JS. */
3700
3701 len = ventoy_data_real_save_all(0);
3702
3703 utf16buf = (unsigned short *)malloc(2 * len + 16);
3704 if (!utf16buf)
3705 {
3706 goto json;
3707 }
3708
3709 utf16enclen = utf8_to_utf16((unsigned char *)JSON_SAVE_BUFFER, len, utf16buf, len + 2);
3710
3711 encodebuf = (char *)malloc(utf16enclen * 4 + 16);
3712 if (!encodebuf)
3713 {
3714 goto json;
3715 }
3716
3717 for (i = 0; i < utf16enclen; i++)
3718 {
3719 scnprintf(encodebuf + i * 4, 5, "%04X", utf16buf[i]);
3720 }
3721
3722 json:
3723 VTOY_JSON_FMT_BEGIN(pos, JSON_BUFFER, JSON_BUF_MAX);
3724 VTOY_JSON_FMT_OBJ_BEGIN();
3725 VTOY_JSON_FMT_STRN("json", (encodebuf ? encodebuf : ""));
3726 VTOY_JSON_FMT_OBJ_END();
3727 VTOY_JSON_FMT_END(pos);
3728
3729 CHECK_FREE(encodebuf);
3730 CHECK_FREE(utf16buf);
3731
3732 ventoy_json_buffer(conn, JSON_BUFFER, pos);
3733 return 0;
3734 }
3735
3736
3737 #if 0
3738 #endif
3739
3740 int ventoy_data_save_all(void)
3741 {
3742 ventoy_set_writeback_event();
3743 return 0;
3744 }
3745
3746 int ventoy_data_real_save_all(int apilock)
3747 {
3748 int i = 0;
3749 int pos = 0;
3750 char title[64];
3751
3752 if (apilock)
3753 {
3754 pthread_mutex_lock(&g_api_mutex);
3755 }
3756
3757 ssprintf(pos, JSON_SAVE_BUFFER, JSON_BUF_MAX, "{\n");
3758
3759 ventoy_save_plug(control);
3760 ventoy_save_plug(theme);
3761 ventoy_save_plug(menu_alias);
3762 ventoy_save_plug(menu_tip);
3763 ventoy_save_plug(menu_class);
3764 ventoy_save_plug(auto_install);
3765 ventoy_save_plug(persistence);
3766 ventoy_save_plug(injection);
3767 ventoy_save_plug(conf_replace);
3768 ventoy_save_plug(password);
3769 ventoy_save_plug(image_list);
3770 ventoy_save_plug(auto_memdisk);
3771 ventoy_save_plug(dud);
3772
3773 if (JSON_SAVE_BUFFER[pos - 1] == '\n' && JSON_SAVE_BUFFER[pos - 2] == ',')
3774 {
3775 JSON_SAVE_BUFFER[pos - 2] = '\n';
3776 pos--;
3777 }
3778 ssprintf(pos, JSON_SAVE_BUFFER, JSON_BUF_MAX, "}\n");
3779
3780 if (apilock)
3781 {
3782 pthread_mutex_unlock(&g_api_mutex);
3783 }
3784
3785 return pos;
3786 }
3787
3788 int ventoy_http_writeback(void)
3789 {
3790 int ret;
3791 int pos;
3792 char filename[128];
3793
3794 ventoy_get_json_path(filename, NULL);
3795
3796 pos = ventoy_data_real_save_all(1);
3797
3798 #ifdef VENTOY_SIM
3799 printf("%s", JSON_SAVE_BUFFER);
3800 #endif
3801
3802 ret = ventoy_write_buf_to_file(filename, JSON_SAVE_BUFFER, pos);
3803 if (ret)
3804 {
3805 vlog("Failed to write ventoy.json file.\n");
3806 g_sysinfo.config_save_error = 1;
3807 }
3808
3809 return 0;
3810 }
3811
3812
3813 static JSON_CB g_ventoy_json_cb[] =
3814 {
3815 { "sysinfo", ventoy_api_sysinfo },
3816 { "handshake", ventoy_api_handshake },
3817 { "check_path", ventoy_api_check_exist },
3818 { "check_path2", ventoy_api_check_exist2 },
3819 { "check_fuzzy", ventoy_api_check_fuzzy },
3820
3821 { "device_info", ventoy_api_device_info },
3822
3823 { "get_control", ventoy_api_get_control },
3824 { "save_control", ventoy_api_save_control },
3825
3826 { "get_theme", ventoy_api_get_theme },
3827 { "save_theme", ventoy_api_save_theme },
3828 { "theme_add_file", ventoy_api_theme_add_file },
3829 { "theme_del_file", ventoy_api_theme_del_file },
3830 { "theme_add_font", ventoy_api_theme_add_font },
3831 { "theme_del_font", ventoy_api_theme_del_font },
3832
3833 { "get_alias", ventoy_api_get_alias },
3834 { "save_alias", ventoy_api_save_alias },
3835 { "alias_add", ventoy_api_alias_add },
3836 { "alias_del", ventoy_api_alias_del },
3837
3838 { "get_tip", ventoy_api_get_tip },
3839 { "save_tip", ventoy_api_save_tip },
3840 { "tip_add", ventoy_api_tip_add },
3841 { "tip_del", ventoy_api_tip_del },
3842
3843 { "get_class", ventoy_api_get_class },
3844 { "save_class", ventoy_api_save_class },
3845 { "class_add", ventoy_api_class_add },
3846 { "class_del", ventoy_api_class_del },
3847
3848 { "get_auto_memdisk", ventoy_api_get_auto_memdisk },
3849 { "save_auto_memdisk", ventoy_api_save_auto_memdisk },
3850 { "auto_memdisk_add", ventoy_api_auto_memdisk_add },
3851 { "auto_memdisk_del", ventoy_api_auto_memdisk_del },
3852
3853 { "get_image_list", ventoy_api_get_image_list },
3854 { "save_image_list", ventoy_api_save_image_list },
3855 { "image_list_add", ventoy_api_image_list_add },
3856 { "image_list_del", ventoy_api_image_list_del },
3857
3858 { "get_conf_replace", ventoy_api_get_conf_replace },
3859 { "save_conf_replace", ventoy_api_save_conf_replace },
3860 { "conf_replace_add", ventoy_api_conf_replace_add },
3861 { "conf_replace_del", ventoy_api_conf_replace_del },
3862
3863 { "get_dud", ventoy_api_get_dud },
3864 { "save_dud", ventoy_api_save_dud },
3865 { "dud_add", ventoy_api_dud_add },
3866 { "dud_del", ventoy_api_dud_del },
3867 { "dud_add_inner", ventoy_api_dud_add_inner },
3868 { "dud_del_inner", ventoy_api_dud_del_inner },
3869
3870 { "get_auto_install", ventoy_api_get_auto_install },
3871 { "save_auto_install", ventoy_api_save_auto_install },
3872 { "auto_install_add", ventoy_api_auto_install_add },
3873 { "auto_install_del", ventoy_api_auto_install_del },
3874 { "auto_install_add_inner", ventoy_api_auto_install_add_inner },
3875 { "auto_install_del_inner", ventoy_api_auto_install_del_inner },
3876
3877 { "get_persistence", ventoy_api_get_persistence },
3878 { "save_persistence", ventoy_api_save_persistence },
3879 { "persistence_add", ventoy_api_persistence_add },
3880 { "persistence_del", ventoy_api_persistence_del },
3881 { "persistence_add_inner", ventoy_api_persistence_add_inner },
3882 { "persistence_del_inner", ventoy_api_persistence_del_inner },
3883
3884 { "get_password", ventoy_api_get_password },
3885 { "save_password", ventoy_api_save_password },
3886 { "password_add", ventoy_api_password_add },
3887 { "password_del", ventoy_api_password_del },
3888
3889 { "get_injection", ventoy_api_get_injection },
3890 { "save_injection", ventoy_api_save_injection },
3891 { "injection_add", ventoy_api_injection_add },
3892 { "injection_del", ventoy_api_injection_del },
3893 { "preview_json", ventoy_api_preview_json },
3894
3895 };
3896
3897 static int ventoy_json_handler(struct mg_connection *conn, VTOY_JSON *json, char *jsonstr)
3898 {
3899 int i;
3900 const char *method = NULL;
3901
3902 method = vtoy_json_get_string_ex(json, "method");
3903 if (!method)
3904 {
3905 ventoy_json_result(conn, VTOY_JSON_SUCCESS_RET);
3906 return 0;
3907 }
3908
3909 if (strcmp(method, "handshake") == 0)
3910 {
3911 ventoy_api_handshake(conn, json);
3912 return 0;
3913 }
3914
3915 for (i = 0; i < (int)(sizeof(g_ventoy_json_cb) / sizeof(g_ventoy_json_cb[0])); i++)
3916 {
3917 if (strcmp(method, g_ventoy_json_cb[i].method) == 0)
3918 {
3919 g_ventoy_json_cb[i].callback(conn, json);
3920 break;
3921 }
3922 }
3923
3924 return 0;
3925 }
3926
3927 static int ventoy_request_handler(struct mg_connection *conn)
3928 {
3929 int post_data_len;
3930 int post_buf_len;
3931 VTOY_JSON *json = NULL;
3932 char *post_data_buf = NULL;
3933 const struct mg_request_info *ri = NULL;
3934 char stack_buf[512];
3935
3936 ri = mg_get_request_info(conn);
3937
3938 if (strcmp(ri->uri, "/vtoy/json") == 0)
3939 {
3940 if (ri->content_length > 500)
3941 {
3942 post_data_buf = malloc((int)(ri->content_length + 4));
3943 post_buf_len = (int)(ri->content_length + 1);
3944 }
3945 else
3946 {
3947 post_data_buf = stack_buf;
3948 post_buf_len = sizeof(stack_buf);
3949 }
3950
3951 post_data_len = mg_read(conn, post_data_buf, post_buf_len);
3952 post_data_buf[post_data_len] = 0;
3953
3954 json = vtoy_json_create();
3955 if (JSON_SUCCESS == vtoy_json_parse(json, post_data_buf))
3956 {
3957 pthread_mutex_lock(&g_api_mutex);
3958 ventoy_json_handler(conn, json->pstChild, post_data_buf);
3959 pthread_mutex_unlock(&g_api_mutex);
3960 }
3961 else
3962 {
3963 ventoy_json_result(conn, VTOY_JSON_INVALID_RET);
3964 }
3965
3966 vtoy_json_destroy(json);
3967
3968 if (post_data_buf != stack_buf)
3969 {
3970 free(post_data_buf);
3971 }
3972 return 1;
3973 }
3974 else
3975 {
3976 return 0;
3977 }
3978 }
3979
3980 const char *ventoy_web_openfile(const struct mg_connection *conn, const char *path, size_t *data_len)
3981 {
3982 ventoy_file *node = NULL;
3983
3984 (void)conn;
3985
3986 if (!path)
3987 {
3988 return NULL;
3989 }
3990
3991 node = ventoy_tar_find_file(path);
3992 if (node)
3993 {
3994 *data_len = node->size;
3995 return node->addr;
3996 }
3997 else
3998 {
3999 return NULL;
4000 }
4001 }
4002
4003 #if 0
4004 #endif
4005
4006 static int ventoy_parse_control(VTOY_JSON *json, void *p)
4007 {
4008 int i;
4009 VTOY_JSON *node = NULL;
4010 VTOY_JSON *child = NULL;
4011 data_control *data = (data_control *)p;
4012
4013 if (json->enDataType != JSON_TYPE_ARRAY)
4014 {
4015 return 0;
4016 }
4017
4018 for (node = json->pstChild; node; node = node->pstNext)
4019 {
4020 if (node->enDataType == JSON_TYPE_OBJECT)
4021 {
4022 child = node->pstChild;
4023
4024 if (child->enDataType != JSON_TYPE_STRING)
4025 {
4026 continue;
4027 }
4028
4029 if (strcmp(child->pcName, "VTOY_DEFAULT_MENU_MODE") == 0)
4030 {
4031 CONTROL_PARSE_INT_DEF_0(child, data->default_menu_mode);
4032 }
4033 else if (strcmp(child->pcName, "VTOY_WIN11_BYPASS_CHECK") == 0)
4034 {
4035 CONTROL_PARSE_INT_DEF_0(child, data->win11_bypass_check);
4036 }
4037 else if (strcmp(child->pcName, "VTOY_LINUX_REMOUNT") == 0)
4038 {
4039 CONTROL_PARSE_INT_DEF_0(child, data->linux_remount);
4040 }
4041 else if (strcmp(child->pcName, "VTOY_SECONDARY_BOOT_MENU") == 0)
4042 {
4043 CONTROL_PARSE_INT_DEF_1(child, data->secondary_menu);
4044 }
4045 else if (strcmp(child->pcName, "VTOY_SHOW_PASSWORD_ASTERISK") == 0)
4046 {
4047 CONTROL_PARSE_INT_DEF_1(child, data->password_asterisk);
4048 }
4049 else if (strcmp(child->pcName, "VTOY_TREE_VIEW_MENU_STYLE") == 0)
4050 {
4051 CONTROL_PARSE_INT_DEF_0(child, data->treeview_style);
4052 }
4053 else if (strcmp(child->pcName, "VTOY_FILT_DOT_UNDERSCORE_FILE") == 0)
4054 {
4055 CONTROL_PARSE_INT_DEF_1(child, data->filter_dot_underscore);
4056 }
4057 else if (strcmp(child->pcName, "VTOY_SORT_CASE_SENSITIVE") == 0)
4058 {
4059 CONTROL_PARSE_INT_DEF_0(child, data->sort_casesensitive);
4060 }
4061 else if (strcmp(child->pcName, "VTOY_MAX_SEARCH_LEVEL") == 0)
4062 {
4063 if (strcmp(child->unData.pcStrVal, "max") == 0)
4064 {
4065 data->max_search_level = -1;
4066 }
4067 else
4068 {
4069 data->max_search_level = (int)strtol(child->unData.pcStrVal, NULL, 10);
4070 }
4071 }
4072 else if (strcmp(child->pcName, "VTOY_DEFAULT_SEARCH_ROOT") == 0)
4073 {
4074 strlcpy(data->default_search_root, child->unData.pcStrVal);
4075 }
4076 else if (strcmp(child->pcName, "VTOY_DEFAULT_IMAGE") == 0)
4077 {
4078 strlcpy(data->default_image, child->unData.pcStrVal);
4079 }
4080 else if (strcmp(child->pcName, "VTOY_DEFAULT_KBD_LAYOUT") == 0)
4081 {
4082 for (i = 0; g_ventoy_kbd_layout[i]; i++)
4083 {
4084 if (strcmp(child->unData.pcStrVal, g_ventoy_kbd_layout[i]) == 0)
4085 {
4086 strlcpy(data->default_kbd_layout, child->unData.pcStrVal);
4087 break;
4088 }
4089 }
4090 }
4091 else if (strcmp(child->pcName, "VTOY_HELP_TXT_LANGUAGE") == 0)
4092 {
4093 for (i = 0; g_ventoy_help_lang[i][0]; i++)
4094 {
4095 if (strcmp(child->unData.pcStrVal, g_ventoy_help_lang[i]) == 0)
4096 {
4097 strlcpy(data->help_text_language, child->unData.pcStrVal);
4098 break;
4099 }
4100 }
4101 }
4102 else if (strcmp(child->pcName, "VTOY_MENU_LANGUAGE") == 0)
4103 {
4104 for (i = 0; g_ventoy_menu_lang[i][0]; i++)
4105 {
4106 if (strcmp(child->unData.pcStrVal, g_ventoy_menu_lang[i]) == 0)
4107 {
4108 strlcpy(data->menu_language, child->unData.pcStrVal);
4109 break;
4110 }
4111 }
4112 }
4113 else if (strcmp(child->pcName, "VTOY_MENU_TIMEOUT") == 0)
4114 {
4115 data->menu_timeout = (int)strtol(child->unData.pcStrVal, NULL, 10);
4116 }
4117 else if (strcmp(child->pcName, "VTOY_SECONDARY_TIMEOUT") == 0)
4118 {
4119 data->secondary_menu_timeout = (int)strtol(child->unData.pcStrVal, NULL, 10);
4120 }
4121 else if (strcmp(child->pcName, "VTOY_VHD_NO_WARNING") == 0)
4122 {
4123 CONTROL_PARSE_INT_DEF_0(child, data->vhd_no_warning);
4124 }
4125 else if (strcmp(child->pcName, "VTOY_FILE_FLT_ISO") == 0)
4126 {
4127 CONTROL_PARSE_INT_DEF_0(child, data->filter_iso);
4128 }
4129 else if (strcmp(child->pcName, "VTOY_FILE_FLT_IMG") == 0)
4130 {
4131 CONTROL_PARSE_INT_DEF_0(child, data->filter_img);
4132 }
4133 else if (strcmp(child->pcName, "VTOY_FILE_FLT_EFI") == 0)
4134 {
4135 CONTROL_PARSE_INT_DEF_0(child, data->filter_efi);
4136 }
4137 else if (strcmp(child->pcName, "VTOY_FILE_FLT_WIM") == 0)
4138 {
4139 CONTROL_PARSE_INT_DEF_0(child, data->filter_wim);
4140 }
4141 else if (strcmp(child->pcName, "VTOY_FILE_FLT_VHD") == 0)
4142 {
4143 CONTROL_PARSE_INT_DEF_0(child, data->filter_vhd);
4144 }
4145 else if (strcmp(child->pcName, "VTOY_FILE_FLT_VTOY") == 0)
4146 {
4147 CONTROL_PARSE_INT_DEF_0(child, data->filter_vtoy);
4148 }
4149
4150 }
4151 }
4152
4153 return 0;
4154 }
4155 static int ventoy_parse_theme(VTOY_JSON *json, void *p)
4156 {
4157 const char *dismode = NULL;
4158 VTOY_JSON *child = NULL;
4159 VTOY_JSON *node = NULL;
4160 path_node *tail = NULL;
4161 path_node *pnode = NULL;
4162 data_theme *data = (data_theme *)p;
4163
4164 if (json->enDataType != JSON_TYPE_OBJECT)
4165 {
4166 return 0;
4167 }
4168
4169 child = json->pstChild;
4170
4171 dismode = vtoy_json_get_string_ex(child, "display_mode");
4172 vtoy_json_get_string(child, "ventoy_left", sizeof(data->ventoy_left), data->ventoy_left);
4173 vtoy_json_get_string(child, "ventoy_top", sizeof(data->ventoy_top), data->ventoy_top);
4174 vtoy_json_get_string(child, "ventoy_color", sizeof(data->ventoy_color), data->ventoy_color);
4175
4176 vtoy_json_get_int(child, "default_file", &(data->default_file));
4177 vtoy_json_get_string(child, "gfxmode", sizeof(data->gfxmode), data->gfxmode);
4178 vtoy_json_get_string(child, "serial_param", sizeof(data->serial_param), data->serial_param);
4179
4180 if (dismode)
4181 {
4182 if (strcmp(dismode, "CLI") == 0)
4183 {
4184 data->display_mode = display_mode_cli;
4185 }
4186 else if (strcmp(dismode, "serial") == 0)
4187 {
4188 data->display_mode = display_mode_serial;
4189 }
4190 else if (strcmp(dismode, "serial_console") == 0)
4191 {
4192 data->display_mode = display_mode_ser_console;
4193 }
4194 else
4195 {
4196 data->display_mode = display_mode_gui;
4197 }
4198 }
4199
4200 node = vtoy_json_find_item(child, JSON_TYPE_STRING, "file");
4201 if (node)
4202 {
4203 data->default_file = 0;
4204
4205 pnode = zalloc(sizeof(path_node));
4206 if (pnode)
4207 {
4208 strlcpy(pnode->path, node->unData.pcStrVal);
4209 data->filelist = pnode;
4210 }
4211 }
4212 else
4213 {
4214 node = vtoy_json_find_item(child, JSON_TYPE_ARRAY, "file");
4215 if (node)
4216 {
4217 for (node = node->pstChild; node; node = node->pstNext)
4218 {
4219 if (node->enDataType == JSON_TYPE_STRING)
4220 {
4221 pnode = zalloc(sizeof(path_node));
4222 if (pnode)
4223 {
4224 strlcpy(pnode->path, node->unData.pcStrVal);
4225 if (data->filelist)
4226 {
4227 tail->next = pnode;
4228 tail = pnode;
4229 }
4230 else
4231 {
4232 data->filelist = tail = pnode;
4233 }
4234 }
4235 }
4236 }
4237 }
4238 }
4239
4240
4241 node = vtoy_json_find_item(child, JSON_TYPE_ARRAY, "fonts");
4242 if (node)
4243 {
4244 for (node = node->pstChild; node; node = node->pstNext)
4245 {
4246 if (node->enDataType == JSON_TYPE_STRING)
4247 {
4248 pnode = zalloc(sizeof(path_node));
4249 if (pnode)
4250 {
4251 strlcpy(pnode->path, node->unData.pcStrVal);
4252 if (data->fontslist)
4253 {
4254 tail->next = pnode;
4255 tail = pnode;
4256 }
4257 else
4258 {
4259 data->fontslist = tail = pnode;
4260 }
4261 }
4262 }
4263 }
4264 }
4265
4266 return 0;
4267 }
4268 static int ventoy_parse_menu_alias(VTOY_JSON *json, void *p)
4269 {
4270 int type;
4271 const char *path = NULL;
4272 const char *alias = NULL;
4273 data_alias *data = (data_alias *)p;
4274 data_alias_node *tail = NULL;
4275 data_alias_node *pnode = NULL;
4276 VTOY_JSON *node = NULL;
4277
4278 if (json->enDataType != JSON_TYPE_ARRAY)
4279 {
4280 return 0;
4281 }
4282
4283 for (node = json->pstChild; node; node = node->pstNext)
4284 {
4285 if (node->enDataType != JSON_TYPE_OBJECT)
4286 {
4287 continue;
4288 }
4289
4290 type = path_type_file;
4291 path = vtoy_json_get_string_ex(node->pstChild, "image");
4292 if (!path)
4293 {
4294 path = vtoy_json_get_string_ex(node->pstChild, "dir");
4295 type = path_type_dir;
4296 }
4297 alias = vtoy_json_get_string_ex(node->pstChild, "alias");
4298
4299 if (path && alias)
4300 {
4301 pnode = zalloc(sizeof(data_alias_node));
4302 if (pnode)
4303 {
4304 pnode->type = type;
4305 strlcpy(pnode->path, path);
4306 strlcpy(pnode->alias, alias);
4307
4308 if (data->list)
4309 {
4310 tail->next = pnode;
4311 tail = pnode;
4312 }
4313 else
4314 {
4315 data->list = tail = pnode;
4316 }
4317 }
4318 }
4319 }
4320
4321 return 0;
4322 }
4323
4324 static int ventoy_parse_menu_tip(VTOY_JSON *json, void *p)
4325 {
4326 int type;
4327 const char *path = NULL;
4328 const char *tip = NULL;
4329 data_tip *data = (data_tip *)p;
4330 data_tip_node *tail = NULL;
4331 data_tip_node *pnode = NULL;
4332 VTOY_JSON *node = NULL;
4333 VTOY_JSON *tips = NULL;
4334
4335 if (json->enDataType != JSON_TYPE_OBJECT)
4336 {
4337 return 0;
4338 }
4339
4340 vtoy_json_get_string(json->pstChild, "left", sizeof(data->left), data->left);
4341 vtoy_json_get_string(json->pstChild, "top", sizeof(data->top), data->top);
4342 vtoy_json_get_string(json->pstChild, "color", sizeof(data->color), data->color);
4343
4344 tips = vtoy_json_find_item(json->pstChild, JSON_TYPE_ARRAY, "tips");
4345 if (!tips)
4346 {
4347 return 0;
4348 }
4349
4350 for (node = tips->pstChild; node; node = node->pstNext)
4351 {
4352 if (node->enDataType != JSON_TYPE_OBJECT)
4353 {
4354 continue;
4355 }
4356
4357 type = path_type_file;
4358 path = vtoy_json_get_string_ex(node->pstChild, "image");
4359 if (!path)
4360 {
4361 path = vtoy_json_get_string_ex(node->pstChild, "dir");
4362 type = path_type_dir;
4363 }
4364 tip = vtoy_json_get_string_ex(node->pstChild, "tip");
4365
4366 if (path && tip)
4367 {
4368 pnode = zalloc(sizeof(data_tip_node));
4369 if (pnode)
4370 {
4371 pnode->type = type;
4372 strlcpy(pnode->path, path);
4373 strlcpy(pnode->tip, tip);
4374
4375 if (data->list)
4376 {
4377 tail->next = pnode;
4378 tail = pnode;
4379 }
4380 else
4381 {
4382 data->list = tail = pnode;
4383 }
4384 }
4385 }
4386 }
4387
4388 return 0;
4389 }
4390 static int ventoy_parse_menu_class(VTOY_JSON *json, void *p)
4391 {
4392 int type;
4393 const char *path = NULL;
4394 const char *class = NULL;
4395 data_class *data = (data_class *)p;
4396 data_class_node *tail = NULL;
4397 data_class_node *pnode = NULL;
4398 VTOY_JSON *node = NULL;
4399
4400 if (json->enDataType != JSON_TYPE_ARRAY)
4401 {
4402 return 0;
4403 }
4404
4405 for (node = json->pstChild; node; node = node->pstNext)
4406 {
4407 if (node->enDataType != JSON_TYPE_OBJECT)
4408 {
4409 continue;
4410 }
4411
4412 type = class_type_key;
4413 path = vtoy_json_get_string_ex(node->pstChild, "key");
4414 if (!path)
4415 {
4416 type = class_type_dir;
4417 path = vtoy_json_get_string_ex(node->pstChild, "dir");
4418 if (!path)
4419 {
4420 type = class_type_parent;
4421 path = vtoy_json_get_string_ex(node->pstChild, "parent");
4422 }
4423 }
4424 class = vtoy_json_get_string_ex(node->pstChild, "class");
4425
4426 if (path && class)
4427 {
4428 pnode = zalloc(sizeof(data_class_node));
4429 if (pnode)
4430 {
4431 pnode->type = type;
4432 strlcpy(pnode->path, path);
4433 strlcpy(pnode->class, class);
4434
4435 if (data->list)
4436 {
4437 tail->next = pnode;
4438 tail = pnode;
4439 }
4440 else
4441 {
4442 data->list = tail = pnode;
4443 }
4444 }
4445 }
4446 }
4447
4448 return 0;
4449 }
4450 static int ventoy_parse_auto_install(VTOY_JSON *json, void *p)
4451 {
4452 int type;
4453 int count;
4454 int timeout;
4455 int timeouten;
4456 int autosel;
4457 int autoselen;
4458 const char *path = NULL;
4459 const char *file = NULL;
4460 data_auto_install *data = (data_auto_install *)p;
4461 auto_install_node *tail = NULL;
4462 auto_install_node *pnode = NULL;
4463 path_node *pathnode = NULL;
4464 path_node *pathtail = NULL;
4465 VTOY_JSON *node = NULL;
4466 VTOY_JSON *filelist = NULL;
4467 VTOY_JSON *filenode = NULL;
4468
4469 if (json->enDataType != JSON_TYPE_ARRAY)
4470 {
4471 return 0;
4472 }
4473
4474 for (node = json->pstChild; node; node = node->pstNext)
4475 {
4476 if (node->enDataType != JSON_TYPE_OBJECT)
4477 {
4478 continue;
4479 }
4480
4481 type = 0;
4482 path = vtoy_json_get_string_ex(node->pstChild, "image");
4483 if (!path)
4484 {
4485 path = vtoy_json_get_string_ex(node->pstChild, "parent");
4486 type = 1;
4487 }
4488 if (!path)
4489 {
4490 continue;
4491 }
4492
4493 file = vtoy_json_get_string_ex(node->pstChild, "template");
4494 if (file)
4495 {
4496 pnode = zalloc(sizeof(auto_install_node));
4497 if (pnode)
4498 {
4499 pnode->type = type;
4500 pnode->autosel = 1;
4501 strlcpy(pnode->path, path);
4502
4503 pathnode = zalloc(sizeof(path_node));
4504 if (pathnode)
4505 {
4506 strlcpy(pathnode->path, file);
4507 pnode->list = pathnode;
4508 }
4509 else
4510 {
4511 free(pnode);
4512 }
4513
4514 if (data->list)
4515 {
4516 tail->next = pnode;
4517 tail = pnode;
4518 }
4519 else
4520 {
4521 data->list = tail = pnode;
4522 }
4523 }
4524
4525 continue;
4526 }
4527
4528
4529 timeouten = autoselen = 0;
4530 if (JSON_SUCCESS == vtoy_json_get_int(node->pstChild, "timeout", &timeout))
4531 {
4532 timeouten = 1;
4533 }
4534 if (JSON_SUCCESS == vtoy_json_get_int(node->pstChild, "autosel", &autosel))
4535 {
4536 autoselen = 1;
4537 }
4538
4539 filelist = vtoy_json_find_item(node->pstChild, JSON_TYPE_ARRAY, "template");
4540 if (!filelist)
4541 {
4542 continue;
4543 }
4544
4545 pnode = zalloc(sizeof(auto_install_node));
4546 if (!pnode)
4547 {
4548 continue;
4549 }
4550
4551 pnode->type = type;
4552 pnode->autoselen = autoselen;
4553 pnode->timeouten = timeouten;
4554 pnode->timeout = timeout;
4555 pnode->autosel = autosel;
4556 strlcpy(pnode->path, path);
4557
4558 count = 0;
4559 for (filenode = filelist->pstChild; filenode; filenode = filenode->pstNext)
4560 {
4561 if (filenode->enDataType != JSON_TYPE_STRING)
4562 {
4563 continue;
4564 }
4565
4566 pathnode = zalloc(sizeof(path_node));
4567 if (pathnode)
4568 {
4569 count++;
4570 strlcpy(pathnode->path, filenode->unData.pcStrVal);
4571
4572 if (pnode->list)
4573 {
4574 pathtail->next = pathnode;
4575 pathtail = pathnode;
4576 }
4577 else
4578 {
4579 pnode->list = pathtail = pathnode;
4580 }
4581 }
4582 }
4583
4584 if (count == 0)
4585 {
4586 free(pnode);
4587 }
4588 else
4589 {
4590 if (pnode->autoselen && pnode->autosel > count)
4591 {
4592 pnode->autosel = 1;
4593 }
4594
4595 if (data->list)
4596 {
4597 tail->next = pnode;
4598 tail = pnode;
4599 }
4600 else
4601 {
4602 data->list = tail = pnode;
4603 }
4604 }
4605 }
4606
4607 return 0;
4608 }
4609 static int ventoy_parse_persistence(VTOY_JSON *json, void *p)
4610 {
4611 int count;
4612 int timeout;
4613 int timeouten;
4614 int autosel;
4615 int autoselen;
4616 const char *path = NULL;
4617 const char *file = NULL;
4618 data_persistence *data = (data_persistence *)p;
4619 persistence_node *tail = NULL;
4620 persistence_node *pnode = NULL;
4621 path_node *pathnode = NULL;
4622 path_node *pathtail = NULL;
4623 VTOY_JSON *node = NULL;
4624 VTOY_JSON *filelist = NULL;
4625 VTOY_JSON *filenode = NULL;
4626
4627 if (json->enDataType != JSON_TYPE_ARRAY)
4628 {
4629 return 0;
4630 }
4631
4632 for (node = json->pstChild; node; node = node->pstNext)
4633 {
4634 if (node->enDataType != JSON_TYPE_OBJECT)
4635 {
4636 continue;
4637 }
4638
4639 path = vtoy_json_get_string_ex(node->pstChild, "image");
4640 if (!path)
4641 {
4642 continue;
4643 }
4644
4645 file = vtoy_json_get_string_ex(node->pstChild, "backend");
4646 if (file)
4647 {
4648 pnode = zalloc(sizeof(persistence_node));
4649 if (pnode)
4650 {
4651 pnode->type = 0;
4652 pnode->autosel = 1;
4653 strlcpy(pnode->path, path);
4654
4655 pathnode = zalloc(sizeof(path_node));
4656 if (pathnode)
4657 {
4658 strlcpy(pathnode->path, file);
4659 pnode->list = pathnode;
4660 }
4661 else
4662 {
4663 free(pnode);
4664 }
4665
4666 if (data->list)
4667 {
4668 tail->next = pnode;
4669 tail = pnode;
4670 }
4671 else
4672 {
4673 data->list = tail = pnode;
4674 }
4675 }
4676
4677 continue;
4678 }
4679
4680
4681 timeouten = autoselen = 0;
4682 if (JSON_SUCCESS == vtoy_json_get_int(node->pstChild, "timeout", &timeout))
4683 {
4684 timeouten = 1;
4685 }
4686 if (JSON_SUCCESS == vtoy_json_get_int(node->pstChild, "autosel", &autosel))
4687 {
4688 autoselen = 1;
4689 }
4690
4691 filelist = vtoy_json_find_item(node->pstChild, JSON_TYPE_ARRAY, "backend");
4692 if (!filelist)
4693 {
4694 continue;
4695 }
4696
4697 pnode = zalloc(sizeof(persistence_node));
4698 if (!pnode)
4699 {
4700 continue;
4701 }
4702
4703 pnode->type = 0;
4704 pnode->autoselen = autoselen;
4705 pnode->timeouten = timeouten;
4706 pnode->timeout = timeout;
4707 pnode->autosel = autosel;
4708 strlcpy(pnode->path, path);
4709
4710 count = 0;
4711 for (filenode = filelist->pstChild; filenode; filenode = filenode->pstNext)
4712 {
4713 if (filenode->enDataType != JSON_TYPE_STRING)
4714 {
4715 continue;
4716 }
4717
4718 pathnode = zalloc(sizeof(path_node));
4719 if (pathnode)
4720 {
4721 count++;
4722 strlcpy(pathnode->path, filenode->unData.pcStrVal);
4723
4724 if (pnode->list)
4725 {
4726 pathtail->next = pathnode;
4727 pathtail = pathnode;
4728 }
4729 else
4730 {
4731 pnode->list = pathtail = pathnode;
4732 }
4733 }
4734 }
4735
4736 if (count == 0)
4737 {
4738 free(pnode);
4739 }
4740 else
4741 {
4742 if (pnode->autoselen && pnode->autosel > count)
4743 {
4744 pnode->autosel = 1;
4745 }
4746
4747 if (data->list)
4748 {
4749 tail->next = pnode;
4750 tail = pnode;
4751 }
4752 else
4753 {
4754 data->list = tail = pnode;
4755 }
4756 }
4757 }
4758
4759 return 0;
4760 }
4761 static int ventoy_parse_injection(VTOY_JSON *json, void *p)
4762 {
4763 int type;
4764 const char *path = NULL;
4765 const char *archive = NULL;
4766 data_injection *data = (data_injection *)p;
4767 injection_node *tail = NULL;
4768 injection_node *pnode = NULL;
4769 VTOY_JSON *node = NULL;
4770
4771 if (json->enDataType != JSON_TYPE_ARRAY)
4772 {
4773 return 0;
4774 }
4775
4776 for (node = json->pstChild; node; node = node->pstNext)
4777 {
4778 if (node->enDataType != JSON_TYPE_OBJECT)
4779 {
4780 continue;
4781 }
4782
4783 type = 0;
4784 path = vtoy_json_get_string_ex(node->pstChild, "image");
4785 if (!path)
4786 {
4787 path = vtoy_json_get_string_ex(node->pstChild, "parent");
4788 type = 1;
4789 }
4790 archive = vtoy_json_get_string_ex(node->pstChild, "archive");
4791
4792 if (path && archive)
4793 {
4794 pnode = zalloc(sizeof(injection_node));
4795 if (pnode)
4796 {
4797 pnode->type = type;
4798 strlcpy(pnode->path, path);
4799 strlcpy(pnode->archive, archive);
4800
4801 if (data->list)
4802 {
4803 tail->next = pnode;
4804 tail = pnode;
4805 }
4806 else
4807 {
4808 data->list = tail = pnode;
4809 }
4810 }
4811 }
4812 }
4813
4814 return 0;
4815 }
4816 static int ventoy_parse_conf_replace(VTOY_JSON *json, void *p)
4817 {
4818 int img = 0;
4819 const char *path = NULL;
4820 const char *org = NULL;
4821 const char *new = NULL;
4822 data_conf_replace *data = (data_conf_replace *)p;
4823 conf_replace_node *tail = NULL;
4824 conf_replace_node *pnode = NULL;
4825 VTOY_JSON *node = NULL;
4826
4827 if (json->enDataType != JSON_TYPE_ARRAY)
4828 {
4829 return 0;
4830 }
4831
4832 for (node = json->pstChild; node; node = node->pstNext)
4833 {
4834 if (node->enDataType != JSON_TYPE_OBJECT)
4835 {
4836 continue;
4837 }
4838
4839 path = vtoy_json_get_string_ex(node->pstChild, "iso");
4840 org = vtoy_json_get_string_ex(node->pstChild, "org");
4841 new = vtoy_json_get_string_ex(node->pstChild, "new");
4842
4843 img = 0;
4844 vtoy_json_get_int(node->pstChild, "img", &img);
4845
4846 if (path && org && new)
4847 {
4848 pnode = zalloc(sizeof(conf_replace_node));
4849 if (pnode)
4850 {
4851 strlcpy(pnode->path, path);
4852 strlcpy(pnode->org, org);
4853 strlcpy(pnode->new, new);
4854 if (img == 1)
4855 {
4856 pnode->image = img;
4857 }
4858
4859 if (data->list)
4860 {
4861 tail->next = pnode;
4862 tail = pnode;
4863 }
4864 else
4865 {
4866 data->list = tail = pnode;
4867 }
4868 }
4869 }
4870 }
4871
4872 return 0;
4873 }
4874 static int ventoy_parse_password(VTOY_JSON *json, void *p)
4875 {
4876 int type;
4877 const char *bootpwd = NULL;
4878 const char *isopwd= NULL;
4879 const char *wimpwd= NULL;
4880 const char *imgpwd= NULL;
4881 const char *efipwd= NULL;
4882 const char *vhdpwd= NULL;
4883 const char *vtoypwd= NULL;
4884 const char *path = NULL;
4885 const char *pwd = NULL;
4886 data_password *data = (data_password *)p;
4887 menu_password *tail = NULL;
4888 menu_password *pnode = NULL;
4889 VTOY_JSON *node = NULL;
4890 VTOY_JSON *menupwd = NULL;
4891
4892 if (json->enDataType != JSON_TYPE_OBJECT)
4893 {
4894 return 0;
4895 }
4896
4897 bootpwd = vtoy_json_get_string_ex(json->pstChild, "bootpwd");
4898 isopwd = vtoy_json_get_string_ex(json->pstChild, "isopwd");
4899 wimpwd = vtoy_json_get_string_ex(json->pstChild, "wimpwd");
4900 imgpwd = vtoy_json_get_string_ex(json->pstChild, "imgpwd");
4901 efipwd = vtoy_json_get_string_ex(json->pstChild, "efipwd");
4902 vhdpwd = vtoy_json_get_string_ex(json->pstChild, "vhdpwd");
4903 vtoypwd = vtoy_json_get_string_ex(json->pstChild, "vtoypwd");
4904
4905
4906 if (bootpwd) strlcpy(data->bootpwd, bootpwd);
4907 if (isopwd) strlcpy(data->isopwd, isopwd);
4908 if (wimpwd) strlcpy(data->wimpwd, wimpwd);
4909 if (imgpwd) strlcpy(data->imgpwd, imgpwd);
4910 if (efipwd) strlcpy(data->efipwd, efipwd);
4911 if (vhdpwd) strlcpy(data->vhdpwd, vhdpwd);
4912 if (vtoypwd) strlcpy(data->vtoypwd, vtoypwd);
4913
4914
4915 menupwd = vtoy_json_find_item(json->pstChild, JSON_TYPE_ARRAY, "menupwd");
4916 if (!menupwd)
4917 {
4918 return 0;
4919 }
4920
4921 for (node = menupwd->pstChild; node; node = node->pstNext)
4922 {
4923 if (node->enDataType != JSON_TYPE_OBJECT)
4924 {
4925 continue;
4926 }
4927
4928 type = 0;
4929 path = vtoy_json_get_string_ex(node->pstChild, "file");
4930 if (!path)
4931 {
4932 path = vtoy_json_get_string_ex(node->pstChild, "parent");
4933 type = 1;
4934 }
4935 pwd = vtoy_json_get_string_ex(node->pstChild, "pwd");
4936
4937 if (path && pwd)
4938 {
4939 pnode = zalloc(sizeof(menu_password));
4940 if (pnode)
4941 {
4942 pnode->type = type;
4943 strlcpy(pnode->path, path);
4944 strlcpy(pnode->pwd, pwd);
4945
4946 if (data->list)
4947 {
4948 tail->next = pnode;
4949 tail = pnode;
4950 }
4951 else
4952 {
4953 data->list = tail = pnode;
4954 }
4955 }
4956 }
4957 }
4958
4959 return 0;
4960 }
4961
4962 static int ventoy_parse_image_list_real(VTOY_JSON *json, int type, void *p)
4963 {
4964 VTOY_JSON *node = NULL;
4965 data_image_list *data = (data_image_list *)p;
4966 path_node *tail = NULL;
4967 path_node *pnode = NULL;
4968
4969 if (json->enDataType != JSON_TYPE_ARRAY)
4970 {
4971 return 0;
4972 }
4973
4974 data->type = type;
4975
4976 for (node = json->pstChild; node; node = node->pstNext)
4977 {
4978 if (node->enDataType == JSON_TYPE_STRING)
4979 {
4980 pnode = zalloc(sizeof(path_node));
4981 if (pnode)
4982 {
4983 strlcpy(pnode->path, node->unData.pcStrVal);
4984 if (data->list)
4985 {
4986 tail->next = pnode;
4987 tail = pnode;
4988 }
4989 else
4990 {
4991 data->list = tail = pnode;
4992 }
4993 }
4994 }
4995 }
4996
4997 return 0;
4998 }
4999 static int ventoy_parse_image_blacklist(VTOY_JSON *json, void *p)
5000 {
5001 return ventoy_parse_image_list_real(json, 1, p);
5002 }
5003 static int ventoy_parse_image_list(VTOY_JSON *json, void *p)
5004 {
5005 return ventoy_parse_image_list_real(json, 0, p);
5006 }
5007
5008 static int ventoy_parse_auto_memdisk(VTOY_JSON *json, void *p)
5009 {
5010 VTOY_JSON *node = NULL;
5011 data_auto_memdisk *data = (data_auto_memdisk *)p;
5012 path_node *tail = NULL;
5013 path_node *pnode = NULL;
5014
5015 if (json->enDataType != JSON_TYPE_ARRAY)
5016 {
5017 return 0;
5018 }
5019
5020 for (node = json->pstChild; node; node = node->pstNext)
5021 {
5022 if (node->enDataType == JSON_TYPE_STRING)
5023 {
5024 pnode = zalloc(sizeof(path_node));
5025 if (pnode)
5026 {
5027 strlcpy(pnode->path, node->unData.pcStrVal);
5028 if (data->list)
5029 {
5030 tail->next = pnode;
5031 tail = pnode;
5032 }
5033 else
5034 {
5035 data->list = tail = pnode;
5036 }
5037 }
5038 }
5039 }
5040
5041 return 0;
5042 }
5043 static int ventoy_parse_dud(VTOY_JSON *json, void *p)
5044 {
5045 int count = 0;
5046 const char *path = NULL;
5047 const char *file = NULL;
5048 data_dud *data = (data_dud *)p;
5049 dud_node *tail = NULL;
5050 dud_node *pnode = NULL;
5051 path_node *pathnode = NULL;
5052 path_node *pathtail = NULL;
5053 VTOY_JSON *node = NULL;
5054 VTOY_JSON *filelist = NULL;
5055 VTOY_JSON *filenode = NULL;
5056
5057 if (json->enDataType != JSON_TYPE_ARRAY)
5058 {
5059 return 0;
5060 }
5061
5062 for (node = json->pstChild; node; node = node->pstNext)
5063 {
5064 if (node->enDataType != JSON_TYPE_OBJECT)
5065 {
5066 continue;
5067 }
5068
5069 path = vtoy_json_get_string_ex(node->pstChild, "image");
5070 if (!path)
5071 {
5072 continue;
5073 }
5074
5075 file = vtoy_json_get_string_ex(node->pstChild, "dud");
5076 if (file)
5077 {
5078 pnode = zalloc(sizeof(dud_node));
5079 if (pnode)
5080 {
5081 strlcpy(pnode->path, path);
5082
5083 pathnode = zalloc(sizeof(path_node));
5084 if (pathnode)
5085 {
5086 strlcpy(pathnode->path, file);
5087 pnode->list = pathnode;
5088 }
5089 else
5090 {
5091 free(pnode);
5092 }
5093
5094 if (data->list)
5095 {
5096 tail->next = pnode;
5097 tail = pnode;
5098 }
5099 else
5100 {
5101 data->list = tail = pnode;
5102 }
5103 }
5104
5105 continue;
5106 }
5107
5108 filelist = vtoy_json_find_item(node->pstChild, JSON_TYPE_ARRAY, "dud");
5109 if (!filelist)
5110 {
5111 continue;
5112 }
5113
5114 pnode = zalloc(sizeof(dud_node));
5115 if (!pnode)
5116 {
5117 continue;
5118 }
5119
5120 strlcpy(pnode->path, path);
5121
5122 for (filenode = filelist->pstChild; filenode; filenode = filenode->pstNext)
5123 {
5124 if (filenode->enDataType != JSON_TYPE_STRING)
5125 {
5126 continue;
5127 }
5128
5129 pathnode = zalloc(sizeof(path_node));
5130 if (pathnode)
5131 {
5132 strlcpy(pathnode->path, filenode->unData.pcStrVal);
5133 count++;
5134
5135 if (pnode->list)
5136 {
5137 pathtail->next = pathnode;
5138 pathtail = pathnode;
5139 }
5140 else
5141 {
5142 pnode->list = pathtail = pathnode;
5143 }
5144 }
5145 }
5146
5147 if (count == 0)
5148 {
5149 free(pnode);
5150 }
5151 else
5152 {
5153 if (data->list)
5154 {
5155 tail->next = pnode;
5156 tail = pnode;
5157 }
5158 else
5159 {
5160 data->list = tail = pnode;
5161 }
5162 }
5163 }
5164
5165 return 0;
5166 }
5167
5168
5169
5170 #if 0
5171 #endif
5172
5173
5174 static int ventoy_load_old_json(const char *filename)
5175 {
5176 int ret = 0;
5177 int offset = 0;
5178 int buflen = 0;
5179 char *buffer = NULL;
5180 unsigned char *start = NULL;
5181 VTOY_JSON *json = NULL;
5182 VTOY_JSON *node = NULL;
5183 VTOY_JSON *next = NULL;
5184
5185 ret = ventoy_read_file_to_buf(filename, 4, (void **)&buffer, &buflen);
5186 if (ret)
5187 {
5188 vlog("Failed to read old ventoy.json file.\n");
5189 return 1;
5190 }
5191 buffer[buflen] = 0;
5192
5193 start = (unsigned char *)buffer;
5194
5195 if (start[0] == 0xef && start[1] == 0xbb && start[2] == 0xbf)
5196 {
5197 offset = 3;
5198 }
5199 else if ((start[0] == 0xff && start[1] == 0xfe) || (start[0] == 0xfe && start[1] == 0xff))
5200 {
5201 vlog("ventoy.json is in UCS-2 encoding, ignore it.\n");
5202 free(buffer);
5203 return 1;
5204 }
5205
5206 json = vtoy_json_create();
5207 if (!json)
5208 {
5209 free(buffer);
5210 return 1;
5211 }
5212
5213 if (vtoy_json_parse_ex(json, buffer + offset, buflen - offset) == JSON_SUCCESS)
5214 {
5215 vlog("parse ventoy.json success\n");
5216
5217 for (node = json->pstChild; node; node = node->pstNext)
5218 for (next = node->pstNext; next; next = next->pstNext)
5219 {
5220 if (node->pcName && next->pcName && strcmp(node->pcName, next->pcName) == 0)
5221 {
5222 vlog("ventoy.json contains duplicate key <%s>.\n", node->pcName);
5223 g_sysinfo.invalid_config = 1;
5224 ret = 1;
5225 goto end;
5226 }
5227 }
5228
5229 for (node = json->pstChild; node; node = node->pstNext)
5230 {
5231 ventoy_parse_json(control);
5232 ventoy_parse_json(theme);
5233 ventoy_parse_json(menu_alias);
5234 ventoy_parse_json(menu_tip);
5235 ventoy_parse_json(menu_class);
5236 ventoy_parse_json(auto_install);
5237 ventoy_parse_json(persistence);
5238 ventoy_parse_json(injection);
5239 ventoy_parse_json(conf_replace);
5240 ventoy_parse_json(password);
5241 ventoy_parse_json(image_list);
5242 ventoy_parse_json(image_blacklist);
5243 ventoy_parse_json(auto_memdisk);
5244 ventoy_parse_json(dud);
5245 }
5246 }
5247 else
5248 {
5249 vlog("ventoy.json has syntax error.\n");
5250 g_sysinfo.syntax_error = 1;
5251 ret = 1;
5252 }
5253
5254 end:
5255 vtoy_json_destroy(json);
5256
5257 free(buffer);
5258 return ret;
5259 }
5260
5261
5262 int ventoy_http_start(const char *ip, const char *port)
5263 {
5264 int i = 0;
5265 int ret = 0;
5266 char addr[128];
5267 char filename[128];
5268 char backupname[128];
5269 struct mg_callbacks callbacks;
5270 const char *options[] =
5271 {
5272 "listening_ports", "24681",
5273 "document_root", "www",
5274 "index_files", "index.html",
5275 "num_threads", "16",
5276 "error_log_file", LOG_FILE,
5277 "request_timeout_ms", "10000",
5278 NULL
5279 };
5280
5281 for (i = 0; i <= bios_max; i++)
5282 {
5283 ventoy_data_default_control(g_data_control + i);
5284 ventoy_data_default_theme(g_data_theme + i);
5285 ventoy_data_default_menu_alias(g_data_menu_alias + i);
5286 ventoy_data_default_menu_class(g_data_menu_class + i);
5287 ventoy_data_default_menu_tip(g_data_menu_tip + i);
5288 ventoy_data_default_auto_install(g_data_auto_install + i);
5289 ventoy_data_default_persistence(g_data_persistence + i);
5290 ventoy_data_default_injection(g_data_injection + i);
5291 ventoy_data_default_conf_replace(g_data_conf_replace + i);
5292 ventoy_data_default_password(g_data_password + i);
5293 ventoy_data_default_image_list(g_data_image_list + i);
5294 ventoy_data_default_auto_memdisk(g_data_auto_memdisk + i);
5295 ventoy_data_default_dud(g_data_dud + i);
5296 }
5297
5298 ventoy_get_json_path(filename, backupname);
5299 if (ventoy_is_file_exist("%s", filename))
5300 {
5301 ventoy_copy_file(filename, backupname);
5302 ret = ventoy_load_old_json(filename);
5303 if (ret == 0)
5304 {
5305 ventoy_data_real_save_all(0);
5306 }
5307 }
5308
5309 /* option */
5310 scnprintf(addr, sizeof(addr), "%s:%s", ip, port);
5311 options[1] = addr;
5312
5313 memset(&callbacks, 0, sizeof(callbacks));
5314 callbacks.begin_request = ventoy_request_handler;
5315 #ifndef VENTOY_SIM
5316 callbacks.open_file = ventoy_web_openfile;
5317 #endif
5318 g_ventoy_http_ctx = mg_start(&callbacks, NULL, options);
5319
5320 ventoy_start_writeback_thread(ventoy_http_writeback);
5321
5322 return g_ventoy_http_ctx ? 0 : 1;
5323 }
5324
5325 int ventoy_http_stop(void)
5326 {
5327 if (g_ventoy_http_ctx)
5328 {
5329 mg_stop(g_ventoy_http_ctx);
5330 }
5331
5332 ventoy_stop_writeback_thread();
5333 return 0;
5334 }
5335
5336 int ventoy_http_init(void)
5337 {
5338 int i = 0;
5339
5340 #ifdef VENTOY_SIM
5341 char *Buffer = NULL;
5342 int BufLen = 0;
5343
5344 ventoy_read_file_to_buf("www/helplist", 4, (void **)&Buffer, &BufLen);
5345 if (Buffer)
5346 {
5347 for (i = 0; i < BufLen / 5; i++)
5348 {
5349 memcpy(g_ventoy_help_lang[i], Buffer + i * 5, 5);
5350 g_ventoy_help_lang[i][5] = 0;
5351 }
5352 free(Buffer);
5353 }
5354
5355 Buffer = NULL;
5356 BufLen = 0;
5357 ventoy_read_file_to_buf("www/menulist", 4, (void **)&Buffer, &BufLen);
5358 if (Buffer)
5359 {
5360 for (i = 0; i < BufLen / 5; i++)
5361 {
5362 memcpy(g_ventoy_menu_lang[i], Buffer + i * 5, 5);
5363 g_ventoy_menu_lang[i][5] = 0;
5364 }
5365 free(Buffer);
5366 }
5367 #else
5368 ventoy_file *file;
5369 file = ventoy_tar_find_file("www/helplist");
5370 if (file)
5371 {
5372 for (i = 0; i < file->size / 5; i++)
5373 {
5374 memcpy(g_ventoy_help_lang[i], (char *)(file->addr) + i * 5, 5);
5375 g_ventoy_help_lang[i][5] = 0;
5376 }
5377 }
5378
5379 file = ventoy_tar_find_file("www/menulist");
5380 if (file)
5381 {
5382 for (i = 0; i < file->size / 5; i++)
5383 {
5384 memcpy(g_ventoy_menu_lang[i], (char *)(file->addr) + i * 5, 5);
5385 g_ventoy_menu_lang[i][5] = 0;
5386 }
5387 }
5388 #endif
5389
5390 if (!g_pub_json_buffer)
5391 {
5392 g_pub_json_buffer = malloc(JSON_BUF_MAX * 2);
5393 g_pub_save_buffer = g_pub_json_buffer + JSON_BUF_MAX;
5394 }
5395
5396
5397 pthread_mutex_init(&g_api_mutex, NULL);
5398 return 0;
5399 }
5400
5401 void ventoy_http_exit(void)
5402 {
5403 check_free(g_pub_json_buffer);
5404 g_pub_json_buffer = NULL;
5405 g_pub_save_buffer = NULL;
5406
5407 pthread_mutex_destroy(&g_api_mutex);
5408 }
5409
5410