Skip to content

Commit d73205e

Browse files
committed
Merge #14192: utils: Convert fs::filesystem_error messages from local multibyte to utf-8 on Windows
e221368 utils: Convert fs error messages from multibyte to utf-8 (Chun Kuan Lee) Pull request description: Before: ![default](https://user-images.githubusercontent.com/11154118/45318798-8d83f480-b570-11e8-8cbb-c729a54f6b9e.png) After: ![2](https://user-images.githubusercontent.com/11154118/45318806-91177b80-b570-11e8-9474-a62342c92dbd.png) Tree-SHA512: 0a598bd159286f6784d117b8a24888b2650d5402d687ab0e8d0849e0c3d53797e266647d8177bb6614307c9598019cd7477311bb9895b1bb52a6bd77b460fda1
2 parents 49fd485 + e221368 commit d73205e

File tree

5 files changed

+21
-3
lines changed

5 files changed

+21
-3
lines changed

src/fs.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,20 @@ bool FileLock::TryLock()
9797
}
9898
#endif
9999

100+
std::string get_filesystem_error_message(const fs::filesystem_error& e)
101+
{
102+
#ifndef WIN32
103+
return e.what();
104+
#else
105+
// Convert from Multi Byte to utf-16
106+
std::string mb_string(e.what());
107+
int size = MultiByteToWideChar(CP_ACP, 0, mb_string.c_str(), mb_string.size(), nullptr, 0);
108+
109+
std::wstring utf16_string(size, L'\0');
110+
MultiByteToWideChar(CP_ACP, 0, mb_string.c_str(), mb_string.size(), &*utf16_string.begin(), size);
111+
// Convert from utf-16 to utf-8
112+
return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t>().to_bytes(utf16_string);
113+
#endif
114+
}
115+
100116
} // fsbridge

src/fs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ namespace fsbridge {
3838
void* hFile = (void*)-1; // INVALID_HANDLE_VALUE
3939
#endif
4040
};
41+
42+
std::string get_filesystem_error_message(const fs::filesystem_error& e);
4143
};
4244

4345
#endif // BITCOIN_FS_H

src/rpc/protocol.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ void DeleteAuthCookie()
128128
try {
129129
fs::remove(GetAuthCookieFile());
130130
} catch (const fs::filesystem_error& e) {
131-
LogPrintf("%s: Unable to remove random auth cookie file: %s\n", __func__, e.what());
131+
LogPrintf("%s: Unable to remove random auth cookie file: %s\n", __func__, fsbridge::get_filesystem_error_message(e));
132132
}
133133
}
134134

src/wallet/db.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ bool BerkeleyDatabase::Backup(const std::string& strDest)
783783
LogPrintf("copied %s to %s\n", strFile, pathDest.string());
784784
return true;
785785
} catch (const fs::filesystem_error& e) {
786-
LogPrintf("error copying %s to %s - %s\n", strFile, pathDest.string(), e.what());
786+
LogPrintf("error copying %s to %s - %s\n", strFile, pathDest.string(), fsbridge::get_filesystem_error_message(e));
787787
return false;
788788
}
789789
}

src/wallet/wallet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3849,7 +3849,7 @@ bool CWallet::Verify(std::string wallet_file, bool salvage_wallet, std::string&
38493849
return false;
38503850
}
38513851
} catch (const fs::filesystem_error& e) {
3852-
error_string = strprintf("Error loading wallet %s. %s", wallet_file, e.what());
3852+
error_string = strprintf("Error loading wallet %s. %s", wallet_file, fsbridge::get_filesystem_error_message(e));
38533853
return false;
38543854
}
38553855

0 commit comments

Comments
 (0)