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