Skip to content

Commit e221368

Browse files
committed
utils: Convert fs error messages from multibyte to utf-8
1 parent 6eeac2e commit e221368

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
@@ -92,4 +92,20 @@ bool FileLock::TryLock()
9292
}
9393
#endif
9494

95+
std::string get_filesystem_error_message(const fs::filesystem_error& e)
96+
{
97+
#ifndef WIN32
98+
return e.what();
99+
#else
100+
// Convert from Multi Byte to utf-16
101+
std::string mb_string(e.what());
102+
int size = MultiByteToWideChar(CP_ACP, 0, mb_string.c_str(), mb_string.size(), nullptr, 0);
103+
104+
std::wstring utf16_string(size, L'\0');
105+
MultiByteToWideChar(CP_ACP, 0, mb_string.c_str(), mb_string.size(), &*utf16_string.begin(), size);
106+
// Convert from utf-16 to utf-8
107+
return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t>().to_bytes(utf16_string);
108+
#endif
109+
}
110+
95111
} // 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)