diff --git a/CMakeLists.txt b/CMakeLists.txt index aea59e46..cd7966fa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,28 +25,6 @@ if(CMAKE_BUILD_TYPE STREQUAL "Release") add_compile_definitions(NDEBUG) endif() -add_compile_definitions(FMT_HEADER_ONLY) - -include(ExternalProject) - -message("Using spdlog from https://github.com/gabime/spdlog on tag v1.10.0") -ExternalProject_Add( - dep_spdlog - GIT_REPOSITORY "https://github.com/gabime/spdlog" - GIT_TAG "v1.10.0" - GIT_SHALLOW 1 - UPDATE_COMMAND "" - PATCH_COMMAND "" - CMAKE_ARGS - -DCMAKE_INSTALL_PREFIX=${CMAKE_CURRENT_BINARY_DIR}/dep_spdlog-prefix/src/dep_spdlog-install - -DSPDLOG_BUILD_EXAMPLES=OFF - -DSPDLOG_BUILD_BENCH=OFF - -DSPDLOG_BUILD_TESTS=OFF - TEST_COMMAND "" -) - -include_directories(${CMAKE_CURRENT_BINARY_DIR}/dep_spdlog-prefix/src/dep_spdlog/include) - # Fetch and include GTest # Adapted from Google's documentation: https://google.github.io/googletest/quickstart-cmake.html#set-up-a-project include(FetchContent) diff --git a/flight_sql/flight_sql_driver.cc b/flight_sql/flight_sql_driver.cc index 5cabfe79..8e731d3b 100644 --- a/flight_sql/flight_sql_driver.cc +++ b/flight_sql/flight_sql_driver.cc @@ -5,41 +5,14 @@ */ #include "flight_sql_connection.h" -#include "odbcabstraction/utils.h" -#include #include -#include #include - -#define DEFAULT_MAXIMUM_FILE_SIZE 16777216 - namespace driver { namespace flight_sql { using odbcabstraction::Connection; using odbcabstraction::OdbcVersion; -using odbcabstraction::LogLevel; -using odbcabstraction::SPDLogger; - -namespace { - LogLevel ToLogLevel(int64_t level) { - switch (level) { - case 0: - return LogLevel::TRACE; - case 1: - return LogLevel::DEBUG; - case 2: - return LogLevel::INFO; - case 3: - return LogLevel::WARN; - case 4: - return LogLevel::ERROR; - default: - return LogLevel::OFF; - } - } -} FlightSqlDriver::FlightSqlDriver() : diagnostics_("Apache Arrow", "Flight SQL", OdbcVersion::V_3), @@ -58,52 +31,5 @@ odbcabstraction::Diagnostics &FlightSqlDriver::GetDiagnostics() { void FlightSqlDriver::SetVersion(std::string version) { version_ = std::move(version); } - -void FlightSqlDriver::RegisterLog(const std::string &configFileName) { - odbcabstraction::PropertyMap propertyMap; - driver::odbcabstraction::ReadConfigFile(propertyMap, configFileName); - - auto log_enable_iterator = propertyMap.find(SPDLogger::LOG_ENABLED); - - auto log_enabled = log_enable_iterator != propertyMap.end() ? - odbcabstraction::AsBool(log_enable_iterator->second) : false; - - auto log_path_iterator = propertyMap.find(SPDLogger::LOG_PATH); - - auto log_path = - log_path_iterator != propertyMap.end() ? log_path_iterator->second : ""; - - if (*log_enabled && !log_path.empty()) { - auto log_level_iterator = propertyMap.find(SPDLogger::LOG_LEVEL); - - auto log_level = - log_level_iterator != propertyMap.end() ? std::stoi(log_level_iterator->second) : 1; - - auto maximum_file_size_iterator = propertyMap.find(SPDLogger::MAXIMUM_FILE_SIZE); - - auto maximum_file_size = maximum_file_size_iterator != propertyMap.end() ? - std::stoi(maximum_file_size_iterator->second) : DEFAULT_MAXIMUM_FILE_SIZE; - - auto maximum_file_quantity_iterator = propertyMap. - find(SPDLogger::FILE_QUANTITY); - - auto maximum_file_quantity = - maximum_file_quantity_iterator != propertyMap.end() ? std::stoi( - maximum_file_quantity_iterator->second) : 1; - - std::unique_ptr logger (new odbcabstraction::SPDLogger()); - - logger->init(maximum_file_quantity, maximum_file_size, - log_path, ToLogLevel(log_level)); - odbcabstraction::Logger::SetInstance(std::move(logger)); - } else { - std::unique_ptr logger (new odbcabstraction::SPDLogger()); - - logger->init(1, 1, - log_path, LogLevel::OFF); - odbcabstraction::Logger::SetInstance(std::move(logger)); - } -} - } // namespace flight_sql } // namespace driver diff --git a/flight_sql/include/flight_sql/flight_sql_driver.h b/flight_sql/include/flight_sql/flight_sql_driver.h index 52078d57..e55a7126 100644 --- a/flight_sql/include/flight_sql/flight_sql_driver.h +++ b/flight_sql/include/flight_sql/flight_sql_driver.h @@ -26,8 +26,6 @@ class FlightSqlDriver : public odbcabstraction::Driver { odbcabstraction::Diagnostics &GetDiagnostics() override; void SetVersion(std::string version) override; - - void RegisterLog(const std::string &configFileName) override; }; }; // namespace flight_sql diff --git a/odbcabstraction/CMakeLists.txt b/odbcabstraction/CMakeLists.txt index 85a25053..0ffd493a 100644 --- a/odbcabstraction/CMakeLists.txt +++ b/odbcabstraction/CMakeLists.txt @@ -12,9 +12,7 @@ add_library(odbcabstraction include/odbcabstraction/diagnostics.h include/odbcabstraction/error_codes.h include/odbcabstraction/exceptions.h - include/odbcabstraction/logger.h include/odbcabstraction/platform.h - include/odbcabstraction/spd_logger.h include/odbcabstraction/types.h include/odbcabstraction/utils.h include/odbcabstraction/odbc_impl/AttributeUtils.h @@ -34,11 +32,7 @@ add_library(odbcabstraction diagnostics.cc encoding.cc exceptions.cc - logger.cc - spd_logger.cc utils.cc - whereami.h - whereami.cc odbc_impl/ODBCConnection.cc odbc_impl/ODBCDescriptor.cc odbc_impl/ODBCEnvironment.cc @@ -51,7 +45,4 @@ set_target_properties(odbcabstraction LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/$/lib RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/$/lib ) - -add_dependencies(odbcabstraction dep_spdlog) - target_link_libraries(odbcabstraction) diff --git a/odbcabstraction/include/odbcabstraction/logger.h b/odbcabstraction/include/odbcabstraction/logger.h deleted file mode 100644 index aac28633..00000000 --- a/odbcabstraction/include/odbcabstraction/logger.h +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright (C) 2020-2022 Dremio Corporation - * - * See "LICENSE" for license information. - */ - -#pragma once - -#include -#include -#include - -#include -#include - -#include - -#define LOG_DEBUG(...) driver::odbcabstraction::Logger::GetInstance()->log(driver::odbcabstraction::LogLevel::DEBUG, __VA_ARGS__); -#define LOG_INFO(...) driver::odbcabstraction::Logger::GetInstance()->log(driver::odbcabstraction::LogLevel::INFO, __VA_ARGS__); -#define LOG_ERROR(...) driver::odbcabstraction::Logger::GetInstance()->log(driver::odbcabstraction::LogLevel::ERROR, __VA_ARGS__); -#define LOG_TRACE(...) driver::odbcabstraction::Logger::GetInstance()->log(driver::odbcabstraction::LogLevel::TRACE, __VA_ARGS__); -#define LOG_WARN(...) driver::odbcabstraction::Logger::GetInstance()->log(driver::odbcabstraction::LogLevel::WARN, __VA_ARGS__); - -namespace driver { -namespace odbcabstraction { - -enum LogLevel { TRACE, DEBUG, INFO, WARN, ERROR, OFF }; - -class Logger { -protected: - Logger() = default; - -public: - static Logger *GetInstance(); - static void SetInstance(std::unique_ptr logger); - - virtual ~Logger() = default; - - virtual void log(LogLevel level, const std::string &message) = 0; - - virtual bool checkLogLevel(LogLevel called) = 0; - - template - void log(LogLevel level, fmt::format_string fmt, Args &&... args) { - if(checkLogLevel(level)) { - log(level, fmt::format(fmt, args...)); - } - }; -}; - -} // namespace odbcabstraction -} // namespace driver diff --git a/odbcabstraction/include/odbcabstraction/spd_logger.h b/odbcabstraction/include/odbcabstraction/spd_logger.h deleted file mode 100644 index 2d4a3e75..00000000 --- a/odbcabstraction/include/odbcabstraction/spd_logger.h +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (C) 2020-2022 Dremio Corporation - * - * See "LICENSE" for license information. - */ - -#pragma once - -#include "odbcabstraction/logger.h" - -#include -#include - -#include - -namespace driver { -namespace odbcabstraction { - -class SPDLogger : public Logger { -protected: - std::shared_ptr logger_; - -public: - static const std::string LOG_LEVEL; - static const std::string LOG_PATH; - static const std::string MAXIMUM_FILE_SIZE; - static const std::string FILE_QUANTITY; - static const std::string LOG_ENABLED; - - SPDLogger() = default; - ~SPDLogger(); - SPDLogger(SPDLogger &other) = delete; - - void operator=(const SPDLogger &) = delete; - void init(int64_t fileQuantity, int64_t maxFileSize, - const std::string &fileNamePrefix, LogLevel level); - - void log(LogLevel level, const std::string &message) override; - - bool checkLogLevel(LogLevel called) override; -}; - -} // namespace odbcabstraction -} // namespace driver diff --git a/odbcabstraction/include/odbcabstraction/spi/driver.h b/odbcabstraction/include/odbcabstraction/spi/driver.h index b5968d30..974704f7 100644 --- a/odbcabstraction/include/odbcabstraction/spi/driver.h +++ b/odbcabstraction/include/odbcabstraction/spi/driver.h @@ -35,9 +35,6 @@ class Driver { /// \brief Sets the driver version. virtual void SetVersion(std::string version) = 0; - - /// \brief Register a log to be used by the system. - virtual void RegisterLog(const std::string &configFileName) = 0; }; } // namespace odbcabstraction diff --git a/odbcabstraction/include/odbcabstraction/utils.h b/odbcabstraction/include/odbcabstraction/utils.h index a3f19c96..a5617ada 100644 --- a/odbcabstraction/include/odbcabstraction/utils.h +++ b/odbcabstraction/include/odbcabstraction/utils.h @@ -7,15 +7,11 @@ #pragma once #include -#include #include namespace driver { namespace odbcabstraction { -typedef std::map - PropertyMap; - using driver::odbcabstraction::Connection; /// Parse a string value to a boolean. @@ -40,8 +36,5 @@ boost::optional AsBool(const Connection::ConnPropertyMap& connPropertyMap, /// \exception std::out_of_range exception from \link std::stoi \endlink boost::optional AsInt32(int32_t min_value, const Connection::ConnPropertyMap& connPropertyMap, const std::string& property_name); - - -void ReadConfigFile(PropertyMap &properties, const std::string &configFileName); } // namespace odbcabstraction } // namespace driver diff --git a/odbcabstraction/logger.cc b/odbcabstraction/logger.cc deleted file mode 100644 index 16ac682d..00000000 --- a/odbcabstraction/logger.cc +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright (C) 2020-2022 Dremio Corporation - * - * See "LICENSE" for license information. - */ - - -#include - -namespace driver { -namespace odbcabstraction { - -static std::unique_ptr odbc_logger_ = nullptr; - -Logger *Logger::GetInstance() { - return odbc_logger_.get(); -} - -void Logger::SetInstance(std::unique_ptrlogger) { - odbc_logger_ = std::move(logger); -} - -} -} diff --git a/odbcabstraction/spd_logger.cc b/odbcabstraction/spd_logger.cc deleted file mode 100644 index 8fcf1ac0..00000000 --- a/odbcabstraction/spd_logger.cc +++ /dev/null @@ -1,146 +0,0 @@ -/* - * Copyright (C) 2020-2022 Dremio Corporation - * - * See "LICENSE" for license information. - */ - -#include "odbcabstraction/spd_logger.h" - -#include "odbcabstraction/logger.h" - -#include -#include -#include - -#include -#include - -namespace driver { -namespace odbcabstraction { - -const std::string SPDLogger::LOG_LEVEL = "LogLevel"; -const std::string SPDLogger::LOG_PATH= "LogPath"; -const std::string SPDLogger::MAXIMUM_FILE_SIZE= "MaximumFileSize"; -const std::string SPDLogger::FILE_QUANTITY= "FileQuantity"; -const std::string SPDLogger::LOG_ENABLED= "LogEnabled"; - -namespace { -std::function shutdown_handler; -void signal_handler(int signal) { - shutdown_handler(signal); -} - -typedef void (*Handler)(int signum); - -Handler old_sigint_handler = SIG_IGN; -Handler old_sigsegv_handler = SIG_IGN; -Handler old_sigabrt_handler = SIG_IGN; -Handler old_sigkill_handler = SIG_IGN; - -Handler GetHandlerFromSignal(int signum) { - switch (signum) { - case(SIGINT): - return old_sigint_handler; - case(SIGSEGV): - return old_sigsegv_handler; - case(SIGABRT): - return old_sigabrt_handler; - case(SIGKILL): - return old_sigkill_handler; - } -} - -void SetSignalHandler(int signum) { - Handler old = signal(signum, SIG_IGN); - if (old != SIG_IGN) { - auto old_handler = GetHandlerFromSignal(signum); - old_handler = old; - } - signal(signum, signal_handler); -} - -void ResetSignalHandler(int signum) { - Handler actual_handler = signal(signum, SIG_IGN); - if (actual_handler == signal_handler) { - signal(signum, GetHandlerFromSignal(signum)); - } -} - - -spdlog::level::level_enum ToSpdLogLevel(LogLevel level) { - switch (level) { - case TRACE: - return spdlog::level::trace; - case DEBUG: - return spdlog::level::debug; - case INFO: - return spdlog::level::info; - case WARN: - return spdlog::level::warn; - case ERROR: - return spdlog::level::err; - default: - return spdlog::level::off; - } -} -} // namespace - -void SPDLogger::init(int64_t fileQuantity, int64_t maxFileSize, - const std::string &fileNamePrefix, LogLevel level) { - logger_ = spdlog::rotating_logger_mt( - "ODBC Logger", fileNamePrefix, maxFileSize, fileQuantity); - - logger_->set_level(ToSpdLogLevel(level)); - - if (level != LogLevel::OFF) { - SetSignalHandler(SIGINT); - SetSignalHandler(SIGSEGV); - SetSignalHandler(SIGABRT); - SetSignalHandler(SIGKILL); - shutdown_handler = [&](int signal) { - logger_->flush(); - spdlog::shutdown(); - auto handler = GetHandlerFromSignal(signal); - handler(signal); - }; - } -} - -void SPDLogger::log(LogLevel level, const std::string &message) { - switch (level) { - case DEBUG: - logger_->debug(message); - break; - case TRACE: - logger_->trace(message); - break; - case INFO: - logger_->info(message); - break; - case WARN: - logger_->warn(message); - break; - case ERROR: - logger_->error(message); - break; - } -} - -SPDLogger::~SPDLogger() { - ResetSignalHandler(SIGINT); - ResetSignalHandler(SIGSEGV); - ResetSignalHandler(SIGABRT); - ResetSignalHandler(SIGKILL); -} - -bool SPDLogger::checkLogLevel(LogLevel called) { - auto set = logger_->level(); - if (set == spdlog::level::off) { - return false; - } else { - return set <= ToSpdLogLevel(called); - } -} - -} // namespace odbcabstraction -} // namespace driver diff --git a/odbcabstraction/utils.cc b/odbcabstraction/utils.cc index 831c63c2..8256cb23 100644 --- a/odbcabstraction/utils.cc +++ b/odbcabstraction/utils.cc @@ -5,16 +5,8 @@ */ #include -#include "whereami.h" - -#include -#include #include -#include -#include - -#define CONFIG_LOG_NAME "/arrow-odbc.ini" namespace driver { namespace odbcabstraction { @@ -53,46 +45,5 @@ boost::optional AsInt32(int32_t min_value, const Connection::ConnProper return boost::none; } -std::string GetModulePath() { - std::vector path; - int length, dirname_length; - length = wai_getModulePath(NULL, 0, &dirname_length); - - if (length != 0) { - path.resize(length); - wai_getModulePath(path.data(), length, &dirname_length); - } else { - throw DriverException("Could not find module path."); - } - - return std::string(path.begin(), path.begin() + dirname_length); -} - -void ReadConfigFile(PropertyMap &properties, const std::string &configFileName) { - auto config_path = GetModulePath(); - - std::ifstream myfile; - myfile.open(config_path + "/" + configFileName); - - if (myfile.fail()) { - throw DriverException("Could not read the driver config file."); - } - - std::string temp_config; - - boost::char_separator separator("="); - while(myfile.good()) { - myfile >> temp_config; - boost::tokenizer< boost::char_separator> tokenizer(temp_config, separator); - - auto iterator = tokenizer.begin(); - - std::string key = *iterator; - std::string value = *++iterator; - - properties[key] = std::move(value); - } -} - } // namespace odbcabstraction } // namespace driver diff --git a/odbcabstraction/whereami.cc b/odbcabstraction/whereami.cc deleted file mode 100644 index 39324d16..00000000 --- a/odbcabstraction/whereami.cc +++ /dev/null @@ -1,804 +0,0 @@ -// (‑●‑●)> dual licensed under the WTFPL v2 and MIT licenses -// without any warranty. -// by Gregory Pakosz (@gpakosz) -// https://github.com/gpakosz/whereami - -// in case you want to #include "whereami.c" in a larger compilation unit -#if !defined(WHEREAMI_H) -#include "whereami.h" -#endif - -#ifdef __cplusplus -extern "C" { -#endif - -#if defined(__linux__) || defined(__CYGWIN__) -#undef _DEFAULT_SOURCE -#define _DEFAULT_SOURCE -#elif defined(__APPLE__) -#undef _DARWIN_C_SOURCE -#define _DARWIN_C_SOURCE -#define _DARWIN_BETTER_REALPATH -#endif - -#if !defined(WAI_MALLOC) || !defined(WAI_FREE) || !defined(WAI_REALLOC) -#include -#endif - -#if !defined(WAI_MALLOC) -#define WAI_MALLOC(size) malloc(size) -#endif - -#if !defined(WAI_FREE) -#define WAI_FREE(p) free(p) -#endif - -#if !defined(WAI_REALLOC) -#define WAI_REALLOC(p, size) realloc(p, size) -#endif - -#ifndef WAI_NOINLINE -#if defined(_MSC_VER) -#define WAI_NOINLINE __declspec(noinline) -#elif defined(__GNUC__) -#define WAI_NOINLINE __attribute__((noinline)) -#else -#error unsupported compiler -#endif -#endif - -#if defined(_MSC_VER) -#define WAI_RETURN_ADDRESS() _ReturnAddress() -#elif defined(__GNUC__) -#define WAI_RETURN_ADDRESS() __builtin_extract_return_addr(__builtin_return_address(0)) -#else -#error unsupported compiler -#endif - -#if defined(_WIN32) - -#ifndef WIN32_LEAN_AND_MEAN -#define WIN32_LEAN_AND_MEAN -#endif -#if defined(_MSC_VER) -#pragma warning(push, 3) -#endif -#include -#include -#if defined(_MSC_VER) -#pragma warning(pop) -#endif -#include - -static int WAI_PREFIX(getModulePath_)(HMODULE module, char* out, int capacity, int* dirname_length) -{ - wchar_t buffer1[MAX_PATH]; - wchar_t buffer2[MAX_PATH]; - wchar_t* path = NULL; - int length = -1; - bool ok; - - for (ok = false; !ok; ok = true) - { - DWORD size; - int length_, length__; - - size = GetModuleFileNameW(module, buffer1, sizeof(buffer1) / sizeof(buffer1[0])); - - if (size == 0) - break; - else if (size == (DWORD)(sizeof(buffer1) / sizeof(buffer1[0]))) - { - DWORD size_ = size; - do - { - wchar_t* path_; - - path_ = (wchar_t*)WAI_REALLOC(path, sizeof(wchar_t) * size_ * 2); - if (!path_) - break; - size_ *= 2; - path = path_; - size = GetModuleFileNameW(module, path, size_); - } - while (size == size_); - - if (size == size_) - break; - } - else - path = buffer1; - - if (!_wfullpath(buffer2, path, MAX_PATH)) - break; - length_ = (int)wcslen(buffer2); - length__ = WideCharToMultiByte(CP_UTF8, 0, buffer2, length_ , out, capacity, NULL, NULL); - - if (length__ == 0) - length__ = WideCharToMultiByte(CP_UTF8, 0, buffer2, length_, NULL, 0, NULL, NULL); - if (length__ == 0) - break; - - if (length__ <= capacity && dirname_length) - { - int i; - - for (i = length__ - 1; i >= 0; --i) - { - if (out[i] == '\\') - { - *dirname_length = i; - break; - } - } - } - - length = length__; - } - - if (path != buffer1) - WAI_FREE(path); - - return ok ? length : -1; -} - -WAI_NOINLINE WAI_FUNCSPEC -int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length) -{ - return WAI_PREFIX(getModulePath_)(NULL, out, capacity, dirname_length); -} - -WAI_NOINLINE WAI_FUNCSPEC -int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length) -{ - HMODULE module; - int length = -1; - -#if defined(_MSC_VER) -#pragma warning(push) -#pragma warning(disable: 4054) -#endif - if (GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCTSTR)WAI_RETURN_ADDRESS(), &module)) -#if defined(_MSC_VER) -#pragma warning(pop) -#endif - { - length = WAI_PREFIX(getModulePath_)(module, out, capacity, dirname_length); - } - - return length; -} - -#elif defined(__linux__) || defined(__CYGWIN__) || defined(__sun) || defined(WAI_USE_PROC_SELF_EXE) - -#include -#include -#include -#if defined(__linux__) -#include -#else -#include -#endif -#ifndef __STDC_FORMAT_MACROS -#define __STDC_FORMAT_MACROS -#endif -#include -#include - -#if !defined(WAI_PROC_SELF_EXE) -#if defined(__sun) -#define WAI_PROC_SELF_EXE "/proc/self/path/a.out" -#else -#define WAI_PROC_SELF_EXE "/proc/self/exe" -#endif -#endif - -WAI_FUNCSPEC -int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length) -{ - char buffer[PATH_MAX]; - char* resolved = NULL; - int length = -1; - bool ok; - - for (ok = false; !ok; ok = true) - { - resolved = realpath(WAI_PROC_SELF_EXE, buffer); - if (!resolved) - break; - - length = (int)strlen(resolved); - if (length <= capacity) - { - memcpy(out, resolved, length); - - if (dirname_length) - { - int i; - - for (i = length - 1; i >= 0; --i) - { - if (out[i] == '/') - { - *dirname_length = i; - break; - } - } - } - } - } - - return ok ? length : -1; -} - -#if !defined(WAI_PROC_SELF_MAPS_RETRY) -#define WAI_PROC_SELF_MAPS_RETRY 5 -#endif - -#if !defined(WAI_PROC_SELF_MAPS) -#if defined(__sun) -#define WAI_PROC_SELF_MAPS "/proc/self/map" -#else -#define WAI_PROC_SELF_MAPS "/proc/self/maps" -#endif -#endif - -#if defined(__ANDROID__) || defined(ANDROID) -#include -#include -#include -#endif -#include - -WAI_NOINLINE WAI_FUNCSPEC -int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length) -{ - int length = -1; - FILE* maps = NULL; - - for (int r = 0; r < WAI_PROC_SELF_MAPS_RETRY; ++r) - { - maps = fopen(WAI_PROC_SELF_MAPS, "r"); - if (!maps) - break; - - for (;;) - { - char buffer[PATH_MAX < 1024 ? 1024 : PATH_MAX]; - uint64_t low, high; - char perms[5]; - uint64_t offset; - uint32_t major, minor; - char path[PATH_MAX]; - uint32_t inode; - - if (!fgets(buffer, sizeof(buffer), maps)) - break; - - if (sscanf(buffer, "%" PRIx64 "-%" PRIx64 " %s %" PRIx64 " %x:%x %u %s\n", &low, &high, perms, &offset, &major, &minor, &inode, path) == 8) - { - uint64_t addr = (uintptr_t)WAI_RETURN_ADDRESS(); - if (low <= addr && addr <= high) - { - char* resolved; - - resolved = realpath(path, buffer); - if (!resolved) - break; - - length = (int)strlen(resolved); -#if defined(__ANDROID__) || defined(ANDROID) - if (length > 4 - &&buffer[length - 1] == 'k' - &&buffer[length - 2] == 'p' - &&buffer[length - 3] == 'a' - &&buffer[length - 4] == '.') - { - int fd = open(path, O_RDONLY); - if (fd == -1) - { - length = -1; // retry - break; - } - - char* begin = (char*)mmap(0, offset, PROT_READ, MAP_SHARED, fd, 0); - if (begin == MAP_FAILED) - { - close(fd); - length = -1; // retry - break; - } - - char* p = begin + offset - 30; // minimum size of local file header - while (p >= begin) // scan backwards - { - if (*((uint32_t*)p) == 0x04034b50UL) // local file header signature found - { - uint16_t length_ = *((uint16_t*)(p + 26)); - - if (length + 2 + length_ < (int)sizeof(buffer)) - { - memcpy(&buffer[length], "!/", 2); - memcpy(&buffer[length + 2], p + 30, length_); - length += 2 + length_; - } - - break; - } - - --p; - } - - munmap(begin, offset); - close(fd); - } -#endif - if (length <= capacity) - { - memcpy(out, resolved, length); - - if (dirname_length) - { - int i; - - for (i = length - 1; i >= 0; --i) - { - if (out[i] == '/') - { - *dirname_length = i; - break; - } - } - } - } - - break; - } - } - } - - fclose(maps); - maps = NULL; - - if (length != -1) - break; - } - - return length; -} - -#elif defined(__APPLE__) - -#include -#include -#include -#include -#include -#include - -WAI_FUNCSPEC -int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length) -{ - char buffer1[PATH_MAX]; - char buffer2[PATH_MAX]; - char* path = buffer1; - char* resolved = NULL; - int length = -1; - bool ok; - - for (ok = false; !ok; ok = true) - { - uint32_t size = (uint32_t)sizeof(buffer1); - if (_NSGetExecutablePath(path, &size) == -1) - { - path = (char*)WAI_MALLOC(size); - if (!_NSGetExecutablePath(path, &size)) - break; - } - - resolved = realpath(path, buffer2); - if (!resolved) - break; - - length = (int)strlen(resolved); - if (length <= capacity) - { - memcpy(out, resolved, length); - - if (dirname_length) - { - int i; - - for (i = length - 1; i >= 0; --i) - { - if (out[i] == '/') - { - *dirname_length = i; - break; - } - } - } - } - } - - if (path != buffer1) - WAI_FREE(path); - - return ok ? length : -1; -} - -WAI_NOINLINE WAI_FUNCSPEC -int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length) -{ - char buffer[PATH_MAX]; - char* resolved = NULL; - int length = -1; - - for(;;) - { - Dl_info info; - - if (dladdr(WAI_RETURN_ADDRESS(), &info)) - { - resolved = realpath(info.dli_fname, buffer); - if (!resolved) - break; - - length = (int)strlen(resolved); - if (length <= capacity) - { - memcpy(out, resolved, length); - - if (dirname_length) - { - int i; - - for (i = length - 1; i >= 0; --i) - { - if (out[i] == '/') - { - *dirname_length = i; - break; - } - } - } - } - } - - break; - } - - return length; -} - -#elif defined(__QNXNTO__) - -#include -#include -#include -#include -#include -#include - -#if !defined(WAI_PROC_SELF_EXE) -#define WAI_PROC_SELF_EXE "/proc/self/exefile" -#endif - -WAI_FUNCSPEC -int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length) -{ - char buffer1[PATH_MAX]; - char buffer2[PATH_MAX]; - char* resolved = NULL; - FILE* self_exe = NULL; - int length = -1; - bool ok; - - for (ok = false; !ok; ok = true) - { - self_exe = fopen(WAI_PROC_SELF_EXE, "r"); - if (!self_exe) - break; - - if (!fgets(buffer1, sizeof(buffer1), self_exe)) - break; - - resolved = realpath(buffer1, buffer2); - if (!resolved) - break; - - length = (int)strlen(resolved); - if (length <= capacity) - { - memcpy(out, resolved, length); - - if (dirname_length) - { - int i; - - for (i = length - 1; i >= 0; --i) - { - if (out[i] == '/') - { - *dirname_length = i; - break; - } - } - } - } - } - - fclose(self_exe); - - return ok ? length : -1; -} - -WAI_FUNCSPEC -int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length) -{ - char buffer[PATH_MAX]; - char* resolved = NULL; - int length = -1; - - for(;;) - { - Dl_info info; - - if (dladdr(WAI_RETURN_ADDRESS(), &info)) - { - resolved = realpath(info.dli_fname, buffer); - if (!resolved) - break; - - length = (int)strlen(resolved); - if (length <= capacity) - { - memcpy(out, resolved, length); - - if (dirname_length) - { - int i; - - for (i = length - 1; i >= 0; --i) - { - if (out[i] == '/') - { - *dirname_length = i; - break; - } - } - } - } - } - - break; - } - - return length; -} - -#elif defined(__DragonFly__) || defined(__FreeBSD__) || \ - defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) - -#include -#include -#include -#include -#include -#include -#include - -#if defined(__OpenBSD__) - -#include - -WAI_FUNCSPEC -int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length) -{ - char buffer1[4096]; - char buffer2[PATH_MAX]; - char buffer3[PATH_MAX]; - char** argv = (char**)buffer1; - char* resolved = NULL; - int length = -1; - bool ok; - - for (ok = false; !ok; ok = true) - { - int mib[4] = { CTL_KERN, KERN_PROC_ARGS, getpid(), KERN_PROC_ARGV }; - size_t size; - - if (sysctl(mib, 4, NULL, &size, NULL, 0) != 0) - break; - - if (size > sizeof(buffer1)) - { - argv = (char**)WAI_MALLOC(size); - if (!argv) - break; - } - - if (sysctl(mib, 4, argv, &size, NULL, 0) != 0) - break; - - if (strchr(argv[0], '/')) - { - resolved = realpath(argv[0], buffer2); - if (!resolved) - break; - } - else - { - const char* PATH = getenv("PATH"); - if (!PATH) - break; - - size_t argv0_length = strlen(argv[0]); - - const char* begin = PATH; - while (1) - { - const char* separator = strchr(begin, ':'); - const char* end = separator ? separator : begin + strlen(begin); - - if (end - begin > 0) - { - if (*(end -1) == '/') - --end; - - if (((end - begin) + 1 + argv0_length + 1) <= sizeof(buffer2)) - { - memcpy(buffer2, begin, end - begin); - buffer2[end - begin] = '/'; - memcpy(buffer2 + (end - begin) + 1, argv[0], argv0_length + 1); - - resolved = realpath(buffer2, buffer3); - if (resolved) - break; - } - } - - if (!separator) - break; - - begin = ++separator; - } - - if (!resolved) - break; - } - - length = (int)strlen(resolved); - if (length <= capacity) - { - memcpy(out, resolved, length); - - if (dirname_length) - { - int i; - - for (i = length - 1; i >= 0; --i) - { - if (out[i] == '/') - { - *dirname_length = i; - break; - } - } - } - } - } - - if (argv != (char**)buffer1) - WAI_FREE(argv); - - return ok ? length : -1; -} - -#else - -WAI_FUNCSPEC -int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length) -{ - char buffer1[PATH_MAX]; - char buffer2[PATH_MAX]; - char* path = buffer1; - char* resolved = NULL; - int length = -1; - bool ok; - - for (ok = false; !ok; ok = true) - { -#if defined(__NetBSD__) - int mib[4] = { CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME }; -#else - int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 }; -#endif - size_t size = sizeof(buffer1); - - if (sysctl(mib, 4, path, &size, NULL, 0) != 0) - break; - - resolved = realpath(path, buffer2); - if (!resolved) - break; - - length = (int)strlen(resolved); - if (length <= capacity) - { - memcpy(out, resolved, length); - - if (dirname_length) - { - int i; - - for (i = length - 1; i >= 0; --i) - { - if (out[i] == '/') - { - *dirname_length = i; - break; - } - } - } - } - } - - return ok ? length : -1; -} - -#endif - -WAI_NOINLINE WAI_FUNCSPEC -int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length) -{ - char buffer[PATH_MAX]; - char* resolved = NULL; - int length = -1; - - for(;;) - { - Dl_info info; - - if (dladdr(WAI_RETURN_ADDRESS(), &info)) - { - resolved = realpath(info.dli_fname, buffer); - if (!resolved) - break; - - length = (int)strlen(resolved); - if (length <= capacity) - { - memcpy(out, resolved, length); - - if (dirname_length) - { - int i; - - for (i = length - 1; i >= 0; --i) - { - if (out[i] == '/') - { - *dirname_length = i; - break; - } - } - } - } - } - - break; - } - - return length; -} - -#else - -#error unsupported platform - -#endif - -#ifdef __cplusplus -} -#endif diff --git a/odbcabstraction/whereami.h b/odbcabstraction/whereami.h deleted file mode 100644 index ca62d674..00000000 --- a/odbcabstraction/whereami.h +++ /dev/null @@ -1,67 +0,0 @@ -// (‑●‑●)> dual licensed under the WTFPL v2 and MIT licenses -// without any warranty. -// by Gregory Pakosz (@gpakosz) -// https://github.com/gpakosz/whereami - -#ifndef WHEREAMI_H -#define WHEREAMI_H - -#ifdef __cplusplus -extern "C" { -#endif - -#ifndef WAI_FUNCSPEC -#define WAI_FUNCSPEC -#endif -#ifndef WAI_PREFIX -#define WAI_PREFIX(function) wai_##function -#endif - -/** - * Returns the path to the current executable. - * - * Usage: - * - first call `int length = wai_getExecutablePath(NULL, 0, NULL);` to - * retrieve the length of the path - * - allocate the destination buffer with `path = (char*)malloc(length + 1);` - * - call `wai_getExecutablePath(path, length, NULL)` again to retrieve the - * path - * - add a terminal NUL character with `path[length] = '\0';` - * - * @param out destination buffer, optional - * @param capacity destination buffer capacity - * @param dirname_length optional recipient for the length of the dirname part - * of the path. - * - * @return the length of the executable path on success (without a terminal NUL - * character), otherwise `-1` - */ -WAI_FUNCSPEC -int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length); - -/** - * Returns the path to the current module - * - * Usage: - * - first call `int length = wai_getModulePath(NULL, 0, NULL);` to retrieve - * the length of the path - * - allocate the destination buffer with `path = (char*)malloc(length + 1);` - * - call `wai_getModulePath(path, length, NULL)` again to retrieve the path - * - add a terminal NUL character with `path[length] = '\0';` - * - * @param out destination buffer, optional - * @param capacity destination buffer capacity - * @param dirname_length optional recipient for the length of the dirname part - * of the path. - * - * @return the length of the module path on success (without a terminal NUL - * character), otherwise `-1` - */ -WAI_FUNCSPEC -int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length); - -#ifdef __cplusplus -} -#endif - -#endif // #ifndef WHEREAMI_H diff --git a/vcpkg.json b/vcpkg.json index 519d6441..88edaa56 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -20,7 +20,6 @@ "protobuf", "zlib", "re2", - "spdlog", "grpc", "utf8proc", "zlib",