VentoyPlugson Update:

1. Add preview json feature
2. Add reset button
3. Add configuration active identifier for each tab
4. Bug fix
This commit is contained in:
longpanda
2022-10-21 16:06:13 +08:00
parent c87ad1d734
commit 3f09fb9a23
25 changed files with 757 additions and 163 deletions

View File

@@ -311,12 +311,24 @@ uint64_t ventoy_get_disk_size_in_byte(const char *disk)
int ventoy_get_disk_vendor(const char *name, char *vendorbuf, int bufsize)
{
return ventoy_get_sys_file_line(vendorbuf, bufsize, "/sys/block/%s/device/vendor", name);
if (strncmp(name, "loop", 4) == 0)
{
scnprintf(vendorbuf, bufsize, "Local");
return 0;
}
return ventoy_get_sys_file_line(vendorbuf, bufsize, "/sys/block/%s/device/vendor", name);
}
int ventoy_get_disk_model(const char *name, char *modelbuf, int bufsize)
{
return ventoy_get_sys_file_line(modelbuf, bufsize, "/sys/block/%s/device/model", name);
if (strncmp(name, "loop", 4) == 0)
{
scnprintf(modelbuf, bufsize, "Loop Device");
return 0;
}
return ventoy_get_sys_file_line(modelbuf, bufsize, "/sys/block/%s/device/model", name);
}
static int fatlib_media_sector_read(uint32 sector, uint8 *buffer, uint32 sector_count)
@@ -576,9 +588,9 @@ int ventoy_get_disk_info(char **argv)
char model[128];
char *disk = argv[4];
if (strncmp(argv[4], "/dev/", 4) == 0)
if (strncmp(argv[4], "/dev/", 5) == 0)
{
disk += 4;
disk += 5;
}
ventoy_get_disk_vendor(disk, vendor, sizeof(vendor));
ventoy_get_disk_model(disk, model, sizeof(model));

View File

@@ -152,6 +152,9 @@ typedef struct tagVTOY_JSON
#define VTOY_JSON_FMT_ITEM_LN(P, Item) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\",\n", P, (Item))
#define VTOY_JSON_FMT_ITEM_PATH_LN(P, Item) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\",\n", P, ventoy_real_path(Item))
#define VTOY_JSON_FMT_ITEM_INT(Item) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%d,", (Item))
#define VTOY_JSON_FMT_COMA() ssprintf(__uiCurPos, __pcBuf, __uiBufLen, ",")
#define VTOY_JSON_FMT_COMA_N(cnt) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, ",\n")
#define VTOY_JSON_FMT_COMA_N_CNT(cnt) if ((cnt) > 0) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, ",\n")

View File

