Show descriptive error messages when FileCommit fails

Only raw errno codes are logged if FileCommit fails. These are
implementation-specific, so it makes it harder to debug based on
user reports. Instead, use SysErrorString to display both the
raw int value and the descriptive message.
This commit is contained in:
John Moffett 2022-12-05 12:59:15 -05:00
parent 91ccb62faa
commit c95a4432d7
4 changed files with 35 additions and 17 deletions

View file

@ -14,6 +14,7 @@
#include <tinyformat.h>
#include <util/fs.h>
#include <util/getuniquepath.h>
#include <util/syserror.h>
#include <cerrno>
#include <filesystem>
@ -120,28 +121,28 @@ std::streampos GetFileSize(const char* path, std::streamsize max)
bool FileCommit(FILE* file)
{
if (fflush(file) != 0) { // harmless if redundantly called
LogPrintf("%s: fflush failed: %d\n", __func__, errno);
LogPrintf("fflush failed: %s\n", SysErrorString(errno));
return false;
}
#ifdef WIN32
HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
if (FlushFileBuffers(hFile) == 0) {
LogPrintf("%s: FlushFileBuffers failed: %d\n", __func__, GetLastError());
LogPrintf("FlushFileBuffers failed: %s\n", Win32ErrorString(GetLastError()));
return false;
}
#elif defined(MAC_OSX) && defined(F_FULLFSYNC)
if (fcntl(fileno(file), F_FULLFSYNC, 0) == -1) { // Manpage says "value other than -1" is returned on success
LogPrintf("%s: fcntl F_FULLFSYNC failed: %d\n", __func__, errno);
LogPrintf("fcntl F_FULLFSYNC failed: %s\n", SysErrorString(errno));
return false;
}
#elif HAVE_FDATASYNC
if (fdatasync(fileno(file)) != 0 && errno != EINVAL) { // Ignore EINVAL for filesystems that don't support sync
LogPrintf("%s: fdatasync failed: %d\n", __func__, errno);
LogPrintf("fdatasync failed: %s\n", SysErrorString(errno));
return false;
}
#else
if (fsync(fileno(file)) != 0 && errno != EINVAL) {
LogPrintf("%s: fsync failed: %d\n", __func__, errno);
LogPrintf("fsync failed: %s\n", SysErrorString(errno));
return false;
}
#endif

View file

@ -419,18 +419,7 @@ void Sock::Close()
#ifdef WIN32
std::string NetworkErrorString(int err)
{
wchar_t buf[256];
buf[0] = 0;
if(FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_MAX_WIDTH_MASK,
nullptr, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
buf, ARRAYSIZE(buf), nullptr))
{
return strprintf("%s (%d)", std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>,wchar_t>().to_bytes(buf), err);
}
else
{
return strprintf("Unknown error (%d)", err);
}
return Win32ErrorString(err);
}
#else
std::string NetworkErrorString(int err)

View file

@ -12,6 +12,12 @@
#include <cstring>
#include <string>
#if defined(WIN32)
#include <windows.h>
#include <locale>
#include <codecvt>
#endif
std::string SysErrorString(int err)
{
char buf[1024];
@ -33,3 +39,21 @@ std::string SysErrorString(int err)
return strprintf("Unknown error (%d)", err);
}
}
#if defined(WIN32)
std::string Win32ErrorString(int err)
{
wchar_t buf[256];
buf[0] = 0;
if(FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_MAX_WIDTH_MASK,
nullptr, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
buf, ARRAYSIZE(buf), nullptr))
{
return strprintf("%s (%d)", std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>,wchar_t>().to_bytes(buf), err);
}
else
{
return strprintf("Unknown error (%d)", err);
}
}
#endif

View file

@ -13,4 +13,8 @@
*/
std::string SysErrorString(int err);
#if defined(WIN32)
std::string Win32ErrorString(int err);
#endif
#endif // BITCOIN_UTIL_SYSERROR_H