From: longpanda Date: Fri, 3 Dec 2021 06:21:47 +0000 (+0800) Subject: Fix the "plugson.tar.xz not found" error, when the path contains unicode characters. X-Git-Tag: v1.0.63~12 X-Git-Url: https://glassweightruler.freedombox.rocks/gitweb/Ventoy.git/commitdiff_plain/8c18f91ac1b818e749c839218c361c3986e79c46 Fix the "plugson.tar.xz not found" error, when the path contains unicode characters. --- diff --git a/Plugson/src/Core/ventoy_util.c b/Plugson/src/Core/ventoy_util.c index d6ce010..2a00433 100644 --- a/Plugson/src/Core/ventoy_util.c +++ b/Plugson/src/Core/ventoy_util.c @@ -80,44 +80,6 @@ uint64_t ventoy_get_human_readable_gb(uint64_t SizeBytes) return (uint64_t)GB; } -int ventoy_read_file_to_buf(const char *FileName, int ExtLen, void **Bufer, int *BufLen) -{ - int FileSize; - FILE *fp = NULL; - void *Data = NULL; - -#if defined(_MSC_VER) || defined(WIN32) - fopen_s(&fp, FileName, "rb"); -#else - fp = fopen(FileName, "rb"); -#endif - 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; -} - ventoy_file * ventoy_tar_find_file(const char *path) { int i; @@ -211,6 +173,7 @@ int ventoy_www_init(void) if (ventoy_decompress_tar(g_tar_buffer, TAR_BUF_MAX, &tarsize)) { + vlog("Failed to decompress tar\n"); return 1; } diff --git a/Plugson/src/Core/ventoy_util_linux.c b/Plugson/src/Core/ventoy_util_linux.c index 69b6022..c432f08 100644 --- a/Plugson/src/Core/ventoy_util_linux.c +++ b/Plugson/src/Core/ventoy_util_linux.c @@ -292,6 +292,41 @@ void ventoy_stop_writeback_thread(void) } + +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) { int len = 0; diff --git a/Plugson/src/Core/ventoy_util_windows.c b/Plugson/src/Core/ventoy_util_windows.c index b65f773..1d8d97f 100644 --- a/Plugson/src/Core/ventoy_util_windows.c +++ b/Plugson/src/Core/ventoy_util_windows.c @@ -655,33 +655,6 @@ int CheckRuntimeEnvironment(char Letter, ventoy_disk *disk) } -int ventoy_write_buf_to_file(const char *FileName, void *Bufer, int BufLen) -{ - BOOL bRet; - DWORD dwBytes; - HANDLE hFile; - - hFile = CreateFileA(FileName, GENERIC_READ | GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0); - if (hFile == INVALID_HANDLE_VALUE) - { - vlog("CreateFile %s failed %u\n", FileName, LASTERR); - return 1; - } - - bRet = WriteFile(hFile, Bufer, (DWORD)BufLen, &dwBytes, NULL); - - if ((!bRet) || ((DWORD)BufLen != dwBytes)) - { - vlog("Failed to write file <%s> %u err:%u", FileName, dwBytes, LASTERR); - CloseHandle(hFile); - return 1; - } - - FlushFileBuffers(hFile); - CloseHandle(hFile); - - return 0; -} static volatile int g_thread_stop = 0; static HANDLE g_writeback_thread; @@ -735,6 +708,85 @@ void ventoy_stop_writeback_thread(void) CHECK_CLOSE_HANDLE(g_writeback_event); } +int ventoy_read_file_to_buf(const char *FileName, int ExtLen, void **Bufer, int *BufLen) +{ + int UTF8 = 0; + int Size = 0; + BOOL bRet; + DWORD dwBytes; + HANDLE hFile; + char *buffer = NULL; + WCHAR FilePathW[MAX_PATH]; + + UTF8 = IsUTF8Encode(FileName); + if (UTF8) + { + Utf8ToUtf16(FileName, FilePathW); + hFile = CreateFileW(FilePathW, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0); + } + else + { + hFile = CreateFileA(FileName, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0); + } + + if (hFile == INVALID_HANDLE_VALUE) + { + vlog("Failed to open %s %u\n", FileName, LASTERR); + return 1; + } + + Size = (int)GetFileSize(hFile, NULL); + buffer = malloc(Size + ExtLen); + if (!buffer) + { + vlog("Failed to alloc file buffer\n"); + CloseHandle(hFile); + return 1; + } + + bRet = ReadFile(hFile, buffer, (DWORD)Size, &dwBytes, NULL); + if ((!bRet) || ((DWORD)Size != dwBytes)) + { + vlog("Failed to read file <%s> %u err:%u", FileName, dwBytes, LASTERR); + CloseHandle(hFile); + free(buffer); + return 1; + } + + *Bufer = buffer; + *BufLen = Size; + + CloseHandle(hFile); + return 0; +} + +int ventoy_write_buf_to_file(const char *FileName, void *Bufer, int BufLen) +{ + BOOL bRet; + DWORD dwBytes; + HANDLE hFile; + + hFile = CreateFileA(FileName, GENERIC_READ | GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0); + if (hFile == INVALID_HANDLE_VALUE) + { + vlog("CreateFile %s failed %u\n", FileName, LASTERR); + return 1; + } + + bRet = WriteFile(hFile, Bufer, (DWORD)BufLen, &dwBytes, NULL); + + if ((!bRet) || ((DWORD)BufLen != dwBytes)) + { + vlog("Failed to write file <%s> %u err:%u", FileName, dwBytes, LASTERR); + CloseHandle(hFile); + return 1; + } + + FlushFileBuffers(hFile); + CloseHandle(hFile); + + return 0; +} int ventoy_copy_file(const char *a, const char *b) { diff --git a/Plugson/src/main_windows.c b/Plugson/src/main_windows.c index 67d6ee0..e6a0bf4 100644 --- a/Plugson/src/main_windows.c +++ b/Plugson/src/main_windows.c @@ -468,6 +468,7 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLi { int rc; HANDLE hMutex; + WCHAR CurDir[MAX_PATH]; UNREFERENCED_PARAMETER(hPrevInstance); @@ -489,7 +490,9 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLi return 1; } - GetCurrentDirectoryA(MAX_PATH, g_cur_dir); + GetCurrentDirectoryW(MAX_PATH, CurDir); + WideCharToMultiByte(CP_UTF8, 0, CurDir, -1, g_cur_dir, MAX_PATH, NULL, 0); + sprintf_s(g_ventoy_dir, sizeof(g_ventoy_dir), "%s", g_cur_dir); sprintf_s(g_log_file, sizeof(g_log_file), "%s\\%s", g_cur_dir, LOG_FILE); ventoy_log_init(); diff --git a/Plugson/vs/VentoyPlugson/Release/VentoyPlugson.exe b/Plugson/vs/VentoyPlugson/Release/VentoyPlugson.exe index 7c4a764..d33e7be 100644 Binary files a/Plugson/vs/VentoyPlugson/Release/VentoyPlugson.exe and b/Plugson/vs/VentoyPlugson/Release/VentoyPlugson.exe differ