Skip to content

Commit c95a443

Browse files
committed
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.
1 parent 91ccb62 commit c95a443

File tree

4 files changed

+35
-17
lines changed

4 files changed

+35
-17
lines changed

src/util/fs_helpers.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <tinyformat.h>
1515
#include <util/fs.h>
1616
#include <util/getuniquepath.h>
17+
#include <util/syserror.h>
1718

1819
#include <cerrno>
1920
#include <filesystem>
@@ -120,28 +121,28 @@ std::streampos GetFileSize(const char* path, std::streamsize max)
120121
bool FileCommit(FILE* file)
121122
{
122123
if (fflush(file) != 0) { // harmless if redundantly called
123-
LogPrintf("%s: fflush failed: %d\n", __func__, errno);
124+
LogPrintf("fflush failed: %s\n", SysErrorString(errno));
124125
return false;
125126
}
126127
#ifdef WIN32
127128
HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
128129
if (FlushFileBuffers(hFile) == 0) {
129-
LogPrintf("%s: FlushFileBuffers failed: %d\n", __func__, GetLastError());
130+
LogPrintf("FlushFileBuffers failed: %s\n", Win32ErrorString(GetLastError()));
130131
return false;
131132
}
132133
#elif defined(MAC_OSX) && defined(F_FULLFSYNC)
133134
if (fcntl(fileno(file), F_FULLFSYNC, 0) == -1) { // Manpage says "value other than -1" is returned on success
134-
LogPrintf("%s: fcntl F_FULLFSYNC failed: %d\n", __func__, errno);
135+
LogPrintf("fcntl F_FULLFSYNC failed: %s\n", SysErrorString(errno));
135136
return false;
136137
}
137138
#elif HAVE_FDATASYNC
138139
if (fdatasync(fileno(file)) != 0 && errno != EINVAL) { // Ignore EINVAL for filesystems that don't support sync
139-
LogPrintf("%s: fdatasync failed: %d\n", __func__, errno);
140+
LogPrintf("fdatasync failed: %s\n", SysErrorString(errno));
140141
return false;
141142
}
142143
#else
143144
if (fsync(fileno(file)) != 0 && errno != EINVAL) {
144-
LogPrintf("%s: fsync failed: %d\n", __func__, errno);
145+
LogPrintf("fsync failed: %s\n", SysErrorString(errno));
145146
return false;
146147
}
147148
#endif

src/util/sock.cpp

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -419,18 +419,7 @@ void Sock::Close()
419419
#ifdef WIN32
420420
std::string NetworkErrorString(int err)
421421
{
422-
wchar_t buf[256];
423-
buf[0] = 0;
424-
if(FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_MAX_WIDTH_MASK,
425-
nullptr, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
426-
buf, ARRAYSIZE(buf), nullptr))
427-
{
428-
return strprintf("%s (%d)", std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>,wchar_t>().to_bytes(buf), err);
429-
}
430-
else
431-
{
432-
return strprintf("Unknown error (%d)", err);
433-
}
422+
return Win32ErrorString(err);
434423
}
435424
#else
436425
std::string NetworkErrorString(int err)

src/util/syserror.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
#include <cstring>
1313
#include <string>
1414

15+
#if defined(WIN32)
16+
#include <windows.h>
17+
#include <locale>
18+
#include <codecvt>
19+
#endif
20+
1521
std::string SysErrorString(int err)
1622
{
1723
char buf[1024];
@@ -33,3 +39,21 @@ std::string SysErrorString(int err)
3339
return strprintf("Unknown error (%d)", err);
3440
}
3541
}
42+
43+
#if defined(WIN32)
44+
std::string Win32ErrorString(int err)
45+
{
46+
wchar_t buf[256];
47+
buf[0] = 0;
48+
if(FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_MAX_WIDTH_MASK,
49+
nullptr, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
50+
buf, ARRAYSIZE(buf), nullptr))
51+
{
52+
return strprintf("%s (%d)", std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>,wchar_t>().to_bytes(buf), err);
53+
}
54+
else
55+
{
56+
return strprintf("Unknown error (%d)", err);
57+
}
58+
}
59+
#endif

src/util/syserror.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,8 @@
1313
*/
1414
std::string SysErrorString(int err);
1515

16+
#if defined(WIN32)
17+
std::string Win32ErrorString(int err);
18+
#endif
19+
1620
#endif // BITCOIN_UTIL_SYSERROR_H

0 commit comments

Comments
 (0)