X-Git-Url: https://glassweightruler.freedombox.rocks/gitweb/Ventoy.git/blobdiff_plain/4bf43ab9d4e6b724836724c1d514760d164df79a..5feb3f7b4bd9161d617a75b00ede5f76e7c479c4:/Plugson/src/Core/ventoy_util_linux.c diff --git a/Plugson/src/Core/ventoy_util_linux.c b/Plugson/src/Core/ventoy_util_linux.c index fa799a9..c5bf838 100644 --- a/Plugson/src/Core/ventoy_util_linux.c +++ b/Plugson/src/Core/ventoy_util_linux.c @@ -33,6 +33,7 @@ #include #include #include +#include #include #include @@ -234,66 +235,18 @@ int ventoy_write_buf_to_file(const char *FileName, void *Bufer, int BufLen) return 0; } -int ventoy_decompress_tar(char *tarbuf, int buflen, int *tarsize) -{ - int rc = 1; - int inused = 0; - int BufLen = 0; - unsigned char *buffer = NULL; - char tarxz[MAX_PATH]; - - scnprintf(tarxz, sizeof(tarxz), "%s/tool/plugson.tar.xz", g_ventoy_dir); - if (ventoy_read_file_to_buf(tarxz, 0, (void **)&buffer, &BufLen)) - { - vlog("Failed to read file <%s>\n", tarxz); - return 1; - } - - g_unxz_buffer = (unsigned char *)tarbuf; - g_unxz_len = 0; - - unxz(buffer, BufLen, NULL, unxz_flush, NULL, &inused, unxz_error); - vlog("xzlen:%u rawdata size:%d\n", BufLen, g_unxz_len); - - if (inused != BufLen) - { - vlog("Failed to unxz data %d %d\n", inused, BufLen); - rc = 1; - } - else - { - *tarsize = g_unxz_len; - rc = 0; - } - - free(buffer); - - return rc; -} - +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; @@ -301,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; @@ -318,15 +270,47 @@ 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); + sem_destroy(&g_writeback_sem); +} - pthread_cond_destroy(&g_writeback_cond); - pthread_mutex_destroy(&g_writeback_mutex); -} +int ventoy_read_file_to_buf(const char *FileName, int ExtLen, void **Bufer, int *BufLen) +{ + int FileSize; + FILE *fp = NULL; + void *Data = NULL; + + fp = fopen(FileName, "rb"); + if (fp == NULL) + { + vlog("Failed to open file %s", FileName); + return 1; + } + + fseek(fp, 0, SEEK_END); + FileSize = (int)ftell(fp); + + Data = malloc(FileSize + ExtLen); + if (!Data) + { + fclose(fp); + return 1; + } + + fseek(fp, 0, SEEK_SET); + fread(Data, 1, FileSize, fp); + + fclose(fp); + + *Bufer = Data; + *BufLen = FileSize; + + return 0; +} int ventoy_copy_file(const char *a, const char *b) {