Skip to content

Commit

Permalink
Merge bitcoin#14192: utils: Convert fs::filesystem_error messages fro…
Browse files Browse the repository at this point in the history
…m 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
  • Loading branch information
laanwj committed Sep 13, 2018
2 parents 49fd485 + e221368 commit d73205e
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 3 deletions.
16 changes: 16 additions & 0 deletions src/fs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,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<std::codecvt_utf8_utf16<wchar_t>, wchar_t>().to_bytes(utf16_string);
#endif
}

} // fsbridge
2 changes: 2 additions & 0 deletions src/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/rpc/protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/wallet/db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit d73205e

Please sign in to comment.