1/* Copyright (C) 2013 Codership Oy <info@codersihp.com>
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; version 2 of the License.
6
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
11
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
15 */
16
17/*! @file Helper functions to deal with GTID string representations */
18
19#include <errno.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <inttypes.h>
23
24#include "wsrep_api.h"
25
26/*!
27 * Read GTID from string
28 * @return length of GTID string representation or -EINVAL in case of error
29 */
30int
31wsrep_gtid_scan(const char* str, size_t str_len, wsrep_gtid_t* gtid)
32{
33 unsigned int offset;
34 char* endptr;
35
36 if ((offset = wsrep_uuid_scan(str, str_len, &gtid->uuid)) > 0 &&
37 offset < str_len && str[offset] == ':') {
38 ++offset;
39 if (offset < str_len)
40 {
41 errno = 0;
42 gtid->seqno = strtoll(str + offset, &endptr, 0);
43
44 if (errno == 0) {
45 offset = endptr - str;
46 return offset;
47 }
48 }
49 }
50 *gtid = WSREP_GTID_UNDEFINED;
51 return -EINVAL;
52}
53
54/*!
55 * Write GTID to string
56 * @return length of GTID stirng representation of -EMSGSIZE if string is too
57 * short
58 */
59int
60wsrep_gtid_print(const wsrep_gtid_t* gtid, char* str, size_t str_len)
61{
62 unsigned int offset, ret;
63 if ((offset = wsrep_uuid_print(&gtid->uuid, str, str_len)) > 0)
64 {
65 ret = snprintf(str + offset, str_len - offset,
66 ":%" PRId64, gtid->seqno);
67 if (ret <= str_len - offset) {
68 return (offset + ret);
69 }
70
71 }
72
73 return -EMSGSIZE;
74}
75