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