1
0
mirror of https://github.com/codership/wsrep-lib.git synced 2025-07-31 18:24:25 +03:00

Cleaning wsrep-API dependencies from library code

Replace all wsrep_api.h dependencies with similar code
in library implementation.

Also add gcc/g++ 4.4 to travis automation to get better
compiler coverage.
This commit is contained in:
Teemu Ollakka
2019-02-16 17:09:18 +02:00
parent 58aa3e821f
commit 29835fbec0
14 changed files with 354 additions and 30 deletions

View File

@ -15,5 +15,6 @@ add_library(wsrep-lib
server_state.cpp
thread.cpp
transaction.cpp
uuid.cpp
wsrep_provider_v26.cpp)
target_link_libraries(wsrep-lib wsrep_api_v26 pthread dl)

View File

@ -36,11 +36,47 @@ std::istream& wsrep::operator>>(std::istream& is, wsrep::gtid& gtid)
std::getline(is, id_str, ':');
long long seq;
is >> seq;
gtid = wsrep::gtid(wsrep::id(id_str), wsrep::seqno(seq));
if (!is)
{
is.clear(std::ios_base::failbit);
return is;
}
try
{
// wsrep::id constructor will throw if it cannot parse the
// id_str.
gtid = wsrep::gtid(wsrep::id(id_str), wsrep::seqno(seq));
}
catch (const wsrep::runtime_error& e)
{
// Formatting or extraction error. Clear the istream state and
// set failibit.
is.clear(std::ios_base::failbit);
}
return is;
}
ssize_t wsrep::gtid_print_to_c_str(
ssize_t wsrep::scan_from_c_str(
const char* buf, size_t buf_len, wsrep::gtid& gtid)
{
std::istringstream is(std::string(buf, buf_len));
is >> gtid;
// Whole string was consumed without failures
if (is && is.eof())
{
return buf_len;
}
// Some failure occurred
if (!is)
{
return -EINVAL;
}
// The string was not consumed completely, return current position
// of the istream.
return is.tellg();
}
ssize_t wsrep::print_to_c_str(
const wsrep::gtid& gtid, char* buf, size_t buf_len)
{
std::ostringstream os;

View File

@ -18,7 +18,7 @@
*/
#include "wsrep/id.hpp"
#include <wsrep_api.h>
#include "uuid.hpp"
#include <cctype>
#include <sstream>
@ -29,15 +29,17 @@ const wsrep::id wsrep::id::undefined_ = wsrep::id();
wsrep::id::id(const std::string& str)
: data_()
{
wsrep_uuid_t wsrep_uuid;
if (wsrep_uuid_scan(str.c_str(), str.size(), &wsrep_uuid) ==
WSREP_UUID_STR_LEN)
wsrep::uuid_t wsrep_uuid;
if (str.size() == WSREP_LIB_UUID_STR_LEN &&
wsrep::uuid_scan(str.c_str(), str.size(), &wsrep_uuid) ==
WSREP_LIB_UUID_STR_LEN)
{
std::memcpy(data_, wsrep_uuid.data, sizeof(data_));
std::memcpy(data_.buf, wsrep_uuid.data, sizeof(data_.buf));
}
else if (str.size() <= 16)
{
std::memcpy(data_, str.c_str(), str.size());
std::memcpy(data_.buf, str.c_str(), str.size());
}
else
{
@ -58,14 +60,14 @@ std::ostream& wsrep::operator<<(std::ostream& os, const wsrep::id& id)
}
else
{
char uuid_str[WSREP_UUID_STR_LEN + 1];
wsrep_uuid_t uuid;
char uuid_str[WSREP_LIB_UUID_STR_LEN + 1];
wsrep::uuid_t uuid;
std::memcpy(uuid.data, ptr, sizeof(uuid.data));
if (wsrep_uuid_print(&uuid, uuid_str, sizeof(uuid_str)) < 0)
if (wsrep::uuid_print(&uuid, uuid_str, sizeof(uuid_str)) < 0)
{
throw wsrep::runtime_error("Could not print uuid");
}
uuid_str[WSREP_UUID_STR_LEN] = '\0';
uuid_str[WSREP_LIB_UUID_STR_LEN] = '\0';
return (os << uuid_str);
}
}

74
src/uuid.cpp Normal file
View File

@ -0,0 +1,74 @@
/*
* Copyright (C) 2019 Codership Oy <info@codership.com>
*
* This file is part of wsrep-lib.
*
* Wsrep-lib is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* Wsrep-lib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with wsrep-lib. If not, see <https://www.gnu.org/licenses/>.
*/
#include "uuid.hpp"
#include <cstring>
#include <cerrno>
#include <cstdio>
#include <cctype>
int wsrep::uuid_scan (const char* str, size_t str_len, wsrep::uuid_t* uuid)
{
unsigned int uuid_len = 0;
unsigned int uuid_offt = 0;
while (uuid_len + 1 < str_len) {
/* We are skipping potential '-' after uuid_offt == 4, 6, 8, 10
* which means
* (uuid_offt >> 1) == 2, 3, 4, 5,
* which in turn means
* (uuid_offt >> 1) - 2 <= 3
* since it is always >= 0, because uuid_offt is unsigned */
if (((uuid_offt >> 1) - 2) <= 3 && str[uuid_len] == '-') {
// skip dashes after 4th, 6th, 8th and 10th positions
uuid_len += 1;
continue;
}
if (isxdigit(str[uuid_len]) && isxdigit(str[uuid_len + 1])) {
// got hex digit, scan another byte to uuid, increment uuid_offt
sscanf (str + uuid_len, "%2hhx", uuid->data + uuid_offt);
uuid_len += 2;
uuid_offt += 1;
if (sizeof (uuid->data) == uuid_offt)
return uuid_len;
}
else {
break;
}
}
*uuid = wsrep::uuid_initializer;
return -EINVAL;
}
int wsrep::uuid_print (const wsrep::uuid_t* uuid, char* str, size_t str_len)
{
if (str_len > WSREP_LIB_UUID_STR_LEN) {
const unsigned char* u = uuid->data;
return snprintf(str, str_len, "%02x%02x%02x%02x-%02x%02x-%02x%02x-"
"%02x%02x-%02x%02x%02x%02x%02x%02x",
u[ 0], u[ 1], u[ 2], u[ 3], u[ 4], u[ 5], u[ 6], u[ 7],
u[ 8], u[ 9], u[10], u[11], u[12], u[13], u[14], u[15]);
}
else {
return -EMSGSIZE;
}
}

79
src/uuid.hpp Normal file
View File

@ -0,0 +1,79 @@
/*
* Copyright (C) 2019 Codership Oy <info@codership.com>
*
* This file is part of wsrep-lib.
*
* Wsrep-lib is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* Wsrep-lib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with wsrep-lib. If not, see <https://www.gnu.org/licenses/>.
*/
/** @file uuid.hpp
*
* Helper methods to parse and print UUIDs, intended to use
* internally in wsrep-lib.
*
* The implementation is copied from wsrep-API v26.
*/
#ifndef WSREP_UUID_HPP
#define WSREP_UUID_HPP
#include "wsrep/compiler.hpp"
#include <cstddef>
/**
* Length of UUID string representation, not including terminating '\0'.
*/
#define WSREP_LIB_UUID_STR_LEN 36
namespace wsrep
{
/**
* UUID type.
*/
typedef union uuid_
{
unsigned char data[16];
size_t alignment;
} uuid_t;
static const wsrep::uuid_t uuid_initializer = {{0, }};
/**
* Read UUID from string.
*
* @param str String to read from
* @param str_len Length of string
* @param[out] UUID to read to
*
* @return Number of bytes read or negative error code in case
* of error.
*/
int uuid_scan(const char* str, size_t str_len, wsrep::uuid_t* uuid);
/**
* Write UUID to string. The caller must allocate at least
* WSREP_LIB_UUID_STR_LEN + 1 space for the output str parameter.
*
* @param uuid UUID to print
* @param str[out] Output buffer
* @param str_len Size of output buffer
*
* @return Number of chars printerd, negative error code in case of
* error.
*/
int uuid_print(const wsrep::uuid_t* uuid, char* str, size_t str_len);
}
#endif // WSREP_UUID_HPP