@@ -257,4 +257,48 @@ if (backup)
#endif
}
static const char g_encoding_table[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', '+', '/'};
char * ventoy_base64_encode(const char *data, int input_length, int *output_length)
{
int i = 0;
int j = 0;
char *encoded_data = NULL;
int mod_table[] = {0, 2, 1};
*output_length = 4 * ((input_length + 2) / 3);
encoded_data = malloc(*output_length + 4);
if (!encoded_data)
{
return NULL;
}
while (i < input_length)
{
unsigned int octet_a = i < input_length ? (unsigned char)data[i++] : 0;
unsigned int octet_b = i < input_length ? (unsigned char)data[i++] : 0;
unsigned int octet_c = i < input_length ? (unsigned char)data[i++] : 0;
unsigned int triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;
encoded_data[j++] = g_encoding_table[(triple >> 3 * 6) & 0x3F];
encoded_data[j++] = g_encoding_table[(triple >> 2 * 6) & 0x3F];
encoded_data[j++] = g_encoding_table[(triple >> 1 * 6) & 0x3F];
encoded_data[j++] = g_encoding_table[(triple >> 0 * 6) & 0x3F];
}
for (i = 0; i < mod_table[input_length % 3]; i++)
{
encoded_data[*output_length - 1 - i] = '=';
}
return encoded_data;
}

View File

@@ -199,6 +199,7 @@ extern unsigned char *g_unxz_buffer;
extern int g_unxz_len;
void unxz_error(char *x);
int unxz_flush(void *src, unsigned int size);
char * ventoy_base64_encode(const char *data, int input_length, int *output_length);
#endif /* __VENTOY_UTIL_H__ */

View File

@@ -33,6 +33,7 @@
#include <linux/fs.h>
#include <dirent.h>
#include <time.h>
#include <semaphore.h>
#include <ventoy_define.h>
#include <ventoy_util.h>
@@ -234,30 +235,18 @@ int ventoy_write_buf_to_file(const char *FileName, void *Bufer, int BufLen)
return 0;
}
static sem_t g_writeback_sem;
static volatile int g_thread_stop = 0;
static pthread_t g_writeback_thread;
static pthread_mutex_t g_writeback_mutex;
static pthread_cond_t g_writeback_cond;
static void * ventoy_local_thread_run(void* data)
{
ventoy_http_writeback_pf callback = (ventoy_http_writeback_pf)data;
while (1)
while (0 == g_thread_stop)
{
pthread_mutex_lock(&g_writeback_mutex);
pthread_cond_wait(&g_writeback_cond, &g_writeback_mutex);
if (g_thread_stop)
{
pthread_mutex_unlock(&g_writeback_mutex);
break;
}
else
{
callback();
pthread_mutex_unlock(&g_writeback_mutex);
}
sem_wait(&g_writeback_sem);
callback();
}
return NULL;
@@ -265,15 +254,14 @@ static void * ventoy_local_thread_run(void* data)
void ventoy_set_writeback_event(void)
{
pthread_cond_signal(&g_writeback_cond);
sem_post(&g_writeback_sem);
}
int ventoy_start_writeback_thread(ventoy_http_writeback_pf callback)
{
g_thread_stop = 0;
pthread_mutex_init(&g_writeback_mutex, NULL);
pthread_cond_init(&g_writeback_cond, NULL);
sem_init(&g_writeback_sem, 0, 0);
pthread_create(&g_writeback_thread, NULL, ventoy_local_thread_run, callback);
return 0;
@@ -282,13 +270,10 @@ int ventoy_start_writeback_thread(ventoy_http_writeback_pf callback)
void ventoy_stop_writeback_thread(void)
{
g_thread_stop = 1;
pthread_cond_signal(&g_writeback_cond);
sem_post(&g_writeback_sem);
pthread_join(g_writeback_thread, NULL);
pthread_cond_destroy(&g_writeback_cond);
pthread_mutex_destroy(&g_writeback_mutex);
sem_destroy(&g_writeback_sem);
}

View File

@@ -60,6 +60,17 @@ static const char *g_ventoy_kbd_layout[] =
NULL
};
#define VTOY_DEL_ALL_PATH "4119ae33-98ea-448e-b9c0-569aafcf1fb4"
static int g_json_exist[plugin_type_max][bios_max];
static const char *g_plugin_name[plugin_type_max] =
{
"control", "theme", "menu_alias", "menu_tip",
"menu_class", "auto_install", "persistence", "injection",
"conf_replace", "password", "image_list",
"auto_memdisk", "dud"
};
static char g_ventoy_help_lang[MAX_LANGUAGE][8];
static char g_pub_path[2 * MAX_PATH];
@@ -341,7 +352,10 @@ static int ventoy_api_sysinfo(struct mg_connection *conn, VTOY_JSON *json)
static int ventoy_api_handshake(struct mg_connection *conn, VTOY_JSON *json)
{
int i = 0;
int j = 0;
int pos = 0;
char key[128];
(void)json;
@@ -350,6 +364,19 @@ static int ventoy_api_handshake(struct mg_connection *conn, VTOY_JSON *json)
VTOY_JSON_FMT_SINT("status", 0);
VTOY_JSON_FMT_SINT("save_error", g_sysinfo.config_save_error);
g_sysinfo.config_save_error = 0;
for (i = 0; i < plugin_type_max; i++)
{
scnprintf(key, sizeof(key), "exist_%s", g_plugin_name[i]);
VTOY_JSON_FMT_KEY(key);
VTOY_JSON_FMT_ARY_BEGIN();
for (j = 0; j < bios_max; j++)
{
VTOY_JSON_FMT_ITEM_INT(g_json_exist[i][j]);
}
VTOY_JSON_FMT_ARY_ENDEX();
}
VTOY_JSON_FMT_OBJ_END();
VTOY_JSON_FMT_END(pos);
@@ -718,7 +745,7 @@ int ventoy_data_cmp_theme(data_theme *data1, data_theme *data2)
{
return 1;
}
if (ventoy_path_list_cmp(data1->fontslist, data2->fontslist))
{
return 1;
@@ -927,7 +954,14 @@ static int ventoy_api_theme_del_file(struct mg_connection *conn, VTOY_JSON *json
path = VTOY_JSON_STR_EX("path");
if (path)
{
vtoy_list_del(last, node, data->filelist, path);
if (strcmp(path, VTOY_DEL_ALL_PATH) == 0)
{
vtoy_list_free(path_node, data->filelist);
}
else
{
vtoy_list_del(last, node, data->filelist, path);
}
}
ret = ventoy_data_save_all();
@@ -982,7 +1016,14 @@ static int ventoy_api_theme_del_font(struct mg_connection *conn, VTOY_JSON *json
path = VTOY_JSON_STR_EX("path");
if (path)
{
vtoy_list_del(last, node, data->fontslist, path);
if (strcmp(path, VTOY_DEL_ALL_PATH) == 0)
{
vtoy_list_free(path_node, data->fontslist);
}
else
{
vtoy_list_del(last, node, data->fontslist, path);
}
}
ret = ventoy_data_save_all();
@@ -1179,7 +1220,14 @@ static int ventoy_api_alias_del(struct mg_connection *conn, VTOY_JSON *json)
path = VTOY_JSON_STR_EX("path");
if (path)
{
vtoy_list_del(last, node, data->list, path);
if (strcmp(path, VTOY_DEL_ALL_PATH) == 0)
{
vtoy_list_free(data_alias_node, data->list);
}
else
{
vtoy_list_del(last, node, data->list, path);
}
}
ret = ventoy_data_save_all();
@@ -1417,7 +1465,14 @@ static int ventoy_api_tip_del(struct mg_connection *conn, VTOY_JSON *json)
path = VTOY_JSON_STR_EX("path");
if (path)
{
vtoy_list_del(last, node, data->list, path);
if (strcmp(path, VTOY_DEL_ALL_PATH) == 0)
{
vtoy_list_free(data_tip_node, data->list);
}
else
{
vtoy_list_del(last, node, data->list, path);
}
}
ret = ventoy_data_save_all();
@@ -1620,7 +1675,14 @@ static int ventoy_api_class_del(struct mg_connection *conn, VTOY_JSON *json)
path = VTOY_JSON_STR_EX("path");
if (path)
{
vtoy_list_del(last, node, data->list, path);
if (strcmp(path, VTOY_DEL_ALL_PATH) == 0)
{
vtoy_list_free(data_class_node, data->list);
}
else
{
vtoy_list_del(last, node, data->list, path);
}
}
ret = ventoy_data_save_all();
@@ -1749,7 +1811,14 @@ static int ventoy_api_auto_memdisk_del(struct mg_connection *conn, VTOY_JSON *js
path = VTOY_JSON_STR_EX("path");
if (path)
{
vtoy_list_del(last, node, data->list, path);
if (strcmp(path, VTOY_DEL_ALL_PATH) == 0)
{
vtoy_list_free(path_node, data->list);
}
else
{
vtoy_list_del(last, node, data->list, path);
}
}
ret = ventoy_data_save_all();
@@ -1924,7 +1993,14 @@ static int ventoy_api_image_list_del(struct mg_connection *conn, VTOY_JSON *json
path = VTOY_JSON_STR_EX("path");
if (path)
{
vtoy_list_del(last, node, data->list, path);
if (strcmp(path, VTOY_DEL_ALL_PATH) == 0)
{
vtoy_list_free(path_node, data->list);
}
else
{
vtoy_list_del(last, node, data->list, path);
}
}
ret = ventoy_data_save_all();
@@ -2175,7 +2251,14 @@ static int ventoy_api_password_del(struct mg_connection *conn, VTOY_JSON *json)
path = VTOY_JSON_STR_EX("path");
if (path)
{
vtoy_list_del(last, node, data->list, path);
if (strcmp(path, VTOY_DEL_ALL_PATH) == 0)
{
vtoy_list_free(menu_password, data->list);
}
else
{
vtoy_list_del(last, node, data->list, path);
}
}
ret = ventoy_data_save_all();
@@ -2366,7 +2449,14 @@ static int ventoy_api_conf_replace_del(struct mg_connection *conn, VTOY_JSON *js
path = VTOY_JSON_STR_EX("path");
if (path)
{
vtoy_list_del(last, node, data->list, path);
if (strcmp(path, VTOY_DEL_ALL_PATH) == 0)
{
vtoy_list_free(conf_replace_node, data->list);
}
else
{
vtoy_list_del(last, node, data->list, path);
}
}
ret = ventoy_data_save_all();
@@ -2555,6 +2645,7 @@ static int ventoy_api_dud_del(struct mg_connection *conn, VTOY_JSON *json)
int ret;
int index = 0;
const char *path = NULL;
dud_node *next = NULL;
dud_node *last = NULL;
dud_node *node = NULL;
data_dud *data = NULL;
@@ -2565,7 +2656,20 @@ static int ventoy_api_dud_del(struct mg_connection *conn, VTOY_JSON *json)
path = VTOY_JSON_STR_EX("path");
if (path)
{
vtoy_list_del_ex(last, node, data->list, path, ventoy_free_path_node_list);
if (strcmp(path, VTOY_DEL_ALL_PATH) == 0)
{
for (node = data->list; node; node = next)
{
next = node->next;
ventoy_free_path_node_list(node->list);
free(node);
}
data->list = NULL;
}
else
{
vtoy_list_del_ex(last, node, data->list, path, ventoy_free_path_node_list);
}
}
ret = ventoy_data_save_all();
@@ -2902,6 +3006,7 @@ static int ventoy_api_auto_install_del(struct mg_connection *conn, VTOY_JSON *js
int index = 0;
const char *path = NULL;
auto_install_node *last = NULL;
auto_install_node *next = NULL;
auto_install_node *node = NULL;
data_auto_install *data = NULL;
@@ -2911,7 +3016,20 @@ static int ventoy_api_auto_install_del(struct mg_connection *conn, VTOY_JSON *js
path = VTOY_JSON_STR_EX("path");
if (path)
{
vtoy_list_del_ex(last, node, data->list, path, ventoy_free_path_node_list);
if (strcmp(path, VTOY_DEL_ALL_PATH) == 0)
{
for (node = data->list; node; node = next)
{
next = node->next;
ventoy_free_path_node_list(node->list);
free(node);
}
data->list = NULL;
}
else
{
vtoy_list_del_ex(last, node, data->list, path, ventoy_free_path_node_list);
}
}
ret = ventoy_data_save_all();
@@ -3229,6 +3347,7 @@ static int ventoy_api_persistence_del(struct mg_connection *conn, VTOY_JSON *jso
int index = 0;
const char *path = NULL;
persistence_node *last = NULL;
persistence_node *next = NULL;
persistence_node *node = NULL;
data_persistence *data = NULL;
@@ -3238,7 +3357,20 @@ static int ventoy_api_persistence_del(struct mg_connection *conn, VTOY_JSON *jso
path = VTOY_JSON_STR_EX("path");
if (path)
{
vtoy_list_del_ex(last, node, data->list, path, ventoy_free_path_node_list);
if (strcmp(path, VTOY_DEL_ALL_PATH) == 0)
{
for (node = data->list; node; node = next)
{
next = node->next;
ventoy_free_path_node_list(node->list);
free(node);
}
data->list = NULL;
}
else
{
vtoy_list_del_ex(last, node, data->list, path, ventoy_free_path_node_list);
}
}
ret = ventoy_data_save_all();
@@ -3515,7 +3647,14 @@ static int ventoy_api_injection_del(struct mg_connection *conn, VTOY_JSON *json)
path = VTOY_JSON_STR_EX("path");
if (path)
{
vtoy_list_del(last, node, data->list, path);
if (strcmp(path, VTOY_DEL_ALL_PATH) == 0)
{
vtoy_list_free(injection_node, data->list);
}
else
{
vtoy_list_del(last, node, data->list, path);
}
}
ret = ventoy_data_save_all();
@@ -3524,6 +3663,34 @@ static int ventoy_api_injection_del(struct mg_connection *conn, VTOY_JSON *json)
return 0;
}
#if 0
#endif
static int ventoy_api_preview_json(struct mg_connection *conn, VTOY_JSON *json)
{
int pos = 0;
int len = 0;
int encodelen = 0;
char *encodebuf = NULL;
(void)json;
len = ventoy_data_real_save_all(0);
encodebuf = ventoy_base64_encode(JSON_SAVE_BUFFER, len, &encodelen);
encodebuf[encodelen] = 0;
VTOY_JSON_FMT_BEGIN(pos, JSON_BUFFER, JSON_BUF_MAX);
VTOY_JSON_FMT_OBJ_BEGIN();
VTOY_JSON_FMT_STRN("json", encodebuf);
VTOY_JSON_FMT_OBJ_END();
VTOY_JSON_FMT_END(pos);
free(encodebuf);
ventoy_json_buffer(conn, JSON_BUFFER, pos);
return 0;
}
#if 0
#endif
@@ -3534,14 +3701,17 @@ int ventoy_data_save_all(void)
return 0;
}
int ventoy_data_real_save_all(void)
int ventoy_data_real_save_all(int apilock)
{
int i = 0;
int pos = 0;
char title[64];
pthread_mutex_lock(&g_api_mutex);
if (apilock)
{
pthread_mutex_lock(&g_api_mutex);
}
ssprintf(pos, JSON_SAVE_BUFFER, JSON_BUF_MAX, "{\n");
ventoy_save_plug(control);
@@ -3565,7 +3735,10 @@ int ventoy_data_real_save_all(void)
}
ssprintf(pos, JSON_SAVE_BUFFER, JSON_BUF_MAX, "}\n");
pthread_mutex_unlock(&g_api_mutex);
if (apilock)
{
pthread_mutex_unlock(&g_api_mutex);
}
return pos;
}
@@ -3578,7 +3751,7 @@ int ventoy_http_writeback(void)
ventoy_get_json_path(filename, NULL);
pos = ventoy_data_real_save_all();
pos = ventoy_data_real_save_all(1);
#ifdef VENTOY_SIM
printf("%s", JSON_SAVE_BUFFER);
@@ -3675,11 +3848,11 @@ static JSON_CB g_ventoy_json_cb[] =
{ "save_injection", ventoy_api_save_injection },
{ "injection_add", ventoy_api_injection_add },
{ "injection_del", ventoy_api_injection_del },
{ "preview_json", ventoy_api_preview_json },
};
static int ventoy_json_handler(struct mg_connection *conn, VTOY_JSON *json)
static int ventoy_json_handler(struct mg_connection *conn, VTOY_JSON *json, char *jsonstr)
{
int i;
const char *method = NULL;
@@ -3696,7 +3869,7 @@ static int ventoy_json_handler(struct mg_connection *conn, VTOY_JSON *json)
ventoy_api_handshake(conn, json);
return 0;
}
for (i = 0; i < (int)(sizeof(g_ventoy_json_cb) / sizeof(g_ventoy_json_cb[0])); i++)
{
if (strcmp(method, g_ventoy_json_cb[i].method) == 0)
@@ -3740,7 +3913,7 @@ static int ventoy_request_handler(struct mg_connection *conn)
if (JSON_SUCCESS == vtoy_json_parse(json, post_data_buf))
{
pthread_mutex_lock(&g_api_mutex);
ventoy_json_handler(conn, json->pstChild);
ventoy_json_handler(conn, json->pstChild, post_data_buf);
pthread_mutex_unlock(&g_api_mutex);
}
else
@@ -5026,7 +5199,8 @@ end:
int ventoy_http_start(const char *ip, const char *port)
{
int i;
int i = 0;
int ret = 0;
char addr[128];
char filename[128];
char backupname[128];
@@ -5063,10 +5237,13 @@ int ventoy_http_start(const char *ip, const char *port)
if (ventoy_is_file_exist("%s", filename))
{
ventoy_copy_file(filename, backupname);
ventoy_load_old_json(filename);
ret = ventoy_load_old_json(filename);
if (ret == 0)
{
ventoy_data_real_save_all(0);
}
}
/* option */
scnprintf(addr, sizeof(addr), "%s:%s", ip, port);
options[1] = addr;

View File

@@ -41,6 +41,24 @@ typedef enum bios_mode
bios_max
}bios_mode;
typedef enum plugin_type
{
plugin_type_control = 0,
plugin_type_theme,
plugin_type_menu_alias,
plugin_type_menu_tip,
plugin_type_menu_class,
plugin_type_auto_install,
plugin_type_persistence,
plugin_type_injection,
plugin_type_conf_replace,
plugin_type_password,
plugin_type_image_list,
plugin_type_auto_memdisk,
plugin_type_dud,
plugin_type_max
}plugin_type;
typedef struct data_control
{
@@ -260,8 +278,10 @@ typedef struct data_persistence
for (i = 0; i < bios_max; i++) \
{\
scnprintf(title, sizeof(title), "%s%s", #plug, g_json_title_postfix[i]);\
g_json_exist[plugin_type_##plug][i] = 0;\
if (ventoy_data_cmp_##plug(g_data_##plug + i, g_data_##plug + bios_max))\
{\
g_json_exist[plugin_type_##plug][i] = 1;\
pos += ventoy_data_save_##plug(g_data_##plug + i, title, JSON_SAVE_BUFFER + pos, JSON_BUF_MAX - pos);\
}\
}\
@@ -302,6 +322,7 @@ typedef struct data_persistence
free(__node);\
__node = __next;\
}\
(list) = NULL;\
}
#define vtoy_list_del(last, node, LIST, field) \
@@ -401,6 +422,7 @@ void ventoy_http_exit(void);
int ventoy_http_start(const char *ip, const char *port);
int ventoy_http_stop(void);
int ventoy_data_save_all(void);
int ventoy_data_real_save_all(int apilock);
#endif /* __VENTOY_HTTP_H__ */