aboutsummaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorJohn Moffett <john.moff@gmail.com>2022-12-05 12:59:15 -0500
committerJohn Moffett <john.moff@gmail.com>2023-06-30 09:48:21 -0400
commitc95a4432d7c9d0e5829cd802908700ba2e2c25dc (patch)
tree0f3fe9d7b7ae4653fc847918d84b0b065618b27d /src/util
parent91ccb62faab21b2b52b089cc04f3a5c1bf6989cc (diff)
downloadbitcoin-c95a4432d7c9d0e5829cd802908700ba2e2c25dc.tar.xz
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.
Diffstat (limited to 'src/util')
-rw-r--r--src/util/fs_helpers.cpp11
-rw-r--r--src/util/sock.cpp13
-rw-r--r--src/util/syserror.cpp24
-rw-r--r--src/util/syserror.h4
4 files changed, 35 insertions, 17 deletions
diff --git a/src/util/fs_helpers.cpp b/src/util/fs_helpers.cpp
index d05cb8a63d..2a9eb3502e 100644
--- a/src/util/fs_helpers.cpp
+++ b/src/util/fs_helpers.cpp
@@ -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
diff --git a/src/util/sock.cpp b/src/util/sock.cpp
index 53d20bdf19..d33ccd135e 100644
--- a/src/util/sock.cpp
+++ b/src/util/sock.cpp
@@ -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)
diff --git a/src/util/syserror.cpp b/src/util/syserror.cpp
index 5270f55366..bac498a23d 100644
--- a/src/util/syserror.cpp
+++ b/src/util/syserror.cpp
@@ -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
diff --git a/src/util/syserror.h b/src/util/syserror.h
index a54ba553ee..eb02141b9d 100644
--- a/src/util/syserror.h
+++ b/src/util/syserror.h
@@ -13,4 +13,8 @@
*/
std::string SysErrorString(int err);
+#if defined(WIN32)
+std::string Win32ErrorString(int err);
+#endif
+
#endif // BITCOIN_UTIL_SYSERROR_H