1/******************************************************
2Copyright (c) 2011-2013 Percona LLC and/or its affiliates.
3
4Common declarations for XtraBackup.
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; version 2 of the License.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
17Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
18
19*******************************************************/
20
21#ifndef XB_COMMON_H
22#define XB_COMMON_H
23
24#include <my_global.h>
25#include <mysql_version.h>
26#include <fcntl.h>
27#include <stdarg.h>
28#include <my_sys.h>
29
30
31/** Determine if (i) is a user tablespace id or not. */
32# define fil_is_user_tablespace_id(i) (i != 0 \
33 && !srv_is_undo_tablespace(i))
34
35#ifdef _MSC_VER
36#define stat _stati64
37#define PATH_MAX MAX_PATH
38#endif
39
40#ifndef HAVE_VASPRINTF
41static inline int vasprintf(char **strp, const char *fmt, va_list args)
42{
43 int len;
44#ifdef _MSC_VER
45 len = _vscprintf(fmt, args);
46#else
47 len = vsnprintf(NULL, 0, fmt, args);
48#endif
49 if (len < 0)
50 {
51 return -1;
52 }
53 *strp = (char *)malloc(len + 1);
54 if (!*strp)
55 {
56 return -1;
57 }
58 vsprintf(*strp, fmt, args);
59 return len;
60}
61
62static inline int asprintf(char **strp, const char *fmt,...)
63{
64 va_list args;
65 va_start(args, fmt);
66 int len = vasprintf(strp, fmt, args);
67 va_end(args);
68 return len;
69}
70#endif
71
72#define xb_a(expr) \
73 do { \
74 if (!(expr)) { \
75 msg("Assertion \"%s\" failed at %s:%lu\n", \
76 #expr, __FILE__, (ulong) __LINE__); \
77 abort(); \
78 } \
79 } while (0);
80
81#ifdef XB_DEBUG
82#define xb_ad(expr) xb_a(expr)
83#else
84#define xb_ad(expr)
85#endif
86
87#define XB_DELTA_INFO_SUFFIX ".meta"
88
89static inline int msg(const char *fmt, ...) ATTRIBUTE_FORMAT(printf, 1, 2);
90static inline int msg(const char *fmt, ...)
91{
92 int result;
93 va_list args;
94
95 va_start(args, fmt);
96 result = vfprintf(stderr, fmt, args);
97 va_end(args);
98
99 return result;
100}
101
102static inline int msg_ts(const char *fmt, ...) ATTRIBUTE_FORMAT(printf, 1, 2);
103static inline int msg_ts(const char *fmt, ...)
104{
105 int result;
106 time_t t = time(NULL);
107 char date[100];
108 char *line;
109 va_list args;
110
111 strftime(date, sizeof(date), "%y%m%d %H:%M:%S", localtime(&t));
112
113 va_start(args, fmt);
114 result = vasprintf(&line, fmt, args);
115 va_end(args);
116
117 if (result != -1) {
118 result = fprintf(stderr, "%s %s", date, line);
119 free(line);
120 }
121
122 return result;
123}
124
125/* Use POSIX_FADV_NORMAL when available */
126
127#ifdef POSIX_FADV_NORMAL
128# define USE_POSIX_FADVISE
129#else
130# define POSIX_FADV_NORMAL
131# define POSIX_FADV_SEQUENTIAL
132# define POSIX_FADV_DONTNEED
133# define posix_fadvise(a,b,c,d) do {} while(0)
134#endif
135
136/***********************************************************************
137Computes bit shift for a given value. If the argument is not a power
138of 2, returns 0.*/
139static inline size_t
140get_bit_shift(size_t value)
141{
142 size_t shift;
143
144 if (value == 0)
145 return 0;
146
147 for (shift = 0; !(value & 1); shift++) {
148 value >>= 1;
149 }
150 return (value >> 1) ? 0 : shift;
151}
152
153/****************************************************************************
154Read 'len' bytes from 'fd'. It is identical to my_read(..., MYF(MY_FULL_IO)),
155i.e. tries to combine partial reads into a single block of size 'len', except
156that it bails out on EOF or error, and returns the number of successfully read
157bytes instead. */
158static inline size_t
159xb_read_full(File fd, uchar *buf, size_t len)
160{
161 size_t tlen = 0;
162 size_t tbytes;
163
164 while (tlen < len) {
165 tbytes = my_read(fd, buf, len - tlen, MYF(MY_WME));
166 if (tbytes == 0 || tbytes == MY_FILE_ERROR) {
167 break;
168 }
169
170 buf += tbytes;
171 tlen += tbytes;
172 }
173
174 return tlen;
175}
176
177#endif
178