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