Fix the "plugson.tar.xz not found" error, when the path contains unicode characters.

This commit is contained in:
longpanda 2021-12-03 14:21:47 +08:00
parent 73fabd0c65
commit 8c18f91ac1
5 changed files with 119 additions and 66 deletions

View File

@ -80,44 +80,6 @@ uint64_t ventoy_get_human_readable_gb(uint64_t SizeBytes)
return (uint64_t)GB; 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) ventoy_file * ventoy_tar_find_file(const char *path)
{ {
int i; int i;
@ -211,6 +173,7 @@ int ventoy_www_init(void)
if (ventoy_decompress_tar(g_tar_buffer, TAR_BUF_MAX, &tarsize)) if (ventoy_decompress_tar(g_tar_buffer, TAR_BUF_MAX, &tarsize))
{ {
vlog("Failed to decompress tar\n");
return 1; return 1;
} }

View File

@ -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 ventoy_copy_file(const char *a, const char *b)
{ {
int len = 0; int len = 0;

View File

@ -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 volatile int g_thread_stop = 0;
static HANDLE g_writeback_thread; static HANDLE g_writeback_thread;
@ -735,6 +708,85 @@ void ventoy_stop_writeback_thread(void)
CHECK_CLOSE_HANDLE(g_writeback_event); 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) int ventoy_copy_file(const char *a, const char *b)
{ {

View File

@ -468,6 +468,7 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLi
{ {
int rc; int rc;
HANDLE hMutex; HANDLE hMutex;
WCHAR CurDir[MAX_PATH];
UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(hPrevInstance);
@ -489,7 +490,9 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLi
return 1; 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_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); sprintf_s(g_log_file, sizeof(g_log_file), "%s\\%s", g_cur_dir, LOG_FILE);
ventoy_log_init(); ventoy_log_init();