From e2213689328f8ba42af1d32d12c7b78a71f1ddc7 Mon Sep 17 00:00:00 2001 From: Chun Kuan Lee Date: Tue, 11 Sep 2018 02:08:56 +0800 Subject: [PATCH] utils: Convert fs error messages from multibyte to utf-8 --- src/fs.cpp | 16 ++++++++++++++++ src/fs.h | 2 ++ src/rpc/protocol.cpp | 2 +- src/wallet/db.cpp | 2 +- src/wallet/wallet.cpp | 2 +- 5 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/fs.cpp b/src/fs.cpp index 2dbc13643..1a221788b 100644 --- a/src/fs.cpp +++ b/src/fs.cpp @@ -92,4 +92,20 @@ bool FileLock::TryLock() } #endif +std::string get_filesystem_error_message(const fs::filesystem_error& e) +{ +#ifndef WIN32 + return e.what(); +#else + // Convert from Multi Byte to utf-16 + std::string mb_string(e.what()); + int size = MultiByteToWideChar(CP_ACP, 0, mb_string.c_str(), mb_string.size(), nullptr, 0); + + std::wstring utf16_string(size, L'\0'); + MultiByteToWideChar(CP_ACP, 0, mb_string.c_str(), mb_string.size(), &*utf16_string.begin(), size); + // Convert from utf-16 to utf-8 + return std::wstring_convert, wchar_t>().to_bytes(utf16_string); +#endif +} + } // fsbridge diff --git a/src/fs.h b/src/fs.h index 5a28d9a81..5492bdd4e 100644 --- a/src/fs.h +++ b/src/fs.h @@ -38,6 +38,8 @@ namespace fsbridge { void* hFile = (void*)-1; // INVALID_HANDLE_VALUE #endif }; + + std::string get_filesystem_error_message(const fs::filesystem_error& e); }; #endif // BITCOIN_FS_H diff --git a/src/rpc/protocol.cpp b/src/rpc/protocol.cpp index dab63ab26..55bebb566 100644 --- a/src/rpc/protocol.cpp +++ b/src/rpc/protocol.cpp @@ -128,7 +128,7 @@ void DeleteAuthCookie() try { fs::remove(GetAuthCookieFile()); } catch (const fs::filesystem_error& e) { - LogPrintf("%s: Unable to remove random auth cookie file: %s\n", __func__, e.what()); + LogPrintf("%s: Unable to remove random auth cookie file: %s\n", __func__, fsbridge::get_filesystem_error_message(e)); } } diff --git a/src/wallet/db.cpp b/src/wallet/db.cpp index dfd60ae5e..025ae2975 100644 --- a/src/wallet/db.cpp +++ b/src/wallet/db.cpp @@ -783,7 +783,7 @@ bool BerkeleyDatabase::Backup(const std::string& strDest) LogPrintf("copied %s to %s\n", strFile, pathDest.string()); return true; } catch (const fs::filesystem_error& e) { - LogPrintf("error copying %s to %s - %s\n", strFile, pathDest.string(), e.what()); + LogPrintf("error copying %s to %s - %s\n", strFile, pathDest.string(), fsbridge::get_filesystem_error_message(e)); return false; } } diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index de707a09f..690c7936d 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -3849,7 +3849,7 @@ bool CWallet::Verify(std::string wallet_file, bool salvage_wallet, std::string& return false; } } catch (const fs::filesystem_error& e) { - error_string = strprintf("Error loading wallet %s. %s", wallet_file, e.what()); + error_string = strprintf("Error loading wallet %s. %s", wallet_file, fsbridge::get_filesystem_error_message(e)); return false; }