From df19db1509fa3b3bdfc4732e46139431e8a3ba42 Mon Sep 17 00:00:00 2001 From: Alexey Milovidov Date: Sun, 7 Jun 2020 20:29:34 +0300 Subject: [PATCH] Added a test for history in clickhouse-client --- base/common/CMakeLists.txt | 1 + base/common/ReplxxLineReader.cpp | 7 ++++--- base/common/errnoToString.cpp | 29 ++++++++++++++++++++++++++++ base/common/errnoToString.h | 6 ++++++ base/common/ya.make | 1 + programs/server/Server.cpp | 1 + src/Common/Exception.cpp | 26 +------------------------ src/Common/Exception.h | 1 - src/Common/PipeFDs.cpp | 1 + src/Common/QueryProfiler.cpp | 1 + src/Common/ShellCommand.cpp | 1 + src/Common/StatusFile.cpp | 1 + src/Interpreters/ThreadStatusExt.cpp | 1 + 13 files changed, 48 insertions(+), 29 deletions(-) create mode 100644 base/common/errnoToString.cpp create mode 100644 base/common/errnoToString.h diff --git a/base/common/CMakeLists.txt b/base/common/CMakeLists.txt index 9b827cdb468..d98da8f0450 100644 --- a/base/common/CMakeLists.txt +++ b/base/common/CMakeLists.txt @@ -16,6 +16,7 @@ set (SRCS shift10.cpp sleep.cpp terminalColors.cpp + errnoToString.cpp ) if (ENABLE_REPLXX) diff --git a/base/common/ReplxxLineReader.cpp b/base/common/ReplxxLineReader.cpp index f2471e65404..fcdab9f39f5 100644 --- a/base/common/ReplxxLineReader.cpp +++ b/base/common/ReplxxLineReader.cpp @@ -1,4 +1,5 @@ #include +#include #include #include @@ -29,13 +30,13 @@ ReplxxLineReader::ReplxxLineReader( history_file_fd = open(history_file_path.c_str(), O_RDWR); if (history_file_fd < 0) { - rx.print("Open of history file failed: %s\n", strerror(errno)); + rx.print("Open of history file failed: %s\n", errnoToString(errno).c_str()); } else { if (flock(history_file_fd, LOCK_SH)) { - rx.print("Shared lock of history file failed: %s\n", strerror(errno)); + rx.print("Shared lock of history file failed: %s\n", errnoToString(errno).c_str()); } else { @@ -43,7 +44,7 @@ ReplxxLineReader::ReplxxLineReader( if (flock(history_file_fd, LOCK_UN)) { - rx.print("Unlock of history file failed: %s\n", strerror(errno)); + rx.print("Unlock of history file failed: %s\n", errnoToString(errno).c_str()); } } } diff --git a/base/common/errnoToString.cpp b/base/common/errnoToString.cpp new file mode 100644 index 00000000000..cdadba2c615 --- /dev/null +++ b/base/common/errnoToString.cpp @@ -0,0 +1,29 @@ +#include "errnoToString.h" + +#include + + +std::string errnoToString(int code, int the_errno) +{ + const size_t buf_size = 128; + char buf[buf_size]; +#ifndef _GNU_SOURCE + int rc = strerror_r(the_errno, buf, buf_size); +#ifdef __APPLE__ + if (rc != 0 && rc != EINVAL) +#else + if (rc != 0) +#endif + { + std::string tmp = std::to_string(code); + const char * code_str = tmp.c_str(); + const char * unknown_message = "Unknown error "; + strcpy(buf, unknown_message); + strcpy(buf + strlen(unknown_message), code_str); + } + return fmt::format("errno: {}, strerror: {}", the_errno, buf); +#else + (void)code; + return fmt::format("errno: {}, strerror: {}", the_errno, strerror_r(the_errno, buf, sizeof(buf))); +#endif +} diff --git a/base/common/errnoToString.h b/base/common/errnoToString.h new file mode 100644 index 00000000000..fd5f81ec2c7 --- /dev/null +++ b/base/common/errnoToString.h @@ -0,0 +1,6 @@ +#pragma once + +#include +#include + +std::string errnoToString(int code, int the_errno = errno); diff --git a/base/common/ya.make b/base/common/ya.make index 6e45b0193c5..d40b1f5abfd 100644 --- a/base/common/ya.make +++ b/base/common/ya.make @@ -47,6 +47,7 @@ SRCS( shift10.cpp sleep.cpp terminalColors.cpp + errnoToString.cpp ) END() diff --git a/programs/server/Server.cpp b/programs/server/Server.cpp index d70806303d2..8b58c5664b6 100644 --- a/programs/server/Server.cpp +++ b/programs/server/Server.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include diff --git a/src/Common/Exception.cpp b/src/Common/Exception.cpp index b0c897127c6..f2470ea0406 100644 --- a/src/Common/Exception.cpp +++ b/src/Common/Exception.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -85,31 +86,6 @@ std::string Exception::getStackTraceString() const } -std::string errnoToString(int code, int the_errno) -{ - const size_t buf_size = 128; - char buf[buf_size]; -#ifndef _GNU_SOURCE - int rc = strerror_r(the_errno, buf, buf_size); -#ifdef __APPLE__ - if (rc != 0 && rc != EINVAL) -#else - if (rc != 0) -#endif - { - std::string tmp = std::to_string(code); - const char * code_str = tmp.c_str(); - const char * unknown_message = "Unknown error "; - strcpy(buf, unknown_message); - strcpy(buf + strlen(unknown_message), code_str); - } - return "errno: " + toString(the_errno) + ", strerror: " + std::string(buf); -#else - (void)code; - return "errno: " + toString(the_errno) + ", strerror: " + std::string(strerror_r(the_errno, buf, sizeof(buf))); -#endif -} - void throwFromErrno(const std::string & s, int code, int the_errno) { throw ErrnoException(s + ", " + errnoToString(code, the_errno), code, the_errno); diff --git a/src/Common/Exception.h b/src/Common/Exception.h index 45a1b3d6340..763b90048bb 100644 --- a/src/Common/Exception.h +++ b/src/Common/Exception.h @@ -81,7 +81,6 @@ private: using Exceptions = std::vector; -std::string errnoToString(int code, int the_errno = errno); [[noreturn]] void throwFromErrno(const std::string & s, int code, int the_errno = errno); /// Useful to produce some extra information about available space and inodes on device [[noreturn]] void throwFromErrnoWithPath(const std::string & s, const std::string & path, int code, diff --git a/src/Common/PipeFDs.cpp b/src/Common/PipeFDs.cpp index 1f57234534f..d91917c23a4 100644 --- a/src/Common/PipeFDs.cpp +++ b/src/Common/PipeFDs.cpp @@ -3,6 +3,7 @@ #include #include +#include #include #include diff --git a/src/Common/QueryProfiler.cpp b/src/Common/QueryProfiler.cpp index a8b7d51a260..c4c7d21314d 100644 --- a/src/Common/QueryProfiler.cpp +++ b/src/Common/QueryProfiler.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include diff --git a/src/Common/ShellCommand.cpp b/src/Common/ShellCommand.cpp index 1b97ed5689c..53ab2301a0a 100644 --- a/src/Common/ShellCommand.cpp +++ b/src/Common/ShellCommand.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include diff --git a/src/Common/StatusFile.cpp b/src/Common/StatusFile.cpp index 758f500e9d2..d228fdb42b6 100644 --- a/src/Common/StatusFile.cpp +++ b/src/Common/StatusFile.cpp @@ -8,6 +8,7 @@ #include #include +#include #include #include diff --git a/src/Interpreters/ThreadStatusExt.cpp b/src/Interpreters/ThreadStatusExt.cpp index 354c7568818..04265734ce7 100644 --- a/src/Interpreters/ThreadStatusExt.cpp +++ b/src/Interpreters/ThreadStatusExt.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #if defined(OS_LINUX) # include